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