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