blob: f4bb8a2a45e0edd9b1eb848bdaefccd4b87f37d8 [file] [log] [blame]
Harald Welted12eab92017-08-02 19:49:47 +02001/*
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 Pedrolfdd732b2017-10-13 14:32:24 +02009 *
Harald Welted12eab92017-08-02 19:49:47 +020010 */
11
12#include "../lib/in46_addr.h"
Harald Weltecee75462017-09-24 17:45:05 +080013#include "../gtp/pdp.h"
Harald Welted12eab92017-08-02 19:49:47 +020014
Harald Welte72a38b52017-08-09 21:58:12 +020015#include <osmocom/core/utils.h>
16
Harald Welted12eab92017-08-02 19:49:47 +020017#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 */
26int in46a_to_af(const struct in46_addr *in)
27{
28 switch (in->len) {
29 case 4:
30 return AF_INET;
Harald Welted4d6e092017-08-08 18:10:43 +020031 case 8:
Harald Welted12eab92017-08-02 19:49:47 +020032 case 16:
33 return AF_INET6;
34 default:
Harald Welte72a38b52017-08-09 21:58:12 +020035 OSMO_ASSERT(0);
Harald Welted12eab92017-08-02 19:49:47 +020036 return -1;
37 }
38}
39
40/*! Convert \ref in46_addr to sockaddr_storage */
41int 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 Welte34a74162017-10-13 16:24:59 +020052 sin6->sin6_family = AF_INET6;
Harald Welted12eab92017-08-02 19:49:47 +020053 sin6->sin6_addr = in->v6;
54 break;
55 default:
Harald Welte72a38b52017-08-09 21:58:12 +020056 OSMO_ASSERT(0);
Harald Welted12eab92017-08-02 19:49:47 +020057 return -1;
58 }
59
60 return 0;
61}
62
63/*! Convenience wrapper around inet_ntop() for \ref in46_addr */
64const char *in46a_ntop(const struct in46_addr *in, char *dst, socklen_t dst_size)
65{
Harald Welte33520b42017-08-12 14:54:28 +020066 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 Welted12eab92017-08-02 19:49:47 +020074 if (af < 0)
75 return NULL;
76
77 return inet_ntop(af, (const void *) &in->v4, dst, dst_size);
78}
79
Harald Welteb62983d2017-08-12 12:46:17 +020080/* like inet_ntoa() */
81const 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 Welte7fc86942017-08-12 12:55:48 +020090const 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 Welted12eab92017-08-02 19:49:47 +020097/*! Determine if two in46_addr are equal or not
98 * \returns 1 in case they are equal; 0 otherwise */
99int 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 Welte365f8fa2017-08-08 18:09:36 +0200107/*! 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 */
110int 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 Welted12eab92017-08-02 19:49:47 +0200124/*! Match if IPv6 addr1 + addr2 are within same \a mask */
125static 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 */
147static 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 */
174int 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 Welte72a38b52017-08-09 21:58:12 +0200193 OSMO_ASSERT(0);
Harald Welted12eab92017-08-02 19:49:47 +0200194 return 0;
195 }
196}
Harald Weltea0d281d2017-08-02 21:48:16 +0200197
Pau Espin Pedrol2e7b9ff2017-10-16 14:27:32 +0200198static 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
213static 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 */
243unsigned 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
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100256/*! Convert given array of in46_addr to PDP End User Address
257 * \param[in] src Array containing 1 or 2 in46_addr
258 * \param[out] eua End User Address structure to fill
259 * \returns 0 on success; negative on error
260 *
261 * In case size is 2, this function expects to find exactly one IPv4 and one
262 * IPv6 addresses in src. */
263int in46a_to_eua(const struct in46_addr *src, unsigned int size, struct ul66_t *eua)
Harald Weltea0d281d2017-08-02 21:48:16 +0200264{
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100265 const struct in46_addr *src_v4, *src_v6;
266 if (size == 1) {
267 switch (src->len) {
268 case 4:
269 eua->l = 6;
270 eua->v[0] = PDP_EUA_ORG_IETF;
271 eua->v[1] = PDP_EUA_TYPE_v4;
272 memcpy(&eua->v[2], &src->v4, 4); /* Copy a 4 byte address */
273 break;
274 case 8:
275 case 16:
276 eua->l = 18;
277 eua->v[0] = PDP_EUA_ORG_IETF;
278 eua->v[1] = PDP_EUA_TYPE_v6;
279 memcpy(&eua->v[2], &src->v6, 16); /* Copy a 16 byte address */
280 break;
281 default:
282 OSMO_ASSERT(0);
283 return -1;
284 }
285 return 0;
Harald Weltea0d281d2017-08-02 21:48:16 +0200286 }
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100287
288 if (src[0].len == src[1].len)
289 return -1; /* we should have a v4 and a v6 address */
290
291 src_v4 = (src[0].len == 4) ? &src[0] : &src[1];
292 src_v6 = (src[0].len == 4) ? &src[1] : &src[0];
293
294 eua->l = 22;
295 eua->v[0] = PDP_EUA_ORG_IETF;
296 eua->v[1] = PDP_EUA_TYPE_v4v6;
297 memcpy(&eua->v[2], &src_v4->v4, 4);
298 memcpy(&eua->v[6], &src_v6->v6, 16);
299
Harald Weltea0d281d2017-08-02 21:48:16 +0200300 return 0;
301}
302
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100303/*! Convert given PDP End User Address to an array of in46_addr
304 * \param[in] eua End User Address structure to parse
305 * \param[out] dst Array containing 2 in46_addr
306 * \returns number of parsed addresses (1 or 2) on success; negative on error
307 *
308 * This function expects to receive an End User Address struct together with an
309 * array of 2 zeroed in46_addr structs. The in46_addr structs are filled in
310 * order, hence if the function returns 1 the parsed address will be stored in
311 * the first struct and the second one will be left intact. If 2 is returned, it
312 * is guaranteed that one of them is an IPv4 and the other one is an IPv6, but
313 * the order in which they are presented is not specified and must be
314 * discovered for instance by checking the len field of each address.
315 */
Harald Weltea0d281d2017-08-02 21:48:16 +0200316int in46a_from_eua(const struct ul66_t *eua, struct in46_addr *dst)
317{
318 if (eua->l < 2)
319 goto default_to_dyn_v4;
320
321 if (eua->v[0] != 0xf1)
322 return -1;
323
324 switch (eua->v[1]) {
Harald Weltecee75462017-09-24 17:45:05 +0800325 case PDP_EUA_TYPE_v4:
Harald Weltea0d281d2017-08-02 21:48:16 +0200326 dst->len = 4;
327 if (eua->l >= 6)
328 memcpy(&dst->v4, &eua->v[2], 4); /* Copy a 4 byte address */
329 else
330 dst->v4.s_addr = 0;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100331 return 1;
Harald Weltecee75462017-09-24 17:45:05 +0800332 case PDP_EUA_TYPE_v6:
Harald Weltea0d281d2017-08-02 21:48:16 +0200333 dst->len = 16;
334 if (eua->l >= 18)
335 memcpy(&dst->v6, &eua->v[2], 16); /* Copy a 16 byte address */
336 else
337 memset(&dst->v6, 0, 16);
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100338 return 1;
339 case PDP_EUA_TYPE_v4v6:
340 /* 3GPP TS 29.060, section 7.7.27 */
341 switch (eua->l) {
342 case 2: /* v4 & v6 dynamic */
343 dst[0].v4.s_addr = 0;
344 memset(&dst[1].v6, 0, 16);
345 break;
346 case 6: /* v4 static, v6 dynamic */
347 memcpy(&dst[0].v4, &eua->v[2], 4);
348 memset(&dst[1].v6, 0, 16);
349 break;
350 case 18: /* v4 dynamic, v6 static */
351 dst[0].v4.s_addr = 0;
352 memcpy(&dst[1].v6, &eua->v[2], 16);
353 break;
354 case 22: /* v4 & v6 static */
355 memcpy(&dst[0].v4, &eua->v[2], 4);
356 memcpy(&dst[1].v6, &eua->v[6], 16);
357 break;
358 default:
359 return -1;
360 }
361 dst[0].len = 4;
362 dst[1].len = 16;
363 return 2;
Harald Weltea0d281d2017-08-02 21:48:16 +0200364 default:
365 return -1;
366 }
Harald Weltea0d281d2017-08-02 21:48:16 +0200367
368default_to_dyn_v4:
369 /* assume dynamic IPv4 by default */
370 dst->len = 4;
371 dst->v4.s_addr = 0;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100372 return 1;
Harald Weltea0d281d2017-08-02 21:48:16 +0200373}