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