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