xref: /freebsd/usr.sbin/ppp/iface.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #ifdef __FreeBSD__
35 #include <net/if_var.h>
36 #endif
37 #include <net/route.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in_var.h>
40 #include <netinet/ip.h>
41 #ifndef NOINET6
42 #include <netinet6/nd6.h>
43 #endif
44 #include <sys/un.h>
45 
46 #include <errno.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <sys/ioctl.h>
52 #include <sys/sysctl.h>
53 #include <termios.h>
54 #include <unistd.h>
55 
56 #include "layer.h"
57 #include "defs.h"
58 #include "command.h"
59 #include "mbuf.h"
60 #include "log.h"
61 #include "id.h"
62 #include "timer.h"
63 #include "fsm.h"
64 #include "iplist.h"
65 #include "lqr.h"
66 #include "hdlc.h"
67 #include "throughput.h"
68 #include "slcompress.h"
69 #include "descriptor.h"
70 #include "ncpaddr.h"
71 #include "ipcp.h"
72 #include "filter.h"
73 #include "lcp.h"
74 #include "ccp.h"
75 #include "link.h"
76 #include "mp.h"
77 #ifndef NORADIUS
78 #include "radius.h"
79 #endif
80 #include "ipv6cp.h"
81 #include "ncp.h"
82 #include "bundle.h"
83 #include "prompt.h"
84 #include "iface.h"
85 
86 #define IN6MASK128	{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
87 			    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
88 static const struct in6_addr in6mask128 = IN6MASK128;
89 
90 
91 struct iface *
92 iface_Create(const char *name)
93 {
94   int mib[6], maxtries, err;
95   size_t needed, namelen;
96   char *buf, *ptr, *end;
97   struct if_msghdr *ifm;
98   struct ifa_msghdr *ifam;
99   struct sockaddr_dl *dl;
100   struct sockaddr *sa[RTAX_MAX];
101   struct iface *iface;
102   struct iface_addr *addr;
103 
104   mib[0] = CTL_NET;
105   mib[1] = PF_ROUTE;
106   mib[2] = 0;
107   mib[3] = 0;
108   mib[4] = NET_RT_IFLIST;
109   mib[5] = 0;
110 
111   maxtries = 20;
112   err = 0;
113   do {
114     if (maxtries-- == 0 || (err && err != ENOMEM)) {
115       fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(err));
116       return NULL;
117     }
118 
119     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
120       fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
121                 strerror(errno));
122       return NULL;
123     }
124 
125     if ((buf = (char *)malloc(needed)) == NULL) {
126       fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
127       return NULL;
128     }
129 
130     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
131       err = errno;
132       free(buf);
133       buf = NULL;
134     }
135   } while (buf == NULL);
136 
137   ptr = buf;
138   end = buf + needed;
139   iface = NULL;
140   namelen = strlen(name);
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 (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) {
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->index = ifm->ifm_index;
155       iface->flags = ifm->ifm_flags;
156       iface->mtu = 0;
157       iface->addrs = 0;
158       iface->addr = NULL;
159     }
160     ptr += ifm->ifm_msglen;				/* First ifa_msghdr */
161     for (; ptr < end; ptr += ifam->ifam_msglen) {
162       ifam = (struct ifa_msghdr *)ptr;			/* Next if address */
163 
164       if (ifam->ifam_type != RTM_NEWADDR)		/* finished this if */
165         break;
166 
167       if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
168         /* Found a configured interface ! */
169         iface_ParseHdr(ifam, sa);
170 
171         if (sa[RTAX_IFA] && (sa[RTAX_IFA]->sa_family == AF_INET
172 #ifndef NOINET6
173                              || sa[RTAX_IFA]->sa_family == AF_INET6
174 #endif
175                              )) {
176           /* Record the address */
177 
178           addr = (struct iface_addr *)
179             realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
180           if (addr == NULL)
181             break;
182           iface->addr = addr;
183 
184           addr += iface->addrs;
185           iface->addrs++;
186 
187           ncprange_setsa(&addr->ifa, sa[RTAX_IFA], sa[RTAX_NETMASK]);
188           if (sa[RTAX_BRD])
189             ncpaddr_setsa(&addr->peer, sa[RTAX_BRD]);
190           else
191             ncpaddr_init(&addr->peer);
192         }
193       }
194     }
195   }
196 
197   free(buf);
198 
199   return iface;
200 }
201 
202 static int
203 iface_addr_Zap(const char *name, struct iface_addr *addr, int s)
204 {
205   struct ifaliasreq ifra;
206 #ifndef NOINET6
207   struct in6_aliasreq ifra6;
208 #endif
209   struct sockaddr_in *me4, *msk4, *peer4;
210   struct sockaddr_storage ssme, sspeer, ssmsk;
211   int res;
212 
213   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
214   ncpaddr_getsa(&addr->peer, &sspeer);
215   res = 0;
216 
217   switch (ncprange_family(&addr->ifa)) {
218   case AF_INET:
219     memset(&ifra, '\0', sizeof ifra);
220     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
221 
222     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
223     memcpy(me4, &ssme, sizeof *me4);
224 
225     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
226     memcpy(msk4, &ssmsk, sizeof *msk4);
227 
228     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
229     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
230       peer4->sin_family = AF_INET;
231       peer4->sin_len = sizeof(*peer4);
232       peer4->sin_addr.s_addr = INADDR_NONE;
233     } else
234       memcpy(peer4, &sspeer, sizeof *peer4);
235 
236     res = ID0ioctl(s, SIOCDIFADDR, &ifra);
237     if (log_IsKept(LogDEBUG)) {
238       char buf[100];
239 
240       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
241       log_Printf(LogWARN, "%s: DIFADDR %s -> %s returns %d\n",
242                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
243     }
244     break;
245 
246 #ifndef NOINET6
247   case AF_INET6:
248     memset(&ifra6, '\0', sizeof ifra6);
249     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
250 
251     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
252     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
253     ifra6.ifra_prefixmask.sin6_family = AF_UNSPEC;
254     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
255       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
256     else
257       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
258     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
259     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
260 
261     res = ID0ioctl(s, SIOCDIFADDR_IN6, &ifra6);
262     break;
263 #endif
264   }
265 
266   if (res == -1) {
267     char dst[40];
268     const char *end =
269 #ifndef NOINET6
270       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
271 #endif
272       "";
273 
274     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
275       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s): %s\n",
276                  end, ncprange_ntoa(&addr->ifa), strerror(errno));
277     else {
278       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
279       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s -> %s): %s\n",
280                  end, ncprange_ntoa(&addr->ifa), dst, strerror(errno));
281     }
282   }
283 
284   return res != -1;
285 }
286 
287 static int
288 iface_addr_Add(const char *name, struct iface_addr *addr, int s)
289 {
290   struct ifaliasreq ifra;
291 #ifndef NOINET6
292   struct in6_aliasreq ifra6;
293 #endif
294   struct sockaddr_in *me4, *msk4, *peer4;
295   struct sockaddr_storage ssme, sspeer, ssmsk;
296   int res;
297 
298   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
299   ncpaddr_getsa(&addr->peer, &sspeer);
300   res = 0;
301 
302   switch (ncprange_family(&addr->ifa)) {
303   case AF_INET:
304     memset(&ifra, '\0', sizeof ifra);
305     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
306 
307     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
308     memcpy(me4, &ssme, sizeof *me4);
309 
310     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
311     memcpy(msk4, &ssmsk, sizeof *msk4);
312 
313     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
314     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
315       peer4->sin_family = AF_INET;
316       peer4->sin_len = sizeof(*peer4);
317       peer4->sin_addr.s_addr = INADDR_NONE;
318     } else
319       memcpy(peer4, &sspeer, sizeof *peer4);
320 
321     res = ID0ioctl(s, SIOCAIFADDR, &ifra);
322     if (log_IsKept(LogDEBUG)) {
323       char buf[100];
324 
325       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
326       log_Printf(LogWARN, "%s: AIFADDR %s -> %s returns %d\n",
327                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
328     }
329     break;
330 
331 #ifndef NOINET6
332   case AF_INET6:
333     memset(&ifra6, '\0', sizeof ifra6);
334     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
335 
336     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
337     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
338     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
339       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
340     else if (memcmp(&((struct sockaddr_in6 *)&ssmsk)->sin6_addr, &in6mask128,
341 		    sizeof in6mask128) == 0)
342       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
343     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
344     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
345 
346     res = ID0ioctl(s, SIOCAIFADDR_IN6, &ifra6);
347     break;
348 #endif
349   }
350 
351   if (res == -1) {
352     char dst[40];
353     const char *end =
354 #ifndef NOINET6
355       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
356 #endif
357       "";
358 
359     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
360       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s): %s\n",
361                  end, ncprange_ntoa(&addr->ifa), strerror(errno));
362     else {
363       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
364       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s -> %s): %s\n",
365                  end, ncprange_ntoa(&addr->ifa), dst, strerror(errno));
366     }
367   }
368 
369   return res != -1;
370 }
371 
372 
373 void
374 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
375 {
376   int addrs, af, inskip, in6skip, s4 = -1, s6 = -1, *s;
377   unsigned n;
378 
379   if (iface->addrs) {
380     inskip = in6skip = how == IFACE_CLEAR_ALL ? 0 : 1;
381     addrs = 0;
382 
383     for (n = 0; n < iface->addrs; n++) {
384       af = ncprange_family(&iface->addr[n].ifa);
385       if (family == 0 || family == af) {
386         if (!iface->addr[n].system && (how & IFACE_SYSTEM))
387           continue;
388         switch (af) {
389         case AF_INET:
390           if (inskip) {
391             inskip = 0;
392             continue;
393           }
394           s = &s4;
395           break;
396 
397 #ifndef NOINET6
398         case AF_INET6:
399           if (in6skip) {
400             in6skip = 0;
401             continue;
402           }
403           s = &s6;
404           break;
405 #endif
406         default:
407           continue;
408         }
409 
410         if (*s == -1 && (*s = ID0socket(af, SOCK_DGRAM, 0)) == -1)
411           log_Printf(LogERROR, "iface_Clear: socket(): %s\n", strerror(errno));
412         else if (iface_addr_Zap(iface->name, iface->addr + n, *s)) {
413           ncp_IfaceAddrDeleted(ncp, iface->addr + n);
414           bcopy(iface->addr + n + 1, iface->addr + n,
415                 (iface->addrs - n - 1) * sizeof *iface->addr);
416           iface->addrs--;
417           n--;
418         }
419       }
420     }
421 
422     /* Don't bother realloc()ing - we have little to gain */
423 
424     if (s4)
425       close(s4);
426     if (s6)
427       close(s6);
428   }
429 }
430 
431 int
432 iface_Add(struct iface *iface, struct ncp *ncp, const struct ncprange *ifa,
433           const struct ncpaddr *peer, int how)
434 {
435   int af, removed, s;
436   unsigned n;
437   struct ncpaddr ncplocal;
438   struct iface_addr *addr, newaddr;
439 
440   af = ncprange_family(ifa);
441   if ((s = ID0socket(af, SOCK_DGRAM, 0)) == -1) {
442     log_Printf(LogERROR, "iface_Add: socket(): %s\n", strerror(errno));
443     return 0;
444   }
445   ncprange_getaddr(ifa, &ncplocal);
446 
447   for (n = 0; n < iface->addrs; n++) {
448     if (ncprange_contains(&iface->addr[n].ifa, &ncplocal) ||
449         ncpaddr_equal(&iface->addr[n].peer, peer)) {
450       /* Replace this sockaddr */
451       if (!(how & IFACE_FORCE_ADD)) {
452         close(s);
453         return 0;	/* errno = EEXIST; */
454       }
455 
456       if (ncprange_equal(&iface->addr[n].ifa, ifa) &&
457           ncpaddr_equal(&iface->addr[n].peer, peer)) {
458         close(s);
459         return 1;	/* Already there */
460       }
461 
462       removed = iface_addr_Zap(iface->name, iface->addr + n, s);
463       if (removed)
464         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
465       ncprange_copy(&iface->addr[n].ifa, ifa);
466       ncpaddr_copy(&iface->addr[n].peer, peer);
467       if (!iface_addr_Add(iface->name, iface->addr + n, s)) {
468         if (removed) {
469           bcopy(iface->addr + n + 1, iface->addr + n,
470                 (iface->addrs - n - 1) * sizeof *iface->addr);
471           iface->addrs--;
472           n--;
473         }
474         close(s);
475         return 0;
476       }
477       close(s);
478       ncp_IfaceAddrAdded(ncp, iface->addr + n);
479       return 1;
480     }
481   }
482 
483   addr = (struct iface_addr *)realloc
484     (iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
485   if (addr == NULL) {
486     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
487     close(s);
488     return 0;
489   }
490   iface->addr = addr;
491 
492   ncprange_copy(&newaddr.ifa, ifa);
493   ncpaddr_copy(&newaddr.peer, peer);
494   newaddr.system = !!(how & IFACE_SYSTEM);
495   if (!iface_addr_Add(iface->name, &newaddr, s)) {
496     close(s);
497     return 0;
498   }
499 
500   if (how & IFACE_ADD_FIRST) {
501     /* Stuff it at the start of our list */
502     n = 0;
503     bcopy(iface->addr, iface->addr + 1, iface->addrs * sizeof *iface->addr);
504   } else
505     n = iface->addrs;
506 
507   iface->addrs++;
508   memcpy(iface->addr + n, &newaddr, sizeof(*iface->addr));
509 
510   close(s);
511   ncp_IfaceAddrAdded(ncp, iface->addr + n);
512 
513   return 1;
514 }
515 
516 int
517 iface_Delete(struct iface *iface, struct ncp *ncp, const struct ncpaddr *del)
518 {
519   struct ncpaddr found;
520   unsigned n;
521   int res, s;
522 
523   if ((s = ID0socket(ncpaddr_family(del), SOCK_DGRAM, 0)) == -1) {
524     log_Printf(LogERROR, "iface_Delete: socket(): %s\n", strerror(errno));
525     return 0;
526   }
527 
528   for (n = res = 0; n < iface->addrs; n++) {
529     ncprange_getaddr(&iface->addr[n].ifa, &found);
530     if (ncpaddr_equal(&found, del)) {
531       if (iface_addr_Zap(iface->name, iface->addr + n, s)) {
532         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
533         bcopy(iface->addr + n + 1, iface->addr + n,
534               (iface->addrs - n - 1) * sizeof *iface->addr);
535         iface->addrs--;
536         res = 1;
537       }
538       break;
539     }
540   }
541 
542   close(s);
543 
544   return res;
545 }
546 
547 #define IFACE_ADDFLAGS 1
548 #define IFACE_DELFLAGS 2
549 
550 static int
551 iface_ChangeFlags(const char *ifname, int flags, int how)
552 {
553   struct ifreq ifrq;
554   int s, new_flags;
555 
556   s = ID0socket(PF_INET, SOCK_DGRAM, 0);
557   if (s < 0) {
558     log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
559     return 0;
560   }
561 
562   memset(&ifrq, '\0', sizeof ifrq);
563   strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1);
564   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
565   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
566     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
567        strerror(errno));
568     close(s);
569     return 0;
570   }
571 #ifdef __FreeBSD__
572   new_flags = (ifrq.ifr_flags & 0xffff) | (ifrq.ifr_flagshigh << 16);
573 #else
574   new_flags = ifrq.ifr_flags & 0xffff;
575 #endif
576 
577   if (how == IFACE_ADDFLAGS)
578     new_flags |= flags;
579   else
580     new_flags &= ~flags;
581   ifrq.ifr_flags = new_flags & 0xffff;
582 #ifdef __FreeBSD__
583   ifrq.ifr_flagshigh = new_flags >> 16;
584 #endif
585 
586   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
587     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
588        strerror(errno));
589     close(s);
590     return 0;
591   }
592   close(s);
593 
594   return 1;	/* Success */
595 }
596 
597 int
598 iface_SetFlags(const char *ifname, int flags)
599 {
600   return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS);
601 }
602 
603 int
604 iface_ClearFlags(const char *ifname, int flags)
605 {
606   return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS);
607 }
608 
609 void
610 iface_Destroy(struct iface *iface)
611 {
612   /*
613    * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
614    * if that's what the user wants.  It's better to leave the interface
615    * allocated so that existing connections can continue to work.
616    */
617 
618   if (iface != NULL) {
619     free(iface->name);
620     free(iface->addr);
621     free(iface);
622   }
623 }
624 
625 #define if_entry(x) { IFF_##x, #x }
626 
627 struct {
628   int flag;
629   const char *value;
630 } if_flags[] = {
631   if_entry(UP),
632   if_entry(BROADCAST),
633   if_entry(DEBUG),
634   if_entry(LOOPBACK),
635   if_entry(POINTOPOINT),
636   if_entry(RUNNING),
637   if_entry(NOARP),
638   if_entry(PROMISC),
639   if_entry(ALLMULTI),
640   if_entry(OACTIVE),
641   if_entry(SIMPLEX),
642   if_entry(LINK0),
643   if_entry(LINK1),
644   if_entry(LINK2),
645   if_entry(MULTICAST),
646   { 0, "???" }
647 };
648 
649 int
650 iface_Show(struct cmdargs const *arg)
651 {
652   struct ncpaddr ncpaddr;
653   struct iface *iface = arg->bundle->iface, *current;
654   unsigned f;
655   int flags;
656 #ifndef NOINET6
657   int scopeid, width;
658 #endif
659   struct in_addr mask;
660 
661   current = iface_Create(iface->name);
662   flags = iface->flags = current->flags;
663   iface_Destroy(current);
664 
665   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
666   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
667     if ((if_flags[f].flag & flags)) {
668       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
669                     if_flags[f].value);
670       flags &= ~if_flags[f].flag;
671     }
672 
673 #if 0
674   if (flags)
675     prompt_Printf(arg->prompt, "%s0x%x", flags == iface->flags ? "" : ",",
676                   flags);
677 #endif
678 
679   prompt_Printf(arg->prompt, "> mtu %lu has %d address%s:\n", iface->mtu,
680                 iface->addrs, iface->addrs == 1 ? "" : "es");
681 
682   for (f = 0; f < iface->addrs; f++) {
683     ncprange_getaddr(&iface->addr[f].ifa, &ncpaddr);
684     switch (ncprange_family(&iface->addr[f].ifa)) {
685     case AF_INET:
686       prompt_Printf(arg->prompt, "  inet %s --> ", ncpaddr_ntoa(&ncpaddr));
687       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
688         prompt_Printf(arg->prompt, "255.255.255.255");
689       else
690         prompt_Printf(arg->prompt, "%s", ncpaddr_ntoa(&iface->addr[f].peer));
691       ncprange_getip4mask(&iface->addr[f].ifa, &mask);
692       prompt_Printf(arg->prompt, " netmask 0x%08lx", (long)ntohl(mask.s_addr));
693       break;
694 
695 #ifndef NOINET6
696     case AF_INET6:
697       prompt_Printf(arg->prompt, "  inet6 %s", ncpaddr_ntoa(&ncpaddr));
698       if (ncpaddr_family(&iface->addr[f].peer) != AF_UNSPEC)
699         prompt_Printf(arg->prompt, " --> %s",
700                       ncpaddr_ntoa(&iface->addr[f].peer));
701       ncprange_getwidth(&iface->addr[f].ifa, &width);
702       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
703         prompt_Printf(arg->prompt, " prefixlen %d", width);
704       if ((scopeid = ncprange_scopeid(&iface->addr[f].ifa)) != -1)
705         prompt_Printf(arg->prompt, " scopeid 0x%x", (unsigned)scopeid);
706       break;
707 #endif
708     }
709     prompt_Printf(arg->prompt, "\n");
710   }
711 
712   return 0;
713 }
714 
715 void
716 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
717 {
718   char *wp;
719   int rtax;
720 
721   wp = (char *)(ifam + 1);
722 
723   for (rtax = 0; rtax < RTAX_MAX; rtax++)
724     if (ifam->ifam_addrs & (1 << rtax)) {
725       sa[rtax] = (struct sockaddr *)wp;
726       wp += ROUNDUP(sa[rtax]->sa_len);
727     } else
728       sa[rtax] = NULL;
729 }
730