]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/ipv6.h
3 * BIRD -- IP Addresses et Cetera for IPv6
5 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
7 * Can be freely distributed and used under the terms of the GNU GPL.
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include "lib/string.h"
16 #include "lib/bitops.h"
17 #include "lib/unaligned.h"
19 typedef struct ipv6_addr
{
23 #define _MI(a,b,c,d) ((struct ipv6_addr) {{ a, b, c, d }})
24 #define _I0(a) ((a).addr[0])
25 #define _I1(a) ((a).addr[1])
26 #define _I2(a) ((a).addr[2])
27 #define _I3(a) ((a).addr[3])
29 #define MAX_PREFIX_LENGTH 128
30 #define BITS_PER_IP_ADDRESS 128
31 #define STD_ADDRESS_P_LENGTH 39
32 #define SIZE_OF_IP_HEADER 40
34 #define IPA_NONE _MI(0,0,0,0)
36 #define ipa_equal(x,y) ({ ip_addr _a=(x), _b=(y); \
37 _I0(_a) == _I0(_b) && \
38 _I1(_a) == _I1(_b) && \
39 _I2(_a) == _I2(_b) && \
40 _I3(_a) == _I3(_b); })
41 #define ipa_nonzero(x) ({ ip_addr _a=(x); (_I0(_a) || _I1(_a) || _I2(_a) || _I3(_a)); })
42 #define ipa_and(x,y) ({ ip_addr _a=(x), _b=(y); \
43 _MI(_I0(_a) & _I0(_b), \
46 _I3(_a) & _I3(_b)); })
47 #define ipa_or(x,y) ({ ip_addr _a=(x), _b=(y); \
48 _MI(_I0(_a) | _I0(_b), \
51 _I3(_a) | _I3(_b)); })
52 #define ipa_xor(x,y) ({ ip_addr _a=(x), _b=(y); \
53 _MI(_I0(_a) ^ _I0(_b), \
56 _I3(_a) ^ _I3(_b)); })
57 #define ipa_not(x) ({ ip_addr _a=(x); _MI(~_I0(_a),~_I1(_a),~_I2(_a),~_I3(_a)); })
58 #define ipa_mkmask(x) ipv6_mkmask(x)
59 #define ipa_mklen(x) ipv6_mklen(&(x))
60 #define ipa_hash(x) ipv6_hash(&(x))
61 #define ipa_hton(x) ipv6_hton(&(x))
62 #define ipa_ntoh(x) ipv6_ntoh(&(x))
63 #define ipa_classify(x) ipv6_classify(&(x))
64 #define ipa_has_link_scope(x) ipv6_has_link_scope(&(x))
65 #define ipa_opposite_m1(x) ({ ip_addr _a=(x); _MI(_I0(_a),_I1(_a),_I2(_a),_I3(_a) ^ 1); })
66 #define ipa_opposite_m2(x) ({ ip_addr _a=(x); _MI(_I0(_a),_I1(_a),_I2(_a),_I3(_a) ^ 3); })
67 /* ipa_class_mask don't make sense with IPv6 */
68 /* ipa_from_u32 and ipa_to_u32 replaced by ipa_build */
69 #define ipa_build(a,b,c,d) _MI(a,b,c,d)
70 #define ipa_compare(x,y) ipv6_compare(x,y)
71 /* ipa_pxlen() requires that x != y */
72 #define ipa_pxlen(x, y) ipv6_pxlen(x, y)
73 #define ipa_getbit(x, y) ipv6_getbit(x, y)
74 #define ipa_put_addr(x, y) ipv6_put_addr(x, y)
75 #define ipa_absolutize(x,y) ipv6_absolutize(x,y)
77 /* In IPv6, SOCK_RAW does not return packet header */
78 #define ip_skip_header(x, y) x
80 ip_addr
ipv6_mkmask(unsigned);
81 unsigned ipv6_mklen(ip_addr
*);
82 int ipv6_classify(ip_addr
*);
83 void ipv6_hton(ip_addr
*);
84 void ipv6_ntoh(ip_addr
*);
85 int ipv6_compare(ip_addr
, ip_addr
);
86 int ipv4_pton_u32(char *, u32
*);
87 void ipv6_absolutize(ip_addr
*, ip_addr
*);
89 static inline int ipv6_has_link_scope(ip_addr
*a
)
91 return ((a
->addr
[0] & 0xffc00000) == 0xfe800000);
95 * This hash function looks well, but once IPv6 enters
96 * mainstream use, we need to check that it has good
97 * distribution properties on real routing tables.
100 static inline unsigned ipv6_hash(ip_addr
*a
)
102 /* Returns a 16-bit hash key */
103 u32 x
= _I0(*a
) ^ _I1(*a
) ^ _I2(*a
) ^ _I3(*a
);
104 return (x
^ (x
>> 16) ^ (x
>> 8)) & 0xffff;
107 static inline u32
ipv6_getbit(ip_addr a
, u32 y
)
109 return a
.addr
[y
/ 32] & (0x80000000 >> (y
% 32));
112 static inline u32
ipv6_pxlen(ip_addr a
, ip_addr b
)
115 i
+= (a
.addr
[i
] == b
.addr
[i
]);
116 i
+= (a
.addr
[i
] == b
.addr
[i
]);
117 i
+= (a
.addr
[i
] == b
.addr
[i
]);
118 i
+= (a
.addr
[i
] == b
.addr
[i
]);
119 return 32 * i
+ 31 - u32_log2(a
.addr
[i
] ^ b
.addr
[i
]);
122 static inline byte
* ipv6_put_addr(byte
*buf
, ip_addr a
)
124 put_u32(buf
+0, _I0(a
));
125 put_u32(buf
+4, _I1(a
));
126 put_u32(buf
+8, _I2(a
));
127 put_u32(buf
+12, _I3(a
));
132 * RFC 1883 defines packet precendece, but RFC 2460 replaces it
133 * by generic Traffic Class ID with no defined semantics. Better
136 #define IP_PREC_INTERNET_CONTROL -1