arp.c revision 46085
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.32 1999/01/28 01:56:30 brian Exp $ 21 * 22 */ 23 24/* 25 * TODO: 26 */ 27 28#include <sys/param.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 <errno.h> 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.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#ifndef NORADIUS 66#include "radius.h" 67#endif 68#include "bundle.h" 69#include "arp.h" 70 71/* 72 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr, 73 * if it exists. 74 */ 75#define SET_SA_FAMILY(addr, family) \ 76 memset((char *) &(addr), '\0', sizeof(addr)); \ 77 addr.sa_family = (family); \ 78 addr.sa_len = sizeof(addr); 79 80 81#if RTM_VERSION >= 3 82 83/* 84 * arp_SetProxy - Make a proxy ARP entry for the peer. 85 */ 86static struct { 87 struct rt_msghdr hdr; 88 struct sockaddr_inarp dst; 89 struct sockaddr_dl hwa; 90 char extra[128]; 91} arpmsg; 92 93static int 94arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add, int s) 95{ 96 int routes; 97 98 /* 99 * Get the hardware address of an interface on the same subnet as our local 100 * address. 101 */ 102 103 memset(&arpmsg, 0, sizeof arpmsg); 104 if (!get_ether_addr(s, addr, &arpmsg.hwa)) { 105 log_Printf(LogWARN, "%s: Cannot determine ethernet address for proxy ARP\n", 106 inet_ntoa(addr)); 107 return 0; 108 } 109 routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET); 110 if (routes < 0) { 111 log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n", 112 strerror(errno)); 113 return 0; 114 } 115 arpmsg.hdr.rtm_type = add ? RTM_ADD : RTM_DELETE; 116 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC; 117 arpmsg.hdr.rtm_version = RTM_VERSION; 118 arpmsg.hdr.rtm_seq = ++bundle->routing_seq; 119 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY; 120 arpmsg.hdr.rtm_inits = RTV_EXPIRE; 121 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp); 122 arpmsg.dst.sin_family = AF_INET; 123 arpmsg.dst.sin_addr.s_addr = addr.s_addr; 124 arpmsg.dst.sin_other = SIN_PROXY; 125 126 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg 127 + arpmsg.hwa.sdl_len; 128 129 130 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 && 131 !(!add && errno == ESRCH)) { 132 log_Printf(LogERROR, "%s proxy arp entry %s: %s\n", 133 add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno)); 134 close(routes); 135 return 0; 136 } 137 close(routes); 138 return 1; 139} 140 141int 142arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 143{ 144 145 return (arp_ProxySub(bundle, addr, 1, s)); 146} 147 148/* 149 * arp_ClearProxy - Delete the proxy ARP entry for the peer. 150 */ 151int 152arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 153{ 154 155 return (arp_ProxySub(bundle, addr, 0, s)); 156} 157 158#else /* RTM_VERSION */ 159 160/* 161 * arp_SetProxy - Make a proxy ARP entry for the peer. 162 */ 163int 164arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s) 165{ 166 struct arpreq arpreq; 167 struct { 168 struct sockaddr_dl sdl; 169 char space[128]; 170 } dls; 171 172 memset(&arpreq, '\0', 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(s, addr, &dls.sdl)) { 179 log_Printf(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 memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen); 185 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 186 ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 187 arpreq.arp_flags = ATF_PERM | ATF_PUBL; 188 if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) { 189 log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n", 190 strerror(errno)); 191 return 0; 192 } 193 return 1; 194} 195 196/* 197 * arp_ClearProxy - Delete the proxy ARP entry for the peer. 198 */ 199int 200arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s) 201{ 202 struct arpreq arpreq; 203 204 memset(&arpreq, '\0', sizeof arpreq); 205 SET_SA_FAMILY(arpreq.arp_pa, AF_INET); 206 ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr; 207 if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) { 208 log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n", 209 strerror(errno)); 210 return 0; 211 } 212 return 1; 213} 214 215#endif /* RTM_VERSION */ 216 217 218/* 219 * get_ether_addr - get the hardware address of an interface on the 220 * the same subnet as ipaddr. 221 */ 222 223int 224get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr) 225{ 226 int mib[6], sa_len, skip, b; 227 size_t needed; 228 char *buf, *ptr, *end; 229 struct if_msghdr *ifm; 230 struct ifa_msghdr *ifam; 231 struct sockaddr *sa; 232 struct sockaddr_dl *dl; 233 struct sockaddr_in *ifa, *mask; 234 235 mib[0] = CTL_NET; 236 mib[1] = PF_ROUTE; 237 mib[2] = 0; 238 mib[3] = 0; 239 mib[4] = NET_RT_IFLIST; 240 mib[5] = 0; 241 242 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 243 log_Printf(LogERROR, "get_ether_addr: sysctl: estimate: %s\n", 244 strerror(errno)); 245 return 0; 246 } 247 248 if ((buf = malloc(needed)) == NULL) 249 return 0; 250 251 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 252 free(buf); 253 return 0; 254 } 255 end = buf + needed; 256 257 ptr = buf; 258 while (ptr < end) { 259 ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 260 if (ifm->ifm_type != RTM_IFINFO) 261 break; 262 dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 263 skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT | 264 IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST); 265 ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 266 while (ptr < end) { 267 ifam = (struct ifa_msghdr *)ptr; /* Next ifa_msghdr (alias) */ 268 if (ifam->ifam_type != RTM_NEWADDR) /* finished ? */ 269 break; 270 sa = (struct sockaddr *)(ifam+1); /* pile of sa's at end */ 271 ptr += ifam->ifam_msglen; 272 if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) != 273 (RTA_NETMASK|RTA_IFA)) 274 continue; 275 /* Found a candidate. Do the addresses match ? */ 276 if (log_IsKept(LogDEBUG) && 277 ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen) 278 log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n", 279 dl->sdl_nlen, dl->sdl_data); 280 b = 1; 281 ifa = mask = NULL; 282 while (b < (RTA_NETMASK|RTA_IFA) && sa < (struct sockaddr *)ptr) { 283 switch (b) { 284 case RTA_IFA: 285 ifa = (struct sockaddr_in *)sa; 286 break; 287 case RTA_NETMASK: 288 /* 289 * Careful here ! this sockaddr doesn't have sa_family set to 290 * AF_INET, and is only 8 bytes big ! I have no idea why ! 291 */ 292 mask = (struct sockaddr_in *)sa; 293 break; 294 } 295 if (ifam->ifam_addrs & b) { 296#define ALN sizeof(ifa->sin_addr.s_addr) 297 sa_len = sa->sa_len > 0 ? ((sa->sa_len-1)|(ALN-1))+1 : ALN; 298 sa = (struct sockaddr *)((char *)sa + sa_len); 299 } 300 b <<= 1; 301 } 302 if (log_IsKept(LogDEBUG)) { 303 char a[16]; 304 strncpy(a, inet_ntoa(mask->sin_addr), sizeof a - 1); 305 a[sizeof a - 1] = '\0'; 306 log_Printf(LogDEBUG, "Check addr %s, mask %s\n", 307 inet_ntoa(ifa->sin_addr), a); 308 } 309 if (ifa->sin_family == AF_INET && 310 (ifa->sin_addr.s_addr & mask->sin_addr.s_addr) == 311 (ipaddr.s_addr & mask->sin_addr.s_addr)) { 312 log_Printf(LogPHASE, "Found interface %.*s for %s\n", 313 dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr)); 314 memcpy(hwaddr, dl, dl->sdl_len); 315 free(buf); 316 return 1; 317 } 318 } 319 } 320 free(buf); 321 322 return 0; 323} 324