xref: /freebsd/usr.sbin/ppp/iface.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
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 #ifdef __FreeBSD__
35 #include <net/if_var.h>
36 #endif
37 #include <net/route.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in_var.h>
40 #include <netinet/ip.h>
41 #ifndef NOINET6
42 #include <netinet6/nd6.h>
43 #endif
44 #include <sys/un.h>
45 
46 #include <errno.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <sys/ioctl.h>
52 #include <sys/sysctl.h>
53 #include <termios.h>
54 #include <unistd.h>
55 
56 #include "layer.h"
57 #include "defs.h"
58 #include "command.h"
59 #include "mbuf.h"
60 #include "log.h"
61 #include "id.h"
62 #include "timer.h"
63 #include "fsm.h"
64 #include "iplist.h"
65 #include "lqr.h"
66 #include "hdlc.h"
67 #include "throughput.h"
68 #include "slcompress.h"
69 #include "descriptor.h"
70 #include "ncpaddr.h"
71 #include "ipcp.h"
72 #include "filter.h"
73 #include "lcp.h"
74 #include "ccp.h"
75 #include "link.h"
76 #include "mp.h"
77 #ifndef NORADIUS
78 #include "radius.h"
79 #endif
80 #include "ipv6cp.h"
81 #include "ncp.h"
82 #include "bundle.h"
83 #include "prompt.h"
84 #include "iface.h"
85 
86 #define IN6MASK128	{{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
87 			    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
88 static const struct in6_addr in6mask128 = IN6MASK128;
89 
90 
91 struct iface *
92 iface_Create(const char *name)
93 {
94   int mib[6], maxtries, err;
95   size_t needed, namelen;
96   char *buf, *ptr, *end;
97   struct if_msghdr *ifm;
98   struct ifa_msghdr *ifam;
99   struct sockaddr_dl *dl;
100   struct sockaddr *sa[RTAX_MAX];
101   struct iface *iface;
102   struct iface_addr *addr;
103 
104   mib[0] = CTL_NET;
105   mib[1] = PF_ROUTE;
106   mib[2] = 0;
107   mib[3] = 0;
108   mib[4] = NET_RT_IFLIST;
109   mib[5] = 0;
110 
111   maxtries = 20;
112   err = 0;
113   do {
114     if (maxtries-- == 0 || (err && err != ENOMEM)) {
115       fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(err));
116       return NULL;
117     }
118 
119     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
120       fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
121                 strerror(errno));
122       return NULL;
123     }
124 
125     if ((buf = (char *)malloc(needed)) == NULL) {
126       fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
127       return NULL;
128     }
129 
130     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
131       err = errno;
132       free(buf);
133       buf = NULL;
134     }
135   } while (buf == NULL);
136 
137   ptr = buf;
138   end = buf + needed;
139   iface = NULL;
140   namelen = strlen(name);
141 
142   while (ptr < end && iface == NULL) {
143     ifm = (struct if_msghdr *)ptr;			/* On if_msghdr */
144     if (ifm->ifm_type != RTM_IFINFO)
145       break;
146     dl = (struct sockaddr_dl *)(ifm + 1);		/* Single _dl at end */
147     if (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) {
148       iface = (struct iface *)malloc(sizeof *iface);
149       if (iface == NULL) {
150         fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
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;
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     if (log_IsKept(LogDEBUG)) {
239       char buf[100];
240 
241       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
242       log_Printf(LogWARN, "%s: DIFADDR %s -> %s returns %d\n",
243                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
244     }
245     break;
246 
247 #ifndef NOINET6
248   case AF_INET6:
249     memset(&ifra6, '\0', sizeof ifra6);
250     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
251 
252     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
253     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
254     ifra6.ifra_prefixmask.sin6_family = AF_UNSPEC;
255     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
256       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
257     else
258       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
259     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
260     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
261 
262     res = ID0ioctl(s, SIOCDIFADDR_IN6, &ifra6);
263     break;
264 #endif
265   }
266 
267   if (res == -1) {
268     char dst[40];
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(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(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;
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     if (log_IsKept(LogDEBUG)) {
324       char buf[100];
325 
326       snprintf(buf, sizeof buf, "%s", ncprange_ntoa(&addr->ifa));
327       log_Printf(LogWARN, "%s: AIFADDR %s -> %s returns %d\n",
328                  ifra.ifra_name, buf, ncpaddr_ntoa(&addr->peer), res);
329     }
330     break;
331 
332 #ifndef NOINET6
333   case AF_INET6:
334     memset(&ifra6, '\0', sizeof ifra6);
335     strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
336 
337     memcpy(&ifra6.ifra_addr, &ssme, sizeof ifra6.ifra_addr);
338     memcpy(&ifra6.ifra_prefixmask, &ssmsk, sizeof ifra6.ifra_prefixmask);
339     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
340       ifra6.ifra_dstaddr.sin6_family = AF_UNSPEC;
341     else if (memcmp(&((struct sockaddr_in6 *)&ssmsk)->sin6_addr, &in6mask128,
342 		    sizeof in6mask128) == 0)
343       memcpy(&ifra6.ifra_dstaddr, &sspeer, sizeof ifra6.ifra_dstaddr);
344     ifra6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
345     ifra6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
346 
347     res = ID0ioctl(s, SIOCAIFADDR_IN6, &ifra6);
348     break;
349 #endif
350   }
351 
352   if (res == -1) {
353     char dst[40];
354     const char *end =
355 #ifndef NOINET6
356       ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
357 #endif
358       "";
359 
360     if (ncpaddr_family(&addr->peer) == AF_UNSPEC)
361       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s): %s\n",
362                  end, ncprange_ntoa(&addr->ifa), strerror(errno));
363     else {
364       snprintf(dst, sizeof dst, "%s", ncpaddr_ntoa(&addr->peer));
365       log_Printf(LogWARN, "iface add: ioctl(SIOCAIFADDR%s, %s -> %s): %s\n",
366                  end, ncprange_ntoa(&addr->ifa), dst, strerror(errno));
367     }
368   }
369 
370   return res != -1;
371 }
372 
373 int
374 iface_Name(struct iface *iface, const char *name)
375 {
376   struct ifreq ifr;
377   int s;
378   char *newname;
379 
380   if ((newname = strdup(name)) == NULL) {
381     log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
382     return 0;
383   }
384 
385   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
386     log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
387     free(newname);
388     return 0;
389   }
390 
391   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
392   ifr.ifr_data = newname;
393   if (ID0ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
394     log_Printf(LogWARN, "iface name: ioctl(SIOCSIFNAME, %s -> %s): %s\n",
395                name, newname, strerror(errno));
396     free(newname);
397     return 0;
398   }
399 
400   free(iface->name);
401   iface->name = newname;
402 
403   return 1;
404 }
405 
406 int
407 iface_Descr(struct cmdargs const *arg)
408 {
409   struct ifreq ifr;
410   struct iface *iface;
411   size_t sz, len;
412   int s, n, ifdescr_maxlen;
413   char *descr;
414 
415   sz = sizeof(int);
416   if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
417     log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
418     return 1;
419   }
420 
421   if (ifdescr_maxlen < 1) {
422     log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
423     return 1;
424   }
425 
426   sz = sizeof(char) * ifdescr_maxlen;
427   if ((descr = malloc(sz)) == NULL) {
428     log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
429     return 1;
430   }
431 
432   *descr = '\0';
433   n = arg->argn;
434   while (n < arg->argc) {
435     if (n > arg->argn && (len = strlcat(descr, " ", sz)) >= sz)
436       break;
437     if ((len = strlcat(descr, arg->argv[n], sz)) >= sz)
438       break;
439     ++n;
440   }
441   if (len >= sz) {
442     log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n",
443                ifdescr_maxlen-1);
444     free(descr);
445     return 1;
446   }
447 
448   if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
449     log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
450     free(descr);
451     return 1;
452   }
453 
454   iface = arg->bundle->iface;
455   strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
456   ifr.ifr_buffer.length = strlen(descr) + 1;
457   ifr.ifr_buffer.buffer = descr;
458   if (ID0ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
459     log_Printf(LogWARN, "iface descr: ioctl(SIOCSIFDESCR, %s): %s\n",
460                descr, strerror(errno));
461     free(descr);
462     return 1;
463   }
464 
465   free(iface->descr);
466   iface->descr = descr;
467 
468   return 0;
469 }
470 
471 void
472 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
473 {
474   int addrs, af, inskip, in6skip, s4 = -1, s6 = -1, *s;
475   unsigned n;
476 
477   if (iface->addrs) {
478     inskip = in6skip = how == IFACE_CLEAR_ALL ? 0 : 1;
479     addrs = 0;
480 
481     for (n = 0; n < iface->addrs; n++) {
482       af = ncprange_family(&iface->addr[n].ifa);
483       if (family == 0 || family == af) {
484         if (!iface->addr[n].system && (how & IFACE_SYSTEM))
485           continue;
486         switch (af) {
487         case AF_INET:
488           if (inskip) {
489             inskip = 0;
490             continue;
491           }
492           s = &s4;
493           break;
494 
495 #ifndef NOINET6
496         case AF_INET6:
497           if (in6skip) {
498             in6skip = 0;
499             continue;
500           }
501           s = &s6;
502           break;
503 #endif
504         default:
505           continue;
506         }
507 
508         if (*s == -1 && (*s = ID0socket(af, SOCK_DGRAM, 0)) == -1)
509           log_Printf(LogERROR, "iface_Clear: socket(): %s\n", strerror(errno));
510         else if (iface_addr_Zap(iface->name, iface->addr + n, *s)) {
511           ncp_IfaceAddrDeleted(ncp, iface->addr + n);
512           bcopy(iface->addr + n + 1, iface->addr + n,
513                 (iface->addrs - n - 1) * sizeof *iface->addr);
514           iface->addrs--;
515           n--;
516         }
517       }
518     }
519 
520     /* Don't bother realloc()ing - we have little to gain */
521 
522     if (s4)
523       close(s4);
524     if (s6)
525       close(s6);
526   }
527 }
528 
529 int
530 iface_Add(struct iface *iface, struct ncp *ncp, const struct ncprange *ifa,
531           const struct ncpaddr *peer, int how)
532 {
533   int af, removed, s;
534   unsigned n;
535   struct ncpaddr ncplocal;
536   struct iface_addr *addr, newaddr;
537 
538   af = ncprange_family(ifa);
539   if ((s = ID0socket(af, SOCK_DGRAM, 0)) == -1) {
540     log_Printf(LogERROR, "iface_Add: socket(): %s\n", strerror(errno));
541     return 0;
542   }
543   ncprange_getaddr(ifa, &ncplocal);
544 
545   for (n = 0; n < iface->addrs; n++) {
546     if (ncprange_contains(&iface->addr[n].ifa, &ncplocal) ||
547         ncpaddr_equal(&iface->addr[n].peer, peer)) {
548       /* Replace this sockaddr */
549       if (!(how & IFACE_FORCE_ADD)) {
550         close(s);
551         return 0;	/* errno = EEXIST; */
552       }
553 
554       if (ncprange_equal(&iface->addr[n].ifa, ifa) &&
555           ncpaddr_equal(&iface->addr[n].peer, peer)) {
556         close(s);
557         ncp_IfaceAddrAdded(ncp, iface->addr + n);
558         return 1;	/* Already there */
559       }
560 
561       removed = iface_addr_Zap(iface->name, iface->addr + n, s);
562       if (removed)
563         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
564       ncprange_copy(&iface->addr[n].ifa, ifa);
565       ncpaddr_copy(&iface->addr[n].peer, peer);
566       if (!iface_addr_Add(iface->name, iface->addr + n, s)) {
567         if (removed) {
568           bcopy(iface->addr + n + 1, iface->addr + n,
569                 (iface->addrs - n - 1) * sizeof *iface->addr);
570           iface->addrs--;
571           n--;
572         }
573         close(s);
574         return 0;
575       }
576       close(s);
577       ncp_IfaceAddrAdded(ncp, iface->addr + n);
578       return 1;
579     }
580   }
581 
582   addr = (struct iface_addr *)realloc
583     (iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
584   if (addr == NULL) {
585     log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
586     close(s);
587     return 0;
588   }
589   iface->addr = addr;
590 
591   ncprange_copy(&newaddr.ifa, ifa);
592   ncpaddr_copy(&newaddr.peer, peer);
593   newaddr.system = !!(how & IFACE_SYSTEM);
594   if (!iface_addr_Add(iface->name, &newaddr, s)) {
595     close(s);
596     return 0;
597   }
598 
599   if (how & IFACE_ADD_FIRST) {
600     /* Stuff it at the start of our list */
601     n = 0;
602     bcopy(iface->addr, iface->addr + 1, iface->addrs * sizeof *iface->addr);
603   } else
604     n = iface->addrs;
605 
606   iface->addrs++;
607   memcpy(iface->addr + n, &newaddr, sizeof(*iface->addr));
608 
609   close(s);
610   ncp_IfaceAddrAdded(ncp, iface->addr + n);
611 
612   return 1;
613 }
614 
615 int
616 iface_Delete(struct iface *iface, struct ncp *ncp, const struct ncpaddr *del)
617 {
618   struct ncpaddr found;
619   unsigned n;
620   int res, s;
621 
622   if ((s = ID0socket(ncpaddr_family(del), SOCK_DGRAM, 0)) == -1) {
623     log_Printf(LogERROR, "iface_Delete: socket(): %s\n", strerror(errno));
624     return 0;
625   }
626 
627   for (n = res = 0; n < iface->addrs; n++) {
628     ncprange_getaddr(&iface->addr[n].ifa, &found);
629     if (ncpaddr_equal(&found, del)) {
630       if (iface_addr_Zap(iface->name, iface->addr + n, s)) {
631         ncp_IfaceAddrDeleted(ncp, iface->addr + n);
632         bcopy(iface->addr + n + 1, iface->addr + n,
633               (iface->addrs - n - 1) * sizeof *iface->addr);
634         iface->addrs--;
635         res = 1;
636       }
637       break;
638     }
639   }
640 
641   close(s);
642 
643   return res;
644 }
645 
646 #define IFACE_ADDFLAGS 1
647 #define IFACE_DELFLAGS 2
648 
649 static int
650 iface_ChangeFlags(const char *ifname, int flags, int how)
651 {
652   struct ifreq ifrq;
653   int s, new_flags;
654 
655   s = ID0socket(PF_INET, SOCK_DGRAM, 0);
656   if (s < 0) {
657     log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
658     return 0;
659   }
660 
661   memset(&ifrq, '\0', sizeof ifrq);
662   strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1);
663   ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
664   if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
665     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
666        strerror(errno));
667     close(s);
668     return 0;
669   }
670 #ifdef __FreeBSD__
671   new_flags = (ifrq.ifr_flags & 0xffff) | (ifrq.ifr_flagshigh << 16);
672 #else
673   new_flags = ifrq.ifr_flags & 0xffff;
674 #endif
675 
676   if (how == IFACE_ADDFLAGS)
677     new_flags |= flags;
678   else
679     new_flags &= ~flags;
680   ifrq.ifr_flags = new_flags & 0xffff;
681 #ifdef __FreeBSD__
682   ifrq.ifr_flagshigh = new_flags >> 16;
683 #endif
684 
685   if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
686     log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
687        strerror(errno));
688     close(s);
689     return 0;
690   }
691   close(s);
692 
693   return 1;	/* Success */
694 }
695 
696 int
697 iface_SetFlags(const char *ifname, int flags)
698 {
699   return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS);
700 }
701 
702 int
703 iface_ClearFlags(const char *ifname, int flags)
704 {
705   return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS);
706 }
707 
708 void
709 iface_Free(struct iface *iface)
710 {
711     free(iface->name);
712     free(iface->descr);
713     free(iface->addr);
714     free(iface);
715 }
716 
717 void
718 iface_Destroy(struct iface *iface)
719 {
720   struct ifreq ifr;
721   int s;
722 
723   if (iface != NULL) {
724     if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
725       log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
726     } else {
727       strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
728       if (ID0ioctl(s, SIOCIFDESTROY, (caddr_t)&ifr) < 0)
729         log_Printf(LogWARN, "iface_Destroy: ioctl(SIOCIFDESTROY, %s): %s\n",
730                iface->name, strerror(errno));
731     }
732     iface_Free(iface);
733   }
734 }
735 
736 #define if_entry(x) { IFF_##x, #x }
737 
738 struct {
739   int flag;
740   const char *value;
741 } if_flags[] = {
742   if_entry(UP),
743   if_entry(BROADCAST),
744   if_entry(DEBUG),
745   if_entry(LOOPBACK),
746   if_entry(POINTOPOINT),
747   if_entry(RUNNING),
748   if_entry(NOARP),
749   if_entry(PROMISC),
750   if_entry(ALLMULTI),
751   if_entry(OACTIVE),
752   if_entry(SIMPLEX),
753   if_entry(LINK0),
754   if_entry(LINK1),
755   if_entry(LINK2),
756   if_entry(MULTICAST),
757   { 0, "???" }
758 };
759 
760 int
761 iface_Show(struct cmdargs const *arg)
762 {
763   struct ncpaddr ncpaddr;
764   struct iface *iface = arg->bundle->iface, *current;
765   unsigned f;
766   int flags;
767 #ifndef NOINET6
768   int scopeid, width;
769 #endif
770   struct in_addr mask;
771 
772   current = iface_Create(iface->name);
773   flags = iface->flags = current->flags;
774   iface_Free(current);
775 
776   prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
777   for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
778     if ((if_flags[f].flag & flags)) {
779       prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
780                     if_flags[f].value);
781       flags &= ~if_flags[f].flag;
782     }
783 
784 #if 0
785   if (flags)
786     prompt_Printf(arg->prompt, "%s0x%x", flags == iface->flags ? "" : ",",
787                   flags);
788 #endif
789 
790   prompt_Printf(arg->prompt, "> mtu %lu has %d address%s:\n", iface->mtu,
791                 iface->addrs, iface->addrs == 1 ? "" : "es");
792 
793   for (f = 0; f < iface->addrs; f++) {
794     ncprange_getaddr(&iface->addr[f].ifa, &ncpaddr);
795     switch (ncprange_family(&iface->addr[f].ifa)) {
796     case AF_INET:
797       prompt_Printf(arg->prompt, "  inet %s --> ", ncpaddr_ntoa(&ncpaddr));
798       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
799         prompt_Printf(arg->prompt, "255.255.255.255");
800       else
801         prompt_Printf(arg->prompt, "%s", ncpaddr_ntoa(&iface->addr[f].peer));
802       ncprange_getip4mask(&iface->addr[f].ifa, &mask);
803       prompt_Printf(arg->prompt, " netmask 0x%08lx", (long)ntohl(mask.s_addr));
804       break;
805 
806 #ifndef NOINET6
807     case AF_INET6:
808       prompt_Printf(arg->prompt, "  inet6 %s", ncpaddr_ntoa(&ncpaddr));
809       if (ncpaddr_family(&iface->addr[f].peer) != AF_UNSPEC)
810         prompt_Printf(arg->prompt, " --> %s",
811                       ncpaddr_ntoa(&iface->addr[f].peer));
812       ncprange_getwidth(&iface->addr[f].ifa, &width);
813       if (ncpaddr_family(&iface->addr[f].peer) == AF_UNSPEC)
814         prompt_Printf(arg->prompt, " prefixlen %d", width);
815       if ((scopeid = ncprange_scopeid(&iface->addr[f].ifa)) != -1)
816         prompt_Printf(arg->prompt, " scopeid 0x%x", (unsigned)scopeid);
817       break;
818 #endif
819     }
820     prompt_Printf(arg->prompt, "\n");
821   }
822 
823   return 0;
824 }
825 
826 void
827 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
828 {
829   char *wp;
830   int rtax;
831 
832   wp = (char *)(ifam + 1);
833 
834   for (rtax = 0; rtax < RTAX_MAX; rtax++)
835     if (ifam->ifam_addrs & (1 << rtax)) {
836       sa[rtax] = (struct sockaddr *)wp;
837       wp += ROUNDUP(sa[rtax]->sa_len);
838     } else
839       sa[rtax] = NULL;
840 }
841