xref: /freebsd/usr.sbin/ppp/iface.c (revision 2ab5515001905576d4f9642691ae99d3ef672fc7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
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 <netinet/in_systm.h>
36 #include <netinet/in_var.h>
37 #include <netinet/ip.h>
38 #ifndef NOINET6
39 #include <netinet6/nd6.h>
40 #endif
41 #include <sys/un.h>
42 
43 #include <errno.h>
44 #include <string.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <sys/ioctl.h>
49 #include <sys/sysctl.h>
50 #include <termios.h>
51 #include <unistd.h>
52 
53 #include "layer.h"
54 #include "defs.h"
55 #include "command.h"
56 #include "mbuf.h"
57 #include "log.h"
58 #include "id.h"
59 #include "timer.h"
60 #include "fsm.h"
61 #include "iplist.h"
62 #include "lqr.h"
63 #include "hdlc.h"
64 #include "throughput.h"
65 #include "slcompress.h"
66 #include "descriptor.h"
67 #include "ncpaddr.h"
68 #include "ipcp.h"
69 #include "filter.h"
70 #include "lcp.h"
71 #include "ccp.h"
72 #include "link.h"
73 #include "mp.h"
74 #ifndef NORADIUS
75 #include "radius.h"
76 #endif
77 #include "ipv6cp.h"
78 #include "ncp.h"
79 #include "bundle.h"
80 #include "prompt.h"
81 #include "iface.h"
82 
83 #define IN6MASK128	{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
84 			    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
85 static const struct in6_addr in6mask128 = IN6MASK128;
86 
87 
88 struct iface *
iface_Create(const char * name)89 iface_Create(const char *name)
90 {
91   int mib[6], maxtries, err;
92   size_t needed, namelen;
93   char *buf, *ptr, *end;
94   struct if_msghdr *ifm;
95   struct ifa_msghdr *ifam;
96   struct sockaddr_dl *dl;
97   struct sockaddr *sa[RTAX_MAX];
98   struct iface *iface;
99   struct iface_addr *addr;
100 
101   mib[0] = CTL_NET;
102   mib[1] = PF_ROUTE;
103   mib[2] = 0;
104   mib[3] = 0;
105   mib[4] = NET_RT_IFLIST;
106   mib[5] = 0;
107 
108   maxtries = 20;
109   err = 0;
110   do {
111     if (maxtries-- == 0 || (err && err != ENOMEM)) {
112       fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(err));
113       return NULL;
114     }
115 
116     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
117       fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
118                 strerror(errno));
119       return NULL;
120     }
121 
122     if ((buf = (char *)malloc(needed)) == NULL) {
123       fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
124       return NULL;
125     }
126 
127     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
128       err = errno;
129       free(buf);
130       buf = NULL;
131     }
132   } while (buf == NULL);
133 
134   ptr = buf;
135   end = buf + needed;
136   iface = NULL;
137   namelen = strlen(name);
138 
139   while (ptr < end && iface == NULL) {
140     ifm = (struct if_msghdr *)ptr;			/* On if_msghdr */
141     if (ifm->ifm_type != RTM_IFINFO)
142       break;
143     dl = (struct sockaddr_dl *)(ifm + 1);		/* Single _dl at end */
144     if (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) {
145       iface = (struct iface *)malloc(sizeof *iface);
146       if (iface == NULL) {
147         fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
148 	free(buf);
149         return NULL;
150       }
151       iface->name = strdup(name);
152       iface->descr = NULL;
153       iface->index = ifm->ifm_index;
154       iface->flags = ifm->ifm_flags;
155       iface->mtu = 0;
156       iface->addrs = 0;
157       iface->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 && ifam->ifam_addrs & RTA_IFA) {
167         /* Found a configured interface ! */
168         iface_ParseHdr(ifam, sa);
169 
170         if (sa[RTAX_IFA] && (sa[RTAX_IFA]->sa_family == AF_INET
171 #ifndef NOINET6
172                              || sa[RTAX_IFA]->sa_family == AF_INET6
173 #endif
174                              )) {
175           /* Record the address */
176 
177           addr = (struct iface_addr *)
178             realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
179           if (addr == NULL)
180             break;
181           iface->addr = addr;
182 
183           addr += iface->addrs;
184           iface->addrs++;
185 
186           ncprange_setsa(&addr->ifa, sa[RTAX_IFA], sa[RTAX_NETMASK]);
187           if (sa[RTAX_BRD])
188             ncpaddr_setsa(&addr->peer, sa[RTAX_BRD]);
189           else
190             ncpaddr_init(&addr->peer);
191         }
192       }
193     }
194   }
195 
196   free(buf);
197 
198   return iface;
199 }
200 
201 static int
iface_addr_Zap(const char * name,struct iface_addr * addr,int s)202 iface_addr_Zap(const char *name, struct iface_addr *addr, int s)
203 {
204   struct ifreq ifr;
205 #ifndef NOINET6
206   struct in6_ifreq ifr6;
207 #endif
208   struct sockaddr_in *me4;
209   struct sockaddr_storage ssme;
210   int res, saved_errno;
211 
212   ncprange_getsa(&addr->ifa, &ssme, NULL);
213   res = 0;
214 
215   switch (ncprange_family(&addr->ifa)) {
216   case AF_INET:
217     memset(&ifr, '\0', sizeof ifr);
218     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name - 1);
219     me4 = (struct sockaddr_in *)&ifr.ifr_addr;
220     memcpy(me4, &ssme, sizeof *me4);
221 
222     res = ID0ioctl(s, SIOCDIFADDR, &ifr);
223     saved_errno = errno;
224     if (log_IsKept(LogDEBUG)) {
225       char buf[NCP_ASCIIBUFFERSIZE];
226 
227       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
228       log_Printf(LogWARN, "%s: DIFADDR %s -> %s returns %d\n",
229                  ifr.ifr_name, buf, ncpaddr_ntoa(&addr->peer), res);
230     }
231     break;
232 
233 #ifndef NOINET6
234   case AF_INET6:
235     memset(&ifr6, '\0', sizeof ifr6);
236     strncpy(ifr6.ifr_name, name, sizeof ifr6.ifr_name - 1);
237     memcpy(&ifr6.ifr_addr, &ssme, sizeof ifr6.ifr_addr);
238 
239     res = ID0ioctl(s, SIOCDIFADDR_IN6, &ifr6);
240     saved_errno = errno;
241     break;
242 #endif
243   }
244 
245   if (res == -1) {
246     char dst[NCP_ASCIIBUFFERSIZE];
247     const char *end =
248 #ifndef NOINET6
249       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
250 #endif
251       "";
252 
253     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
254       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s): %s\n",
255                  end, ncprange_ntoa(&addr->ifa), strerror(saved_errno));
256     else {
257       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
258       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s -> %s): %s\n",
259                  end, ncprange_ntoa(&addr->ifa), dst, strerror(saved_errno));
260     }
261   }
262 
263   return res != -1;
264 }
265 
266 static int
iface_addr_Add(const char * name,struct iface_addr * addr,int s)267 iface_addr_Add(const char *name, struct iface_addr *addr, int s)
268 {
269   struct ifaliasreq ifra;
270 #ifndef NOINET6
271   struct in6_aliasreq ifra6;
272 #endif
273   struct sockaddr_in *me4, *msk4, *peer4;
274   struct sockaddr_storage ssme, sspeer, ssmsk;
275   int res, saved_errno;
276 
277   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
278   ncpaddr_getsa(&addr->peer, &sspeer);
279   res = 0;
280 
281   switch (ncprange_family(&addr->ifa)) {
282   case AF_INET:
283     memset(&ifra, '\0', sizeof ifra);
284     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
285 
286     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
287     memcpy(me4, &ssme, sizeof *me4);
288 
289     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
290     memcpy(msk4, &ssmsk, sizeof *msk4);
291 
292     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
293     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
294       peer4->sin_family = AF_INET;
295       peer4->sin_len = sizeof(*peer4);
296       peer4->sin_addr.s_addr = INADDR_NONE;
297     } else
298       memcpy(peer4, &sspeer, sizeof *peer4);
299 
300     res = ID0ioctl(s, SIOCAIFADDR, &ifra);
301     saved_errno = errno;
302     if (log_IsKept(LogDEBUG)) {
303       char buf[NCP_ASCIIBUFFERSIZE];
304 
305       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
306       log_Printf(LogWARN, "%s: AIFADDR %s -> %s returns %d\n",
307                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
308     }
309     break;
310 
311 #ifndef NOINET6
312   case AF_INET6:
313     memset(&ifra6, '\0', sizeof ifra6);
314     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
315 
316     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
317     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
318     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
319       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
320     else if (memcmp(&((struct sockaddr_in6 *)&ssmsk)->sin6_addr, &in6mask128,
321 		    sizeof in6mask128) == 0)
322       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
323     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
324     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
325 
326     res = ID0ioctl(s, SIOCAIFADDR_IN6, &ifra6);
327     saved_errno = errno;
328     break;
329 #endif
330   }
331 
332   if (res == -1) {
333     char dst[NCP_ASCIIBUFFERSIZE];
334     const char *end =
335 #ifndef NOINET6
336       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
337 #endif
338       "";
339 
340     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
341       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s): %s\n",
342                  end, ncprange_ntoa(&addr->ifa), strerror(saved_errno));
343     else {
344       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
345       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s -> %s): %s\n",
346                  end, ncprange_ntoa(&addr->ifa), dst, strerror(saved_errno));
347     }
348   }
349 
350   return res != -1;
351 }
352 
353 int
iface_Name(struct iface * iface,const char * name)354 iface_Name(struct iface *iface, const char *name)
355 {
356   struct ifreq ifr;
357   int s;
358   char *newname;
359 
360   if ((newname = strdup(name)) == NULL) {
361     log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
362     return 0;
363   }
364 
365   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
366     log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
367     free(newname);
368     return 0;
369   }
370 
371   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
372   ifr.ifr_data = newname;
373   if (ID0ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
374     log_Printf(LogWARN, "iface name: ioctl(SIOCSIFNAME, %s -> %s): %s\n",
375                name, newname, strerror(errno));
376     free(newname);
377     return 0;
378   }
379 
380   free(iface->name);
381   iface->name = newname;
382 
383   return 1;
384 }
385 
386 int
iface_Descr(struct cmdargs const * arg)387 iface_Descr(struct cmdargs const *arg)
388 {
389   struct ifreq ifr;
390   struct iface *iface;
391   size_t sz, len;
392   int s, n, ifdescr_maxlen;
393   char *descr;
394 
395   sz = sizeof(int);
396   if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
397     log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
398     return 1;
399   }
400 
401   if (ifdescr_maxlen < 1) {
402     log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
403     return 1;
404   }
405 
406   sz = sizeof(char) * ifdescr_maxlen;
407   if ((descr = malloc(sz)) == NULL) {
408     log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
409     return 1;
410   }
411 
412   *descr = '\0';
413   n = arg->argn;
414   while (n < arg->argc) {
415     if (n > arg->argn && (len = strlcat(descr, " ", sz)) >= sz)
416       break;
417     if ((len = strlcat(descr, arg->argv[n], sz)) >= sz)
418       break;
419     ++n;
420   }
421   if (len >= sz) {
422     log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n",
423                ifdescr_maxlen-1);
424     free(descr);
425     return 1;
426   }
427 
428   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
429     log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
430     free(descr);
431     return 1;
432   }
433 
434   iface = arg->bundle->iface;
435   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
436   ifr.ifr_buffer.length = strlen(descr) + 1;
437   ifr.ifr_buffer.buffer = descr;
438   if (ID0ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
439     log_Printf(LogWARN, "iface descr: ioctl(SIOCSIFDESCR, %s): %s\n",
440                descr, strerror(errno));
441     free(descr);
442     return 1;
443   }
444 
445   free(iface->descr);
446   iface->descr = descr;
447 
448   return 0;
449 }
450 
451 void
iface_Clear(struct iface * iface,struct ncp * ncp,int family,int how)452 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
453 {
454   int af, inskip, in6skip, s4 = -1, s6 = -1, *s;
455   unsigned n;
456 
457   if (iface->addrs) {
458     inskip = in6skip = how == IFACE_CLEAR_ALL ? 0 : 1;
459 
460     for (n = 0; n < iface->addrs; n++) {
461       af = ncprange_family(&iface->addr[n].ifa);
462       if (family == 0 || family == af) {
463         if (!iface->addr[n].system && (how & IFACE_SYSTEM))
464           continue;
465         switch (af) {
466         case AF_INET:
467           if (inskip) {
468             inskip = 0;
469             continue;
470           }
471           s = &s4;
472           break;
473 
474 #ifndef NOINET6
475         case AF_INET6:
476           if (in6skip) {
477             in6skip = 0;
478             continue;
479           }
480           s = &s6;
481           break;
482 #endif
483         default:
484           continue;
485         }
486 
487         if (*s == -1 && (*s = ID0socket(af, SOCK_DGRAM, 0)) == -1)
488           log_Printf(LogERROR, "iface_Clear: socket(): %s\n", strerror(errno));
489         else if (iface_addr_Zap(iface->name, iface->addr + n, *s)) {
490           ncp_IfaceAddrDeleted(ncp, iface->addr + n);
491           bcopy(iface->addr + n + 1, iface->addr + n,
492                 (iface->addrs - n - 1) * sizeof *iface->addr);
493           iface->addrs--;
494           n--;
495         }
496       }
497     }
498 
499     /* Don't bother realloc()ing - we have little to gain */
500 
501     if (s4)
502       close(s4);
503     if (s6)
504       close(s6);
505   }
506 }
507 
508 int
iface_Add(struct iface * iface,struct ncp * ncp,const struct ncprange * ifa,const struct ncpaddr * peer,int how)509 iface_Add(struct iface *iface, struct ncp *ncp, const struct ncprange *ifa,
510           const struct ncpaddr *peer, int how)
511 {
512   int af, removed, s;
513   unsigned n;
514   struct ncpaddr ncplocal;
515   struct iface_addr *addr, newaddr;
516 
517   af = ncprange_family(ifa);
518   if ((s = ID0socket(af, SOCK_DGRAM, 0)) == -1) {
519     log_Printf(LogERROR, "iface_Add: socket(): %s\n", strerror(errno));
520     return 0;
521   }
522   ncprange_getaddr(ifa, &ncplocal);
523 
524   for (n = 0; n < iface->addrs; n++) {
525     if (ncprange_contains(&iface->addr[n].ifa, &ncplocal) ||
526         ncpaddr_equal(&iface->addr[n].peer, peer)) {
527       /* Replace this sockaddr */
528       if (!(how & IFACE_FORCE_ADD)) {
529         close(s);
530         return 0;	/* errno = EEXIST; */
531       }
532 
533       if (ncprange_equal(&iface->addr[n].ifa, ifa) &&
534           ncpaddr_equal(&iface->addr[n].peer, peer)) {
535         close(s);
536         ncp_IfaceAddrAdded(ncp, iface->addr + n);
537         return 1;	/* Already there */
538       }
539 
540       removed = iface_addr_Zap(iface->name, iface->addr + n, s);
541       if (removed)
542         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
543       ncprange_copy(&iface->addr[n].ifa, ifa);
544       ncpaddr_copy(&iface->addr[n].peer, peer);
545       if (!iface_addr_Add(iface->name, iface->addr + n, s)) {
546         if (removed) {
547           bcopy(iface->addr + n + 1, iface->addr + n,
548                 (iface->addrs - n - 1) * sizeof *iface->addr);
549           iface->addrs--;
550           n--;
551         }
552         close(s);
553         return 0;
554       }
555       close(s);
556       ncp_IfaceAddrAdded(ncp, iface->addr + n);
557       return 1;
558     }
559   }
560 
561   addr = (struct iface_addr *)realloc
562     (iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
563   if (addr == NULL) {
564     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
565     close(s);
566     return 0;
567   }
568   iface->addr = addr;
569 
570   ncprange_copy(&newaddr.ifa, ifa);
571   ncpaddr_copy(&newaddr.peer, peer);
572   newaddr.system = !!(how & IFACE_SYSTEM);
573   if (!iface_addr_Add(iface->name, &newaddr, s)) {
574     close(s);
575     return 0;
576   }
577 
578   if (how & IFACE_ADD_FIRST) {
579     /* Stuff it at the start of our list */
580     n = 0;
581     bcopy(iface->addr, iface->addr + 1, iface->addrs * sizeof *iface->addr);
582   } else
583     n = iface->addrs;
584 
585   iface->addrs++;
586   memcpy(iface->addr + n, &newaddr, sizeof(*iface->addr));
587 
588   close(s);
589   ncp_IfaceAddrAdded(ncp, iface->addr + n);
590 
591   return 1;
592 }
593 
594 int
iface_Delete(struct iface * iface,struct ncp * ncp,const struct ncpaddr * del)595 iface_Delete(struct iface *iface, struct ncp *ncp, const struct ncpaddr *del)
596 {
597   struct ncpaddr found;
598   unsigned n;
599   int res, s;
600 
601   if ((s = ID0socket(ncpaddr_family(del), SOCK_DGRAM, 0)) == -1) {
602     log_Printf(LogERROR, "iface_Delete: socket(): %s\n", strerror(errno));
603     return 0;
604   }
605 
606   for (n = res = 0; n < iface->addrs; n++) {
607     ncprange_getaddr(&iface->addr[n].ifa, &found);
608     if (ncpaddr_equal(&found, del)) {
609       if (iface_addr_Zap(iface->name, iface->addr + n, s)) {
610         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
611         bcopy(iface->addr + n + 1, iface->addr + n,
612               (iface->addrs - n - 1) * sizeof *iface->addr);
613         iface->addrs--;
614         res = 1;
615       }
616       break;
617     }
618   }
619 
620   close(s);
621 
622   return res;
623 }
624 
625 #define IFACE_ADDFLAGS 1
626 #define IFACE_DELFLAGS 2
627 
628 static int
iface_ChangeFlags(const char * ifname,int flags,int how)629 iface_ChangeFlags(const char *ifname, int flags, int how)
630 {
631   struct ifreq ifrq;
632   int s, new_flags;
633 
634   s = ID0socket(PF_INET, SOCK_DGRAM, 0);
635   if (s < 0) {
636     log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
637     return 0;
638   }
639 
640   memset(&ifrq, '\0', sizeof ifrq);
641   strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1);
642   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
643   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
644     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
645        strerror(errno));
646     close(s);
647     return 0;
648   }
649 #ifdef __FreeBSD__
650   new_flags = (ifrq.ifr_flags & 0xffff) | (ifrq.ifr_flagshigh << 16);
651 #else
652   new_flags = ifrq.ifr_flags & 0xffff;
653 #endif
654 
655   if (how == IFACE_ADDFLAGS)
656     new_flags |= flags;
657   else
658     new_flags &= ~flags;
659   ifrq.ifr_flags = new_flags & 0xffff;
660 #ifdef __FreeBSD__
661   ifrq.ifr_flagshigh = new_flags >> 16;
662 #endif
663 
664   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
665     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
666        strerror(errno));
667     close(s);
668     return 0;
669   }
670   close(s);
671 
672   return 1;	/* Success */
673 }
674 
675 int
iface_SetFlags(const char * ifname,int flags)676 iface_SetFlags(const char *ifname, int flags)
677 {
678   return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS);
679 }
680 
681 int
iface_ClearFlags(const char * ifname,int flags)682 iface_ClearFlags(const char *ifname, int flags)
683 {
684   return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS);
685 }
686 
687 void
iface_Free(struct iface * iface)688 iface_Free(struct iface *iface)
689 {
690     free(iface->name);
691     free(iface->descr);
692     free(iface->addr);
693     free(iface);
694 }
695 
696 void
iface_Destroy(struct iface * iface)697 iface_Destroy(struct iface *iface)
698 {
699   struct ifreq ifr;
700   int s;
701 
702   if (iface != NULL) {
703     if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
704       log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
705     } else {
706       strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
707       if (ID0ioctl(s, SIOCIFDESTROY, (caddr_t)&ifr) < 0)
708         log_Printf(LogWARN, "iface_Destroy: ioctl(SIOCIFDESTROY, %s): %s\n",
709                iface->name, strerror(errno));
710     }
711     iface_Free(iface);
712   }
713 }
714 
715 #define if_entry(x) { IFF_##x, #x }
716 
717 struct {
718   int flag;
719   const char *value;
720 } if_flags[] = {
721   if_entry(UP),
722   if_entry(BROADCAST),
723   if_entry(DEBUG),
724   if_entry(LOOPBACK),
725   if_entry(POINTOPOINT),
726   if_entry(RUNNING),
727   if_entry(NOARP),
728   if_entry(PROMISC),
729   if_entry(ALLMULTI),
730   if_entry(OACTIVE),
731   if_entry(SIMPLEX),
732   if_entry(LINK0),
733   if_entry(LINK1),
734   if_entry(LINK2),
735   if_entry(MULTICAST),
736   { 0, "???" }
737 };
738 
739 int
iface_Show(struct cmdargs const * arg)740 iface_Show(struct cmdargs const *arg)
741 {
742   struct ncpaddr ncpaddr;
743   struct iface *iface = arg->bundle->iface, *current;
744   unsigned f;
745   int flags;
746 #ifndef NOINET6
747   int scopeid, width;
748 #endif
749   struct in_addr mask;
750 
751   current = iface_Create(iface->name);
752   flags = iface->flags = current->flags;
753   iface_Free(current);
754 
755   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
756   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
757     if ((if_flags[f].flag & flags)) {
758       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
759                     if_flags[f].value);
760       flags &= ~if_flags[f].flag;
761     }
762 
763 #if 0
764   if (flags)
765     prompt_Printf(arg->prompt, "%s0x%x", flags == iface->flags ? "" : ",",
766                   flags);
767 #endif
768 
769   prompt_Printf(arg->prompt, "> mtu %lu has %d address%s:\n", iface->mtu,
770                 iface->addrs, iface->addrs == 1 ? "" : "es");
771 
772   for (f = 0; f < iface->addrs; f++) {
773     ncprange_getaddr(&iface->addr[f].ifa, &ncpaddr);
774     switch (ncprange_family(&iface->addr[f].ifa)) {
775     case AF_INET:
776       prompt_Printf(arg->prompt, "  inet %s --> ", ncpaddr_ntoa(&ncpaddr));
777       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
778         prompt_Printf(arg->prompt, "255.255.255.255");
779       else
780         prompt_Printf(arg->prompt, "%s", ncpaddr_ntoa(&iface->addr[f].peer));
781       ncprange_getip4mask(&iface->addr[f].ifa, &mask);
782       prompt_Printf(arg->prompt, " netmask 0x%08lx", (long)ntohl(mask.s_addr));
783       break;
784 
785 #ifndef NOINET6
786     case AF_INET6:
787       prompt_Printf(arg->prompt, "  inet6 %s", ncpaddr_ntoa(&ncpaddr));
788       if (ncpaddr_family(&iface->addr[f].peer) != AF_UNSPEC)
789         prompt_Printf(arg->prompt, " --> %s",
790                       ncpaddr_ntoa(&iface->addr[f].peer));
791       ncprange_getwidth(&iface->addr[f].ifa, &width);
792       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
793         prompt_Printf(arg->prompt, " prefixlen %d", width);
794       if ((scopeid = ncprange_scopeid(&iface->addr[f].ifa)) != -1)
795         prompt_Printf(arg->prompt, " scopeid 0x%x", (unsigned)scopeid);
796       break;
797 #endif
798     }
799     prompt_Printf(arg->prompt, "\n");
800   }
801 
802   return 0;
803 }
804 
805 void
iface_ParseHdr(struct ifa_msghdr * ifam,struct sockaddr * sa[RTAX_MAX])806 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
807 {
808   char *wp;
809   int rtax;
810 
811   wp = (char *)(ifam + 1);
812 
813   for (rtax = 0; rtax < RTAX_MAX; rtax++)
814     if (ifam->ifam_addrs & (1 << rtax)) {
815       sa[rtax] = (struct sockaddr *)wp;
816       wp += ROUNDUP(sa[rtax]->sa_len);
817     } else
818       sa[rtax] = NULL;
819 }
820