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