2 * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <net/if_types.h>
32 #include <net/route.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/ip.h>
36 #include <arpa/inet.h>
51 #include "slcompress.h"
53 #include "throughput.h"
58 #include "descriptor.h"
75 #define ncprange_ip4addr u.ip4.ipaddr
76 #define ncprange_ip4mask u.ip4.mask
77 #define ncprange_ip4width u.ip4.width
78 #define ncpaddr_ip4addr u.ip4addr
80 #define ncprange_ip6addr u.ip6.ipaddr
81 #define ncprange_ip6width u.ip6.width
82 #define ncpaddr_ip6addr u.ip6addr
85 #define NCP_ASCIIBUFFERSIZE 52
90 struct in_addr result;
91 u_int32_t bit = 0x80000000;
101 result.s_addr = htonl(result.s_addr);
106 mask42bits(struct in_addr mask)
108 u_int32_t msk = ntohl(mask.s_addr);
112 for (ret = 32, tst = 1; tst; ret--, tst <<= 1)
116 for (tst <<= 1; tst; tst <<= 1)
120 return tst ? -1 : ret;
124 static struct in6_addr
127 struct in6_addr result;
128 u_int32_t bit = 0x80;
129 u_char *c = result.s6_addr;
131 memset(&result, '\0', sizeof result);
147 mask62bits(const struct in6_addr *mask)
149 const u_char masks[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
150 const u_char *c, *p, *end;
153 p = (const u_char *)mask;
154 for (masklen = 0, end = p + 16; p < end && *p == 0xff; p++)
158 for (c = masks; c < masks + sizeof masks; c++)
160 masklen += c - masks;
169 adjust_linklocal(struct sockaddr_in6 *sin6)
171 /* XXX: ?????!?!?!!!!! This is horrible ! */
172 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
173 IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr)) {
174 sin6->sin6_scope_id =
175 ntohs(*(u_short *)&sin6->sin6_addr.s6_addr[2]);
176 *(u_short *)&sin6->sin6_addr.s6_addr[2] = 0;
182 ncpaddr_init(struct ncpaddr *addr)
184 addr->ncpaddr_family = AF_UNSPEC;
188 ncpaddr_isset(const struct ncpaddr *addr)
190 return addr->ncpaddr_family != AF_UNSPEC;
194 ncpaddr_isdefault(const struct ncpaddr *addr)
196 switch (addr->ncpaddr_family) {
198 if (addr->ncpaddr_ip4addr.s_addr == INADDR_ANY)
204 if (IN6_IS_ADDR_UNSPECIFIED(&addr->ncpaddr_ip6addr))
214 ncpaddr_equal(const struct ncpaddr *addr, const struct ncpaddr *cmp)
216 if (addr->ncpaddr_family != cmp->ncpaddr_family)
219 switch (addr->ncpaddr_family) {
221 return addr->ncpaddr_ip4addr.s_addr == cmp->ncpaddr_ip4addr.s_addr;
225 return !memcmp(&addr->ncpaddr_ip6addr, &cmp->ncpaddr_ip6addr,
226 sizeof addr->ncpaddr_ip6addr);
237 ncpaddr_copy(struct ncpaddr *addr, const struct ncpaddr *from)
239 switch (from->ncpaddr_family) {
241 addr->ncpaddr_family = AF_INET;
242 addr->ncpaddr_ip4addr = from->ncpaddr_ip4addr;
246 addr->ncpaddr_family = AF_INET6;
247 addr->ncpaddr_ip6addr = from->ncpaddr_ip6addr;
251 addr->ncpaddr_family = AF_UNSPEC;
256 ncpaddr_setip4addr(struct ncpaddr *addr, u_int32_t ip)
258 addr->ncpaddr_family = AF_INET;
259 addr->ncpaddr_ip4addr.s_addr = ip;
263 ncpaddr_getip4addr(const struct ncpaddr *addr, u_int32_t *ip)
265 if (addr->ncpaddr_family != AF_INET)
267 *ip = addr->ncpaddr_ip4addr.s_addr;
272 ncpaddr_setip4(struct ncpaddr *addr, struct in_addr ip)
274 addr->ncpaddr_family = AF_INET;
275 addr->ncpaddr_ip4addr = ip;
279 ncpaddr_getip4(const struct ncpaddr *addr, struct in_addr *ip)
281 if (addr->ncpaddr_family != AF_INET)
283 *ip = addr->ncpaddr_ip4addr;
289 ncpaddr_setip6(struct ncpaddr *addr, const struct in6_addr *ip6)
291 addr->ncpaddr_family = AF_INET6;
292 addr->ncpaddr_ip6addr = *ip6;
296 ncpaddr_getip6(const struct ncpaddr *addr, struct in6_addr *ip6)
298 if (addr->ncpaddr_family != AF_INET6)
300 *ip6 = addr->ncpaddr_ip6addr;
306 ncpaddr_getsa(const struct ncpaddr *addr, struct sockaddr_storage *host)
308 struct sockaddr_in *host4 = (struct sockaddr_in *)host;
310 struct sockaddr_in6 *host6 = (struct sockaddr_in6 *)host;
313 memset(host, '\0', sizeof(*host));
315 switch (addr->ncpaddr_family) {
317 host4->sin_family = AF_INET;
318 host4->sin_len = sizeof(*host4);
319 host4->sin_addr = addr->ncpaddr_ip4addr;
324 host6->sin6_family = AF_INET6;
325 host6->sin6_len = sizeof(*host6);
326 host6->sin6_addr = addr->ncpaddr_ip6addr;
331 host->ss_family = AF_UNSPEC;
337 ncpaddr_setsa(struct ncpaddr *addr, const struct sockaddr *host)
339 const struct sockaddr_in *host4 = (const struct sockaddr_in *)host;
341 const struct sockaddr_in6 *host6 = (const struct sockaddr_in6 *)host;
344 switch (host->sa_family) {
346 addr->ncpaddr_family = AF_INET;
347 addr->ncpaddr_ip4addr = host4->sin_addr;
352 if (IN6_IS_ADDR_V4MAPPED(&host6->sin6_addr)) {
353 addr->ncpaddr_family = AF_INET;
354 addr->ncpaddr_ip4addr.s_addr =
355 *(const u_int32_t *)(host6->sin6_addr.s6_addr + 12);
357 addr->ncpaddr_family = AF_INET6;
358 addr->ncpaddr_ip6addr = host6->sin6_addr;
364 addr->ncpaddr_family = AF_UNSPEC;
369 ncpaddr_ntowa(const struct ncpaddr *addr)
371 static char res[NCP_ASCIIBUFFERSIZE];
373 struct sockaddr_in6 sin6;
376 switch (addr->ncpaddr_family) {
378 snprintf(res, sizeof res, "%s", inet_ntoa(addr->ncpaddr_ip4addr));
383 memset(&sin6, '\0', sizeof(sin6));
384 sin6.sin6_len = sizeof(sin6);
385 sin6.sin6_family = AF_INET6;
386 sin6.sin6_addr = addr->ncpaddr_ip6addr;
387 adjust_linklocal(&sin6);
388 if (getnameinfo((struct sockaddr *)&sin6, sizeof sin6, res, sizeof(res),
389 NULL, 0, NI_WITHSCOPEID | NI_NUMERICHOST) != 0)
396 snprintf(res, sizeof res, "<AF_UNSPEC>");
401 ncpaddr_ntoa(const struct ncpaddr *addr)
403 return ncpaddr_ntowa(addr);
408 ncpaddr_aton(struct ncpaddr *addr, struct ncp *ncp, const char *data)
410 struct ncprange range;
412 if (!ncprange_aton(&range, ncp, data))
415 if (range.ncprange_family == AF_INET && range.ncprange_ip4width != 32) {
416 log_Printf(LogWARN, "ncpaddr_aton: %s: Only 32 bits allowed\n", data);
421 if (range.ncprange_family == AF_INET6 && range.ncprange_ip6width != 128) {
422 log_Printf(LogWARN, "ncpaddr_aton: %s: Only 128 bits allowed\n", data);
427 switch (range.ncprange_family) {
429 addr->ncpaddr_family = range.ncprange_family;
430 addr->ncpaddr_ip4addr = range.ncprange_ip4addr;
435 addr->ncpaddr_family = range.ncprange_family;
436 addr->ncpaddr_ip6addr = range.ncprange_ip6addr;
445 ncprange_init(struct ncprange *range)
447 range->ncprange_family = AF_UNSPEC;
451 ncprange_isset(const struct ncprange *range)
453 return range->ncprange_family != AF_UNSPEC;
457 ncprange_equal(const struct ncprange *range, const struct ncprange *cmp)
459 if (range->ncprange_family != cmp->ncprange_family)
462 switch (range->ncprange_family) {
464 if (range->ncprange_ip4addr.s_addr != cmp->ncprange_ip4addr.s_addr)
466 return range->ncprange_ip4mask.s_addr == cmp->ncprange_ip4mask.s_addr;
470 if (range->ncprange_ip6width != cmp->ncprange_ip6width)
472 return !memcmp(&range->ncprange_ip6addr, &cmp->ncprange_ip6addr,
473 sizeof range->ncprange_ip6addr);
484 ncprange_isdefault(const struct ncprange *range)
486 switch (range->ncprange_family) {
488 if (range->ncprange_ip4addr.s_addr == INADDR_ANY)
494 if (range->ncprange_ip6width == 0 &&
495 IN6_IS_ADDR_UNSPECIFIED(&range->ncprange_ip6addr))
505 ncprange_setdefault(struct ncprange *range, int af)
507 memset(range, '\0', sizeof *range);
508 range->ncprange_family = af;
512 ncprange_contains(const struct ncprange *range, const struct ncpaddr *addr)
515 const u_char masks[] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
516 const u_char *addrp, *rangep;
520 if (range->ncprange_family != addr->ncpaddr_family)
523 switch (range->ncprange_family) {
525 return !((addr->ncpaddr_ip4addr.s_addr ^ range->ncprange_ip4addr.s_addr) &
526 range->ncprange_ip4mask.s_addr);
530 rangep = (const u_char *)range->ncprange_ip6addr.s6_addr;
531 addrp = (const u_char *)addr->ncpaddr_ip6addr.s6_addr;
533 for (bits = range->ncprange_ip6width; bits > 0; bits -= 8)
534 if ((*addrp++ ^ *rangep++) & masks[bits > 7 ? 7 : bits - 1])
545 ncprange_containsip4(const struct ncprange *range, struct in_addr addr)
547 switch (range->ncprange_family) {
549 return !((addr.s_addr ^ range->ncprange_ip4addr.s_addr) &
550 range->ncprange_ip4mask.s_addr);
557 ncprange_copy(struct ncprange *range, const struct ncprange *from)
559 switch (from->ncprange_family) {
561 range->ncprange_family = AF_INET;
562 range->ncprange_ip4addr = from->ncprange_ip4addr;
563 range->ncprange_ip4mask = from->ncprange_ip4mask;
564 range->ncprange_ip4width = from->ncprange_ip4width;
569 range->ncprange_family = AF_INET6;
570 range->ncprange_ip6addr = from->ncprange_ip6addr;
571 range->ncprange_ip6width = from->ncprange_ip6width;
576 range->ncprange_family = AF_UNSPEC;
581 ncprange_set(struct ncprange *range, const struct ncpaddr *addr, int width)
583 ncprange_sethost(range, addr);
584 ncprange_setwidth(range, width);
588 ncprange_sethost(struct ncprange *range, const struct ncpaddr *from)
590 switch (from->ncpaddr_family) {
592 range->ncprange_family = AF_INET;
593 range->ncprange_ip4addr = from->ncpaddr_ip4addr;
594 if (from->ncpaddr_ip4addr.s_addr == INADDR_ANY) {
595 range->ncprange_ip4mask.s_addr = INADDR_ANY;
596 range->ncprange_ip4width = 0;
598 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
599 range->ncprange_ip4width = 32;
605 range->ncprange_family = AF_INET6;
606 range->ncprange_ip6addr = from->ncpaddr_ip6addr;
607 range->ncprange_ip6width = 128;
612 range->ncprange_family = AF_UNSPEC;
617 ncprange_ishost(const struct ncprange *range)
619 switch (range->ncprange_family) {
621 return range->ncprange_ip4width == 32;
624 return range->ncprange_ip6width == 128;
632 ncprange_setwidth(struct ncprange *range, int width)
634 switch (range->ncprange_family) {
636 if (width < 0 || width > 32)
638 range->ncprange_ip4width = width;
639 range->ncprange_ip4mask = bits2mask4(width);
644 if (width < 0 || width > 128)
646 range->ncprange_ip6width = width;
658 ncprange_setip4host(struct ncprange *range, struct in_addr from)
660 range->ncprange_family = AF_INET;
661 range->ncprange_ip4addr = from;
662 if (from.s_addr == INADDR_ANY) {
663 range->ncprange_ip4mask.s_addr = INADDR_ANY;
664 range->ncprange_ip4width = 0;
666 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
667 range->ncprange_ip4width = 32;
672 ncprange_setip4(struct ncprange *range, struct in_addr from, struct in_addr msk)
674 range->ncprange_family = AF_INET;
675 range->ncprange_ip4addr = from;
676 range->ncprange_ip4mask = msk;
677 range->ncprange_ip4width = mask42bits(msk);
682 ncprange_setip4mask(struct ncprange *range, struct in_addr mask)
684 if (range->ncprange_family != AF_INET)
686 range->ncprange_ip4mask = mask;
687 range->ncprange_ip4width = mask42bits(mask);
692 ncprange_setsa(struct ncprange *range, const struct sockaddr *host,
693 const struct sockaddr *mask)
695 const struct sockaddr_in *host4 = (const struct sockaddr_in *)host;
696 const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;
698 const struct sockaddr_in6 *host6 = (const struct sockaddr_in6 *)host;
699 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *)mask;
702 switch (host->sa_family) {
704 range->ncprange_family = AF_INET;
705 range->ncprange_ip4addr = host4->sin_addr;
706 if (host4->sin_addr.s_addr == INADDR_ANY) {
707 range->ncprange_ip4mask.s_addr = INADDR_ANY;
708 range->ncprange_ip4width = 0;
709 } else if (mask4 && mask4->sin_family == AF_INET) {
710 range->ncprange_ip4mask.s_addr = mask4->sin_addr.s_addr;
711 range->ncprange_ip4width = mask42bits(mask4->sin_addr);
713 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
714 range->ncprange_ip4width = 32;
720 range->ncprange_family = AF_INET6;
721 range->ncprange_ip6addr = host6->sin6_addr;
722 range->ncprange_ip6width = mask6 ? mask62bits(&mask6->sin6_addr) : 128;
727 range->ncprange_family = AF_UNSPEC;
732 ncprange_getsa(const struct ncprange *range, struct sockaddr_storage *host,
733 struct sockaddr_storage *mask)
735 struct sockaddr_in *host4 = (struct sockaddr_in *)host;
736 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
738 struct sockaddr_in6 *host6 = (struct sockaddr_in6 *)host;
739 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
742 memset(host, '\0', sizeof(*host));
744 memset(mask, '\0', sizeof(*mask));
746 switch (range->ncprange_family) {
748 host4->sin_family = AF_INET;
749 host4->sin_len = sizeof(*host4);
750 host4->sin_addr = range->ncprange_ip4addr;
752 mask4->sin_family = AF_INET;
753 mask4->sin_len = sizeof(*host4);
754 mask4->sin_addr = range->ncprange_ip4mask;
760 host6->sin6_family = AF_INET6;
761 host6->sin6_len = sizeof(*host6);
762 host6->sin6_addr = range->ncprange_ip6addr;
764 mask6->sin6_family = AF_INET6;
765 mask6->sin6_len = sizeof(*host6);
766 mask6->sin6_addr = bits2mask6(range->ncprange_ip6width);
772 host->ss_family = AF_UNSPEC;
774 mask->ss_family = AF_UNSPEC;
780 ncprange_getaddr(const struct ncprange *range, struct ncpaddr *addr)
782 switch (range->ncprange_family) {
784 addr->ncpaddr_family = AF_INET;
785 addr->ncpaddr_ip4addr = range->ncprange_ip4addr;
789 addr->ncpaddr_family = AF_INET6;
790 addr->ncpaddr_ip6addr = range->ncprange_ip6addr;
799 ncprange_getip4addr(const struct ncprange *range, struct in_addr *addr)
801 if (range->ncprange_family != AF_INET)
804 *addr = range->ncprange_ip4addr;
809 ncprange_getip4mask(const struct ncprange *range, struct in_addr *mask)
811 switch (range->ncprange_family) {
813 *mask = range->ncprange_ip4mask;
821 ncprange_getwidth(const struct ncprange *range, int *width)
823 switch (range->ncprange_family) {
825 *width = range->ncprange_ip4width;
829 *width = range->ncprange_ip6width;
838 ncprange_ntoa(const struct ncprange *range)
844 if (!ncprange_getaddr(range, &addr))
845 return "<AF_UNSPEC>";
847 res = ncpaddr_ntowa(&addr);
849 if (len >= NCP_ASCIIBUFFERSIZE - 1)
852 switch (range->ncprange_family) {
854 if (range->ncprange_ip4width == -1) {
855 /* A non-contiguous mask */
856 for (; len >= 3; res[len -= 2] = '\0')
857 if (strcmp(res + len - 2, ".0"))
859 snprintf(res + len, sizeof res - len, "&0x%08lx",
860 (unsigned long)ntohl(range->ncprange_ip4mask.s_addr));
861 } else if (range->ncprange_ip4width < 32)
862 snprintf(res + len, sizeof res - len, "/%d", range->ncprange_ip4width);
868 if (range->ncprange_ip6width != 128)
869 snprintf(res + len, sizeof res - len, "/%d", range->ncprange_ip6width);
875 return "<AF_UNSPEC>";
880 ncprange_scopeid(const struct ncprange *range)
882 const struct in6_addr *sin6;
885 if (range->ncprange_family == AF_INET6) {
886 sin6 = &range->ncprange_ip6addr;
887 if (IN6_IS_ADDR_LINKLOCAL(sin6) || IN6_IS_ADDR_MC_LINKLOCAL(sin6))
888 if ((scopeid = ntohs(*(const u_short *)&sin6->s6_addr[2])) == 0)
897 ncprange_aton(struct ncprange *range, struct ncp *ncp, const char *data)
904 len = strcspn(data, "/");
906 if (ncp && strncasecmp(data, "HISADDR", len) == 0) {
907 range->ncprange_family = AF_INET;
908 range->ncprange_ip4addr = ncp->ipcp.peer_ip;
909 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
910 range->ncprange_ip4width = 32;
913 } else if (ncp && strncasecmp(data, "HISADDR6", len) == 0) {
914 ncprange_sethost(range, &ncp->ipv6cp.hisaddr);
917 } else if (ncp && strncasecmp(data, "MYADDR", len) == 0) {
918 range->ncprange_family = AF_INET;
919 range->ncprange_ip4addr = ncp->ipcp.my_ip;
920 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
921 range->ncprange_ip4width = 32;
924 } else if (ncp && strncasecmp(data, "MYADDR6", len) == 0) {
925 ncprange_sethost(range, &ncp->ipv6cp.myaddr);
928 } else if (ncp && strncasecmp(data, "DNS0", len) == 0) {
929 range->ncprange_family = AF_INET;
930 range->ncprange_ip4addr = ncp->ipcp.ns.dns[0];
931 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
932 range->ncprange_ip4width = 32;
934 } else if (ncp && strncasecmp(data, "DNS1", len) == 0) {
935 range->ncprange_family = AF_INET;
936 range->ncprange_ip4addr = ncp->ipcp.ns.dns[1];
937 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
938 range->ncprange_ip4width = 32;
942 s = (char *)alloca(len + 1);
943 strncpy(s, data, len);
947 if (data[len] != '\0') {
948 bits = strtol(data + len + 1, &wp, 0);
949 if (*wp || wp == data + len + 1 || bits < 0 || bits > 128) {
950 log_Printf(LogWARN, "ncprange_aton: bad mask width.\n");
955 if ((cp = strchr(data, ':')) == NULL) {
956 range->ncprange_family = AF_INET;
958 range->ncprange_ip4addr = GetIpAddr(s);
960 if (range->ncprange_ip4addr.s_addr == INADDR_NONE) {
961 log_Printf(LogWARN, "ncprange_aton: %s: Bad address\n", s);
965 if (range->ncprange_ip4addr.s_addr == INADDR_ANY) {
966 range->ncprange_ip4mask.s_addr = INADDR_ANY;
967 range->ncprange_ip4width = 0;
968 } else if (bits == -1) {
969 range->ncprange_ip4mask.s_addr = INADDR_BROADCAST;
970 range->ncprange_ip4width = 32;
971 } else if (bits > 32) {
972 log_Printf(LogWARN, "ncprange_aton: bad mask width.\n");
975 range->ncprange_ip4mask = bits2mask4(bits);
976 range->ncprange_ip4width = bits;
981 } else if (strchr(cp + 1, ':') != NULL) {
982 range->ncprange_family = AF_INET6;
984 if (inet_pton(AF_INET6, s, &range->ncprange_ip6addr) != 1) {
985 log_Printf(LogWARN, "ncprange_aton: %s: Bad address\n", s);
989 if (IN6_IS_ADDR_UNSPECIFIED(&range->ncprange_ip6addr))
990 range->ncprange_ip6width = 0;
992 range->ncprange_ip6width = (bits == -1) ? 128 : bits;