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