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