Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * dhcpcd - DHCP client daemon
      3  * SPDX-License-Identifier: BSD-2-Clause
      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 IF_OPTIONS_H
     30 #define IF_OPTIONS_H
     31 
     32 #include <sys/param.h>
     33 #include <sys/socket.h>
     34 
     35 #include <net/if.h>
     36 #include <netinet/in.h>
     37 
     38 #include <getopt.h>
     39 #include <limits.h>
     40 #include <stdint.h>
     41 
     42 #include "auth.h"
     43 #include "route.h"
     44 
     45 /* Don't set any optional arguments here so we retain POSIX
     46  * compatibility with getopt */
     47 #define IF_OPTS                                        \
     48 	"146bc:de:f:gh:i:j:kl:m:no:pqr:s:t:u:v:wxy:z:" \
     49 	"ABC:DEF:GHI:JKLMNO:PQ:S:TUVW:X:Z:"
     50 #define NOERR_IF_OPTS	 ":" IF_OPTS
     51 
     52 #define DEFAULT_TIMEOUT	 30
     53 #define DEFAULT_REBOOT	 5
     54 #define DEFAULT_REQUEST	 180 /* secs to request, mirror DHCP6 */
     55 #define DEFAULT_FALLBACK 5   /* secs until fallback */
     56 #define DEFAULT_IPV4LL	 5   /* secs until ipv4ll */
     57 /* DHCPv4 retransmission backoff defaults (RFC 2131); DHCPv4-only. */
     58 #define DEFAULT_INITIAL_INTERVAL 4    /* DHCP_BASE per RFC 2131 */
     59 #define DEFAULT_BACKOFF_CUTOFF	 64   /* DHCP_MAX per RFC 2131 */
     60 #define DEFAULT_BACKOFF_JITTER	 1000 /* +/- milliseconds */
     61 
     62 /* Upper bounds to keep the retransmit timeout arithmetic well within range. */
     63 #define MAX_INITIAL_INTERVAL 4	  /* DHCP_BASE per RFC 2131 */
     64 #define MAX_BACKOFF_CUTOFF   64	  /* DHCP_MAX per RFC 2131 */
     65 #define MAX_BACKOFF_JITTER   1000 /* +/- milliseconds, per RFC 2131 */
     66 
     67 #ifndef HOSTNAME_MAX_LEN
     68 #define HOSTNAME_MAX_LEN 250 /* 255 - 3 (FQDN) - 2 (DNS enc) */
     69 #endif
     70 #define DHCP_OPTION_MAX_LEN 255
     71 
     72 #define DHCPCD_ARP	    (1ULL << 0)
     73 #define DHCPCD_RELEASE	    (1ULL << 1)
     74 #define DHCPCD_RTBUILD	    (1ULL << 2)
     75 #define DHCPCD_GATEWAY	    (1ULL << 3)
     76 #define DHCPCD_STATIC	    (1ULL << 4)
     77 // unused				(1ULL << 5)
     78 #define DHCPCD_ARP_PERSISTDEFENCE (1ULL << 6)
     79 #define DHCPCD_LASTLEASE	  (1ULL << 7)
     80 #define DHCPCD_INFORM		  (1ULL << 8)
     81 #define DHCPCD_REQUEST		  (1ULL << 9)
     82 #define DHCPCD_IPV4LL		  (1ULL << 10)
     83 #define DHCPCD_DUID		  (1ULL << 11)
     84 #define DHCPCD_PERSISTENT	  (1ULL << 12)
     85 #define DHCPCD_DAEMONISE	  (1ULL << 14)
     86 #define DHCPCD_DAEMONISED	  (1ULL << 15)
     87 #define DHCPCD_TEST		  (1ULL << 16)
     88 #define DHCPCD_MANAGER		  (1ULL << 17)
     89 #define DHCPCD_HOSTNAME		  (1ULL << 18)
     90 #define DHCPCD_CLIENTID		  (1ULL << 19)
     91 #define DHCPCD_LINK		  (1ULL << 20)
     92 #define DHCPCD_ANONYMOUS	  (1ULL << 21)
     93 #define DHCPCD_BACKGROUND	  (1ULL << 22)
     94 #define DHCPCD_VENDORRAW	  (1ULL << 23)
     95 // unused				(1ULL << 24)
     96 #define DHCPCD_WAITIP			(1ULL << 25)
     97 #define DHCPCD_SLAACPRIVATE		(1ULL << 26)
     98 #define DHCPCD_CSR_WARNED		(1ULL << 27)
     99 #define DHCPCD_XID_HWADDR		(1ULL << 28)
    100 #define DHCPCD_BROADCAST		(1ULL << 29)
    101 #define DHCPCD_DUMPLEASE		(1ULL << 30)
    102 #define DHCPCD_IPV6RS			(1ULL << 31)
    103 #define DHCPCD_IPV6RA_REQRDNSS		(1ULL << 32)
    104 #define DHCPCD_PRIVSEP			(1ULL << 33)
    105 #define DHCPCD_CONFIGURE		(1ULL << 34)
    106 #define DHCPCD_IPV4			(1ULL << 35)
    107 #define DHCPCD_FORKED			(1ULL << 36)
    108 #define DHCPCD_IPV6			(1ULL << 37)
    109 #define DHCPCD_STARTED			(1ULL << 38)
    110 #define DHCPCD_NOALIAS			(1ULL << 39)
    111 #define DHCPCD_IA_FORCED		(1ULL << 40)
    112 #define DHCPCD_STOPPING			(1ULL << 41)
    113 #define DHCPCD_LAUNCHER			(1ULL << 42)
    114 #define DHCPCD_HOSTNAME_SHORT		(1ULL << 43)
    115 #define DHCPCD_EXITING			(1ULL << 44)
    116 #define DHCPCD_WAITIP4			(1ULL << 45)
    117 #define DHCPCD_WAITIP6			(1ULL << 46)
    118 #define DHCPCD_DEV			(1ULL << 47)
    119 #define DHCPCD_IAID			(1ULL << 48)
    120 #define DHCPCD_DHCP			(1ULL << 49)
    121 #define DHCPCD_DHCP6			(1ULL << 50)
    122 #define DHCPCD_IF_UP			(1ULL << 51)
    123 #define DHCPCD_INFORM6			(1ULL << 52)
    124 #define DHCPCD_WANTDHCP			(1ULL << 53)
    125 #define DHCPCD_IPV6RA_AUTOCONF		(1ULL << 54)
    126 #define DHCPCD_ROUTER_HOST_ROUTE_WARNED (1ULL << 55)
    127 #define DHCPCD_LASTLEASE_EXTEND		(1ULL << 56)
    128 #define DHCPCD_BOOTP			(1ULL << 57)
    129 #define DHCPCD_INITIAL_DELAY		(1ULL << 58)
    130 #define DHCPCD_PRINT_PIDFILE		(1ULL << 59)
    131 #define DHCPCD_ONESHOT			(1ULL << 60)
    132 #define DHCPCD_INACTIVE			(1ULL << 61)
    133 #define DHCPCD_SLAACTEMP		(1ULL << 62)
    134 #define DHCPCD_PRIVSEPROOT		(1ULL << 63)
    135 
    136 #define DHCPCD_NODROP			(DHCPCD_EXITING | DHCPCD_PERSISTENT)
    137 
    138 #define DHCPCD_WAITOPTS			(DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
    139 
    140 #define DHCPCD_WARNINGS			(DHCPCD_CSR_WARNED | DHCPCD_ROUTER_HOST_ROUTE_WARNED)
    141 
    142 /* These options only make sense in the config file, so don't use any
    143    valid short options for them */
    144 #define O_BASE		     MAX('z', 'Z') + 1
    145 #define O_ARPING	     O_BASE + 1
    146 #define O_FALLBACK	     O_BASE + 2
    147 #define O_DESTINATION	     O_BASE + 3
    148 #define O_IPV6RS	     O_BASE + 4
    149 #define O_NOIPV6RS	     O_BASE + 5
    150 #define O_IPV6RA_FORK	     O_BASE + 6
    151 #define O_LINK_RCVBUF	     O_BASE + 7
    152 #define O_ANONYMOUS	     O_BASE + 8
    153 #define O_NOALIAS	     O_BASE + 9
    154 #define O_IA_NA		     O_BASE + 10
    155 #define O_IA_TA		     O_BASE + 11
    156 #define O_IA_PD		     O_BASE + 12
    157 #define O_HOSTNAME_SHORT     O_BASE + 13
    158 #define O_DEV		     O_BASE + 14
    159 #define O_NODEV		     O_BASE + 15
    160 #define O_NOIPV4	     O_BASE + 16
    161 #define O_NOIPV6	     O_BASE + 17
    162 #define O_IAID		     O_BASE + 18
    163 #define O_DEFINE	     O_BASE + 19
    164 #define O_DEFINE6	     O_BASE + 20
    165 #define O_EMBED		     O_BASE + 21
    166 #define O_ENCAP		     O_BASE + 22
    167 #define O_VENDOPT	     O_BASE + 23
    168 #define O_VENDCLASS	     O_BASE + 24
    169 #define O_AUTHPROTOCOL	     O_BASE + 25
    170 #define O_AUTHTOKEN	     O_BASE + 26
    171 #define O_AUTHNOTREQUIRED    O_BASE + 27
    172 #define O_NODHCP	     O_BASE + 28
    173 #define O_NODHCP6	     O_BASE + 29
    174 #define O_DHCP		     O_BASE + 30
    175 #define O_DHCP6		     O_BASE + 31
    176 #define O_IPV4		     O_BASE + 32
    177 #define O_IPV6		     O_BASE + 33
    178 #define O_CONTROLGRP	     O_BASE + 34
    179 #define O_SLAAC		     O_BASE + 35
    180 #define O_GATEWAY	     O_BASE + 36
    181 #define O_NOUP		     O_BASE + 37
    182 #define O_IPV6RA_AUTOCONF    O_BASE + 38
    183 #define O_IPV6RA_NOAUTOCONF  O_BASE + 39
    184 #define O_REJECT	     O_BASE + 40
    185 #define O_BOOTP		     O_BASE + 42
    186 #define O_DEFINEND	     O_BASE + 43
    187 #define O_NODELAY	     O_BASE + 44
    188 #define O_INFORM6	     O_BASE + 45
    189 #define O_LASTLEASE_EXTEND   O_BASE + 46
    190 #define O_INACTIVE	     O_BASE + 47
    191 #define O_MUDURL	     O_BASE + 48
    192 #define O_MSUSERCLASS	     O_BASE + 49
    193 #define O_CONFIGURE	     O_BASE + 50
    194 #define O_NOCONFIGURE	     O_BASE + 51
    195 #define O_RANDOMISE_HWADDR   O_BASE + 52
    196 #define O_ARP_PERSISTDEFENCE O_BASE + 53
    197 #define O_REQUEST_TIME	     O_BASE + 54
    198 #define O_FALLBACK_TIME	     O_BASE + 55
    199 #define O_IPV4LL_TIME	     O_BASE + 56
    200 #define O_VSIO		     O_BASE + 57
    201 #define O_VSIO6		     O_BASE + 58
    202 #define O_NOSYSLOG	     O_BASE + 59
    203 #define O_INITIAL_INTERVAL   O_BASE + 60
    204 #define O_BACKOFF_CUTOFF     O_BASE + 61
    205 #define O_BACKOFF_JITTER     O_BASE + 62
    206 #define O_ALLOW		     O_BASE + 63
    207 #define O_READGRP	     O_BASE + 64
    208 
    209 extern const struct option cf_options[];
    210 
    211 struct if_sla {
    212 	char ifname[IF_NAMESIZE];
    213 	uint32_t sla;
    214 	uint8_t prefix_len;
    215 	uint64_t suffix;
    216 	bool sla_set;
    217 };
    218 
    219 struct if_ia {
    220 	uint8_t iaid[4];
    221 #ifdef INET6
    222 	uint16_t ia_type;
    223 	uint8_t iaid_set;
    224 	struct in6_addr addr;
    225 	uint8_t prefix_len;
    226 #ifndef SMALL
    227 	uint32_t sla_max;
    228 	size_t sla_len;
    229 	struct if_sla *sla;
    230 #endif
    231 #endif
    232 };
    233 
    234 #ifndef SMALL
    235 struct vivco {
    236 	uint32_t en;
    237 	size_t len;
    238 	uint8_t *data;
    239 };
    240 struct vsio_so {
    241 	uint16_t opt;
    242 	uint16_t len;
    243 	void *data;
    244 };
    245 struct vsio {
    246 	uint32_t en;
    247 	size_t so_len;
    248 	struct vsio_so *so;
    249 };
    250 #endif
    251 
    252 #define OPTION_POLICY_MAX 5
    253 struct dho_policy {
    254 	uint32_t *dhop_policy;
    255 	size_t dhop_policy_len;
    256 };
    257 
    258 struct dho_policy_group {
    259 	struct dho_policy dhop_request;
    260 	struct dho_policy dhop_require;
    261 	struct dho_policy dhop_allow;
    262 	struct dho_policy dhop_remove;
    263 	struct dho_policy dhop_reject;
    264 };
    265 
    266 struct if_options {
    267 	time_t mtime;
    268 	uint8_t iaid[4];
    269 	int metric;
    270 	uint32_t leasetime;
    271 	uint32_t timeout;
    272 	uint32_t reboot;
    273 	uint32_t request_time;
    274 	uint32_t fallback_time;
    275 	uint32_t ipv4ll_time;
    276 	uint32_t initial_interval;
    277 	uint32_t backoff_cutoff;
    278 	uint32_t backoff_jitter;
    279 	unsigned long long options;
    280 	bool randomise_hwaddr;
    281 
    282 #ifdef INET
    283 	struct dho_policy_group dhopg_dhcp;
    284 	struct dho_policy dhop_destination;
    285 #endif
    286 
    287 #ifdef INET6
    288 	struct dho_policy_group dhopg_nd;
    289 #endif
    290 #ifdef DHCP6
    291 	struct dho_policy_group dhopg_dhcp6;
    292 #endif
    293 
    294 	struct in_addr req_addr;
    295 	struct in_addr req_mask;
    296 	struct in_addr req_brd;
    297 	rb_tree_t routes;
    298 	struct in6_addr req_addr6;
    299 	uint8_t req_prefix_len;
    300 	unsigned int mtu;
    301 	char **config;
    302 
    303 	char **environ;
    304 
    305 	char hostname[HOSTNAME_MAX_LEN + 1]; /* NUL terminated */
    306 	uint8_t fqdn;
    307 	/* The first byte is the option length */
    308 	uint8_t vendorclassid[DHCP_OPTION_MAX_LEN + 1];
    309 	uint8_t clientid[DHCP_OPTION_MAX_LEN + 1];
    310 	uint8_t userclass[DHCP_OPTION_MAX_LEN + 1];
    311 	uint8_t vendor[DHCP_OPTION_MAX_LEN + 1];
    312 	uint8_t mudurl[DHCP_OPTION_MAX_LEN + 1];
    313 
    314 	size_t blacklist_len;
    315 	in_addr_t *blacklist;
    316 	size_t whitelist_len;
    317 	in_addr_t *whitelist;
    318 	ssize_t arping_len;
    319 	in_addr_t *arping;
    320 	char *fallback;
    321 
    322 	struct if_ia *ia;
    323 	size_t ia_len;
    324 #ifdef INET6
    325 	struct in6_addr token;
    326 #endif
    327 
    328 	struct dhcp_opt *dhcp_override;
    329 	size_t dhcp_override_len;
    330 	struct dhcp_opt *nd_override;
    331 	size_t nd_override_len;
    332 	struct dhcp_opt *dhcp6_override;
    333 	size_t dhcp6_override_len;
    334 	struct dhcp_opt *vivso_override;
    335 	size_t vivso_override_len;
    336 
    337 #ifndef SMALL
    338 	size_t vivco_len;
    339 	struct vivco *vivco;
    340 	size_t vsio_len;
    341 	struct vsio *vsio;
    342 	size_t vsio6_len;
    343 	struct vsio *vsio6;
    344 #endif
    345 
    346 	struct auth auth;
    347 };
    348 
    349 struct if_options *read_config(struct dhcpcd_ctx *, const char *, const char *,
    350     const char *);
    351 int add_options(struct dhcpcd_ctx *, const char *, struct if_options *, int,
    352     char **);
    353 void free_dhcp_opt_embenc(struct dhcp_opt *);
    354 void free_options(struct dhcpcd_ctx *, struct if_options *);
    355 
    356 char **alloc_args(int argc, char **argv);
    357 
    358 #endif
    359