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