47 Commits

Author SHA1 Message Date
Nicholas J. Kain
b511d45c2f Change most error comparisons from == -1 to < 0. Some were not changed,
as the different negative values equate to different errors.

Tests against syscall returns and fds are very common and mostly fit
the pattern of this change.

The gain is increased range-exclusion.
2014-04-06 06:33:14 -04:00
Nicholas J. Kain
2d8654ae59 Update copyright dates. 2014-03-30 17:23:35 -04:00
Nicholas J. Kain
82d9682ed8 Update to latest ncmlib changes. 2014-03-30 17:02:48 -04:00
Nicholas J. Kain
15598c9207 Support RFC4361.
RFC4361 requires clients to send a clientid, and specifies that by default
that clientid should be a combination of a machine-static DUID and an
interface-static IAID.

There are several RFC-compliant DUIDs.  ndhc uses RFC6355's DUID-UUID,
but chooses not to follow RFC4122 for the UUID and instead simply uses
random bytes from its combined Tausworthe PRNG.

RFC4122 is excessively complex, and 128-bit random values are more than
sufficiently collision-resistant on even large DHCP segments.

ndhc requires a read/writable directory to store the DUID/IAID states.  By
default this directory is /etc/ndhc.  It exists outside the chroot.  The DUID
will be stored in a single file, DUID.  The IAIDs exist per-interface and are
stored in files with names similar to IAID-xx:xx:xx:xx:xx:xx, where the xx
values are replaced by the Ethernet hardware address of the interface.

If it is impossible to read or store the DUIDs or IAIDs, ndhc will
fail at start time before it performs any network activity or forks any
subprocesses.

If the host system lacks volatile storage, then a clientid should manually
be specified using the -c or --clientid command arguments.
2014-03-19 00:42:32 -04:00
Nicholas J. Kain
cd269c7261 Make ndhc RFC6842-compliant.
All this entails is that ndhc needs to check to make sure that if the remote
server sends a dhcp packet with a client identifier, the client identifier
of that packet matches the client identifier that ndhc uses to identify
itself.

If the remote server does not attach a client identifier to its dhcp packets,
then the behavior of ndhc does not change.
2014-03-18 03:13:51 -04:00
Nicholas J. Kain
3f267576ac Use RTA_DATA() instead of rtattr_get_data(). 2014-03-17 20:28:26 -04:00
Nicholas J. Kain
f7de4a14fd Use NLMSG_DATA() instead of nlmsg_get_data(). 2014-03-17 20:26:37 -04:00
Nicholas J. Kain
a052d069b7 nlattr was being used where rtattr should have been used. Happily, the
types are almost identical (same number of fields, each field has the
same length), so the code worked anyway, but nlattr and rtattr are
distinct.

nlattr is the type/length part of the nlmsg header.  rtattr is the
type/length part of the individual rt attribute items attached after
a nlmsg.

Use the correct rtattr type, and use the standard macros where appropriate.
2014-03-17 20:22:20 -04:00
Nicholas J. Kain
268e88dc24 Make nl_getifdata() use a netlink sequence number generated from the
tv_nsec field from clock_gettime(CLOCK_REALTIME), and actually check
that the sequence number matches that of the netlink replies.
2014-03-17 06:15:59 -04:00
Nicholas J. Kain
22fede861f Netlink is pickier than the ioctl interfaces and requires the link to
manually be set to an 'up' state before much of anything can be changed.
Ensure that this is done very early in ndhc's lifetime, and record the
link status at startup time so that the hardware link status monitoring
will not get confused.  A perform_ifup() function is added to faciliate
this need.

Handle nl_getifdata() and get_if_index_and_mac() separately from the
hardware link status monitoring; don't call get_if_index_and_mac() from
nl_process_msgs().

Create the permanent ndhc-master cs.nlFd socket for hardware link status
monitoring after forking subprocesses.
2014-03-17 05:56:30 -04:00
Nicholas J. Kain
3e78ffd576 We don't need to disable hardware link status while ifch is working; if
the link status goes down, we will fail horribly anyway because ARP
will fail, so we must keep track of the link status to be safe.
2014-03-17 03:17:24 -04:00
Nicholas J. Kain
6fcc43d169 Hardware link status can 'bounce' erratically while link properties are
being updated, so perform status debouncing.

While ifch is doing work, the netlink events are ignored.  Once ifch has
finished its work, netlink events are no longer ignored.  Making this
work requires ifch to communicate back to ndhc, but it is no problem
since the necessary pipes are already in place for IPC.
2014-03-17 02:55:47 -04:00
Nicholas J. Kain
50d6284fee Use netlink for setting link flags instead of ioctl. 2014-03-16 05:10:21 -04:00
Nicholas J. Kain
cce93139d0 Delete old IP addresses associated with the interface when setting the
DHCP-assigned IP, broadcast, and subnet.

The nl_foreach_nlmsg() gains a seq parameter that when set to non-0 will
cause nl_foreach_nlmsg() to ignore any nlmsg that has a seq number
that does not match the caller-supplied seq argument.
2014-03-15 04:43:29 -04:00
Nicholas J. Kain
19d009891a When updating the IP and broadcast addresses, clear out any old IP and
broadcast addresses that may be still bound to the interface.
2014-03-15 02:44:43 -04:00
Nicholas J. Kain
4d1cfe8586 Add a nl_sendgetaddr() function. 2014-03-15 00:30:39 -04:00
Nicholas J. Kain
e5d9f4acf2 Move nl_sendgetlink() to nl.c. 2014-03-14 23:54:21 -04:00
Nicholas J. Kain
1222f4f22a Handle sendto() returning EINTR in nl_sendgetlink(), and print if an
error is encountered while performing sendto().
2014-03-12 15:14:40 -04:00
Nicholas J. Kain
765f3de274 Use strncmp rather than strcmp in netlink.c when fetching the interface
MAC address and index.
2014-03-12 13:03:34 -04:00
Nicholas J. Kain
e990246207 Move nlbuf onto stack and don't share a single buffer for sending
and receiving.  Move nlportid into client state structure.
2012-04-12 20:06:05 -04:00
Nicholas J. Kain
659aafaec8 Silence new kernel warning introduced in 3.3: 'netlink: 12 bytes leftover
after parsing attributes.' RTM_GETLINK messages should have a struct rtattr
as payload.
2012-04-12 04:52:26 -04:00
Nicholas J. Kain
a8a761da14 Shrink stack use of nl_getifdata(). 2012-04-03 22:00:47 -04:00
Nicholas J. Kain
93b44ed48d Tidy up nl_getifdata() a bit. Eliminate some unnecessary local variables,
and use NLMSG_LENGTH().
2012-04-03 10:06:00 -04:00
Nicholas J. Kain
77af1d81fe Relicense under New BSD (2-clause) license. I don't see any advantage to
restricting use unnecessarily, and simple attribution is fine.
2011-07-25 02:30:57 -04:00
Nicholas J. Kain
cfa22626e4 Check the server identifier option when receiving a DHCP ACK or NAK to make
sure that it matches that of our associated DHCP server.

Normalize, prune, and beautify the appearance of log messages.

Minor cosmetic cleanups/refactoring.
2011-07-11 16:33:57 -04:00
Nicholas J. Kain
4453391154 Move dhcp state handling code out of netlink.c and into state.c. netlink.c
now just calls hooks exposed by state.c, just like the dhcp packet,
poll timeout, and signal code.

Make ifchange_deconfig() idempotent.
2011-07-04 22:10:14 -04:00
Nicholas J. Kain
9ef66af020 Simplify the ifchange API. 2011-07-04 20:51:27 -04:00
Nicholas J. Kain
e4ff1e9261 Remove the ugly hack for forcing nl_getifdata() to be synchronous. It now
properly performs a synchronous wait using poll().
2011-07-03 18:10:00 -04:00
Nicholas J. Kain
01c4731403 Make sure that the netlink socket will never block after program
initialization.  Fetching if/address/index/mac mappings is done only once at
program init, so it is done synchronously as an exception to this rule.

Rewrite the netlink handling.  Now uses NIH code that should be safe, small,
and correct.  No external deps FTW.
2011-07-03 17:30:55 -04:00
Nicholas J. Kain
801ec356f4 Convert DHCP option code numbers to an enum.
Convert many remaining /**/ comments to // where it makes for less visual
clutter.
2011-07-02 06:31:57 -04:00
Nicholas J. Kain
d304fb6b38 Only bring down the interface at startup time if it is not already up and
running.  ndhc does its job entirely with raw sockets before a lease is
established, so it will work just fine, and this change allows for the dhcp
client to be re-run without causing downtime on an interface.
2011-07-02 04:58:58 -04:00
Nicholas J. Kain
7104b56ab9 Change netlink event handling so that the DHCP client will sleep if the
interface carrier is lost or the interface is deconfigured but still exists,
eg via ifconfig down.  If the hardware is removed, then ndhcp will exit.
2011-07-02 01:51:32 -04:00
Nicholas J. Kain
17570e9a38 Rewrite the netlink handling code to use libmnl. The size cost is very small,
and it is hard to do netlink completely correctly as it is poorly documented.
2011-07-02 01:34:50 -04:00
Nicholas J. Kain
38ad2399ed Clean up the DHCP packet sending functions and make them more RFC-compliant.
Rename cs->requestedIP to cs->clientAddr.
Move the IFS_* defines into netlink.c.
Cosmetic cleanups.
2011-07-01 11:37:13 -04:00
Nicholas J. Kain
a68c8cb64c Make the log prints less debug-like and more sysadmin-friendly. 2011-07-01 05:49:39 -04:00
Nicholas J. Kain
1f514cbb50 Hide details of the listen mode implementation in packet.c. 2011-06-30 21:33:38 -04:00
Nicholas J. Kain
76ecfffce2 Centralize DHCP timeout, packet reciept, and user-demanded action handling
into state.[ch].  Remove timeout.c.
2011-06-29 23:47:31 -04:00
Nicholas J. Kain
7ee0001e97 Constant correctness: socket(PF_*, ...) -> socket(AF_*, ...) 2011-06-25 11:11:48 -04:00
Nicholas J. Kain
d72b24a2fe Theoretical correctness fix:
Handle EAGAIN and EWOULDBLOCK more gracefully when dealing with safe_read().
All occurrences of safe_read() should only be invoked on fds that have signaled
ready-to-read state via the epoll() mechanism, so this change should not
result in any observable difference, but it is best to be safe.

Additionally, a constant stack variable is converted to an equivalent
macro define for cleanliness.

Finally, print the error type encountered if reading data from an ARP response
fails with a read error.
2011-05-30 10:54:05 -04:00
Nicholas J. Kain
da193ae35a Add support for writing lease files. 2011-04-19 16:37:43 -04:00
Nicholas J. Kain
a7db2c4bd2 Make arp code more robust and refactor it a bit.
Handle failure to create arp sockets more gracefully.
Add initial support for retransmitting arp requests if no reply is met after
a certain number of spurious packets.
2011-03-31 02:32:34 -04:00
Nicholas J. Kain
a6fa236700 Massive cosmetic patch: update or add copyright headers, untabify, and
change all unsigned char to uint8_t.
2011-03-30 23:59:22 -04:00
Nicholas J. Kain
03717e1593 If physical link state changes to UP and a lease is bound, check to see if
the currently assigned gateway/router still replies to ARP queries.  If so,
keep the lease.  If not, get a new lease.

Save the IP address of the current gateway/router.
Remove an outdated check for a buggy compiler.
Fix a typo in the previous commit that caused timeouts to be 1000x faster than
they should be.
2011-03-30 20:13:48 -04:00
Nicholas J. Kain
811cc67e16 Move add_requests() from dhcpmsg.c to options.c. Fixes a layering violation.
Document an ugly-as-hell code bit in ifchange.c.
Add some debugging messages for the netlink response code.
Clean headers a bit more.
2011-03-30 07:26:42 -04:00
Nicholas J. Kain
d01b104d48 Handle netlink interface-up notification for non-DS_BOUND states. 2011-03-30 05:24:16 -04:00
Nicholas J. Kain
9d03795a15 Wire up the netlink socket to the epoll handler. Still need to actually
react to events in the processing function.
Pass the client_state structure to the netlink code explicitly rather than
making it a global variable.
2011-03-29 15:34:00 -04:00
Nicholas J. Kain
55bc002ad6 Use netlink for getting interface mac and index in ndhc instead of ioctl. 2011-03-29 14:37:45 -04:00