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