blob: 068499c7550129435370ef605281b738eeeeee3b [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:
52 sin6->sin6_family = AF_INET;
53 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
198/*! Convert given PDP End User Address to in46_addr
199 * \returns 0 on success; negative on error */
200int in46a_to_eua(const struct in46_addr *src, struct ul66_t *eua)
201{
202 switch (src->len) {
203 case 4:
204 eua->l = 6;
Harald Weltecee75462017-09-24 17:45:05 +0800205 eua->v[0] = PDP_EUA_ORG_IETF;
206 eua->v[1] = PDP_EUA_TYPE_v4;
Harald Weltea0d281d2017-08-02 21:48:16 +0200207 memcpy(&eua->v[2], &src->v4, 4); /* Copy a 4 byte address */
208 break;
Harald Welted4d6e092017-08-08 18:10:43 +0200209 case 8:
Harald Weltea0d281d2017-08-02 21:48:16 +0200210 case 16:
211 eua->l = 18;
Harald Weltecee75462017-09-24 17:45:05 +0800212 eua->v[0] = PDP_EUA_ORG_IETF;
213 eua->v[1] = PDP_EUA_TYPE_v6;
Harald Weltea0d281d2017-08-02 21:48:16 +0200214 memcpy(&eua->v[2], &src->v6, 16); /* Copy a 16 byte address */
215 break;
216 default:
Harald Welte72a38b52017-08-09 21:58:12 +0200217 OSMO_ASSERT(0);
Harald Weltea0d281d2017-08-02 21:48:16 +0200218 return -1;
219 }
220 return 0;
221}
222
223/*! Convert given in46_addr to PDP End User Address
224 * \returns 0 on success; negative on error */
225int in46a_from_eua(const struct ul66_t *eua, struct in46_addr *dst)
226{
227 if (eua->l < 2)
228 goto default_to_dyn_v4;
229
230 if (eua->v[0] != 0xf1)
231 return -1;
232
233 switch (eua->v[1]) {
Harald Weltecee75462017-09-24 17:45:05 +0800234 case PDP_EUA_TYPE_v4:
Harald Weltea0d281d2017-08-02 21:48:16 +0200235 dst->len = 4;
236 if (eua->l >= 6)
237 memcpy(&dst->v4, &eua->v[2], 4); /* Copy a 4 byte address */
238 else
239 dst->v4.s_addr = 0;
240 break;
Harald Weltecee75462017-09-24 17:45:05 +0800241 case PDP_EUA_TYPE_v6:
Harald Weltea0d281d2017-08-02 21:48:16 +0200242 dst->len = 16;
243 if (eua->l >= 18)
244 memcpy(&dst->v6, &eua->v[2], 16); /* Copy a 16 byte address */
245 else
246 memset(&dst->v6, 0, 16);
247 break;
248 default:
249 return -1;
250 }
251 return 0;
252
253default_to_dyn_v4:
254 /* assume dynamic IPv4 by default */
255 dst->len = 4;
256 dst->v4.s_addr = 0;
257 return 0;
258}