xref: /dragonfly/contrib/dhcpcd/src/if.h (revision a31d3627)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2020 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 INTERFACE_H
30 #define INTERFACE_H
31 
32 #include <net/if.h>
33 #include <net/route.h>		/* for RTM_ADD et all */
34 #include <netinet/in.h>
35 #ifdef BSD
36 #include <netinet/in_var.h>	/* for IN_IFF_TENTATIVE et all */
37 #endif
38 
39 #include <ifaddrs.h>
40 
41 /* If the interface does not support carrier status (ie PPP),
42  * dhcpcd can poll it for the relevant flags periodically */
43 #define IF_POLL_UP	100	/* milliseconds */
44 
45 /* Some systems have in-built IPv4 DAD.
46  * However, we need them to do DAD at carrier up as well. */
47 #ifdef IN_IFF_TENTATIVE
48 #  ifdef __NetBSD__
49 #    define NOCARRIER_PRESERVE_IP
50 #  endif
51 #endif
52 
53 /*
54  * Systems which handle 1 address per alias.
55  * Currenly this is just Solaris.
56  * While Linux can do aliased addresses, it is only useful for their
57  * legacy ifconfig(8) tool which cannot display >1 IPv4 address
58  * (it can display many IPv6 addresses which makes the limitation odd).
59  * Linux has ip(8) which is a more feature rich tool, without the above
60  * restriction.
61  */
62 #ifndef ALIAS_ADDR
63 #  ifdef __sun
64 #    define ALIAS_ADDR
65 #  endif
66 #endif
67 
68 #include "config.h"
69 
70 /* POSIX defines ioctl request as an int, which Solaris and musl use.
71  * Everyone else use an unsigned long, which happens to be the bigger one
72  * so we use that in our on wire API. */
73 #ifdef IOCTL_REQUEST_TYPE
74 typedef IOCTL_REQUEST_TYPE	ioctl_request_t;
75 #else
76 typedef unsigned long		ioctl_request_t;
77 #endif
78 
79 #include "dhcpcd.h"
80 #include "ipv4.h"
81 #include "ipv6.h"
82 #include "route.h"
83 
84 #define EUI64_ADDR_LEN			8
85 #define INFINIBAND_ADDR_LEN		20
86 
87 /* Linux 2.4 doesn't define this */
88 #ifndef ARPHRD_IEEE1394
89 #  define ARPHRD_IEEE1394		24
90 #endif
91 
92 /* The BSD's don't define this yet */
93 #ifndef ARPHRD_INFINIBAND
94 #  define ARPHRD_INFINIBAND		32
95 #endif
96 
97 /* Maximum frame length.
98  * Support jumbo frames and some extra. */
99 #define	FRAMEHDRLEN_MAX			14	/* only ethernet support */
100 #define	FRAMELEN_MAX			(FRAMEHDRLEN_MAX + 9216)
101 
102 #define UDPLEN_MAX			64 * 1024
103 
104 /* Work out if we have a private address or not
105  * 10/8
106  * 172.16/12
107  * 192.168/16
108  */
109 #ifndef IN_PRIVATE
110 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
111 	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
112 	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
113 #endif
114 
115 #ifndef CLLADDR
116 #ifdef AF_LINK
117 #  define CLLADDR(sdl) (const void *)((sdl)->sdl_data + (sdl)->sdl_nlen)
118 #endif
119 #endif
120 
121 #ifdef __sun
122 /* Solaris stupidly defines this for compat with BSD
123  * but then ignores it. */
124 #undef RTF_CLONING
125 
126 /* This interface is busted on DilOS at least.
127  * It used to work, but lukily Solaris can fall back to
128  * IP_PKTINFO. */
129 #undef IP_RECVIF
130 
131 /* Solaris getifaddrs is very un-suitable for dhcpcd.
132  * See if-sun.c for details why. */
133 struct ifaddrs;
134 int if_getifaddrs(struct ifaddrs **);
135 #define	getifaddrs	if_getifaddrs
136 int if_getsubnet(struct dhcpcd_ctx *, const char *, int, void *, size_t);
137 #endif
138 
139 int if_ioctl(struct dhcpcd_ctx *, ioctl_request_t, void *, size_t);
140 #ifdef HAVE_PLEDGE
141 #define	pioctl(ctx, req, data, len) if_ioctl((ctx), (req), (data), (len))
142 #else
143 #define	pioctl(ctx, req, data, len) ioctl((ctx)->pf_inet_fd, (req),(data),(len))
144 #endif
145 int if_getflags(struct interface *);
146 int if_setflag(struct interface *, short, short);
147 #define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING), 0)
148 #define if_down(ifp) if_setflag((ifp), 0, IFF_UP);
149 bool if_valid_hwaddr(const uint8_t *, size_t);
150 struct if_head *if_discover(struct dhcpcd_ctx *, struct ifaddrs **,
151     int, char * const *);
152 void if_markaddrsstale(struct if_head *);
153 void if_learnaddrs(struct dhcpcd_ctx *, struct if_head *, struct ifaddrs **);
154 void if_deletestaleaddrs(struct if_head *);
155 struct interface *if_find(struct if_head *, const char *);
156 struct interface *if_findindex(struct if_head *, unsigned int);
157 struct interface *if_loopback(struct dhcpcd_ctx *);
158 void if_free(struct interface *);
159 int if_domtu(const struct interface *, short int);
160 #define if_getmtu(ifp) if_domtu((ifp), 0)
161 #define if_setmtu(ifp, mtu) if_domtu((ifp), (mtu))
162 int if_carrier(struct interface *, const void *);
163 
164 #ifdef ALIAS_ADDR
165 int if_makealias(char *, size_t, const char *, int);
166 #endif
167 
168 int if_mtu_os(const struct interface *);
169 
170 /*
171  * Helper to decode an interface name of bge0:1 to
172  * devname = bge0, drvname = bge0, ppa = 0, lun = 1.
173  * If ppa or lun are invalid they are set to -1.
174  */
175 struct if_spec {
176 	char ifname[IF_NAMESIZE];
177 	char devname[IF_NAMESIZE];
178 	char drvname[IF_NAMESIZE];
179 	int ppa;
180 	int vlid;
181 	int lun;
182 };
183 int if_nametospec(const char *, struct if_spec *);
184 
185 /* The below functions are provided by if-KERNEL.c */
186 int os_init(void);
187 int if_conf(struct interface *);
188 int if_init(struct interface *);
189 int if_getssid(struct interface *);
190 int if_ignoregroup(int, const char *);
191 bool if_ignore(struct dhcpcd_ctx *, const char *);
192 int if_vimaster(struct dhcpcd_ctx *ctx, const char *);
193 unsigned short if_vlanid(const struct interface *);
194 char * if_getnetworknamespace(char *, size_t);
195 int if_opensockets(struct dhcpcd_ctx *);
196 int if_opensockets_os(struct dhcpcd_ctx *);
197 void if_closesockets(struct dhcpcd_ctx *);
198 void if_closesockets_os(struct dhcpcd_ctx *);
199 int if_handlelink(struct dhcpcd_ctx *);
200 int if_randomisemac(struct interface *);
201 int if_setmac(struct interface *ifp, void *, uint8_t);
202 
203 /* dhcpcd uses the same routing flags as BSD.
204  * If the platform doesn't use these flags,
205  * map them in the platform interace file. */
206 #ifndef RTM_ADD
207 #define RTM_ADD		0x1	/* Add Route */
208 #define RTM_DELETE	0x2	/* Delete Route */
209 #define RTM_CHANGE	0x3	/* Change Metrics or flags */
210 #define RTM_GET		0x4	/* Report Metrics */
211 #endif
212 
213 /* Define SOCK_CLOEXEC and SOCK_NONBLOCK for systems that lack it.
214  * xsocket() in if.c will map them to fctnl FD_CLOEXEC and O_NONBLOCK. */
215 #ifdef SOCK_CLOEXEC
216 # define HAVE_SOCK_CLOEXEC
217 #else
218 # define SOCK_CLOEXEC	0x10000000
219 #endif
220 #ifdef SOCK_NONBLOCK
221 # define HAVE_SOCK_NONBLOCK
222 #else
223 # define SOCK_NONBLOCK	0x20000000
224 #endif
225 #ifndef SOCK_CXNB
226 #define	SOCK_CXNB	SOCK_CLOEXEC | SOCK_NONBLOCK
227 #endif
228 int xsocket(int, int, int);
229 int xsocketpair(int, int, int, int[2]);
230 
231 int if_route(unsigned char, const struct rt *rt);
232 int if_initrt(struct dhcpcd_ctx *, rb_tree_t *, int);
233 
234 int if_missfilter(struct interface *, struct sockaddr *);
235 int if_missfilter_apply(struct dhcpcd_ctx *);
236 
237 #ifdef INET
238 int if_address(unsigned char, const struct ipv4_addr *);
239 int if_addrflags(const struct interface *, const struct in_addr *,
240     const char *);
241 
242 #endif
243 
244 #ifdef INET6
245 void if_disable_rtadv(void);
246 void if_setup_inet6(const struct interface *);
247 int ip6_forwarding(const char *ifname);
248 
249 struct ra;
250 struct ipv6_addr;
251 
252 int if_applyra(const struct ra *);
253 int if_address6(unsigned char, const struct ipv6_addr *);
254 int if_addrflags6(const struct interface *, const struct in6_addr *,
255     const char *);
256 int if_getlifetime6(struct ipv6_addr *);
257 
258 #else
259 #define if_checkipv6(a, b, c) (-1)
260 #endif
261 
262 int if_machinearch(char *, size_t);
263 struct interface *if_findifpfromcmsg(struct dhcpcd_ctx *,
264     struct msghdr *, int *);
265 
266 #ifdef __linux__
267 int if_linksocket(struct sockaddr_nl *, int, int);
268 int if_getnetlink(struct dhcpcd_ctx *, struct iovec *, int, int,
269     int (*)(struct dhcpcd_ctx *, void *, struct nlmsghdr *), void *);
270 #endif
271 #endif
272