1 /* 2 * sys-bsd.c - System-dependent procedures for setting up 3 * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.) 4 * 5 * Copyright (c) 1989 Carnegie Mellon University. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by Carnegie Mellon University. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * $Id: arp.c,v 1.30 1998/08/26 17:39:36 brian Exp $ 21 * 22 */ 23 24 /* 25 * TODO: 26 */ 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <net/if.h> 31 #include <net/route.h> 32 #include <net/if_dl.h> 33 #include <netinet/in.h> 34 #include <netinet/if_ether.h> 35 #include <arpa/inet.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/ip.h> 38 #include <sys/un.h> 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <sys/errno.h> 44 #include <sys/sysctl.h> 45 #include <unistd.h> 46 47 #include "mbuf.h" 48 #include "log.h" 49 #include "id.h" 50 #include "timer.h" 51 #include "fsm.h" 52 #include "defs.h" 53 #include "iplist.h" 54 #include "throughput.h" 55 #include "slcompress.h" 56 #include "lqr.h" 57 #include "hdlc.h" 58 #include "ipcp.h" 59 #include "filter.h" 60 #include "descriptor.h" 61 #include "lcp.h" 62 #include "ccp.h" 63 #include "link.h" 64 #include "mp.h" 65 #include "bundle.h" 66 #include "arp.h" 67 68 /* 69 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 70 * if it exists. 71 */ 72 #define SET_SA_FAMILY(addr, family) \ 73 memset((char *) &(addr), '\0', sizeof(addr)); \ 74 addr.sa_family = (family); \ 75 addr.sa_len = sizeof(addr); 76 77 78 #if RTM_VERSION >= 3 79 80 /* 81 * arp_SetProxy - Make a proxy ARP entry for the peer. 82 */ 83 static struct { 84 struct rt_msghdr hdr; 85 struct sockaddr_inarp dst; 86 struct sockaddr_dl hwa; 87 char extra[128]; 88 } arpmsg; 89 90 static int 91 arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add, int s) 92 { 93 int routes; 94 95 /* 96 * Get the hardware address of an interface on the same subnet as our local 97 * address. 98 */ 99 100 memset(&arpmsg, 0, sizeof arpmsg); 101 if (!get_ether_addr(s, addr, &arpmsg.hwa)) { 102 log_Printf(LogWARN, "%s: Cannot determine ethernet address for proxy ARP\n", 103 inet_ntoa(addr)); 104 return 0; 105 } 106 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 107 if (routes < 0) { 108 log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n", 109 strerror(errno)); 110 return 0; 111 } 112 arpmsg.hdr.rtm_type = add ? RTM_ADD : RTM_DELETE; 113 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; 114 arpmsg.hdr.rtm_version = RTM_VERSION; 115 arpmsg.hdr.rtm_seq = ++bundle->routing_seq; 116 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 117 arpmsg.hdr.rtm_inits = RTV_EXPIRE; 118 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); 119 arpmsg.dst.sin_family = AF_INET; 120 arpmsg.dst.sin_addr.s_addr = addr.s_addr; 121 arpmsg.dst.sin_other = SIN_PROXY; 122 123 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 124 + arpmsg.hwa.sdl_len; 125 126 127 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 && 128 !(!add && errno == ESRCH)) { 129 log_Printf(LogERROR, "%s proxy arp entry %s: %s\n", 130 add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno)); 131 close(routes); 132 return 0; 133 } 134 close(routes); 135 return 1; 136 } 137 138 int 139 arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 140 { 141 142 return (arp_ProxySub(bundle, addr, 1, s)); 143 } 144 145 /* 146 * arp_ClearProxy - Delete the proxy ARP entry for the peer. 147 */ 148 int 149 arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 150 { 151 152 return (arp_ProxySub(bundle, addr, 0, s)); 153 } 154 155 #else /* RTM_VERSION */ 156 157 /* 158 * arp_SetProxy - Make a proxy ARP entry for the peer. 159 */ 160 int 161 arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 162 { 163 struct arpreq arpreq; 164 struct { 165 struct sockaddr_dl sdl; 166 char space[128]; 167 } dls; 168 169 memset(&arpreq, '\0', sizeof arpreq); 170 171 /* 172 * Get the hardware address of an interface on the same subnet as our local 173 * address. 174 */ 175 if (!get_ether_addr(s, addr, &dls.sdl)) { 176 log_Printf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n"); 177 return 0; 178 } 179 arpreq.arp_ha.sa_len = sizeof(struct sockaddr); 180 arpreq.arp_ha.sa_family = AF_UNSPEC; 181 memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen); 182 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 183 ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 184 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 185 if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) { 186 log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n", 187 strerror(errno)); 188 return 0; 189 } 190 return 1; 191 } 192 193 /* 194 * arp_ClearProxy - Delete the proxy ARP entry for the peer. 195 */ 196 int 197 arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 198 { 199 struct arpreq arpreq; 200 201 memset(&arpreq, '\0', sizeof arpreq); 202 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 203 ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 204 if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) { 205 log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n", 206 strerror(errno)); 207 return 0; 208 } 209 return 1; 210 } 211 212 #endif /* RTM_VERSION */ 213 214 215 /* 216 * get_ether_addr - get the hardware address of an interface on the 217 * the same subnet as ipaddr. 218 */ 219 220 int 221 get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr) 222 { 223 int mib[6], sa_len, skip, b; 224 size_t needed; 225 char *buf, *ptr, *end; 226 struct if_msghdr *ifm; 227 struct ifa_msghdr *ifam; 228 struct sockaddr *sa; 229 struct sockaddr_dl *dl; 230 struct sockaddr_in *ifa, *mask; 231 232 mib[0] = CTL_NET; 233 mib[1] = PF_ROUTE; 234 mib[2] = 0; 235 mib[3] = 0; 236 mib[4] = NET_RT_IFLIST; 237 mib[5] = 0; 238 239 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 240 log_Printf(LogERROR, "get_ether_addr: sysctl: estimate: %s\n", 241 strerror(errno)); 242 return 0; 243 } 244 245 if ((buf = malloc(needed)) == NULL) 246 return 0; 247 248 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 249 free(buf); 250 return 0; 251 } 252 end = buf + needed; 253 254 ptr = buf; 255 while (ptr < end) { 256 ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 257 if (ifm->ifm_type != RTM_IFINFO) 258 break; 259 dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 260 skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | 261 IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST); 262 ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 263 while (ptr < end) { 264 ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */ 265 if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */ 266 break; 267 sa = (struct sockaddr *)(ifam+1); /* pile of sa's at end */ 268 ptr += ifam->ifam_msglen; 269 if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) != 270 (RTA_NETMASK|RTA_IFA)) 271 continue; 272 /* Found a candidate. Do the addresses match ? */ 273 if (log_IsKept(LogDEBUG) && 274 ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen) 275 log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n", 276 dl->sdl_nlen, dl->sdl_data); 277 b = 1; 278 ifa = mask = NULL; 279 while (b < (RTA_NETMASK|RTA_IFA) && sa < (struct sockaddr *)ptr) { 280 switch (b) { 281 case RTA_IFA: 282 ifa = (struct sockaddr_in *)sa; 283 break; 284 case RTA_NETMASK: 285 /* 286 * Careful here ! this sockaddr doesn't have sa_family set to 287 * AF_INET, and is only 8 bytes big ! I have no idea why ! 288 */ 289 mask = (struct sockaddr_in *)sa; 290 break; 291 } 292 if (ifam->ifam_addrs & b) { 293 #define ALN sizeof(ifa->sin_addr.s_addr) 294 sa_len = sa->sa_len > 0 ? ((sa->sa_len-1)|(ALN-1))+1 : ALN; 295 sa = (struct sockaddr *)((char *)sa + sa_len); 296 } 297 b <<= 1; 298 } 299 if (log_IsKept(LogDEBUG)) { 300 char a[16]; 301 strncpy(a, inet_ntoa(mask->sin_addr), sizeof a - 1); 302 a[sizeof a - 1] = '\0'; 303 log_Printf(LogDEBUG, "Check addr %s, mask %s\n", 304 inet_ntoa(ifa->sin_addr), a); 305 } 306 if (ifa->sin_family == AF_INET && 307 (ifa->sin_addr.s_addr & mask->sin_addr.s_addr) == 308 (ipaddr.s_addr & mask->sin_addr.s_addr)) { 309 log_Printf(LogPHASE, "Found interface %.*s for %s\n", 310 dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr)); 311 memcpy(hwaddr, dl, dl->sdl_len); 312 free(buf); 313 return 1; 314 } 315 } 316 } 317 free(buf); 318 319 return 0; 320 } 321