arp.c revision 28679
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.12 1997/06/09 03:27:11 brian Exp $ 21 * 22 */ 23 24/* 25 * TODO: 26 */ 27 28#include <sys/ioctl.h> 29#include <sys/types.h> 30#include <sys/uio.h> 31#include <sys/socket.h> 32#include <sys/time.h> 33#include <sys/errno.h> 34#include <unistd.h> 35#include <string.h> 36 37#include <net/if.h> 38#include <net/if_var.h> 39#include <net/route.h> 40#include <net/if_dl.h> 41#include <netinet/in.h> 42#include <stdio.h> 43#include <fcntl.h> 44#ifdef __bsdi__ 45#include <kvm.h> 46#endif 47#include <net/if_types.h> 48#include <netinet/in_var.h> 49#include <netinet/if_ether.h> 50#include "log.h" 51 52#if RTM_VERSION >= 3 53#include <netinet/if_ether.h> 54#endif 55 56static int rtm_seq; 57 58static int get_ether_addr(int, u_long, struct sockaddr_dl *); 59 60#define BCOPY(s, d, l) memcpy(d, s, l) 61#define BZERO(s, n) memset(s, 0, n) 62/* 63 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 64 * if it exists. 65 */ 66#define SET_SA_FAMILY(addr, family) \ 67 BZERO((char *) &(addr), sizeof(addr)); \ 68 addr.sa_family = (family); \ 69 addr.sa_len = sizeof(addr); 70 71 72#if RTM_VERSION >= 3 73 74/* 75 * sifproxyarp - Make a proxy ARP entry for the peer. 76 */ 77static struct { 78 struct rt_msghdr hdr; 79 struct sockaddr_inarp dst; 80 struct sockaddr_dl hwa; 81 char extra[128]; 82} arpmsg; 83 84static int arpmsg_valid; 85 86int 87sifproxyarp(int unit, u_long hisaddr) 88{ 89 int routes; 90 91 /* 92 * Get the hardware address of an interface on the same subnet as our local 93 * address. 94 */ 95 memset(&arpmsg, 0, sizeof(arpmsg)); 96 if (!get_ether_addr(unit, hisaddr, &arpmsg.hwa)) { 97 LogPrintf(LogERROR, "Cannot determine ethernet address" 98 " for proxy ARP\n"); 99 return 0; 100 } 101 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) { 102 LogPrintf(LogERROR, "sifproxyarp: opening routing socket: %s\n", 103 strerror(errno)); 104 return 0; 105 } 106 arpmsg.hdr.rtm_type = RTM_ADD; 107 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; 108 arpmsg.hdr.rtm_version = RTM_VERSION; 109 arpmsg.hdr.rtm_seq = ++rtm_seq; 110 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 111 arpmsg.hdr.rtm_inits = RTV_EXPIRE; 112 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); 113 arpmsg.dst.sin_family = AF_INET; 114 arpmsg.dst.sin_addr.s_addr = hisaddr; 115 arpmsg.dst.sin_other = SIN_PROXY; 116 117 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 118 + arpmsg.hwa.sdl_len; 119 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 120 LogPrintf(LogERROR, "Add proxy arp entry: %s\n", strerror(errno)); 121 close(routes); 122 return 0; 123 } 124 close(routes); 125 arpmsg_valid = 1; 126 return 1; 127} 128 129/* 130 * cifproxyarp - Delete the proxy ARP entry for the peer. 131 */ 132int 133cifproxyarp(int unit, u_long hisaddr) 134{ 135 int routes; 136 137 if (!arpmsg_valid) 138 return 0; 139 arpmsg_valid = 0; 140 141 arpmsg.hdr.rtm_type = RTM_DELETE; 142 arpmsg.hdr.rtm_seq = ++rtm_seq; 143 144 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) { 145 LogPrintf(LogERROR, "sifproxyarp: opening routing socket: %s\n", 146 strerror(errno)); 147 return 0; 148 } 149 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 150 LogPrintf(LogERROR, "Delete proxy arp entry: %s\n", strerror(errno)); 151 close(routes); 152 return 0; 153 } 154 close(routes); 155 return 1; 156} 157 158#else /* RTM_VERSION */ 159 160/* 161 * sifproxyarp - Make a proxy ARP entry for the peer. 162 */ 163int 164sifproxyarp(int unit, u_long hisaddr) 165{ 166 struct arpreq arpreq; 167 struct { 168 struct sockaddr_dl sdl; 169 char space[128]; 170 } dls; 171 172 BZERO(&arpreq, sizeof(arpreq)); 173 174 /* 175 * Get the hardware address of an interface on the same subnet as our local 176 * address. 177 */ 178 if (!get_ether_addr(unit, hisaddr, &dls.sdl)) { 179 LogPrintf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n"); 180 return 0; 181 } 182 arpreq.arp_ha.sa_len = sizeof(struct sockaddr); 183 arpreq.arp_ha.sa_family = AF_UNSPEC; 184 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen); 185 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 186 ((struct sockaddr_in *) & arpreq.arp_pa)->sin_addr.s_addr = hisaddr; 187 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 188 if (ioctl(unit, SIOCSARP, (caddr_t) & arpreq) < 0) { 189 LogPrintf(LogERROR, "sifproxyarp: ioctl(SIOCSARP): \n"); 190 return 0; 191 } 192 return 1; 193} 194 195/* 196 * cifproxyarp - Delete the proxy ARP entry for the peer. 197 */ 198int 199cifproxyarp(int unit, u_long hisaddr) 200{ 201 struct arpreq arpreq; 202 203 BZERO(&arpreq, sizeof(arpreq)); 204 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 205 ((struct sockaddr_in *) & arpreq.arp_pa)->sin_addr.s_addr = hisaddr; 206 if (ioctl(unit, SIOCDARP, (caddr_t) & arpreq) < 0) { 207 LogPrintf(LogERROR, "cifproxyarp: ioctl(SIOCDARP): \n"); 208 return 0; 209 } 210 return 1; 211} 212 213#endif /* RTM_VERSION */ 214 215 216/* 217 * get_ether_addr - get the hardware address of an interface on the 218 * the same subnet as ipaddr. 219 */ 220#define MAX_IFS 32 221 222int 223get_ether_addr(int s, u_long ipaddr, struct sockaddr_dl * hwaddr) 224{ 225 struct ifreq *ifr, *ifend, *ifp; 226 u_long ina, mask; 227 struct sockaddr_dl *dla; 228 struct ifreq ifreq; 229 struct ifconf ifc; 230 struct ifreq ifs[MAX_IFS]; 231 232 ifc.ifc_len = sizeof(ifs); 233 ifc.ifc_req = ifs; 234 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { 235 LogPrintf(LogERROR, "get_ether_addr: ioctl(SIOCGIFCONF): \n"); 236 return 0; 237 } 238 239 /* 240 * Scan through looking for an interface with an Internet address on the 241 * same subnet as `ipaddr'. 242 */ 243 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); 244 for (ifr = ifc.ifc_req; ifr < ifend;) { 245 if (ifr->ifr_addr.sa_family == AF_INET) { 246 ina = ((struct sockaddr_in *) & ifr->ifr_addr)->sin_addr.s_addr; 247 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); 248 ifreq.ifr_name[sizeof(ifreq.ifr_name) - 1] = '\0'; 249 250 /* 251 * Check that the interface is up, and not point-to-point or loopback. 252 */ 253 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) 254 continue; 255 if ((ifreq.ifr_flags & 256 (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)) 257 != (IFF_UP | IFF_BROADCAST)) 258 goto nextif; 259 260 /* 261 * Get its netmask and check that it's on the right subnet. 262 */ 263 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0) 264 continue; 265 mask = ((struct sockaddr_in *) & ifreq.ifr_addr)->sin_addr.s_addr; 266 if ((ipaddr & mask) != (ina & mask)) 267 goto nextif; 268 269 break; 270 } 271nextif: 272 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr + ifr->ifr_addr.sa_len); 273 } 274 275 if (ifr >= ifend) 276 return 0; 277 LogPrintf(LogPHASE, "Found interface %s for proxy arp\n", ifr->ifr_name); 278 279 /* 280 * Now scan through again looking for a link-level address for this 281 * interface. 282 */ 283 ifp = ifr; 284 for (ifr = ifc.ifc_req; ifr < ifend;) { 285 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 286 && ifr->ifr_addr.sa_family == AF_LINK) { 287 288 /* 289 * Found the link-level address - copy it out 290 */ 291 dla = (struct sockaddr_dl *) & ifr->ifr_addr; 292#ifdef __bsdi__ 293 if (dla->sdl_alen == 0) 294 kmemgetether(ifr->ifr_name, dla); 295#endif 296 BCOPY(dla, hwaddr, dla->sdl_len); 297 return 1; 298 } 299 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr + ifr->ifr_addr.sa_len); 300 } 301 302 return 0; 303} 304 305#ifdef __bsdi__ 306#include <nlist.h> 307 308struct nlist nl[] = { 309#define N_IFNET 0 310 {"_ifnet"}, 311 "", 312}; 313 314 315kvm_t *kvmd; 316 317/* 318 * Read kernel memory, return 0 on success. 319 */ 320int 321kread(u_long addr, char *buf, int size) 322{ 323 if (kvm_read(kvmd, addr, buf, size) != size) { 324 /* XXX this duplicates kvm_read's error printout */ 325 LogPrintf(LogERROR, "kvm_read %s\n", kvm_geterr(kvmd)); 326 return -1; 327 } 328 return 0; 329} 330 331void 332kmemgetether(char *ifname, struct sockaddr_dl * dlo) 333{ 334 struct ifnet ifnet; 335 int n; 336 u_long addr, ifaddraddr, ifnetfound, ifaddrfound; 337 char name[16 + 32]; 338 struct sockaddr *sa; 339 char *cp; 340 struct sockaddr_dl *sdl; 341 union { 342 struct ifaddr ifa; 343 struct in_ifaddr in; 344 } ifaddr; 345 struct arpcom ac; 346 347 kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); 348 if (kvmd) { 349 n = kvm_nlist(kvmd, nl); 350 if (n >= 0) { 351 addr = nl[N_IFNET].n_value; 352 kread(addr, (char *) &addr, sizeof(addr)); 353 ifaddraddr = ifnetfound = 0; 354 while (addr || ifaddraddr) { 355 ifnetfound = addr; 356 if (ifaddraddr == 0) { 357 if (kread(addr, (char *) &ifnet, sizeof(ifnet)) || 358 kread((u_long) ifnet.if_name, name, 16)) 359 return; 360 name[15] = 0; 361 addr = (u_long) ifnet.if_next; 362 cp = (char *) index(name, '\0'); 363 cp += sprintf(cp, "%d", ifnet.if_unit); 364 *cp = '\0'; 365 ifaddraddr = (u_long) ifnet.if_addrlist; 366 } 367 ifaddrfound = ifaddraddr; 368 if (ifaddraddr) { 369 if (kread(ifaddraddr, (char *) &ifaddr, sizeof ifaddr)) { 370 ifaddraddr = 0; 371 continue; 372 } 373#define CP(x) ((char *)(x)) 374 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + CP(&ifaddr); 375 sa = (struct sockaddr *) cp; 376 if (sa->sa_family == AF_LINK && strcmp(ifname, name) == 0) { 377 sdl = (struct sockaddr_dl *) sa; 378 cp = (char *) LLADDR(sdl); 379 n = sdl->sdl_alen; 380 if (ifnet.if_type == IFT_ETHER) { 381 if (n == 0) { 382 kread(ifnetfound, (char *) &ac, sizeof(ac)); 383 cp = (char *) LLADDR(sdl); 384 bcopy((char *) ac.ac_enaddr, cp, 6); 385 sdl->sdl_alen = 6; 386 } 387 bcopy(sdl, dlo, sizeof(*sdl)); 388 return; 389 } 390 } 391 ifaddraddr = (u_long) ifaddr.ifa.ifa_next; 392 } 393 } 394 } 395 } 396} 397 398#endif 399 400#ifdef DEBUG 401main() 402{ 403 u_long ipaddr; 404 int s; 405 406 s = socket(AF_INET, SOCK_DGRAM, 0); 407 ipaddr = inet_addr("192.168.1.32"); 408 sifproxyarp(s, ipaddr); 409 close(s); 410} 411 412#endif 413