]> git.proxmox.com Git - mirror_frr.git/blame - babeld/kernel.c
Merge pull request #13199 from pguibert6WIND/isis_ipv4_linklocal
[mirror_frr.git] / babeld / kernel.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: MIT
ca10883e
DS
2/*
3Copyright 2007, 2008 by Grégoire Henry, Julien Cristau and Juliusz Chroboczek
4Copyright 2011, 2012 by Matthieu Boutier and Juliusz Chroboczek
ca10883e
DS
5*/
6
b45ac5f5
DL
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
ca10883e
DS
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"
24a58196 32#include "frrevent.h"
98c02f4c 33#include "nexthop.h"
ca10883e
DS
34
35#include "util.h"
36#include "babel_interface.h"
37#include "babel_zebra.h"
38
39
40static int
c8f9e916
RW
41zebra_route(int add, int familt, const unsigned char *pref, unsigned short plen,
42 const unsigned char *gate, int ifindex, unsigned int metric);
ca10883e
DS
43
44int
45kernel_interface_operational(struct interface *interface)
46{
47 return if_is_operative(interface);
48}
49
50int
51kernel_interface_mtu(struct interface *interface)
52{
53 return MIN(interface->mtu, interface->mtu6);
54}
55
56int
57kernel_interface_wireless(struct interface *interface)
58{
59 return 0;
60}
61
62int
e8c8f2e0
DS
63kernel_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,
ca10883e
DS
66 unsigned int newmetric)
67{
68 int rc;
c8f9e916 69 int family;
ca10883e
DS
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 }
c8f9e916 77 family = AF_INET;
ca10883e
DS
78 } else {
79 if(v4mapped(gate)) {
80 errno = EINVAL;
81 return -1;
82 }
c8f9e916 83 family = AF_INET6;
ca10883e
DS
84 }
85
86 switch (operation) {
87 case ROUTE_ADD:
c8f9e916 88 return zebra_route(1, family, pref, plen, gate, ifindex, metric);
ca10883e 89 case ROUTE_FLUSH:
c8f9e916 90 return zebra_route(0, family, pref, plen, gate, ifindex, metric);
ca10883e
DS
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.");
c8f9e916 96 rc = zebra_route(0, family, pref, plen, gate, ifindex, metric);
ca10883e
DS
97 if (rc < 0)
98 return -1;
99
c8f9e916
RW
100 rc = zebra_route(1, family, pref, plen, newgate, newifindex,
101 newmetric);
ca10883e 102 return rc;
ca10883e 103 }
e8c8f2e0
DS
104
105 return 0;
ca10883e
DS
106}
107
108static int
c8f9e916
RW
109zebra_route(int add, int family, const unsigned char *pref, unsigned short plen,
110 const unsigned char *gate, int ifindex, unsigned int metric)
ca10883e 111{
98c02f4c
DS
112 struct zapi_route api; /* quagga's communication system */
113 struct prefix quagga_prefix; /* quagga's prefix */
c8f9e916
RW
114 union g_addr babel_prefix_addr; /* babeld's prefix addr */
115 struct zapi_nexthop *api_nh; /* next router to go - no ECMP */
bb1b9c47
RW
116
117 api_nh = &api.nexthops[0];
ca10883e
DS
118
119 /* convert to be understandable by quagga */
120 /* convert given addresses */
c8f9e916
RW
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;
ca10883e
DS
128 }
129
ca10883e
DS
130 /* make prefix structure */
131 memset (&quagga_prefix, 0, sizeof(quagga_prefix));
c8f9e916
RW
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 }
98c02f4c 144 apply_mask(&quagga_prefix);
ca10883e 145
e4d9c771 146 memset(&api, 0, sizeof(api));
ca10883e 147 api.type = ZEBRA_ROUTE_BABEL;
ca10883e 148 api.safi = SAFI_UNICAST;
e4d9c771 149 api.vrf_id = VRF_DEFAULT;
bb1b9c47 150 api.prefix = quagga_prefix;
e4d9c771 151
ca10883e 152 if(metric >= KERNEL_INFINITY) {
09a484dd 153 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
ca10883e
DS
154 } else {
155 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
156 api.nexthop_num = 1;
bb1b9c47 157 api_nh->ifindex = ifindex;
4a7371e9
DS
158 api_nh->vrf_id = VRF_DEFAULT;
159 switch (family) {
c8f9e916
RW
160 case AF_INET:
161 uchar_to_inaddr(&api_nh->gate.ipv4, gate);
12256b84
DA
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;
c8f9e916
RW
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 }
ca10883e
DS
175 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
176 api.metric = metric;
177 }
178
c8f9e916
RW
179 debugf(BABEL_DEBUG_ROUTE, "%s route (%s) to zebra",
180 add ? "adding" : "removing",
181 (family == AF_INET) ? "ipv4" : "ipv6");
0e51b4a3
RW
182 return zclient_route_send (add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE,
183 zclient, &api);
ca10883e
DS
184}
185
186int
edb1d4a0 187if_eui64(int ifindex, unsigned char *eui)
ca10883e
DS
188{
189 struct interface *ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
190 if (ifp == NULL) {
191 return -1;
192 }
7217de2d 193
d7c0a89a 194 uint8_t len = (uint8_t)ifp->hw_addr_len;
ca10883e 195 char *tmp = (void*) ifp->hw_addr;
7217de2d 196
ca10883e
DS
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. */
c392d9f4 213void
ca10883e
DS
214gettime(struct timeval *tv)
215{
c392d9f4 216 monotime(tv);
ca10883e
DS
217}
218
219/* If /dev/urandom doesn't exist, this will fail with ENOENT, which the
220 caller will deal with gracefully. */
221
222int
223read_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}