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