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. |
Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 9 | * |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "../lib/in46_addr.h" |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 13 | #include "../gtp/pdp.h" |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 14 | |
Harald Welte | 72a38b5 | 2017-08-09 21:58:12 +0200 | [diff] [blame] | 15 | #include <osmocom/core/utils.h> |
| 16 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 17 | #include <sys/types.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <arpa/inet.h> |
| 21 | #include <netdb.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | /*! Return the address family of given \reff in46_addr argument */ |
| 26 | int in46a_to_af(const struct in46_addr *in) |
| 27 | { |
| 28 | switch (in->len) { |
| 29 | case 4: |
| 30 | return AF_INET; |
Harald Welte | d4d6e09 | 2017-08-08 18:10:43 +0200 | [diff] [blame] | 31 | case 8: |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 32 | case 16: |
| 33 | return AF_INET6; |
| 34 | default: |
Harald Welte | 72a38b5 | 2017-08-09 21:58:12 +0200 | [diff] [blame] | 35 | OSMO_ASSERT(0); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 36 | return -1; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /*! Convert \ref in46_addr to sockaddr_storage */ |
| 41 | int in46a_to_sas(struct sockaddr_storage *out, const struct in46_addr *in) |
| 42 | { |
| 43 | struct sockaddr_in *sin = (struct sockaddr_in *)out; |
| 44 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)out; |
| 45 | |
| 46 | switch (in->len) { |
| 47 | case 4: |
| 48 | sin->sin_family = AF_INET; |
| 49 | sin->sin_addr = in->v4; |
| 50 | break; |
| 51 | case 16: |
Harald Welte | 34a7416 | 2017-10-13 16:24:59 +0200 | [diff] [blame] | 52 | sin6->sin6_family = AF_INET6; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 53 | sin6->sin6_addr = in->v6; |
| 54 | break; |
| 55 | default: |
Harald Welte | 72a38b5 | 2017-08-09 21:58:12 +0200 | [diff] [blame] | 56 | OSMO_ASSERT(0); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /*! Convenience wrapper around inet_ntop() for \ref in46_addr */ |
| 64 | const char *in46a_ntop(const struct in46_addr *in, char *dst, socklen_t dst_size) |
| 65 | { |
Harald Welte | 33520b4 | 2017-08-12 14:54:28 +0200 | [diff] [blame] | 66 | int af; |
| 67 | |
| 68 | if (!in || in->len == 0) { |
| 69 | strncpy(dst, "UNDEFINED", dst_size); |
| 70 | return dst; |
| 71 | } |
| 72 | |
| 73 | af = in46a_to_af(in); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 74 | if (af < 0) |
| 75 | return NULL; |
| 76 | |
| 77 | return inet_ntop(af, (const void *) &in->v4, dst, dst_size); |
| 78 | } |
| 79 | |
Harald Welte | b62983d | 2017-08-12 12:46:17 +0200 | [diff] [blame] | 80 | /* like inet_ntoa() */ |
| 81 | const char *in46a_ntoa(const struct in46_addr *in46) |
| 82 | { |
| 83 | static char addrstr_buf[256]; |
| 84 | if (in46a_ntop(in46, addrstr_buf, sizeof(addrstr_buf)) < 0) |
| 85 | return "INVALID"; |
| 86 | else |
| 87 | return addrstr_buf; |
| 88 | } |
| 89 | |
Harald Welte | 7fc8694 | 2017-08-12 12:55:48 +0200 | [diff] [blame] | 90 | const char *in46p_ntoa(const struct in46_prefix *in46p) |
| 91 | { |
| 92 | static char addrstr_buf[256]; |
| 93 | snprintf(addrstr_buf, sizeof(addrstr_buf), "%s/%u", in46a_ntoa(&in46p->addr), in46p->prefixlen); |
| 94 | return addrstr_buf; |
| 95 | } |
| 96 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 97 | /*! Determine if two in46_addr are equal or not |
| 98 | * \returns 1 in case they are equal; 0 otherwise */ |
| 99 | int in46a_equal(const struct in46_addr *a, const struct in46_addr *b) |
| 100 | { |
| 101 | if (a->len == b->len && !memcmp(&a->v6, &b->v6, a->len)) |
| 102 | return 1; |
| 103 | else |
| 104 | return 0; |
| 105 | } |
| 106 | |
Harald Welte | 365f8fa | 2017-08-08 18:09:36 +0200 | [diff] [blame] | 107 | /*! Determine if two in46_addr prefix are equal or not |
| 108 | * The prefix length is determined by the shortest of the prefixes of a and b |
| 109 | * \returns 1 in case the common prefix are equal; 0 otherwise */ |
| 110 | int in46a_prefix_equal(const struct in46_addr *a, const struct in46_addr *b) |
| 111 | { |
| 112 | unsigned int len; |
| 113 | if (a->len > b->len) |
| 114 | len = b->len; |
| 115 | else |
| 116 | len = a->len; |
| 117 | |
| 118 | if (!memcmp(&a->v6, &b->v6, len)) |
| 119 | return 1; |
| 120 | else |
| 121 | return 0; |
| 122 | } |
| 123 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 124 | /*! Match if IPv6 addr1 + addr2 are within same \a mask */ |
| 125 | static int ipv6_within_mask(const struct in6_addr *addr1, const struct in6_addr *addr2, |
| 126 | const struct in6_addr *mask) |
| 127 | { |
| 128 | struct in6_addr masked = *addr2; |
| 129 | #if defined(__linux__) |
| 130 | masked.s6_addr32[0] &= mask->s6_addr32[0]; |
| 131 | masked.s6_addr32[1] &= mask->s6_addr32[1]; |
| 132 | masked.s6_addr32[2] &= mask->s6_addr32[2]; |
| 133 | masked.s6_addr32[3] &= mask->s6_addr32[3]; |
| 134 | #else |
| 135 | masked.__u6_addr.__u6_addr32[0] &= mask->__u6_addr.__u6_addr32[0]; |
| 136 | masked.__u6_addr.__u6_addr32[1] &= mask->__u6_addr.__u6_addr32[1]; |
| 137 | masked.__u6_addr.__u6_addr32[2] &= mask->__u6_addr.__u6_addr32[2]; |
| 138 | masked.__u6_addr.__u6_addr32[3] &= mask->__u6_addr.__u6_addr32[3]; |
| 139 | #endif |
| 140 | if (!memcmp(addr1, &masked, sizeof(struct in6_addr))) |
| 141 | return 1; |
| 142 | else |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /*! Create an IPv6 netmask from the given prefix length */ |
| 147 | static void create_ipv6_netmask(struct in6_addr *netmask, int prefixlen) |
| 148 | { |
| 149 | uint32_t *p_netmask; |
| 150 | memset(netmask, 0, sizeof(struct in6_addr)); |
| 151 | if (prefixlen < 0) |
| 152 | prefixlen = 0; |
| 153 | else if (128 < prefixlen) |
| 154 | prefixlen = 128; |
| 155 | |
| 156 | #if defined(__linux__) |
| 157 | p_netmask = &netmask->s6_addr32[0]; |
| 158 | #else |
| 159 | p_netmask = &netmask->__u6_addr.__u6_addr32[0]; |
| 160 | #endif |
| 161 | while (32 < prefixlen) { |
| 162 | *p_netmask = 0xffffffff; |
| 163 | p_netmask++; |
| 164 | prefixlen -= 32; |
| 165 | } |
| 166 | if (prefixlen != 0) { |
| 167 | *p_netmask = htonl(0xFFFFFFFF << (32 - prefixlen)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /*! Determine if given \a addr is within given \a net + \a prefixlen |
| 172 | * Builds the netmask from \a net + \a prefixlen and matches it to \a addr |
| 173 | * \returns 1 in case of a match, 0 otherwise */ |
| 174 | int in46a_within_mask(const struct in46_addr *addr, const struct in46_addr *net, size_t prefixlen) |
| 175 | { |
| 176 | struct in_addr netmask; |
| 177 | struct in6_addr netmask6; |
| 178 | |
| 179 | if (addr->len != net->len) |
| 180 | return 0; |
| 181 | |
| 182 | switch (addr->len) { |
| 183 | case 4: |
| 184 | netmask.s_addr = htonl(0xFFFFFFFF << (32 - prefixlen)); |
| 185 | if ((addr->v4.s_addr & netmask.s_addr) == net->v4.s_addr) |
| 186 | return 1; |
| 187 | else |
| 188 | return 0; |
| 189 | case 16: |
| 190 | create_ipv6_netmask(&netmask6, prefixlen); |
| 191 | return ipv6_within_mask(&addr->v6, &net->v6, &netmask6); |
| 192 | default: |
Harald Welte | 72a38b5 | 2017-08-09 21:58:12 +0200 | [diff] [blame] | 193 | OSMO_ASSERT(0); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | } |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 197 | |
Pau Espin Pedrol | 2e7b9ff | 2017-10-16 14:27:32 +0200 | [diff] [blame] | 198 | static unsigned int ipv4_netmasklen(const struct in_addr *netmask) |
| 199 | { |
| 200 | uint32_t bits = netmask->s_addr; |
| 201 | uint8_t *b = (uint8_t*) &bits; |
| 202 | unsigned int i, prefix = 0; |
| 203 | |
| 204 | for (i = 0; i < 4; i++) { |
| 205 | while (b[i] & 0x80) { |
| 206 | prefix++; |
| 207 | b[i] = b[i] << 1; |
| 208 | } |
| 209 | } |
| 210 | return prefix; |
| 211 | } |
| 212 | |
| 213 | static unsigned int ipv6_netmasklen(const struct in6_addr *netmask) |
| 214 | { |
| 215 | #if defined(__linux__) |
| 216 | #define ADDRFIELD(i) s6_addr32[i] |
| 217 | #else |
| 218 | #define ADDRFIELD(i) __u6_addr.__u6_addr32[i] |
| 219 | #endif |
| 220 | |
| 221 | unsigned int i, j, prefix = 0; |
| 222 | |
| 223 | for (j = 0; j < 4; j++) { |
| 224 | uint32_t bits = netmask->ADDRFIELD(j); |
| 225 | uint8_t *b = (uint8_t*) &bits; |
| 226 | for (i = 0; i < 4; i++) { |
| 227 | while (b[i] & 0x80) { |
| 228 | prefix++; |
| 229 | b[i] = b[i] << 1; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | #undef ADDRFIELD |
| 235 | |
| 236 | return prefix; |
| 237 | } |
| 238 | |
| 239 | /*! Convert netmask to prefix length representation |
| 240 | * \param[in] netmask in46_addr containing a netmask (consecutive list of 1-bit followed by consecutive list of 0-bit) |
| 241 | * \returns prefix length representation of the netmask (count of 1-bit from the start of the netmask) |
| 242 | */ |
| 243 | unsigned int in46a_netmasklen(const struct in46_addr *netmask) |
| 244 | { |
| 245 | switch (netmask->len) { |
| 246 | case 4: |
| 247 | return ipv4_netmasklen(&netmask->v4); |
| 248 | case 16: |
| 249 | return ipv6_netmasklen(&netmask->v6); |
| 250 | default: |
| 251 | OSMO_ASSERT(0); |
| 252 | return 0; |
| 253 | } |
| 254 | } |
| 255 | |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 256 | /*! Convert given PDP End User Address to in46_addr |
| 257 | * \returns 0 on success; negative on error */ |
| 258 | int in46a_to_eua(const struct in46_addr *src, struct ul66_t *eua) |
| 259 | { |
| 260 | switch (src->len) { |
| 261 | case 4: |
| 262 | eua->l = 6; |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 263 | eua->v[0] = PDP_EUA_ORG_IETF; |
| 264 | eua->v[1] = PDP_EUA_TYPE_v4; |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 265 | memcpy(&eua->v[2], &src->v4, 4); /* Copy a 4 byte address */ |
| 266 | break; |
Harald Welte | d4d6e09 | 2017-08-08 18:10:43 +0200 | [diff] [blame] | 267 | case 8: |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 268 | case 16: |
| 269 | eua->l = 18; |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 270 | eua->v[0] = PDP_EUA_ORG_IETF; |
| 271 | eua->v[1] = PDP_EUA_TYPE_v6; |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 272 | memcpy(&eua->v[2], &src->v6, 16); /* Copy a 16 byte address */ |
| 273 | break; |
| 274 | default: |
Harald Welte | 72a38b5 | 2017-08-09 21:58:12 +0200 | [diff] [blame] | 275 | OSMO_ASSERT(0); |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | /*! Convert given in46_addr to PDP End User Address |
| 282 | * \returns 0 on success; negative on error */ |
| 283 | int in46a_from_eua(const struct ul66_t *eua, struct in46_addr *dst) |
| 284 | { |
| 285 | if (eua->l < 2) |
| 286 | goto default_to_dyn_v4; |
| 287 | |
| 288 | if (eua->v[0] != 0xf1) |
| 289 | return -1; |
| 290 | |
| 291 | switch (eua->v[1]) { |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 292 | case PDP_EUA_TYPE_v4: |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 293 | dst->len = 4; |
| 294 | if (eua->l >= 6) |
| 295 | memcpy(&dst->v4, &eua->v[2], 4); /* Copy a 4 byte address */ |
| 296 | else |
| 297 | dst->v4.s_addr = 0; |
| 298 | break; |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 299 | case PDP_EUA_TYPE_v6: |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 300 | dst->len = 16; |
| 301 | if (eua->l >= 18) |
| 302 | memcpy(&dst->v6, &eua->v[2], 16); /* Copy a 16 byte address */ |
| 303 | else |
| 304 | memset(&dst->v6, 0, 16); |
| 305 | break; |
| 306 | default: |
| 307 | return -1; |
| 308 | } |
| 309 | return 0; |
| 310 | |
| 311 | default_to_dyn_v4: |
| 312 | /* assume dynamic IPv4 by default */ |
| 313 | dst->len = 4; |
| 314 | dst->v4.s_addr = 0; |
| 315 | return 0; |
| 316 | } |