Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * IPv4/v6 address functions. |
| 3 | * Copyright (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * The contents of this file may be used under the terms of the GNU |
| 6 | * General Public License Version 2, provided that the above copyright |
| 7 | * notice and this permission notice is included in all copies or |
| 8 | * substantial portions of the software. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include "../lib/in46_addr.h" |
| 13 | |
| 14 | #include <sys/types.h> |
| 15 | #include <netinet/in.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <arpa/inet.h> |
| 18 | #include <netdb.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | /*! Return the address family of given \reff in46_addr argument */ |
| 23 | int in46a_to_af(const struct in46_addr *in) |
| 24 | { |
| 25 | switch (in->len) { |
| 26 | case 4: |
| 27 | return AF_INET; |
| 28 | case 16: |
| 29 | return AF_INET6; |
| 30 | default: |
| 31 | return -1; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /*! Convert \ref in46_addr to sockaddr_storage */ |
| 36 | int in46a_to_sas(struct sockaddr_storage *out, const struct in46_addr *in) |
| 37 | { |
| 38 | struct sockaddr_in *sin = (struct sockaddr_in *)out; |
| 39 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)out; |
| 40 | |
| 41 | switch (in->len) { |
| 42 | case 4: |
| 43 | sin->sin_family = AF_INET; |
| 44 | sin->sin_addr = in->v4; |
| 45 | break; |
| 46 | case 16: |
| 47 | sin6->sin6_family = AF_INET; |
| 48 | sin6->sin6_addr = in->v6; |
| 49 | break; |
| 50 | default: |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /*! Convenience wrapper around inet_ntop() for \ref in46_addr */ |
| 58 | const char *in46a_ntop(const struct in46_addr *in, char *dst, socklen_t dst_size) |
| 59 | { |
| 60 | int af = in46a_to_af(in); |
| 61 | if (af < 0) |
| 62 | return NULL; |
| 63 | |
| 64 | return inet_ntop(af, (const void *) &in->v4, dst, dst_size); |
| 65 | } |
| 66 | |
| 67 | /*! Determine if two in46_addr are equal or not |
| 68 | * \returns 1 in case they are equal; 0 otherwise */ |
| 69 | int in46a_equal(const struct in46_addr *a, const struct in46_addr *b) |
| 70 | { |
| 71 | if (a->len == b->len && !memcmp(&a->v6, &b->v6, a->len)) |
| 72 | return 1; |
| 73 | else |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /*! Match if IPv6 addr1 + addr2 are within same \a mask */ |
| 78 | static int ipv6_within_mask(const struct in6_addr *addr1, const struct in6_addr *addr2, |
| 79 | const struct in6_addr *mask) |
| 80 | { |
| 81 | struct in6_addr masked = *addr2; |
| 82 | #if defined(__linux__) |
| 83 | masked.s6_addr32[0] &= mask->s6_addr32[0]; |
| 84 | masked.s6_addr32[1] &= mask->s6_addr32[1]; |
| 85 | masked.s6_addr32[2] &= mask->s6_addr32[2]; |
| 86 | masked.s6_addr32[3] &= mask->s6_addr32[3]; |
| 87 | #else |
| 88 | masked.__u6_addr.__u6_addr32[0] &= mask->__u6_addr.__u6_addr32[0]; |
| 89 | masked.__u6_addr.__u6_addr32[1] &= mask->__u6_addr.__u6_addr32[1]; |
| 90 | masked.__u6_addr.__u6_addr32[2] &= mask->__u6_addr.__u6_addr32[2]; |
| 91 | masked.__u6_addr.__u6_addr32[3] &= mask->__u6_addr.__u6_addr32[3]; |
| 92 | #endif |
| 93 | if (!memcmp(addr1, &masked, sizeof(struct in6_addr))) |
| 94 | return 1; |
| 95 | else |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /*! Create an IPv6 netmask from the given prefix length */ |
| 100 | static void create_ipv6_netmask(struct in6_addr *netmask, int prefixlen) |
| 101 | { |
| 102 | uint32_t *p_netmask; |
| 103 | memset(netmask, 0, sizeof(struct in6_addr)); |
| 104 | if (prefixlen < 0) |
| 105 | prefixlen = 0; |
| 106 | else if (128 < prefixlen) |
| 107 | prefixlen = 128; |
| 108 | |
| 109 | #if defined(__linux__) |
| 110 | p_netmask = &netmask->s6_addr32[0]; |
| 111 | #else |
| 112 | p_netmask = &netmask->__u6_addr.__u6_addr32[0]; |
| 113 | #endif |
| 114 | while (32 < prefixlen) { |
| 115 | *p_netmask = 0xffffffff; |
| 116 | p_netmask++; |
| 117 | prefixlen -= 32; |
| 118 | } |
| 119 | if (prefixlen != 0) { |
| 120 | *p_netmask = htonl(0xFFFFFFFF << (32 - prefixlen)); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /*! Determine if given \a addr is within given \a net + \a prefixlen |
| 125 | * Builds the netmask from \a net + \a prefixlen and matches it to \a addr |
| 126 | * \returns 1 in case of a match, 0 otherwise */ |
| 127 | int in46a_within_mask(const struct in46_addr *addr, const struct in46_addr *net, size_t prefixlen) |
| 128 | { |
| 129 | struct in_addr netmask; |
| 130 | struct in6_addr netmask6; |
| 131 | |
| 132 | if (addr->len != net->len) |
| 133 | return 0; |
| 134 | |
| 135 | switch (addr->len) { |
| 136 | case 4: |
| 137 | netmask.s_addr = htonl(0xFFFFFFFF << (32 - prefixlen)); |
| 138 | if ((addr->v4.s_addr & netmask.s_addr) == net->v4.s_addr) |
| 139 | return 1; |
| 140 | else |
| 141 | return 0; |
| 142 | case 16: |
| 143 | create_ipv6_netmask(&netmask6, prefixlen); |
| 144 | return ipv6_within_mask(&addr->v6, &net->v6, &netmask6); |
| 145 | default: |
| 146 | return 0; |
| 147 | } |
| 148 | } |