arp.c revision 32671
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.23 1998/01/19 22:34:20 brian Exp $ 21 * 22 */ 23 24/* 25 * TODO: 26 */ 27 28#include <sys/param.h> 29#include <sys/time.h> 30#include <sys/socket.h> 31#include <net/if.h> 32#include <net/route.h> 33#include <net/if_dl.h> 34#include <netinet/in.h> 35#include <net/if_types.h> 36#include <netinet/if_ether.h> 37 38#include <fcntl.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <sys/errno.h> 43#include <sys/ioctl.h> 44#include <sys/sysctl.h> 45#include <sys/uio.h> 46#include <unistd.h> 47 48#include "command.h" 49#include "mbuf.h" 50#include "log.h" 51#include "id.h" 52#include "route.h" 53#include "arp.h" 54 55#ifdef DEBUG 56/* 57 * To test the proxy arp stuff, put the following in your Makefile: 58 * 59 * arp-test: arp.c 60 * cp ${.CURDIR}/arp.c arp-test.c 61 * echo 'const char *' >>arp-test.c 62 * awk '/^Index2Nam/,/^}/' ${.CURDIR}/route.c >>arp-test.c 63 * cc -I${.CURDIR} -DDEBUG arp-test.c -o arp-test 64 * 65 * and type ``make arp-test''. 66 * 67 */ 68#define LogIsKept(x) 0 69#define LogPrintf fprintf 70#undef LogDEBUG 71#define LogDEBUG stderr 72#undef LogERROR 73#define LogERROR stderr 74#undef LogPHASE 75#define LogPHASE stdout 76#define ID0socket socket 77#define ID0ioctl ioctl 78#endif 79 80static int rtm_seq; 81 82static int get_ether_addr(int, struct in_addr, struct sockaddr_dl *); 83 84/* 85 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 86 * if it exists. 87 */ 88#define SET_SA_FAMILY(addr, family) \ 89 memset((char *) &(addr), '\0', sizeof(addr)); \ 90 addr.sa_family = (family); \ 91 addr.sa_len = sizeof(addr); 92 93 94#if RTM_VERSION >= 3 95 96/* 97 * sifproxyarp - Make a proxy ARP entry for the peer. 98 */ 99static struct { 100 struct rt_msghdr hdr; 101 struct sockaddr_inarp dst; 102 struct sockaddr_dl hwa; 103 char extra[128]; 104} arpmsg; 105 106static int arpmsg_valid; 107 108int 109sifproxyarp(int unit, struct in_addr hisaddr) 110{ 111 int routes; 112 113 /* 114 * Get the hardware address of an interface on the same subnet as our local 115 * address. 116 */ 117 memset(&arpmsg, 0, sizeof arpmsg); 118 if (!get_ether_addr(unit, hisaddr, &arpmsg.hwa)) { 119 LogPrintf(LogERROR, "Cannot determine ethernet address for proxy ARP\n"); 120 return 0; 121 } 122 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 123 if (routes < 0) { 124 LogPrintf(LogERROR, "sifproxyarp: opening routing socket: %s\n", 125 strerror(errno)); 126 return 0; 127 } 128 arpmsg.hdr.rtm_type = RTM_ADD; 129 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; 130 arpmsg.hdr.rtm_version = RTM_VERSION; 131 arpmsg.hdr.rtm_seq = ++rtm_seq; 132 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 133 arpmsg.hdr.rtm_inits = RTV_EXPIRE; 134 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); 135 arpmsg.dst.sin_family = AF_INET; 136 arpmsg.dst.sin_addr.s_addr = hisaddr.s_addr; 137 arpmsg.dst.sin_other = SIN_PROXY; 138 139 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 140 + arpmsg.hwa.sdl_len; 141 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 142 LogPrintf(LogERROR, "Add proxy arp entry: %s\n", strerror(errno)); 143 close(routes); 144 return 0; 145 } 146 close(routes); 147 arpmsg_valid = 1; 148 return 1; 149} 150 151/* 152 * cifproxyarp - Delete the proxy ARP entry for the peer. 153 */ 154int 155cifproxyarp(int unit, struct in_addr hisaddr) 156{ 157 int routes; 158 159 if (!arpmsg_valid) 160 return 0; 161 arpmsg_valid = 0; 162 163 arpmsg.hdr.rtm_type = RTM_DELETE; 164 arpmsg.hdr.rtm_seq = ++rtm_seq; 165 166 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 167 if (routes < 0) { 168 LogPrintf(LogERROR, "sifproxyarp: opening routing socket: %s\n", 169 strerror(errno)); 170 return 0; 171 } 172 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) { 173 LogPrintf(LogERROR, "Delete proxy arp entry: %s\n", strerror(errno)); 174 close(routes); 175 return 0; 176 } 177 close(routes); 178 return 1; 179} 180 181#else /* RTM_VERSION */ 182 183/* 184 * sifproxyarp - Make a proxy ARP entry for the peer. 185 */ 186int 187sifproxyarp(int unit, struct in_addr hisaddr) 188{ 189 struct arpreq arpreq; 190 struct { 191 struct sockaddr_dl sdl; 192 char space[128]; 193 } dls; 194 195 memset(&arpreq, '\0', sizeof arpreq); 196 197 /* 198 * Get the hardware address of an interface on the same subnet as our local 199 * address. 200 */ 201 if (!get_ether_addr(unit, hisaddr, &dls.sdl)) { 202 LogPrintf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n"); 203 return 0; 204 } 205 arpreq.arp_ha.sa_len = sizeof(struct sockaddr); 206 arpreq.arp_ha.sa_family = AF_UNSPEC; 207 memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen); 208 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 209 ((struct sockaddr_in *) & arpreq.arp_pa)->sin_addr.s_addr = hisaddr.s_addr; 210 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 211 if (ID0ioctl(unit, SIOCSARP, (caddr_t) & arpreq) < 0) { 212 LogPrintf(LogERROR, "sifproxyarp: ioctl(SIOCSARP): %s\n", strerror(errno)); 213 return 0; 214 } 215 return 1; 216} 217 218/* 219 * cifproxyarp - Delete the proxy ARP entry for the peer. 220 */ 221int 222cifproxyarp(int unit, struct in_addr hisaddr) 223{ 224 struct arpreq arpreq; 225 226 memset(&arpreq, '\0', sizeof arpreq); 227 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 228 ((struct sockaddr_in *) & arpreq.arp_pa)->sin_addr.s_addr = hisaddr.s_addr; 229 if (ID0ioctl(unit, SIOCDARP, (caddr_t) & arpreq) < 0) { 230 LogPrintf(LogERROR, "cifproxyarp: ioctl(SIOCDARP): %s\n", strerror(errno)); 231 return 0; 232 } 233 return 1; 234} 235 236#endif /* RTM_VERSION */ 237 238 239/* 240 * get_ether_addr - get the hardware address of an interface on the 241 * the same subnet as ipaddr. 242 */ 243#define MAX_IFS 32 244 245static int 246get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr) 247{ 248 int idx; 249 const char *got; 250 char *sp, *ep, *cp, *wp; 251 struct ifreq ifrq; 252 struct in_addr addr, mask; 253 struct rt_msghdr *rtm; 254 struct sockaddr *sa_dst, *sa_gw; 255 struct sockaddr_dl *dl; 256 size_t needed; 257 int mib[6]; 258 259 idx = 1; 260 while (strcmp(got = Index2Nam(idx), "???")) { 261 strncpy(ifrq.ifr_name, got, sizeof ifrq.ifr_name - 1); 262 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 263 if (ID0ioctl(s, SIOCGIFADDR, &ifrq) == 0 && 264 ifrq.ifr_addr.sa_family == AF_INET) { 265 addr = ((struct sockaddr_in *)&ifrq.ifr_addr)->sin_addr; 266 if (ID0ioctl(s, SIOCGIFNETMASK, &ifrq) == 0) { 267 mask = ((struct sockaddr_in *)&ifrq.ifr_broadaddr)->sin_addr; 268 if ((ipaddr.s_addr & mask.s_addr) == (addr.s_addr & mask.s_addr)) 269 break; 270 } 271 } 272 idx++; 273 } 274 275 if (!strcmp(got, "???")) 276 return 0; 277 278 LogPrintf(LogPHASE, "Found interface %s for proxy arp\n", got); 279 280 mib[0] = CTL_NET; 281 mib[1] = PF_ROUTE; 282 mib[2] = 0; 283 mib[3] = 0; 284 mib[4] = NET_RT_DUMP; 285 mib[5] = 0; 286 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 287 LogPrintf(LogERROR, "get_ether_addr: sysctl: estimate: %s\n", 288 strerror(errno)); 289 return 0; 290 } 291 if (needed < 0) 292 return 0; 293 if ((sp = malloc(needed)) == NULL) 294 return 0; 295 if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) { 296 LogPrintf(LogERROR, "ShowRoute: sysctl: getroute: %s\n", strerror(errno)); 297 free(sp); 298 return (1); 299 } 300 ep = sp + needed; 301 302 for (cp = sp; cp < ep; cp += rtm->rtm_msglen) { 303 rtm = (struct rt_msghdr *) cp; 304 if (rtm->rtm_index == idx) { 305 wp = (char *)(rtm+1); 306 307 if (rtm->rtm_addrs & RTA_DST) { 308 sa_dst = (struct sockaddr *)wp; 309 wp += sa_dst->sa_len; 310 } else 311 sa_dst = NULL; 312 313 if (rtm->rtm_addrs & RTA_GATEWAY) { 314 sa_gw = (struct sockaddr *)wp; 315 if (sa_gw->sa_family == AF_LINK) { 316 dl = (struct sockaddr_dl *)wp; 317 if (!dl->sdl_nlen && !dl->sdl_alen && !dl->sdl_slen) { 318 memcpy(hwaddr, dl, dl->sdl_len); 319 free(sp); 320 return 1; 321 } 322 } 323 } 324 } 325 } 326 free(sp); 327 return 0; 328} 329 330#ifdef DEBUG 331int 332main(int argc, char **argv) 333{ 334 struct in_addr ipaddr; 335 int s, f; 336 337 s = socket(AF_INET, SOCK_DGRAM, 0); 338 for (f = 1; f < argc; f++) { 339 if (inet_aton(argv[f], &ipaddr)) 340 sifproxyarp(s, ipaddr); 341 } 342 close(s); 343} 344#endif 345