]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Kernel routing table updates using netlink over GNU/Linux system. |
2 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
ddfeb486 DL |
22 | |
23 | #ifdef HAVE_NETLINK | |
24 | ||
8ccc7e80 | 25 | #include <net/if_arp.h> |
ba777396 RW |
26 | #include <linux/lwtunnel.h> |
27 | #include <linux/mpls_iptunnel.h> | |
28 | #include <linux/neighbour.h> | |
29 | #include <linux/rtnetlink.h> | |
718e3744 | 30 | |
31 | /* Hack for GNU libc version 2. */ | |
32 | #ifndef MSG_TRUNC | |
33 | #define MSG_TRUNC 0x20 | |
34 | #endif /* MSG_TRUNC */ | |
35 | ||
36 | #include "linklist.h" | |
37 | #include "if.h" | |
38 | #include "log.h" | |
39 | #include "prefix.h" | |
40 | #include "connected.h" | |
41 | #include "table.h" | |
26e2ae36 | 42 | #include "memory.h" |
4a1ab8e4 | 43 | #include "zebra_memory.h" |
718e3744 | 44 | #include "rib.h" |
e04ab74d | 45 | #include "thread.h" |
edd7c245 | 46 | #include "privs.h" |
fb018d25 | 47 | #include "nexthop.h" |
78104b9b | 48 | #include "vrf.h" |
5e6a74d8 | 49 | #include "vty.h" |
40c7bdb0 | 50 | #include "mpls.h" |
13d60d35 | 51 | #include "vxlan.h" |
718e3744 | 52 | |
bf094f69 | 53 | #include "zebra/zapi_msg.h" |
fe18ee2d | 54 | #include "zebra/zebra_ns.h" |
7c551956 | 55 | #include "zebra/zebra_vrf.h" |
6621ca86 | 56 | #include "zebra/rt.h" |
718e3744 | 57 | #include "zebra/redistribute.h" |
58 | #include "zebra/interface.h" | |
59 | #include "zebra/debug.h" | |
12f6fb97 | 60 | #include "zebra/rtadv.h" |
567b877d | 61 | #include "zebra/zebra_ptm.h" |
40c7bdb0 | 62 | #include "zebra/zebra_mpls.h" |
1fdc9eae | 63 | #include "zebra/kernel_netlink.h" |
64 | #include "zebra/rt_netlink.h" | |
e3be0432 | 65 | #include "zebra/zebra_mroute.h" |
2232a77c | 66 | #include "zebra/zebra_vxlan.h" |
e3be0432 | 67 | |
40c7bdb0 | 68 | #ifndef AF_MPLS |
69 | #define AF_MPLS 28 | |
70 | #endif | |
71 | ||
2232a77c | 72 | static vlanid_t filter_vlan = 0; |
73 | ||
d62a17ae | 74 | struct gw_family_t { |
d7c0a89a QY |
75 | uint16_t filler; |
76 | uint16_t family; | |
d62a17ae | 77 | union g_addr gate; |
40c7bdb0 | 78 | }; |
79 | ||
8755598a DS |
80 | char ipv4_ll_buf[16] = "169.254.0.1"; |
81 | struct in_addr ipv4_ll; | |
82 | ||
83 | /* | |
84 | * The ipv4_ll data structure is used for all 5549 | |
85 | * additions to the kernel. Let's figure out the | |
86 | * correct value one time instead for every | |
87 | * install/remove of a 5549 type route | |
88 | */ | |
d62a17ae | 89 | void rt_netlink_init(void) |
8755598a | 90 | { |
d62a17ae | 91 | inet_pton(AF_INET, ipv4_ll_buf, &ipv4_ll); |
8755598a DS |
92 | } |
93 | ||
23b1f334 DD |
94 | static inline int is_selfroute(int proto) |
95 | { | |
d62a17ae | 96 | if ((proto == RTPROT_BGP) || (proto == RTPROT_OSPF) |
d4d71f11 | 97 | || (proto == RTPROT_ZSTATIC) || (proto == RTPROT_ZEBRA) |
d62a17ae | 98 | || (proto == RTPROT_ISIS) || (proto == RTPROT_RIPNG) |
99 | || (proto == RTPROT_NHRP) || (proto == RTPROT_EIGRP) | |
915902cb | 100 | || (proto == RTPROT_LDP) || (proto == RTPROT_BABEL) |
0761368a DS |
101 | || (proto == RTPROT_RIP) || (proto == RTPROT_SHARP) |
102 | || (proto == RTPROT_PBR)) { | |
d62a17ae | 103 | return 1; |
104 | } | |
105 | ||
106 | return 0; | |
23b1f334 DD |
107 | } |
108 | ||
915902cb | 109 | static inline int zebra2proto(int proto) |
23b1f334 | 110 | { |
d62a17ae | 111 | switch (proto) { |
112 | case ZEBRA_ROUTE_BABEL: | |
113 | proto = RTPROT_BABEL; | |
114 | break; | |
115 | case ZEBRA_ROUTE_BGP: | |
116 | proto = RTPROT_BGP; | |
117 | break; | |
118 | case ZEBRA_ROUTE_OSPF: | |
119 | case ZEBRA_ROUTE_OSPF6: | |
120 | proto = RTPROT_OSPF; | |
121 | break; | |
122 | case ZEBRA_ROUTE_STATIC: | |
d4d71f11 | 123 | proto = RTPROT_ZSTATIC; |
d62a17ae | 124 | break; |
125 | case ZEBRA_ROUTE_ISIS: | |
126 | proto = RTPROT_ISIS; | |
127 | break; | |
128 | case ZEBRA_ROUTE_RIP: | |
129 | proto = RTPROT_RIP; | |
130 | break; | |
131 | case ZEBRA_ROUTE_RIPNG: | |
132 | proto = RTPROT_RIPNG; | |
133 | break; | |
134 | case ZEBRA_ROUTE_NHRP: | |
135 | proto = RTPROT_NHRP; | |
136 | break; | |
137 | case ZEBRA_ROUTE_EIGRP: | |
138 | proto = RTPROT_EIGRP; | |
139 | break; | |
140 | case ZEBRA_ROUTE_LDP: | |
141 | proto = RTPROT_LDP; | |
142 | break; | |
8a71d93d DS |
143 | case ZEBRA_ROUTE_SHARP: |
144 | proto = RTPROT_SHARP; | |
145 | break; | |
0761368a DS |
146 | case ZEBRA_ROUTE_PBR: |
147 | proto = RTPROT_PBR; | |
148 | break; | |
d62a17ae | 149 | default: |
0761368a DS |
150 | /* |
151 | * When a user adds a new protocol this will show up | |
152 | * to let them know to do something about it. This | |
153 | * is intentionally a warn because we should see | |
154 | * this as part of development of a new protocol | |
155 | */ | |
156 | zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling", | |
157 | __PRETTY_FUNCTION__, proto); | |
d62a17ae | 158 | proto = RTPROT_ZEBRA; |
159 | break; | |
160 | } | |
161 | ||
162 | return proto; | |
23b1f334 DD |
163 | } |
164 | ||
915902cb DS |
165 | static inline int proto2zebra(int proto, int family) |
166 | { | |
167 | switch (proto) { | |
168 | case RTPROT_BABEL: | |
169 | proto = ZEBRA_ROUTE_BABEL; | |
170 | break; | |
171 | case RTPROT_BGP: | |
172 | proto = ZEBRA_ROUTE_BGP; | |
173 | break; | |
174 | case RTPROT_OSPF: | |
996c9314 LB |
175 | proto = (family == AFI_IP) ? ZEBRA_ROUTE_OSPF |
176 | : ZEBRA_ROUTE_OSPF6; | |
915902cb DS |
177 | break; |
178 | case RTPROT_ISIS: | |
179 | proto = ZEBRA_ROUTE_ISIS; | |
180 | break; | |
181 | case RTPROT_RIP: | |
182 | proto = ZEBRA_ROUTE_RIP; | |
183 | break; | |
184 | case RTPROT_RIPNG: | |
185 | proto = ZEBRA_ROUTE_RIPNG; | |
186 | break; | |
187 | case RTPROT_NHRP: | |
188 | proto = ZEBRA_ROUTE_NHRP; | |
189 | break; | |
190 | case RTPROT_EIGRP: | |
191 | proto = ZEBRA_ROUTE_EIGRP; | |
192 | break; | |
193 | case RTPROT_LDP: | |
194 | proto = ZEBRA_ROUTE_LDP; | |
195 | break; | |
196 | case RTPROT_STATIC: | |
d4d71f11 | 197 | case RTPROT_ZSTATIC: |
915902cb DS |
198 | proto = ZEBRA_ROUTE_STATIC; |
199 | break; | |
0761368a DS |
200 | case RTPROT_SHARP: |
201 | proto = ZEBRA_ROUTE_SHARP; | |
202 | break; | |
203 | case RTPROT_PBR: | |
204 | proto = ZEBRA_ROUTE_PBR; | |
205 | break; | |
915902cb | 206 | default: |
0761368a DS |
207 | /* |
208 | * When a user adds a new protocol this will show up | |
209 | * to let them know to do something about it. This | |
210 | * is intentionally a warn because we should see | |
211 | * this as part of development of a new protocol | |
212 | */ | |
213 | zlog_warn("%s: Please add this protocol(%d) to proper rt_netlink.c handling", | |
214 | __PRETTY_FUNCTION__, | |
215 | proto); | |
915902cb DS |
216 | proto = ZEBRA_ROUTE_KERNEL; |
217 | break; | |
218 | } | |
219 | return proto; | |
220 | } | |
221 | ||
12f6fb97 DS |
222 | /* |
223 | Pending: create an efficient table_id (in a tree/hash) based lookup) | |
224 | */ | |
d7c0a89a | 225 | static vrf_id_t vrf_lookup_by_table(uint32_t table_id, ns_id_t ns_id) |
12f6fb97 | 226 | { |
d62a17ae | 227 | struct vrf *vrf; |
228 | struct zebra_vrf *zvrf; | |
12f6fb97 | 229 | |
a2addae8 | 230 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
78dd30b2 PG |
231 | zvrf = vrf->info; |
232 | if (zvrf == NULL) | |
d62a17ae | 233 | continue; |
78dd30b2 PG |
234 | /* case vrf with netns : match the netnsid */ |
235 | if (vrf_is_backend_netns()) { | |
236 | if (ns_id == zvrf_id(zvrf)) | |
237 | return zvrf_id(zvrf); | |
238 | } else { | |
239 | /* VRF is VRF_BACKEND_VRF_LITE */ | |
240 | if (zvrf->table_id != table_id) | |
241 | continue; | |
242 | return zvrf_id(zvrf); | |
243 | } | |
d62a17ae | 244 | } |
12f6fb97 | 245 | |
d62a17ae | 246 | return VRF_DEFAULT; |
12f6fb97 DS |
247 | } |
248 | ||
87da6a60 SW |
249 | /** |
250 | * @parse_encap_mpls() - Parses encapsulated mpls attributes | |
251 | * @tb: Pointer to rtattr to look for nested items in. | |
252 | * @labels: Pointer to store labels in. | |
253 | * | |
254 | * Return: Number of mpls labels found. | |
255 | */ | |
256 | static int parse_encap_mpls(struct rtattr *tb, mpls_label_t *labels) | |
257 | { | |
258 | struct rtattr *tb_encap[MPLS_IPTUNNEL_MAX + 1] = {0}; | |
259 | mpls_lse_t *lses = NULL; | |
260 | int num_labels = 0; | |
261 | uint32_t ttl = 0; | |
262 | uint32_t bos = 0; | |
263 | uint32_t exp = 0; | |
264 | mpls_label_t label = 0; | |
265 | ||
266 | netlink_parse_rtattr_nested(tb_encap, MPLS_IPTUNNEL_MAX, tb); | |
267 | lses = (mpls_lse_t *)RTA_DATA(tb_encap[MPLS_IPTUNNEL_DST]); | |
268 | while (!bos && num_labels < MPLS_MAX_LABELS) { | |
269 | mpls_lse_decode(lses[num_labels], &label, &ttl, &exp, &bos); | |
270 | labels[num_labels++] = label; | |
271 | } | |
272 | ||
273 | return num_labels; | |
274 | } | |
275 | ||
718e3744 | 276 | /* Looking up routing table by netlink interface. */ |
2414abd3 | 277 | static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id, |
d62a17ae | 278 | int startup) |
718e3744 | 279 | { |
d62a17ae | 280 | int len; |
281 | struct rtmsg *rtm; | |
282 | struct rtattr *tb[RTA_MAX + 1]; | |
d7c0a89a | 283 | uint8_t flags = 0; |
d62a17ae | 284 | struct prefix p; |
792fa92e | 285 | struct prefix_ipv6 src_p = {}; |
78dd30b2 | 286 | vrf_id_t vrf_id; |
d62a17ae | 287 | |
288 | char anyaddr[16] = {0}; | |
289 | ||
915902cb | 290 | int proto = ZEBRA_ROUTE_KERNEL; |
d62a17ae | 291 | int index = 0; |
292 | int table; | |
293 | int metric = 0; | |
d7c0a89a | 294 | uint32_t mtu = 0; |
25715c7e | 295 | uint8_t distance = 0; |
4e40b6d6 | 296 | route_tag_t tag = 0; |
d62a17ae | 297 | |
298 | void *dest = NULL; | |
299 | void *gate = NULL; | |
300 | void *prefsrc = NULL; /* IPv4 preferred source host address */ | |
301 | void *src = NULL; /* IPv6 srcdest source prefix */ | |
e655a03c | 302 | enum blackhole_type bh_type = BLACKHOLE_UNSPEC; |
d62a17ae | 303 | |
87da6a60 SW |
304 | /* MPLS labels */ |
305 | mpls_label_t labels[MPLS_MAX_LABELS] = {0}; | |
306 | int num_labels = 0; | |
307 | ||
d62a17ae | 308 | rtm = NLMSG_DATA(h); |
309 | ||
310 | if (startup && h->nlmsg_type != RTM_NEWROUTE) | |
311 | return 0; | |
e655a03c DL |
312 | switch (rtm->rtm_type) { |
313 | case RTN_UNICAST: | |
314 | break; | |
315 | case RTN_BLACKHOLE: | |
316 | bh_type = BLACKHOLE_NULL; | |
317 | break; | |
318 | case RTN_UNREACHABLE: | |
319 | bh_type = BLACKHOLE_REJECT; | |
320 | break; | |
321 | case RTN_PROHIBIT: | |
322 | bh_type = BLACKHOLE_ADMINPROHIB; | |
323 | break; | |
324 | default: | |
d62a17ae | 325 | return 0; |
e655a03c | 326 | } |
d62a17ae | 327 | |
328 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
9bdf8618 DS |
329 | if (len < 0) { |
330 | zlog_err("%s: Message received from netlink is of a broken size %d %zu", | |
331 | __PRETTY_FUNCTION__, h->nlmsg_len, | |
332 | (size_t)NLMSG_LENGTH(sizeof(struct rtmsg))); | |
d62a17ae | 333 | return -1; |
9bdf8618 | 334 | } |
d62a17ae | 335 | |
336 | memset(tb, 0, sizeof tb); | |
337 | netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len); | |
338 | ||
339 | if (rtm->rtm_flags & RTM_F_CLONED) | |
340 | return 0; | |
341 | if (rtm->rtm_protocol == RTPROT_REDIRECT) | |
342 | return 0; | |
343 | if (rtm->rtm_protocol == RTPROT_KERNEL) | |
344 | return 0; | |
345 | ||
346 | if (!startup && is_selfroute(rtm->rtm_protocol) | |
6ab5222f DS |
347 | && h->nlmsg_type == RTM_NEWROUTE) { |
348 | if (IS_ZEBRA_DEBUG_KERNEL) | |
349 | zlog_debug("Route type: %d Received that we think we have originated, ignoring", | |
350 | rtm->rtm_protocol); | |
d62a17ae | 351 | return 0; |
6ab5222f | 352 | } |
d62a17ae | 353 | |
354 | /* We don't care about change notifications for the MPLS table. */ | |
355 | /* TODO: Revisit this. */ | |
356 | if (rtm->rtm_family == AF_MPLS) | |
357 | return 0; | |
358 | ||
359 | /* Table corresponding to route. */ | |
360 | if (tb[RTA_TABLE]) | |
361 | table = *(int *)RTA_DATA(tb[RTA_TABLE]); | |
362 | else | |
363 | table = rtm->rtm_table; | |
364 | ||
365 | /* Map to VRF */ | |
78dd30b2 | 366 | vrf_id = vrf_lookup_by_table(table, ns_id); |
d62a17ae | 367 | if (vrf_id == VRF_DEFAULT) { |
368 | if (!is_zebra_valid_kernel_table(table) | |
369 | && !is_zebra_main_routing_table(table)) | |
370 | return 0; | |
371 | } | |
372 | ||
373 | /* Route which inserted by Zebra. */ | |
915902cb | 374 | if (is_selfroute(rtm->rtm_protocol)) { |
d62a17ae | 375 | flags |= ZEBRA_FLAG_SELFROUTE; |
915902cb DS |
376 | proto = proto2zebra(rtm->rtm_protocol, rtm->rtm_family); |
377 | } | |
d62a17ae | 378 | if (tb[RTA_OIF]) |
379 | index = *(int *)RTA_DATA(tb[RTA_OIF]); | |
380 | ||
381 | if (tb[RTA_DST]) | |
382 | dest = RTA_DATA(tb[RTA_DST]); | |
383 | else | |
384 | dest = anyaddr; | |
385 | ||
386 | if (tb[RTA_SRC]) | |
387 | src = RTA_DATA(tb[RTA_SRC]); | |
388 | else | |
389 | src = anyaddr; | |
390 | ||
391 | if (tb[RTA_PREFSRC]) | |
392 | prefsrc = RTA_DATA(tb[RTA_PREFSRC]); | |
393 | ||
394 | if (tb[RTA_GATEWAY]) | |
395 | gate = RTA_DATA(tb[RTA_GATEWAY]); | |
396 | ||
f19435a8 DS |
397 | if (tb[RTA_PRIORITY]) |
398 | metric = *(int *)RTA_DATA(tb[RTA_PRIORITY]); | |
d62a17ae | 399 | |
4e40b6d6 KK |
400 | #if defined(SUPPORT_REALMS) |
401 | if (tb[RTA_FLOW]) | |
402 | tag = *(uint32_t *)RTA_DATA(tb[RTA_FLOW]); | |
403 | #endif | |
404 | ||
f19435a8 DS |
405 | if (tb[RTA_METRICS]) { |
406 | struct rtattr *mxrta[RTAX_MAX + 1]; | |
d62a17ae | 407 | |
f19435a8 | 408 | memset(mxrta, 0, sizeof mxrta); |
996c9314 | 409 | netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), |
f19435a8 | 410 | RTA_PAYLOAD(tb[RTA_METRICS])); |
d62a17ae | 411 | |
f19435a8 | 412 | if (mxrta[RTAX_MTU]) |
d7c0a89a | 413 | mtu = *(uint32_t *)RTA_DATA(mxrta[RTAX_MTU]); |
d62a17ae | 414 | } |
415 | ||
416 | if (rtm->rtm_family == AF_INET) { | |
417 | p.family = AF_INET; | |
930571d2 | 418 | if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) { |
e17d9b2d | 419 | zlog_err( |
75829703 | 420 | "Invalid destination prefix length: %u received from kernel route change", |
930571d2 | 421 | rtm->rtm_dst_len); |
e17d9b2d | 422 | return -1; |
930571d2 | 423 | } |
d62a17ae | 424 | memcpy(&p.u.prefix4, dest, 4); |
425 | p.prefixlen = rtm->rtm_dst_len; | |
426 | ||
1f610a1f CF |
427 | if (rtm->rtm_src_len != 0) { |
428 | char buf[PREFIX_STRLEN]; | |
429 | zlog_warn("unsupported IPv4 sourcedest route (dest %s vrf %u)", | |
430 | prefix2str(&p, buf, sizeof(buf)), vrf_id); | |
431 | return 0; | |
432 | } | |
930571d2 | 433 | |
1f610a1f CF |
434 | /* Force debug below to not display anything for source */ |
435 | src_p.prefixlen = 0; | |
d62a17ae | 436 | } else if (rtm->rtm_family == AF_INET6) { |
437 | p.family = AF_INET6; | |
930571d2 | 438 | if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) { |
e17d9b2d | 439 | zlog_err( |
75829703 | 440 | "Invalid destination prefix length: %u received from kernel route change", |
930571d2 | 441 | rtm->rtm_dst_len); |
e17d9b2d | 442 | return -1; |
930571d2 | 443 | } |
d62a17ae | 444 | memcpy(&p.u.prefix6, dest, 16); |
445 | p.prefixlen = rtm->rtm_dst_len; | |
446 | ||
447 | src_p.family = AF_INET6; | |
930571d2 | 448 | if (rtm->rtm_src_len > IPV6_MAX_BITLEN) { |
e17d9b2d | 449 | zlog_err( |
75829703 | 450 | "Invalid source prefix length: %u received from kernel route change", |
930571d2 | 451 | rtm->rtm_src_len); |
e17d9b2d | 452 | return -1; |
930571d2 | 453 | } |
d62a17ae | 454 | memcpy(&src_p.prefix, src, 16); |
455 | src_p.prefixlen = rtm->rtm_src_len; | |
456 | } | |
457 | ||
25715c7e DS |
458 | /* |
459 | * For ZEBRA_ROUTE_KERNEL types: | |
460 | * | |
461 | * The metric/priority of the route received from the kernel | |
462 | * is a 32 bit number. We are going to interpret the high | |
463 | * order byte as the Admin Distance and the low order 3 bytes | |
464 | * as the metric. | |
465 | * | |
466 | * This will allow us to do two things: | |
467 | * 1) Allow the creation of kernel routes that can be | |
468 | * overridden by zebra. | |
469 | * 2) Allow the old behavior for 'most' kernel route types | |
470 | * if a user enters 'ip route ...' v4 routes get a metric | |
471 | * of 0 and v6 routes get a metric of 1024. Both of these | |
472 | * values will end up with a admin distance of 0, which | |
473 | * will cause them to win for the purposes of zebra. | |
474 | */ | |
475 | if (proto == ZEBRA_ROUTE_KERNEL) { | |
476 | distance = (metric >> 24) & 0xFF; | |
996c9314 | 477 | metric = (metric & 0x00FFFFFF); |
25715c7e DS |
478 | } |
479 | ||
d62a17ae | 480 | if (IS_ZEBRA_DEBUG_KERNEL) { |
481 | char buf[PREFIX_STRLEN]; | |
482 | char buf2[PREFIX_STRLEN]; | |
45df4e96 | 483 | zlog_debug("%s %s%s%s vrf %u(%u) metric: %d Admin Distance: %d", |
996c9314 LB |
484 | nl_msg_type_to_str(h->nlmsg_type), |
485 | prefix2str(&p, buf, sizeof(buf)), | |
486 | src_p.prefixlen ? " from " : "", | |
487 | src_p.prefixlen | |
488 | ? prefix2str(&src_p, buf2, sizeof(buf2)) | |
489 | : "", | |
45df4e96 | 490 | vrf_id, table, metric, distance); |
d62a17ae | 491 | } |
492 | ||
493 | afi_t afi = AFI_IP; | |
494 | if (rtm->rtm_family == AF_INET6) | |
495 | afi = AFI_IP6; | |
496 | ||
497 | if (h->nlmsg_type == RTM_NEWROUTE) { | |
8795f904 DS |
498 | struct interface *ifp; |
499 | vrf_id_t nh_vrf_id = vrf_id; | |
500 | ||
fd36be7e DL |
501 | if (!tb[RTA_MULTIPATH]) { |
502 | struct nexthop nh; | |
503 | size_t sz = (afi == AFI_IP) ? 4 : 16; | |
504 | ||
505 | memset(&nh, 0, sizeof(nh)); | |
af760ec1 DS |
506 | |
507 | if (bh_type == BLACKHOLE_UNSPEC) { | |
508 | if (index && !gate) | |
509 | nh.type = NEXTHOP_TYPE_IFINDEX; | |
510 | else if (index && gate) | |
996c9314 LB |
511 | nh.type = |
512 | (afi == AFI_IP) | |
513 | ? NEXTHOP_TYPE_IPV4_IFINDEX | |
514 | : NEXTHOP_TYPE_IPV6_IFINDEX; | |
af760ec1 | 515 | else if (!index && gate) |
1dca2eaa RW |
516 | nh.type = (afi == AFI_IP) |
517 | ? NEXTHOP_TYPE_IPV4 | |
518 | : NEXTHOP_TYPE_IPV6; | |
af760ec1 DS |
519 | else { |
520 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
521 | nh.bh_type = bh_type; | |
522 | } | |
523 | } else { | |
fd36be7e | 524 | nh.type = NEXTHOP_TYPE_BLACKHOLE; |
e655a03c DL |
525 | nh.bh_type = bh_type; |
526 | } | |
fd36be7e DL |
527 | nh.ifindex = index; |
528 | if (prefsrc) | |
529 | memcpy(&nh.src, prefsrc, sz); | |
530 | if (gate) | |
531 | memcpy(&nh.gate, gate, sz); | |
915902cb | 532 | |
8795f904 | 533 | if (index) { |
fac4d51e PG |
534 | ifp = if_lookup_by_index_per_ns( |
535 | zebra_ns_lookup(ns_id), | |
536 | index); | |
8795f904 DS |
537 | if (ifp) |
538 | nh_vrf_id = ifp->vrf_id; | |
539 | } | |
4a7371e9 | 540 | nh.vrf_id = nh_vrf_id; |
8795f904 | 541 | |
87da6a60 SW |
542 | if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE] |
543 | && *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE]) | |
544 | == LWTUNNEL_ENCAP_MPLS) { | |
545 | num_labels = | |
546 | parse_encap_mpls(tb[RTA_ENCAP], labels); | |
547 | } | |
548 | ||
549 | if (num_labels) | |
550 | nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, | |
551 | num_labels, labels); | |
552 | ||
4a7371e9 | 553 | rib_add(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, &p, |
1f610a1f | 554 | &src_p, &nh, table, metric, mtu, distance, tag); |
fd36be7e | 555 | } else { |
d62a17ae | 556 | /* This is a multipath route */ |
557 | ||
558 | struct route_entry *re; | |
559 | struct rtnexthop *rtnh = | |
560 | (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]); | |
561 | ||
562 | len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); | |
563 | ||
564 | re = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); | |
915902cb | 565 | re->type = proto; |
25715c7e | 566 | re->distance = distance; |
d62a17ae | 567 | re->flags = flags; |
568 | re->metric = metric; | |
569 | re->mtu = mtu; | |
570 | re->vrf_id = vrf_id; | |
571 | re->table = table; | |
572 | re->nexthop_num = 0; | |
573 | re->uptime = time(NULL); | |
4e40b6d6 | 574 | re->tag = tag; |
d62a17ae | 575 | |
576 | for (;;) { | |
87da6a60 | 577 | struct nexthop *nh = NULL; |
4a7371e9 | 578 | vrf_id_t nh_vrf_id; |
d62a17ae | 579 | if (len < (int)sizeof(*rtnh) |
580 | || rtnh->rtnh_len > len) | |
581 | break; | |
582 | ||
583 | index = rtnh->rtnh_ifindex; | |
8795f904 DS |
584 | if (index) { |
585 | /* | |
586 | * Yes we are looking this up | |
587 | * for every nexthop and just | |
588 | * using the last one looked | |
589 | * up right now | |
590 | */ | |
fac4d51e PG |
591 | ifp = if_lookup_by_index_per_ns( |
592 | zebra_ns_lookup(ns_id), | |
593 | index); | |
8795f904 | 594 | if (ifp) |
4a7371e9 DS |
595 | nh_vrf_id = ifp->vrf_id; |
596 | else { | |
597 | zlog_warn( | |
598 | "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT", | |
599 | __PRETTY_FUNCTION__, | |
600 | index); | |
601 | nh_vrf_id = VRF_DEFAULT; | |
602 | } | |
603 | } else | |
604 | nh_vrf_id = vrf_id; | |
605 | ||
d62a17ae | 606 | gate = 0; |
607 | if (rtnh->rtnh_len > sizeof(*rtnh)) { | |
608 | memset(tb, 0, sizeof(tb)); | |
609 | netlink_parse_rtattr( | |
610 | tb, RTA_MAX, RTNH_DATA(rtnh), | |
611 | rtnh->rtnh_len - sizeof(*rtnh)); | |
612 | if (tb[RTA_GATEWAY]) | |
613 | gate = RTA_DATA( | |
614 | tb[RTA_GATEWAY]); | |
87da6a60 SW |
615 | if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE] |
616 | && *(uint16_t *)RTA_DATA( | |
617 | tb[RTA_ENCAP_TYPE]) | |
618 | == LWTUNNEL_ENCAP_MPLS) { | |
619 | num_labels = parse_encap_mpls( | |
620 | tb[RTA_ENCAP], labels); | |
621 | } | |
d62a17ae | 622 | } |
623 | ||
624 | if (gate) { | |
625 | if (rtm->rtm_family == AF_INET) { | |
626 | if (index) | |
87da6a60 | 627 | nh = route_entry_nexthop_ipv4_ifindex_add( |
d62a17ae | 628 | re, gate, |
4a7371e9 DS |
629 | prefsrc, index, |
630 | nh_vrf_id); | |
d62a17ae | 631 | else |
87da6a60 | 632 | nh = route_entry_nexthop_ipv4_add( |
d62a17ae | 633 | re, gate, |
4a7371e9 DS |
634 | prefsrc, |
635 | nh_vrf_id); | |
d62a17ae | 636 | } else if (rtm->rtm_family |
637 | == AF_INET6) { | |
638 | if (index) | |
87da6a60 | 639 | nh = route_entry_nexthop_ipv6_ifindex_add( |
4a7371e9 DS |
640 | re, gate, index, |
641 | nh_vrf_id); | |
d62a17ae | 642 | else |
87da6a60 | 643 | nh = route_entry_nexthop_ipv6_add( |
4a7371e9 DS |
644 | re, gate, |
645 | nh_vrf_id); | |
d62a17ae | 646 | } |
647 | } else | |
87da6a60 | 648 | nh = route_entry_nexthop_ifindex_add( |
4a7371e9 | 649 | re, index, nh_vrf_id); |
d62a17ae | 650 | |
87da6a60 SW |
651 | if (nh && num_labels) |
652 | nexthop_add_labels(nh, ZEBRA_LSP_STATIC, | |
653 | num_labels, labels); | |
654 | ||
3c04071d SW |
655 | if (rtnh->rtnh_len == 0) |
656 | break; | |
657 | ||
d62a17ae | 658 | len -= NLMSG_ALIGN(rtnh->rtnh_len); |
659 | rtnh = RTNH_NEXT(rtnh); | |
660 | } | |
661 | ||
662 | zserv_nexthop_num_warn(__func__, | |
663 | (const struct prefix *)&p, | |
664 | re->nexthop_num); | |
665 | if (re->nexthop_num == 0) | |
666 | XFREE(MTYPE_RE, re); | |
667 | else | |
1f610a1f CF |
668 | rib_add_multipath(afi, SAFI_UNICAST, &p, |
669 | &src_p, re); | |
d62a17ae | 670 | } |
671 | } else { | |
fd36be7e DL |
672 | if (!tb[RTA_MULTIPATH]) { |
673 | struct nexthop nh; | |
674 | size_t sz = (afi == AFI_IP) ? 4 : 16; | |
675 | ||
676 | memset(&nh, 0, sizeof(nh)); | |
8ba5bd58 RW |
677 | if (bh_type == BLACKHOLE_UNSPEC) { |
678 | if (index && !gate) | |
679 | nh.type = NEXTHOP_TYPE_IFINDEX; | |
680 | else if (index && gate) | |
681 | nh.type = | |
682 | (afi == AFI_IP) | |
683 | ? NEXTHOP_TYPE_IPV4_IFINDEX | |
684 | : NEXTHOP_TYPE_IPV6_IFINDEX; | |
685 | else if (!index && gate) | |
686 | nh.type = (afi == AFI_IP) | |
687 | ? NEXTHOP_TYPE_IPV4 | |
60466a63 | 688 | : NEXTHOP_TYPE_IPV6; |
8ba5bd58 RW |
689 | else { |
690 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
691 | nh.bh_type = BLACKHOLE_UNSPEC; | |
692 | } | |
693 | } else { | |
fd36be7e | 694 | nh.type = NEXTHOP_TYPE_BLACKHOLE; |
8ba5bd58 RW |
695 | nh.bh_type = bh_type; |
696 | } | |
fd36be7e DL |
697 | nh.ifindex = index; |
698 | if (gate) | |
699 | memcpy(&nh.gate, gate, sz); | |
996c9314 | 700 | rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, |
51c4ed0a DS |
701 | &p, &src_p, &nh, table, metric, distance, |
702 | true); | |
fd36be7e DL |
703 | } else { |
704 | /* XXX: need to compare the entire list of nexthops | |
705 | * here for NLM_F_APPEND stupidity */ | |
996c9314 | 706 | rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, |
51c4ed0a DS |
707 | &p, &src_p, NULL, table, metric, distance, |
708 | true); | |
d62a17ae | 709 | } |
710 | } | |
711 | ||
712 | return 0; | |
718e3744 | 713 | } |
714 | ||
e3be0432 DS |
715 | static struct mcast_route_data *mroute = NULL; |
716 | ||
2414abd3 | 717 | static int netlink_route_change_read_multicast(struct nlmsghdr *h, |
d62a17ae | 718 | ns_id_t ns_id, int startup) |
565fdc75 | 719 | { |
d62a17ae | 720 | int len; |
721 | struct rtmsg *rtm; | |
722 | struct rtattr *tb[RTA_MAX + 1]; | |
723 | struct mcast_route_data *m; | |
724 | struct mcast_route_data mr; | |
725 | int iif = 0; | |
726 | int count; | |
727 | int oif[256]; | |
728 | int oif_count = 0; | |
729 | char sbuf[40]; | |
730 | char gbuf[40]; | |
731 | char oif_list[256] = "\0"; | |
78dd30b2 | 732 | vrf_id_t vrf; |
43b5cc5e | 733 | int table; |
d62a17ae | 734 | |
735 | if (mroute) | |
736 | m = mroute; | |
737 | else { | |
738 | memset(&mr, 0, sizeof(mr)); | |
739 | m = &mr; | |
740 | } | |
741 | ||
742 | rtm = NLMSG_DATA(h); | |
743 | ||
744 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
745 | ||
746 | memset(tb, 0, sizeof tb); | |
747 | netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len); | |
90d82769 | 748 | |
43b5cc5e DS |
749 | if (tb[RTA_TABLE]) |
750 | table = *(int *)RTA_DATA(tb[RTA_TABLE]); | |
751 | else | |
752 | table = rtm->rtm_table; | |
753 | ||
78dd30b2 | 754 | vrf = vrf_lookup_by_table(table, ns_id); |
43b5cc5e | 755 | |
d62a17ae | 756 | if (tb[RTA_IIF]) |
757 | iif = *(int *)RTA_DATA(tb[RTA_IIF]); | |
758 | ||
759 | if (tb[RTA_SRC]) | |
bd8b9272 | 760 | m->sg.src = *(struct in_addr *)RTA_DATA(tb[RTA_SRC]); |
d62a17ae | 761 | |
762 | if (tb[RTA_DST]) | |
bd8b9272 | 763 | m->sg.grp = *(struct in_addr *)RTA_DATA(tb[RTA_DST]); |
d62a17ae | 764 | |
765 | if ((RTA_EXPIRES <= RTA_MAX) && tb[RTA_EXPIRES]) | |
766 | m->lastused = *(unsigned long long *)RTA_DATA(tb[RTA_EXPIRES]); | |
767 | ||
768 | if (tb[RTA_MULTIPATH]) { | |
769 | struct rtnexthop *rtnh = | |
770 | (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]); | |
771 | ||
772 | len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); | |
773 | for (;;) { | |
774 | if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len) | |
775 | break; | |
776 | ||
777 | oif[oif_count] = rtnh->rtnh_ifindex; | |
778 | oif_count++; | |
779 | ||
3c04071d SW |
780 | if (rtnh->rtnh_len == 0) |
781 | break; | |
782 | ||
d62a17ae | 783 | len -= NLMSG_ALIGN(rtnh->rtnh_len); |
784 | rtnh = RTNH_NEXT(rtnh); | |
785 | } | |
786 | } | |
787 | ||
788 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
789 | struct interface *ifp; | |
0af35d90 RW |
790 | strlcpy(sbuf, inet_ntoa(m->sg.src), sizeof(sbuf)); |
791 | strlcpy(gbuf, inet_ntoa(m->sg.grp), sizeof(gbuf)); | |
d62a17ae | 792 | for (count = 0; count < oif_count; count++) { |
793 | ifp = if_lookup_by_index(oif[count], vrf); | |
794 | char temp[256]; | |
795 | ||
796 | sprintf(temp, "%s ", ifp->name); | |
797 | strcat(oif_list, temp); | |
798 | } | |
43b5cc5e | 799 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf); |
d62a17ae | 800 | ifp = if_lookup_by_index(iif, vrf); |
43b5cc5e DS |
801 | zlog_debug( |
802 | "MCAST VRF: %s(%d) %s (%s,%s) IIF: %s OIF: %s jiffies: %lld", | |
803 | zvrf->vrf->name, vrf, nl_msg_type_to_str(h->nlmsg_type), | |
804 | sbuf, gbuf, ifp->name, oif_list, m->lastused); | |
90d82769 | 805 | } |
d62a17ae | 806 | return 0; |
565fdc75 DS |
807 | } |
808 | ||
2414abd3 | 809 | int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
565fdc75 | 810 | { |
d62a17ae | 811 | int len; |
d62a17ae | 812 | struct rtmsg *rtm; |
813 | ||
814 | rtm = NLMSG_DATA(h); | |
815 | ||
816 | if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) { | |
817 | /* If this is not route add/delete message print warning. */ | |
78dd30b2 | 818 | zlog_warn("Kernel message: %d NS %u\n", h->nlmsg_type, ns_id); |
d62a17ae | 819 | return 0; |
820 | } | |
821 | ||
de1cd6b9 | 822 | if (!(rtm->rtm_family == AF_INET || rtm->rtm_family == AF_INET6)) { |
8a1b681c | 823 | zlog_warn( |
81227874 | 824 | "Invalid address family: %u received from kernel route change: %u", |
8a1b681c SW |
825 | rtm->rtm_family, h->nlmsg_type); |
826 | return 0; | |
827 | } | |
828 | ||
d62a17ae | 829 | /* Connected route. */ |
830 | if (IS_ZEBRA_DEBUG_KERNEL) | |
78dd30b2 | 831 | zlog_debug("%s %s %s proto %s NS %u", |
d62a17ae | 832 | nl_msg_type_to_str(h->nlmsg_type), |
833 | nl_family_to_str(rtm->rtm_family), | |
834 | nl_rttype_to_str(rtm->rtm_type), | |
78dd30b2 | 835 | nl_rtproto_to_str(rtm->rtm_protocol), ns_id); |
d62a17ae | 836 | |
d62a17ae | 837 | |
838 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
9bdf8618 DS |
839 | if (len < 0) { |
840 | zlog_err("%s: Message received from netlink is of a broken size: %d %zu", | |
841 | __PRETTY_FUNCTION__, | |
842 | h->nlmsg_len, | |
843 | (size_t)NLMSG_LENGTH(sizeof(struct rtmsg))); | |
d62a17ae | 844 | return -1; |
9bdf8618 | 845 | } |
d62a17ae | 846 | |
e655a03c | 847 | if (rtm->rtm_type == RTN_MULTICAST) |
2414abd3 | 848 | netlink_route_change_read_multicast(h, ns_id, startup); |
e655a03c | 849 | else |
2414abd3 | 850 | netlink_route_change_read_unicast(h, ns_id, startup); |
d62a17ae | 851 | return 0; |
565fdc75 DS |
852 | } |
853 | ||
289602d7 | 854 | /* Request for specific route information from the kernel */ |
d62a17ae | 855 | static int netlink_request_route(struct zebra_ns *zns, int family, int type) |
289602d7 | 856 | { |
d62a17ae | 857 | struct { |
858 | struct nlmsghdr n; | |
859 | struct rtmsg rtm; | |
860 | } req; | |
861 | ||
862 | /* Form the request, specifying filter (rtattr) if needed. */ | |
863 | memset(&req, 0, sizeof(req)); | |
864 | req.n.nlmsg_type = type; | |
865 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
866 | req.rtm.rtm_family = family; | |
867 | ||
868 | return netlink_request(&zns->netlink_cmd, &req.n); | |
289602d7 | 869 | } |
870 | ||
718e3744 | 871 | /* Routing table read function using netlink interface. Only called |
872 | bootstrap time. */ | |
d62a17ae | 873 | int netlink_route_read(struct zebra_ns *zns) |
718e3744 | 874 | { |
d62a17ae | 875 | int ret; |
876 | ||
877 | /* Get IPv4 routing table. */ | |
878 | ret = netlink_request_route(zns, AF_INET, RTM_GETROUTE); | |
879 | if (ret < 0) | |
880 | return ret; | |
881 | ret = netlink_parse_info(netlink_route_change_read_unicast, | |
882 | &zns->netlink_cmd, zns, 0, 1); | |
883 | if (ret < 0) | |
884 | return ret; | |
885 | ||
886 | /* Get IPv6 routing table. */ | |
887 | ret = netlink_request_route(zns, AF_INET6, RTM_GETROUTE); | |
888 | if (ret < 0) | |
889 | return ret; | |
890 | ret = netlink_parse_info(netlink_route_change_read_unicast, | |
891 | &zns->netlink_cmd, zns, 0, 1); | |
892 | if (ret < 0) | |
893 | return ret; | |
894 | ||
895 | return 0; | |
718e3744 | 896 | } |
897 | ||
d7c0a89a QY |
898 | static void _netlink_route_nl_add_gateway_info(uint8_t route_family, |
899 | uint8_t gw_family, | |
d62a17ae | 900 | struct nlmsghdr *nlmsg, |
901 | size_t req_size, int bytelen, | |
902 | struct nexthop *nexthop) | |
40c7bdb0 | 903 | { |
d62a17ae | 904 | if (route_family == AF_MPLS) { |
905 | struct gw_family_t gw_fam; | |
906 | ||
907 | gw_fam.family = gw_family; | |
908 | if (gw_family == AF_INET) | |
909 | memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen); | |
910 | else | |
911 | memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen); | |
912 | addattr_l(nlmsg, req_size, RTA_VIA, &gw_fam.family, | |
913 | bytelen + 2); | |
914 | } else { | |
915 | if (gw_family == AF_INET) | |
916 | addattr_l(nlmsg, req_size, RTA_GATEWAY, | |
917 | &nexthop->gate.ipv4, bytelen); | |
918 | else | |
919 | addattr_l(nlmsg, req_size, RTA_GATEWAY, | |
920 | &nexthop->gate.ipv6, bytelen); | |
921 | } | |
40c7bdb0 | 922 | } |
923 | ||
d7c0a89a QY |
924 | static void _netlink_route_rta_add_gateway_info(uint8_t route_family, |
925 | uint8_t gw_family, | |
d62a17ae | 926 | struct rtattr *rta, |
927 | struct rtnexthop *rtnh, | |
928 | size_t req_size, int bytelen, | |
929 | struct nexthop *nexthop) | |
40c7bdb0 | 930 | { |
d62a17ae | 931 | if (route_family == AF_MPLS) { |
932 | struct gw_family_t gw_fam; | |
933 | ||
934 | gw_fam.family = gw_family; | |
935 | if (gw_family == AF_INET) | |
936 | memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen); | |
937 | else | |
938 | memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen); | |
939 | rta_addattr_l(rta, req_size, RTA_VIA, &gw_fam.family, | |
940 | bytelen + 2); | |
941 | rtnh->rtnh_len += RTA_LENGTH(bytelen + 2); | |
942 | } else { | |
943 | if (gw_family == AF_INET) | |
944 | rta_addattr_l(rta, req_size, RTA_GATEWAY, | |
945 | &nexthop->gate.ipv4, bytelen); | |
946 | else | |
947 | rta_addattr_l(rta, req_size, RTA_GATEWAY, | |
948 | &nexthop->gate.ipv6, bytelen); | |
949 | rtnh->rtnh_len += sizeof(struct rtattr) + bytelen; | |
950 | } | |
40c7bdb0 | 951 | } |
952 | ||
fa713d9e CF |
953 | /* This function takes a nexthop as argument and adds |
954 | * the appropriate netlink attributes to an existing | |
955 | * netlink message. | |
956 | * | |
957 | * @param routedesc: Human readable description of route type | |
958 | * (direct/recursive, single-/multipath) | |
959 | * @param bytelen: Length of addresses in bytes. | |
960 | * @param nexthop: Nexthop information | |
961 | * @param nlmsg: nlmsghdr structure to fill in. | |
962 | * @param req_size: The size allocated for the message. | |
963 | */ | |
d62a17ae | 964 | static void _netlink_route_build_singlepath(const char *routedesc, int bytelen, |
965 | struct nexthop *nexthop, | |
966 | struct nlmsghdr *nlmsg, | |
967 | struct rtmsg *rtmsg, | |
968 | size_t req_size, int cmd) | |
fa713d9e | 969 | { |
8ecdb26e | 970 | struct mpls_label_stack *nh_label; |
d62a17ae | 971 | mpls_lse_t out_lse[MPLS_MAX_LABELS]; |
fa712963 | 972 | int num_labels = 0; |
9a62e84b | 973 | char label_buf[256]; |
d62a17ae | 974 | |
975 | /* | |
976 | * label_buf is *only* currently used within debugging. | |
977 | * As such when we assign it we are guarding it inside | |
978 | * a debug test. If you want to change this make sure | |
979 | * you fix this assumption | |
980 | */ | |
981 | label_buf[0] = '\0'; | |
d62a17ae | 982 | |
fa712963 RW |
983 | assert(nexthop); |
984 | for (struct nexthop *nh = nexthop; nh; nh = nh->rparent) { | |
d62a17ae | 985 | char label_buf1[20]; |
986 | ||
fa712963 RW |
987 | nh_label = nh->nh_label; |
988 | if (!nh_label || !nh_label->num_labels) | |
989 | continue; | |
990 | ||
991 | for (int i = 0; i < nh_label->num_labels; i++) { | |
992 | if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL) | |
993 | continue; | |
994 | ||
995 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
996 | if (!num_labels) | |
997 | sprintf(label_buf, "label %u", | |
998 | nh_label->label[i]); | |
999 | else { | |
1000 | sprintf(label_buf1, "/%u", | |
1001 | nh_label->label[i]); | |
1002 | strlcat(label_buf, label_buf1, | |
1003 | sizeof(label_buf)); | |
d62a17ae | 1004 | } |
d62a17ae | 1005 | } |
fa712963 RW |
1006 | |
1007 | out_lse[num_labels] = | |
1008 | mpls_lse_encode(nh_label->label[i], 0, 0, 0); | |
1009 | num_labels++; | |
d62a17ae | 1010 | } |
fa712963 RW |
1011 | } |
1012 | ||
1013 | if (num_labels) { | |
1014 | /* Set the BoS bit */ | |
1015 | out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT); | |
1016 | ||
1017 | if (rtmsg->rtm_family == AF_MPLS) | |
1018 | addattr_l(nlmsg, req_size, RTA_NEWDST, &out_lse, | |
1019 | num_labels * sizeof(mpls_lse_t)); | |
1020 | else { | |
1021 | struct rtattr *nest; | |
d7c0a89a | 1022 | uint16_t encap = LWTUNNEL_ENCAP_MPLS; |
fa712963 RW |
1023 | |
1024 | addattr_l(nlmsg, req_size, RTA_ENCAP_TYPE, &encap, | |
d7c0a89a | 1025 | sizeof(uint16_t)); |
fa712963 RW |
1026 | nest = addattr_nest(nlmsg, req_size, RTA_ENCAP); |
1027 | addattr_l(nlmsg, req_size, MPLS_IPTUNNEL_DST, &out_lse, | |
1028 | num_labels * sizeof(mpls_lse_t)); | |
1029 | addattr_nest_end(nlmsg, nest); | |
66d42727 | 1030 | } |
0aabccc0 | 1031 | } |
fa713d9e | 1032 | |
d62a17ae | 1033 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) |
1034 | rtmsg->rtm_flags |= RTNH_F_ONLINK; | |
1035 | ||
1036 | if (rtmsg->rtm_family == AF_INET | |
1037 | && (nexthop->type == NEXTHOP_TYPE_IPV6 | |
1038 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) { | |
1039 | rtmsg->rtm_flags |= RTNH_F_ONLINK; | |
1040 | addattr_l(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4); | |
1041 | addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex); | |
1042 | ||
1043 | if (nexthop->rmap_src.ipv4.s_addr && (cmd == RTM_NEWROUTE)) | |
1044 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1045 | &nexthop->rmap_src.ipv4, bytelen); | |
1046 | else if (nexthop->src.ipv4.s_addr && (cmd == RTM_NEWROUTE)) | |
1047 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1048 | &nexthop->src.ipv4, bytelen); | |
1049 | ||
1050 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1051 | zlog_debug( | |
1052 | " 5549: _netlink_route_build_singlepath() (%s): " | |
7556c3fd | 1053 | "nexthop via %s %s if %u(%u)", |
d62a17ae | 1054 | routedesc, ipv4_ll_buf, label_buf, |
7556c3fd | 1055 | nexthop->ifindex, nexthop->vrf_id); |
d62a17ae | 1056 | return; |
0aabccc0 DD |
1057 | } |
1058 | ||
d62a17ae | 1059 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
1060 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) { | |
1061 | /* Send deletes to the kernel without specifying the next-hop */ | |
1062 | if (cmd != RTM_DELROUTE) | |
1063 | _netlink_route_nl_add_gateway_info( | |
1064 | rtmsg->rtm_family, AF_INET, nlmsg, req_size, | |
1065 | bytelen, nexthop); | |
1066 | ||
1067 | if (cmd == RTM_NEWROUTE) { | |
1068 | if (nexthop->rmap_src.ipv4.s_addr) | |
1069 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1070 | &nexthop->rmap_src.ipv4, bytelen); | |
1071 | else if (nexthop->src.ipv4.s_addr) | |
1072 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1073 | &nexthop->src.ipv4, bytelen); | |
1074 | } | |
1075 | ||
1076 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1077 | zlog_debug( | |
1078 | "netlink_route_multipath() (%s): " | |
7556c3fd | 1079 | "nexthop via %s %s if %u(%u)", |
d62a17ae | 1080 | routedesc, inet_ntoa(nexthop->gate.ipv4), |
7556c3fd | 1081 | label_buf, nexthop->ifindex, nexthop->vrf_id); |
0aabccc0 | 1082 | } |
fa713d9e | 1083 | |
d62a17ae | 1084 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
1085 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { | |
1086 | _netlink_route_nl_add_gateway_info(rtmsg->rtm_family, AF_INET6, | |
1087 | nlmsg, req_size, bytelen, | |
1088 | nexthop); | |
1089 | ||
1090 | if (cmd == RTM_NEWROUTE) { | |
1091 | if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) | |
1092 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1093 | &nexthop->rmap_src.ipv6, bytelen); | |
1094 | else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) | |
1095 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1096 | &nexthop->src.ipv6, bytelen); | |
1097 | } | |
fa713d9e | 1098 | |
d62a17ae | 1099 | if (IS_ZEBRA_DEBUG_KERNEL) |
1100 | zlog_debug( | |
1101 | "netlink_route_multipath() (%s): " | |
7556c3fd | 1102 | "nexthop via %s %s if %u(%u)", |
d62a17ae | 1103 | routedesc, inet6_ntoa(nexthop->gate.ipv6), |
7556c3fd | 1104 | label_buf, nexthop->ifindex, nexthop->vrf_id); |
d62a17ae | 1105 | } |
5e210522 DS |
1106 | |
1107 | /* | |
1108 | * We have the ifindex so we should always send it | |
1109 | * This is especially useful if we are doing route | |
1110 | * leaking. | |
1111 | */ | |
1112 | if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) | |
d62a17ae | 1113 | addattr32(nlmsg, req_size, RTA_OIF, nexthop->ifindex); |
1114 | ||
5e210522 DS |
1115 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
1116 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) { | |
d62a17ae | 1117 | if (cmd == RTM_NEWROUTE) { |
1118 | if (nexthop->rmap_src.ipv4.s_addr) | |
1119 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1120 | &nexthop->rmap_src.ipv4, bytelen); | |
1121 | else if (nexthop->src.ipv4.s_addr) | |
1122 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1123 | &nexthop->src.ipv4, bytelen); | |
1124 | } | |
fa713d9e | 1125 | |
d62a17ae | 1126 | if (IS_ZEBRA_DEBUG_KERNEL) |
1127 | zlog_debug( | |
1128 | "netlink_route_multipath() (%s): " | |
7556c3fd DS |
1129 | "nexthop via if %u(%u)", |
1130 | routedesc, nexthop->ifindex, nexthop->vrf_id); | |
0aabccc0 DD |
1131 | } |
1132 | ||
d62a17ae | 1133 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { |
d62a17ae | 1134 | if (cmd == RTM_NEWROUTE) { |
1135 | if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) | |
1136 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1137 | &nexthop->rmap_src.ipv6, bytelen); | |
1138 | else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) | |
1139 | addattr_l(nlmsg, req_size, RTA_PREFSRC, | |
1140 | &nexthop->src.ipv6, bytelen); | |
1141 | } | |
1142 | ||
1143 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1144 | zlog_debug( | |
1145 | "netlink_route_multipath() (%s): " | |
7556c3fd DS |
1146 | "nexthop via if %u(%u)", |
1147 | routedesc, nexthop->ifindex, nexthop->vrf_id); | |
d62a17ae | 1148 | } |
fa713d9e CF |
1149 | } |
1150 | ||
1151 | /* This function takes a nexthop as argument and | |
1152 | * appends to the given rtattr/rtnexthop pair the | |
1153 | * representation of the nexthop. If the nexthop | |
1154 | * defines a preferred source, the src parameter | |
1155 | * will be modified to point to that src, otherwise | |
1156 | * it will be kept unmodified. | |
1157 | * | |
1158 | * @param routedesc: Human readable description of route type | |
1159 | * (direct/recursive, single-/multipath) | |
1160 | * @param bytelen: Length of addresses in bytes. | |
1161 | * @param nexthop: Nexthop information | |
1162 | * @param rta: rtnetlink attribute structure | |
1163 | * @param rtnh: pointer to an rtnetlink nexthop structure | |
1164 | * @param src: pointer pointing to a location where | |
1165 | * the prefsrc should be stored. | |
1166 | */ | |
d62a17ae | 1167 | static void _netlink_route_build_multipath(const char *routedesc, int bytelen, |
1168 | struct nexthop *nexthop, | |
1169 | struct rtattr *rta, | |
1170 | struct rtnexthop *rtnh, | |
1171 | struct rtmsg *rtmsg, | |
1172 | union g_addr **src) | |
fa713d9e | 1173 | { |
8ecdb26e | 1174 | struct mpls_label_stack *nh_label; |
d62a17ae | 1175 | mpls_lse_t out_lse[MPLS_MAX_LABELS]; |
fa712963 | 1176 | int num_labels = 0; |
9a62e84b | 1177 | char label_buf[256]; |
d62a17ae | 1178 | |
1179 | rtnh->rtnh_len = sizeof(*rtnh); | |
1180 | rtnh->rtnh_flags = 0; | |
1181 | rtnh->rtnh_hops = 0; | |
1182 | rta->rta_len += rtnh->rtnh_len; | |
1183 | ||
1184 | /* | |
1185 | * label_buf is *only* currently used within debugging. | |
1186 | * As such when we assign it we are guarding it inside | |
1187 | * a debug test. If you want to change this make sure | |
1188 | * you fix this assumption | |
1189 | */ | |
1190 | label_buf[0] = '\0'; | |
d62a17ae | 1191 | |
fa712963 RW |
1192 | assert(nexthop); |
1193 | for (struct nexthop *nh = nexthop; nh; nh = nh->rparent) { | |
d62a17ae | 1194 | char label_buf1[20]; |
1195 | ||
fa712963 RW |
1196 | nh_label = nh->nh_label; |
1197 | if (!nh_label || !nh_label->num_labels) | |
1198 | continue; | |
1199 | ||
1200 | for (int i = 0; i < nh_label->num_labels; i++) { | |
1201 | if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL) | |
1202 | continue; | |
1203 | ||
1204 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
1205 | if (!num_labels) | |
1206 | sprintf(label_buf, "label %u", | |
1207 | nh_label->label[i]); | |
1208 | else { | |
1209 | sprintf(label_buf1, "/%u", | |
1210 | nh_label->label[i]); | |
1211 | strlcat(label_buf, label_buf1, | |
1212 | sizeof(label_buf)); | |
d62a17ae | 1213 | } |
d62a17ae | 1214 | } |
fa712963 RW |
1215 | |
1216 | out_lse[num_labels] = | |
1217 | mpls_lse_encode(nh_label->label[i], 0, 0, 0); | |
1218 | num_labels++; | |
d62a17ae | 1219 | } |
fa712963 RW |
1220 | } |
1221 | ||
1222 | if (num_labels) { | |
1223 | /* Set the BoS bit */ | |
1224 | out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT); | |
1225 | ||
1226 | if (rtmsg->rtm_family == AF_MPLS) { | |
1227 | rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_NEWDST, | |
1228 | &out_lse, | |
1229 | num_labels * sizeof(mpls_lse_t)); | |
1230 | rtnh->rtnh_len += | |
1231 | RTA_LENGTH(num_labels * sizeof(mpls_lse_t)); | |
1232 | } else { | |
1233 | struct rtattr *nest; | |
d7c0a89a | 1234 | uint16_t encap = LWTUNNEL_ENCAP_MPLS; |
fa712963 RW |
1235 | int len = rta->rta_len; |
1236 | ||
1237 | rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_ENCAP_TYPE, | |
d7c0a89a | 1238 | &encap, sizeof(uint16_t)); |
fa712963 RW |
1239 | nest = rta_nest(rta, NL_PKT_BUF_SIZE, RTA_ENCAP); |
1240 | rta_addattr_l(rta, NL_PKT_BUF_SIZE, MPLS_IPTUNNEL_DST, | |
1241 | &out_lse, | |
1242 | num_labels * sizeof(mpls_lse_t)); | |
1243 | rta_nest_end(rta, nest); | |
1244 | rtnh->rtnh_len += rta->rta_len - len; | |
66d42727 | 1245 | } |
d62a17ae | 1246 | } |
1247 | ||
1248 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) | |
1249 | rtnh->rtnh_flags |= RTNH_F_ONLINK; | |
1250 | ||
1251 | if (rtmsg->rtm_family == AF_INET | |
1252 | && (nexthop->type == NEXTHOP_TYPE_IPV6 | |
1253 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)) { | |
1254 | bytelen = 4; | |
1255 | rtnh->rtnh_flags |= RTNH_F_ONLINK; | |
1256 | rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, &ipv4_ll, | |
1257 | bytelen); | |
1258 | rtnh->rtnh_len += sizeof(struct rtattr) + bytelen; | |
1259 | rtnh->rtnh_ifindex = nexthop->ifindex; | |
1260 | ||
1261 | if (nexthop->rmap_src.ipv4.s_addr) | |
1262 | *src = &nexthop->rmap_src; | |
1263 | else if (nexthop->src.ipv4.s_addr) | |
1264 | *src = &nexthop->src; | |
1265 | ||
1266 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1267 | zlog_debug( | |
1268 | " 5549: netlink_route_build_multipath() (%s): " | |
1269 | "nexthop via %s %s if %u", | |
1270 | routedesc, ipv4_ll_buf, label_buf, | |
1271 | nexthop->ifindex); | |
1272 | return; | |
1273 | } | |
1274 | ||
1275 | if (nexthop->type == NEXTHOP_TYPE_IPV4 | |
1276 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) { | |
1277 | _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET, | |
1278 | rta, rtnh, NL_PKT_BUF_SIZE, | |
1279 | bytelen, nexthop); | |
1280 | if (nexthop->rmap_src.ipv4.s_addr) | |
1281 | *src = &nexthop->rmap_src; | |
1282 | else if (nexthop->src.ipv4.s_addr) | |
1283 | *src = &nexthop->src; | |
1284 | ||
1285 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1286 | zlog_debug( | |
1287 | "netlink_route_multipath() (%s): " | |
1288 | "nexthop via %s %s if %u", | |
1289 | routedesc, inet_ntoa(nexthop->gate.ipv4), | |
1290 | label_buf, nexthop->ifindex); | |
1291 | } | |
1292 | if (nexthop->type == NEXTHOP_TYPE_IPV6 | |
1293 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { | |
1294 | _netlink_route_rta_add_gateway_info(rtmsg->rtm_family, AF_INET6, | |
1295 | rta, rtnh, NL_PKT_BUF_SIZE, | |
1296 | bytelen, nexthop); | |
1297 | ||
1298 | if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) | |
1299 | *src = &nexthop->rmap_src; | |
1300 | else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) | |
1301 | *src = &nexthop->src; | |
1302 | ||
1303 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1304 | zlog_debug( | |
1305 | "netlink_route_multipath() (%s): " | |
1306 | "nexthop via %s %s if %u", | |
1307 | routedesc, inet6_ntoa(nexthop->gate.ipv6), | |
1308 | label_buf, nexthop->ifindex); | |
1309 | } | |
5e210522 DS |
1310 | |
1311 | /* | |
1312 | * We have figured out the ifindex so we should always send it | |
1313 | * This is especially useful if we are doing route | |
1314 | * leaking. | |
1315 | */ | |
1316 | if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) | |
1317 | rtnh->rtnh_ifindex = nexthop->ifindex; | |
1318 | ||
d62a17ae | 1319 | /* ifindex */ |
1320 | if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX | |
1321 | || nexthop->type == NEXTHOP_TYPE_IFINDEX) { | |
d62a17ae | 1322 | if (nexthop->rmap_src.ipv4.s_addr) |
1323 | *src = &nexthop->rmap_src; | |
1324 | else if (nexthop->src.ipv4.s_addr) | |
1325 | *src = &nexthop->src; | |
1326 | ||
1327 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1328 | zlog_debug( | |
1329 | "netlink_route_multipath() (%s): " | |
1330 | "nexthop via if %u", | |
1331 | routedesc, nexthop->ifindex); | |
d62a17ae | 1332 | } |
fa713d9e CF |
1333 | } |
1334 | ||
d62a17ae | 1335 | static inline void _netlink_mpls_build_singlepath(const char *routedesc, |
1336 | zebra_nhlfe_t *nhlfe, | |
1337 | struct nlmsghdr *nlmsg, | |
1338 | struct rtmsg *rtmsg, | |
1339 | size_t req_size, int cmd) | |
40c7bdb0 | 1340 | { |
d62a17ae | 1341 | int bytelen; |
d7c0a89a | 1342 | uint8_t family; |
40c7bdb0 | 1343 | |
d62a17ae | 1344 | family = NHLFE_FAMILY(nhlfe); |
1345 | bytelen = (family == AF_INET ? 4 : 16); | |
1346 | _netlink_route_build_singlepath(routedesc, bytelen, nhlfe->nexthop, | |
1347 | nlmsg, rtmsg, req_size, cmd); | |
40c7bdb0 | 1348 | } |
1349 | ||
1350 | ||
1351 | static inline void | |
d62a17ae | 1352 | _netlink_mpls_build_multipath(const char *routedesc, zebra_nhlfe_t *nhlfe, |
1353 | struct rtattr *rta, struct rtnexthop *rtnh, | |
1354 | struct rtmsg *rtmsg, union g_addr **src) | |
40c7bdb0 | 1355 | { |
d62a17ae | 1356 | int bytelen; |
d7c0a89a | 1357 | uint8_t family; |
40c7bdb0 | 1358 | |
d62a17ae | 1359 | family = NHLFE_FAMILY(nhlfe); |
1360 | bytelen = (family == AF_INET ? 4 : 16); | |
1361 | _netlink_route_build_multipath(routedesc, bytelen, nhlfe->nexthop, rta, | |
1362 | rtnh, rtmsg, src); | |
40c7bdb0 | 1363 | } |
1364 | ||
1365 | ||
fa713d9e CF |
1366 | /* Log debug information for netlink_route_multipath |
1367 | * if debug logging is enabled. | |
1368 | * | |
1369 | * @param cmd: Netlink command which is to be processed | |
1370 | * @param p: Prefix for which the change is due | |
fa713d9e | 1371 | * @param family: Address family which the change concerns |
45df4e96 DS |
1372 | * @param zvrf: The vrf we are in |
1373 | * @param tableid: The table we are working on | |
fa713d9e | 1374 | */ |
86391e56 MS |
1375 | static void _netlink_route_debug(int cmd, const struct prefix *p, |
1376 | int family, vrf_id_t vrfid, | |
7556c3fd | 1377 | uint32_t tableid) |
fa713d9e | 1378 | { |
d62a17ae | 1379 | if (IS_ZEBRA_DEBUG_KERNEL) { |
1380 | char buf[PREFIX_STRLEN]; | |
1381 | zlog_debug( | |
45df4e96 DS |
1382 | "netlink_route_multipath(): %s %s vrf %u(%u)", |
1383 | nl_msg_type_to_str(cmd), | |
1384 | prefix2str(p, buf, sizeof(buf)), | |
86391e56 | 1385 | vrfid, tableid); |
d62a17ae | 1386 | } |
1387 | } | |
1388 | ||
d7c0a89a | 1389 | static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc) |
40c7bdb0 | 1390 | { |
d62a17ae | 1391 | if (IS_ZEBRA_DEBUG_KERNEL) |
1392 | zlog_debug("netlink_mpls_multipath() (%s): %s %u/20", routedesc, | |
1393 | nl_msg_type_to_str(cmd), label); | |
fa713d9e CF |
1394 | } |
1395 | ||
d62a17ae | 1396 | static int netlink_neigh_update(int cmd, int ifindex, uint32_t addr, char *lla, |
5895d33f | 1397 | int llalen, ns_id_t ns_id) |
5c610faf | 1398 | { |
d62a17ae | 1399 | struct { |
1400 | struct nlmsghdr n; | |
1401 | struct ndmsg ndm; | |
1402 | char buf[256]; | |
1403 | } req; | |
5c610faf | 1404 | |
5895d33f | 1405 | struct zebra_ns *zns = zebra_ns_lookup(ns_id); |
8f7d9fc0 | 1406 | |
5605ecfc | 1407 | memset(&req, 0, sizeof(req)); |
5c610faf | 1408 | |
d62a17ae | 1409 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); |
1410 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
1411 | req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH | |
1412 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
a55ba23f | 1413 | |
d62a17ae | 1414 | req.ndm.ndm_family = AF_INET; |
1415 | req.ndm.ndm_state = NUD_PERMANENT; | |
1416 | req.ndm.ndm_ifindex = ifindex; | |
1417 | req.ndm.ndm_type = RTN_UNICAST; | |
5c610faf | 1418 | |
d62a17ae | 1419 | addattr_l(&req.n, sizeof(req), NDA_DST, &addr, 4); |
1420 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, lla, llalen); | |
5c610faf | 1421 | |
d62a17ae | 1422 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, |
1423 | 0); | |
5c610faf DS |
1424 | } |
1425 | ||
718e3744 | 1426 | /* Routing table change via netlink interface. */ |
6ae24471 | 1427 | /* Update flag indicates whether this is a "replace" or not. */ |
86391e56 MS |
1428 | static int netlink_route_multipath(int cmd, const struct prefix *p, |
1429 | const struct prefix *src_p, | |
1430 | struct route_entry *re, | |
d62a17ae | 1431 | int update) |
718e3744 | 1432 | { |
d62a17ae | 1433 | int bytelen; |
1434 | struct sockaddr_nl snl; | |
1435 | struct nexthop *nexthop = NULL; | |
1436 | unsigned int nexthop_num; | |
d62a17ae | 1437 | int family = PREFIX_FAMILY(p); |
1438 | const char *routedesc; | |
1439 | int setsrc = 0; | |
1440 | union g_addr src; | |
1441 | ||
1442 | struct { | |
1443 | struct nlmsghdr n; | |
1444 | struct rtmsg r; | |
1445 | char buf[NL_PKT_BUF_SIZE]; | |
1446 | } req; | |
1447 | ||
9a76375f | 1448 | struct zebra_ns *zns; |
d62a17ae | 1449 | struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id); |
1450 | ||
009f8ad5 | 1451 | zns = zvrf->zns; |
d62a17ae | 1452 | memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE); |
1453 | ||
1454 | bytelen = (family == AF_INET ? 4 : 16); | |
1455 | ||
1456 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
1457 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
1458 | if ((cmd == RTM_NEWROUTE) && update) | |
1459 | req.n.nlmsg_flags |= NLM_F_REPLACE; | |
1460 | req.n.nlmsg_type = cmd; | |
1461 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
1462 | ||
1463 | req.r.rtm_family = family; | |
1464 | req.r.rtm_dst_len = p->prefixlen; | |
1465 | req.r.rtm_src_len = src_p ? src_p->prefixlen : 0; | |
915902cb | 1466 | req.r.rtm_protocol = zebra2proto(re->type); |
d62a17ae | 1467 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
c766824c DS |
1468 | |
1469 | /* | |
1470 | * blackhole routes are not RTN_UNICAST, they are | |
1471 | * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT | |
1472 | * so setting this value as a RTN_UNICAST would | |
1473 | * cause the route lookup of just the prefix | |
1474 | * to fail. So no need to specify this for | |
1475 | * the RTM_DELROUTE case | |
1476 | */ | |
1477 | if (cmd != RTM_DELROUTE) | |
1478 | req.r.rtm_type = RTN_UNICAST; | |
d62a17ae | 1479 | |
d62a17ae | 1480 | addattr_l(&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen); |
1481 | if (src_p) | |
1482 | addattr_l(&req.n, sizeof req, RTA_SRC, &src_p->u.prefix, | |
1483 | bytelen); | |
1484 | ||
1485 | /* Metric. */ | |
1486 | /* Hardcode the metric for all routes coming from zebra. Metric isn't | |
1487 | * used | |
1488 | * either by the kernel or by zebra. Its purely for calculating best | |
1489 | * path(s) | |
1490 | * by the routing protocol and for communicating with protocol peers. | |
1491 | */ | |
1492 | addattr32(&req.n, sizeof req, RTA_PRIORITY, NL_DEFAULT_ROUTE_METRIC); | |
4e40b6d6 KK |
1493 | #if defined(SUPPORT_REALMS) |
1494 | if (re->tag > 0 && re->tag <= 255) | |
1495 | addattr32(&req.n, sizeof req, RTA_FLOW, re->tag); | |
1496 | #endif | |
d62a17ae | 1497 | /* Table corresponding to this route. */ |
1498 | if (re->table < 256) | |
1499 | req.r.rtm_table = re->table; | |
1500 | else { | |
1501 | req.r.rtm_table = RT_TABLE_UNSPEC; | |
1502 | addattr32(&req.n, sizeof req, RTA_TABLE, re->table); | |
0aabccc0 | 1503 | } |
718e3744 | 1504 | |
86391e56 | 1505 | _netlink_route_debug(cmd, p, family, zvrf_id(zvrf), re->table); |
45df4e96 | 1506 | |
c1d63a93 DS |
1507 | /* |
1508 | * If we are not updating the route and we have received | |
1509 | * a route delete, then all we need to fill in is the | |
1510 | * prefix information to tell the kernel to schwack | |
1511 | * it. | |
1512 | */ | |
1513 | if (!update && cmd == RTM_DELROUTE) | |
a8309422 DL |
1514 | goto skip; |
1515 | ||
d62a17ae | 1516 | if (re->mtu || re->nexthop_mtu) { |
1517 | char buf[NL_PKT_BUF_SIZE]; | |
1518 | struct rtattr *rta = (void *)buf; | |
d7c0a89a | 1519 | uint32_t mtu = re->mtu; |
d62a17ae | 1520 | if (!mtu || (re->nexthop_mtu && re->nexthop_mtu < mtu)) |
1521 | mtu = re->nexthop_mtu; | |
1522 | rta->rta_type = RTA_METRICS; | |
1523 | rta->rta_len = RTA_LENGTH(0); | |
1524 | rta_addattr_l(rta, NL_PKT_BUF_SIZE, RTAX_MTU, &mtu, sizeof mtu); | |
1525 | addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA(rta), | |
1526 | RTA_PAYLOAD(rta)); | |
1527 | } | |
1528 | ||
d62a17ae | 1529 | /* Count overall nexthops so we can decide whether to use singlepath |
1530 | * or multipath case. */ | |
1531 | nexthop_num = 0; | |
7ee30f28 | 1532 | for (ALL_NEXTHOPS(re->ng, nexthop)) { |
d62a17ae | 1533 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
1534 | continue; | |
996c9314 | 1535 | if (cmd == RTM_NEWROUTE && !NEXTHOP_IS_ACTIVE(nexthop->flags)) |
d62a17ae | 1536 | continue; |
1537 | if (cmd == RTM_DELROUTE | |
1538 | && !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
1539 | continue; | |
1540 | ||
1541 | nexthop_num++; | |
1542 | } | |
1543 | ||
1544 | /* Singlepath case. */ | |
1545 | if (nexthop_num == 1 || multipath_num == 1) { | |
1546 | nexthop_num = 0; | |
7ee30f28 | 1547 | for (ALL_NEXTHOPS(re->ng, nexthop)) { |
1f1d24a8 DS |
1548 | /* |
1549 | * So we want to cover 2 types of blackhole | |
1550 | * routes here: | |
1551 | * 1) A normal blackhole route( ala from a static | |
1552 | * install. | |
1553 | * 2) A recursively resolved blackhole route | |
1554 | */ | |
1555 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) { | |
1556 | switch (nexthop->bh_type) { | |
1557 | case BLACKHOLE_ADMINPROHIB: | |
1558 | req.r.rtm_type = RTN_PROHIBIT; | |
1559 | break; | |
1560 | case BLACKHOLE_REJECT: | |
1561 | req.r.rtm_type = RTN_UNREACHABLE; | |
1562 | break; | |
1563 | default: | |
1564 | req.r.rtm_type = RTN_BLACKHOLE; | |
1565 | break; | |
1566 | } | |
1567 | goto skip; | |
1568 | } | |
d62a17ae | 1569 | if (CHECK_FLAG(nexthop->flags, |
1570 | NEXTHOP_FLAG_RECURSIVE)) { | |
1571 | if (!setsrc) { | |
1572 | if (family == AF_INET) { | |
1573 | if (nexthop->rmap_src.ipv4 | |
1574 | .s_addr | |
1575 | != 0) { | |
1576 | src.ipv4 = | |
1577 | nexthop->rmap_src | |
1578 | .ipv4; | |
1579 | setsrc = 1; | |
1580 | } else if (nexthop->src.ipv4 | |
1581 | .s_addr | |
1582 | != 0) { | |
1583 | src.ipv4 = | |
1584 | nexthop->src | |
1585 | .ipv4; | |
1586 | setsrc = 1; | |
1587 | } | |
1588 | } else if (family == AF_INET6) { | |
1589 | if (!IN6_IS_ADDR_UNSPECIFIED( | |
1590 | &nexthop->rmap_src | |
1591 | .ipv6)) { | |
1592 | src.ipv6 = | |
1593 | nexthop->rmap_src | |
1594 | .ipv6; | |
1595 | setsrc = 1; | |
1596 | } else if ( | |
1597 | !IN6_IS_ADDR_UNSPECIFIED( | |
1598 | &nexthop->src | |
1599 | .ipv6)) { | |
1600 | src.ipv6 = | |
1601 | nexthop->src | |
1602 | .ipv6; | |
1603 | setsrc = 1; | |
1604 | } | |
1605 | } | |
1606 | } | |
1607 | continue; | |
1608 | } | |
1609 | ||
1610 | if ((cmd == RTM_NEWROUTE | |
25b9cb0c | 1611 | && NEXTHOP_IS_ACTIVE(nexthop->flags)) |
d62a17ae | 1612 | || (cmd == RTM_DELROUTE |
1613 | && CHECK_FLAG(nexthop->flags, | |
1614 | NEXTHOP_FLAG_FIB))) { | |
1615 | routedesc = nexthop->rparent | |
8b1450b9 RW |
1616 | ? "recursive, single-path" |
1617 | : "single-path"; | |
d62a17ae | 1618 | |
d62a17ae | 1619 | _netlink_route_build_singlepath( |
1620 | routedesc, bytelen, nexthop, &req.n, | |
1621 | &req.r, sizeof req, cmd); | |
1622 | nexthop_num++; | |
1623 | break; | |
1624 | } | |
1625 | } | |
1626 | if (setsrc && (cmd == RTM_NEWROUTE)) { | |
1627 | if (family == AF_INET) | |
1628 | addattr_l(&req.n, sizeof req, RTA_PREFSRC, | |
1629 | &src.ipv4, bytelen); | |
1630 | else if (family == AF_INET6) | |
1631 | addattr_l(&req.n, sizeof req, RTA_PREFSRC, | |
1632 | &src.ipv6, bytelen); | |
1633 | } | |
1634 | } else { | |
1635 | char buf[NL_PKT_BUF_SIZE]; | |
1636 | struct rtattr *rta = (void *)buf; | |
1637 | struct rtnexthop *rtnh; | |
1638 | union g_addr *src1 = NULL; | |
1639 | ||
1640 | rta->rta_type = RTA_MULTIPATH; | |
1641 | rta->rta_len = RTA_LENGTH(0); | |
1642 | rtnh = RTA_DATA(rta); | |
1643 | ||
1644 | nexthop_num = 0; | |
7ee30f28 | 1645 | for (ALL_NEXTHOPS(re->ng, nexthop)) { |
d62a17ae | 1646 | if (nexthop_num >= multipath_num) |
1647 | break; | |
1648 | ||
1649 | if (CHECK_FLAG(nexthop->flags, | |
1650 | NEXTHOP_FLAG_RECURSIVE)) { | |
1651 | /* This only works for IPv4 now */ | |
1652 | if (!setsrc) { | |
1653 | if (family == AF_INET) { | |
1654 | if (nexthop->rmap_src.ipv4 | |
1655 | .s_addr | |
1656 | != 0) { | |
1657 | src.ipv4 = | |
1658 | nexthop->rmap_src | |
1659 | .ipv4; | |
1660 | setsrc = 1; | |
1661 | } else if (nexthop->src.ipv4 | |
1662 | .s_addr | |
1663 | != 0) { | |
1664 | src.ipv4 = | |
1665 | nexthop->src | |
1666 | .ipv4; | |
1667 | setsrc = 1; | |
1668 | } | |
1669 | } else if (family == AF_INET6) { | |
1670 | if (!IN6_IS_ADDR_UNSPECIFIED( | |
1671 | &nexthop->rmap_src | |
1672 | .ipv6)) { | |
1673 | src.ipv6 = | |
1674 | nexthop->rmap_src | |
1675 | .ipv6; | |
1676 | setsrc = 1; | |
1677 | } else if ( | |
1678 | !IN6_IS_ADDR_UNSPECIFIED( | |
1679 | &nexthop->src | |
1680 | .ipv6)) { | |
1681 | src.ipv6 = | |
1682 | nexthop->src | |
1683 | .ipv6; | |
1684 | setsrc = 1; | |
1685 | } | |
1686 | } | |
1687 | } | |
1688 | continue; | |
1689 | } | |
1690 | ||
1691 | if ((cmd == RTM_NEWROUTE | |
25b9cb0c | 1692 | && NEXTHOP_IS_ACTIVE(nexthop->flags)) |
d62a17ae | 1693 | || (cmd == RTM_DELROUTE |
1694 | && CHECK_FLAG(nexthop->flags, | |
1695 | NEXTHOP_FLAG_FIB))) { | |
1696 | routedesc = nexthop->rparent | |
8b1450b9 RW |
1697 | ? "recursive, multipath" |
1698 | : "multipath"; | |
d62a17ae | 1699 | nexthop_num++; |
1700 | ||
d62a17ae | 1701 | _netlink_route_build_multipath( |
1702 | routedesc, bytelen, nexthop, rta, rtnh, | |
1703 | &req.r, &src1); | |
1704 | rtnh = RTNH_NEXT(rtnh); | |
1705 | ||
1706 | if (!setsrc && src1) { | |
1707 | if (family == AF_INET) | |
1708 | src.ipv4 = src1->ipv4; | |
1709 | else if (family == AF_INET6) | |
1710 | src.ipv6 = src1->ipv6; | |
1711 | ||
1712 | setsrc = 1; | |
1713 | } | |
1714 | } | |
1715 | } | |
1716 | if (setsrc && (cmd == RTM_NEWROUTE)) { | |
1717 | if (family == AF_INET) | |
1718 | addattr_l(&req.n, sizeof req, RTA_PREFSRC, | |
1719 | &src.ipv4, bytelen); | |
1720 | else if (family == AF_INET6) | |
1721 | addattr_l(&req.n, sizeof req, RTA_PREFSRC, | |
1722 | &src.ipv6, bytelen); | |
1723 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1724 | zlog_debug("Setting source"); | |
1725 | } | |
1726 | ||
1727 | if (rta->rta_len > RTA_LENGTH(0)) | |
1728 | addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH, | |
1729 | RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
1730 | } | |
718e3744 | 1731 | |
d62a17ae | 1732 | /* If there is no useful nexthop then return. */ |
1733 | if (nexthop_num == 0) { | |
1734 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1735 | zlog_debug( | |
1736 | "netlink_route_multipath(): No useful nexthop."); | |
1737 | return 0; | |
1738 | } | |
718e3744 | 1739 | |
7021c425 | 1740 | skip: |
718e3744 | 1741 | |
d62a17ae | 1742 | /* Destination netlink address. */ |
1743 | memset(&snl, 0, sizeof snl); | |
1744 | snl.nl_family = AF_NETLINK; | |
718e3744 | 1745 | |
d62a17ae | 1746 | /* Talk to netlink socket. */ |
1747 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
1748 | 0); | |
718e3744 | 1749 | } |
1750 | ||
43b5cc5e | 1751 | int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in) |
e3be0432 | 1752 | { |
d62a17ae | 1753 | int suc = 0; |
1754 | struct mcast_route_data *mr = (struct mcast_route_data *)in; | |
bd8b9272 DS |
1755 | struct { |
1756 | struct nlmsghdr n; | |
1757 | struct ndmsg ndm; | |
1758 | char buf[256]; | |
1759 | } req; | |
e3be0432 | 1760 | |
d62a17ae | 1761 | mroute = mr; |
5895d33f | 1762 | struct zebra_ns *zns; |
bd8b9272 | 1763 | |
009f8ad5 | 1764 | zns = zvrf->zns; |
5605ecfc | 1765 | memset(&req, 0, sizeof(req)); |
bd8b9272 DS |
1766 | |
1767 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
1768 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
1769 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
1770 | ||
1771 | req.ndm.ndm_family = RTNL_FAMILY_IPMR; | |
1772 | req.n.nlmsg_type = RTM_GETROUTE; | |
1773 | ||
1774 | addattr_l(&req.n, sizeof(req), RTA_IIF, &mroute->ifindex, 4); | |
1775 | addattr_l(&req.n, sizeof(req), RTA_OIF, &mroute->ifindex, 4); | |
1776 | addattr_l(&req.n, sizeof(req), RTA_SRC, &mroute->sg.src.s_addr, 4); | |
1777 | addattr_l(&req.n, sizeof(req), RTA_DST, &mroute->sg.grp.s_addr, 4); | |
1778 | addattr_l(&req.n, sizeof(req), RTA_TABLE, &zvrf->table_id, 4); | |
e3be0432 | 1779 | |
bd8b9272 DS |
1780 | suc = netlink_talk(netlink_route_change_read_multicast, &req.n, |
1781 | &zns->netlink_cmd, zns, 0); | |
e3be0432 | 1782 | |
bd8b9272 | 1783 | mroute = NULL; |
d62a17ae | 1784 | return suc; |
e3be0432 DS |
1785 | } |
1786 | ||
1e885672 | 1787 | enum dp_req_result kernel_route_rib(struct route_node *rn, |
86391e56 MS |
1788 | const struct prefix *p, |
1789 | const struct prefix *src_p, | |
215181cb DS |
1790 | struct route_entry *old, |
1791 | struct route_entry *new) | |
718e3744 | 1792 | { |
0c555cc6 DS |
1793 | int ret = 0; |
1794 | ||
0af35d90 RW |
1795 | assert(old || new); |
1796 | ||
0c555cc6 | 1797 | if (new) { |
6b093863 | 1798 | if (p->family == AF_INET || v6_rr_semantics) |
0c555cc6 DS |
1799 | ret = netlink_route_multipath(RTM_NEWROUTE, p, src_p, |
1800 | new, (old) ? 1 : 0); | |
1801 | else { | |
1802 | /* | |
1803 | * So v6 route replace semantics are not in | |
1804 | * the kernel at this point as I understand it. | |
1805 | * So let's do a delete than an add. | |
1806 | * In the future once v6 route replace semantics | |
1807 | * are in we can figure out what to do here to | |
1808 | * allow working with old and new kernels. | |
1809 | * | |
1810 | * I'm also intentionally ignoring the failure case | |
1811 | * of the route delete. If that happens yeah we're | |
1812 | * screwed. | |
1813 | */ | |
1814 | if (old) | |
996c9314 LB |
1815 | netlink_route_multipath(RTM_DELROUTE, p, src_p, |
1816 | old, 0); | |
1817 | ret = netlink_route_multipath(RTM_NEWROUTE, p, src_p, | |
1818 | new, 0); | |
0c555cc6 | 1819 | } |
7d974ba3 | 1820 | kernel_route_rib_pass_fail(rn, p, new, |
215181cb DS |
1821 | (!ret) ? DP_INSTALL_SUCCESS |
1822 | : DP_INSTALL_FAILURE); | |
1e885672 | 1823 | return DP_REQUEST_SUCCESS; |
0c555cc6 | 1824 | } |
718e3744 | 1825 | |
0c555cc6 DS |
1826 | if (old) { |
1827 | ret = netlink_route_multipath(RTM_DELROUTE, p, src_p, old, 0); | |
e7fcb843 | 1828 | |
7d974ba3 | 1829 | kernel_route_rib_pass_fail(rn, p, old, |
215181cb DS |
1830 | (!ret) ? DP_DELETE_SUCCESS |
1831 | : DP_DELETE_FAILURE); | |
0c555cc6 | 1832 | } |
215181cb | 1833 | |
1e885672 | 1834 | return DP_REQUEST_SUCCESS; |
718e3744 | 1835 | } |
1836 | ||
d62a17ae | 1837 | int kernel_neigh_update(int add, int ifindex, uint32_t addr, char *lla, |
5895d33f | 1838 | int llalen, ns_id_t ns_id) |
6b8a5694 | 1839 | { |
d62a17ae | 1840 | return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex, |
5895d33f | 1841 | addr, lla, llalen, ns_id); |
6b8a5694 | 1842 | } |
718e3744 | 1843 | |
13d60d35 | 1844 | /* |
1845 | * Add remote VTEP to the flood list for this VxLAN interface (VNI). This | |
1846 | * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00. | |
1847 | */ | |
d62a17ae | 1848 | static int netlink_vxlan_flood_list_update(struct interface *ifp, |
1849 | struct in_addr *vtep_ip, int cmd) | |
13d60d35 | 1850 | { |
5895d33f | 1851 | struct zebra_ns *zns; |
d62a17ae | 1852 | struct { |
1853 | struct nlmsghdr n; | |
1854 | struct ndmsg ndm; | |
1855 | char buf[256]; | |
1856 | } req; | |
d7c0a89a | 1857 | uint8_t dst_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
009f8ad5 | 1858 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id); |
d62a17ae | 1859 | |
009f8ad5 | 1860 | zns = zvrf->zns; |
5605ecfc | 1861 | memset(&req, 0, sizeof(req)); |
d62a17ae | 1862 | |
1863 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
1864 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
1865 | if (cmd == RTM_NEWNEIGH) | |
1866 | req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_APPEND); | |
1867 | req.n.nlmsg_type = cmd; | |
1868 | req.ndm.ndm_family = PF_BRIDGE; | |
1869 | req.ndm.ndm_state = NUD_NOARP | NUD_PERMANENT; | |
1870 | req.ndm.ndm_flags |= NTF_SELF; // Handle by "self", not "master" | |
1871 | ||
1872 | ||
1873 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, &dst_mac, 6); | |
1874 | req.ndm.ndm_ifindex = ifp->ifindex; | |
1875 | addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip->s_addr, 4); | |
1876 | ||
1877 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
1878 | 0); | |
13d60d35 | 1879 | } |
1880 | ||
1881 | /* | |
d62a17ae | 1882 | * Add remote VTEP for this VxLAN interface (VNI). In Linux, this involves |
1883 | * adding | |
13d60d35 | 1884 | * a "flood" MAC FDB entry. |
1885 | */ | |
d62a17ae | 1886 | int kernel_add_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip) |
13d60d35 | 1887 | { |
d62a17ae | 1888 | if (IS_ZEBRA_DEBUG_VXLAN) |
1889 | zlog_debug("Install %s into flood list for VNI %u intf %s(%u)", | |
1890 | inet_ntoa(*vtep_ip), vni, ifp->name, ifp->ifindex); | |
13d60d35 | 1891 | |
d62a17ae | 1892 | return netlink_vxlan_flood_list_update(ifp, vtep_ip, RTM_NEWNEIGH); |
13d60d35 | 1893 | } |
1894 | ||
1895 | /* | |
1896 | * Remove remote VTEP for this VxLAN interface (VNI). In Linux, this involves | |
1897 | * deleting the "flood" MAC FDB entry. | |
1898 | */ | |
d62a17ae | 1899 | int kernel_del_vtep(vni_t vni, struct interface *ifp, struct in_addr *vtep_ip) |
13d60d35 | 1900 | { |
d62a17ae | 1901 | if (IS_ZEBRA_DEBUG_VXLAN) |
1902 | zlog_debug( | |
1903 | "Uninstall %s from flood list for VNI %u intf %s(%u)", | |
1904 | inet_ntoa(*vtep_ip), vni, ifp->name, ifp->ifindex); | |
13d60d35 | 1905 | |
d62a17ae | 1906 | return netlink_vxlan_flood_list_update(ifp, vtep_ip, RTM_DELNEIGH); |
13d60d35 | 1907 | } |
1908 | ||
2232a77c | 1909 | #ifndef NDA_RTA |
d62a17ae | 1910 | #define NDA_RTA(r) \ |
1911 | ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) | |
2232a77c | 1912 | #endif |
1913 | ||
2414abd3 | 1914 | static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id) |
2232a77c | 1915 | { |
d62a17ae | 1916 | struct ndmsg *ndm; |
1917 | struct interface *ifp; | |
1918 | struct zebra_if *zif; | |
d62a17ae | 1919 | struct rtattr *tb[NDA_MAX + 1]; |
1920 | struct interface *br_if; | |
1921 | struct ethaddr mac; | |
1922 | vlanid_t vid = 0; | |
1923 | struct prefix vtep_ip; | |
1924 | int vid_present = 0, dst_present = 0; | |
1925 | char buf[ETHER_ADDR_STRLEN]; | |
1926 | char vid_buf[20]; | |
1927 | char dst_buf[30]; | |
d7c0a89a | 1928 | uint8_t sticky = 0; |
d62a17ae | 1929 | |
1930 | ndm = NLMSG_DATA(h); | |
1931 | ||
2853fed6 | 1932 | /* We only process macfdb notifications if EVPN is enabled */ |
1933 | if (!is_evpn_enabled()) | |
1934 | return 0; | |
1935 | ||
d62a17ae | 1936 | /* The interface should exist. */ |
5895d33f | 1937 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), |
d62a17ae | 1938 | ndm->ndm_ifindex); |
2853fed6 | 1939 | if (!ifp || !ifp->info) |
d62a17ae | 1940 | return 0; |
1941 | ||
1942 | /* The interface should be something we're interested in. */ | |
1943 | if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) | |
1944 | return 0; | |
1945 | ||
1946 | /* Drop "permanent" entries. */ | |
1947 | if (ndm->ndm_state & NUD_PERMANENT) | |
1948 | return 0; | |
1949 | ||
1950 | zif = (struct zebra_if *)ifp->info; | |
1951 | if ((br_if = zif->brslave_info.br_if) == NULL) { | |
1952 | zlog_warn("%s family %s IF %s(%u) brIF %u - no bridge master", | |
1953 | nl_msg_type_to_str(h->nlmsg_type), | |
1954 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
1955 | ndm->ndm_ifindex, zif->brslave_info.bridge_ifindex); | |
1956 | return 0; | |
1957 | } | |
1958 | ||
1959 | /* Parse attributes and extract fields of interest. */ | |
1960 | memset(tb, 0, sizeof tb); | |
1961 | netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len); | |
1962 | ||
1963 | if (!tb[NDA_LLADDR]) { | |
1964 | zlog_warn("%s family %s IF %s(%u) brIF %u - no LLADDR", | |
1965 | nl_msg_type_to_str(h->nlmsg_type), | |
1966 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
1967 | ndm->ndm_ifindex, zif->brslave_info.bridge_ifindex); | |
1968 | return 0; | |
1969 | } | |
1970 | ||
ff8b7eb8 | 1971 | if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) { |
d62a17ae | 1972 | zlog_warn( |
df0b13cf | 1973 | "%s family %s IF %s(%u) brIF %u - LLADDR is not MAC, len %lu", |
d62a17ae | 1974 | nl_msg_type_to_str(h->nlmsg_type), |
1975 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
1976 | ndm->ndm_ifindex, zif->brslave_info.bridge_ifindex, | |
df0b13cf | 1977 | (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR])); |
d62a17ae | 1978 | return 0; |
1979 | } | |
1980 | ||
ff8b7eb8 | 1981 | memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN); |
d62a17ae | 1982 | |
1983 | if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) { | |
1984 | vid_present = 1; | |
d7c0a89a | 1985 | vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]); |
d62a17ae | 1986 | sprintf(vid_buf, " VLAN %u", vid); |
1987 | } | |
1988 | ||
1989 | if (tb[NDA_DST]) { | |
1990 | /* TODO: Only IPv4 supported now. */ | |
1991 | dst_present = 1; | |
1992 | vtep_ip.family = AF_INET; | |
1993 | vtep_ip.prefixlen = IPV4_MAX_BITLEN; | |
1994 | memcpy(&(vtep_ip.u.prefix4.s_addr), RTA_DATA(tb[NDA_DST]), | |
1995 | IPV4_MAX_BYTELEN); | |
1996 | sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip.u.prefix4)); | |
1997 | } | |
1998 | ||
1999 | sticky = (ndm->ndm_state & NUD_NOARP) ? 1 : 0; | |
2000 | ||
2001 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2002 | zlog_debug("Rx %s family %s IF %s(%u)%s %sMAC %s%s", | |
2003 | nl_msg_type_to_str(h->nlmsg_type), | |
2004 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
2005 | ndm->ndm_ifindex, vid_present ? vid_buf : "", | |
2006 | sticky ? "sticky " : "", | |
2007 | prefix_mac2str(&mac, buf, sizeof(buf)), | |
2008 | dst_present ? dst_buf : ""); | |
2009 | ||
2010 | if (filter_vlan && vid != filter_vlan) | |
2011 | return 0; | |
2012 | ||
2013 | /* If add or update, do accordingly if learnt on a "local" interface; if | |
2014 | * the notification is over VxLAN, this has to be related to | |
2015 | * multi-homing, | |
2016 | * so perform an implicit delete of any local entry (if it exists). | |
2017 | */ | |
2018 | if (h->nlmsg_type == RTM_NEWNEIGH) { | |
2019 | /* Drop "permanent" entries. */ | |
2020 | if (ndm->ndm_state & NUD_PERMANENT) | |
2021 | return 0; | |
2022 | ||
2023 | if (IS_ZEBRA_IF_VXLAN(ifp)) | |
2024 | return zebra_vxlan_check_del_local_mac(ifp, br_if, &mac, | |
2025 | vid); | |
2026 | ||
2027 | return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid, | |
2028 | sticky); | |
2029 | } | |
2030 | ||
2031 | /* This is a delete notification. | |
2032 | * 1. For a MAC over VxLan, check if it needs to be refreshed(readded) | |
2033 | * 2. For a MAC over "local" interface, delete the mac | |
2034 | * Note: We will get notifications from both bridge driver and VxLAN | |
2035 | * driver. | |
2036 | * Ignore the notification from VxLan driver as it is also generated | |
2037 | * when mac moves from remote to local. | |
2038 | */ | |
2039 | if (dst_present) | |
2040 | return 0; | |
2041 | ||
2042 | if (IS_ZEBRA_IF_VXLAN(ifp)) | |
2043 | return zebra_vxlan_check_readd_remote_mac(ifp, br_if, &mac, | |
2044 | vid); | |
2045 | ||
2046 | return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid); | |
2232a77c | 2047 | } |
2048 | ||
2414abd3 | 2049 | static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
2232a77c | 2050 | { |
d62a17ae | 2051 | int len; |
2052 | struct ndmsg *ndm; | |
2232a77c | 2053 | |
d62a17ae | 2054 | if (h->nlmsg_type != RTM_NEWNEIGH) |
2055 | return 0; | |
2232a77c | 2056 | |
d62a17ae | 2057 | /* Length validity. */ |
2058 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg)); | |
2059 | if (len < 0) | |
2060 | return -1; | |
2232a77c | 2061 | |
d62a17ae | 2062 | /* We are interested only in AF_BRIDGE notifications. */ |
2063 | ndm = NLMSG_DATA(h); | |
2064 | if (ndm->ndm_family != AF_BRIDGE) | |
2065 | return 0; | |
2232a77c | 2066 | |
2414abd3 | 2067 | return netlink_macfdb_change(h, len, ns_id); |
2232a77c | 2068 | } |
2069 | ||
2070 | /* Request for MAC FDB information from the kernel */ | |
d62a17ae | 2071 | static int netlink_request_macs(struct zebra_ns *zns, int family, int type, |
2072 | ifindex_t master_ifindex) | |
2232a77c | 2073 | { |
d62a17ae | 2074 | struct { |
2075 | struct nlmsghdr n; | |
2076 | struct ifinfomsg ifm; | |
2077 | char buf[256]; | |
2078 | } req; | |
2079 | ||
2080 | /* Form the request, specifying filter (rtattr) if needed. */ | |
2081 | memset(&req, 0, sizeof(req)); | |
2082 | req.n.nlmsg_type = type; | |
2083 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
2084 | req.ifm.ifi_family = family; | |
2085 | if (master_ifindex) | |
2086 | addattr32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex); | |
2087 | ||
2088 | return netlink_request(&zns->netlink_cmd, &req.n); | |
2232a77c | 2089 | } |
2090 | ||
2091 | /* | |
2092 | * MAC forwarding database read using netlink interface. This is invoked | |
2093 | * at startup. | |
2094 | */ | |
d62a17ae | 2095 | int netlink_macfdb_read(struct zebra_ns *zns) |
2232a77c | 2096 | { |
d62a17ae | 2097 | int ret; |
2098 | ||
2099 | /* Get bridge FDB table. */ | |
2100 | ret = netlink_request_macs(zns, AF_BRIDGE, RTM_GETNEIGH, 0); | |
2101 | if (ret < 0) | |
2102 | return ret; | |
2103 | /* We are reading entire table. */ | |
2104 | filter_vlan = 0; | |
2105 | ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, zns, | |
2106 | 0, 1); | |
2107 | ||
2108 | return ret; | |
2232a77c | 2109 | } |
2110 | ||
2111 | /* | |
2112 | * MAC forwarding database read using netlink interface. This is for a | |
2113 | * specific bridge and matching specific access VLAN (if VLAN-aware bridge). | |
2114 | */ | |
d62a17ae | 2115 | int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp, |
2116 | struct interface *br_if) | |
2232a77c | 2117 | { |
d62a17ae | 2118 | struct zebra_if *br_zif; |
2119 | struct zebra_if *zif; | |
2120 | struct zebra_l2info_vxlan *vxl; | |
2121 | int ret = 0; | |
2122 | ||
2123 | ||
2124 | /* Save VLAN we're filtering on, if needed. */ | |
2125 | br_zif = (struct zebra_if *)br_if->info; | |
2126 | zif = (struct zebra_if *)ifp->info; | |
2127 | vxl = &zif->l2info.vxl; | |
2128 | if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif)) | |
2129 | filter_vlan = vxl->access_vlan; | |
2130 | ||
2131 | /* Get bridge FDB table for specific bridge - we do the VLAN filtering. | |
2132 | */ | |
2133 | ret = netlink_request_macs(zns, AF_BRIDGE, RTM_GETNEIGH, | |
2134 | br_if->ifindex); | |
2135 | if (ret < 0) | |
2136 | return ret; | |
2137 | ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, zns, | |
2138 | 0, 0); | |
2139 | ||
2140 | /* Reset VLAN filter. */ | |
2141 | filter_vlan = 0; | |
2142 | return ret; | |
2232a77c | 2143 | } |
2144 | ||
d62a17ae | 2145 | static int netlink_macfdb_update(struct interface *ifp, vlanid_t vid, |
2146 | struct ethaddr *mac, struct in_addr vtep_ip, | |
d7c0a89a | 2147 | int local, int cmd, uint8_t sticky) |
2232a77c | 2148 | { |
5895d33f | 2149 | struct zebra_ns *zns; |
d62a17ae | 2150 | struct { |
2151 | struct nlmsghdr n; | |
2152 | struct ndmsg ndm; | |
2153 | char buf[256]; | |
2154 | } req; | |
2155 | int dst_alen; | |
2156 | struct zebra_if *zif; | |
2157 | struct interface *br_if; | |
2158 | struct zebra_if *br_zif; | |
2159 | char buf[ETHER_ADDR_STRLEN]; | |
2160 | int vid_present = 0, dst_present = 0; | |
2161 | char vid_buf[20]; | |
2162 | char dst_buf[30]; | |
009f8ad5 | 2163 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id); |
d62a17ae | 2164 | |
009f8ad5 | 2165 | zns = zvrf->zns; |
d62a17ae | 2166 | zif = ifp->info; |
2167 | if ((br_if = zif->brslave_info.br_if) == NULL) { | |
2168 | zlog_warn("MAC %s on IF %s(%u) - no mapping to bridge", | |
2169 | (cmd == RTM_NEWNEIGH) ? "add" : "del", ifp->name, | |
2170 | ifp->ifindex); | |
2171 | return -1; | |
2172 | } | |
2173 | ||
5605ecfc | 2174 | memset(&req, 0, sizeof(req)); |
d62a17ae | 2175 | |
2176 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
2177 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
2178 | if (cmd == RTM_NEWNEIGH) | |
2179 | req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE); | |
2180 | req.n.nlmsg_type = cmd; | |
2181 | req.ndm.ndm_family = AF_BRIDGE; | |
2182 | req.ndm.ndm_flags |= NTF_SELF | NTF_MASTER; | |
2183 | req.ndm.ndm_state = NUD_REACHABLE; | |
2184 | ||
2185 | if (sticky) | |
2186 | req.ndm.ndm_state |= NUD_NOARP; | |
2187 | else | |
2188 | req.ndm.ndm_flags |= NTF_EXT_LEARNED; | |
2189 | ||
2190 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6); | |
2191 | req.ndm.ndm_ifindex = ifp->ifindex; | |
2192 | if (!local) { | |
2193 | dst_alen = 4; // TODO: hardcoded | |
2194 | addattr_l(&req.n, sizeof(req), NDA_DST, &vtep_ip, dst_alen); | |
2195 | dst_present = 1; | |
2196 | sprintf(dst_buf, " dst %s", inet_ntoa(vtep_ip)); | |
2197 | } | |
2198 | br_zif = (struct zebra_if *)br_if->info; | |
2199 | if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0) { | |
2200 | addattr16(&req.n, sizeof(req), NDA_VLAN, vid); | |
2201 | vid_present = 1; | |
2202 | sprintf(vid_buf, " VLAN %u", vid); | |
2203 | } | |
2204 | addattr32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex); | |
2205 | ||
2206 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2207 | zlog_debug("Tx %s family %s IF %s(%u)%s %sMAC %s%s", | |
2208 | nl_msg_type_to_str(cmd), | |
2209 | nl_family_to_str(req.ndm.ndm_family), ifp->name, | |
2210 | ifp->ifindex, vid_present ? vid_buf : "", | |
2211 | sticky ? "sticky " : "", | |
2212 | prefix_mac2str(mac, buf, sizeof(buf)), | |
2213 | dst_present ? dst_buf : ""); | |
2214 | ||
2215 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
2216 | 0); | |
2232a77c | 2217 | } |
2218 | ||
d62a17ae | 2219 | #define NUD_VALID \ |
2220 | (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \ | |
2221 | | NUD_DELAY) | |
2232a77c | 2222 | |
2414abd3 | 2223 | static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id) |
2232a77c | 2224 | { |
d62a17ae | 2225 | struct ndmsg *ndm; |
2226 | struct interface *ifp; | |
2227 | struct zebra_if *zif; | |
d62a17ae | 2228 | struct rtattr *tb[NDA_MAX + 1]; |
2229 | struct interface *link_if; | |
2230 | struct ethaddr mac; | |
2231 | struct ipaddr ip; | |
2232 | char buf[ETHER_ADDR_STRLEN]; | |
2233 | char buf2[INET6_ADDRSTRLEN]; | |
2234 | int mac_present = 0; | |
d7c0a89a | 2235 | uint8_t ext_learned; |
68e33151 | 2236 | uint8_t router_flag; |
d62a17ae | 2237 | |
2238 | ndm = NLMSG_DATA(h); | |
2239 | ||
2240 | /* The interface should exist. */ | |
5895d33f | 2241 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), |
d62a17ae | 2242 | ndm->ndm_ifindex); |
2853fed6 | 2243 | if (!ifp || !ifp->info) |
d62a17ae | 2244 | return 0; |
2245 | ||
20089ae2 DS |
2246 | zif = (struct zebra_if *)ifp->info; |
2247 | ||
2248 | /* Parse attributes and extract fields of interest. */ | |
2249 | memset(tb, 0, sizeof tb); | |
2250 | netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len); | |
2251 | ||
2252 | if (!tb[NDA_DST]) { | |
2253 | zlog_warn("%s family %s IF %s(%u) - no DST", | |
2254 | nl_msg_type_to_str(h->nlmsg_type), | |
2255 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
2256 | ndm->ndm_ifindex); | |
d62a17ae | 2257 | return 0; |
20089ae2 DS |
2258 | } |
2259 | ||
2260 | memset(&ip, 0, sizeof(struct ipaddr)); | |
2261 | ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6; | |
2262 | memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST])); | |
2263 | ||
2264 | /* Drop some "permanent" entries. */ | |
2265 | if (ndm->ndm_state & NUD_PERMANENT) { | |
2266 | char buf[16] = "169.254.0.1"; | |
2267 | struct in_addr ipv4_ll; | |
2268 | ||
2269 | if (ndm->ndm_family != AF_INET) | |
2270 | return 0; | |
2271 | ||
2272 | if (!zif->v6_2_v4_ll_neigh_entry) | |
2273 | return 0; | |
2274 | ||
2275 | if (h->nlmsg_type != RTM_DELNEIGH) | |
2276 | return 0; | |
2277 | ||
2278 | inet_pton(AF_INET, buf, &ipv4_ll); | |
2279 | if (ipv4_ll.s_addr != ip.ip._v4_addr.s_addr) | |
2280 | return 0; | |
2281 | ||
2282 | if_nbr_ipv6ll_to_ipv4ll_neigh_update( | |
2283 | ifp, &zif->v6_2_v4_ll_addr6, true); | |
2284 | return 0; | |
2285 | } | |
d62a17ae | 2286 | |
d62a17ae | 2287 | /* The neighbor is present on an SVI. From this, we locate the |
2288 | * underlying | |
2289 | * bridge because we're only interested in neighbors on a VxLAN bridge. | |
2290 | * The bridge is located based on the nature of the SVI: | |
2291 | * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN | |
2292 | * interface | |
2293 | * and is linked to the bridge | |
2294 | * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge | |
2295 | * inteface | |
2296 | * itself | |
2297 | */ | |
2298 | if (IS_ZEBRA_IF_VLAN(ifp)) { | |
5895d33f | 2299 | link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), |
71349e03 | 2300 | zif->link_ifindex); |
d62a17ae | 2301 | if (!link_if) |
2302 | return 0; | |
2303 | } else if (IS_ZEBRA_IF_BRIDGE(ifp)) | |
2304 | link_if = ifp; | |
2305 | else | |
2306 | return 0; | |
2307 | ||
d62a17ae | 2308 | memset(&mac, 0, sizeof(struct ethaddr)); |
d62a17ae | 2309 | if (h->nlmsg_type == RTM_NEWNEIGH) { |
2310 | if (tb[NDA_LLADDR]) { | |
ff8b7eb8 | 2311 | if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) { |
d62a17ae | 2312 | zlog_warn( |
df0b13cf | 2313 | "%s family %s IF %s(%u) - LLADDR is not MAC, len %lu", |
d62a17ae | 2314 | nl_msg_type_to_str(h->nlmsg_type), |
2315 | nl_family_to_str(ndm->ndm_family), | |
2316 | ifp->name, ndm->ndm_ifindex, | |
996c9314 LB |
2317 | (unsigned long)RTA_PAYLOAD( |
2318 | tb[NDA_LLADDR])); | |
d62a17ae | 2319 | return 0; |
2320 | } | |
2321 | ||
2322 | mac_present = 1; | |
ff8b7eb8 | 2323 | memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN); |
d62a17ae | 2324 | } |
2325 | ||
2326 | ext_learned = (ndm->ndm_flags & NTF_EXT_LEARNED) ? 1 : 0; | |
68e33151 | 2327 | router_flag = (ndm->ndm_flags & NTF_ROUTER) ? 1 : 0; |
d62a17ae | 2328 | |
2329 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2330 | zlog_debug( | |
2331 | "Rx %s family %s IF %s(%u) IP %s MAC %s state 0x%x flags 0x%x", | |
2332 | nl_msg_type_to_str(h->nlmsg_type), | |
2333 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
2334 | ndm->ndm_ifindex, | |
2335 | ipaddr2str(&ip, buf2, sizeof(buf2)), | |
2336 | mac_present | |
2337 | ? prefix_mac2str(&mac, buf, sizeof(buf)) | |
2338 | : "", | |
2339 | ndm->ndm_state, ndm->ndm_flags); | |
2340 | ||
2341 | /* If the neighbor state is valid for use, process as an add or | |
2342 | * update | |
2343 | * else process as a delete. Note that the delete handling may | |
2344 | * result | |
2345 | * in re-adding the neighbor if it is a valid "remote" neighbor. | |
2346 | */ | |
2347 | if (ndm->ndm_state & NUD_VALID) | |
ee69da27 | 2348 | return zebra_vxlan_handle_kernel_neigh_update( |
d62a17ae | 2349 | ifp, link_if, &ip, &mac, ndm->ndm_state, |
68e33151 | 2350 | ext_learned, router_flag); |
d62a17ae | 2351 | |
ee69da27 | 2352 | return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip); |
d62a17ae | 2353 | } |
2354 | ||
2355 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2356 | zlog_debug("Rx %s family %s IF %s(%u) IP %s", | |
2357 | nl_msg_type_to_str(h->nlmsg_type), | |
2358 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
2359 | ndm->ndm_ifindex, | |
2360 | ipaddr2str(&ip, buf2, sizeof(buf2))); | |
2361 | ||
2362 | /* Process the delete - it may result in re-adding the neighbor if it is | |
2363 | * a valid "remote" neighbor. | |
2364 | */ | |
ee69da27 | 2365 | return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip); |
2232a77c | 2366 | } |
2367 | ||
2414abd3 | 2368 | static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
2232a77c | 2369 | { |
d62a17ae | 2370 | int len; |
2371 | struct ndmsg *ndm; | |
2232a77c | 2372 | |
d62a17ae | 2373 | if (h->nlmsg_type != RTM_NEWNEIGH) |
2374 | return 0; | |
2232a77c | 2375 | |
d62a17ae | 2376 | /* Length validity. */ |
2377 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg)); | |
2378 | if (len < 0) | |
2379 | return -1; | |
2232a77c | 2380 | |
d62a17ae | 2381 | /* We are interested only in AF_INET or AF_INET6 notifications. */ |
2382 | ndm = NLMSG_DATA(h); | |
2383 | if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6) | |
2384 | return 0; | |
2232a77c | 2385 | |
2414abd3 | 2386 | return netlink_neigh_change(h, len); |
2232a77c | 2387 | } |
2388 | ||
2389 | /* Request for IP neighbor information from the kernel */ | |
d62a17ae | 2390 | static int netlink_request_neigh(struct zebra_ns *zns, int family, int type, |
2391 | ifindex_t ifindex) | |
2232a77c | 2392 | { |
d62a17ae | 2393 | struct { |
2394 | struct nlmsghdr n; | |
2395 | struct ndmsg ndm; | |
2396 | char buf[256]; | |
2397 | } req; | |
2398 | ||
2399 | /* Form the request, specifying filter (rtattr) if needed. */ | |
2400 | memset(&req, 0, sizeof(req)); | |
2401 | req.n.nlmsg_type = type; | |
2402 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
2403 | req.ndm.ndm_family = family; | |
2404 | if (ifindex) | |
2405 | addattr32(&req.n, sizeof(req), NDA_IFINDEX, ifindex); | |
2406 | ||
2407 | return netlink_request(&zns->netlink_cmd, &req.n); | |
2232a77c | 2408 | } |
2409 | ||
2410 | /* | |
2411 | * IP Neighbor table read using netlink interface. This is invoked | |
2412 | * at startup. | |
2413 | */ | |
d62a17ae | 2414 | int netlink_neigh_read(struct zebra_ns *zns) |
2232a77c | 2415 | { |
d62a17ae | 2416 | int ret; |
2232a77c | 2417 | |
d62a17ae | 2418 | /* Get IP neighbor table. */ |
2419 | ret = netlink_request_neigh(zns, AF_UNSPEC, RTM_GETNEIGH, 0); | |
2420 | if (ret < 0) | |
2421 | return ret; | |
2422 | ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, zns, 0, | |
2423 | 1); | |
2232a77c | 2424 | |
d62a17ae | 2425 | return ret; |
2232a77c | 2426 | } |
2427 | ||
2428 | /* | |
2429 | * IP Neighbor table read using netlink interface. This is for a specific | |
2430 | * VLAN device. | |
2431 | */ | |
d62a17ae | 2432 | int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if) |
2232a77c | 2433 | { |
d62a17ae | 2434 | int ret = 0; |
2232a77c | 2435 | |
d62a17ae | 2436 | ret = netlink_request_neigh(zns, AF_UNSPEC, RTM_GETNEIGH, |
2437 | vlan_if->ifindex); | |
2438 | if (ret < 0) | |
2439 | return ret; | |
2440 | ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, zns, 0, | |
2441 | 0); | |
2232a77c | 2442 | |
d62a17ae | 2443 | return ret; |
2232a77c | 2444 | } |
2445 | ||
2414abd3 | 2446 | int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id) |
2232a77c | 2447 | { |
d62a17ae | 2448 | int len; |
2449 | struct ndmsg *ndm; | |
2232a77c | 2450 | |
d62a17ae | 2451 | if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH)) |
2452 | return 0; | |
2232a77c | 2453 | |
d62a17ae | 2454 | /* Length validity. */ |
2455 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg)); | |
9bdf8618 DS |
2456 | if (len < 0) { |
2457 | zlog_err("%s: Message received from netlink is of a broken size %d %zu", | |
2458 | __PRETTY_FUNCTION__, h->nlmsg_len, | |
2459 | (size_t)NLMSG_LENGTH(sizeof(struct ndmsg))); | |
d62a17ae | 2460 | return -1; |
9bdf8618 | 2461 | } |
2232a77c | 2462 | |
d62a17ae | 2463 | /* Is this a notification for the MAC FDB or IP neighbor table? */ |
2464 | ndm = NLMSG_DATA(h); | |
2465 | if (ndm->ndm_family == AF_BRIDGE) | |
2414abd3 | 2466 | return netlink_macfdb_change(h, len, ns_id); |
2232a77c | 2467 | |
d62a17ae | 2468 | if (ndm->ndm_type != RTN_UNICAST) |
2469 | return 0; | |
2232a77c | 2470 | |
d62a17ae | 2471 | if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6) |
2414abd3 | 2472 | return netlink_ipneigh_change(h, len, ns_id); |
8a1b681c SW |
2473 | else { |
2474 | zlog_warn( | |
81227874 | 2475 | "Invalid address family: %u received from kernel neighbor change: %u", |
8a1b681c SW |
2476 | ndm->ndm_family, h->nlmsg_type); |
2477 | return 0; | |
2478 | } | |
2232a77c | 2479 | |
d62a17ae | 2480 | return 0; |
2232a77c | 2481 | } |
2482 | ||
d62a17ae | 2483 | static int netlink_neigh_update2(struct interface *ifp, struct ipaddr *ip, |
68e33151 CS |
2484 | struct ethaddr *mac, uint8_t flags, |
2485 | uint16_t state, int cmd) | |
2232a77c | 2486 | { |
d62a17ae | 2487 | struct { |
2488 | struct nlmsghdr n; | |
2489 | struct ndmsg ndm; | |
2490 | char buf[256]; | |
2491 | } req; | |
2492 | int ipa_len; | |
2493 | ||
5895d33f | 2494 | struct zebra_ns *zns; |
d62a17ae | 2495 | char buf[INET6_ADDRSTRLEN]; |
2496 | char buf2[ETHER_ADDR_STRLEN]; | |
009f8ad5 | 2497 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(ifp->vrf_id); |
d62a17ae | 2498 | |
009f8ad5 | 2499 | zns = zvrf->zns; |
5605ecfc | 2500 | memset(&req, 0, sizeof(req)); |
d62a17ae | 2501 | |
2502 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
2503 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
2504 | if (cmd == RTM_NEWNEIGH) | |
2505 | req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE); | |
2506 | req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH | |
2507 | req.ndm.ndm_family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6; | |
68e33151 | 2508 | req.ndm.ndm_state = state; |
d62a17ae | 2509 | req.ndm.ndm_ifindex = ifp->ifindex; |
2510 | req.ndm.ndm_type = RTN_UNICAST; | |
68e33151 | 2511 | req.ndm.ndm_flags = flags; |
d62a17ae | 2512 | |
2513 | ipa_len = IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN; | |
2514 | addattr_l(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len); | |
2515 | if (mac) | |
2516 | addattr_l(&req.n, sizeof(req), NDA_LLADDR, mac, 6); | |
2517 | ||
2518 | if (IS_ZEBRA_DEBUG_KERNEL) | |
68e33151 | 2519 | zlog_debug("Tx %s family %s IF %s(%u) Neigh %s MAC %s flags 0x%x", |
d62a17ae | 2520 | nl_msg_type_to_str(cmd), |
2521 | nl_family_to_str(req.ndm.ndm_family), ifp->name, | |
2522 | ifp->ifindex, ipaddr2str(ip, buf, sizeof(buf)), | |
2523 | mac ? prefix_mac2str(mac, buf2, sizeof(buf2)) | |
68e33151 | 2524 | : "null", flags); |
d62a17ae | 2525 | |
2526 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
2527 | 0); | |
2232a77c | 2528 | } |
2529 | ||
d62a17ae | 2530 | int kernel_add_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac, |
d7c0a89a | 2531 | struct in_addr vtep_ip, uint8_t sticky) |
2232a77c | 2532 | { |
d62a17ae | 2533 | return netlink_macfdb_update(ifp, vid, mac, vtep_ip, 0, RTM_NEWNEIGH, |
2534 | sticky); | |
2232a77c | 2535 | } |
2536 | ||
d62a17ae | 2537 | int kernel_del_mac(struct interface *ifp, vlanid_t vid, struct ethaddr *mac, |
2538 | struct in_addr vtep_ip, int local) | |
2232a77c | 2539 | { |
d62a17ae | 2540 | return netlink_macfdb_update(ifp, vid, mac, vtep_ip, local, |
2541 | RTM_DELNEIGH, 0); | |
2232a77c | 2542 | } |
2543 | ||
d62a17ae | 2544 | int kernel_add_neigh(struct interface *ifp, struct ipaddr *ip, |
68e33151 | 2545 | struct ethaddr *mac, uint8_t flags) |
2232a77c | 2546 | { |
68e33151 CS |
2547 | return netlink_neigh_update2(ifp, ip, mac, flags, |
2548 | NUD_NOARP, RTM_NEWNEIGH); | |
2232a77c | 2549 | } |
2550 | ||
d62a17ae | 2551 | int kernel_del_neigh(struct interface *ifp, struct ipaddr *ip) |
2232a77c | 2552 | { |
68e33151 | 2553 | return netlink_neigh_update2(ifp, ip, NULL, 0, 0, RTM_DELNEIGH); |
2232a77c | 2554 | } |
2555 | ||
40c7bdb0 | 2556 | /* |
2557 | * MPLS label forwarding table change via netlink interface. | |
2558 | */ | |
d62a17ae | 2559 | int netlink_mpls_multipath(int cmd, zebra_lsp_t *lsp) |
40c7bdb0 | 2560 | { |
d62a17ae | 2561 | mpls_lse_t lse; |
2562 | zebra_nhlfe_t *nhlfe; | |
2563 | struct nexthop *nexthop = NULL; | |
2564 | unsigned int nexthop_num; | |
2565 | const char *routedesc; | |
2566 | struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT); | |
805444ce | 2567 | int route_type; |
d62a17ae | 2568 | |
2569 | struct { | |
2570 | struct nlmsghdr n; | |
2571 | struct rtmsg r; | |
2572 | char buf[NL_PKT_BUF_SIZE]; | |
2573 | } req; | |
2574 | ||
2575 | memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE); | |
2576 | ||
d62a17ae | 2577 | /* |
2578 | * Count # nexthops so we can decide whether to use singlepath | |
2579 | * or multipath case. | |
2580 | */ | |
2581 | nexthop_num = 0; | |
2582 | for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) { | |
2583 | nexthop = nhlfe->nexthop; | |
2584 | if (!nexthop) | |
2585 | continue; | |
2586 | if (cmd == RTM_NEWROUTE) { | |
2587 | /* Count all selected NHLFEs */ | |
2588 | if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) | |
2589 | && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
2590 | nexthop_num++; | |
2591 | } else /* DEL */ | |
2592 | { | |
2593 | /* Count all installed NHLFEs */ | |
2594 | if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) | |
2595 | && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
2596 | nexthop_num++; | |
2597 | } | |
2598 | } | |
2599 | ||
8dc8a4b6 | 2600 | if ((nexthop_num == 0) || (!lsp->best_nhlfe && (cmd != RTM_DELROUTE))) |
d62a17ae | 2601 | return 0; |
2602 | ||
2603 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
2604 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
2605 | req.n.nlmsg_type = cmd; | |
2606 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
2607 | ||
2608 | req.r.rtm_family = AF_MPLS; | |
2609 | req.r.rtm_table = RT_TABLE_MAIN; | |
2610 | req.r.rtm_dst_len = MPLS_LABEL_LEN_BITS; | |
d62a17ae | 2611 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
2612 | req.r.rtm_type = RTN_UNICAST; | |
2613 | ||
8dc8a4b6 | 2614 | if (cmd == RTM_NEWROUTE) { |
d62a17ae | 2615 | /* We do a replace to handle update. */ |
2616 | req.n.nlmsg_flags |= NLM_F_REPLACE; | |
2617 | ||
8dc8a4b6 DS |
2618 | /* set the protocol value if installing */ |
2619 | route_type = re_type_from_lsp_type(lsp->best_nhlfe->type); | |
2620 | req.r.rtm_protocol = zebra2proto(route_type); | |
2621 | } | |
2622 | ||
d62a17ae | 2623 | /* Fill destination */ |
2624 | lse = mpls_lse_encode(lsp->ile.in_label, 0, 0, 1); | |
2625 | addattr_l(&req.n, sizeof req, RTA_DST, &lse, sizeof(mpls_lse_t)); | |
2626 | ||
2627 | /* Fill nexthops (paths) based on single-path or multipath. The paths | |
2628 | * chosen depend on the operation. | |
2629 | */ | |
2630 | if (nexthop_num == 1 || multipath_num == 1) { | |
8b1450b9 | 2631 | routedesc = "single-path"; |
d62a17ae | 2632 | _netlink_mpls_debug(cmd, lsp->ile.in_label, routedesc); |
2633 | ||
2634 | nexthop_num = 0; | |
2635 | for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) { | |
2636 | nexthop = nhlfe->nexthop; | |
2637 | if (!nexthop) | |
2638 | continue; | |
2639 | ||
2640 | if ((cmd == RTM_NEWROUTE | |
2641 | && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) | |
2642 | && CHECK_FLAG(nexthop->flags, | |
2643 | NEXTHOP_FLAG_ACTIVE))) | |
2644 | || (cmd == RTM_DELROUTE | |
2645 | && (CHECK_FLAG(nhlfe->flags, | |
2646 | NHLFE_FLAG_INSTALLED) | |
2647 | && CHECK_FLAG(nexthop->flags, | |
2648 | NEXTHOP_FLAG_FIB)))) { | |
2649 | /* Add the gateway */ | |
2650 | _netlink_mpls_build_singlepath(routedesc, nhlfe, | |
2651 | &req.n, &req.r, | |
2652 | sizeof req, cmd); | |
d62a17ae | 2653 | nexthop_num++; |
2654 | break; | |
2655 | } | |
2656 | } | |
2657 | } else /* Multipath case */ | |
2658 | { | |
2659 | char buf[NL_PKT_BUF_SIZE]; | |
2660 | struct rtattr *rta = (void *)buf; | |
2661 | struct rtnexthop *rtnh; | |
2662 | union g_addr *src1 = NULL; | |
2663 | ||
2664 | rta->rta_type = RTA_MULTIPATH; | |
2665 | rta->rta_len = RTA_LENGTH(0); | |
2666 | rtnh = RTA_DATA(rta); | |
2667 | ||
8b1450b9 | 2668 | routedesc = "multipath"; |
d62a17ae | 2669 | _netlink_mpls_debug(cmd, lsp->ile.in_label, routedesc); |
2670 | ||
2671 | nexthop_num = 0; | |
2672 | for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) { | |
2673 | nexthop = nhlfe->nexthop; | |
2674 | if (!nexthop) | |
2675 | continue; | |
2676 | ||
2677 | if (nexthop_num >= multipath_num) | |
2678 | break; | |
2679 | ||
2680 | if ((cmd == RTM_NEWROUTE | |
2681 | && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) | |
2682 | && CHECK_FLAG(nexthop->flags, | |
2683 | NEXTHOP_FLAG_ACTIVE))) | |
2684 | || (cmd == RTM_DELROUTE | |
2685 | && (CHECK_FLAG(nhlfe->flags, | |
2686 | NHLFE_FLAG_INSTALLED) | |
2687 | && CHECK_FLAG(nexthop->flags, | |
2688 | NEXTHOP_FLAG_FIB)))) { | |
2689 | nexthop_num++; | |
2690 | ||
2691 | /* Build the multipath */ | |
2692 | _netlink_mpls_build_multipath(routedesc, nhlfe, | |
2693 | rta, rtnh, &req.r, | |
2694 | &src1); | |
2695 | rtnh = RTNH_NEXT(rtnh); | |
d62a17ae | 2696 | } |
2697 | } | |
2698 | ||
2699 | /* Add the multipath */ | |
2700 | if (rta->rta_len > RTA_LENGTH(0)) | |
2701 | addattr_l(&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH, | |
2702 | RTA_DATA(rta), RTA_PAYLOAD(rta)); | |
2703 | } | |
2704 | ||
2705 | /* Talk to netlink socket. */ | |
2706 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
2707 | 0); | |
40c7bdb0 | 2708 | } |
ddfeb486 | 2709 | #endif /* HAVE_NETLINK */ |