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