xref: /freebsd/usr.sbin/ppp/arp.c (revision 56ca39961bd1c9946a505c41c3fc634ef63fdd42)
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 "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 "iface.h"
72 #include "arp.h"
73 
74 /*
75  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
76  * if it exists.
77  */
78 #define SET_SA_FAMILY(addr, family)		\
79     memset((char *) &(addr), '\0', sizeof(addr));	\
80     addr.sa_family = (family); 			\
81     addr.sa_len = sizeof(addr);
82 
83 
84 #if RTM_VERSION >= 3
85 
86 /*
87  * arp_SetProxy - Make a proxy ARP entry for the peer.
88  */
89 static struct {
90   struct rt_msghdr hdr;
91   struct sockaddr_inarp dst;
92   struct sockaddr_dl hwa;
93   char extra[128];
94 } arpmsg;
95 
96 static int
97 arp_ProxySub(struct bundle *bundle, struct in_addr addr, int add, int s)
98 {
99   int routes;
100 
101   /*
102    * Get the hardware address of an interface on the same subnet as our local
103    * address.
104    */
105 
106   memset(&arpmsg, 0, sizeof arpmsg);
107   if (!get_ether_addr(s, addr, &arpmsg.hwa)) {
108     log_Printf(LogWARN, "%s: Cannot determine ethernet address for proxy ARP\n",
109 	       inet_ntoa(addr));
110     return 0;
111   }
112   routes = ID0socket(PF_ROUTE, SOCK_RAW, AF_INET);
113   if (routes < 0) {
114     log_Printf(LogERROR, "arp_SetProxy: opening routing socket: %s\n",
115 	      strerror(errno));
116     return 0;
117   }
118   arpmsg.hdr.rtm_type = add ? RTM_ADD : RTM_DELETE;
119   arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
120   arpmsg.hdr.rtm_version = RTM_VERSION;
121   arpmsg.hdr.rtm_seq = ++bundle->routing_seq;
122   arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
123   arpmsg.hdr.rtm_inits = RTV_EXPIRE;
124   arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
125   arpmsg.dst.sin_family = AF_INET;
126   arpmsg.dst.sin_addr.s_addr = addr.s_addr;
127   arpmsg.dst.sin_other = SIN_PROXY;
128 
129   arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
130     + arpmsg.hwa.sdl_len;
131 
132 
133   if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0 &&
134       !(!add && errno == ESRCH)) {
135     log_Printf(LogERROR, "%s proxy arp entry %s: %s\n",
136 	add ? "Add" : "Delete", inet_ntoa(addr), strerror(errno));
137     close(routes);
138     return 0;
139   }
140   close(routes);
141   return 1;
142 }
143 
144 int
145 arp_SetProxy(struct bundle *bundle, struct in_addr addr, int s)
146 {
147 
148   return (arp_ProxySub(bundle, addr, 1, s));
149 }
150 
151 /*
152  * arp_ClearProxy - Delete the proxy ARP entry for the peer.
153  */
154 int
155 arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s)
156 {
157 
158   return (arp_ProxySub(bundle, addr, 0, s));
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 (!get_ether_addr(s, addr, &dls.sdl)) {
182     log_Printf(LOG_PHASE_BIT, "Cannot determine ethernet address for proxy ARP\n");
183     return 0;
184   }
185   arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
186   arpreq.arp_ha.sa_family = AF_UNSPEC;
187   memcpy(arpreq.arp_ha.sa_data, LLADDR(&dls.sdl), dls.sdl.sdl_alen);
188   SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
189   ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr;
190   arpreq.arp_flags = ATF_PERM | ATF_PUBL;
191   if (ID0ioctl(s, SIOCSARP, (caddr_t) & arpreq) < 0) {
192     log_Printf(LogERROR, "arp_SetProxy: ioctl(SIOCSARP): %s\n",
193                strerror(errno));
194     return 0;
195   }
196   return 1;
197 }
198 
199 /*
200  * arp_ClearProxy - Delete the proxy ARP entry for the peer.
201  */
202 int
203 arp_ClearProxy(struct bundle *bundle, struct in_addr addr, int s)
204 {
205   struct arpreq arpreq;
206 
207   memset(&arpreq, '\0', sizeof arpreq);
208   SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
209   ((struct sockaddr_in *)&arpreq.arp_pa)->sin_addr.s_addr = addr.s_addr;
210   if (ID0ioctl(s, SIOCDARP, (caddr_t) & arpreq) < 0) {
211     log_Printf(LogERROR, "arp_ClearProxy: ioctl(SIOCDARP): %s\n",
212                strerror(errno));
213     return 0;
214   }
215   return 1;
216 }
217 
218 #endif				/* RTM_VERSION */
219 
220 
221 /*
222  * get_ether_addr - get the hardware address of an interface on the
223  * the same subnet as ipaddr.
224  */
225 
226 int
227 get_ether_addr(int s, struct in_addr ipaddr, struct sockaddr_dl *hwaddr)
228 {
229   int mib[6], skip;
230   size_t needed;
231   char *buf, *ptr, *end;
232   struct if_msghdr *ifm;
233   struct ifa_msghdr *ifam;
234   struct sockaddr_dl *dl;
235   struct sockaddr *sa[RTAX_MAX];
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       ptr += ifam->ifam_msglen;
273       if (skip || (ifam->ifam_addrs & (RTA_NETMASK|RTA_IFA)) !=
274           (RTA_NETMASK|RTA_IFA))
275         continue;
276       /* Found a candidate.  Do the addresses match ? */
277       if (log_IsKept(LogDEBUG) &&
278           ptr == (char *)ifm + ifm->ifm_msglen + ifam->ifam_msglen)
279         log_Printf(LogDEBUG, "%.*s interface is a candidate for proxy\n",
280                   dl->sdl_nlen, dl->sdl_data);
281 
282       iface_ParseHdr(ifam, sa);
283 
284       if (sa[RTAX_IFA]->sa_family == AF_INET) {
285         struct sockaddr_in *ifa, *netmask;
286 
287         ifa = (struct sockaddr_in *)sa[RTAX_IFA];
288         netmask = (struct sockaddr_in *)sa[RTAX_NETMASK];
289 
290         if (log_IsKept(LogDEBUG)) {
291           char a[16];
292 
293           strncpy(a, inet_ntoa(netmask->sin_addr), sizeof a - 1);
294           a[sizeof a - 1] = '\0';
295           log_Printf(LogDEBUG, "Check addr %s, mask %s\n",
296                      inet_ntoa(ifa->sin_addr), a);
297         }
298 
299         if ((ifa->sin_addr.s_addr & netmask->sin_addr.s_addr) ==
300             (ipaddr.s_addr & netmask->sin_addr.s_addr)) {
301           log_Printf(LogPHASE, "Found interface %.*s for %s\n",
302                     dl->sdl_alen, dl->sdl_data, inet_ntoa(ipaddr));
303           memcpy(hwaddr, dl, dl->sdl_len);
304           free(buf);
305           return 1;
306         }
307       }
308     }
309   }
310   free(buf);
311 
312   return 0;
313 }
314