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