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