xref: /freebsd/usr.sbin/ppp/iface.c (revision f9ce010afdd3136fc73e2b500f2ed916bf9cfa59)
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], i, s;
98   size_t needed;
99   char *buf, *ptr, *end, *cp, *lim;
100   struct if_msghdr *ifm;
101   struct ifa_msghdr *ifam;
102   struct sockaddr_dl *dl;
103   struct rt_addrinfo rti;
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, "clean: sysctl: estimate: %s\n",
122               strerror(errno));
123     close(s);
124     return NULL;
125   }
126 
127   if ((buf = (char *)malloc(needed)) == NULL) {
128     close(s);
129     return NULL;
130   }
131 
132   if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
133     free(buf);
134     close(s);
135     return NULL;
136   }
137 
138   ptr = buf;
139   end = buf + needed;
140   iface = NULL;
141 
142   while (ptr < end && iface == NULL) {
143     ifm = (struct if_msghdr *)ptr;			/* On if_msghdr */
144     if (ifm->ifm_type != RTM_IFINFO)
145       break;
146     dl = (struct sockaddr_dl *)(ifm + 1);		/* Single _dl at end */
147     if (!strncmp(name, dl->sdl_data, dl->sdl_nlen)) {
148       iface = (struct iface *)malloc(sizeof *iface);
149       if (iface == NULL) {
150         fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
151         return NULL;
152       }
153       iface->name = strdup(name);
154       iface->flags = ifm->ifm_flags;
155       iface->index = ifm->ifm_index;
156       iface->in_addrs = 0;
157       iface->in_addr = NULL;
158     }
159     ptr += ifm->ifm_msglen;				/* First ifa_msghdr */
160     for (; ptr < end; ptr += ifam->ifam_msglen) {
161       ifam = (struct ifa_msghdr *)ptr;			/* Next if address */
162 
163       if (ifam->ifam_type != RTM_NEWADDR)		/* finished this if */
164         break;
165 
166       if (iface == NULL)				/* Keep wading */
167         continue;
168 
169       /* Found an address ! */
170 
171       if (ifam->ifam_addrs & (1 << RTAX_IFA)) {
172         /* *And* it's configured ! */
173         rti.rti_addrs = ifam->ifam_addrs;
174         lim = (char *)ifam + ifam->ifam_msglen;
175         cp = (char *)(ifam + 1);
176         memset(rti.rti_info, '\0', sizeof(rti.rti_info));
177         for (i = 0; i < RTAX_MAX && cp < lim; i++) {
178           if ((rti.rti_addrs & (1 << i)) == 0)
179             continue;
180           rti.rti_info[i] = (struct sockaddr *)cp;
181 #define ROUNDUP(x) \
182           ((x) > 0 ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
183           cp += ROUNDUP(rti.rti_info[i]->sa_len);
184         }
185 
186         if (rti.rti_info[RTAX_IFA] &&
187             rti.rti_info[RTAX_IFA]->sa_family == AF_INET) {
188           /* Record the iface address rti */
189 
190           addr = (struct iface_addr *)realloc
191             (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
192           if (addr == NULL)
193             break;
194           iface->in_addr = addr;
195 
196           addr += iface->in_addrs;
197           iface->in_addrs++;
198 
199           addr->ifa.s_addr = ((struct sockaddr_in *)rti.rti_info[RTAX_IFA])->
200             sin_addr.s_addr;
201           addr->brd.s_addr = rti.rti_info[RTAX_BRD] ?
202             ((struct sockaddr_in *)rti.rti_info[RTAX_BRD])->sin_addr.s_addr :
203             INADDR_ANY;
204           addr->mask.s_addr = rti.rti_info[RTAX_NETMASK] ?
205             ((struct sockaddr_in *)rti.rti_info[RTAX_NETMASK])->sin_addr.s_addr:
206             INADDR_ANY;
207 
208           addr->bits = bitsinmask(addr->mask);
209         }
210       }
211     }
212   }
213 
214   free(buf);
215   close(s);
216 
217   return iface;
218 }
219 
220 static void
221 iface_addr_Zap(const char *name, struct iface_addr *addr)
222 {
223   struct ifaliasreq ifra;
224   struct sockaddr_in *me, *peer;
225   int s;
226 
227   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
228   if (s < 0)
229     log_Printf(LogERROR, "iface_addr_Zap: socket(): %s\n", strerror(errno));
230   else {
231     memset(&ifra, '\0', sizeof ifra);
232     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
233     me = (struct sockaddr_in *)&ifra.ifra_addr;
234     peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
235     me->sin_family = peer->sin_family = AF_INET;
236     me->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
237     me->sin_addr = addr->ifa;
238     peer->sin_addr = addr->brd;
239     log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(addr->ifa));
240     if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0)
241       log_Printf(LogWARN, "iface_addr_Zap: ioctl(SIOCDIFADDR, %s): %s\n",
242                  inet_ntoa(addr->ifa), strerror(errno));
243     close(s);
244   }
245 }
246 
247 void
248 iface_inClear(struct iface *iface, int how)
249 {
250   int n, addrs;
251 
252   addrs = n = how == IFACE_CLEAR_ALL ? 0 : 1;
253   for (; n < iface->in_addrs; n++)
254     iface_addr_Zap(iface->name, iface->in_addr + n);
255 
256   iface->in_addrs = addrs;
257   /* Don't bother realloc()ing - we have little to gain */
258 }
259 
260 int
261 iface_inAdd(struct iface *iface, struct in_addr ifa, struct in_addr mask,
262             struct in_addr brd, int how)
263 {
264   int slot, s, chg;
265   struct ifaliasreq ifra;
266   struct sockaddr_in *me, *peer, *msk;
267   struct iface_addr *addr;
268 
269   for (slot = 0; slot < iface->in_addrs; slot++)
270     if (iface->in_addr[slot].ifa.s_addr == ifa.s_addr) {
271       if (how & IFACE_FORCE_ADD)
272         break;
273       else
274         /* errno = EEXIST; */
275         return 0;
276     }
277 
278   addr = (struct iface_addr *)realloc
279     (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
280   if (addr == NULL) {
281     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
282     return 0;
283   }
284   iface->in_addr = addr;
285 
286   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
287   if (s < 0) {
288     log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno));
289     return 0;
290   }
291 
292   /*
293    * We've gotta be careful here.  If we try to add an address with the
294    * same destination as an existing interface, nothing will work.
295    * Instead, we tweak all previous address entries that match the
296    * to-be-added destination to 255.255.255.255 (w/ a similar netmask).
297    * There *may* be more than one - if the user has ``iface add''ed
298    * stuff previously.
299    */
300   for (chg = 0; chg < iface->in_addrs; chg++) {
301     if ((iface->in_addr[chg].brd.s_addr == brd.s_addr &&
302          brd.s_addr != INADDR_BROADCAST) || chg == slot) {
303       memset(&ifra, '\0', sizeof ifra);
304       strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
305       me = (struct sockaddr_in *)&ifra.ifra_addr;
306       msk = (struct sockaddr_in *)&ifra.ifra_mask;
307       peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
308       me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
309       me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
310       me->sin_addr = iface->in_addr[chg].ifa;
311       msk->sin_addr = iface->in_addr[chg].mask;
312       peer->sin_addr = iface->in_addr[chg].brd;
313       log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(me->sin_addr));
314       ID0ioctl(s, SIOCDIFADDR, &ifra);	/* Don't care if it fails... */
315       if (chg != slot) {
316         peer->sin_addr.s_addr = iface->in_addr[chg].brd.s_addr =
317           msk->sin_addr.s_addr = iface->in_addr[chg].mask.s_addr =
318             INADDR_BROADCAST;
319         iface->in_addr[chg].bits = 32;
320         log_Printf(LogDEBUG, "Add %s -> 255.255.255.255\n",
321                    inet_ntoa(me->sin_addr));
322         if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && errno != EEXIST) {
323           /* Oops - that's bad(ish) news !  We've lost an alias ! */
324           log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
325                inet_ntoa(me->sin_addr), strerror(errno));
326           iface->in_addrs--;
327           bcopy(iface->in_addr + chg + 1, iface->in_addr + chg,
328                 (iface->in_addrs - chg) * sizeof iface->in_addr[0]);
329           if (slot > chg)
330             slot--;
331           chg--;
332         }
333       }
334     }
335   }
336 
337   memset(&ifra, '\0', sizeof ifra);
338   strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
339   me = (struct sockaddr_in *)&ifra.ifra_addr;
340   msk = (struct sockaddr_in *)&ifra.ifra_mask;
341   peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
342   me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
343   me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
344   me->sin_addr = ifa;
345   msk->sin_addr = mask;
346   peer->sin_addr = brd;
347 
348   if (log_IsKept(LogDEBUG)) {
349     char buf[16];
350 
351     strncpy(buf, inet_ntoa(brd), sizeof buf-1);
352     buf[sizeof buf - 1] = '\0';
353     log_Printf(LogDEBUG, "Add %s -> %s\n", inet_ntoa(ifa), buf);
354   }
355 
356   /* An EEXIST failure w/ brd == INADDR_BROADCAST is ok (and works!) */
357   if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 &&
358       (brd.s_addr != INADDR_BROADCAST || errno != EEXIST)) {
359     log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
360                inet_ntoa(ifa), strerror(errno));
361     ID0ioctl(s, SIOCDIFADDR, &ifra);	/* EEXIST ? */
362     close(s);
363     return 0;
364   }
365   close(s);
366 
367   if (slot == iface->in_addrs) {
368     /* We're adding a new interface address */
369 
370     if (how & IFACE_ADD_FIRST) {
371       /* Stuff it at the start of our list */
372       slot = 0;
373       bcopy(iface->in_addr, iface->in_addr + 1,
374             iface->in_addrs * sizeof iface->in_addr[0]);
375     }
376 
377     iface->in_addrs++;
378   } else if (how & IFACE_ADD_FIRST) {
379     /* Shift it up to the first slot */
380     bcopy(iface->in_addr, iface->in_addr + 1, slot * sizeof iface->in_addr[0]);
381     slot = 0;
382   }
383 
384   iface->in_addr[slot].ifa = ifa;
385   iface->in_addr[slot].mask = mask;
386   iface->in_addr[slot].brd = brd;
387   iface->in_addr[slot].bits = bitsinmask(iface->in_addr[slot].mask);
388 
389   return 1;
390 }
391 
392 int
393 iface_inDelete(struct iface *iface, struct in_addr ip)
394 {
395   int n;
396 
397   for (n = 0; n < iface->in_addrs; n++)
398     if (iface->in_addr[n].ifa.s_addr == ip.s_addr) {
399       iface_addr_Zap(iface->name, iface->in_addr + n);
400       bcopy(iface->in_addr + n + 1, iface->in_addr + n,
401             (iface->in_addrs - n - 1) * sizeof iface->in_addr[0]);
402       iface->in_addrs--;
403       return 1;
404     }
405 
406   return 0;
407 }
408 
409 #define IFACE_ADDFLAGS 1
410 #define IFACE_DELFLAGS 2
411 
412 static int
413 iface_ChangeFlags(struct iface *iface, int flags, int how)
414 {
415   struct ifreq ifrq;
416   int s;
417 
418   s = ID0socket(AF_INET, SOCK_DGRAM, 0);
419   if (s < 0) {
420     log_Printf(LogERROR, "iface_ClearFlags: socket: %s\n", strerror(errno));
421     return 0;
422   }
423 
424   memset(&ifrq, '\0', sizeof ifrq);
425   strncpy(ifrq.ifr_name, iface->name, sizeof ifrq.ifr_name - 1);
426   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
427   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
428     log_Printf(LogERROR, "iface_ClearFlags: ioctl(SIOCGIFFLAGS): %s\n",
429        strerror(errno));
430     close(s);
431     return 0;
432   }
433 
434   if (how == IFACE_ADDFLAGS)
435     ifrq.ifr_flags |= flags;
436   else
437     ifrq.ifr_flags &= ~flags;
438 
439   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
440     log_Printf(LogERROR, "iface_ClearFlags: ioctl(SIOCSIFFLAGS): %s\n",
441        strerror(errno));
442     close(s);
443     return 0;
444   }
445   close(s);
446 
447   return 1;	/* Success */
448 }
449 
450 int
451 iface_SetFlags(struct iface *iface, int flags)
452 {
453   return iface_ChangeFlags(iface, flags, IFACE_ADDFLAGS);
454 }
455 
456 int
457 iface_ClearFlags(struct iface *iface, int flags)
458 {
459   return iface_ChangeFlags(iface, flags, IFACE_DELFLAGS);
460 }
461 
462 void
463 iface_Destroy(struct iface *iface)
464 {
465   /*
466    * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
467    * if that's what the user wants.  It's better to leave the interface
468    * allocated so that existing connections can continue to work.
469    */
470 
471   if (iface != NULL) {
472     free(iface->name);
473     free(iface->in_addr);
474     free(iface);
475   }
476 }
477 
478 #define if_entry(x) { IFF_##x, #x }
479 
480 struct {
481   int flag;
482   const char *value;
483 } if_flags[] = {
484   if_entry(UP),
485   if_entry(BROADCAST),
486   if_entry(DEBUG),
487   if_entry(LOOPBACK),
488   if_entry(POINTOPOINT),
489   if_entry(RUNNING),
490   if_entry(NOARP),
491   if_entry(PROMISC),
492   if_entry(ALLMULTI),
493   if_entry(OACTIVE),
494   if_entry(SIMPLEX),
495   if_entry(LINK0),
496   if_entry(LINK1),
497   if_entry(LINK2),
498   if_entry(MULTICAST),
499   { 0, "???" }
500 };
501 
502 int
503 iface_Show(struct cmdargs const *arg)
504 {
505   struct iface *iface = arg->bundle->iface, *current;
506   int f, flags;
507 
508   current = iface_Create(iface->name);
509   flags = iface->flags = current->flags;
510   iface_Destroy(current);
511 
512   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
513   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
514     if ((if_flags[f].flag & flags) || (!if_flags[f].flag && flags)) {
515       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
516                     if_flags[f].value);
517       flags &= ~if_flags[f].flag;
518     }
519   prompt_Printf(arg->prompt, "> has %d address%s:\n", iface->in_addrs,
520                 iface->in_addrs == 1 ? "" : "es");
521 
522   for (f = 0; f < iface->in_addrs; f++) {
523     prompt_Printf(arg->prompt, "  %s", inet_ntoa(iface->in_addr[f].ifa));
524     if (iface->in_addr[f].bits >= 0)
525       prompt_Printf(arg->prompt, "/%d", iface->in_addr[f].bits);
526     if (iface->flags & IFF_POINTOPOINT)
527       prompt_Printf(arg->prompt, " -> %s", inet_ntoa(iface->in_addr[f].brd));
528     else if (iface->flags & IFF_BROADCAST)
529       prompt_Printf(arg->prompt, " broadcast %s",
530                     inet_ntoa(iface->in_addr[f].brd));
531     if (iface->in_addr[f].bits < 0)
532       prompt_Printf(arg->prompt, " (mask %s)",
533                     inet_ntoa(iface->in_addr[f].mask));
534     prompt_Printf(arg->prompt, "\n");
535   }
536 
537   return 0;
538 }
539