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