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