2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.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>
41 #include <netinet6/nd6.h>
50 #include <sys/ioctl.h>
51 #include <sys/sysctl.h>
66 #include "throughput.h"
67 #include "slcompress.h"
68 #include "descriptor.h"
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;
91 iface_Create(const char *name)
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];
101 struct iface_addr *addr;
107 mib[4] = NET_RT_IFLIST;
113 if (maxtries-- == 0 || (err && err != ENOMEM)) {
114 fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(err));
118 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
119 fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
124 if ((buf = (char *)malloc(needed)) == NULL) {
125 fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
129 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
134 } while (buf == NULL);
139 namelen = strlen(name);
141 while (ptr < end && iface == NULL) {
142 ifm = (struct if_msghdr *)ptr; /* On if_msghdr */
143 if (ifm->ifm_type != RTM_IFINFO)
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);
149 fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
153 iface->name = strdup(name);
155 iface->index = ifm->ifm_index;
156 iface->flags = ifm->ifm_flags;
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 */
165 if (ifam->ifam_type != RTM_NEWADDR) /* finished this if */
168 if (iface != NULL && ifam->ifam_addrs & RTA_IFA) {
169 /* Found a configured interface ! */
170 iface_ParseHdr(ifam, sa);
172 if (sa[RTAX_IFA] && (sa[RTAX_IFA]->sa_family == AF_INET
174 || sa[RTAX_IFA]->sa_family == AF_INET6
177 /* Record the address */
179 addr = (struct iface_addr *)
180 realloc(iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
185 addr += iface->addrs;
188 ncprange_setsa(&addr->ifa, sa[RTAX_IFA], sa[RTAX_NETMASK]);
190 ncpaddr_setsa(&addr->peer, sa[RTAX_BRD]);
192 ncpaddr_init(&addr->peer);
204 iface_addr_Zap(const char *name, struct iface_addr *addr, int s)
206 struct ifaliasreq ifra;
208 struct in6_aliasreq ifra6;
210 struct sockaddr_in *me4, *msk4, *peer4;
211 struct sockaddr_storage ssme, sspeer, ssmsk;
212 int res, saved_errno;
214 ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
215 ncpaddr_getsa(&addr->peer, &sspeer);
218 switch (ncprange_family(&addr->ifa)) {
220 memset(&ifra, '\0', sizeof ifra);
221 strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
223 me4 = (struct sockaddr_in *)&ifra.ifra_addr;
224 memcpy(me4, &ssme, sizeof *me4);
226 msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
227 memcpy(msk4, &ssmsk, sizeof *msk4);
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;
235 memcpy(peer4, &sspeer, sizeof *peer4);
237 res = ID0ioctl(s, SIOCDIFADDR, &ifra);
239 if (log_IsKept(LogDEBUG)) {
240 char buf[NCP_ASCIIBUFFERSIZE];
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);
250 memset(&ifra6, '\0', sizeof ifra6);
251 strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
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;
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;
263 res = ID0ioctl(s, SIOCDIFADDR_IN6, &ifra6);
270 char dst[NCP_ASCIIBUFFERSIZE];
273 ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
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));
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));
291 iface_addr_Add(const char *name, struct iface_addr *addr, int s)
293 struct ifaliasreq ifra;
295 struct in6_aliasreq ifra6;
297 struct sockaddr_in *me4, *msk4, *peer4;
298 struct sockaddr_storage ssme, sspeer, ssmsk;
299 int res, saved_errno;
301 ncprange_getsa(&addr->ifa, &ssme, &ssmsk);
302 ncpaddr_getsa(&addr->peer, &sspeer);
305 switch (ncprange_family(&addr->ifa)) {
307 memset(&ifra, '\0', sizeof ifra);
308 strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
310 me4 = (struct sockaddr_in *)&ifra.ifra_addr;
311 memcpy(me4, &ssme, sizeof *me4);
313 msk4 = (struct sockaddr_in *)&ifra.ifra_mask;
314 memcpy(msk4, &ssmsk, sizeof *msk4);
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;
322 memcpy(peer4, &sspeer, sizeof *peer4);
324 res = ID0ioctl(s, SIOCAIFADDR, &ifra);
326 if (log_IsKept(LogDEBUG)) {
327 char buf[NCP_ASCIIBUFFERSIZE];
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);
337 memset(&ifra6, '\0', sizeof ifra6);
338 strncpy(ifra6.ifra_name, name, sizeof ifra6.ifra_name - 1);
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;
350 res = ID0ioctl(s, SIOCAIFADDR_IN6, &ifra6);
357 char dst[NCP_ASCIIBUFFERSIZE];
360 ncprange_family(&addr->ifa) == AF_INET6 ? "_IN6" :
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));
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));
378 iface_Name(struct iface *iface, const char *name)
384 if ((newname = strdup(name)) == NULL) {
385 log_Printf(LogWARN, "iface name: strdup failed: %s\n", strerror(errno));
389 if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
390 log_Printf(LogERROR, "iface name: socket(): %s\n", strerror(errno));
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));
405 iface->name = newname;
411 iface_Descr(struct cmdargs const *arg)
416 int s, n, ifdescr_maxlen;
420 if (sysctlbyname("net.ifdescr_maxlen", &ifdescr_maxlen, &sz, NULL, 0) < 0) {
421 log_Printf(LogERROR, "iface descr: sysctl failed: %s\n", strerror(errno));
425 if (ifdescr_maxlen < 1) {
426 log_Printf(LogERROR, "iface descr: sysctl net.ifdescr_maxlen < 1\n");
430 sz = sizeof(char) * ifdescr_maxlen;
431 if ((descr = malloc(sz)) == NULL) {
432 log_Printf(LogERROR, "iface descr: malloc failed: %s\n", strerror(errno));
438 while (n < arg->argc) {
439 if (n > arg->argn && (len = strlcat(descr, " ", sz)) >= sz)
441 if ((len = strlcat(descr, arg->argv[n], sz)) >= sz)
446 log_Printf(LogERROR, "iface descr: description exceeds maximum (%d)\n",
452 if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
453 log_Printf(LogERROR, "iface descr: socket(): %s\n", strerror(errno));
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));
470 iface->descr = descr;
476 iface_Clear(struct iface *iface, struct ncp *ncp, int family, int how)
478 int af, inskip, in6skip, s4 = -1, s6 = -1, *s;
482 inskip = in6skip = how == IFACE_CLEAR_ALL ? 0 : 1;
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))
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);
523 /* Don't bother realloc()ing - we have little to gain */
533 iface_Add(struct iface *iface, struct ncp *ncp, const struct ncprange *ifa,
534 const struct ncpaddr *peer, int how)
538 struct ncpaddr ncplocal;
539 struct iface_addr *addr, newaddr;
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));
546 ncprange_getaddr(ifa, &ncplocal);
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)) {
554 return 0; /* errno = EEXIST; */
557 if (ncprange_equal(&iface->addr[n].ifa, ifa) &&
558 ncpaddr_equal(&iface->addr[n].peer, peer)) {
560 ncp_IfaceAddrAdded(ncp, iface->addr + n);
561 return 1; /* Already there */
564 removed = iface_addr_Zap(iface->name, iface->addr + n, s);
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)) {
571 bcopy(iface->addr + n + 1, iface->addr + n,
572 (iface->addrs - n - 1) * sizeof *iface->addr);
580 ncp_IfaceAddrAdded(ncp, iface->addr + n);
585 addr = (struct iface_addr *)realloc
586 (iface->addr, (iface->addrs + 1) * sizeof iface->addr[0]);
588 log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
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)) {
602 if (how & IFACE_ADD_FIRST) {
603 /* Stuff it at the start of our list */
605 bcopy(iface->addr, iface->addr + 1, iface->addrs * sizeof *iface->addr);
610 memcpy(iface->addr + n, &newaddr, sizeof(*iface->addr));
613 ncp_IfaceAddrAdded(ncp, iface->addr + n);
619 iface_Delete(struct iface *iface, struct ncp *ncp, const struct ncpaddr *del)
621 struct ncpaddr found;
625 if ((s = ID0socket(ncpaddr_family(del), SOCK_DGRAM, 0)) == -1) {
626 log_Printf(LogERROR, "iface_Delete: socket(): %s\n", strerror(errno));
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);
649 #define IFACE_ADDFLAGS 1
650 #define IFACE_DELFLAGS 2
653 iface_ChangeFlags(const char *ifname, int flags, int how)
658 s = ID0socket(PF_INET, SOCK_DGRAM, 0);
660 log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
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",
674 new_flags = (ifrq.ifr_flags & 0xffff) | (ifrq.ifr_flagshigh << 16);
676 new_flags = ifrq.ifr_flags & 0xffff;
679 if (how == IFACE_ADDFLAGS)
683 ifrq.ifr_flags = new_flags & 0xffff;
685 ifrq.ifr_flagshigh = new_flags >> 16;
688 if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
689 log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
696 return 1; /* Success */
700 iface_SetFlags(const char *ifname, int flags)
702 return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS);
706 iface_ClearFlags(const char *ifname, int flags)
708 return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS);
712 iface_Free(struct iface *iface)
721 iface_Destroy(struct iface *iface)
727 if ((s = ID0socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
728 log_Printf(LogERROR, "iface_Destroy: socket(): %s\n", strerror(errno));
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));
739 #define if_entry(x) { IFF_##x, #x }
749 if_entry(POINTOPOINT),
764 iface_Show(struct cmdargs const *arg)
766 struct ncpaddr ncpaddr;
767 struct iface *iface = arg->bundle->iface, *current;
775 current = iface_Create(iface->name);
776 flags = iface->flags = current->flags;
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 ? "" : ",",
784 flags &= ~if_flags[f].flag;
789 prompt_Printf(arg->prompt, "%s0x%x", flags == iface->flags ? "" : ",",
793 prompt_Printf(arg->prompt, "> mtu %lu has %d address%s:\n", iface->mtu,
794 iface->addrs, iface->addrs == 1 ? "" : "es");
796 for (f = 0; f < iface->addrs; f++) {
797 ncprange_getaddr(&iface->addr[f].ifa, &ncpaddr);
798 switch (ncprange_family(&iface->addr[f].ifa)) {
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");
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));
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);
823 prompt_Printf(arg->prompt, "\n");
830 iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX])
835 wp = (char *)(ifam + 1);
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);