blob: 2562c711a66ea3f1d99bb6fece7b59873a6b5967 [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
Pau Espin Pedrol95cd8972019-08-20 12:20:11 +020063/*! Convenience wrapper around inet_ntop() for in46_addr.
64 * \param[in] in the in46_addr to print
65 * \param[out] dst destination buffer where string representation of the address is stored
66 * \param[out] dst_size size dst. Usually it should be at least INET6_ADDRSTRLEN.
67 * \return address of dst on success, NULL on error */
Harald Welted12eab92017-08-02 19:49:47 +020068const char *in46a_ntop(const struct in46_addr *in, char *dst, socklen_t dst_size)
69{
Harald Welte33520b42017-08-12 14:54:28 +020070 int af;
71
72 if (!in || in->len == 0) {
73 strncpy(dst, "UNDEFINED", dst_size);
74 return dst;
75 }
76
77 af = in46a_to_af(in);
Harald Welted12eab92017-08-02 19:49:47 +020078 if (af < 0)
79 return NULL;
80
81 return inet_ntop(af, (const void *) &in->v4, dst, dst_size);
82}
83
Harald Welteb62983d2017-08-12 12:46:17 +020084/* like inet_ntoa() */
85const char *in46a_ntoa(const struct in46_addr *in46)
86{
87 static char addrstr_buf[256];
88 if (in46a_ntop(in46, addrstr_buf, sizeof(addrstr_buf)) < 0)
89 return "INVALID";
90 else
91 return addrstr_buf;
92}
93
Harald Welte7fc86942017-08-12 12:55:48 +020094const char *in46p_ntoa(const struct in46_prefix *in46p)
95{
96 static char addrstr_buf[256];
97 snprintf(addrstr_buf, sizeof(addrstr_buf), "%s/%u", in46a_ntoa(&in46p->addr), in46p->prefixlen);
98 return addrstr_buf;
99}
100
Harald Welted12eab92017-08-02 19:49:47 +0200101/*! Determine if two in46_addr are equal or not
102 * \returns 1 in case they are equal; 0 otherwise */
103int in46a_equal(const struct in46_addr *a, const struct in46_addr *b)
104{
105 if (a->len == b->len && !memcmp(&a->v6, &b->v6, a->len))
106 return 1;
107 else
108 return 0;
109}
110
Harald Welte365f8fa2017-08-08 18:09:36 +0200111/*! Determine if two in46_addr prefix are equal or not
112 * The prefix length is determined by the shortest of the prefixes of a and b
113 * \returns 1 in case the common prefix are equal; 0 otherwise */
114int in46a_prefix_equal(const struct in46_addr *a, const struct in46_addr *b)
115{
116 unsigned int len;
117 if (a->len > b->len)
118 len = b->len;
119 else
120 len = a->len;
121
122 if (!memcmp(&a->v6, &b->v6, len))
123 return 1;
124 else
125 return 0;
126}
127
Harald Welted12eab92017-08-02 19:49:47 +0200128/*! Match if IPv6 addr1 + addr2 are within same \a mask */
129static int ipv6_within_mask(const struct in6_addr *addr1, const struct in6_addr *addr2,
130 const struct in6_addr *mask)
131{
132 struct in6_addr masked = *addr2;
133#if defined(__linux__)
134 masked.s6_addr32[0] &= mask->s6_addr32[0];
135 masked.s6_addr32[1] &= mask->s6_addr32[1];
136 masked.s6_addr32[2] &= mask->s6_addr32[2];
137 masked.s6_addr32[3] &= mask->s6_addr32[3];
138#else
139 masked.__u6_addr.__u6_addr32[0] &= mask->__u6_addr.__u6_addr32[0];
140 masked.__u6_addr.__u6_addr32[1] &= mask->__u6_addr.__u6_addr32[1];
141 masked.__u6_addr.__u6_addr32[2] &= mask->__u6_addr.__u6_addr32[2];
142 masked.__u6_addr.__u6_addr32[3] &= mask->__u6_addr.__u6_addr32[3];
143#endif
144 if (!memcmp(addr1, &masked, sizeof(struct in6_addr)))
145 return 1;
146 else
147 return 0;
148}
149
150/*! Create an IPv6 netmask from the given prefix length */
151static void create_ipv6_netmask(struct in6_addr *netmask, int prefixlen)
152{
153 uint32_t *p_netmask;
154 memset(netmask, 0, sizeof(struct in6_addr));
155 if (prefixlen < 0)
156 prefixlen = 0;
157 else if (128 < prefixlen)
158 prefixlen = 128;
159
160#if defined(__linux__)
161 p_netmask = &netmask->s6_addr32[0];
162#else
163 p_netmask = &netmask->__u6_addr.__u6_addr32[0];
164#endif
165 while (32 < prefixlen) {
166 *p_netmask = 0xffffffff;
167 p_netmask++;
168 prefixlen -= 32;
169 }
170 if (prefixlen != 0) {
171 *p_netmask = htonl(0xFFFFFFFF << (32 - prefixlen));
172 }
173}
174
175/*! Determine if given \a addr is within given \a net + \a prefixlen
176 * Builds the netmask from \a net + \a prefixlen and matches it to \a addr
177 * \returns 1 in case of a match, 0 otherwise */
178int in46a_within_mask(const struct in46_addr *addr, const struct in46_addr *net, size_t prefixlen)
179{
180 struct in_addr netmask;
181 struct in6_addr netmask6;
182
183 if (addr->len != net->len)
184 return 0;
185
186 switch (addr->len) {
187 case 4:
188 netmask.s_addr = htonl(0xFFFFFFFF << (32 - prefixlen));
189 if ((addr->v4.s_addr & netmask.s_addr) == net->v4.s_addr)
190 return 1;
191 else
192 return 0;
193 case 16:
194 create_ipv6_netmask(&netmask6, prefixlen);
195 return ipv6_within_mask(&addr->v6, &net->v6, &netmask6);
196 default:
Harald Welte72a38b52017-08-09 21:58:12 +0200197 OSMO_ASSERT(0);
Harald Welted12eab92017-08-02 19:49:47 +0200198 return 0;
199 }
200}
Harald Weltea0d281d2017-08-02 21:48:16 +0200201
Pau Espin Pedrol2e7b9ff2017-10-16 14:27:32 +0200202static unsigned int ipv4_netmasklen(const struct in_addr *netmask)
203{
204 uint32_t bits = netmask->s_addr;
205 uint8_t *b = (uint8_t*) &bits;
206 unsigned int i, prefix = 0;
207
208 for (i = 0; i < 4; i++) {
209 while (b[i] & 0x80) {
210 prefix++;
211 b[i] = b[i] << 1;
212 }
213 }
214 return prefix;
215}
216
217static unsigned int ipv6_netmasklen(const struct in6_addr *netmask)
218{
219 #if defined(__linux__)
220 #define ADDRFIELD(i) s6_addr32[i]
221 #else
222 #define ADDRFIELD(i) __u6_addr.__u6_addr32[i]
223 #endif
224
225 unsigned int i, j, prefix = 0;
226
227 for (j = 0; j < 4; j++) {
228 uint32_t bits = netmask->ADDRFIELD(j);
229 uint8_t *b = (uint8_t*) &bits;
230 for (i = 0; i < 4; i++) {
231 while (b[i] & 0x80) {
232 prefix++;
233 b[i] = b[i] << 1;
234 }
235 }
236 }
237
238 #undef ADDRFIELD
239
240 return prefix;
241}
242
243/*! Convert netmask to prefix length representation
244 * \param[in] netmask in46_addr containing a netmask (consecutive list of 1-bit followed by consecutive list of 0-bit)
245 * \returns prefix length representation of the netmask (count of 1-bit from the start of the netmask)
246 */
247unsigned int in46a_netmasklen(const struct in46_addr *netmask)
248{
249 switch (netmask->len) {
250 case 4:
251 return ipv4_netmasklen(&netmask->v4);
252 case 16:
253 return ipv6_netmasklen(&netmask->v6);
254 default:
255 OSMO_ASSERT(0);
256 return 0;
257 }
258}
259
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100260/*! Convert given array of in46_addr to PDP End User Address
261 * \param[in] src Array containing 1 or 2 in46_addr
262 * \param[out] eua End User Address structure to fill
263 * \returns 0 on success; negative on error
264 *
265 * In case size is 2, this function expects to find exactly one IPv4 and one
266 * IPv6 addresses in src. */
267int in46a_to_eua(const struct in46_addr *src, unsigned int size, struct ul66_t *eua)
Harald Weltea0d281d2017-08-02 21:48:16 +0200268{
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100269 const struct in46_addr *src_v4, *src_v6;
270 if (size == 1) {
271 switch (src->len) {
272 case 4:
273 eua->l = 6;
274 eua->v[0] = PDP_EUA_ORG_IETF;
275 eua->v[1] = PDP_EUA_TYPE_v4;
276 memcpy(&eua->v[2], &src->v4, 4); /* Copy a 4 byte address */
277 break;
278 case 8:
279 case 16:
280 eua->l = 18;
281 eua->v[0] = PDP_EUA_ORG_IETF;
282 eua->v[1] = PDP_EUA_TYPE_v6;
283 memcpy(&eua->v[2], &src->v6, 16); /* Copy a 16 byte address */
284 break;
285 default:
286 OSMO_ASSERT(0);
287 return -1;
288 }
289 return 0;
Harald Weltea0d281d2017-08-02 21:48:16 +0200290 }
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100291
292 if (src[0].len == src[1].len)
293 return -1; /* we should have a v4 and a v6 address */
294
295 src_v4 = (src[0].len == 4) ? &src[0] : &src[1];
296 src_v6 = (src[0].len == 4) ? &src[1] : &src[0];
297
298 eua->l = 22;
299 eua->v[0] = PDP_EUA_ORG_IETF;
300 eua->v[1] = PDP_EUA_TYPE_v4v6;
301 memcpy(&eua->v[2], &src_v4->v4, 4);
302 memcpy(&eua->v[6], &src_v6->v6, 16);
303
Harald Weltea0d281d2017-08-02 21:48:16 +0200304 return 0;
305}
306
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100307/*! Convert given PDP End User Address to an array of in46_addr
308 * \param[in] eua End User Address structure to parse
309 * \param[out] dst Array containing 2 in46_addr
310 * \returns number of parsed addresses (1 or 2) on success; negative on error
311 *
312 * This function expects to receive an End User Address struct together with an
313 * array of 2 zeroed in46_addr structs. The in46_addr structs are filled in
314 * order, hence if the function returns 1 the parsed address will be stored in
315 * the first struct and the second one will be left intact. If 2 is returned, it
316 * is guaranteed that one of them is an IPv4 and the other one is an IPv6, but
317 * the order in which they are presented is not specified and must be
318 * discovered for instance by checking the len field of each address.
319 */
Harald Weltea0d281d2017-08-02 21:48:16 +0200320int in46a_from_eua(const struct ul66_t *eua, struct in46_addr *dst)
321{
322 if (eua->l < 2)
323 goto default_to_dyn_v4;
324
325 if (eua->v[0] != 0xf1)
326 return -1;
327
328 switch (eua->v[1]) {
Harald Weltecee75462017-09-24 17:45:05 +0800329 case PDP_EUA_TYPE_v4:
Harald Weltea0d281d2017-08-02 21:48:16 +0200330 dst->len = 4;
331 if (eua->l >= 6)
332 memcpy(&dst->v4, &eua->v[2], 4); /* Copy a 4 byte address */
333 else
334 dst->v4.s_addr = 0;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100335 return 1;
Harald Weltecee75462017-09-24 17:45:05 +0800336 case PDP_EUA_TYPE_v6:
Harald Weltea0d281d2017-08-02 21:48:16 +0200337 dst->len = 16;
338 if (eua->l >= 18)
339 memcpy(&dst->v6, &eua->v[2], 16); /* Copy a 16 byte address */
340 else
341 memset(&dst->v6, 0, 16);
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100342 return 1;
343 case PDP_EUA_TYPE_v4v6:
344 /* 3GPP TS 29.060, section 7.7.27 */
345 switch (eua->l) {
346 case 2: /* v4 & v6 dynamic */
347 dst[0].v4.s_addr = 0;
348 memset(&dst[1].v6, 0, 16);
349 break;
350 case 6: /* v4 static, v6 dynamic */
351 memcpy(&dst[0].v4, &eua->v[2], 4);
352 memset(&dst[1].v6, 0, 16);
353 break;
354 case 18: /* v4 dynamic, v6 static */
355 dst[0].v4.s_addr = 0;
356 memcpy(&dst[1].v6, &eua->v[2], 16);
357 break;
358 case 22: /* v4 & v6 static */
359 memcpy(&dst[0].v4, &eua->v[2], 4);
360 memcpy(&dst[1].v6, &eua->v[6], 16);
361 break;
362 default:
363 return -1;
364 }
365 dst[0].len = 4;
366 dst[1].len = 16;
367 return 2;
Harald Weltea0d281d2017-08-02 21:48:16 +0200368 default:
369 return -1;
370 }
Harald Weltea0d281d2017-08-02 21:48:16 +0200371
372default_to_dyn_v4:
373 /* assume dynamic IPv4 by default */
374 dst->len = 4;
375 dst->v4.s_addr = 0;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100376 return 1;
Harald Weltea0d281d2017-08-02 21:48:16 +0200377}