xref: /freebsd/usr.sbin/ppp/iface.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
1 /*-
2  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <net/if.h>
33 #include <net/if_dl.h>
34 #include <net/route.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 <string.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <sys/ioctl.h>
45 #include <sys/sysctl.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 #include "layer.h"
50 #include "defs.h"
51 #include "command.h"
52 #include "mbuf.h"
53 #include "log.h"
54 #include "id.h"
55 #include "timer.h"
56 #include "fsm.h"
57 #include "iplist.h"
58 #include "lqr.h"
59 #include "hdlc.h"
60 #include "throughput.h"
61 #include "slcompress.h"
62 #include "descriptor.h"
63 #include "ipcp.h"
64 #include "filter.h"
65 #include "lcp.h"
66 #include "ccp.h"
67 #include "link.h"
68 #include "mp.h"
69 #ifndef NORADIUS
70 #include "radius.h"
71 #endif
72 #include "bundle.h"
73 #include "prompt.h"
74 #include "iface.h"
75 
76 
77 static int
78 bitsinmask(struct in_addr mask)
79 {
80   u_int32_t bitmask, maskaddr;
81   int bits;
82 
83   bitmask = 0xffffffff;
84   maskaddr = ntohl(mask.s_addr);
85   for (bits = 32; bits >= 0; bits--) {
86     if (maskaddr == bitmask)
87       break;
88     bitmask &= ~(1 << (32 - bits));
89   }
90 
91   return bits;
92 }
93 
94 struct iface *
95 iface_Create(const char *name)
96 {
97   int mib[6], s;
98   size_t needed, namelen;
99   char *buf, *ptr, *end;
100   struct if_msghdr *ifm;
101   struct ifa_msghdr *ifam;
102   struct sockaddr_dl *dl;
103   struct sockaddr *sa[RTAX_MAX];
104   struct iface *iface;
105   struct iface_addr *addr;
106 
107   s = socket(AF_INET, SOCK_DGRAM, 0);
108   if (s < 0) {
109     fprintf(stderr, "iface_Create: socket(): %s\n", strerror(errno));
110     return NULL;
111   }
112 
113   mib[0] = CTL_NET;
114   mib[1] = PF_ROUTE;
115   mib[2] = 0;
116   mib[3] = 0;
117   mib[4] = NET_RT_IFLIST;
118   mib[5] = 0;
119 
120   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
121     fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
122               strerror(errno));
123     close(s);
124     return NULL;
125   }
126 
127   if ((buf = (char *)malloc(needed)) == NULL) {
128     fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
129     close(s);
130     return NULL;
131   }
132 
133   if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
134     fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(errno));
135     free(buf);
136     close(s);
137     return NULL;
138   }
139 
140   ptr = buf;
141   end = buf + needed;
142   iface = NULL;
143   namelen = strlen(name);
144 
145   while (ptr < end && iface == NULL) {
146     ifm = (struct if_msghdr *)ptr;			/* On if_msghdr */
147     if (ifm->ifm_type != RTM_IFINFO)
148       break;
149     dl = (struct sockaddr_dl *)(ifm + 1);		/* Single _dl at end */
150     if (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) {
151       iface = (struct iface *)malloc(sizeof *iface);
152       if (iface == NULL) {
153         fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
154         return NULL;
155       }
156       iface->name = strdup(name);
157       iface->flags = ifm->ifm_flags;
158       iface->index = ifm->ifm_index;
159       iface->in_addrs = 0;
160       iface->in_addr = NULL;
161     }
162     ptr += ifm->ifm_msglen;				/* First ifa_msghdr */
163     for (; ptr < end; ptr += ifam->ifam_msglen) {
164       ifam = (struct ifa_msghdr *)ptr;			/* Next if address */
165 
166       if (ifam->ifam_type != RTM_NEWADDR)		/* finished this if */
167         break;
168 
169       if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
170         /* Found a configured interface ! */
171         iface_ParseHdr(ifam, sa);
172 
173         if (sa[RTAX_IFA] && sa[RTAX_IFA]->sa_family == AF_INET) {
174           /* Record the address */
175 
176           addr = (struct iface_addr *)realloc
177             (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
178           if (addr == NULL)
179             break;
180           iface->in_addr = addr;
181 
182           addr += iface->in_addrs;
183           iface->in_addrs++;
184 
185           addr->ifa = ((struct sockaddr_in *)sa[RTAX_IFA])->sin_addr;
186 
187           if (sa[RTAX_BRD])
188             addr->brd = ((struct sockaddr_in *)sa[RTAX_BRD])->sin_addr;
189           else
190             addr->brd.s_addr = INADDR_ANY;
191 
192           if (sa[RTAX_NETMASK])
193             addr->mask = ((struct sockaddr_in *)sa[RTAX_NETMASK])->sin_addr;
194           else
195             addr->mask.s_addr = INADDR_ANY;
196 
197           addr->bits = bitsinmask(addr->mask);
198         }
199       }
200     }
201   }
202 
203   free(buf);
204   close(s);
205 
206   return iface;
207 }
208 
209 static void
210 iface_addr_Zap(const char *name, struct iface_addr *addr)
211 {
212   struct ifaliasreq ifra;
213   struct sockaddr_in *me, *peer;
214   int s;
215 
216   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
217   if (s < 0)
218     log_Printf(LogERROR, "iface_addr_Zap: socket(): %s\n", strerror(errno));
219   else {
220     memset(&ifra, '\0', sizeof ifra);
221     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
222     me = (struct sockaddr_in *)&ifra.ifra_addr;
223     peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
224     me->sin_family = peer->sin_family = AF_INET;
225     me->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
226     me->sin_addr = addr->ifa;
227     peer->sin_addr = addr->brd;
228     log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(addr->ifa));
229     if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0)
230       log_Printf(LogWARN, "iface_addr_Zap: ioctl(SIOCDIFADDR, %s): %s\n",
231                  inet_ntoa(addr->ifa), strerror(errno));
232     close(s);
233   }
234 }
235 
236 void
237 iface_inClear(struct iface *iface, int how)
238 {
239   int n, addrs;
240 
241   if (iface->in_addrs) {
242     addrs = n = how == IFACE_CLEAR_ALL ? 0 : 1;
243     for (; n < iface->in_addrs; n++)
244       iface_addr_Zap(iface->name, iface->in_addr + n);
245 
246     iface->in_addrs = addrs;
247     /* Don't bother realloc()ing - we have little to gain */
248   }
249 }
250 
251 int
252 iface_inAdd(struct iface *iface, struct in_addr ifa, struct in_addr mask,
253             struct in_addr brd, int how)
254 {
255   int slot, s, chg;
256   struct ifaliasreq ifra;
257   struct sockaddr_in *me, *peer, *msk;
258   struct iface_addr *addr;
259 
260   for (slot = 0; slot < iface->in_addrs; slot++)
261     if (iface->in_addr[slot].ifa.s_addr == ifa.s_addr) {
262       if (how & IFACE_FORCE_ADD)
263         break;
264       else
265         /* errno = EEXIST; */
266         return 0;
267     }
268 
269   addr = (struct iface_addr *)realloc
270     (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
271   if (addr == NULL) {
272     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
273     return 0;
274   }
275   iface->in_addr = addr;
276 
277   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
278   if (s < 0) {
279     log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno));
280     return 0;
281   }
282 
283   /*
284    * We've gotta be careful here.  If we try to add an address with the
285    * same destination as an existing interface, nothing will work.
286    * Instead, we tweak all previous address entries that match the
287    * to-be-added destination to 255.255.255.255 (w/ a similar netmask).
288    * There *may* be more than one - if the user has ``iface add''ed
289    * stuff previously.
290    */
291   for (chg = 0; chg < iface->in_addrs; chg++) {
292     if ((iface->in_addr[chg].brd.s_addr == brd.s_addr &&
293          brd.s_addr != INADDR_BROADCAST) || chg == slot) {
294       memset(&ifra, '\0', sizeof ifra);
295       strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
296       me = (struct sockaddr_in *)&ifra.ifra_addr;
297       msk = (struct sockaddr_in *)&ifra.ifra_mask;
298       peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
299       me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
300       me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
301       me->sin_addr = iface->in_addr[chg].ifa;
302       msk->sin_addr = iface->in_addr[chg].mask;
303       peer->sin_addr = iface->in_addr[chg].brd;
304       log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(me->sin_addr));
305       ID0ioctl(s, SIOCDIFADDR, &ifra);	/* Don't care if it fails... */
306       if (chg != slot) {
307         peer->sin_addr.s_addr = iface->in_addr[chg].brd.s_addr =
308           msk->sin_addr.s_addr = iface->in_addr[chg].mask.s_addr =
309             INADDR_BROADCAST;
310         iface->in_addr[chg].bits = 32;
311         log_Printf(LogDEBUG, "Add %s -> 255.255.255.255\n",
312                    inet_ntoa(me->sin_addr));
313         if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && errno != EEXIST) {
314           /* Oops - that's bad(ish) news !  We've lost an alias ! */
315           log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
316                inet_ntoa(me->sin_addr), strerror(errno));
317           iface->in_addrs--;
318           bcopy(iface->in_addr + chg + 1, iface->in_addr + chg,
319                 (iface->in_addrs - chg) * sizeof iface->in_addr[0]);
320           if (slot > chg)
321             slot--;
322           chg--;
323         }
324       }
325     }
326   }
327 
328   memset(&ifra, '\0', sizeof ifra);
329   strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
330   me = (struct sockaddr_in *)&ifra.ifra_addr;
331   msk = (struct sockaddr_in *)&ifra.ifra_mask;
332   peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
333   me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
334   me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
335   me->sin_addr = ifa;
336   msk->sin_addr = mask;
337   peer->sin_addr = brd;
338 
339   if (log_IsKept(LogDEBUG)) {
340     char buf[16];
341 
342     strncpy(buf, inet_ntoa(brd), sizeof buf-1);
343     buf[sizeof buf - 1] = '\0';
344     log_Printf(LogDEBUG, "Add %s -> %s\n", inet_ntoa(ifa), buf);
345   }
346 
347   /* An EEXIST failure w/ brd == INADDR_BROADCAST is ok (and works!) */
348   if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 &&
349       (brd.s_addr != INADDR_BROADCAST || errno != EEXIST)) {
350     log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
351                inet_ntoa(ifa), strerror(errno));
352     ID0ioctl(s, SIOCDIFADDR, &ifra);	/* EEXIST ? */
353     close(s);
354     return 0;
355   }
356   close(s);
357 
358   if (slot == iface->in_addrs) {
359     /* We're adding a new interface address */
360 
361     if (how & IFACE_ADD_FIRST) {
362       /* Stuff it at the start of our list */
363       slot = 0;
364       bcopy(iface->in_addr, iface->in_addr + 1,
365             iface->in_addrs * sizeof iface->in_addr[0]);
366     }
367 
368     iface->in_addrs++;
369   } else if (how & IFACE_ADD_FIRST) {
370     /* Shift it up to the first slot */
371     bcopy(iface->in_addr, iface->in_addr + 1, slot * sizeof iface->in_addr[0]);
372     slot = 0;
373   }
374 
375   iface->in_addr[slot].ifa = ifa;
376   iface->in_addr[slot].mask = mask;
377   iface->in_addr[slot].brd = brd;
378   iface->in_addr[slot].bits = bitsinmask(iface->in_addr[slot].mask);
379 
380   return 1;
381 }
382 
383 int
384 iface_inDelete(struct iface *iface, struct in_addr ip)
385 {
386   int n;
387 
388   for (n = 0; n < iface->in_addrs; n++)
389     if (iface->in_addr[n].ifa.s_addr == ip.s_addr) {
390       iface_addr_Zap(iface->name, iface->in_addr + n);
391       bcopy(iface->in_addr + n + 1, iface->in_addr + n,
392             (iface->in_addrs - n - 1) * sizeof iface->in_addr[0]);
393       iface->in_addrs--;
394       return 1;
395     }
396 
397   return 0;
398 }
399 
400 #define IFACE_ADDFLAGS 1
401 #define IFACE_DELFLAGS 2
402 
403 static int
404 iface_ChangeFlags(struct iface *iface, int flags, int how)
405 {
406   struct ifreq ifrq;
407   int s;
408 
409   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
410   if (s < 0) {
411     log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
412     return 0;
413   }
414 
415   memset(&ifrq, '\0', sizeof ifrq);
416   strncpy(ifrq.ifr_name, iface->name, sizeof ifrq.ifr_name - 1);
417   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
418   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
419     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
420        strerror(errno));
421     close(s);
422     return 0;
423   }
424 
425   if (how == IFACE_ADDFLAGS)
426     ifrq.ifr_flags |= flags;
427   else
428     ifrq.ifr_flags &= ~flags;
429 
430   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
431     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
432        strerror(errno));
433     close(s);
434     return 0;
435   }
436   close(s);
437 
438   return 1;	/* Success */
439 }
440 
441 int
442 iface_SetFlags(struct iface *iface, int flags)
443 {
444   return iface_ChangeFlags(iface, flags, IFACE_ADDFLAGS);
445 }
446 
447 int
448 iface_ClearFlags(struct iface *iface, int flags)
449 {
450   return iface_ChangeFlags(iface, flags, IFACE_DELFLAGS);
451 }
452 
453 void
454 iface_Destroy(struct iface *iface)
455 {
456   /*
457    * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
458    * if that's what the user wants.  It's better to leave the interface
459    * allocated so that existing connections can continue to work.
460    */
461 
462   if (iface != NULL) {
463     free(iface->name);
464     free(iface->in_addr);
465     free(iface);
466   }
467 }
468 
469 #define if_entry(x) { IFF_##x, #x }
470 
471 struct {
472   int flag;
473   const char *value;
474 } if_flags[] = {
475   if_entry(UP),
476   if_entry(BROADCAST),
477   if_entry(DEBUG),
478   if_entry(LOOPBACK),
479   if_entry(POINTOPOINT),
480   if_entry(RUNNING),
481   if_entry(NOARP),
482   if_entry(PROMISC),
483   if_entry(ALLMULTI),
484   if_entry(OACTIVE),
485   if_entry(SIMPLEX),
486   if_entry(LINK0),
487   if_entry(LINK1),
488   if_entry(LINK2),
489   if_entry(MULTICAST),
490   { 0, "???" }
491 };
492 
493 int
494 iface_Show(struct cmdargs const *arg)
495 {
496   struct iface *iface = arg->bundle->iface, *current;
497   int f, flags;
498 
499   current = iface_Create(iface->name);
500   flags = iface->flags = current->flags;
501   iface_Destroy(current);
502 
503   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
504   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
505     if ((if_flags[f].flag & flags) || (!if_flags[f].flag && flags)) {
506       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
507                     if_flags[f].value);
508       flags &= ~if_flags[f].flag;
509     }
510   prompt_Printf(arg->prompt, "> has %d address%s:\n", iface->in_addrs,
511                 iface->in_addrs == 1 ? "" : "es");
512 
513   for (f = 0; f < iface->in_addrs; f++) {
514     prompt_Printf(arg->prompt, "  %s", inet_ntoa(iface->in_addr[f].ifa));
515     if (iface->in_addr[f].bits >= 0)
516       prompt_Printf(arg->prompt, "/%d", iface->in_addr[f].bits);
517     if (iface->flags & IFF_POINTOPOINT)
518       prompt_Printf(arg->prompt, " -> %s", inet_ntoa(iface->in_addr[f].brd));
519     else if (iface->flags & IFF_BROADCAST)
520       prompt_Printf(arg->prompt, " broadcast %s",
521                     inet_ntoa(iface->in_addr[f].brd));
522     if (iface->in_addr[f].bits < 0)
523       prompt_Printf(arg->prompt, " (mask %s)",
524                     inet_ntoa(iface->in_addr[f].mask));
525     prompt_Printf(arg->prompt, "\n");
526   }
527 
528   return 0;
529 }
530 
531 void
532 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
533 {
534   char *wp;
535   int rtax;
536 
537   wp = (char *)(ifam + 1);
538 
539   for (rtax = 0; rtax < RTAX_MAX; rtax++)
540     if (ifam->ifam_addrs & (1 << rtax)) {
541       sa[rtax] = (struct sockaddr *)wp;
542       wp += ROUNDUP(sa[rtax]->sa_len);
543     } else
544       sa[rtax] = NULL;
545 }
546