xref: /freebsd/usr.sbin/ppp/arp.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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  * SPDX-License-Identifier: BSD-1-Clause
6  *
7  * Copyright (c) 1989 Carnegie Mellon University.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that the above copyright notice and this paragraph are
12  * duplicated in all such forms and that any documentation,
13  * advertising materials, and other materials related to such
14  * distribution and use acknowledge that the software was developed
15  * by Carnegie Mellon University.  The name of the
16  * University may not be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
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_in 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_in);
128   arpmsg.dst.sin_family = AF_INET;
129   arpmsg.dst.sin_addr.s_addr = addr.s_addr;
130 
131   arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
132     + arpmsg.hwa.sdl_len;
133 
134 
135   if (ID0write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 &&
136       !(!add && errno == ESRCH)) {
137     log_Printf(LogERROR, "%s proxy arp entry %s: %s\n",
138 	add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno));
139     close(routes);
140     return 0;
141   }
142   close(routes);
143   return 1;
144 }
145 
146 int
147 arp_SetProxy(struct bundle *bundle, struct in_addr addr)
148 {
149   return (arp_ProxySub(bundle, addr, 1));
150 }
151 
152 /*
153  * arp_ClearProxy - Delete the proxy ARP entry for the peer.
154  */
155 int
156 arp_ClearProxy(struct bundle *bundle, struct in_addr addr)
157 {
158   return (arp_ProxySub(bundle, addr, 0));
159 }
160 
161 #else				/* RTM_VERSION */
162 
163 /*
164  * arp_SetProxy - Make a proxy ARP entry for the peer.
165  */
166 int
167 arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s)
168 {
169   struct arpreq arpreq;
170   struct {
171     struct sockaddr_dl sdl;
172     char space[128];
173   }      dls;
174 
175   memset(&arpreq, '\0', sizeof arpreq);
176 
177   /*
178    * Get the hardware address of an interface on the same subnet as our local
179    * address.
180    */
181   if (!arp_EtherAddr(addr, &dls.sdl, 1)) {
182     log_Printf(LOG_PHASE_BIT, "Cannot determine ethernet address for "
183                "proxy ARP\n");
184     return 0;
185   }
186   arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
187   arpreq.arp_ha.sa_family = AF_UNSPEC;
188   memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen);
189   SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
190   ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr;
191   arpreq.arp_flags = ATF_PERM | ATF_PUBL;
192   if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) {
193     log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n",
194                strerror(errno));
195     return 0;
196   }
197   return 1;
198 }
199 
200 /*
201  * arp_ClearProxy - Delete the proxy ARP entry for the peer.
202  */
203 int
204 arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s)
205 {
206   struct arpreq arpreq;
207 
208   memset(&arpreq, '\0', sizeof arpreq);
209   SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
210   ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr;
211   if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) {
212     log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n",
213                strerror(errno));
214     return 0;
215   }
216   return 1;
217 }
218 
219 #endif				/* RTM_VERSION */
220 
221 
222 /*
223  * arp_EtherAddr - get the hardware address of an interface on the
224  * same subnet as ipaddr.
225  */
226 
227 int
228 arp_EtherAddr(struct in_addr ipaddr, struct sockaddr_dl *hwaddr,
229               int verbose)
230 {
231   int mib[6], skip;
232   size_t needed;
233   char *buf, *ptr, *end;
234   struct if_msghdr *ifm;
235   struct ifa_msghdr *ifam;
236   struct sockaddr_dl *dl;
237   struct sockaddr *sa[RTAX_MAX];
238 
239   mib[0] = CTL_NET;
240   mib[1] = PF_ROUTE;
241   mib[2] = 0;
242   mib[3] = 0;
243   mib[4] = NET_RT_IFLIST;
244   mib[5] = 0;
245 
246   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
247     log_Printf(LogERROR, "arp_EtherAddr: sysctl: estimate: %s\n",
248               strerror(errno));
249     return 0;
250   }
251 
252   if ((buf = malloc(needed)) == NULL)
253     return 0;
254 
255   if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
256     free(buf);
257     return 0;
258   }
259   end = buf + needed;
260 
261   ptr = buf;
262   while (ptr < end) {
263     ifm = (struct if_msghdr *)ptr;		/* On if_msghdr */
264     if (ifm->ifm_type != RTM_IFINFO)
265       break;
266     dl = (struct sockaddr_dl *)(ifm + 1);	/* Single _dl at end */
267     skip = (ifm->ifm_flags & (IFF_UP | IFF_BROADCAST | IFF_POINTOPOINT |
268             IFF_NOARP | IFF_LOOPBACK)) != (IFF_UP | IFF_BROADCAST);
269     ptr += ifm->ifm_msglen;			/* First ifa_msghdr */
270     while (ptr < end) {
271       ifam = (struct ifa_msghdr *)ptr;	/* Next ifa_msghdr (alias) */
272       if (ifam->ifam_type != RTM_NEWADDR)	/* finished ? */
273         break;
274       ptr += ifam->ifam_msglen;
275       if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) !=
276           (RTA_NETMASK|RTA_IFA))
277         continue;
278       /* Found a candidate.  Do the addresses match ? */
279       if (log_IsKept(LogDEBUG) &&
280           ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen)
281         log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n",
282                   dl->sdl_nlen, dl->sdl_data);
283 
284       iface_ParseHdr(ifam, sa);
285 
286       if (sa[RTAX_IFA]->sa_family == AF_INET) {
287         struct sockaddr_in *ifa, *netmask;
288 
289         ifa = (struct sockaddr_in *)sa[RTAX_IFA];
290         netmask = (struct sockaddr_in *)sa[RTAX_NETMASK];
291 
292         if (log_IsKept(LogDEBUG)) {
293           char a[16];
294 
295           strncpy(a, inet_ntoa(netmask->sin_addr), sizeof a - 1);
296           a[sizeof a - 1] = '\0';
297           log_Printf(LogDEBUG, "Check addr %s, mask %s\n",
298                      inet_ntoa(ifa->sin_addr), a);
299         }
300 
301         if ((ifa->sin_addr.s_addr & netmask->sin_addr.s_addr) ==
302             (ipaddr.s_addr & netmask->sin_addr.s_addr)) {
303           log_Printf(verbose ? LogPHASE : LogDEBUG,
304                      "Found interface %.*s for %s\n", dl->sdl_nlen,
305                      dl->sdl_data, inet_ntoa(ipaddr));
306           memcpy(hwaddr, dl, dl->sdl_len);
307           free(buf);
308           return 1;
309         }
310       }
311     }
312   }
313   free(buf);
314 
315   return 0;
316 }
317