1 /*
2  *	BIRD Internet Routing Daemon -- The Internet Protocol
3  *
4  *	(c) 1998 Martin Mares <mj@ucw.cz>
5  *
6  *	Can be freely distributed and used under the terms of the GNU GPL.
7  */
8 
9 #ifndef _BIRD_IP_H_
10 #define _BIRD_IP_H_
11 
12 #include "sysdep/unix/endian.h"
13 #include "lib/string.h"
14 #include "lib/bitops.h"
15 #include "lib/unaligned.h"
16 
17 
18 #define IP4_ALL_NODES		ipa_build4(224, 0, 0, 1)
19 #define IP4_ALL_ROUTERS		ipa_build4(224, 0, 0, 2)
20 #define IP4_OSPF_ALL_ROUTERS	ipa_build4(224, 0, 0, 5)
21 #define IP4_OSPF_DES_ROUTERS	ipa_build4(224, 0, 0, 6)
22 #define IP4_RIP_ROUTERS		ipa_build4(224, 0, 0, 9)
23 
24 #define IP6_ALL_NODES		ipa_build6(0xFF020000, 0, 0, 1)
25 #define IP6_ALL_ROUTERS		ipa_build6(0xFF020000, 0, 0, 2)
26 #define IP6_OSPF_ALL_ROUTERS	ipa_build6(0xFF020000, 0, 0, 5)
27 #define IP6_OSPF_DES_ROUTERS	ipa_build6(0xFF020000, 0, 0, 6)
28 #define IP6_RIP_ROUTERS		ipa_build6(0xFF020000, 0, 0, 9)
29 #define IP6_BABEL_ROUTERS	ipa_build6(0xFF020000, 0, 0, 0x00010006)
30 
31 #define IP4_NONE		_MI4(0)
32 #define IP6_NONE		_MI6(0,0,0,0)
33 
34 #define IP4_MAX_PREFIX_LENGTH	32
35 #define IP6_MAX_PREFIX_LENGTH	128
36 
37 #define IP4_MAX_TEXT_LENGTH	15	/* "255.255.255.255" */
38 #define IP6_MAX_TEXT_LENGTH	39	/* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" */
39 #define IPA_MAX_TEXT_LENGTH	39
40 
41 #define IP4_MIN_MTU		576
42 #define IP6_MIN_MTU		1280
43 
44 #define IP_PREC_INTERNET_CONTROL 0xc0
45 
46 #define IP4_HEADER_LENGTH	20
47 #define IP6_HEADER_LENGTH	40
48 #define UDP_HEADER_LENGTH	8
49 
50 
51 /* IANA Address Family Numbers */
52 /* https://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml */
53 /* Would use AF_ prefix, but that collides with POSIX address family numbers */
54 #define AFI_IPV4		1
55 #define AFI_IPV6		2
56 
57 
58 #ifdef DEBUGGING
59 
60 typedef struct ip4_addr {
61   u32 addr;
62 } ip4_addr;
63 
64 #define _MI4(x) ((struct ip4_addr) { x })
65 #define _I(x) (x).addr
66 
67 #else
68 
69 typedef u32 ip4_addr;
70 
71 #define _MI4(x) ((u32) (x))
72 #define _I(x) (x)
73 
74 #endif
75 
76 
77 typedef struct ip6_addr {
78   u32 addr[4];
79 } ip6_addr;
80 
81 #define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
82 #define _I0(a) ((a).addr[0])
83 #define _I1(a) ((a).addr[1])
84 #define _I2(a) ((a).addr[2])
85 #define _I3(a) ((a).addr[3])
86 
87 
88 /* Structure ip_addr may contain both IPv4 and IPv6 addresses */
89 typedef ip6_addr ip_addr;
90 #define IPA_NONE IP6_NONE
91 
92 #define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
93 #define ipa_from_ip6(x) x
94 #define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
95 
96 #define ipa_to_ip4(x) _MI4(_I3(x))
97 #define ipa_to_ip6(x) x
98 #define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
99 
100 #define ipa_is_ip4(a) ip6_is_v4mapped(a)
101 #define ipa_is_ip6(a) (! ip6_is_v4mapped(a))
102 
103 #define IPA_NONE4 ipa_from_ip4(IP4_NONE)
104 #define IPA_NONE6 ipa_from_ip6(IP6_NONE)
105 
106 
107 /*
108  *	Public constructors
109  */
110 
111 #define ip4_from_u32(x) _MI4(x)
112 #define ip4_to_u32(x) _I(x)
113 
114 #define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
115 #define ip6_build(a,b,c,d) _MI6(a,b,c,d)
116 
117 #define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
118 #define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
119 
120 
121 /*
122  *	Basic algebraic functions
123  */
124 
ip4_equal(ip4_addr a,ip4_addr b)125 static inline int ip4_equal(ip4_addr a, ip4_addr b)
126 { return _I(a) == _I(b); }
127 
ip4_zero(ip4_addr a)128 static inline int ip4_zero(ip4_addr a)
129 { return _I(a) == 0; }
130 
ip4_nonzero(ip4_addr a)131 static inline int ip4_nonzero(ip4_addr a)
132 { return _I(a) != 0; }
133 
ip4_and(ip4_addr a,ip4_addr b)134 static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
135 { return _MI4(_I(a) & _I(b)); }
136 
ip4_or(ip4_addr a,ip4_addr b)137 static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
138 { return _MI4(_I(a) | _I(b)); }
139 
ip4_xor(ip4_addr a,ip4_addr b)140 static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
141 { return _MI4(_I(a) ^ _I(b)); }
142 
ip4_not(ip4_addr a)143 static inline ip4_addr ip4_not(ip4_addr a)
144 { return _MI4(~_I(a)); }
145 
146 
ip6_equal(ip6_addr a,ip6_addr b)147 static inline int ip6_equal(ip6_addr a, ip6_addr b)
148 { return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
149 
ip6_zero(ip6_addr a)150 static inline int ip6_zero(ip6_addr a)
151 { return  !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
152 
ip6_nonzero(ip6_addr a)153 static inline int ip6_nonzero(ip6_addr a)
154 { return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
155 
ip6_and(ip6_addr a,ip6_addr b)156 static inline ip6_addr ip6_and(ip6_addr a, ip6_addr b)
157 { return _MI6(_I0(a) & _I0(b), _I1(a) & _I1(b), _I2(a) & _I2(b), _I3(a) & _I3(b)); }
158 
ip6_or(ip6_addr a,ip6_addr b)159 static inline ip6_addr ip6_or(ip6_addr a, ip6_addr b)
160 { return _MI6(_I0(a) | _I0(b), _I1(a) | _I1(b), _I2(a) | _I2(b), _I3(a) | _I3(b)); }
161 
ip6_xor(ip6_addr a,ip6_addr b)162 static inline ip6_addr ip6_xor(ip6_addr a, ip6_addr b)
163 { return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
164 
ip6_not(ip6_addr a)165 static inline ip6_addr ip6_not(ip6_addr a)
166 { return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
167 
168 
169 #define ipa_equal(x,y) ip6_equal(x,y)
170 #define ipa_zero(x) ip6_zero(x)
171 #define ipa_nonzero(x) ip6_nonzero(x)
172 #define ipa_and(x,y) ip6_and(x,y)
173 #define ipa_or(x,y) ip6_or(x,y)
174 #define ipa_xor(x,y) ip6_xor(x,y)
175 #define ipa_not(x) ip6_not(x)
176 
177 
178 /*
179  * A zero address is either a token for invalid/unused, or the prefix of default
180  * routes. These functions should be used in the second case, where both IPv4
181  * and IPv6 zero addresses should be checked.
182  */
183 
ipa_zero2(ip_addr a)184 static inline int ipa_zero2(ip_addr a)
185 { return  !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
186 
ipa_nonzero2(ip_addr a)187 static inline int ipa_nonzero2(ip_addr a)
188 { return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
189 
190 
191 /*
192  *	Hash and compare functions
193  */
194 
ip4_hash(ip4_addr a)195 static inline u32 ip4_hash(ip4_addr a)
196 { return u32_hash(_I(a)); }
197 
ip6_hash(ip6_addr a)198 static inline u32 ip6_hash(ip6_addr a)
199 {
200   /* Returns a 32-bit hash key, although low-order bits are not mixed */
201   u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
202   return x ^ (x << 16) ^ (x << 24);
203 }
204 
ip4_compare(ip4_addr a,ip4_addr b)205 static inline int ip4_compare(ip4_addr a, ip4_addr b)
206 { return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
207 
208 int ip6_compare(ip6_addr a, ip6_addr b);
209 
210 #define ipa_hash(x) ip6_hash(x)
211 #define ipa_compare(x,y) ip6_compare(x,y)
212 
213 
214 /*
215  *	IP address classification
216  */
217 
218 /* Address class */
219 #define IADDR_INVALID		-1
220 #define IADDR_SCOPE_MASK       	0xfff
221 #define IADDR_HOST		0x1000
222 #define IADDR_BROADCAST		0x2000
223 #define IADDR_MULTICAST		0x4000
224 
225 /* Address scope */
226 #define SCOPE_HOST		0
227 #define SCOPE_LINK		1
228 #define SCOPE_SITE		2
229 #define SCOPE_ORGANIZATION	3
230 #define SCOPE_UNIVERSE		4
231 #define SCOPE_UNDEFINED		5
232 
233 int ip4_classify(ip4_addr ad);
234 int ip6_classify(ip6_addr *a);
235 
ip6_is_link_local(ip6_addr a)236 static inline int ip6_is_link_local(ip6_addr a)
237 { return (_I0(a) & 0xffc00000) == 0xfe800000; }
238 
ip6_is_v4mapped(ip6_addr a)239 static inline int ip6_is_v4mapped(ip6_addr a)
240 { return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
241 
242 #define ipa_classify(x) ip6_classify(&(x))
243 #define ipa_is_link_local(x) ip6_is_link_local(x)
244 
ip4_is_unicast(ip4_addr a)245 static inline int ip4_is_unicast(ip4_addr a)
246 { return _I(a) < 0xe0000000; }
247 
248 /* XXXX remove */
ipa_classify_net(ip_addr a)249 static inline int ipa_classify_net(ip_addr a)
250 { return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
251 
252 
253 /*
254  *	Miscellaneous IP prefix manipulation
255  */
256 
ip4_mkmask(uint n)257 static inline ip4_addr ip4_mkmask(uint n)
258 { return _MI4(u32_mkmask(n)); }
259 
ip4_masklen(ip4_addr a)260 static inline uint ip4_masklen(ip4_addr a)
261 { return u32_masklen(_I(a)); }
262 
263 ip6_addr ip6_mkmask(uint n);
264 uint ip6_masklen(ip6_addr *a);
265 
266 /* ipX_pxlen() requires that x != y */
ip4_pxlen(ip4_addr a,ip4_addr b)267 static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
268 { return 31 - u32_log2(_I(a) ^ _I(b)); }
269 
ip6_pxlen(ip6_addr a,ip6_addr b)270 static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
271 {
272   int i = 0;
273   i += (a.addr[i] == b.addr[i]);
274   i += (a.addr[i] == b.addr[i]);
275   i += (a.addr[i] == b.addr[i]);
276   i += (a.addr[i] == b.addr[i]);
277   return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
278 }
279 
ip4_getbit(ip4_addr a,uint pos)280 static inline u32 ip4_getbit(ip4_addr a, uint pos)
281 { return _I(a) & (0x80000000 >> pos); }
282 
ip6_getbit(ip6_addr a,uint pos)283 static inline u32 ip6_getbit(ip6_addr a, uint pos)
284 { return a.addr[pos / 32] & (0x80000000 >> (pos % 32)); }
285 
ip4_setbit(ip4_addr * a,uint pos)286 static inline u32 ip4_setbit(ip4_addr *a, uint pos)
287 { return _I(*a) |= (0x80000000 >> pos); }
288 
ip6_setbit(ip6_addr * a,uint pos)289 static inline u32 ip6_setbit(ip6_addr *a, uint pos)
290 { return a->addr[pos / 32] |= (0x80000000 >> (pos % 32)); }
291 
ip4_clrbit(ip4_addr * a,uint pos)292 static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
293 { return _I(*a) &= ~(0x80000000 >> pos); }
294 
ip6_clrbit(ip6_addr * a,uint pos)295 static inline u32 ip6_clrbit(ip6_addr *a, uint pos)
296 { return a->addr[pos / 32] &= ~(0x80000000 >> (pos % 32)); }
297 
ip4_opposite_m1(ip4_addr a)298 static inline ip4_addr ip4_opposite_m1(ip4_addr a)
299 { return _MI4(_I(a) ^ 1); }
300 
ip4_opposite_m2(ip4_addr a)301 static inline ip4_addr ip4_opposite_m2(ip4_addr a)
302 { return _MI4(_I(a) ^ 3); }
303 
ip6_opposite_m1(ip6_addr a)304 static inline ip6_addr ip6_opposite_m1(ip6_addr a)
305 { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
306 
ip6_opposite_m2(ip6_addr a)307 static inline ip6_addr ip6_opposite_m2(ip6_addr a)
308 { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
309 
310 ip4_addr ip4_class_mask(ip4_addr ad);
311 
312 #define ipa_opposite_m1(x) ip6_opposite_m1(x)
313 #define ipa_opposite_m2(x) ip6_opposite_m2(x)
314 
315 
316 /*
317  *	Host/network order conversions
318  */
319 
ip4_hton(ip4_addr a)320 static inline ip4_addr ip4_hton(ip4_addr a)
321 { return _MI4(htonl(_I(a))); }
322 
ip4_ntoh(ip4_addr a)323 static inline ip4_addr ip4_ntoh(ip4_addr a)
324 { return _MI4(ntohl(_I(a))); }
325 
ip6_hton(ip6_addr a)326 static inline ip6_addr ip6_hton(ip6_addr a)
327 { return _MI6(htonl(_I0(a)), htonl(_I1(a)), htonl(_I2(a)), htonl(_I3(a))); }
328 
ip6_ntoh(ip6_addr a)329 static inline ip6_addr ip6_ntoh(ip6_addr a)
330 { return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
331 
332 #define MPLS_MAX_LABEL_STACK 8
333 typedef struct mpls_label_stack {
334   uint len;
335   u32 stack[MPLS_MAX_LABEL_STACK];
336 } mpls_label_stack;
337 
338 static inline int
mpls_get(const char * buf,int buflen,u32 * stack)339 mpls_get(const char *buf, int buflen, u32 *stack)
340 {
341   for (int i=0; (i<MPLS_MAX_LABEL_STACK) && (i*4+3 < buflen); i++)
342   {
343     u32 s = get_u32(buf + i*4);
344     stack[i] = s >> 12;
345     if (s & 0x100)
346       return i+1;
347   }
348   return -1;
349 }
350 
351 static inline int
mpls_put(char * buf,int len,u32 * stack)352 mpls_put(char *buf, int len, u32 *stack)
353 {
354   for (int i=0; i<len; i++)
355     put_u32(buf + i*4, stack[i] << 12 | (i+1 == len ? 0x100 : 0));
356 
357   return len*4;
358 }
359 
360 /*
361  *	Unaligned data access (in network order)
362  */
363 
get_ip4(const void * buf)364 static inline ip4_addr get_ip4(const void *buf)
365 {
366   return _MI4(get_u32(buf));
367 }
368 
get_ip6(const void * buf)369 static inline ip6_addr get_ip6(const void *buf)
370 {
371   ip6_addr a;
372   memcpy(&a, buf, 16);
373   return ip6_ntoh(a);
374 }
375 
put_ip4(void * buf,ip4_addr a)376 static inline void * put_ip4(void *buf, ip4_addr a)
377 {
378   put_u32(buf, _I(a));
379   return buf+4;
380 }
381 
put_ip6(void * buf,ip6_addr a)382 static inline void * put_ip6(void *buf, ip6_addr a)
383 {
384   a = ip6_hton(a);
385   memcpy(buf, &a, 16);
386   return buf+16;
387 }
388 
389 
390 /*
391  *	Binary/text form conversions
392  */
393 
394 char *ip4_ntop(ip4_addr a, char *b);
395 char *ip6_ntop(ip6_addr a, char *b);
396 
ip4_ntox(ip4_addr a,char * b)397 static inline char * ip4_ntox(ip4_addr a, char *b)
398 { return b + bsprintf(b, "%08x", _I(a)); }
399 
ip6_ntox(ip6_addr a,char * b)400 static inline char * ip6_ntox(ip6_addr a, char *b)
401 { return b + bsprintf(b, "%08x.%08x.%08x.%08x", _I0(a), _I1(a), _I2(a), _I3(a)); }
402 
403 int ip4_pton(const char *a, ip4_addr *o);
404 int ip6_pton(const char *a, ip6_addr *o);
405 
406 
407 /*
408  *	Miscellaneous
409  */
410 
411 char *ip_scope_text(uint);
412 
413 #endif
414