blob: 36ad6afca08e2cd855b9c1a3e9fc82aec7422059 [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
Harald Weltea0d281d2017-08-02 21:48:16 +0200256/*! Convert given PDP End User Address to in46_addr
257 * \returns 0 on success; negative on error */
258int 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 Weltecee75462017-09-24 17:45:05 +0800263 eua->v[0] = PDP_EUA_ORG_IETF;
264 eua->v[1] = PDP_EUA_TYPE_v4;
Harald Weltea0d281d2017-08-02 21:48:16 +0200265 memcpy(&eua->v[2], &src->v4, 4); /* Copy a 4 byte address */
266 break;
Harald Welted4d6e092017-08-08 18:10:43 +0200267 case 8:
Harald Weltea0d281d2017-08-02 21:48:16 +0200268 case 16:
269 eua->l = 18;
Harald Weltecee75462017-09-24 17:45:05 +0800270 eua->v[0] = PDP_EUA_ORG_IETF;
271 eua->v[1] = PDP_EUA_TYPE_v6;
Harald Weltea0d281d2017-08-02 21:48:16 +0200272 memcpy(&eua->v[2], &src->v6, 16); /* Copy a 16 byte address */
273 break;
274 default:
Harald Welte72a38b52017-08-09 21:58:12 +0200275 OSMO_ASSERT(0);
Harald Weltea0d281d2017-08-02 21:48:16 +0200276 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 */
283int 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 Weltecee75462017-09-24 17:45:05 +0800292 case PDP_EUA_TYPE_v4:
Harald Weltea0d281d2017-08-02 21:48:16 +0200293 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 Weltecee75462017-09-24 17:45:05 +0800299 case PDP_EUA_TYPE_v6:
Harald Weltea0d281d2017-08-02 21:48:16 +0200300 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
311default_to_dyn_v4:
312 /* assume dynamic IPv4 by default */
313 dst->len = 4;
314 dst->v4.s_addr = 0;
315 return 0;
316}