Commit graph

37 commits

Author SHA1 Message Date
Yu Watanabe 9e5b649655 tree-wide: use setsockopt_int() more 2018-10-19 05:52:42 +09:00
Lennart Poettering 2ff48e981e tree-wide: introduce setsockopt_int() helper and make use of it everywhere
As suggested by @heftig:

6d5e65f645 (commitcomment-30938667)
2018-10-18 19:50:29 +02:00
Lennart Poettering 6d5e65f645 tree-wide: add a single version of "static const int one = 1"
All over the place we define local variables for the various sockopts
that take a bool-like "int" value. Sometimes they are const, sometimes
static, sometimes both, sometimes neither.

Let's clean this up, introduce a common const variable "const_int_one"
(as well as one matching "const_int_zero") and use it everywhere, all
acorss the codebase.
2018-10-15 19:40:51 +02:00
Thomas Haller a507737e5c dhcp: fix assertion starting DHCP client without MAC address (#10054)
An assertion in dhcp_network_bind_raw_socket() is triggered when
starting an sd_dhcp_client without setting a MAC address first.

  - sd_dhcp_client_start()
    - client_start()
      - client_start_delayed()
        - dhcp_network_bind_raw_socket()

In that case, the arp-type and MAC address is still unset. Note that
dhcp_network_bind_raw_socket() already checks for a valid arp-type
and MAC address below, so we should just gracefully return -EINVAL.

Maybe sd_dhcp_client_start() should fail earlier when starting without
MAC address. But the failure here will be correctly propagated and
the start aborted.

Fixes: 76253e73f9
2018-09-11 09:45:47 +09:00
Lennart Poettering 810adae9e9 tree-wide: use proper unicode © instead of (C) where we can
Let's use a proper unicode copyright symbol where we can, it's prettier.

This important patch is very important.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Zbigniew Jędrzejewski-Szmek 11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00
Lennart Poettering c10d6bdb89 macro: introduce new TAKE_FD() macro
This is similar to TAKE_PTR() but operates on file descriptors, and thus
assigns -1 to the fd parameter after returning it.

Removes 60 lines from our codebase. Pretty good too I think.
2018-03-22 20:30:40 +01:00
Shawn Landden f11cba7479 libsystemd-network: fix unaligned loads (issue #7654)
should fix https://github.com/systemd/systemd/issues/7654

Also fix up mistaken endianness swap. BPF is native endianness.
2017-12-18 13:02:07 -08:00
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
bengal b1f24b75af dhcp-network: adjust sockaddr length for addresses longer than 8 bytes (#6527)
An infiniband hardware address is 20 bytes, but sockaddr_ll.sll_addr is only 8
bytes. Explicitly ensure that sockaddr_union has enough space for infiniband
addresses, even if they run over sockaddr_ll and add a macro to compute the
proper size to pass to kernel.
2017-08-08 18:55:31 +02:00
27o 3e7b9f76f5 dhcp: bind udp sockets to interfaces (#4822) 2016-12-07 02:00:05 +01:00
Susant Sahani 9faed222fc networkd: support setting dhcp client listen port (#4631)
Allow setting custom port for the DHCP client to listen on in networkd.

[DHCP]
ListenPort=6677
2016-11-10 18:34:19 -05:00
Lennart Poettering 8e38570ebe tree-wide: htonl() is weird, let's use htobe32() instead (#3538)
Super-important change, yeah!
2016-06-15 01:26:01 +02:00
Lennart Poettering 3ffd4af220 util-lib: split out fd-related operations into fd-util.[ch]
There are more than enough to deserve their own .c file, hence move them
over.
2015-10-25 13:19:18 +01:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Torstein Husebø e2acdb6b0f treewide: correct typos and use consistent "MAC" spelling 2015-02-09 14:32:49 +01:00
Dan Williams 76253e73f9 sd-dhcp-client: support non-Ethernet hardware addresses
Like Infiniband.  See RFC 4390 section 2.1 for details on DHCP
and Infiniband; chaddr is zeroed, hlen is set to 0, and htype
is set to ARPHRD_INFINIBAND because IB hardware addresses
are 20 bytes in length.
2014-10-11 12:15:54 +02:00
Lennart Poettering 088b6ba264 sd-network: make socket filter programs static const where possible
(also, fix some whitespace/indentation issues, and avoid "index" as identifier in
order to not clash against libc's "index()" call)
2014-07-29 23:53:08 +02:00
Tom Gundersen 076adf013a dhcp-network: enable IP_FREEBIND for UDP sockets
This allows the sockets to be bound to a specific address before it is configured,
also enable SO_REUSEADDR to allow multiple DHCP clients to run at the same time.
2014-07-28 10:44:51 +02:00
Michal Sekletar ffc90a1196 dhcp-network: make clear that we are ANDing Fragment offset field with mask
Reading BPF assembly written as C macros is inherently difficult. Don't
make it harder than necessary and provide clearer explanation in the
comment.
2014-07-07 15:59:13 +02:00
Michal Sekletar b064329fd8 dhcp-network: ignore IP packets with More Fragments (MF) flag set
We already ignore IP fragments, because we expect that Fragment
offset (FO) field is not set. However first fragment in a fragmented IP
flow will have all zeroes in FO field. We should ignore such packet as
well, thus we need to look at MF flag in the IP header. Checking MF flag
will filter out all except last packet in fragmented flows. Last one
will be ruled out by next check for value of FO.
2014-07-07 15:32:12 +02:00
Michal Sekletar 58587a7a0c dhcp-network: add check for DHCP.chaddr
Check that received DHCP packets actually include our MAC address in
chaddr field. BPF interpreter has 32 bit wide registers but MAC address
is 48 bits long so we have to do check in two steps.
2014-07-07 12:17:55 +02:00
Michal Sekletar 68328ed63c dhcp-network: don't bother with IP fragments 2014-06-22 12:26:09 -04:00
Tom Gundersen d6bd972d06 sd-dhcp-server: fix broadcast of DHCP packets
The destination IP address should be INADDR_BROADCAST, but was
accidentally left as INADDR_ANY.
2014-06-21 15:02:30 +02:00
Michal Sekletar 03966da2ca dhcp-network: remove TODO
There is no need to explicitly check version of L3 protocol in the
ethernet header because we bind socket with .sll_protocol set to
ETH_P_IP, thus we only receive IPv4 packets on the socket.
2014-06-18 18:14:51 +02:00
Tom Gundersen b44cd88210 sd-dhcp-server: add basic functionality for creating/destroying server instance 2014-06-13 16:53:13 +02:00
Tom Gundersen fef0e0f3b2 dhcp-network: allow UDP socket to listen on any address
For this to work nicely we need to use REUSEADDR so that more than one socket
can be open at the same time. Also, we request the ifindex to be appended
to incoming messages, so we know whence it came.
2014-06-13 16:53:13 +02:00
Tom Gundersen c3d2994b59 sd-dhcp: network - don't leak sockets on failure
Also some general cleanups
2014-05-06 22:15:46 +02:00
Tom Gundersen 85923f79e1 sd-dhcp: network - set TOS on outgoing packets
This should improve performance on busy wireless networks and the
like. Inspired by a similar change in dnsmasq.
2014-05-06 22:09:05 +02:00
Tom Gundersen 085cabf266 dhcp-network: don't pass ifindex to bind_udp_socket
UDP sockets can anyway not be bound to specific netdev's. The packages would have to be filtered
when received instead.
2014-05-06 21:58:08 +02:00
Tom Gundersen 7429b07f82 sd-dhcp-client: improve BPF
Try a bit harder to make the kernel drop packets not for us. This should reduce
the number of wakeups from n^2 to n in the number of dhcp clients, which admittedly
only makes a differenc in very extreme cases.
2014-04-06 19:42:11 +02:00
Tom Gundersen 0c79c68d93 sd-dhcp-client: eagerly drop too small packets
If they are too small to fit the IP+UDP+DHCP headers they can be of no use, so
don't waste resources parsing them. This is at the cost of losing some verbosity
in the logging.
2014-04-06 19:42:11 +02:00
Tom Gundersen bc29e507e2 sd-dhcp-client: use BPF on raw socket
Filter out everything except UDP packets destined for the DHCP client port,
this should avoid the vast majority of spurious wakeups.

Filter based on [0], with permission.

Possible improvemnts: also check for the DHCP magic cookie to drop invalid
packets. Check for our xid to filter out packets destined for other clients.

[0]: <https://github.com/ambrop72/badvpn/blob/master/dhcpclient/BDHCPClient.c#L57>
2014-04-02 14:49:51 +02:00
Tom Gundersen 66a67effcc sd-dhcp-client: --omg-optimized
Passing the protocol to socket() is redundant as it will be specified again in
bind(). Dropping the redundancy reduces the cost of bind() from ~30ms to ~0ms.
For details see [0].

networkd in a container (i.e., with next to no network latency) can now
negotiate a DHCP lease in 0.7 - 5 ms.

Thanks to Kay for help with debugging and to Daniel Borkmann for the pointer
to fix the problem.

[0]: <https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=902fefb82ef72a50c78cb4a20cc954b037a98d1c>
2014-04-01 00:32:51 +02:00
Lennart Poettering 03e334a1c7 util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:

        fd = safe_close(fd);

Which will close an fd if it is open, and reset the fd variable
correctly.

By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.
2014-03-18 19:31:34 +01:00
Tom Gundersen fe8db0c5ee sd-network: add new library
This is similar to sd-login, but exposes the state of networkd rather than logind.

Include it in libsystemd-dhcp and rename it to libsystemd-network.
2014-02-28 01:01:13 +01:00
Renamed from src/libsystemd-dhcp/dhcp-network.c (Browse further)