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