Home | History | Annotate | Line # | Download | only in src
      1 /* SPDX-License-Identifier: BSD-2-Clause */
      2 /*
      3  * dhcpcd - DHCP client daemon
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) marples.name>
      5  * All rights reserved
      6 
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #ifndef DHCP_H
     30 #define DHCP_H
     31 
     32 #include <arpa/inet.h>
     33 #include <netinet/in.h>
     34 
     35 #include <netinet/ip.h>
     36 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
     37 #include <netinet/udp.h>
     38 #undef __FAVOR_BSD
     39 
     40 #include <limits.h>
     41 #include <stdint.h>
     42 
     43 #include "arp.h"
     44 #include "bpf.h"
     45 #include "auth.h"
     46 #include "dhcp-common.h"
     47 
     48 /* UDP port numbers for BOOTP */
     49 #define BOOTPS			67
     50 #define BOOTPC			68
     51 
     52 #define MAGIC_COOKIE		0x63825363
     53 #define BROADCAST_FLAG		0x8000
     54 
     55 /* BOOTP message OP code */
     56 #define BOOTREQUEST		1
     57 #define BOOTREPLY		2
     58 
     59 /* DHCP message type */
     60 #define DHCP_DISCOVER		1
     61 #define DHCP_OFFER		2
     62 #define DHCP_REQUEST		3
     63 #define DHCP_DECLINE		4
     64 #define DHCP_ACK		5
     65 #define DHCP_NAK		6
     66 #define DHCP_RELEASE		7
     67 #define DHCP_INFORM		8
     68 #define DHCP_FORCERENEW		9
     69 
     70 /* Constants taken from RFC 2131. */
     71 #define T1			0.5
     72 #define T2			0.875
     73 #define DHCP_BASE		4
     74 #define DHCP_MAX		64
     75 #define DHCP_RAND_MIN		-1
     76 #define DHCP_RAND_MAX		1
     77 
     78 #ifdef RFC2131_STRICT
     79 /* Be strictly conformant for section 4.1.1 */
     80 #  define DHCP_MIN_DELAY	1
     81 #  define DHCP_MAX_DELAY	10
     82 #else
     83 /* or mirror the more modern IPv6RS and DHCPv6 delays */
     84 #  define DHCP_MIN_DELAY	0
     85 #  define DHCP_MAX_DELAY	1
     86 #endif
     87 
     88 /* DHCP options */
     89 enum DHO {
     90 	DHO_PAD                    = 0,
     91 	DHO_SUBNETMASK             = 1,
     92 	DHO_ROUTER                 = 3,
     93 	DHO_DNSSERVER              = 6,
     94 	DHO_HOSTNAME               = 12,
     95 	DHO_DNSDOMAIN              = 15,
     96 	DHO_MTU                    = 26,
     97 	DHO_BROADCAST              = 28,
     98 	DHO_STATICROUTE            = 33,
     99 	DHO_NISDOMAIN              = 40,
    100 	DHO_NISSERVER              = 41,
    101 	DHO_NTPSERVER              = 42,
    102 	DHO_VENDOR                 = 43,
    103 	DHO_IPADDRESS              = 50,
    104 	DHO_LEASETIME              = 51,
    105 	DHO_OPTSOVERLOADED         = 52,
    106 	DHO_MESSAGETYPE            = 53,
    107 	DHO_SERVERID               = 54,
    108 	DHO_PARAMETERREQUESTLIST   = 55,
    109 	DHO_MESSAGE                = 56,
    110 	DHO_MAXMESSAGESIZE         = 57,
    111 	DHO_RENEWALTIME            = 58,
    112 	DHO_REBINDTIME             = 59,
    113 	DHO_VENDORCLASSID          = 60,
    114 	DHO_CLIENTID               = 61,
    115 	DHO_USERCLASS              = 77,  /* RFC 3004 */
    116 	DHO_RAPIDCOMMIT            = 80,  /* RFC 4039 */
    117 	DHO_FQDN                   = 81,
    118 	DHO_AUTHENTICATION         = 90,  /* RFC 3118 */
    119 	DHO_IPV6_PREFERRED_ONLY    = 108, /* RFC 8925 */
    120 	DHO_AUTOCONFIGURE          = 116, /* RFC 2563 */
    121 	DHO_DNSSEARCH              = 119, /* RFC 3397 */
    122 	DHO_CSR                    = 121, /* RFC 3442 */
    123 	DHO_VIVCO                  = 124, /* RFC 3925 */
    124 	DHO_VIVSO                  = 125, /* RFC 3925 */
    125 	DHO_FORCERENEW_NONCE       = 145, /* RFC 6704 */
    126 	DHO_MUDURL                 = 161, /* draft-ietf-opsawg-mud */
    127 	DHO_SIXRD                  = 212, /* RFC 5969 */
    128 	DHO_MSCSR                  = 249, /* MS code for RFC 3442 */
    129 	DHO_END                    = 255
    130 };
    131 
    132 /* FQDN values - lsnybble used in flags
    133  * hsnybble to create order
    134  * and to allow 0x00 to mean disable
    135  */
    136 enum FQDN {
    137 	FQDN_DISABLE    = 0x00,
    138 	FQDN_NONE       = 0x18,
    139 	FQDN_PTR        = 0x20,
    140 	FQDN_BOTH       = 0x31
    141 };
    142 
    143 #define	MIN_V6ONLY_WAIT		300 /* seconds, RFC 8925 */
    144 
    145 /* Sizes for BOOTP options */
    146 #define	BOOTP_CHADDR_LEN	 16
    147 #define	BOOTP_SNAME_LEN		 64
    148 #define	BOOTP_FILE_LEN		128
    149 #define	BOOTP_VEND_LEN		 64
    150 
    151 /* DHCP is basically an extension to BOOTP */
    152 struct bootp {
    153 	uint8_t op;		/* message type */
    154 	uint8_t htype;		/* hardware address type */
    155 	uint8_t hlen;		/* hardware address length */
    156 	uint8_t hops;		/* should be zero in client message */
    157 	uint32_t xid;		/* transaction id */
    158 	uint16_t secs;		/* elapsed time in sec. from boot */
    159 	uint16_t flags;		/* such as broadcast flag */
    160 	uint32_t ciaddr;	/* (previously allocated) client IP */
    161 	uint32_t yiaddr;	/* 'your' client IP address */
    162 	uint32_t siaddr;	/* should be zero in client's messages */
    163 	uint32_t giaddr;	/* should be zero in client's messages */
    164 	uint8_t chaddr[BOOTP_CHADDR_LEN];	/* client's hardware address */
    165 	uint8_t sname[BOOTP_SNAME_LEN];		/* server host name */
    166 	uint8_t file[BOOTP_FILE_LEN];		/* boot file name */
    167 	uint8_t vend[BOOTP_VEND_LEN];		/* vendor specific area */
    168 	/* DHCP allows a variable length vendor area */
    169 };
    170 
    171 #define	DHCP_MIN_LEN		(offsetof(struct bootp, vend) + 4)
    172 
    173 struct bootp_pkt
    174 {
    175 	struct ip ip;
    176 	struct udphdr udp;
    177 	struct bootp bootp;
    178 };
    179 
    180 struct dhcp_lease {
    181 	struct in_addr addr;
    182 	struct in_addr mask;
    183 	struct in_addr brd;
    184 	uint32_t leasetime;
    185 	uint32_t renewaltime;
    186 	uint32_t rebindtime;
    187 	struct in_addr server;
    188 	uint8_t frominfo;
    189 	uint32_t cookie;
    190 };
    191 
    192 #ifndef DHCP_INFINITE_LIFETIME
    193 #  define DHCP_INFINITE_LIFETIME	(~0U)
    194 #endif
    195 
    196 enum DHS {
    197 	DHS_NONE,
    198 	DHS_INIT,
    199 	DHS_DISCOVER,
    200 	DHS_REQUEST,
    201 	DHS_PROBE,
    202 	DHS_BOUND,
    203 	DHS_RENEW,
    204 	DHS_REBIND,
    205 	DHS_REBOOT,
    206 	DHS_INFORM,
    207 	DHS_RENEW_REQUESTED,
    208 	DHS_RELEASE
    209 };
    210 
    211 struct dhcp_state {
    212 	enum DHS state;
    213 	struct bootp *sent;
    214 	size_t sent_len;
    215 	struct bootp *offer;
    216 	size_t offer_len;
    217 	struct bootp *new;
    218 	size_t new_len;
    219 	struct bootp *old;
    220 	size_t old_len;
    221 	struct dhcp_lease lease;
    222 	const char *reason;
    223 	unsigned int interval;
    224 	unsigned int nakoff;
    225 	uint32_t xid;
    226 	int socket;
    227 
    228 	struct bpf *bpf;
    229 	int udp_rfd;
    230 	struct ipv4_addr *addr;
    231 	uint8_t added;
    232 
    233 	char leasefile[sizeof(LEASEFILE) + IF_NAMESIZE + (IF_SSIDLEN * 4)];
    234 	struct timespec started;
    235 	unsigned char *clientid;
    236 	struct authstate auth;
    237 #ifdef ARPING
    238 	ssize_t arping_index;
    239 #endif
    240 };
    241 
    242 #ifdef INET
    243 #define D_STATE(ifp)							       \
    244 	((struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
    245 #define D_CSTATE(ifp)							       \
    246 	((const struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
    247 #define D_STATE_RUNNING(ifp)						       \
    248 	(D_CSTATE((ifp)) && D_CSTATE((ifp))->new && D_CSTATE((ifp))->reason)
    249 
    250 #define IS_DHCP(b)	((b)->vend[0] == 0x63 &&	\
    251 			 (b)->vend[1] == 0x82 &&	\
    252 			 (b)->vend[2] == 0x53 &&	\
    253 			 (b)->vend[3] == 0x63)
    254 
    255 #include "dhcpcd.h"
    256 #include "if-options.h"
    257 
    258 ssize_t print_rfc3361(FILE *, const uint8_t *, size_t);
    259 ssize_t print_rfc3442(FILE *, const uint8_t *, size_t);
    260 
    261 int dhcp_openudp(struct in_addr *);
    262 void dhcp_packet(struct interface *, uint8_t *, size_t, unsigned int);
    263 void dhcp_recvmsg(struct dhcpcd_ctx *, struct msghdr *);
    264 void dhcp_printoptions(const struct dhcpcd_ctx *,
    265     const struct dhcp_opt *, size_t);
    266 uint16_t dhcp_get_mtu(const struct interface *);
    267 int dhcp_get_routes(rb_tree_t *, struct interface *);
    268 ssize_t dhcp_env(FILE *, const char *, const struct interface *,
    269     const struct bootp *, size_t);
    270 
    271 struct ipv4_addr *dhcp_handleifa(int, struct ipv4_addr *, pid_t pid);
    272 void dhcp_drop(struct interface *, const char *);
    273 void dhcp_start(struct interface *);
    274 void dhcp_abort(struct interface *);
    275 void dhcp_discover(void *);
    276 void dhcp_inform(struct interface *);
    277 void dhcp_renew(struct interface *);
    278 void dhcp_bind(struct interface *);
    279 void dhcp_reboot_newopts(struct interface *, unsigned long long);
    280 void dhcp_close(struct interface *);
    281 void dhcp_free(struct interface *);
    282 int dhcp_dump(struct interface *);
    283 #endif /* INET */
    284 
    285 #endif /* DHCP_H */
    286