1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2015 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 #ifdef __FreeBSD__
33 #include <net/if_var.h>
34 #endif
35 #include <net/route.h>		/* for RTM_ADD et all */
36 #include <netinet/in.h>
37 #ifdef BSD
38 #include <netinet/in_var.h>	/* for IN_IFF_TENTATIVE et all */
39 #endif
40 
41 /* Some systems have route metrics.
42  * OpenBSD route priority is not this. */
43 #ifndef HAVE_ROUTE_METRIC
44 # if defined(__linux__)
45 #  define HAVE_ROUTE_METRIC 1
46 # endif
47 #endif
48 
49 /* Some systems have in-built IPv4 DAD.
50  * However, we need them to do DAD at carrier up as well. */
51 #ifdef IN_IFF_TENTATIVE
52 #  ifdef __NetBSD__
53 #    define NOCARRIER_PRESERVE_IP
54 #  endif
55 #endif
56 
57 #include "config.h"
58 #include "dhcpcd.h"
59 #include "ipv4.h"
60 #include "ipv6.h"
61 
62 #define EUI64_ADDR_LEN			8
63 #define INFINIBAND_ADDR_LEN		20
64 
65 /* Linux 2.4 doesn't define this */
66 #ifndef ARPHRD_IEEE1394
67 #  define ARPHRD_IEEE1394		24
68 #endif
69 
70 /* The BSD's don't define this yet */
71 #ifndef ARPHRD_INFINIBAND
72 #  define ARPHRD_INFINIBAND		32
73 #endif
74 
75 /* Work out if we have a private address or not
76  * 10/8
77  * 172.16/12
78  * 192.168/16
79  */
80 #ifndef IN_PRIVATE
81 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
82 	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
83 	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
84 #endif
85 
86 #define LINKLOCAL_ADDR	0xa9fe0000
87 #define LINKLOCAL_MASK	IN_CLASSB_NET
88 #define LINKLOCAL_BRDC	(LINKLOCAL_ADDR | ~LINKLOCAL_MASK)
89 
90 #ifndef IN_LINKLOCAL
91 # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR)
92 #endif
93 
94 #define RAW_EOF			1 << 0
95 #define RAW_PARTIALCSUM		2 << 0
96 
97 int if_setflag(struct interface *ifp, short flag);
98 #define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING))
99 struct if_head *if_discover(struct dhcpcd_ctx *, int, char * const *);
100 struct interface *if_find(struct if_head *, const char *);
101 struct interface *if_findindex(struct if_head *, unsigned int);
102 void if_sortinterfaces(struct dhcpcd_ctx *);
103 void if_free(struct interface *);
104 int if_domtu(const char *, short int);
105 #define if_getmtu(iface) if_domtu(iface, 0)
106 #define if_setmtu(iface, mtu) if_domtu(iface, mtu)
107 int if_carrier(struct interface *);
108 int split_interface_lease(const char *ifname, int *ifname_len,
109 			  const char **lease_identifier);
110 
111 /* The below functions are provided by if-KERNEL.c */
112 int if_conf(struct interface *);
113 int if_init(struct interface *);
114 int if_getssid(struct interface *);
115 int if_vimaster(const char *);
116 int if_openlinksocket(void);
117 int if_managelink(struct dhcpcd_ctx *);
118 
119 /* dhcpcd uses the same routing flags as BSD.
120  * If the platform doesn't use these flags,
121  * map them in the platform interace file. */
122 #ifndef RTM_ADD
123 #define RTM_ADD		0x1	/* Add Route */
124 #define RTM_DELETE	0x2	/* Delete Route */
125 #define RTM_CHANGE	0x3	/* Change Metrics or flags */
126 #define RTM_GET		0x4	/* Report Metrics */
127 #endif
128 
129 #ifdef INET
130 extern const char *if_pfname;
131 int if_openrawsocket(struct interface *, uint16_t);
132 ssize_t if_sendrawpacket(const struct interface *,
133     uint16_t, const void *, size_t, const uint8_t *dest_hw_addr);
134 ssize_t if_readrawpacket(struct interface *, uint16_t, void *, size_t, int *);
135 
136 int if_address(const struct interface *,
137     const struct in_addr *, const struct in_addr *,
138     const struct in_addr *, int);
139 #define if_addaddress(ifp, addr, net, brd)	\
140 	if_address(ifp, addr, net, brd, 1)
141 #define if_deladdress(ifp, addr, net)		\
142 	if_address(ifp, addr, net, NULL, -1)
143 
144 int if_addrflags(const struct in_addr *, const struct interface *);
145 
146 int if_route(unsigned char, const struct rt *rt);
147 int if_initrt(struct interface *);
148 #endif
149 
150 #ifdef INET6
151 int if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *, int);
152 #ifdef IPV6_MANAGETEMPADDR
153 int ip6_use_tempaddr(const char *ifname);
154 int ip6_temp_preferred_lifetime(const char *ifname);
155 int ip6_temp_valid_lifetime(const char *ifname);
156 #else
157 #define ip6_use_tempaddr(a) (0)
158 #endif
159 
160 int if_address6(const struct ipv6_addr *, int);
161 #define if_addaddress6(a) if_address6(a, 1)
162 #define if_deladdress6(a) if_address6(a, -1)
163 
164 int if_addrflags6(const struct in6_addr *, const struct interface *);
165 int if_getlifetime6(struct ipv6_addr *);
166 
167 int if_route6(unsigned char, const struct rt6 *rt);
168 int if_initrt6(struct interface *);
169 #else
170 #define if_checkipv6(a, b, c) (-1)
171 #endif
172 
173 int if_machinearch(char *, size_t);
174 #endif
175