xref: /dragonfly/contrib/dhcpcd/src/if.h (revision 7d2302ac)
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 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 /* Some systems have in-built IPv4 DAD.
42  * However, we need them to do DAD at carrier up as well. */
43 #ifdef IN_IFF_TENTATIVE
44 #  ifdef __NetBSD__
45 #    define NOCARRIER_PRESERVE_IP
46 #  endif
47 #endif
48 
49 /*
50  * Systems which handle 1 address per alias.
51  * Currenly this is just Solaris.
52  * While Linux can do aliased addresses, it is only useful for their
53  * legacy ifconfig(8) tool which cannot display >1 IPv4 address
54  * (it can display many IPv6 addresses which makes the limitation odd).
55  * Linux has ip(8) which is a more feature rich tool, without the above
56  * restriction.
57  */
58 #ifndef ALIAS_ADDR
59 #  ifdef __sun
60 #    define ALIAS_ADDR
61 #  endif
62 #endif
63 
64 #include "config.h"
65 #include "dhcpcd.h"
66 #include "ipv4.h"
67 #include "ipv6.h"
68 #include "route.h"
69 
70 #define EUI64_ADDR_LEN			8
71 #define INFINIBAND_ADDR_LEN		20
72 
73 /* Linux 2.4 doesn't define this */
74 #ifndef ARPHRD_IEEE1394
75 #  define ARPHRD_IEEE1394		24
76 #endif
77 
78 /* The BSD's don't define this yet */
79 #ifndef ARPHRD_INFINIBAND
80 #  define ARPHRD_INFINIBAND		32
81 #endif
82 
83 /* Work out if we have a private address or not
84  * 10/8
85  * 172.16/12
86  * 192.168/16
87  */
88 #ifndef IN_PRIVATE
89 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
90 	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
91 	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
92 #endif
93 
94 #ifndef CLLADDR
95 #ifdef AF_LINK
96 #  define CLLADDR(sdl) (const void *)((sdl)->sdl_data + (sdl)->sdl_nlen)
97 #endif
98 #endif
99 
100 #ifdef __sun
101 /* Solaris stupidly defines this for compat with BSD
102  * but then ignores it. */
103 #undef RTF_CLONING
104 
105 /* Solaris getifaddrs is very un-suitable for dhcpcd.
106  * See if-sun.c for details why. */
107 struct ifaddrs;
108 int if_getifaddrs(struct ifaddrs **);
109 #define	getifaddrs	if_getifaddrs
110 int if_getsubnet(struct dhcpcd_ctx *, const char *, int, void *, size_t);
111 #endif
112 
113 int if_getflags(struct interface *ifp);
114 int if_setflag(struct interface *ifp, short flag);
115 #define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING))
116 bool if_valid_hwaddr(const uint8_t *, size_t);
117 struct if_head *if_discover(struct dhcpcd_ctx *, struct ifaddrs **,
118     int, char * const *);
119 void if_markaddrsstale(struct if_head *);
120 void if_learnaddrs(struct dhcpcd_ctx *, struct if_head *, struct ifaddrs **);
121 void if_deletestaleaddrs(struct if_head *);
122 struct interface *if_find(struct if_head *, const char *);
123 struct interface *if_findindex(struct if_head *, unsigned int);
124 struct interface *if_loopback(struct dhcpcd_ctx *);
125 void if_free(struct interface *);
126 int if_domtu(const struct interface *, short int);
127 #define if_getmtu(ifp) if_domtu((ifp), 0)
128 #define if_setmtu(ifp, mtu) if_domtu((ifp), (mtu))
129 int if_carrier(struct interface *);
130 
131 #ifdef ALIAS_ADDR
132 int if_makealias(char *, size_t, const char *, int);
133 #endif
134 
135 int if_mtu_os(const struct interface *);
136 
137 /*
138  * Helper to decode an interface name of bge0:1 to
139  * devname = bge0, drvname = bge0, ppa = 0, lun = 1.
140  * If ppa or lun are invalid they are set to -1.
141  */
142 struct if_spec {
143 	char ifname[IF_NAMESIZE];
144 	char devname[IF_NAMESIZE];
145 	char drvname[IF_NAMESIZE];
146 	int ppa;
147 	int lun;
148 };
149 int if_nametospec(const char *, struct if_spec *);
150 
151 /* The below functions are provided by if-KERNEL.c */
152 int if_conf(struct interface *);
153 int if_init(struct interface *);
154 int if_getssid(struct interface *);
155 bool if_ignore(struct dhcpcd_ctx *, const char *);
156 int if_vimaster(const struct dhcpcd_ctx *ctx, const char *);
157 unsigned short if_vlanid(const struct interface *);
158 int if_opensockets(struct dhcpcd_ctx *);
159 int if_opensockets_os(struct dhcpcd_ctx *);
160 void if_closesockets(struct dhcpcd_ctx *);
161 void if_closesockets_os(struct dhcpcd_ctx *);
162 int if_handlelink(struct dhcpcd_ctx *);
163 
164 /* dhcpcd uses the same routing flags as BSD.
165  * If the platform doesn't use these flags,
166  * map them in the platform interace file. */
167 #ifndef RTM_ADD
168 #define RTM_ADD		0x1	/* Add Route */
169 #define RTM_DELETE	0x2	/* Delete Route */
170 #define RTM_CHANGE	0x3	/* Change Metrics or flags */
171 #define RTM_GET		0x4	/* Report Metrics */
172 #endif
173 
174 /* Define SOCK_CLOEXEC and SOCK_NONBLOCK for systems that lack it.
175  * xsocket() in if.c will map them to fctnl FD_CLOEXEC and O_NONBLOCK. */
176 #ifdef SOCK_CLOEXEC
177 # define HAVE_SOCK_CLOEXEC
178 #else
179 # define SOCK_CLOEXEC	0x10000000
180 #endif
181 #ifdef SOCK_NONBLOCK
182 # define HAVE_SOCK_NONBLOCK
183 #else
184 # define SOCK_NONBLOCK	0x20000000
185 #endif
186 
187 int if_route(unsigned char, const struct rt *rt);
188 int if_initrt(struct dhcpcd_ctx *, rb_tree_t *, int);
189 
190 #ifdef INET
191 int if_address(unsigned char, const struct ipv4_addr *);
192 int if_addrflags(const struct interface *, const struct in_addr *,
193     const char *);
194 
195 #endif
196 
197 #ifdef INET6
198 void if_setup_inet6(const struct interface *);
199 #ifdef IPV6_MANAGETEMPADDR
200 int ip6_use_tempaddr(const char *ifname);
201 int ip6_temp_preferred_lifetime(const char *ifname);
202 int ip6_temp_valid_lifetime(const char *ifname);
203 #else
204 #define ip6_use_tempaddr(a) (0)
205 #endif
206 int ip6_forwarding(const char *ifname);
207 
208 int if_address6(unsigned char, const struct ipv6_addr *);
209 int if_addrflags6(const struct interface *, const struct in6_addr *,
210     const char *);
211 int if_getlifetime6(struct ipv6_addr *);
212 
213 #else
214 #define if_checkipv6(a, b, c) (-1)
215 #endif
216 
217 int if_machinearch(char *, size_t);
218 struct interface *if_findifpfromcmsg(struct dhcpcd_ctx *,
219     struct msghdr *, int *);
220 int xsocket(int, int, int);
221 #endif
222