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