]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/net.c
Netlink and BSD: Integrating IPv4 and IPv6
[thirdparty/bird.git] / lib / net.c
1
2 #include "nest/bird.h"
3 #include "lib/ip.h"
4 #include "lib/net.h"
5
6 const u16 net_addr_length[] = {
7 [NET_IP4] = sizeof(net_addr_ip4),
8 [NET_IP6] = sizeof(net_addr_ip6),
9 [NET_VPN4] = sizeof(net_addr_vpn4),
10 [NET_VPN6] = sizeof(net_addr_vpn6)
11 };
12
13 int
14 net_format(const net_addr *N, char *buf, int buflen)
15 {
16 net_addr_union *n = (void *) N;
17
18 /* FIXME: quick hack */
19 switch (n->n.type)
20 {
21 case NET_IP4:
22 return bsnprintf(buf, buflen, "%I/%d", n->ip4.prefix, n->ip4.pxlen);
23 case NET_IP6:
24 return bsnprintf(buf, buflen, "%I/%d", n->ip6.prefix, n->ip6.pxlen);
25 case NET_VPN4:
26 return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
27 case NET_VPN6:
28 return bsnprintf(buf, buflen, "%u:%u %I/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
29 }
30
31 return 0;
32 }
33
34
35 ip_addr
36 net_pxmask(const net_addr *a)
37 {
38 switch (a->type)
39 {
40 case NET_IP4:
41 case NET_VPN4:
42 return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
43
44 case NET_IP6:
45 case NET_VPN6:
46 return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
47
48 default:
49 return IPA_NONE;
50 }
51 }
52
53
54 static inline int net_validate_ip4(const net_addr_ip4 *n)
55 {
56 return (n->pxlen <= IP4_MAX_PREFIX_LENGTH) &&
57 ip4_zero(ip4_and(n->prefix, ip4_not(ip4_mkmask(n->pxlen))));
58 }
59
60 static inline int net_validate_ip6(const net_addr_ip6 *n)
61 {
62 return (n->pxlen <= IP6_MAX_PREFIX_LENGTH) &&
63 ip6_zero(ip6_and(n->prefix, ip6_not(ip6_mkmask(n->pxlen))));
64 }
65
66 int
67 net_validate(const net_addr *N)
68 {
69 switch (a->type)
70 {
71 case NET_IP4:
72 case NET_VPN4:
73 return net_validate_ip4((net_addr_ip4 *) N);
74
75 case NET_IP6:
76 case NET_VPN6:
77 return net_validate_ip6((net_addr_ip6 *) N);
78
79 default:
80 return 0;
81 }
82 }
83
84 int
85 net_classify(const net_addr *N)
86 {
87 net_addr_union *n = (void *) N;
88
89 switch (n->n.type)
90 {
91 case NET_IP4:
92 case NET_VPN4:
93 return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
94
95 case NET_IP6:
96 case NET_VPN6:
97 return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
98 }
99
100 return 0;
101 }