arp.c revision 36285
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.27.2.15 1998/05/01 19:23:46 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 "ipcp.h" 57#include "filter.h" 58#include "descriptor.h" 59#include "lqr.h" 60#include "hdlc.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 */ 83static struct { 84 struct rt_msghdr hdr; 85 struct sockaddr_inarp dst; 86 struct sockaddr_dl hwa; 87 char extra[128]; 88} arpmsg; 89 90static int arpmsg_valid; 91 92int 93arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 94{ 95 int routes; 96 97 /* 98 * Get the hardware address of an interface on the same subnet as our local 99 * address. 100 */ 101 memset(&arpmsg, 0, sizeof arpmsg); 102 if (!get_ether_addr(s, addr, &arpmsg.hwa)) { 103 log_Printf(LogERROR, "Cannot determine ethernet address for proxy ARP\n"); 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 = RTM_ADD; 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 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 126 log_Printf(LogERROR, "Add proxy arp entry: %s\n", strerror(errno)); 127 close(routes); 128 return 0; 129 } 130 close(routes); 131 arpmsg_valid = 1; 132 return 1; 133} 134 135/* 136 * arp_ClearProxy - Delete the proxy ARP entry for the peer. 137 */ 138int 139arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 140{ 141 int routes; 142 143 if (!arpmsg_valid) 144 return 0; 145 arpmsg_valid = 0; 146 147 arpmsg.hdr.rtm_type = RTM_DELETE; 148 arpmsg.hdr.rtm_seq = ++bundle->routing_seq; 149 150 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 151 if (routes < 0) { 152 log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n", 153 strerror(errno)); 154 return 0; 155 } 156 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 157 log_Printf(LogERROR, "Delete proxy arp entry: %s\n", strerror(errno)); 158 close(routes); 159 return 0; 160 } 161 close(routes); 162 return 1; 163} 164 165#else /* RTM_VERSION */ 166 167/* 168 * arp_SetProxy - Make a proxy ARP entry for the peer. 169 */ 170int 171arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 172{ 173 struct arpreq arpreq; 174 struct { 175 struct sockaddr_dl sdl; 176 char space[128]; 177 } dls; 178 179 memset(&arpreq, '\0', sizeof arpreq); 180 181 /* 182 * Get the hardware address of an interface on the same subnet as our local 183 * address. 184 */ 185 if (!get_ether_addr(s, addr, &dls.sdl)) { 186 log_Printf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n"); 187 return 0; 188 } 189 arpreq.arp_ha.sa_len = sizeof(struct sockaddr); 190 arpreq.arp_ha.sa_family = AF_UNSPEC; 191 memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen); 192 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 193 ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 194 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 195 if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) { 196 log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n", strerror(errno)); 197 return 0; 198 } 199 return 1; 200} 201 202/* 203 * arp_ClearProxy - Delete the proxy ARP entry for the peer. 204 */ 205int 206arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 207{ 208 struct arpreq arpreq; 209 210 memset(&arpreq, '\0', sizeof arpreq); 211 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 212 ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 213 if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) { 214 log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n", strerror(errno)); 215 return 0; 216 } 217 return 1; 218} 219 220#endif /* RTM_VERSION */ 221 222 223/* 224 * get_ether_addr - get the hardware address of an interface on the 225 * the same subnet as ipaddr. 226 */ 227 228int 229get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr) 230{ 231 int mib[6], sa_len, skip, b; 232 size_t needed; 233 char *buf, *ptr, *end; 234 struct if_msghdr *ifm; 235 struct ifa_msghdr *ifam; 236 struct sockaddr *sa; 237 struct sockaddr_dl *dl; 238 struct sockaddr_in *ifa, *mask; 239 240 mib[0] = CTL_NET; 241 mib[1] = PF_ROUTE; 242 mib[2] = 0; 243 mib[3] = 0; 244 mib[4] = NET_RT_IFLIST; 245 mib[5] = 0; 246 247 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 248 log_Printf(LogERROR, "get_ether_addr: sysctl: estimate: %s\n", 249 strerror(errno)); 250 return 0; 251 } 252 253 if ((buf = malloc(needed)) == NULL) 254 return 0; 255 256 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 257 free(buf); 258 return 0; 259 } 260 end = buf + needed; 261 262 ptr = buf; 263 while (ptr < end) { 264 ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 265 if (ifm->ifm_type != RTM_IFINFO) 266 break; 267 dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 268 skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | 269 IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST); 270 ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 271 while (ptr < end) { 272 ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */ 273 if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */ 274 break; 275 sa = (struct sockaddr *)(ifam+1); /* pile of sa's at end */ 276 ptr += ifam->ifam_msglen; 277 if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) != 278 (RTA_NETMASK|RTA_IFA)) 279 continue; 280 /* Found a candidate. Do the addresses match ? */ 281 if (log_IsKept(LogDEBUG) && 282 ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen) 283 log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n", 284 dl->sdl_nlen, dl->sdl_data); 285 b = 1; 286 ifa = mask = NULL; 287 while (b < (RTA_NETMASK|RTA_IFA) && sa < (struct sockaddr *)ptr) { 288 switch (b) { 289 case RTA_IFA: 290 ifa = (struct sockaddr_in *)sa; 291 break; 292 case RTA_NETMASK: 293 /* 294 * Careful here ! this sockaddr doesn't have sa_family set to 295 * AF_INET, and is only 8 bytes big ! I have no idea why ! 296 */ 297 mask = (struct sockaddr_in *)sa; 298 break; 299 } 300 if (ifam->ifam_addrs & b) { 301#define ALN sizeof(ifa->sin_addr.s_addr) 302 sa_len = sa->sa_len > 0 ? ((sa->sa_len-1)|(ALN-1))+1 : ALN; 303 sa = (struct sockaddr *)((char *)sa + sa_len); 304 } 305 b <<= 1; 306 } 307 if (log_IsKept(LogDEBUG)) { 308 char a[16]; 309 strncpy(a, inet_ntoa(mask->sin_addr), sizeof a - 1); 310 a[sizeof a - 1] = '\0'; 311 log_Printf(LogDEBUG, "Check addr %s, mask %s\n", 312 inet_ntoa(ifa->sin_addr), a); 313 } 314 if (ifa->sin_family == AF_INET && 315 (ifa->sin_addr.s_addr & mask->sin_addr.s_addr) == 316 (ipaddr.s_addr & mask->sin_addr.s_addr)) { 317 log_Printf(LogPHASE, "Found interface %.*s for %s\n", 318 dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr)); 319 memcpy(hwaddr, dl, dl->sdl_len); 320 free(buf); 321 return 1; 322 } 323 } 324 } 325 free(buf); 326 327 return 0; 328} 329