xref: /dragonfly/contrib/dhcpcd/src/dhcp-common.h (revision 91dc43dd)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2019 Roy Marples <roy@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 DHCPCOMMON_H
30 #define	DHCPCOMMON_H
31 
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 
35 #include <stdint.h>
36 
37 #include <arpa/nameser.h> /* after normal includes for sunos */
38 
39 #include "common.h"
40 #include "dhcpcd.h"
41 
42 /* Support very old arpa/nameser.h as found in OpenBSD */
43 #ifndef NS_MAXDNAME
44 #define NS_MAXCDNAME MAXCDNAME
45 #define NS_MAXDNAME MAXDNAME
46 #define NS_MAXLABEL MAXLABEL
47 #endif
48 
49 /* Max MTU - defines dhcp option length */
50 #define	IP_UDP_SIZE		  28
51 #define	MTU_MAX			1500 - IP_UDP_SIZE
52 #define	MTU_MIN			 576 + IP_UDP_SIZE
53 
54 #define	OT_REQUEST		(1 << 0)
55 #define	OT_UINT8		(1 << 1)
56 #define	OT_INT8			(1 << 2)
57 #define	OT_UINT16		(1 << 3)
58 #define	OT_INT16		(1 << 4)
59 #define	OT_UINT32		(1 << 5)
60 #define	OT_INT32		(1 << 6)
61 #define	OT_ADDRIPV4		(1 << 7)
62 #define	OT_STRING		(1 << 8)
63 #define	OT_ARRAY		(1 << 9)
64 #define	OT_RFC3361		(1 << 10)
65 #define	OT_RFC1035		(1 << 11)
66 #define	OT_RFC3442		(1 << 12)
67 #define	OT_OPTIONAL		(1 << 13)
68 #define	OT_ADDRIPV6		(1 << 14)
69 #define	OT_BINHEX		(1 << 15)
70 #define	OT_FLAG			(1 << 16)
71 #define	OT_NOREQ		(1 << 17)
72 #define	OT_EMBED		(1 << 18)
73 #define	OT_ENCAP		(1 << 19)
74 #define	OT_INDEX		(1 << 20)
75 #define	OT_OPTION		(1 << 21)
76 #define	OT_DOMAIN		(1 << 22)
77 #define	OT_ASCII		(1 << 23)
78 #define	OT_RAW			(1 << 24)
79 #define	OT_ESCSTRING		(1 << 25)
80 #define	OT_ESCFILE		(1 << 26)
81 #define	OT_BITFLAG		(1 << 27)
82 #define	OT_RESERVED		(1 << 28)
83 
84 #define DHC_REQOPT(o, r, n)						  \
85 	(!((o)->type & OT_NOREQ) &&					  \
86 	 ((o)->type & OT_REQUEST || has_option_mask((r), (o)->option)) && \
87 	  !has_option_mask((n), (o)->option))
88 
89 struct dhcp_opt {
90 	uint32_t option; /* Also used for IANA Enterpise Number */
91 	int type;
92 	size_t len;
93 	char *var;
94 
95 	int index; /* Index counter for many instances of the same option */
96 	char bitflags[8];
97 
98 	/* Embedded options.
99 	 * The option code is irrelevant here. */
100 	struct dhcp_opt *embopts;
101 	size_t embopts_len;
102 
103 	/* Encapsulated options */
104 	struct dhcp_opt *encopts;
105 	size_t encopts_len;
106 };
107 
108 const char *dhcp_get_hostname(char *, size_t, const struct if_options *);
109 struct dhcp_opt *vivso_find(uint32_t, const void *);
110 
111 ssize_t dhcp_vendor(char *, size_t);
112 
113 void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols);
114 #define	add_option_mask(var, val) \
115 	((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] | 1 << ((val) & 7)))
116 #define	del_option_mask(var, val) \
117 	((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] & ~(1 << ((val) & 7))))
118 #define	has_option_mask(var, val) \
119 	((var)[(val) >> 3] & (uint8_t)(1 << ((val) & 7)))
120 int make_option_mask(const struct dhcp_opt *, size_t,
121     const struct dhcp_opt *, size_t,
122     uint8_t *, const char *, int);
123 
124 size_t encode_rfc1035(const char *src, uint8_t *dst);
125 ssize_t decode_rfc1035(char *, size_t, const uint8_t *, size_t);
126 ssize_t print_string(char *, size_t, int, const uint8_t *, size_t);
127 int dhcp_set_leasefile(char *, size_t, int, const struct interface *);
128 
129 void dhcp_envoption(struct dhcpcd_ctx *,
130     FILE *, const char *, const char *, struct dhcp_opt *,
131     const uint8_t *(*dgetopt)(struct dhcpcd_ctx *,
132     size_t *, unsigned int *, size_t *,
133     const uint8_t *, size_t, struct dhcp_opt **),
134     const uint8_t *od, size_t ol);
135 void dhcp_zero_index(struct dhcp_opt *);
136 size_t dhcp_read_lease_fd(int, void **);
137 
138 #endif
139