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