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