xref: /dflybsd-src/contrib/dhcpcd/src/dhcp-common.h (revision e9678c6f9939e4603a10c5f27f033a91d33856b4)
18d36e1dfSRoy Marples /* SPDX-License-Identifier: BSD-2-Clause */
27827cba2SAaron LI /*
37827cba2SAaron LI  * dhcpcd - DHCP client daemon
480aa9461SRoy Marples  * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
57827cba2SAaron LI  * All rights reserved
67827cba2SAaron LI 
77827cba2SAaron LI  * Redistribution and use in source and binary forms, with or without
87827cba2SAaron LI  * modification, are permitted provided that the following conditions
97827cba2SAaron LI  * are met:
107827cba2SAaron LI  * 1. Redistributions of source code must retain the above copyright
117827cba2SAaron LI  *    notice, this list of conditions and the following disclaimer.
127827cba2SAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
137827cba2SAaron LI  *    notice, this list of conditions and the following disclaimer in the
147827cba2SAaron LI  *    documentation and/or other materials provided with the distribution.
157827cba2SAaron LI  *
167827cba2SAaron LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
177827cba2SAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187827cba2SAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197827cba2SAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207827cba2SAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217827cba2SAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227827cba2SAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237827cba2SAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247827cba2SAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257827cba2SAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267827cba2SAaron LI  * SUCH DAMAGE.
277827cba2SAaron LI  */
287827cba2SAaron LI 
297827cba2SAaron LI #ifndef DHCPCOMMON_H
307827cba2SAaron LI #define	DHCPCOMMON_H
317827cba2SAaron LI 
327827cba2SAaron LI #include <arpa/inet.h>
337827cba2SAaron LI #include <netinet/in.h>
347827cba2SAaron LI 
357827cba2SAaron LI #include <stdint.h>
367827cba2SAaron LI 
378d36e1dfSRoy Marples #include <arpa/nameser.h> /* after normal includes for sunos */
388d36e1dfSRoy Marples 
397827cba2SAaron LI #include "common.h"
407827cba2SAaron LI #include "dhcpcd.h"
417827cba2SAaron LI 
428d36e1dfSRoy Marples /* Support very old arpa/nameser.h as found in OpenBSD */
438d36e1dfSRoy Marples #ifndef NS_MAXDNAME
448d36e1dfSRoy Marples #define NS_MAXCDNAME MAXCDNAME
458d36e1dfSRoy Marples #define NS_MAXDNAME MAXDNAME
468d36e1dfSRoy Marples #define NS_MAXLABEL MAXLABEL
478d36e1dfSRoy Marples #endif
488d36e1dfSRoy Marples 
497827cba2SAaron LI /* Max MTU - defines dhcp option length */
507827cba2SAaron LI #define	IP_UDP_SIZE		  28
517827cba2SAaron LI #define	MTU_MAX			1500 - IP_UDP_SIZE
527827cba2SAaron LI #define	MTU_MIN			 576 + IP_UDP_SIZE
537827cba2SAaron LI 
547827cba2SAaron LI #define	OT_REQUEST		(1 << 0)
557827cba2SAaron LI #define	OT_UINT8		(1 << 1)
567827cba2SAaron LI #define	OT_INT8			(1 << 2)
577827cba2SAaron LI #define	OT_UINT16		(1 << 3)
587827cba2SAaron LI #define	OT_INT16		(1 << 4)
597827cba2SAaron LI #define	OT_UINT32		(1 << 5)
607827cba2SAaron LI #define	OT_INT32		(1 << 6)
617827cba2SAaron LI #define	OT_ADDRIPV4		(1 << 7)
627827cba2SAaron LI #define	OT_STRING		(1 << 8)
637827cba2SAaron LI #define	OT_ARRAY		(1 << 9)
647827cba2SAaron LI #define	OT_RFC3361		(1 << 10)
657827cba2SAaron LI #define	OT_RFC1035		(1 << 11)
667827cba2SAaron LI #define	OT_RFC3442		(1 << 12)
677827cba2SAaron LI #define	OT_OPTIONAL		(1 << 13)
687827cba2SAaron LI #define	OT_ADDRIPV6		(1 << 14)
697827cba2SAaron LI #define	OT_BINHEX		(1 << 15)
707827cba2SAaron LI #define	OT_FLAG			(1 << 16)
717827cba2SAaron LI #define	OT_NOREQ		(1 << 17)
727827cba2SAaron LI #define	OT_EMBED		(1 << 18)
737827cba2SAaron LI #define	OT_ENCAP		(1 << 19)
747827cba2SAaron LI #define	OT_INDEX		(1 << 20)
757827cba2SAaron LI #define	OT_OPTION		(1 << 21)
767827cba2SAaron LI #define	OT_DOMAIN		(1 << 22)
777827cba2SAaron LI #define	OT_ASCII		(1 << 23)
787827cba2SAaron LI #define	OT_RAW			(1 << 24)
797827cba2SAaron LI #define	OT_ESCSTRING		(1 << 25)
807827cba2SAaron LI #define	OT_ESCFILE		(1 << 26)
817827cba2SAaron LI #define	OT_BITFLAG		(1 << 27)
827827cba2SAaron LI #define	OT_RESERVED		(1 << 28)
83*4f235d86SRoy Marples #define	OT_URI			(1 << 29)
847827cba2SAaron LI 
856e63cc1fSRoy Marples #define	DHC_REQ(r, n, o) \
866e63cc1fSRoy Marples 	(has_option_mask((r), (o)) && !has_option_mask((n), (o)))
876e63cc1fSRoy Marples 
881b3b16a2SRoy Marples #define DHC_REQOPT(o, r, n)						  \
891b3b16a2SRoy Marples 	(!((o)->type & OT_NOREQ) &&					  \
901b3b16a2SRoy Marples 	 ((o)->type & OT_REQUEST || has_option_mask((r), (o)->option)) && \
911b3b16a2SRoy Marples 	  !has_option_mask((n), (o)->option))
921b3b16a2SRoy Marples 
937827cba2SAaron LI struct dhcp_opt {
947827cba2SAaron LI 	uint32_t option; /* Also used for IANA Enterpise Number */
957827cba2SAaron LI 	int type;
967827cba2SAaron LI 	size_t len;
977827cba2SAaron LI 	char *var;
987827cba2SAaron LI 
997827cba2SAaron LI 	int index; /* Index counter for many instances of the same option */
1007827cba2SAaron LI 	char bitflags[8];
1017827cba2SAaron LI 
1027827cba2SAaron LI 	/* Embedded options.
1037827cba2SAaron LI 	 * The option code is irrelevant here. */
1047827cba2SAaron LI 	struct dhcp_opt *embopts;
1057827cba2SAaron LI 	size_t embopts_len;
1067827cba2SAaron LI 
1077827cba2SAaron LI 	/* Encapsulated options */
1087827cba2SAaron LI 	struct dhcp_opt *encopts;
1097827cba2SAaron LI 	size_t encopts_len;
1107827cba2SAaron LI };
1117827cba2SAaron LI 
1127827cba2SAaron LI const char *dhcp_get_hostname(char *, size_t, const struct if_options *);
1137827cba2SAaron LI struct dhcp_opt *vivso_find(uint32_t, const void *);
1147827cba2SAaron LI 
1157827cba2SAaron LI ssize_t dhcp_vendor(char *, size_t);
1167827cba2SAaron LI 
1177827cba2SAaron LI void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols);
1187827cba2SAaron LI #define	add_option_mask(var, val) \
1197827cba2SAaron LI 	((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] | 1 << ((val) & 7)))
1207827cba2SAaron LI #define	del_option_mask(var, val) \
1217827cba2SAaron LI 	((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] & ~(1 << ((val) & 7))))
1227827cba2SAaron LI #define	has_option_mask(var, val) \
1237827cba2SAaron LI 	((var)[(val) >> 3] & (uint8_t)(1 << ((val) & 7)))
1247827cba2SAaron LI int make_option_mask(const struct dhcp_opt *, size_t,
1257827cba2SAaron LI     const struct dhcp_opt *, size_t,
1267827cba2SAaron LI     uint8_t *, const char *, int);
1277827cba2SAaron LI 
1287827cba2SAaron LI size_t encode_rfc1035(const char *src, uint8_t *dst);
1297827cba2SAaron LI ssize_t decode_rfc1035(char *, size_t, const uint8_t *, size_t);
1307827cba2SAaron LI ssize_t print_string(char *, size_t, int, const uint8_t *, size_t);
1317827cba2SAaron LI int dhcp_set_leasefile(char *, size_t, int, const struct interface *);
1327827cba2SAaron LI 
1338d36e1dfSRoy Marples void dhcp_envoption(struct dhcpcd_ctx *,
1348d36e1dfSRoy Marples     FILE *, const char *, const char *, struct dhcp_opt *,
1357827cba2SAaron LI     const uint8_t *(*dgetopt)(struct dhcpcd_ctx *,
1367827cba2SAaron LI     size_t *, unsigned int *, size_t *,
1377827cba2SAaron LI     const uint8_t *, size_t, struct dhcp_opt **),
1387827cba2SAaron LI     const uint8_t *od, size_t ol);
1397827cba2SAaron LI void dhcp_zero_index(struct dhcp_opt *);
1407827cba2SAaron LI 
141d4fb1e02SRoy Marples ssize_t dhcp_readfile(struct dhcpcd_ctx *, const char *, void *, size_t);
142d4fb1e02SRoy Marples ssize_t dhcp_writefile(struct dhcpcd_ctx *, const char *, mode_t,
143d4fb1e02SRoy Marples     const void *, size_t);
144d4fb1e02SRoy Marples int dhcp_filemtime(struct dhcpcd_ctx *, const char *, time_t *);
145d4fb1e02SRoy Marples int dhcp_unlink(struct dhcpcd_ctx *, const char *);
146d4fb1e02SRoy Marples size_t dhcp_read_hwaddr_aton(struct dhcpcd_ctx *, uint8_t **, const char *);
1477827cba2SAaron LI #endif
148