Annotation of embedaddon/bird/lib/ip.h, revision 1.1.1.2
1.1 misho 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 "lib/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_MIN_MTU 576
35: #define IP6_MIN_MTU 1280
36:
37: #define IP_PREC_INTERNET_CONTROL 0xc0
38:
39: #define IP4_HEADER_LENGTH 20
40: #define IP6_HEADER_LENGTH 40
41: #define UDP_HEADER_LENGTH 8
42:
43:
44: #ifdef IPV6
45: #define MAX_PREFIX_LENGTH 128
46: #define BITS_PER_IP_ADDRESS 128
47: #define STD_ADDRESS_P_LENGTH 39
48: #define SIZE_OF_IP_HEADER 40
49: #else
50: #define MAX_PREFIX_LENGTH 32
51: #define BITS_PER_IP_ADDRESS 32
52: #define STD_ADDRESS_P_LENGTH 15
53: #define SIZE_OF_IP_HEADER 24
54: #endif
55:
56:
57: #ifdef DEBUGGING
58:
59: typedef struct ip4_addr {
60: u32 addr;
61: } ip4_addr;
62:
63: #define _MI4(x) ((struct ip4_addr) { x })
64: #define _I(x) (x).addr
65:
66: #else
67:
68: typedef u32 ip4_addr;
69:
1.1.1.2 ! misho 70: #define _MI4(x) ((u32) (x))
1.1 misho 71: #define _I(x) (x)
72:
73: #endif
74:
75:
76: typedef struct ip6_addr {
77: u32 addr[4];
78: } ip6_addr;
79:
80: #define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
81: #define _I0(a) ((a).addr[0])
82: #define _I1(a) ((a).addr[1])
83: #define _I2(a) ((a).addr[2])
84: #define _I3(a) ((a).addr[3])
85:
86:
87: #ifdef IPV6
88:
89: /* Structure ip_addr may contain both IPv4 and IPv6 addresses */
90: typedef ip6_addr ip_addr;
91: #define IPA_NONE IP6_NONE
92:
93: #define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
94: #define ipa_from_ip6(x) x
95: #define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
96:
97: #define ipa_to_ip4(x) _MI4(_I3(x))
98: #define ipa_to_ip6(x) x
99: #define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
100:
101: #define ipa_is_ip4(a) ip6_is_v4mapped(a)
1.1.1.2 ! misho 102: #define ipa_is_ip6(a) (! ip6_is_v4mapped(a))
1.1 misho 103:
104: #else
105:
106: /* Provisionary ip_addr definition same as ip4_addr */
107: typedef ip4_addr ip_addr;
108: #define IPA_NONE IP4_NONE
109:
110: #define ipa_from_ip4(x) x
111: #define ipa_from_ip6(x) IPA_NONE
112: #define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
113:
114: #define ipa_to_ip4(x) x
115: #define ipa_to_ip6(x) IP6_NONE
116: #define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
117:
118: #define ipa_is_ip4(a) 1
1.1.1.2 ! misho 119: #define ipa_is_ip6(a) 0
1.1 misho 120:
121: #endif
122:
123:
124: /*
125: * Public constructors
126: */
127:
128: #define ip4_from_u32(x) _MI4(x)
129: #define ip4_to_u32(x) _I(x)
130:
131: #define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
132: #define ip6_build(a,b,c,d) _MI6(a,b,c,d)
133:
134: #define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
135: #define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
136:
137:
138: /*
139: * Basic algebraic functions
140: */
141:
142: static inline int ip4_equal(ip4_addr a, ip4_addr b)
143: { return _I(a) == _I(b); }
144:
145: static inline int ip4_zero(ip4_addr a)
146: { return _I(a) == 0; }
147:
148: static inline int ip4_nonzero(ip4_addr a)
149: { return _I(a) != 0; }
150:
151: static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
152: { return _MI4(_I(a) & _I(b)); }
153:
154: static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
155: { return _MI4(_I(a) | _I(b)); }
156:
157: static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
158: { return _MI4(_I(a) ^ _I(b)); }
159:
160: static inline ip4_addr ip4_not(ip4_addr a)
161: { return _MI4(~_I(a)); }
162:
163:
164: static inline int ip6_equal(ip6_addr a, ip6_addr b)
165: { return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
166:
167: static inline int ip6_zero(ip6_addr a)
168: { return !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
169:
170: static inline int ip6_nonzero(ip6_addr a)
171: { return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
172:
173: static inline ip6_addr ip6_and(ip6_addr a, ip6_addr b)
174: { return _MI6(_I0(a) & _I0(b), _I1(a) & _I1(b), _I2(a) & _I2(b), _I3(a) & _I3(b)); }
175:
176: static inline ip6_addr ip6_or(ip6_addr a, ip6_addr b)
177: { return _MI6(_I0(a) | _I0(b), _I1(a) | _I1(b), _I2(a) | _I2(b), _I3(a) | _I3(b)); }
178:
179: static inline ip6_addr ip6_xor(ip6_addr a, ip6_addr b)
180: { return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
181:
182: static inline ip6_addr ip6_not(ip6_addr a)
183: { return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
184:
185:
186: #ifdef IPV6
187: #define ipa_equal(x,y) ip6_equal(x,y)
188: #define ipa_zero(x) ip6_zero(x)
189: #define ipa_nonzero(x) ip6_nonzero(x)
190: #define ipa_and(x,y) ip6_and(x,y)
191: #define ipa_or(x,y) ip6_or(x,y)
192: #define ipa_xor(x,y) ip6_xor(x,y)
193: #define ipa_not(x) ip6_not(x)
194: #else
195: #define ipa_equal(x,y) ip4_equal(x,y)
196: #define ipa_zero(x) ip4_zero(x)
197: #define ipa_nonzero(x) ip4_nonzero(x)
198: #define ipa_and(x,y) ip4_and(x,y)
199: #define ipa_or(x,y) ip4_or(x,y)
200: #define ipa_xor(x,y) ip4_xor(x,y)
201: #define ipa_not(x) ip4_not(x)
202: #endif
203:
204:
205:
206: #ifdef IPV6
207: /*
208: * A zero address is either a token for invalid/unused, or the prefix of default
209: * routes. These functions should be used in the second case, where both IPv4
210: * and IPv6 zero addresses should be checked.
211: */
212:
213: static inline int ipa_zero2(ip_addr a)
214: { return !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
215:
216: static inline int ipa_nonzero2(ip_addr a)
217: { return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
218:
219: #else
220: #define ipa_zero2(x) ip4_zero(x)
221: #define ipa_nonzero2(x) ip4_nonzero(x)
222: #endif
223:
224:
225: /*
226: * Hash and compare functions
227: */
228:
229: static inline uint ip4_hash(ip4_addr a)
230: {
231: /* Returns a 16-bit value */
232: u32 x = _I(a);
233: x ^= x >> 16;
234: x ^= x << 10;
235: return x & 0xffff;
236: }
237:
238: static inline u32 ip4_hash32(ip4_addr a)
239: {
240: /* Returns a 32-bit value, although low-order bits are not mixed */
241: u32 x = _I(a);
242: x ^= x << 16;
243: x ^= x << 12;
244: return x;
245: }
246:
247: static inline uint ip6_hash(ip6_addr a)
248: {
249: /* Returns a 16-bit hash key */
250: u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
251: return (x ^ (x >> 16) ^ (x >> 8)) & 0xffff;
252: }
253:
254: static inline u32 ip6_hash32(ip6_addr a)
255: {
256: /* Returns a 32-bit hash key, although low-order bits are not mixed */
257: u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
258: return x ^ (x << 16) ^ (x << 24);
259: }
260:
261: static inline int ip4_compare(ip4_addr a, ip4_addr b)
262: { return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
263:
264: int ip6_compare(ip6_addr a, ip6_addr b);
265:
266:
267: #ifdef IPV6
268: #define ipa_hash(x) ip6_hash(x)
269: #define ipa_hash32(x) ip6_hash32(x)
270: #define ipa_compare(x,y) ip6_compare(x,y)
271: #else
272: #define ipa_hash(x) ip4_hash(x)
273: #define ipa_hash32(x) ip4_hash32(x)
274: #define ipa_compare(x,y) ip4_compare(x,y)
275: #endif
276:
277:
278: /*
279: * IP address classification
280: */
281:
282: /* Address class */
283: #define IADDR_INVALID -1
284: #define IADDR_SCOPE_MASK 0xfff
285: #define IADDR_HOST 0x1000
286: #define IADDR_BROADCAST 0x2000
287: #define IADDR_MULTICAST 0x4000
288:
289: /* Address scope */
290: #define SCOPE_HOST 0
291: #define SCOPE_LINK 1
292: #define SCOPE_SITE 2
293: #define SCOPE_ORGANIZATION 3
294: #define SCOPE_UNIVERSE 4
295: #define SCOPE_UNDEFINED 5
296:
297: int ip4_classify(ip4_addr ad);
298: int ip6_classify(ip6_addr *a);
299:
300: static inline int ip6_is_link_local(ip6_addr a)
301: { return (_I0(a) & 0xffc00000) == 0xfe800000; }
302:
303: static inline int ip6_is_v4mapped(ip6_addr a)
304: { return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
305:
306: #ifdef IPV6
307: #define ipa_classify(x) ip6_classify(&(x))
308: #define ipa_is_link_local(x) ip6_is_link_local(x)
309: #else
310: #define ipa_classify(x) ip4_classify(x)
311: #define ipa_is_link_local(x) 0
312: #endif
313:
1.1.1.2 ! misho 314: static inline int ip4_is_unicast(ip4_addr a)
! 315: { return _I(a) < 0xe0000000; }
! 316:
1.1 misho 317: static inline int ipa_classify_net(ip_addr a)
318: { return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
319:
320:
321: /*
322: * Miscellaneous IP prefix manipulation
323: */
324:
325: static inline ip4_addr ip4_mkmask(uint n)
326: { return _MI4(u32_mkmask(n)); }
327:
328: static inline int ip4_masklen(ip4_addr a)
329: { return u32_masklen(_I(a)); }
330:
331: ip6_addr ip6_mkmask(uint n);
332: int ip6_masklen(ip6_addr *a);
333:
334: /* ipX_pxlen() requires that x != y */
335: static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
336: { return 31 - u32_log2(_I(a) ^ _I(b)); }
337:
338: static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
339: {
340: int i = 0;
341: i += (a.addr[i] == b.addr[i]);
342: i += (a.addr[i] == b.addr[i]);
343: i += (a.addr[i] == b.addr[i]);
344: i += (a.addr[i] == b.addr[i]);
345: return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
346: }
347:
348: static inline u32 ip4_getbit(ip4_addr a, uint pos)
349: { return _I(a) & (0x80000000 >> pos); }
350:
351: static inline u32 ip6_getbit(ip6_addr a, uint pos)
352: { return a.addr[pos / 32] & (0x80000000 >> (pos % 32)); }
353:
354: static inline ip4_addr ip4_opposite_m1(ip4_addr a)
355: { return _MI4(_I(a) ^ 1); }
356:
357: static inline ip4_addr ip4_opposite_m2(ip4_addr a)
358: { return _MI4(_I(a) ^ 3); }
359:
360: static inline ip6_addr ip6_opposite_m1(ip6_addr a)
361: { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
362:
363: static inline ip6_addr ip6_opposite_m2(ip6_addr a)
364: { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
365:
366: ip4_addr ip4_class_mask(ip4_addr ad);
367:
368: #ifdef IPV6
369: #define ipa_mkmask(x) ip6_mkmask(x)
370: #define ipa_masklen(x) ip6_masklen(&x)
371: #define ipa_pxlen(x,y) ip6_pxlen(x,y)
372: #define ipa_getbit(x,n) ip6_getbit(x,n)
373: #define ipa_opposite_m1(x) ip6_opposite_m1(x)
374: #define ipa_opposite_m2(x) ip6_opposite_m2(x)
375: #else
376: #define ipa_mkmask(x) ip4_mkmask(x)
377: #define ipa_masklen(x) ip4_masklen(x)
378: #define ipa_pxlen(x,y) ip4_pxlen(x,y)
379: #define ipa_getbit(x,n) ip4_getbit(x,n)
380: #define ipa_opposite_m1(x) ip4_opposite_m1(x)
381: #define ipa_opposite_m2(x) ip4_opposite_m2(x)
382: #endif
383:
384:
385: /*
386: * Host/network order conversions
387: */
388:
389: static inline ip4_addr ip4_hton(ip4_addr a)
390: { return _MI4(htonl(_I(a))); }
391:
392: static inline ip4_addr ip4_ntoh(ip4_addr a)
393: { return _MI4(ntohl(_I(a))); }
394:
395: static inline ip6_addr ip6_hton(ip6_addr a)
396: { return _MI6(htonl(_I0(a)), htonl(_I1(a)), htonl(_I2(a)), htonl(_I3(a))); }
397:
398: static inline ip6_addr ip6_ntoh(ip6_addr a)
399: { return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
400:
401: #ifdef IPV6
402: #define ipa_hton(x) x = ip6_hton(x)
403: #define ipa_ntoh(x) x = ip6_ntoh(x)
404: #else
405: #define ipa_hton(x) x = ip4_hton(x)
406: #define ipa_ntoh(x) x = ip4_ntoh(x)
407: #endif
408:
409:
410: /*
411: * Unaligned data access (in network order)
412: */
413:
414: static inline ip4_addr get_ip4(void *buf)
415: {
416: return _MI4(get_u32(buf));
417: }
418:
419: static inline ip6_addr get_ip6(void *buf)
420: {
421: ip6_addr a;
422: memcpy(&a, buf, 16);
423: return ip6_ntoh(a);
424: }
425:
426: static inline void * put_ip4(void *buf, ip4_addr a)
427: {
428: put_u32(buf, _I(a));
429: return buf+4;
430: }
431:
432: static inline void * put_ip6(void *buf, ip6_addr a)
433: {
434: a = ip6_hton(a);
435: memcpy(buf, &a, 16);
436: return buf+16;
437: }
438:
439: // XXXX these functions must be redesigned or removed
440: #ifdef IPV6
441: #define get_ipa(x) get_ip6(x)
442: #define put_ipa(x,y) put_ip6(x,y)
443: #else
444: #define get_ipa(x) get_ip4(x)
445: #define put_ipa(x,y) put_ip4(x,y)
446: #endif
447:
448:
449: /*
450: * Binary/text form conversions
451: */
452:
453: char *ip4_ntop(ip4_addr a, char *b);
454: char *ip6_ntop(ip6_addr a, char *b);
455:
456: static inline char * ip4_ntox(ip4_addr a, char *b)
457: { return b + bsprintf(b, "%08x", _I(a)); }
458:
459: static inline char * ip6_ntox(ip6_addr a, char *b)
460: { return b + bsprintf(b, "%08x.%08x.%08x.%08x", _I0(a), _I1(a), _I2(a), _I3(a)); }
461:
462: int ip4_pton(const char *a, ip4_addr *o);
463: int ip6_pton(const char *a, ip6_addr *o);
464:
465: // XXXX these functions must be redesigned or removed
466: #ifdef IPV6
467: #define ipa_ntop(x,y) ip6_ntop(x,y)
468: #define ipa_ntox(x,y) ip6_ntox(x,y)
469: #define ipa_pton(x,y) ip6_pton(x,y)
470: #else
471: #define ipa_ntop(x,y) ip4_ntop(x,y)
472: #define ipa_ntox(x,y) ip4_ntox(x,y)
473: #define ipa_pton(x,y) ip4_pton(x,y)
474: #endif
475:
476:
477: /*
478: * Miscellaneous
479: */
480:
481: // XXXX review this
482:
483: #define ip_is_prefix(a,l) (!ipa_nonzero(ipa_and(a, ipa_not(ipa_mkmask(l)))))
484: #define ipa_in_net(x,n,p) (ipa_zero(ipa_and(ipa_xor((n),(x)),ipa_mkmask(p))))
485: #define net_in_net(n1,l1,n2,l2) (((l1) >= (l2)) && (ipa_zero(ipa_and(ipa_xor((n1),(n2)),ipa_mkmask(l2)))))
486:
487: char *ip_scope_text(uint);
488:
489: struct prefix {
490: ip_addr addr;
491: uint len;
492: };
493:
494:
495: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>