]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/iface.c
MFC r326276:
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / iface.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <net/if.h>
35 #include <net/if_dl.h>
36 #include <net/route.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/in_var.h>
39 #include <netinet/ip.h>
40 #ifndef NOINET6
41 #include <netinet6/nd6.h>
42 #endif
43 #include <sys/un.h>
44
45 #include <errno.h>
46 #include <string.h>
47 #include <stdarg.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 #define IN6MASK128      {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
86                             0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
87 static const struct in6_addr in6mask128 = IN6MASK128;
88
89
90 struct iface *
91 iface_Create(const char *name)
92 {
93   int mib[6], maxtries, err;
94   size_t needed, namelen;
95   char *buf, *ptr, *end;
96   struct if_msghdr *ifm;
97   struct ifa_msghdr *ifam;
98   struct sockaddr_dl *dl;
99   struct sockaddr *sa[RTAX_MAX];
100   struct iface *iface;
101   struct iface_addr *addr;
102
103   mib[0] = CTL_NET;
104   mib[1] = PF_ROUTE;
105   mib[2] = 0;
106   mib[3] = 0;
107   mib[4] = NET_RT_IFLIST;
108   mib[5] = 0;
109
110   maxtries = 20;
111   err = 0;
112   do {
113     if (maxtries-- == 0 || (err && err != ENOMEM)) {
114       fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(err));
115       return NULL;
116     }
117
118     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
119       fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
120                 strerror(errno));
121       return NULL;
122     }
123
124     if ((buf = (char *)malloc(needed)) == NULL) {
125       fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
126       return NULL;
127     }
128
129     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
130       err = errno;
131       free(buf);
132       buf = NULL;
133     }
134   } while (buf == NULL);
135
136   ptr = buf;
137   end = buf + needed;
138   iface = NULL;
139   namelen = strlen(name);
140
141   while (ptr < end && iface == NULL) {
142     ifm = (struct if_msghdr *)ptr;                      /* On if_msghdr */
143     if (ifm->ifm_type != RTM_IFINFO)
144       break;
145     dl = (struct sockaddr_dl *)(ifm + 1);               /* Single _dl at end */
146     if (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) {
147       iface = (struct iface *)malloc(sizeof *iface);
148       if (iface == NULL) {
149         fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
150         return NULL;
151       }
152       iface->name = strdup(name);
153       iface->descr = NULL;
154       iface->index = ifm->ifm_index;
155       iface->flags = ifm->ifm_flags;
156       iface->mtu = 0;
157       iface->addrs = 0;
158       iface->addr = NULL;
159     }
160     ptr += ifm->ifm_msglen;                             /* First ifa_msghdr */
161     for (; ptr < end; ptr += ifam->ifam_msglen) {
162       ifam = (struct ifa_msghdr *)ptr;                  /* Next if address */
163
164       if (ifam->ifam_type != RTM_NEWADDR)               /* finished this if */
165         break;
166
167       if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
168         /* Found a configured interface ! */
169         iface_ParseHdr(ifam, sa);
170
171         if (sa[RTAX_IFA] && (sa[RTAX_IFA]->sa_family == AF_INET
172 #ifndef NOINET6
173                              || sa[RTAX_IFA]->sa_family == AF_INET6
174 #endif
175                              )) {
176           /* Record the address */
177
178           addr = (struct iface_addr *)
179             realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
180           if (addr == NULL)
181             break;
182           iface->addr = addr;
183
184           addr += iface->addrs;
185           iface->addrs++;
186
187           ncprange_setsa(&addr->ifa, sa[RTAX_IFA], sa[RTAX_NETMASK]);
188           if (sa[RTAX_BRD])
189             ncpaddr_setsa(&addr->peer, sa[RTAX_BRD]);
190           else
191             ncpaddr_init(&addr->peer);
192         }
193       }
194     }
195   }
196
197   free(buf);
198
199   return iface;
200 }
201
202 static int
203 iface_addr_Zap(const char *name, struct iface_addr *addr, int s)
204 {
205   struct ifaliasreq ifra;
206 #ifndef NOINET6
207   struct in6_aliasreq ifra6;
208 #endif
209   struct sockaddr_in *me4, *msk4, *peer4;
210   struct sockaddr_storage ssme, sspeer, ssmsk;
211   int res;
212
213   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
214   ncpaddr_getsa(&addr->peer, &sspeer);
215   res = 0;
216
217   switch (ncprange_family(&addr->ifa)) {
218   case AF_INET:
219     memset(&ifra, '\0', sizeof ifra);
220     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
221
222     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
223     memcpy(me4, &ssme, sizeof *me4);
224
225     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
226     memcpy(msk4, &ssmsk, sizeof *msk4);
227
228     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
229     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
230       peer4->sin_family = AF_INET;
231       peer4->sin_len = sizeof(*peer4);
232       peer4->sin_addr.s_addr = INADDR_NONE;
233     } else
234       memcpy(peer4, &sspeer, sizeof *peer4);
235
236     res = ID0ioctl(s, SIOCDIFADDR, &ifra);
237     if (log_IsKept(LogDEBUG)) {
238       char buf[100];
239
240       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
241       log_Printf(LogWARN, "%s: DIFADDR %s -> %s returns %d\n",
242                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
243     }
244     break;
245
246 #ifndef NOINET6
247   case AF_INET6:
248     memset(&ifra6, '\0', sizeof ifra6);
249     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
250
251     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
252     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
253     ifra6.ifra_prefixmask.sin6_family = AF_UNSPEC;
254     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
255       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
256     else
257       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
258     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
259     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
260
261     res = ID0ioctl(s, SIOCDIFADDR_IN6, &ifra6);
262     break;
263 #endif
264   }
265
266   if (res == -1) {
267     char dst[40];
268     const char *end =
269 #ifndef NOINET6
270       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
271 #endif
272       "";
273
274     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
275       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s): %s\n",
276                  end, ncprange_ntoa(&addr->ifa), strerror(errno));
277     else {
278       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
279       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s -> %s): %s\n",
280                  end, ncprange_ntoa(&addr->ifa), dst, strerror(errno));
281     }
282   }
283
284   return res != -1;
285 }
286
287 static int
288 iface_addr_Add(const char *name, struct iface_addr *addr, int s)
289 {
290   struct ifaliasreq ifra;
291 #ifndef NOINET6
292   struct in6_aliasreq ifra6;
293 #endif
294   struct sockaddr_in *me4, *msk4, *peer4;
295   struct sockaddr_storage ssme, sspeer, ssmsk;
296   int res;
297
298   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
299   ncpaddr_getsa(&addr->peer, &sspeer);
300   res = 0;
301
302   switch (ncprange_family(&addr->ifa)) {
303   case AF_INET:
304     memset(&ifra, '\0', sizeof ifra);
305     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
306
307     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
308     memcpy(me4, &ssme, sizeof *me4);
309
310     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
311     memcpy(msk4, &ssmsk, sizeof *msk4);
312
313     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
314     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
315       peer4->sin_family = AF_INET;
316       peer4->sin_len = sizeof(*peer4);
317       peer4->sin_addr.s_addr = INADDR_NONE;
318     } else
319       memcpy(peer4, &sspeer, sizeof *peer4);
320
321     res = ID0ioctl(s, SIOCAIFADDR, &ifra);
322     if (log_IsKept(LogDEBUG)) {
323       char buf[100];
324
325       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
326       log_Printf(LogWARN, "%s: AIFADDR %s -> %s returns %d\n",
327                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
328     }
329     break;
330
331 #ifndef NOINET6
332   case AF_INET6:
333     memset(&ifra6, '\0', sizeof ifra6);
334     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
335
336     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
337     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
338     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
339       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
340     else if (memcmp(&((struct sockaddr_in6 *)&ssmsk)->sin6_addr, &in6mask128,
341                     sizeof in6mask128) == 0)
342       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
343     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
344     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
345
346     res = ID0ioctl(s, SIOCAIFADDR_IN6, &ifra6);
347     break;
348 #endif
349   }
350
351   if (res == -1) {
352     char dst[40];
353     const char *end =
354 #ifndef NOINET6
355       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
356 #endif
357       "";
358
359     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
360       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s): %s\n",
361                  end, ncprange_ntoa(&addr->ifa), strerror(errno));
362     else {
363       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
364       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s -> %s): %s\n",
365                  end, ncprange_ntoa(&addr->ifa), dst, strerror(errno));
366     }
367   }
368
369   return res != -1;
370 }
371
372 int
373 iface_Name(struct iface *iface, const char *name)
374 {
375   struct ifreq ifr;
376   int s;
377   char *newname;
378
379   if ((newname = strdup(name)) == NULL) {
380     log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
381     return 0;
382   }
383
384   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
385     log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
386     free(newname);
387     return 0;
388   }
389
390   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
391   ifr.ifr_data = newname;
392   if (ID0ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
393     log_Printf(LogWARN, "iface name: ioctl(SIOCSIFNAME, %s -> %s): %s\n",
394                name, newname, strerror(errno));
395     free(newname);
396     return 0;
397   }
398
399   free(iface->name);
400   iface->name = newname;
401
402   return 1;
403 }
404
405 int
406 iface_Descr(struct cmdargs const *arg)
407 {
408   struct ifreq ifr;
409   struct iface *iface;
410   size_t sz, len;
411   int s, n, ifdescr_maxlen;
412   char *descr;
413
414   sz = sizeof(int);
415   if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
416     log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
417     return 1;
418   }
419
420   if (ifdescr_maxlen < 1) {
421     log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
422     return 1;
423   }
424
425   sz = sizeof(char) * ifdescr_maxlen;
426   if ((descr = malloc(sz)) == NULL) {
427     log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
428     return 1;
429   }
430
431   *descr = '\0';
432   n = arg->argn;
433   while (n < arg->argc) {
434     if (n > arg->argn && (len = strlcat(descr, " ", sz)) >= sz)
435       break;
436     if ((len = strlcat(descr, arg->argv[n], sz)) >= sz)
437       break;
438     ++n;
439   }
440   if (len >= sz) {
441     log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n",
442                ifdescr_maxlen-1);
443     free(descr);
444     return 1;
445   }
446
447   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
448     log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
449     free(descr);
450     return 1;
451   }
452
453   iface = arg->bundle->iface;
454   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
455   ifr.ifr_buffer.length = strlen(descr) + 1;
456   ifr.ifr_buffer.buffer = descr;
457   if (ID0ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
458     log_Printf(LogWARN, "iface descr: ioctl(SIOCSIFDESCR, %s): %s\n",
459                descr, strerror(errno));
460     free(descr);
461     return 1;
462   }
463
464   free(iface->descr);
465   iface->descr = descr;
466
467   return 0;
468 }
469
470 void
471 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
472 {
473   int af, inskip, in6skip, s4 = -1, s6 = -1, *s;
474   unsigned n;
475
476   if (iface->addrs) {
477     inskip = in6skip = how == IFACE_CLEAR_ALL ? 0 : 1;
478
479     for (n = 0; n < iface->addrs; n++) {
480       af = ncprange_family(&iface->addr[n].ifa);
481       if (family == 0 || family == af) {
482         if (!iface->addr[n].system && (how & IFACE_SYSTEM))
483           continue;
484         switch (af) {
485         case AF_INET:
486           if (inskip) {
487             inskip = 0;
488             continue;
489           }
490           s = &s4;
491           break;
492
493 #ifndef NOINET6
494         case AF_INET6:
495           if (in6skip) {
496             in6skip = 0;
497             continue;
498           }
499           s = &s6;
500           break;
501 #endif
502         default:
503           continue;
504         }
505
506         if (*s == -1 && (*s = ID0socket(af, SOCK_DGRAM, 0)) == -1)
507           log_Printf(LogERROR, "iface_Clear: socket(): %s\n", strerror(errno));
508         else if (iface_addr_Zap(iface->name, iface->addr + n, *s)) {
509           ncp_IfaceAddrDeleted(ncp, iface->addr + n);
510           bcopy(iface->addr + n + 1, iface->addr + n,
511                 (iface->addrs - n - 1) * sizeof *iface->addr);
512           iface->addrs--;
513           n--;
514         }
515       }
516     }
517
518     /* Don't bother realloc()ing - we have little to gain */
519
520     if (s4)
521       close(s4);
522     if (s6)
523       close(s6);
524   }
525 }
526
527 int
528 iface_Add(struct iface *iface, struct ncp *ncp, const struct ncprange *ifa,
529           const struct ncpaddr *peer, int how)
530 {
531   int af, removed, s;
532   unsigned n;
533   struct ncpaddr ncplocal;
534   struct iface_addr *addr, newaddr;
535
536   af = ncprange_family(ifa);
537   if ((s = ID0socket(af, SOCK_DGRAM, 0)) == -1) {
538     log_Printf(LogERROR, "iface_Add: socket(): %s\n", strerror(errno));
539     return 0;
540   }
541   ncprange_getaddr(ifa, &ncplocal);
542
543   for (n = 0; n < iface->addrs; n++) {
544     if (ncprange_contains(&iface->addr[n].ifa, &ncplocal) ||
545         ncpaddr_equal(&iface->addr[n].peer, peer)) {
546       /* Replace this sockaddr */
547       if (!(how & IFACE_FORCE_ADD)) {
548         close(s);
549         return 0;       /* errno = EEXIST; */
550       }
551
552       if (ncprange_equal(&iface->addr[n].ifa, ifa) &&
553           ncpaddr_equal(&iface->addr[n].peer, peer)) {
554         close(s);
555         ncp_IfaceAddrAdded(ncp, iface->addr + n);
556         return 1;       /* Already there */
557       }
558
559       removed = iface_addr_Zap(iface->name, iface->addr + n, s);
560       if (removed)
561         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
562       ncprange_copy(&iface->addr[n].ifa, ifa);
563       ncpaddr_copy(&iface->addr[n].peer, peer);
564       if (!iface_addr_Add(iface->name, iface->addr + n, s)) {
565         if (removed) {
566           bcopy(iface->addr + n + 1, iface->addr + n,
567                 (iface->addrs - n - 1) * sizeof *iface->addr);
568           iface->addrs--;
569           n--;
570         }
571         close(s);
572         return 0;
573       }
574       close(s);
575       ncp_IfaceAddrAdded(ncp, iface->addr + n);
576       return 1;
577     }
578   }
579
580   addr = (struct iface_addr *)realloc
581     (iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
582   if (addr == NULL) {
583     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
584     close(s);
585     return 0;
586   }
587   iface->addr = addr;
588
589   ncprange_copy(&newaddr.ifa, ifa);
590   ncpaddr_copy(&newaddr.peer, peer);
591   newaddr.system = !!(how & IFACE_SYSTEM);
592   if (!iface_addr_Add(iface->name, &newaddr, s)) {
593     close(s);
594     return 0;
595   }
596
597   if (how & IFACE_ADD_FIRST) {
598     /* Stuff it at the start of our list */
599     n = 0;
600     bcopy(iface->addr, iface->addr + 1, iface->addrs * sizeof *iface->addr);
601   } else
602     n = iface->addrs;
603
604   iface->addrs++;
605   memcpy(iface->addr + n, &newaddr, sizeof(*iface->addr));
606
607   close(s);
608   ncp_IfaceAddrAdded(ncp, iface->addr + n);
609
610   return 1;
611 }
612
613 int
614 iface_Delete(struct iface *iface, struct ncp *ncp, const struct ncpaddr *del)
615 {
616   struct ncpaddr found;
617   unsigned n;
618   int res, s;
619
620   if ((s = ID0socket(ncpaddr_family(del), SOCK_DGRAM, 0)) == -1) {
621     log_Printf(LogERROR, "iface_Delete: socket(): %s\n", strerror(errno));
622     return 0;
623   }
624
625   for (n = res = 0; n < iface->addrs; n++) {
626     ncprange_getaddr(&iface->addr[n].ifa, &found);
627     if (ncpaddr_equal(&found, del)) {
628       if (iface_addr_Zap(iface->name, iface->addr + n, s)) {
629         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
630         bcopy(iface->addr + n + 1, iface->addr + n,
631               (iface->addrs - n - 1) * sizeof *iface->addr);
632         iface->addrs--;
633         res = 1;
634       }
635       break;
636     }
637   }
638
639   close(s);
640
641   return res;
642 }
643
644 #define IFACE_ADDFLAGS 1
645 #define IFACE_DELFLAGS 2
646
647 static int
648 iface_ChangeFlags(const char *ifname, int flags, int how)
649 {
650   struct ifreq ifrq;
651   int s, new_flags;
652
653   s = ID0socket(PF_INET, SOCK_DGRAM, 0);
654   if (s < 0) {
655     log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
656     return 0;
657   }
658
659   memset(&ifrq, '\0', sizeof ifrq);
660   strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1);
661   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
662   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
663     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
664        strerror(errno));
665     close(s);
666     return 0;
667   }
668 #ifdef __FreeBSD__
669   new_flags = (ifrq.ifr_flags & 0xffff) | (ifrq.ifr_flagshigh << 16);
670 #else
671   new_flags = ifrq.ifr_flags & 0xffff;
672 #endif
673
674   if (how == IFACE_ADDFLAGS)
675     new_flags |= flags;
676   else
677     new_flags &= ~flags;
678   ifrq.ifr_flags = new_flags & 0xffff;
679 #ifdef __FreeBSD__
680   ifrq.ifr_flagshigh = new_flags >> 16;
681 #endif
682
683   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
684     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
685        strerror(errno));
686     close(s);
687     return 0;
688   }
689   close(s);
690
691   return 1;     /* Success */
692 }
693
694 int
695 iface_SetFlags(const char *ifname, int flags)
696 {
697   return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS);
698 }
699
700 int
701 iface_ClearFlags(const char *ifname, int flags)
702 {
703   return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS);
704 }
705
706 void
707 iface_Free(struct iface *iface)
708 {
709     free(iface->name);
710     free(iface->descr);
711     free(iface->addr);
712     free(iface);
713 }
714
715 void
716 iface_Destroy(struct iface *iface)
717 {
718   struct ifreq ifr;
719   int s;
720
721   if (iface != NULL) {
722     if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
723       log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
724     } else {
725       strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
726       if (ID0ioctl(s, SIOCIFDESTROY, (caddr_t)&ifr) < 0)
727         log_Printf(LogWARN, "iface_Destroy: ioctl(SIOCIFDESTROY, %s): %s\n",
728                iface->name, strerror(errno));
729     }
730     iface_Free(iface);
731   }
732 }
733
734 #define if_entry(x) { IFF_##x, #x }
735
736 struct {
737   int flag;
738   const char *value;
739 } if_flags[] = {
740   if_entry(UP),
741   if_entry(BROADCAST),
742   if_entry(DEBUG),
743   if_entry(LOOPBACK),
744   if_entry(POINTOPOINT),
745   if_entry(RUNNING),
746   if_entry(NOARP),
747   if_entry(PROMISC),
748   if_entry(ALLMULTI),
749   if_entry(OACTIVE),
750   if_entry(SIMPLEX),
751   if_entry(LINK0),
752   if_entry(LINK1),
753   if_entry(LINK2),
754   if_entry(MULTICAST),
755   { 0, "???" }
756 };
757
758 int
759 iface_Show(struct cmdargs const *arg)
760 {
761   struct ncpaddr ncpaddr;
762   struct iface *iface = arg->bundle->iface, *current;
763   unsigned f;
764   int flags;
765 #ifndef NOINET6
766   int scopeid, width;
767 #endif
768   struct in_addr mask;
769
770   current = iface_Create(iface->name);
771   flags = iface->flags = current->flags;
772   iface_Free(current);
773
774   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
775   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
776     if ((if_flags[f].flag & flags)) {
777       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
778                     if_flags[f].value);
779       flags &= ~if_flags[f].flag;
780     }
781
782 #if 0
783   if (flags)
784     prompt_Printf(arg->prompt, "%s0x%x", flags == iface->flags ? "" : ",",
785                   flags);
786 #endif
787
788   prompt_Printf(arg->prompt, "> mtu %lu has %d address%s:\n", iface->mtu,
789                 iface->addrs, iface->addrs == 1 ? "" : "es");
790
791   for (f = 0; f < iface->addrs; f++) {
792     ncprange_getaddr(&iface->addr[f].ifa, &ncpaddr);
793     switch (ncprange_family(&iface->addr[f].ifa)) {
794     case AF_INET:
795       prompt_Printf(arg->prompt, "  inet %s --> ", ncpaddr_ntoa(&ncpaddr));
796       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
797         prompt_Printf(arg->prompt, "255.255.255.255");
798       else
799         prompt_Printf(arg->prompt, "%s", ncpaddr_ntoa(&iface->addr[f].peer));
800       ncprange_getip4mask(&iface->addr[f].ifa, &mask);
801       prompt_Printf(arg->prompt, " netmask 0x%08lx", (long)ntohl(mask.s_addr));
802       break;
803
804 #ifndef NOINET6
805     case AF_INET6:
806       prompt_Printf(arg->prompt, "  inet6 %s", ncpaddr_ntoa(&ncpaddr));
807       if (ncpaddr_family(&iface->addr[f].peer) != AF_UNSPEC)
808         prompt_Printf(arg->prompt, " --> %s",
809                       ncpaddr_ntoa(&iface->addr[f].peer));
810       ncprange_getwidth(&iface->addr[f].ifa, &width);
811       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
812         prompt_Printf(arg->prompt, " prefixlen %d", width);
813       if ((scopeid = ncprange_scopeid(&iface->addr[f].ifa)) != -1)
814         prompt_Printf(arg->prompt, " scopeid 0x%x", (unsigned)scopeid);
815       break;
816 #endif
817     }
818     prompt_Printf(arg->prompt, "\n");
819   }
820
821   return 0;
822 }
823
824 void
825 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
826 {
827   char *wp;
828   int rtax;
829
830   wp = (char *)(ifam + 1);
831
832   for (rtax = 0; rtax < RTAX_MAX; rtax++)
833     if (ifam->ifam_addrs & (1 << rtax)) {
834       sa[rtax] = (struct sockaddr *)wp;
835       wp += ROUNDUP(sa[rtax]->sa_len);
836     } else
837       sa[rtax] = NULL;
838 }