arp.c revision 31061
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.16 1997/10/26 01:02:03 brian Exp $ 21 * 22 */ 23 24/* 25 * TODO: 26 */ 27 28#include <sys/types.h> 29#include <sys/time.h> 30#include <sys/socket.h> 31#include <net/if.h> 32#include <net/if_var.h> 33#include <net/route.h> 34#include <net/if_dl.h> 35#include <netinet/in.h> 36#include <net/if_types.h> 37#include <netinet/in_var.h> 38#include <netinet/if_ether.h> 39 40#include <fcntl.h> 41#include <stdio.h> 42#include <string.h> 43#include <sys/errno.h> 44#include <sys/ioctl.h> 45#include <sys/uio.h> 46#include <unistd.h> 47 48#include "mbuf.h" 49#include "log.h" 50#include "id.h" 51#include "arp.h" 52 53static int rtm_seq; 54 55static int get_ether_addr(int, u_long, struct sockaddr_dl *); 56 57/* 58 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 59 * if it exists. 60 */ 61#define SET_SA_FAMILY(addr, family) \ 62 memset((char *) &(addr), '\0', sizeof(addr)); \ 63 addr.sa_family = (family); \ 64 addr.sa_len = sizeof(addr); 65 66 67#if RTM_VERSION >= 3 68 69/* 70 * sifproxyarp - Make a proxy ARP entry for the peer. 71 */ 72static struct { 73 struct rt_msghdr hdr; 74 struct sockaddr_inarp dst; 75 struct sockaddr_dl hwa; 76 char extra[128]; 77} arpmsg; 78 79static int arpmsg_valid; 80 81int 82sifproxyarp(int unit, u_long hisaddr) 83{ 84 int routes; 85 86 /* 87 * Get the hardware address of an interface on the same subnet as our local 88 * address. 89 */ 90 memset(&arpmsg, 0, sizeof(arpmsg)); 91 if (!get_ether_addr(unit, hisaddr, &arpmsg.hwa)) { 92 LogPrintf(LogERROR, "Cannot determine ethernet address for proxy ARP\n"); 93 return 0; 94 } 95 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 96 if (routes < 0) { 97 LogPrintf(LogERROR, "sifproxyarp: opening routing socket: %s\n", 98 strerror(errno)); 99 return 0; 100 } 101 arpmsg.hdr.rtm_type = RTM_ADD; 102 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; 103 arpmsg.hdr.rtm_version = RTM_VERSION; 104 arpmsg.hdr.rtm_seq = ++rtm_seq; 105 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 106 arpmsg.hdr.rtm_inits = RTV_EXPIRE; 107 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); 108 arpmsg.dst.sin_family = AF_INET; 109 arpmsg.dst.sin_addr.s_addr = hisaddr; 110 arpmsg.dst.sin_other = SIN_PROXY; 111 112 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 113 + arpmsg.hwa.sdl_len; 114 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 115 LogPrintf(LogERROR, "Add proxy arp entry: %s\n", strerror(errno)); 116 close(routes); 117 return 0; 118 } 119 close(routes); 120 arpmsg_valid = 1; 121 return 1; 122} 123 124/* 125 * cifproxyarp - Delete the proxy ARP entry for the peer. 126 */ 127int 128cifproxyarp(int unit, u_long hisaddr) 129{ 130 int routes; 131 132 if (!arpmsg_valid) 133 return 0; 134 arpmsg_valid = 0; 135 136 arpmsg.hdr.rtm_type = RTM_DELETE; 137 arpmsg.hdr.rtm_seq = ++rtm_seq; 138 139 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 140 if (routes < 0) { 141 LogPrintf(LogERROR, "sifproxyarp: opening routing socket: %s\n", 142 strerror(errno)); 143 return 0; 144 } 145 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 146 LogPrintf(LogERROR, "Delete proxy arp entry: %s\n", strerror(errno)); 147 close(routes); 148 return 0; 149 } 150 close(routes); 151 return 1; 152} 153 154#else /* RTM_VERSION */ 155 156/* 157 * sifproxyarp - Make a proxy ARP entry for the peer. 158 */ 159int 160sifproxyarp(int unit, u_long hisaddr) 161{ 162 struct arpreq arpreq; 163 struct { 164 struct sockaddr_dl sdl; 165 char space[128]; 166 } dls; 167 168 memset(&arpreq, '\0', sizeof(arpreq)); 169 170 /* 171 * Get the hardware address of an interface on the same subnet as our local 172 * address. 173 */ 174 if (!get_ether_addr(unit, hisaddr, &dls.sdl)) { 175 LogPrintf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n"); 176 return 0; 177 } 178 arpreq.arp_ha.sa_len = sizeof(struct sockaddr); 179 arpreq.arp_ha.sa_family = AF_UNSPEC; 180 memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen); 181 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 182 ((struct sockaddr_in *) & arpreq.arp_pa)->sin_addr.s_addr = hisaddr; 183 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 184 if (ID0ioctl(unit, SIOCSARP, (caddr_t) & arpreq) < 0) { 185 LogPrintf(LogERROR, "sifproxyarp: ioctl(SIOCSARP): %s\n", strerror(errno)); 186 return 0; 187 } 188 return 1; 189} 190 191/* 192 * cifproxyarp - Delete the proxy ARP entry for the peer. 193 */ 194int 195cifproxyarp(int unit, u_long hisaddr) 196{ 197 struct arpreq arpreq; 198 199 memset(&arpreq, '\0', sizeof(arpreq)); 200 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 201 ((struct sockaddr_in *) & arpreq.arp_pa)->sin_addr.s_addr = hisaddr; 202 if (ID0ioctl(unit, SIOCDARP, (caddr_t) & arpreq) < 0) { 203 LogPrintf(LogERROR, "cifproxyarp: ioctl(SIOCDARP): %s\n", strerror(errno)); 204 return 0; 205 } 206 return 1; 207} 208 209#endif /* RTM_VERSION */ 210 211 212/* 213 * get_ether_addr - get the hardware address of an interface on the 214 * the same subnet as ipaddr. 215 */ 216#define MAX_IFS 32 217 218static int 219get_ether_addr(int s, u_long ipaddr, struct sockaddr_dl *hwaddr) 220{ 221 struct ifreq *ifr, *ifend, *ifp; 222 u_long ina, mask; 223 struct sockaddr_dl *dla; 224 struct ifreq ifreq; 225 struct ifconf ifc; 226 struct ifreq ifs[MAX_IFS]; 227 228 ifc.ifc_len = sizeof(ifs); 229 ifc.ifc_req = ifs; 230 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { 231 LogPrintf(LogERROR, "get_ether_addr: ioctl(SIOCGIFCONF): %s\n", 232 strerror(errno)); 233 return 0; 234 } 235 236 /* 237 * Scan through looking for an interface with an Internet address on the 238 * same subnet as `ipaddr'. 239 */ 240 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); 241 for (ifr = ifc.ifc_req; ifr < ifend;) { 242 if (ifr->ifr_addr.sa_family == AF_INET) { 243 ina = ((struct sockaddr_in *) & ifr->ifr_addr)->sin_addr.s_addr; 244 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); 245 ifreq.ifr_name[sizeof(ifreq.ifr_name) - 1] = '\0'; 246 247 /* 248 * Check that the interface is up, and not point-to-point or loopback. 249 */ 250 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0) 251 continue; 252 if ((ifreq.ifr_flags & 253 (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | IFF_LOOPBACK | IFF_NOARP)) 254 != (IFF_UP | IFF_BROADCAST)) 255 goto nextif; 256 257 /* 258 * Get its netmask and check that it's on the right subnet. 259 */ 260 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0) 261 continue; 262 mask = ((struct sockaddr_in *) & ifreq.ifr_addr)->sin_addr.s_addr; 263 if ((ipaddr & mask) != (ina & mask)) 264 goto nextif; 265 266 break; 267 } 268nextif: 269 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr + ifr->ifr_addr.sa_len); 270 } 271 272 if (ifr >= ifend) 273 return 0; 274 LogPrintf(LogPHASE, "Found interface %s for proxy arp\n", ifr->ifr_name); 275 276 /* 277 * Now scan through again looking for a link-level address for this 278 * interface. 279 */ 280 ifp = ifr; 281 for (ifr = ifc.ifc_req; ifr < ifend;) { 282 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 283 && ifr->ifr_addr.sa_family == AF_LINK) { 284 285 /* 286 * Found the link-level address - copy it out 287 */ 288 dla = (struct sockaddr_dl *) & ifr->ifr_addr; 289 memcpy(hwaddr, dla, dla->sdl_len); 290 return 1; 291 } 292 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr + ifr->ifr_addr.sa_len); 293 } 294 295 return 0; 296} 297 298 299#ifdef DEBUG 300int 301main() 302{ 303 u_long ipaddr; 304 int s; 305 306 s = socket(AF_INET, SOCK_DGRAM, 0); 307 ipaddr = inet_addr("192.168.1.32"); 308 sifproxyarp(s, ipaddr); 309 close(s); 310} 311#endif 312