xref: /freebsd/usr.sbin/ppp/iface.c (revision 550cb4ab85c7e514629c8bacbbb07085b81d916b)
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 	free(buf);
151         return NULL;
152       }
153       iface->name = strdup(name);
154       iface->descr = NULL;
155       iface->index = ifm->ifm_index;
156       iface->flags = ifm->ifm_flags;
157       iface->mtu = 0;
158       iface->addrs = 0;
159       iface->addr = NULL;
160     }
161     ptr += ifm->ifm_msglen;				/* First ifa_msghdr */
162     for (; ptr < end; ptr += ifam->ifam_msglen) {
163       ifam = (struct ifa_msghdr *)ptr;			/* Next if address */
164 
165       if (ifam->ifam_type != RTM_NEWADDR)		/* finished this if */
166         break;
167 
168       if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
169         /* Found a configured interface ! */
170         iface_ParseHdr(ifam, sa);
171 
172         if (sa[RTAX_IFA] && (sa[RTAX_IFA]->sa_family == AF_INET
173 #ifndef NOINET6
174                              || sa[RTAX_IFA]->sa_family == AF_INET6
175 #endif
176                              )) {
177           /* Record the address */
178 
179           addr = (struct iface_addr *)
180             realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
181           if (addr == NULL)
182             break;
183           iface->addr = addr;
184 
185           addr += iface->addrs;
186           iface->addrs++;
187 
188           ncprange_setsa(&addr->ifa, sa[RTAX_IFA], sa[RTAX_NETMASK]);
189           if (sa[RTAX_BRD])
190             ncpaddr_setsa(&addr->peer, sa[RTAX_BRD]);
191           else
192             ncpaddr_init(&addr->peer);
193         }
194       }
195     }
196   }
197 
198   free(buf);
199 
200   return iface;
201 }
202 
203 static int
204 iface_addr_Zap(const char *name, struct iface_addr *addr, int s)
205 {
206   struct ifaliasreq ifra;
207 #ifndef NOINET6
208   struct in6_aliasreq ifra6;
209 #endif
210   struct sockaddr_in *me4, *msk4, *peer4;
211   struct sockaddr_storage ssme, sspeer, ssmsk;
212   int res, saved_errno;
213 
214   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
215   ncpaddr_getsa(&addr->peer, &sspeer);
216   res = 0;
217 
218   switch (ncprange_family(&addr->ifa)) {
219   case AF_INET:
220     memset(&ifra, '\0', sizeof ifra);
221     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
222 
223     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
224     memcpy(me4, &ssme, sizeof *me4);
225 
226     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
227     memcpy(msk4, &ssmsk, sizeof *msk4);
228 
229     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
230     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
231       peer4->sin_family = AF_INET;
232       peer4->sin_len = sizeof(*peer4);
233       peer4->sin_addr.s_addr = INADDR_NONE;
234     } else
235       memcpy(peer4, &sspeer, sizeof *peer4);
236 
237     res = ID0ioctl(s, SIOCDIFADDR, &ifra);
238     saved_errno = errno;
239     if (log_IsKept(LogDEBUG)) {
240       char buf[NCP_ASCIIBUFFERSIZE];
241 
242       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
243       log_Printf(LogWARN, "%s: DIFADDR %s -> %s returns %d\n",
244                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
245     }
246     break;
247 
248 #ifndef NOINET6
249   case AF_INET6:
250     memset(&ifra6, '\0', sizeof ifra6);
251     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
252 
253     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
254     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
255     ifra6.ifra_prefixmask.sin6_family = AF_UNSPEC;
256     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
257       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
258     else
259       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
260     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
261     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
262 
263     res = ID0ioctl(s, SIOCDIFADDR_IN6, &ifra6);
264     saved_errno = errno;
265     break;
266 #endif
267   }
268 
269   if (res == -1) {
270     char dst[NCP_ASCIIBUFFERSIZE];
271     const char *end =
272 #ifndef NOINET6
273       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
274 #endif
275       "";
276 
277     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
278       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s): %s\n",
279                  end, ncprange_ntoa(&addr->ifa), strerror(saved_errno));
280     else {
281       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
282       log_Printf(LogWARN, "iface rm: ioctl(SIOCDIFADDR%s, %s -> %s): %s\n",
283                  end, ncprange_ntoa(&addr->ifa), dst, strerror(saved_errno));
284     }
285   }
286 
287   return res != -1;
288 }
289 
290 static int
291 iface_addr_Add(const char *name, struct iface_addr *addr, int s)
292 {
293   struct ifaliasreq ifra;
294 #ifndef NOINET6
295   struct in6_aliasreq ifra6;
296 #endif
297   struct sockaddr_in *me4, *msk4, *peer4;
298   struct sockaddr_storage ssme, sspeer, ssmsk;
299   int res, saved_errno;
300 
301   ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
302   ncpaddr_getsa(&addr->peer, &sspeer);
303   res = 0;
304 
305   switch (ncprange_family(&addr->ifa)) {
306   case AF_INET:
307     memset(&ifra, '\0', sizeof ifra);
308     strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
309 
310     me4 = (struct sockaddr_in *)&ifra.ifra_addr;
311     memcpy(me4, &ssme, sizeof *me4);
312 
313     msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
314     memcpy(msk4, &ssmsk, sizeof *msk4);
315 
316     peer4 = (struct sockaddr_in *)&ifra.ifra_broadaddr;
317     if (ncpaddr_family(&addr->peer) == AF_UNSPEC) {
318       peer4->sin_family = AF_INET;
319       peer4->sin_len = sizeof(*peer4);
320       peer4->sin_addr.s_addr = INADDR_NONE;
321     } else
322       memcpy(peer4, &sspeer, sizeof *peer4);
323 
324     res = ID0ioctl(s, SIOCAIFADDR, &ifra);
325     saved_errno = errno;
326     if (log_IsKept(LogDEBUG)) {
327       char buf[NCP_ASCIIBUFFERSIZE];
328 
329       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
330       log_Printf(LogWARN, "%s: AIFADDR %s -> %s returns %d\n",
331                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
332     }
333     break;
334 
335 #ifndef NOINET6
336   case AF_INET6:
337     memset(&ifra6, '\0', sizeof ifra6);
338     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
339 
340     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
341     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
342     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
343       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
344     else if (memcmp(&((struct sockaddr_in6 *)&ssmsk)->sin6_addr, &in6mask128,
345 		    sizeof in6mask128) == 0)
346       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
347     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
348     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
349 
350     res = ID0ioctl(s, SIOCAIFADDR_IN6, &ifra6);
351     saved_errno = errno;
352     break;
353 #endif
354   }
355 
356   if (res == -1) {
357     char dst[NCP_ASCIIBUFFERSIZE];
358     const char *end =
359 #ifndef NOINET6
360       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
361 #endif
362       "";
363 
364     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
365       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s): %s\n",
366                  end, ncprange_ntoa(&addr->ifa), strerror(saved_errno));
367     else {
368       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
369       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s -> %s): %s\n",
370                  end, ncprange_ntoa(&addr->ifa), dst, strerror(saved_errno));
371     }
372   }
373 
374   return res != -1;
375 }
376 
377 int
378 iface_Name(struct iface *iface, const char *name)
379 {
380   struct ifreq ifr;
381   int s;
382   char *newname;
383 
384   if ((newname = strdup(name)) == NULL) {
385     log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
386     return 0;
387   }
388 
389   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
390     log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
391     free(newname);
392     return 0;
393   }
394 
395   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
396   ifr.ifr_data = newname;
397   if (ID0ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
398     log_Printf(LogWARN, "iface name: ioctl(SIOCSIFNAME, %s -> %s): %s\n",
399                name, newname, strerror(errno));
400     free(newname);
401     return 0;
402   }
403 
404   free(iface->name);
405   iface->name = newname;
406 
407   return 1;
408 }
409 
410 int
411 iface_Descr(struct cmdargs const *arg)
412 {
413   struct ifreq ifr;
414   struct iface *iface;
415   size_t sz, len;
416   int s, n, ifdescr_maxlen;
417   char *descr;
418 
419   sz = sizeof(int);
420   if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
421     log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
422     return 1;
423   }
424 
425   if (ifdescr_maxlen < 1) {
426     log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
427     return 1;
428   }
429 
430   sz = sizeof(char) * ifdescr_maxlen;
431   if ((descr = malloc(sz)) == NULL) {
432     log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
433     return 1;
434   }
435 
436   *descr = '\0';
437   n = arg->argn;
438   while (n < arg->argc) {
439     if (n > arg->argn && (len = strlcat(descr, " ", sz)) >= sz)
440       break;
441     if ((len = strlcat(descr, arg->argv[n], sz)) >= sz)
442       break;
443     ++n;
444   }
445   if (len >= sz) {
446     log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n",
447                ifdescr_maxlen-1);
448     free(descr);
449     return 1;
450   }
451 
452   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
453     log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
454     free(descr);
455     return 1;
456   }
457 
458   iface = arg->bundle->iface;
459   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
460   ifr.ifr_buffer.length = strlen(descr) + 1;
461   ifr.ifr_buffer.buffer = descr;
462   if (ID0ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
463     log_Printf(LogWARN, "iface descr: ioctl(SIOCSIFDESCR, %s): %s\n",
464                descr, strerror(errno));
465     free(descr);
466     return 1;
467   }
468 
469   free(iface->descr);
470   iface->descr = descr;
471 
472   return 0;
473 }
474 
475 void
476 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
477 {
478   int af, inskip, in6skip, s4 = -1, s6 = -1, *s;
479   unsigned n;
480 
481   if (iface->addrs) {
482     inskip = in6skip = how == IFACE_CLEAR_ALL ? 0 : 1;
483 
484     for (n = 0; n < iface->addrs; n++) {
485       af = ncprange_family(&iface->addr[n].ifa);
486       if (family == 0 || family == af) {
487         if (!iface->addr[n].system && (how & IFACE_SYSTEM))
488           continue;
489         switch (af) {
490         case AF_INET:
491           if (inskip) {
492             inskip = 0;
493             continue;
494           }
495           s = &s4;
496           break;
497 
498 #ifndef NOINET6
499         case AF_INET6:
500           if (in6skip) {
501             in6skip = 0;
502             continue;
503           }
504           s = &s6;
505           break;
506 #endif
507         default:
508           continue;
509         }
510 
511         if (*s == -1 && (*s = ID0socket(af, SOCK_DGRAM, 0)) == -1)
512           log_Printf(LogERROR, "iface_Clear: socket(): %s\n", strerror(errno));
513         else if (iface_addr_Zap(iface->name, iface->addr + n, *s)) {
514           ncp_IfaceAddrDeleted(ncp, iface->addr + n);
515           bcopy(iface->addr + n + 1, iface->addr + n,
516                 (iface->addrs - n - 1) * sizeof *iface->addr);
517           iface->addrs--;
518           n--;
519         }
520       }
521     }
522 
523     /* Don't bother realloc()ing - we have little to gain */
524 
525     if (s4)
526       close(s4);
527     if (s6)
528       close(s6);
529   }
530 }
531 
532 int
533 iface_Add(struct iface *iface, struct ncp *ncp, const struct ncprange *ifa,
534           const struct ncpaddr *peer, int how)
535 {
536   int af, removed, s;
537   unsigned n;
538   struct ncpaddr ncplocal;
539   struct iface_addr *addr, newaddr;
540 
541   af = ncprange_family(ifa);
542   if ((s = ID0socket(af, SOCK_DGRAM, 0)) == -1) {
543     log_Printf(LogERROR, "iface_Add: socket(): %s\n", strerror(errno));
544     return 0;
545   }
546   ncprange_getaddr(ifa, &ncplocal);
547 
548   for (n = 0; n < iface->addrs; n++) {
549     if (ncprange_contains(&iface->addr[n].ifa, &ncplocal) ||
550         ncpaddr_equal(&iface->addr[n].peer, peer)) {
551       /* Replace this sockaddr */
552       if (!(how & IFACE_FORCE_ADD)) {
553         close(s);
554         return 0;	/* errno = EEXIST; */
555       }
556 
557       if (ncprange_equal(&iface->addr[n].ifa, ifa) &&
558           ncpaddr_equal(&iface->addr[n].peer, peer)) {
559         close(s);
560         ncp_IfaceAddrAdded(ncp, iface->addr + n);
561         return 1;	/* Already there */
562       }
563 
564       removed = iface_addr_Zap(iface->name, iface->addr + n, s);
565       if (removed)
566         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
567       ncprange_copy(&iface->addr[n].ifa, ifa);
568       ncpaddr_copy(&iface->addr[n].peer, peer);
569       if (!iface_addr_Add(iface->name, iface->addr + n, s)) {
570         if (removed) {
571           bcopy(iface->addr + n + 1, iface->addr + n,
572                 (iface->addrs - n - 1) * sizeof *iface->addr);
573           iface->addrs--;
574           n--;
575         }
576         close(s);
577         return 0;
578       }
579       close(s);
580       ncp_IfaceAddrAdded(ncp, iface->addr + n);
581       return 1;
582     }
583   }
584 
585   addr = (struct iface_addr *)realloc
586     (iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
587   if (addr == NULL) {
588     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
589     close(s);
590     return 0;
591   }
592   iface->addr = addr;
593 
594   ncprange_copy(&newaddr.ifa, ifa);
595   ncpaddr_copy(&newaddr.peer, peer);
596   newaddr.system = !!(how & IFACE_SYSTEM);
597   if (!iface_addr_Add(iface->name, &newaddr, s)) {
598     close(s);
599     return 0;
600   }
601 
602   if (how & IFACE_ADD_FIRST) {
603     /* Stuff it at the start of our list */
604     n = 0;
605     bcopy(iface->addr, iface->addr + 1, iface->addrs * sizeof *iface->addr);
606   } else
607     n = iface->addrs;
608 
609   iface->addrs++;
610   memcpy(iface->addr + n, &newaddr, sizeof(*iface->addr));
611 
612   close(s);
613   ncp_IfaceAddrAdded(ncp, iface->addr + n);
614 
615   return 1;
616 }
617 
618 int
619 iface_Delete(struct iface *iface, struct ncp *ncp, const struct ncpaddr *del)
620 {
621   struct ncpaddr found;
622   unsigned n;
623   int res, s;
624 
625   if ((s = ID0socket(ncpaddr_family(del), SOCK_DGRAM, 0)) == -1) {
626     log_Printf(LogERROR, "iface_Delete: socket(): %s\n", strerror(errno));
627     return 0;
628   }
629 
630   for (n = res = 0; n < iface->addrs; n++) {
631     ncprange_getaddr(&iface->addr[n].ifa, &found);
632     if (ncpaddr_equal(&found, del)) {
633       if (iface_addr_Zap(iface->name, iface->addr + n, s)) {
634         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
635         bcopy(iface->addr + n + 1, iface->addr + n,
636               (iface->addrs - n - 1) * sizeof *iface->addr);
637         iface->addrs--;
638         res = 1;
639       }
640       break;
641     }
642   }
643 
644   close(s);
645 
646   return res;
647 }
648 
649 #define IFACE_ADDFLAGS 1
650 #define IFACE_DELFLAGS 2
651 
652 static int
653 iface_ChangeFlags(const char *ifname, int flags, int how)
654 {
655   struct ifreq ifrq;
656   int s, new_flags;
657 
658   s = ID0socket(PF_INET, SOCK_DGRAM, 0);
659   if (s < 0) {
660     log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
661     return 0;
662   }
663 
664   memset(&ifrq, '\0', sizeof ifrq);
665   strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1);
666   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
667   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
668     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
669        strerror(errno));
670     close(s);
671     return 0;
672   }
673 #ifdef __FreeBSD__
674   new_flags = (ifrq.ifr_flags & 0xffff) | (ifrq.ifr_flagshigh << 16);
675 #else
676   new_flags = ifrq.ifr_flags & 0xffff;
677 #endif
678 
679   if (how == IFACE_ADDFLAGS)
680     new_flags |= flags;
681   else
682     new_flags &= ~flags;
683   ifrq.ifr_flags = new_flags & 0xffff;
684 #ifdef __FreeBSD__
685   ifrq.ifr_flagshigh = new_flags >> 16;
686 #endif
687 
688   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
689     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
690        strerror(errno));
691     close(s);
692     return 0;
693   }
694   close(s);
695 
696   return 1;	/* Success */
697 }
698 
699 int
700 iface_SetFlags(const char *ifname, int flags)
701 {
702   return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS);
703 }
704 
705 int
706 iface_ClearFlags(const char *ifname, int flags)
707 {
708   return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS);
709 }
710 
711 void
712 iface_Free(struct iface *iface)
713 {
714     free(iface->name);
715     free(iface->descr);
716     free(iface->addr);
717     free(iface);
718 }
719 
720 void
721 iface_Destroy(struct iface *iface)
722 {
723   struct ifreq ifr;
724   int s;
725 
726   if (iface != NULL) {
727     if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
728       log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
729     } else {
730       strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
731       if (ID0ioctl(s, SIOCIFDESTROY, (caddr_t)&ifr) < 0)
732         log_Printf(LogWARN, "iface_Destroy: ioctl(SIOCIFDESTROY, %s): %s\n",
733                iface->name, strerror(errno));
734     }
735     iface_Free(iface);
736   }
737 }
738 
739 #define if_entry(x) { IFF_##x, #x }
740 
741 struct {
742   int flag;
743   const char *value;
744 } if_flags[] = {
745   if_entry(UP),
746   if_entry(BROADCAST),
747   if_entry(DEBUG),
748   if_entry(LOOPBACK),
749   if_entry(POINTOPOINT),
750   if_entry(RUNNING),
751   if_entry(NOARP),
752   if_entry(PROMISC),
753   if_entry(ALLMULTI),
754   if_entry(OACTIVE),
755   if_entry(SIMPLEX),
756   if_entry(LINK0),
757   if_entry(LINK1),
758   if_entry(LINK2),
759   if_entry(MULTICAST),
760   { 0, "???" }
761 };
762 
763 int
764 iface_Show(struct cmdargs const *arg)
765 {
766   struct ncpaddr ncpaddr;
767   struct iface *iface = arg->bundle->iface, *current;
768   unsigned f;
769   int flags;
770 #ifndef NOINET6
771   int scopeid, width;
772 #endif
773   struct in_addr mask;
774 
775   current = iface_Create(iface->name);
776   flags = iface->flags = current->flags;
777   iface_Free(current);
778 
779   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
780   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
781     if ((if_flags[f].flag & flags)) {
782       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
783                     if_flags[f].value);
784       flags &= ~if_flags[f].flag;
785     }
786 
787 #if 0
788   if (flags)
789     prompt_Printf(arg->prompt, "%s0x%x", flags == iface->flags ? "" : ",",
790                   flags);
791 #endif
792 
793   prompt_Printf(arg->prompt, "> mtu %lu has %d address%s:\n", iface->mtu,
794                 iface->addrs, iface->addrs == 1 ? "" : "es");
795 
796   for (f = 0; f < iface->addrs; f++) {
797     ncprange_getaddr(&iface->addr[f].ifa, &ncpaddr);
798     switch (ncprange_family(&iface->addr[f].ifa)) {
799     case AF_INET:
800       prompt_Printf(arg->prompt, "  inet %s --> ", ncpaddr_ntoa(&ncpaddr));
801       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
802         prompt_Printf(arg->prompt, "255.255.255.255");
803       else
804         prompt_Printf(arg->prompt, "%s", ncpaddr_ntoa(&iface->addr[f].peer));
805       ncprange_getip4mask(&iface->addr[f].ifa, &mask);
806       prompt_Printf(arg->prompt, " netmask 0x%08lx", (long)ntohl(mask.s_addr));
807       break;
808 
809 #ifndef NOINET6
810     case AF_INET6:
811       prompt_Printf(arg->prompt, "  inet6 %s", ncpaddr_ntoa(&ncpaddr));
812       if (ncpaddr_family(&iface->addr[f].peer) != AF_UNSPEC)
813         prompt_Printf(arg->prompt, " --> %s",
814                       ncpaddr_ntoa(&iface->addr[f].peer));
815       ncprange_getwidth(&iface->addr[f].ifa, &width);
816       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
817         prompt_Printf(arg->prompt, " prefixlen %d", width);
818       if ((scopeid = ncprange_scopeid(&iface->addr[f].ifa)) != -1)
819         prompt_Printf(arg->prompt, " scopeid 0x%x", (unsigned)scopeid);
820       break;
821 #endif
822     }
823     prompt_Printf(arg->prompt, "\n");
824   }
825 
826   return 0;
827 }
828 
829 void
830 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
831 {
832   char *wp;
833   int rtax;
834 
835   wp = (char *)(ifam + 1);
836 
837   for (rtax = 0; rtax < RTAX_MAX; rtax++)
838     if (ifam->ifam_addrs & (1 << rtax)) {
839       sa[rtax] = (struct sockaddr *)wp;
840       wp += ROUNDUP(sa[rtax]->sa_len);
841     } else
842       sa[rtax] = NULL;
843 }
844