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