xref: /dragonfly/contrib/dhcpcd/src/if.h (revision 493fd20c)
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2018 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef INTERFACE_H
29 #define INTERFACE_H
30 
31 #include <net/if.h>
32 #include <net/route.h>		/* for RTM_ADD et all */
33 #include <netinet/in.h>
34 #ifdef BSD
35 #include <netinet/in_var.h>	/* for IN_IFF_TENTATIVE et all */
36 #endif
37 
38 #include <ifaddrs.h>
39 
40 /* Some systems have in-built IPv4 DAD.
41  * However, we need them to do DAD at carrier up as well. */
42 #ifdef IN_IFF_TENTATIVE
43 #  ifdef __NetBSD__
44 #    define NOCARRIER_PRESERVE_IP
45 #  endif
46 #endif
47 
48 /*
49  * Systems which handle 1 address per alias.
50  * Currenly this is just Solaris.
51  * While Linux can do aliased addresses, it is only useful for their
52  * legacy ifconfig(8) tool which cannot display >1 IPv4 address
53  * (it can display many IPv6 addresses which makes the limitation odd).
54  * Linux has ip(8) which is a more feature rich tool, without the above
55  * restriction.
56  */
57 #ifndef ALIAS_ADDR
58 #  ifdef __sun
59 #    define ALIAS_ADDR
60 #  endif
61 #endif
62 
63 #include "config.h"
64 #include "dhcpcd.h"
65 #include "ipv4.h"
66 #include "ipv6.h"
67 #include "route.h"
68 
69 #define EUI64_ADDR_LEN			8
70 #define INFINIBAND_ADDR_LEN		20
71 
72 /* Linux 2.4 doesn't define this */
73 #ifndef ARPHRD_IEEE1394
74 #  define ARPHRD_IEEE1394		24
75 #endif
76 
77 /* The BSD's don't define this yet */
78 #ifndef ARPHRD_INFINIBAND
79 #  define ARPHRD_INFINIBAND		32
80 #endif
81 
82 /* Work out if we have a private address or not
83  * 10/8
84  * 172.16/12
85  * 192.168/16
86  */
87 #ifndef IN_PRIVATE
88 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
89 	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
90 	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
91 #endif
92 
93 #define RAW_EOF			1 << 0
94 #define RAW_PARTIALCSUM		2 << 0
95 
96 #ifndef CLLADDR
97 #ifdef AF_LINK
98 #  define CLLADDR(sdl) (const void *)((sdl)->sdl_data + (sdl)->sdl_nlen)
99 #endif
100 #endif
101 
102 #ifdef __sun
103 /* Solaris stupidly defines this for compat with BSD
104  * but then ignores it. */
105 #undef RTF_CLONING
106 
107 /* Solaris getifaddrs is very un-suitable for dhcpcd.
108  * See if-sun.c for details why. */
109 struct ifaddrs;
110 int if_getifaddrs(struct ifaddrs **);
111 #define	getifaddrs	if_getifaddrs
112 #endif
113 
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_sortinterfaces(struct dhcpcd_ctx *);
126 void if_free(struct interface *);
127 int if_domtu(const struct interface *, short int);
128 #define if_getmtu(ifp) if_domtu((ifp), 0)
129 #define if_setmtu(ifp, mtu) if_domtu((ifp), (mtu))
130 int if_carrier(struct interface *);
131 
132 /*
133  * Helper to decode an interface name of bge0:1 to
134  * devname = bge0, drvname = bge0, ppa = 0, lun = 1.
135  * If ppa or lun are invalid they are set to -1.
136  */
137 struct if_spec {
138 	char ifname[IF_NAMESIZE];
139 	char devname[IF_NAMESIZE];
140 	char drvname[IF_NAMESIZE];
141 	int ppa;
142 	int lun;
143 };
144 int if_nametospec(const char *, struct if_spec *);
145 
146 /* The below functions are provided by if-KERNEL.c */
147 int if_conf(struct interface *);
148 int if_init(struct interface *);
149 int if_getssid(struct interface *);
150 int if_vimaster(const struct dhcpcd_ctx *ctx, const char *);
151 unsigned short if_vlanid(const struct interface *);
152 int if_opensockets(struct dhcpcd_ctx *);
153 int if_opensockets_os(struct dhcpcd_ctx *);
154 void if_closesockets(struct dhcpcd_ctx *);
155 void if_closesockets_os(struct dhcpcd_ctx *);
156 int if_handlelink(struct dhcpcd_ctx *);
157 
158 /* dhcpcd uses the same routing flags as BSD.
159  * If the platform doesn't use these flags,
160  * map them in the platform interace file. */
161 #ifndef RTM_ADD
162 #define RTM_ADD		0x1	/* Add Route */
163 #define RTM_DELETE	0x2	/* Delete Route */
164 #define RTM_CHANGE	0x3	/* Change Metrics or flags */
165 #define RTM_GET		0x4	/* Report Metrics */
166 #endif
167 
168 /* Define SOCK_CLOEXEC and SOCK_NONBLOCK for systems that lack it.
169  * xsocket() in if.c will map them to fctnl FD_CLOEXEC and O_NONBLOCK. */
170 #ifdef SOCK_CLOEXEC
171 # define HAVE_SOCK_CLOEXEC
172 #else
173 # define SOCK_CLOEXEC	0x10000000
174 #endif
175 #ifdef SOCK_NONBLOCK
176 # define HAVE_SOCK_NONBLOCK
177 #else
178 # define SOCK_NONBLOCK	0x20000000
179 #endif
180 
181 int if_route(unsigned char, const struct rt *rt);
182 int if_initrt(struct dhcpcd_ctx *, int);
183 
184 #ifdef INET
185 int if_address(unsigned char, const struct ipv4_addr *);
186 int if_addrflags(const struct interface *, const struct in_addr *,
187     const char *);
188 
189 #endif
190 
191 #ifdef INET6
192 void if_setup_inet6(const struct interface *);
193 #ifdef IPV6_MANAGETEMPADDR
194 int ip6_use_tempaddr(const char *ifname);
195 int ip6_temp_preferred_lifetime(const char *ifname);
196 int ip6_temp_valid_lifetime(const char *ifname);
197 #else
198 #define ip6_use_tempaddr(a) (0)
199 #endif
200 
201 int if_address6(unsigned char, const struct ipv6_addr *);
202 int if_addrflags6(const struct interface *, const struct in6_addr *,
203     const char *);
204 int if_getlifetime6(struct ipv6_addr *);
205 
206 #else
207 #define if_checkipv6(a, b, c) (-1)
208 #endif
209 
210 int if_machinearch(char *, size_t);
211 int xsocket(int, int, int);
212 #endif
213