]> git.proxmox.com Git - mirror_frr.git/blob - babeld/kernel.c
Merge pull request #13484 from sri-mohan1/srib-ldpd
[mirror_frr.git] / babeld / kernel.c
1 // SPDX-License-Identifier: MIT
2 /*
3 Copyright 2007, 2008 by Grégoire Henry, Julien Cristau and Juliusz Chroboczek
4 Copyright 2011, 2012 by Matthieu Boutier and Juliusz Chroboczek
5 */
6
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <sys/time.h>
12 #include <sys/param.h>
13 #include <time.h>
14
15 #include "babeld.h"
16
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <arpa/inet.h>
23
24 #include <zebra.h>
25 #include "prefix.h"
26 #include "zclient.h"
27 #include "kernel.h"
28 #include "privs.h"
29 #include "command.h"
30 #include "vty.h"
31 #include "memory.h"
32 #include "frrevent.h"
33 #include "nexthop.h"
34
35 #include "util.h"
36 #include "babel_interface.h"
37 #include "babel_zebra.h"
38
39
40 static int
41 zebra_route(int add, int familt, const unsigned char *pref, unsigned short plen,
42 const unsigned char *gate, int ifindex, unsigned int metric);
43
44 int
45 kernel_interface_operational(struct interface *interface)
46 {
47 return if_is_operative(interface);
48 }
49
50 int
51 kernel_interface_mtu(struct interface *interface)
52 {
53 return MIN(interface->mtu, interface->mtu6);
54 }
55
56 int
57 kernel_interface_wireless(struct interface *interface)
58 {
59 return 0;
60 }
61
62 int
63 kernel_route(enum babel_kernel_routes operation, const unsigned char *pref,
64 unsigned short plen, const unsigned char *gate, int ifindex,
65 unsigned int metric, const unsigned char *newgate, int newifindex,
66 unsigned int newmetric)
67 {
68 int rc;
69 int family;
70
71 /* Check that the protocol family is consistent. */
72 if(plen >= 96 && v4mapped(pref)) {
73 if(!v4mapped(gate)) {
74 errno = EINVAL;
75 return -1;
76 }
77 family = AF_INET;
78 } else {
79 if(v4mapped(gate)) {
80 errno = EINVAL;
81 return -1;
82 }
83 family = AF_INET6;
84 }
85
86 switch (operation) {
87 case ROUTE_ADD:
88 return zebra_route(1, family, pref, plen, gate, ifindex, metric);
89 case ROUTE_FLUSH:
90 return zebra_route(0, family, pref, plen, gate, ifindex, metric);
91 case ROUTE_MODIFY:
92 if(newmetric == metric && memcmp(newgate, gate, 16) == 0 &&
93 newifindex == ifindex)
94 return 0;
95 debugf(BABEL_DEBUG_ROUTE, "Modify route: delete old; add new.");
96 rc = zebra_route(0, family, pref, plen, gate, ifindex, metric);
97 if (rc < 0)
98 return -1;
99
100 rc = zebra_route(1, family, pref, plen, newgate, newifindex,
101 newmetric);
102 return rc;
103 }
104
105 return 0;
106 }
107
108 static int
109 zebra_route(int add, int family, const unsigned char *pref, unsigned short plen,
110 const unsigned char *gate, int ifindex, unsigned int metric)
111 {
112 struct zapi_route api; /* quagga's communication system */
113 struct prefix quagga_prefix; /* quagga's prefix */
114 union g_addr babel_prefix_addr; /* babeld's prefix addr */
115 struct zapi_nexthop *api_nh; /* next router to go - no ECMP */
116
117 api_nh = &api.nexthops[0];
118
119 /* convert to be understandable by quagga */
120 /* convert given addresses */
121 switch (family) {
122 case AF_INET:
123 uchar_to_inaddr(&babel_prefix_addr.ipv4, pref);
124 break;
125 case AF_INET6:
126 uchar_to_in6addr(&babel_prefix_addr.ipv6, pref);
127 break;
128 }
129
130 /* make prefix structure */
131 memset (&quagga_prefix, 0, sizeof(quagga_prefix));
132 quagga_prefix.family = family;
133 switch (family) {
134 case AF_INET:
135 IPV4_ADDR_COPY (&quagga_prefix.u.prefix4, &babel_prefix_addr.ipv4);
136 /* our plen is for v4mapped's addr */
137 quagga_prefix.prefixlen = plen - 96;
138 break;
139 case AF_INET6:
140 IPV6_ADDR_COPY (&quagga_prefix.u.prefix6, &babel_prefix_addr.ipv6);
141 quagga_prefix.prefixlen = plen;
142 break;
143 }
144 apply_mask(&quagga_prefix);
145
146 memset(&api, 0, sizeof(api));
147 api.type = ZEBRA_ROUTE_BABEL;
148 api.safi = SAFI_UNICAST;
149 api.vrf_id = VRF_DEFAULT;
150 api.prefix = quagga_prefix;
151
152 if(metric >= KERNEL_INFINITY) {
153 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
154 } else {
155 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
156 api.nexthop_num = 1;
157 api_nh->ifindex = ifindex;
158 api_nh->vrf_id = VRF_DEFAULT;
159 switch (family) {
160 case AF_INET:
161 uchar_to_inaddr(&api_nh->gate.ipv4, gate);
162 if (IPV4_ADDR_SAME(&api_nh->gate.ipv4, &quagga_prefix.u.prefix4)
163 && quagga_prefix.prefixlen == IPV4_MAX_BITLEN) {
164 api_nh->type = NEXTHOP_TYPE_IFINDEX;
165 } else {
166 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
167 }
168 break;
169 case AF_INET6:
170 uchar_to_in6addr(&api_nh->gate.ipv6, gate);
171 /* difference to IPv4: always leave the linklocal as nexthop */
172 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
173 break;
174 }
175 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
176 api.metric = metric;
177 }
178
179 debugf(BABEL_DEBUG_ROUTE, "%s route (%s) to zebra",
180 add ? "adding" : "removing",
181 (family == AF_INET) ? "ipv4" : "ipv6");
182 return zclient_route_send (add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
183 zclient, &api);
184 }
185
186 int
187 if_eui64(int ifindex, unsigned char *eui)
188 {
189 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
190 if (ifp == NULL) {
191 return -1;
192 }
193
194 uint8_t len = (uint8_t)ifp->hw_addr_len;
195 char *tmp = (void*) ifp->hw_addr;
196
197 if (len == 8) {
198 memcpy(eui, tmp, 8);
199 eui[0] ^= 2;
200 } else if (len == 6) {
201 memcpy(eui, tmp, 3);
202 eui[3] = 0xFF;
203 eui[4] = 0xFE;
204 memcpy(eui+5, tmp+3, 3);
205 } else {
206 return -1;
207 }
208 return 0;
209 }
210
211 /* Like gettimeofday, but returns monotonic time. If POSIX clocks are not
212 available, falls back to gettimeofday but enforces monotonicity. */
213 void
214 gettime(struct timeval *tv)
215 {
216 monotime(tv);
217 }
218
219 /* If /dev/urandom doesn't exist, this will fail with ENOENT, which the
220 caller will deal with gracefully. */
221
222 int
223 read_random_bytes(void *buf, size_t len)
224 {
225 int fd;
226 int rc;
227
228 fd = open("/dev/urandom", O_RDONLY);
229 if(fd < 0) {
230 rc = -1;
231 } else {
232 rc = read(fd, buf, len);
233 if(rc < 0 || (unsigned) rc < len)
234 rc = -1;
235 close(fd);
236 }
237 return rc;
238 }