]>
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> | |
d9f5b2f5 | 30 | #include <linux/nexthop.h> |
718e3744 | 31 | |
32 | /* Hack for GNU libc version 2. */ | |
33 | #ifndef MSG_TRUNC | |
34 | #define MSG_TRUNC 0x20 | |
35 | #endif /* MSG_TRUNC */ | |
36 | ||
37 | #include "linklist.h" | |
38 | #include "if.h" | |
39 | #include "log.h" | |
40 | #include "prefix.h" | |
41 | #include "connected.h" | |
42 | #include "table.h" | |
26e2ae36 | 43 | #include "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" |
8d03bc50 | 52 | #include "printfrr.h" |
718e3744 | 53 | |
bf094f69 | 54 | #include "zebra/zapi_msg.h" |
fe18ee2d | 55 | #include "zebra/zebra_ns.h" |
7c551956 | 56 | #include "zebra/zebra_vrf.h" |
6621ca86 | 57 | #include "zebra/rt.h" |
718e3744 | 58 | #include "zebra/redistribute.h" |
59 | #include "zebra/interface.h" | |
60 | #include "zebra/debug.h" | |
12f6fb97 | 61 | #include "zebra/rtadv.h" |
567b877d | 62 | #include "zebra/zebra_ptm.h" |
40c7bdb0 | 63 | #include "zebra/zebra_mpls.h" |
1fdc9eae | 64 | #include "zebra/kernel_netlink.h" |
65 | #include "zebra/rt_netlink.h" | |
d9f5b2f5 | 66 | #include "zebra/zebra_nhg.h" |
e3be0432 | 67 | #include "zebra/zebra_mroute.h" |
2232a77c | 68 | #include "zebra/zebra_vxlan.h" |
364fed6b | 69 | #include "zebra/zebra_errors.h" |
506efd37 | 70 | #include "zebra/zebra_evpn_mh.h" |
e3be0432 | 71 | |
40c7bdb0 | 72 | #ifndef AF_MPLS |
73 | #define AF_MPLS 28 | |
74 | #endif | |
75 | ||
d87ed8d7 AK |
76 | /* Re-defining as I am unable to include <linux/if_bridge.h> which has the |
77 | * UAPI for MAC sync. */ | |
78 | #ifndef _UAPI_LINUX_IF_BRIDGE_H | |
4bcdb608 | 79 | #define BR_SPH_LIST_SIZE 10 |
d87ed8d7 AK |
80 | #endif |
81 | ||
2232a77c | 82 | static vlanid_t filter_vlan = 0; |
83 | ||
7c99d51b MS |
84 | /* We capture whether the current kernel supports nexthop ids; by |
85 | * default, we'll use them if possible. There's also a configuration | |
86 | * available to _disable_ use of kernel nexthops. | |
87 | */ | |
fec211ad | 88 | static bool supports_nh; |
81505946 | 89 | |
d62a17ae | 90 | struct gw_family_t { |
d7c0a89a QY |
91 | uint16_t filler; |
92 | uint16_t family; | |
d62a17ae | 93 | union g_addr gate; |
40c7bdb0 | 94 | }; |
95 | ||
2b64873d DL |
96 | static const char ipv4_ll_buf[16] = "169.254.0.1"; |
97 | static struct in_addr ipv4_ll; | |
8755598a | 98 | |
002e5c43 SW |
99 | /* Is this a ipv4 over ipv6 route? */ |
100 | static bool is_route_v4_over_v6(unsigned char rtm_family, | |
101 | enum nexthop_types_t nexthop_type) | |
102 | { | |
103 | if (rtm_family == AF_INET | |
104 | && (nexthop_type == NEXTHOP_TYPE_IPV6 | |
105 | || nexthop_type == NEXTHOP_TYPE_IPV6_IFINDEX)) | |
106 | return true; | |
107 | ||
108 | return false; | |
109 | } | |
110 | ||
7c99d51b MS |
111 | /* Helper to control use of kernel-level nexthop ids */ |
112 | static bool kernel_nexthops_supported(void) | |
113 | { | |
d982012a SW |
114 | return (supports_nh && !vrf_is_backend_netns() |
115 | && zebra_nhg_kernel_nexthops_enabled()); | |
7c99d51b MS |
116 | } |
117 | ||
6c67f41f SW |
118 | /* |
119 | * Some people may only want to use NHGs created by protos and not | |
120 | * implicitly created by Zebra. This check accounts for that. | |
121 | */ | |
122 | static bool proto_nexthops_only(void) | |
123 | { | |
124 | return zebra_nhg_proto_nexthops_only(); | |
125 | } | |
126 | ||
127 | /* Is this a proto created NHG? */ | |
128 | static bool is_proto_nhg(uint32_t id, int type) | |
129 | { | |
130 | /* If type is available, use it as the source of truth */ | |
131 | if (type) { | |
132 | if (type != ZEBRA_ROUTE_NHG) | |
133 | return true; | |
134 | return false; | |
135 | } | |
136 | ||
54c89c93 | 137 | if (id >= ZEBRA_NHG_PROTO_LOWER) |
6c67f41f SW |
138 | return true; |
139 | ||
140 | return false; | |
141 | } | |
142 | ||
8755598a DS |
143 | /* |
144 | * The ipv4_ll data structure is used for all 5549 | |
145 | * additions to the kernel. Let's figure out the | |
146 | * correct value one time instead for every | |
147 | * install/remove of a 5549 type route | |
148 | */ | |
d62a17ae | 149 | void rt_netlink_init(void) |
8755598a | 150 | { |
d62a17ae | 151 | inet_pton(AF_INET, ipv4_ll_buf, &ipv4_ll); |
8755598a DS |
152 | } |
153 | ||
931fa60c MS |
154 | /* |
155 | * Mapping from dataplane neighbor flags to netlink flags | |
156 | */ | |
157 | static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags) | |
158 | { | |
159 | uint8_t flags = 0; | |
160 | ||
161 | if (dplane_flags & DPLANE_NTF_EXT_LEARNED) | |
162 | flags |= NTF_EXT_LEARNED; | |
163 | if (dplane_flags & DPLANE_NTF_ROUTER) | |
164 | flags |= NTF_ROUTER; | |
d68e74b4 JU |
165 | if (dplane_flags & DPLANE_NTF_USE) |
166 | flags |= NTF_USE; | |
931fa60c MS |
167 | |
168 | return flags; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Mapping from dataplane neighbor state to netlink state | |
173 | */ | |
174 | static uint16_t neigh_state_to_netlink(uint16_t dplane_state) | |
175 | { | |
176 | uint16_t state = 0; | |
177 | ||
178 | if (dplane_state & DPLANE_NUD_REACHABLE) | |
179 | state |= NUD_REACHABLE; | |
180 | if (dplane_state & DPLANE_NUD_STALE) | |
181 | state |= NUD_STALE; | |
182 | if (dplane_state & DPLANE_NUD_NOARP) | |
183 | state |= NUD_NOARP; | |
184 | if (dplane_state & DPLANE_NUD_PROBE) | |
185 | state |= NUD_PROBE; | |
d68e74b4 JU |
186 | if (dplane_state & DPLANE_NUD_INCOMPLETE) |
187 | state |= NUD_INCOMPLETE; | |
0a27a2fe PG |
188 | if (dplane_state & DPLANE_NUD_PERMANENT) |
189 | state |= NUD_PERMANENT; | |
190 | if (dplane_state & DPLANE_NUD_FAILED) | |
191 | state |= NUD_FAILED; | |
931fa60c MS |
192 | |
193 | return state; | |
194 | } | |
195 | ||
196 | ||
6a6d11a3 | 197 | static inline bool is_selfroute(int proto) |
23b1f334 | 198 | { |
d62a17ae | 199 | if ((proto == RTPROT_BGP) || (proto == RTPROT_OSPF) |
d4d71f11 | 200 | || (proto == RTPROT_ZSTATIC) || (proto == RTPROT_ZEBRA) |
d62a17ae | 201 | || (proto == RTPROT_ISIS) || (proto == RTPROT_RIPNG) |
202 | || (proto == RTPROT_NHRP) || (proto == RTPROT_EIGRP) | |
915902cb | 203 | || (proto == RTPROT_LDP) || (proto == RTPROT_BABEL) |
0761368a | 204 | || (proto == RTPROT_RIP) || (proto == RTPROT_SHARP) |
31f937fb SM |
205 | || (proto == RTPROT_PBR) || (proto == RTPROT_OPENFABRIC) |
206 | || (proto == RTPROT_SRTE)) { | |
6a6d11a3 | 207 | return true; |
d62a17ae | 208 | } |
209 | ||
6a6d11a3 | 210 | return false; |
23b1f334 DD |
211 | } |
212 | ||
915902cb | 213 | static inline int zebra2proto(int proto) |
23b1f334 | 214 | { |
d62a17ae | 215 | switch (proto) { |
216 | case ZEBRA_ROUTE_BABEL: | |
217 | proto = RTPROT_BABEL; | |
218 | break; | |
219 | case ZEBRA_ROUTE_BGP: | |
220 | proto = RTPROT_BGP; | |
221 | break; | |
222 | case ZEBRA_ROUTE_OSPF: | |
223 | case ZEBRA_ROUTE_OSPF6: | |
224 | proto = RTPROT_OSPF; | |
225 | break; | |
226 | case ZEBRA_ROUTE_STATIC: | |
d4d71f11 | 227 | proto = RTPROT_ZSTATIC; |
d62a17ae | 228 | break; |
229 | case ZEBRA_ROUTE_ISIS: | |
230 | proto = RTPROT_ISIS; | |
231 | break; | |
232 | case ZEBRA_ROUTE_RIP: | |
233 | proto = RTPROT_RIP; | |
234 | break; | |
235 | case ZEBRA_ROUTE_RIPNG: | |
236 | proto = RTPROT_RIPNG; | |
237 | break; | |
238 | case ZEBRA_ROUTE_NHRP: | |
239 | proto = RTPROT_NHRP; | |
240 | break; | |
241 | case ZEBRA_ROUTE_EIGRP: | |
242 | proto = RTPROT_EIGRP; | |
243 | break; | |
244 | case ZEBRA_ROUTE_LDP: | |
245 | proto = RTPROT_LDP; | |
246 | break; | |
8a71d93d DS |
247 | case ZEBRA_ROUTE_SHARP: |
248 | proto = RTPROT_SHARP; | |
249 | break; | |
0761368a DS |
250 | case ZEBRA_ROUTE_PBR: |
251 | proto = RTPROT_PBR; | |
252 | break; | |
da82f6b4 CF |
253 | case ZEBRA_ROUTE_OPENFABRIC: |
254 | proto = RTPROT_OPENFABRIC; | |
255 | break; | |
31f937fb SM |
256 | case ZEBRA_ROUTE_SRTE: |
257 | proto = RTPROT_SRTE; | |
258 | break; | |
a56ec5c0 | 259 | case ZEBRA_ROUTE_TABLE: |
38e40db1 | 260 | case ZEBRA_ROUTE_NHG: |
0a27a2fe | 261 | case ZEBRA_ROUTE_NEIGH: |
a56ec5c0 DS |
262 | proto = RTPROT_ZEBRA; |
263 | break; | |
911d4d48 DE |
264 | case ZEBRA_ROUTE_CONNECT: |
265 | case ZEBRA_ROUTE_KERNEL: | |
266 | proto = RTPROT_KERNEL; | |
267 | break; | |
d62a17ae | 268 | default: |
0761368a DS |
269 | /* |
270 | * When a user adds a new protocol this will show up | |
271 | * to let them know to do something about it. This | |
272 | * is intentionally a warn because we should see | |
273 | * this as part of development of a new protocol | |
274 | */ | |
9df414fe QY |
275 | zlog_debug( |
276 | "%s: Please add this protocol(%d) to proper rt_netlink.c handling", | |
15569c58 | 277 | __func__, proto); |
d62a17ae | 278 | proto = RTPROT_ZEBRA; |
279 | break; | |
280 | } | |
281 | ||
282 | return proto; | |
23b1f334 DD |
283 | } |
284 | ||
38e40db1 | 285 | static inline int proto2zebra(int proto, int family, bool is_nexthop) |
915902cb DS |
286 | { |
287 | switch (proto) { | |
288 | case RTPROT_BABEL: | |
289 | proto = ZEBRA_ROUTE_BABEL; | |
290 | break; | |
291 | case RTPROT_BGP: | |
292 | proto = ZEBRA_ROUTE_BGP; | |
293 | break; | |
294 | case RTPROT_OSPF: | |
d6816f68 DS |
295 | proto = (family == AF_INET) ? ZEBRA_ROUTE_OSPF |
296 | : ZEBRA_ROUTE_OSPF6; | |
915902cb DS |
297 | break; |
298 | case RTPROT_ISIS: | |
299 | proto = ZEBRA_ROUTE_ISIS; | |
300 | break; | |
301 | case RTPROT_RIP: | |
302 | proto = ZEBRA_ROUTE_RIP; | |
303 | break; | |
304 | case RTPROT_RIPNG: | |
305 | proto = ZEBRA_ROUTE_RIPNG; | |
306 | break; | |
307 | case RTPROT_NHRP: | |
308 | proto = ZEBRA_ROUTE_NHRP; | |
309 | break; | |
310 | case RTPROT_EIGRP: | |
311 | proto = ZEBRA_ROUTE_EIGRP; | |
312 | break; | |
313 | case RTPROT_LDP: | |
314 | proto = ZEBRA_ROUTE_LDP; | |
315 | break; | |
316 | case RTPROT_STATIC: | |
d4d71f11 | 317 | case RTPROT_ZSTATIC: |
915902cb DS |
318 | proto = ZEBRA_ROUTE_STATIC; |
319 | break; | |
0761368a DS |
320 | case RTPROT_SHARP: |
321 | proto = ZEBRA_ROUTE_SHARP; | |
322 | break; | |
323 | case RTPROT_PBR: | |
324 | proto = ZEBRA_ROUTE_PBR; | |
325 | break; | |
da82f6b4 CF |
326 | case RTPROT_OPENFABRIC: |
327 | proto = ZEBRA_ROUTE_OPENFABRIC; | |
328 | break; | |
31f937fb SM |
329 | case RTPROT_SRTE: |
330 | proto = ZEBRA_ROUTE_SRTE; | |
331 | break; | |
38e40db1 SW |
332 | case RTPROT_ZEBRA: |
333 | if (is_nexthop) { | |
334 | proto = ZEBRA_ROUTE_NHG; | |
335 | break; | |
336 | } | |
337 | /* Intentional fall thru */ | |
915902cb | 338 | default: |
0761368a DS |
339 | /* |
340 | * When a user adds a new protocol this will show up | |
341 | * to let them know to do something about it. This | |
342 | * is intentionally a warn because we should see | |
343 | * this as part of development of a new protocol | |
344 | */ | |
9df414fe QY |
345 | zlog_debug( |
346 | "%s: Please add this protocol(%d) to proper rt_netlink.c handling", | |
15569c58 | 347 | __func__, proto); |
915902cb DS |
348 | proto = ZEBRA_ROUTE_KERNEL; |
349 | break; | |
350 | } | |
351 | return proto; | |
352 | } | |
353 | ||
12f6fb97 DS |
354 | /* |
355 | Pending: create an efficient table_id (in a tree/hash) based lookup) | |
356 | */ | |
9d866c07 | 357 | vrf_id_t vrf_lookup_by_table(uint32_t table_id, ns_id_t ns_id) |
12f6fb97 | 358 | { |
d62a17ae | 359 | struct vrf *vrf; |
360 | struct zebra_vrf *zvrf; | |
12f6fb97 | 361 | |
a2addae8 | 362 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
78dd30b2 PG |
363 | zvrf = vrf->info; |
364 | if (zvrf == NULL) | |
d62a17ae | 365 | continue; |
78dd30b2 PG |
366 | /* case vrf with netns : match the netnsid */ |
367 | if (vrf_is_backend_netns()) { | |
368 | if (ns_id == zvrf_id(zvrf)) | |
369 | return zvrf_id(zvrf); | |
370 | } else { | |
371 | /* VRF is VRF_BACKEND_VRF_LITE */ | |
372 | if (zvrf->table_id != table_id) | |
373 | continue; | |
374 | return zvrf_id(zvrf); | |
375 | } | |
d62a17ae | 376 | } |
12f6fb97 | 377 | |
d62a17ae | 378 | return VRF_DEFAULT; |
12f6fb97 DS |
379 | } |
380 | ||
87da6a60 SW |
381 | /** |
382 | * @parse_encap_mpls() - Parses encapsulated mpls attributes | |
383 | * @tb: Pointer to rtattr to look for nested items in. | |
384 | * @labels: Pointer to store labels in. | |
385 | * | |
386 | * Return: Number of mpls labels found. | |
387 | */ | |
388 | static int parse_encap_mpls(struct rtattr *tb, mpls_label_t *labels) | |
389 | { | |
390 | struct rtattr *tb_encap[MPLS_IPTUNNEL_MAX + 1] = {0}; | |
391 | mpls_lse_t *lses = NULL; | |
392 | int num_labels = 0; | |
393 | uint32_t ttl = 0; | |
394 | uint32_t bos = 0; | |
395 | uint32_t exp = 0; | |
396 | mpls_label_t label = 0; | |
397 | ||
398 | netlink_parse_rtattr_nested(tb_encap, MPLS_IPTUNNEL_MAX, tb); | |
399 | lses = (mpls_lse_t *)RTA_DATA(tb_encap[MPLS_IPTUNNEL_DST]); | |
400 | while (!bos && num_labels < MPLS_MAX_LABELS) { | |
401 | mpls_lse_decode(lses[num_labels], &label, &ttl, &exp, &bos); | |
402 | labels[num_labels++] = label; | |
403 | } | |
404 | ||
405 | return num_labels; | |
406 | } | |
407 | ||
77a44d94 SW |
408 | static struct nexthop |
409 | parse_nexthop_unicast(ns_id_t ns_id, struct rtmsg *rtm, struct rtattr **tb, | |
410 | enum blackhole_type bh_type, int index, void *prefsrc, | |
20822f9d | 411 | void *gate, afi_t afi, vrf_id_t vrf_id) |
77a44d94 SW |
412 | { |
413 | struct interface *ifp = NULL; | |
414 | struct nexthop nh = {0}; | |
415 | mpls_label_t labels[MPLS_MAX_LABELS] = {0}; | |
416 | int num_labels = 0; | |
417 | ||
20822f9d | 418 | vrf_id_t nh_vrf_id = vrf_id; |
77a44d94 SW |
419 | size_t sz = (afi == AFI_IP) ? 4 : 16; |
420 | ||
421 | if (bh_type == BLACKHOLE_UNSPEC) { | |
422 | if (index && !gate) | |
423 | nh.type = NEXTHOP_TYPE_IFINDEX; | |
424 | else if (index && gate) | |
425 | nh.type = (afi == AFI_IP) ? NEXTHOP_TYPE_IPV4_IFINDEX | |
426 | : NEXTHOP_TYPE_IPV6_IFINDEX; | |
427 | else if (!index && gate) | |
428 | nh.type = (afi == AFI_IP) ? NEXTHOP_TYPE_IPV4 | |
429 | : NEXTHOP_TYPE_IPV6; | |
430 | else { | |
431 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
432 | nh.bh_type = bh_type; | |
433 | } | |
434 | } else { | |
435 | nh.type = NEXTHOP_TYPE_BLACKHOLE; | |
436 | nh.bh_type = bh_type; | |
437 | } | |
438 | nh.ifindex = index; | |
439 | if (prefsrc) | |
440 | memcpy(&nh.src, prefsrc, sz); | |
441 | if (gate) | |
442 | memcpy(&nh.gate, gate, sz); | |
443 | ||
444 | if (index) { | |
445 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), index); | |
446 | if (ifp) | |
447 | nh_vrf_id = ifp->vrf_id; | |
448 | } | |
449 | nh.vrf_id = nh_vrf_id; | |
450 | ||
451 | if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE] | |
452 | && *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE]) | |
453 | == LWTUNNEL_ENCAP_MPLS) { | |
454 | num_labels = parse_encap_mpls(tb[RTA_ENCAP], labels); | |
455 | } | |
456 | ||
457 | if (rtm->rtm_flags & RTNH_F_ONLINK) | |
458 | SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK); | |
459 | ||
460 | if (num_labels) | |
461 | nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels, labels); | |
462 | ||
463 | return nh; | |
464 | } | |
465 | ||
20822f9d | 466 | static uint8_t parse_multipath_nexthops_unicast(ns_id_t ns_id, |
0eb97b86 | 467 | struct nexthop_group *ng, |
20822f9d SW |
468 | struct rtmsg *rtm, |
469 | struct rtnexthop *rtnh, | |
470 | struct rtattr **tb, | |
471 | void *prefsrc, vrf_id_t vrf_id) | |
472 | { | |
473 | void *gate = NULL; | |
474 | struct interface *ifp = NULL; | |
475 | int index = 0; | |
476 | /* MPLS labels */ | |
477 | mpls_label_t labels[MPLS_MAX_LABELS] = {0}; | |
478 | int num_labels = 0; | |
479 | struct rtattr *rtnh_tb[RTA_MAX + 1] = {}; | |
480 | ||
481 | int len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); | |
482 | vrf_id_t nh_vrf_id = vrf_id; | |
483 | ||
20822f9d SW |
484 | for (;;) { |
485 | struct nexthop *nh = NULL; | |
486 | ||
487 | if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len) | |
488 | break; | |
489 | ||
490 | index = rtnh->rtnh_ifindex; | |
491 | if (index) { | |
492 | /* | |
493 | * Yes we are looking this up | |
494 | * for every nexthop and just | |
495 | * using the last one looked | |
496 | * up right now | |
497 | */ | |
498 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), | |
499 | index); | |
500 | if (ifp) | |
501 | nh_vrf_id = ifp->vrf_id; | |
502 | else { | |
503 | flog_warn( | |
504 | EC_ZEBRA_UNKNOWN_INTERFACE, | |
505 | "%s: Unknown interface %u specified, defaulting to VRF_DEFAULT", | |
15569c58 | 506 | __func__, index); |
20822f9d SW |
507 | nh_vrf_id = VRF_DEFAULT; |
508 | } | |
509 | } else | |
510 | nh_vrf_id = vrf_id; | |
511 | ||
512 | if (rtnh->rtnh_len > sizeof(*rtnh)) { | |
513 | memset(rtnh_tb, 0, sizeof(rtnh_tb)); | |
514 | ||
515 | netlink_parse_rtattr(rtnh_tb, RTA_MAX, RTNH_DATA(rtnh), | |
516 | rtnh->rtnh_len - sizeof(*rtnh)); | |
517 | if (rtnh_tb[RTA_GATEWAY]) | |
518 | gate = RTA_DATA(rtnh_tb[RTA_GATEWAY]); | |
519 | if (rtnh_tb[RTA_ENCAP] && rtnh_tb[RTA_ENCAP_TYPE] | |
520 | && *(uint16_t *)RTA_DATA(rtnh_tb[RTA_ENCAP_TYPE]) | |
521 | == LWTUNNEL_ENCAP_MPLS) { | |
522 | num_labels = parse_encap_mpls( | |
523 | rtnh_tb[RTA_ENCAP], labels); | |
524 | } | |
525 | } | |
526 | ||
f3354e16 SW |
527 | if (gate && rtm->rtm_family == AF_INET) { |
528 | if (index) | |
0eb97b86 MS |
529 | nh = nexthop_from_ipv4_ifindex( |
530 | gate, prefsrc, index, nh_vrf_id); | |
f3354e16 | 531 | else |
0eb97b86 MS |
532 | nh = nexthop_from_ipv4(gate, prefsrc, |
533 | nh_vrf_id); | |
f3354e16 SW |
534 | } else if (gate && rtm->rtm_family == AF_INET6) { |
535 | if (index) | |
0eb97b86 MS |
536 | nh = nexthop_from_ipv6_ifindex( |
537 | gate, index, nh_vrf_id); | |
f3354e16 | 538 | else |
0eb97b86 | 539 | nh = nexthop_from_ipv6(gate, nh_vrf_id); |
20822f9d | 540 | } else |
0eb97b86 | 541 | nh = nexthop_from_ifindex(index, nh_vrf_id); |
20822f9d SW |
542 | |
543 | if (nh) { | |
df7fb580 DS |
544 | nh->weight = rtnh->rtnh_hops + 1; |
545 | ||
20822f9d SW |
546 | if (num_labels) |
547 | nexthop_add_labels(nh, ZEBRA_LSP_STATIC, | |
548 | num_labels, labels); | |
549 | ||
550 | if (rtnh->rtnh_flags & RTNH_F_ONLINK) | |
551 | SET_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK); | |
0eb97b86 MS |
552 | |
553 | /* Add to temporary list */ | |
554 | nexthop_group_add_sorted(ng, nh); | |
20822f9d SW |
555 | } |
556 | ||
557 | if (rtnh->rtnh_len == 0) | |
558 | break; | |
559 | ||
560 | len -= NLMSG_ALIGN(rtnh->rtnh_len); | |
561 | rtnh = RTNH_NEXT(rtnh); | |
562 | } | |
563 | ||
0eb97b86 | 564 | uint8_t nhop_num = nexthop_group_nexthop_num(ng); |
20822f9d SW |
565 | |
566 | return nhop_num; | |
567 | } | |
568 | ||
718e3744 | 569 | /* Looking up routing table by netlink interface. */ |
2414abd3 | 570 | static int netlink_route_change_read_unicast(struct nlmsghdr *h, ns_id_t ns_id, |
d62a17ae | 571 | int startup) |
718e3744 | 572 | { |
d62a17ae | 573 | int len; |
574 | struct rtmsg *rtm; | |
575 | struct rtattr *tb[RTA_MAX + 1]; | |
acde7f6b | 576 | uint32_t flags = 0; |
d62a17ae | 577 | struct prefix p; |
792fa92e | 578 | struct prefix_ipv6 src_p = {}; |
78dd30b2 | 579 | vrf_id_t vrf_id; |
6a6d11a3 | 580 | bool selfroute; |
d62a17ae | 581 | |
582 | char anyaddr[16] = {0}; | |
583 | ||
915902cb | 584 | int proto = ZEBRA_ROUTE_KERNEL; |
d62a17ae | 585 | int index = 0; |
586 | int table; | |
587 | int metric = 0; | |
d7c0a89a | 588 | uint32_t mtu = 0; |
25715c7e | 589 | uint8_t distance = 0; |
4e40b6d6 | 590 | route_tag_t tag = 0; |
fcc89a9c | 591 | uint32_t nhe_id = 0; |
d62a17ae | 592 | |
593 | void *dest = NULL; | |
594 | void *gate = NULL; | |
595 | void *prefsrc = NULL; /* IPv4 preferred source host address */ | |
596 | void *src = NULL; /* IPv6 srcdest source prefix */ | |
e655a03c | 597 | enum blackhole_type bh_type = BLACKHOLE_UNSPEC; |
d62a17ae | 598 | |
599 | rtm = NLMSG_DATA(h); | |
600 | ||
601 | if (startup && h->nlmsg_type != RTM_NEWROUTE) | |
602 | return 0; | |
e655a03c DL |
603 | switch (rtm->rtm_type) { |
604 | case RTN_UNICAST: | |
605 | break; | |
606 | case RTN_BLACKHOLE: | |
607 | bh_type = BLACKHOLE_NULL; | |
608 | break; | |
609 | case RTN_UNREACHABLE: | |
610 | bh_type = BLACKHOLE_REJECT; | |
611 | break; | |
612 | case RTN_PROHIBIT: | |
613 | bh_type = BLACKHOLE_ADMINPROHIB; | |
614 | break; | |
615 | default: | |
8c8f250b DS |
616 | if (IS_ZEBRA_DEBUG_KERNEL) |
617 | zlog_debug("Route rtm_type: %s(%d) intentionally ignoring", | |
618 | nl_rttype_to_str(rtm->rtm_type), | |
619 | rtm->rtm_type); | |
d62a17ae | 620 | return 0; |
e655a03c | 621 | } |
d62a17ae | 622 | |
623 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
9bdf8618 | 624 | if (len < 0) { |
15569c58 DA |
625 | zlog_err( |
626 | "%s: Message received from netlink is of a broken size %d %zu", | |
627 | __func__, h->nlmsg_len, | |
628 | (size_t)NLMSG_LENGTH(sizeof(struct rtmsg))); | |
d62a17ae | 629 | return -1; |
9bdf8618 | 630 | } |
d62a17ae | 631 | |
0d6f7fd6 | 632 | memset(tb, 0, sizeof(tb)); |
d62a17ae | 633 | netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len); |
634 | ||
635 | if (rtm->rtm_flags & RTM_F_CLONED) | |
636 | return 0; | |
637 | if (rtm->rtm_protocol == RTPROT_REDIRECT) | |
638 | return 0; | |
639 | if (rtm->rtm_protocol == RTPROT_KERNEL) | |
640 | return 0; | |
641 | ||
6a6d11a3 NN |
642 | selfroute = is_selfroute(rtm->rtm_protocol); |
643 | ||
e4876266 DS |
644 | if (!startup && selfroute |
645 | && h->nlmsg_type == RTM_NEWROUTE | |
646 | && !zrouter.asic_offloaded) { | |
6ab5222f DS |
647 | if (IS_ZEBRA_DEBUG_KERNEL) |
648 | zlog_debug("Route type: %d Received that we think we have originated, ignoring", | |
649 | rtm->rtm_protocol); | |
d62a17ae | 650 | return 0; |
6ab5222f | 651 | } |
d62a17ae | 652 | |
653 | /* We don't care about change notifications for the MPLS table. */ | |
654 | /* TODO: Revisit this. */ | |
655 | if (rtm->rtm_family == AF_MPLS) | |
656 | return 0; | |
657 | ||
658 | /* Table corresponding to route. */ | |
659 | if (tb[RTA_TABLE]) | |
660 | table = *(int *)RTA_DATA(tb[RTA_TABLE]); | |
661 | else | |
662 | table = rtm->rtm_table; | |
663 | ||
664 | /* Map to VRF */ | |
78dd30b2 | 665 | vrf_id = vrf_lookup_by_table(table, ns_id); |
d62a17ae | 666 | if (vrf_id == VRF_DEFAULT) { |
667 | if (!is_zebra_valid_kernel_table(table) | |
668 | && !is_zebra_main_routing_table(table)) | |
669 | return 0; | |
670 | } | |
671 | ||
5a3cf853 DS |
672 | if (rtm->rtm_flags & RTM_F_TRAP) |
673 | flags |= ZEBRA_FLAG_TRAPPED; | |
674 | if (rtm->rtm_flags & RTM_F_OFFLOAD) | |
675 | flags |= ZEBRA_FLAG_OFFLOADED; | |
0d32fbee DS |
676 | if (rtm->rtm_flags & RTM_F_OFFLOAD_FAILED) |
677 | flags |= ZEBRA_FLAG_OFFLOAD_FAILED; | |
5a3cf853 | 678 | |
d62a17ae | 679 | /* Route which inserted by Zebra. */ |
6a6d11a3 | 680 | if (selfroute) { |
d62a17ae | 681 | flags |= ZEBRA_FLAG_SELFROUTE; |
38e40db1 | 682 | proto = proto2zebra(rtm->rtm_protocol, rtm->rtm_family, false); |
915902cb | 683 | } |
d62a17ae | 684 | if (tb[RTA_OIF]) |
685 | index = *(int *)RTA_DATA(tb[RTA_OIF]); | |
686 | ||
687 | if (tb[RTA_DST]) | |
688 | dest = RTA_DATA(tb[RTA_DST]); | |
689 | else | |
690 | dest = anyaddr; | |
691 | ||
692 | if (tb[RTA_SRC]) | |
693 | src = RTA_DATA(tb[RTA_SRC]); | |
694 | else | |
695 | src = anyaddr; | |
696 | ||
697 | if (tb[RTA_PREFSRC]) | |
698 | prefsrc = RTA_DATA(tb[RTA_PREFSRC]); | |
699 | ||
700 | if (tb[RTA_GATEWAY]) | |
701 | gate = RTA_DATA(tb[RTA_GATEWAY]); | |
702 | ||
fcc89a9c SW |
703 | if (tb[RTA_NH_ID]) |
704 | nhe_id = *(uint32_t *)RTA_DATA(tb[RTA_NH_ID]); | |
705 | ||
f19435a8 DS |
706 | if (tb[RTA_PRIORITY]) |
707 | metric = *(int *)RTA_DATA(tb[RTA_PRIORITY]); | |
d62a17ae | 708 | |
4e40b6d6 KK |
709 | #if defined(SUPPORT_REALMS) |
710 | if (tb[RTA_FLOW]) | |
711 | tag = *(uint32_t *)RTA_DATA(tb[RTA_FLOW]); | |
712 | #endif | |
713 | ||
f19435a8 DS |
714 | if (tb[RTA_METRICS]) { |
715 | struct rtattr *mxrta[RTAX_MAX + 1]; | |
d62a17ae | 716 | |
0d6f7fd6 | 717 | memset(mxrta, 0, sizeof(mxrta)); |
996c9314 | 718 | netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), |
f19435a8 | 719 | RTA_PAYLOAD(tb[RTA_METRICS])); |
d62a17ae | 720 | |
f19435a8 | 721 | if (mxrta[RTAX_MTU]) |
d7c0a89a | 722 | mtu = *(uint32_t *)RTA_DATA(mxrta[RTAX_MTU]); |
d62a17ae | 723 | } |
724 | ||
725 | if (rtm->rtm_family == AF_INET) { | |
726 | p.family = AF_INET; | |
930571d2 | 727 | if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) { |
e17d9b2d | 728 | zlog_err( |
75829703 | 729 | "Invalid destination prefix length: %u received from kernel route change", |
930571d2 | 730 | rtm->rtm_dst_len); |
e17d9b2d | 731 | return -1; |
930571d2 | 732 | } |
d62a17ae | 733 | memcpy(&p.u.prefix4, dest, 4); |
734 | p.prefixlen = rtm->rtm_dst_len; | |
735 | ||
1f610a1f | 736 | if (rtm->rtm_src_len != 0) { |
9df414fe | 737 | flog_warn( |
e914ccbe | 738 | EC_ZEBRA_UNSUPPORTED_V4_SRCDEST, |
2dbe669b DA |
739 | "unsupported IPv4 sourcedest route (dest %pFX vrf %u)", |
740 | &p, vrf_id); | |
1f610a1f CF |
741 | return 0; |
742 | } | |
930571d2 | 743 | |
1f610a1f CF |
744 | /* Force debug below to not display anything for source */ |
745 | src_p.prefixlen = 0; | |
d62a17ae | 746 | } else if (rtm->rtm_family == AF_INET6) { |
747 | p.family = AF_INET6; | |
930571d2 | 748 | if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) { |
e17d9b2d | 749 | zlog_err( |
75829703 | 750 | "Invalid destination prefix length: %u received from kernel route change", |
930571d2 | 751 | rtm->rtm_dst_len); |
e17d9b2d | 752 | return -1; |
930571d2 | 753 | } |
d62a17ae | 754 | memcpy(&p.u.prefix6, dest, 16); |
755 | p.prefixlen = rtm->rtm_dst_len; | |
756 | ||
757 | src_p.family = AF_INET6; | |
930571d2 | 758 | if (rtm->rtm_src_len > IPV6_MAX_BITLEN) { |
e17d9b2d | 759 | zlog_err( |
75829703 | 760 | "Invalid source prefix length: %u received from kernel route change", |
930571d2 | 761 | rtm->rtm_src_len); |
e17d9b2d | 762 | return -1; |
930571d2 | 763 | } |
d62a17ae | 764 | memcpy(&src_p.prefix, src, 16); |
765 | src_p.prefixlen = rtm->rtm_src_len; | |
766 | } | |
767 | ||
25715c7e DS |
768 | /* |
769 | * For ZEBRA_ROUTE_KERNEL types: | |
770 | * | |
771 | * The metric/priority of the route received from the kernel | |
772 | * is a 32 bit number. We are going to interpret the high | |
773 | * order byte as the Admin Distance and the low order 3 bytes | |
774 | * as the metric. | |
775 | * | |
776 | * This will allow us to do two things: | |
777 | * 1) Allow the creation of kernel routes that can be | |
778 | * overridden by zebra. | |
779 | * 2) Allow the old behavior for 'most' kernel route types | |
780 | * if a user enters 'ip route ...' v4 routes get a metric | |
781 | * of 0 and v6 routes get a metric of 1024. Both of these | |
782 | * values will end up with a admin distance of 0, which | |
783 | * will cause them to win for the purposes of zebra. | |
784 | */ | |
785 | if (proto == ZEBRA_ROUTE_KERNEL) { | |
786 | distance = (metric >> 24) & 0xFF; | |
996c9314 | 787 | metric = (metric & 0x00FFFFFF); |
25715c7e DS |
788 | } |
789 | ||
d62a17ae | 790 | if (IS_ZEBRA_DEBUG_KERNEL) { |
d62a17ae | 791 | char buf2[PREFIX_STRLEN]; |
2dbe669b | 792 | |
bd47f3a3 | 793 | zlog_debug( |
2dbe669b DA |
794 | "%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d", |
795 | nl_msg_type_to_str(h->nlmsg_type), &p, | |
bd47f3a3 JU |
796 | src_p.prefixlen ? " from " : "", |
797 | src_p.prefixlen ? prefix2str(&src_p, buf2, sizeof(buf2)) | |
798 | : "", | |
799 | vrf_id_to_name(vrf_id), vrf_id, table, metric, | |
800 | distance); | |
d62a17ae | 801 | } |
802 | ||
803 | afi_t afi = AFI_IP; | |
804 | if (rtm->rtm_family == AF_INET6) | |
805 | afi = AFI_IP6; | |
806 | ||
807 | if (h->nlmsg_type == RTM_NEWROUTE) { | |
8795f904 | 808 | |
fd36be7e | 809 | if (!tb[RTA_MULTIPATH]) { |
77a44d94 | 810 | struct nexthop nh = {0}; |
8795f904 | 811 | |
77a44d94 SW |
812 | if (!nhe_id) { |
813 | nh = parse_nexthop_unicast( | |
814 | ns_id, rtm, tb, bh_type, index, prefsrc, | |
20822f9d | 815 | gate, afi, vrf_id); |
87da6a60 | 816 | } |
4a7371e9 | 817 | rib_add(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, &p, |
8032b717 SW |
818 | &src_p, &nh, nhe_id, table, metric, mtu, |
819 | distance, tag); | |
fd36be7e | 820 | } else { |
d62a17ae | 821 | /* This is a multipath route */ |
d62a17ae | 822 | struct route_entry *re; |
0eb97b86 | 823 | struct nexthop_group *ng = NULL; |
d62a17ae | 824 | struct rtnexthop *rtnh = |
825 | (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]); | |
d62a17ae | 826 | |
827 | re = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); | |
915902cb | 828 | re->type = proto; |
25715c7e | 829 | re->distance = distance; |
d62a17ae | 830 | re->flags = flags; |
831 | re->metric = metric; | |
832 | re->mtu = mtu; | |
833 | re->vrf_id = vrf_id; | |
834 | re->table = table; | |
98572489 | 835 | re->uptime = monotime(NULL); |
4e40b6d6 | 836 | re->tag = tag; |
bbb322f2 | 837 | re->nhe_id = nhe_id; |
3c04071d | 838 | |
20822f9d | 839 | if (!nhe_id) { |
0eb97b86 MS |
840 | uint8_t nhop_num; |
841 | ||
842 | /* Use temporary list of nexthops; parse | |
843 | * message payload's nexthops. | |
844 | */ | |
845 | ng = nexthop_group_new(); | |
846 | nhop_num = | |
20822f9d | 847 | parse_multipath_nexthops_unicast( |
0eb97b86 | 848 | ns_id, ng, rtm, rtnh, tb, |
20822f9d SW |
849 | prefsrc, vrf_id); |
850 | ||
851 | zserv_nexthop_num_warn( | |
852 | __func__, (const struct prefix *)&p, | |
853 | nhop_num); | |
0eb97b86 MS |
854 | |
855 | if (nhop_num == 0) { | |
856 | nexthop_group_delete(&ng); | |
857 | ng = NULL; | |
858 | } | |
d62a17ae | 859 | } |
860 | ||
0eb97b86 | 861 | if (nhe_id || ng) |
1f610a1f | 862 | rib_add_multipath(afi, SAFI_UNICAST, &p, |
0eb97b86 | 863 | &src_p, re, ng); |
20822f9d SW |
864 | else |
865 | XFREE(MTYPE_RE, re); | |
d62a17ae | 866 | } |
867 | } else { | |
bc541126 SW |
868 | if (nhe_id) { |
869 | rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, flags, | |
870 | &p, &src_p, NULL, nhe_id, table, metric, | |
3ceae22b | 871 | distance, true); |
bc541126 SW |
872 | } else { |
873 | if (!tb[RTA_MULTIPATH]) { | |
874 | struct nexthop nh; | |
760f39dc HS |
875 | |
876 | nh = parse_nexthop_unicast( | |
877 | ns_id, rtm, tb, bh_type, index, prefsrc, | |
878 | gate, afi, vrf_id); | |
bc541126 SW |
879 | rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, |
880 | flags, &p, &src_p, &nh, 0, table, | |
3ceae22b | 881 | metric, distance, true); |
8ba5bd58 | 882 | } else { |
bc541126 SW |
883 | /* XXX: need to compare the entire list of |
884 | * nexthops here for NLM_F_APPEND stupidity */ | |
885 | rib_delete(afi, SAFI_UNICAST, vrf_id, proto, 0, | |
886 | flags, &p, &src_p, NULL, 0, table, | |
3ceae22b | 887 | metric, distance, true); |
8ba5bd58 | 888 | } |
d62a17ae | 889 | } |
890 | } | |
891 | ||
892 | return 0; | |
718e3744 | 893 | } |
894 | ||
e3be0432 DS |
895 | static struct mcast_route_data *mroute = NULL; |
896 | ||
2414abd3 | 897 | static int netlink_route_change_read_multicast(struct nlmsghdr *h, |
d62a17ae | 898 | ns_id_t ns_id, int startup) |
565fdc75 | 899 | { |
d62a17ae | 900 | int len; |
901 | struct rtmsg *rtm; | |
902 | struct rtattr *tb[RTA_MAX + 1]; | |
903 | struct mcast_route_data *m; | |
904 | struct mcast_route_data mr; | |
905 | int iif = 0; | |
906 | int count; | |
907 | int oif[256]; | |
908 | int oif_count = 0; | |
d62a17ae | 909 | char oif_list[256] = "\0"; |
78dd30b2 | 910 | vrf_id_t vrf; |
43b5cc5e | 911 | int table; |
d62a17ae | 912 | |
913 | if (mroute) | |
914 | m = mroute; | |
915 | else { | |
916 | memset(&mr, 0, sizeof(mr)); | |
917 | m = &mr; | |
918 | } | |
919 | ||
920 | rtm = NLMSG_DATA(h); | |
921 | ||
922 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
923 | ||
0d6f7fd6 | 924 | memset(tb, 0, sizeof(tb)); |
d62a17ae | 925 | netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len); |
90d82769 | 926 | |
43b5cc5e DS |
927 | if (tb[RTA_TABLE]) |
928 | table = *(int *)RTA_DATA(tb[RTA_TABLE]); | |
929 | else | |
930 | table = rtm->rtm_table; | |
931 | ||
78dd30b2 | 932 | vrf = vrf_lookup_by_table(table, ns_id); |
43b5cc5e | 933 | |
d62a17ae | 934 | if (tb[RTA_IIF]) |
935 | iif = *(int *)RTA_DATA(tb[RTA_IIF]); | |
936 | ||
937 | if (tb[RTA_SRC]) | |
bd8b9272 | 938 | m->sg.src = *(struct in_addr *)RTA_DATA(tb[RTA_SRC]); |
d62a17ae | 939 | |
940 | if (tb[RTA_DST]) | |
bd8b9272 | 941 | m->sg.grp = *(struct in_addr *)RTA_DATA(tb[RTA_DST]); |
d62a17ae | 942 | |
62819462 | 943 | if (tb[RTA_EXPIRES]) |
d62a17ae | 944 | m->lastused = *(unsigned long long *)RTA_DATA(tb[RTA_EXPIRES]); |
945 | ||
946 | if (tb[RTA_MULTIPATH]) { | |
947 | struct rtnexthop *rtnh = | |
948 | (struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]); | |
949 | ||
950 | len = RTA_PAYLOAD(tb[RTA_MULTIPATH]); | |
951 | for (;;) { | |
952 | if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len) | |
953 | break; | |
954 | ||
955 | oif[oif_count] = rtnh->rtnh_ifindex; | |
956 | oif_count++; | |
957 | ||
3c04071d SW |
958 | if (rtnh->rtnh_len == 0) |
959 | break; | |
960 | ||
d62a17ae | 961 | len -= NLMSG_ALIGN(rtnh->rtnh_len); |
962 | rtnh = RTNH_NEXT(rtnh); | |
963 | } | |
964 | } | |
965 | ||
966 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
822c9af2 SW |
967 | struct interface *ifp = NULL; |
968 | struct zebra_vrf *zvrf = NULL; | |
969 | ||
d62a17ae | 970 | for (count = 0; count < oif_count; count++) { |
971 | ifp = if_lookup_by_index(oif[count], vrf); | |
972 | char temp[256]; | |
973 | ||
772270f3 QY |
974 | snprintf(temp, sizeof(temp), "%s(%d) ", |
975 | ifp ? ifp->name : "Unknown", oif[count]); | |
eab4a5c2 | 976 | strlcat(oif_list, temp, sizeof(oif_list)); |
d62a17ae | 977 | } |
822c9af2 | 978 | zvrf = zebra_vrf_lookup_by_id(vrf); |
d62a17ae | 979 | ifp = if_lookup_by_index(iif, vrf); |
822c9af2 | 980 | zlog_debug( |
9bcef951 | 981 | "MCAST VRF: %s(%d) %s (%pI4,%pI4) IIF: %s(%d) OIF: %s jiffies: %lld", |
bd47f3a3 | 982 | zvrf_name(zvrf), vrf, nl_msg_type_to_str(h->nlmsg_type), |
9bcef951 MS |
983 | &m->sg.src, &m->sg.grp, ifp ? ifp->name : "Unknown", |
984 | iif, oif_list, | |
822c9af2 | 985 | m->lastused); |
90d82769 | 986 | } |
d62a17ae | 987 | return 0; |
565fdc75 DS |
988 | } |
989 | ||
2414abd3 | 990 | int netlink_route_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
565fdc75 | 991 | { |
d62a17ae | 992 | int len; |
d62a17ae | 993 | struct rtmsg *rtm; |
994 | ||
995 | rtm = NLMSG_DATA(h); | |
996 | ||
997 | if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) { | |
998 | /* If this is not route add/delete message print warning. */ | |
9165c5f5 | 999 | zlog_debug("Kernel message: %s NS %u", |
87b5d1b0 | 1000 | nl_msg_type_to_str(h->nlmsg_type), ns_id); |
d62a17ae | 1001 | return 0; |
1002 | } | |
1003 | ||
c25e2f1a DS |
1004 | if (!(rtm->rtm_family == AF_INET || |
1005 | rtm->rtm_family == AF_INET6 || | |
1006 | rtm->rtm_family == RTNL_FAMILY_IPMR )) { | |
9df414fe | 1007 | flog_warn( |
e914ccbe | 1008 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
1009 | "Invalid address family: %u received from kernel route change: %s", |
1010 | rtm->rtm_family, nl_msg_type_to_str(h->nlmsg_type)); | |
8a1b681c SW |
1011 | return 0; |
1012 | } | |
1013 | ||
d62a17ae | 1014 | /* Connected route. */ |
1015 | if (IS_ZEBRA_DEBUG_KERNEL) | |
78dd30b2 | 1016 | zlog_debug("%s %s %s proto %s NS %u", |
d62a17ae | 1017 | nl_msg_type_to_str(h->nlmsg_type), |
1018 | nl_family_to_str(rtm->rtm_family), | |
1019 | nl_rttype_to_str(rtm->rtm_type), | |
78dd30b2 | 1020 | nl_rtproto_to_str(rtm->rtm_protocol), ns_id); |
d62a17ae | 1021 | |
d62a17ae | 1022 | |
1023 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
9bdf8618 | 1024 | if (len < 0) { |
15569c58 DA |
1025 | zlog_err( |
1026 | "%s: Message received from netlink is of a broken size: %d %zu", | |
1027 | __func__, h->nlmsg_len, | |
1028 | (size_t)NLMSG_LENGTH(sizeof(struct rtmsg))); | |
d62a17ae | 1029 | return -1; |
9bdf8618 | 1030 | } |
d62a17ae | 1031 | |
e655a03c | 1032 | if (rtm->rtm_type == RTN_MULTICAST) |
2414abd3 | 1033 | netlink_route_change_read_multicast(h, ns_id, startup); |
e655a03c | 1034 | else |
2414abd3 | 1035 | netlink_route_change_read_unicast(h, ns_id, startup); |
d62a17ae | 1036 | return 0; |
565fdc75 DS |
1037 | } |
1038 | ||
289602d7 | 1039 | /* Request for specific route information from the kernel */ |
d62a17ae | 1040 | static int netlink_request_route(struct zebra_ns *zns, int family, int type) |
289602d7 | 1041 | { |
d62a17ae | 1042 | struct { |
1043 | struct nlmsghdr n; | |
1044 | struct rtmsg rtm; | |
1045 | } req; | |
1046 | ||
1047 | /* Form the request, specifying filter (rtattr) if needed. */ | |
1048 | memset(&req, 0, sizeof(req)); | |
1049 | req.n.nlmsg_type = type; | |
718f9b0f | 1050 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
d62a17ae | 1051 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
1052 | req.rtm.rtm_family = family; | |
1053 | ||
fd3f8e52 | 1054 | return netlink_request(&zns->netlink_cmd, &req); |
289602d7 | 1055 | } |
1056 | ||
718e3744 | 1057 | /* Routing table read function using netlink interface. Only called |
1058 | bootstrap time. */ | |
d62a17ae | 1059 | int netlink_route_read(struct zebra_ns *zns) |
718e3744 | 1060 | { |
d62a17ae | 1061 | int ret; |
85a75f1e MS |
1062 | struct zebra_dplane_info dp_info; |
1063 | ||
1064 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
d62a17ae | 1065 | |
1066 | /* Get IPv4 routing table. */ | |
1067 | ret = netlink_request_route(zns, AF_INET, RTM_GETROUTE); | |
1068 | if (ret < 0) | |
1069 | return ret; | |
1070 | ret = netlink_parse_info(netlink_route_change_read_unicast, | |
85a75f1e | 1071 | &zns->netlink_cmd, &dp_info, 0, 1); |
d62a17ae | 1072 | if (ret < 0) |
1073 | return ret; | |
1074 | ||
1075 | /* Get IPv6 routing table. */ | |
1076 | ret = netlink_request_route(zns, AF_INET6, RTM_GETROUTE); | |
1077 | if (ret < 0) | |
1078 | return ret; | |
1079 | ret = netlink_parse_info(netlink_route_change_read_unicast, | |
85a75f1e | 1080 | &zns->netlink_cmd, &dp_info, 0, 1); |
d62a17ae | 1081 | if (ret < 0) |
1082 | return ret; | |
1083 | ||
1084 | return 0; | |
718e3744 | 1085 | } |
1086 | ||
0be6e7d7 JU |
1087 | /* |
1088 | * The function returns true if the gateway info could be added | |
1089 | * to the message, otherwise false is returned. | |
1090 | */ | |
1091 | static bool _netlink_route_add_gateway_info(uint8_t route_family, | |
312a6bee JU |
1092 | uint8_t gw_family, |
1093 | struct nlmsghdr *nlmsg, | |
1094 | size_t req_size, int bytelen, | |
1095 | const struct nexthop *nexthop) | |
40c7bdb0 | 1096 | { |
d62a17ae | 1097 | if (route_family == AF_MPLS) { |
1098 | struct gw_family_t gw_fam; | |
1099 | ||
1100 | gw_fam.family = gw_family; | |
1101 | if (gw_family == AF_INET) | |
1102 | memcpy(&gw_fam.gate.ipv4, &nexthop->gate.ipv4, bytelen); | |
1103 | else | |
1104 | memcpy(&gw_fam.gate.ipv6, &nexthop->gate.ipv6, bytelen); | |
0be6e7d7 JU |
1105 | if (!nl_attr_put(nlmsg, req_size, RTA_VIA, &gw_fam.family, |
1106 | bytelen + 2)) | |
1107 | return false; | |
d62a17ae | 1108 | } else { |
92d6f769 K |
1109 | if (!(nexthop->rparent |
1110 | && IS_MAPPED_IPV6(&nexthop->rparent->gate.ipv6))) { | |
1111 | if (gw_family == AF_INET) { | |
1112 | if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, | |
1113 | &nexthop->gate.ipv4, bytelen)) | |
1114 | return false; | |
1115 | } else { | |
1116 | if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, | |
1117 | &nexthop->gate.ipv6, bytelen)) | |
1118 | return false; | |
1119 | } | |
0be6e7d7 | 1120 | } |
d62a17ae | 1121 | } |
0be6e7d7 JU |
1122 | |
1123 | return true; | |
40c7bdb0 | 1124 | } |
1125 | ||
b7537db6 SW |
1126 | static int build_label_stack(struct mpls_label_stack *nh_label, |
1127 | mpls_lse_t *out_lse, char *label_buf, | |
1128 | size_t label_buf_size) | |
1129 | { | |
1130 | char label_buf1[20]; | |
1131 | int num_labels = 0; | |
1132 | ||
1133 | for (int i = 0; nh_label && i < nh_label->num_labels; i++) { | |
1134 | if (nh_label->label[i] == MPLS_LABEL_IMPLICIT_NULL) | |
1135 | continue; | |
1136 | ||
1137 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
1138 | if (!num_labels) | |
7533cad7 QY |
1139 | snprintf(label_buf, label_buf_size, "label %u", |
1140 | nh_label->label[i]); | |
b7537db6 | 1141 | else { |
772270f3 QY |
1142 | snprintf(label_buf1, sizeof(label_buf1), "/%u", |
1143 | nh_label->label[i]); | |
b7537db6 SW |
1144 | strlcat(label_buf, label_buf1, label_buf_size); |
1145 | } | |
1146 | } | |
1147 | ||
1148 | out_lse[num_labels] = | |
1149 | mpls_lse_encode(nh_label->label[i], 0, 0, 0); | |
1150 | num_labels++; | |
1151 | } | |
1152 | ||
1153 | return num_labels; | |
1154 | } | |
1155 | ||
a757997c JU |
1156 | static bool _netlink_route_encode_label_info(struct mpls_label_stack *nh_label, |
1157 | struct nlmsghdr *nlmsg, | |
1158 | size_t buflen, struct rtmsg *rtmsg, | |
1159 | char *label_buf, | |
1160 | size_t label_buf_size) | |
fa713d9e | 1161 | { |
d62a17ae | 1162 | mpls_lse_t out_lse[MPLS_MAX_LABELS]; |
a757997c | 1163 | int num_labels; |
bd47f3a3 | 1164 | |
d62a17ae | 1165 | /* |
1166 | * label_buf is *only* currently used within debugging. | |
1167 | * As such when we assign it we are guarding it inside | |
1168 | * a debug test. If you want to change this make sure | |
1169 | * you fix this assumption | |
1170 | */ | |
1171 | label_buf[0] = '\0'; | |
d62a17ae | 1172 | |
a757997c JU |
1173 | num_labels = |
1174 | build_label_stack(nh_label, out_lse, label_buf, label_buf_size); | |
fa712963 RW |
1175 | |
1176 | if (num_labels) { | |
1177 | /* Set the BoS bit */ | |
1178 | out_lse[num_labels - 1] |= htonl(1 << MPLS_LS_S_SHIFT); | |
1179 | ||
0be6e7d7 | 1180 | if (rtmsg->rtm_family == AF_MPLS) { |
a757997c | 1181 | if (!nl_attr_put(nlmsg, buflen, RTA_NEWDST, &out_lse, |
0be6e7d7 JU |
1182 | num_labels * sizeof(mpls_lse_t))) |
1183 | return false; | |
1184 | } else { | |
fa712963 | 1185 | struct rtattr *nest; |
fa712963 | 1186 | |
a757997c JU |
1187 | if (!nl_attr_put16(nlmsg, buflen, RTA_ENCAP_TYPE, |
1188 | LWTUNNEL_ENCAP_MPLS)) | |
0be6e7d7 JU |
1189 | return false; |
1190 | ||
a757997c | 1191 | nest = nl_attr_nest(nlmsg, buflen, RTA_ENCAP); |
0be6e7d7 JU |
1192 | if (!nest) |
1193 | return false; | |
1194 | ||
a757997c | 1195 | if (!nl_attr_put(nlmsg, buflen, MPLS_IPTUNNEL_DST, |
0be6e7d7 JU |
1196 | &out_lse, |
1197 | num_labels * sizeof(mpls_lse_t))) | |
1198 | return false; | |
312a6bee | 1199 | nl_attr_nest_end(nlmsg, nest); |
66d42727 | 1200 | } |
0aabccc0 | 1201 | } |
fa713d9e | 1202 | |
a757997c JU |
1203 | return true; |
1204 | } | |
1205 | ||
1206 | static bool _netlink_route_encode_nexthop_src(const struct nexthop *nexthop, | |
1207 | int family, | |
1208 | struct nlmsghdr *nlmsg, | |
1209 | size_t buflen, int bytelen) | |
1210 | { | |
1211 | if (family == AF_INET) { | |
1212 | if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) { | |
1213 | if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC, | |
1214 | &nexthop->rmap_src.ipv4, bytelen)) | |
1215 | return false; | |
1216 | } else if (nexthop->src.ipv4.s_addr != INADDR_ANY) { | |
1217 | if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC, | |
1218 | &nexthop->src.ipv4, bytelen)) | |
1219 | return false; | |
1220 | } | |
1221 | } else if (family == AF_INET6) { | |
1222 | if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) { | |
1223 | if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC, | |
1224 | &nexthop->rmap_src.ipv6, bytelen)) | |
1225 | return false; | |
1226 | } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) { | |
1227 | if (!nl_attr_put(nlmsg, buflen, RTA_PREFSRC, | |
1228 | &nexthop->src.ipv6, bytelen)) | |
1229 | return false; | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | return true; | |
1234 | } | |
1235 | ||
1236 | /* This function takes a nexthop as argument and adds | |
1237 | * the appropriate netlink attributes to an existing | |
1238 | * netlink message. | |
1239 | * | |
1240 | * @param routedesc: Human readable description of route type | |
1241 | * (direct/recursive, single-/multipath) | |
1242 | * @param bytelen: Length of addresses in bytes. | |
1243 | * @param nexthop: Nexthop information | |
1244 | * @param nlmsg: nlmsghdr structure to fill in. | |
1245 | * @param req_size: The size allocated for the message. | |
1246 | * | |
1247 | * The function returns true if the nexthop could be added | |
1248 | * to the message, otherwise false is returned. | |
1249 | */ | |
1250 | static bool _netlink_route_build_singlepath(const struct prefix *p, | |
1251 | const char *routedesc, int bytelen, | |
1252 | const struct nexthop *nexthop, | |
1253 | struct nlmsghdr *nlmsg, | |
1254 | struct rtmsg *rtmsg, | |
1255 | size_t req_size, int cmd) | |
1256 | { | |
1257 | ||
1258 | char label_buf[256]; | |
1259 | struct vrf *vrf; | |
1260 | char addrstr[INET6_ADDRSTRLEN]; | |
1261 | ||
1262 | assert(nexthop); | |
1263 | ||
1264 | vrf = vrf_lookup_by_id(nexthop->vrf_id); | |
1265 | ||
1266 | if (!_netlink_route_encode_label_info(nexthop->nh_label, nlmsg, | |
1267 | req_size, rtmsg, label_buf, | |
1268 | sizeof(label_buf))) | |
1269 | return false; | |
1270 | ||
d62a17ae | 1271 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) |
1272 | rtmsg->rtm_flags |= RTNH_F_ONLINK; | |
1273 | ||
002e5c43 | 1274 | if (is_route_v4_over_v6(rtmsg->rtm_family, nexthop->type)) { |
d62a17ae | 1275 | rtmsg->rtm_flags |= RTNH_F_ONLINK; |
0be6e7d7 JU |
1276 | if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4)) |
1277 | return false; | |
1278 | if (!nl_attr_put32(nlmsg, req_size, RTA_OIF, nexthop->ifindex)) | |
1279 | return false; | |
d62a17ae | 1280 | |
a757997c JU |
1281 | if (cmd == RTM_NEWROUTE) { |
1282 | if (!_netlink_route_encode_nexthop_src( | |
1283 | nexthop, AF_INET, nlmsg, req_size, bytelen)) | |
0be6e7d7 JU |
1284 | return false; |
1285 | } | |
d62a17ae | 1286 | |
1287 | if (IS_ZEBRA_DEBUG_KERNEL) | |
9266b315 RZ |
1288 | zlog_debug("%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)", |
1289 | __func__, routedesc, p, ipv4_ll_buf, | |
1290 | label_buf, nexthop->ifindex, | |
1291 | VRF_LOGNAME(vrf), nexthop->vrf_id); | |
0be6e7d7 | 1292 | return true; |
0aabccc0 DD |
1293 | } |
1294 | ||
d62a17ae | 1295 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
1296 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) { | |
1297 | /* Send deletes to the kernel without specifying the next-hop */ | |
0be6e7d7 JU |
1298 | if (cmd != RTM_DELROUTE) { |
1299 | if (!_netlink_route_add_gateway_info( | |
1300 | rtmsg->rtm_family, AF_INET, nlmsg, req_size, | |
1301 | bytelen, nexthop)) | |
1302 | return false; | |
1303 | } | |
d62a17ae | 1304 | |
1305 | if (cmd == RTM_NEWROUTE) { | |
a757997c JU |
1306 | if (!_netlink_route_encode_nexthop_src( |
1307 | nexthop, AF_INET, nlmsg, req_size, bytelen)) | |
1308 | return false; | |
d62a17ae | 1309 | } |
1310 | ||
9266b315 RZ |
1311 | if (IS_ZEBRA_DEBUG_KERNEL) { |
1312 | inet_ntop(AF_INET, &nexthop->gate.ipv4, addrstr, | |
1313 | sizeof(addrstr)); | |
1314 | zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)", | |
1315 | __func__, routedesc, p, addrstr, label_buf, | |
1316 | nexthop->ifindex, VRF_LOGNAME(vrf), | |
1317 | nexthop->vrf_id); | |
1318 | } | |
0aabccc0 | 1319 | } |
fa713d9e | 1320 | |
d62a17ae | 1321 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
1322 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { | |
0be6e7d7 JU |
1323 | if (!_netlink_route_add_gateway_info(rtmsg->rtm_family, |
1324 | AF_INET6, nlmsg, req_size, | |
1325 | bytelen, nexthop)) | |
1326 | return false; | |
d62a17ae | 1327 | |
1328 | if (cmd == RTM_NEWROUTE) { | |
a757997c JU |
1329 | if (!_netlink_route_encode_nexthop_src( |
1330 | nexthop, AF_INET6, nlmsg, req_size, | |
1331 | bytelen)) | |
1332 | return false; | |
d62a17ae | 1333 | } |
fa713d9e | 1334 | |
9266b315 RZ |
1335 | if (IS_ZEBRA_DEBUG_KERNEL) { |
1336 | inet_ntop(AF_INET6, &nexthop->gate.ipv6, addrstr, | |
1337 | sizeof(addrstr)); | |
1338 | zlog_debug("%s: (%s): %pFX nexthop via %s %s if %u vrf %s(%u)", | |
1339 | __func__, routedesc, p, addrstr, label_buf, | |
1340 | nexthop->ifindex, VRF_LOGNAME(vrf), | |
1341 | nexthop->vrf_id); | |
1342 | } | |
d62a17ae | 1343 | } |
5e210522 DS |
1344 | |
1345 | /* | |
1346 | * We have the ifindex so we should always send it | |
1347 | * This is especially useful if we are doing route | |
1348 | * leaking. | |
1349 | */ | |
0be6e7d7 JU |
1350 | if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) { |
1351 | if (!nl_attr_put32(nlmsg, req_size, RTA_OIF, nexthop->ifindex)) | |
1352 | return false; | |
1353 | } | |
d62a17ae | 1354 | |
275565fb | 1355 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX) { |
d62a17ae | 1356 | if (cmd == RTM_NEWROUTE) { |
a757997c JU |
1357 | if (!_netlink_route_encode_nexthop_src( |
1358 | nexthop, AF_INET, nlmsg, req_size, bytelen)) | |
1359 | return false; | |
d62a17ae | 1360 | } |
fa713d9e | 1361 | |
d62a17ae | 1362 | if (IS_ZEBRA_DEBUG_KERNEL) |
9266b315 RZ |
1363 | zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)", |
1364 | __func__, routedesc, p, nexthop->ifindex, | |
1365 | VRF_LOGNAME(vrf), nexthop->vrf_id); | |
0aabccc0 | 1366 | } |
0be6e7d7 JU |
1367 | |
1368 | return true; | |
fa713d9e CF |
1369 | } |
1370 | ||
1371 | /* This function takes a nexthop as argument and | |
312a6bee | 1372 | * appends to the given netlink msg. If the nexthop |
fa713d9e CF |
1373 | * defines a preferred source, the src parameter |
1374 | * will be modified to point to that src, otherwise | |
1375 | * it will be kept unmodified. | |
1376 | * | |
1377 | * @param routedesc: Human readable description of route type | |
1378 | * (direct/recursive, single-/multipath) | |
1379 | * @param bytelen: Length of addresses in bytes. | |
1380 | * @param nexthop: Nexthop information | |
312a6bee JU |
1381 | * @param nlmsg: nlmsghdr structure to fill in. |
1382 | * @param req_size: The size allocated for the message. | |
fa713d9e CF |
1383 | * @param src: pointer pointing to a location where |
1384 | * the prefsrc should be stored. | |
0be6e7d7 JU |
1385 | * |
1386 | * The function returns true if the nexthop could be added | |
1387 | * to the message, otherwise false is returned. | |
fa713d9e | 1388 | */ |
0be6e7d7 | 1389 | static bool _netlink_route_build_multipath(const struct prefix *p, |
312a6bee JU |
1390 | const char *routedesc, int bytelen, |
1391 | const struct nexthop *nexthop, | |
1392 | struct nlmsghdr *nlmsg, | |
1393 | size_t req_size, struct rtmsg *rtmsg, | |
1394 | const union g_addr **src) | |
fa713d9e | 1395 | { |
9a62e84b | 1396 | char label_buf[256]; |
bd47f3a3 | 1397 | struct vrf *vrf; |
312a6bee | 1398 | struct rtnexthop *rtnh; |
d62a17ae | 1399 | |
312a6bee | 1400 | rtnh = nl_attr_rtnh(nlmsg, req_size); |
0be6e7d7 JU |
1401 | if (rtnh == NULL) |
1402 | return false; | |
d62a17ae | 1403 | |
b7537db6 SW |
1404 | assert(nexthop); |
1405 | ||
bd47f3a3 JU |
1406 | vrf = vrf_lookup_by_id(nexthop->vrf_id); |
1407 | ||
a757997c JU |
1408 | if (!_netlink_route_encode_label_info(nexthop->nh_label, nlmsg, |
1409 | req_size, rtmsg, label_buf, | |
1410 | sizeof(label_buf))) | |
1411 | return false; | |
d62a17ae | 1412 | |
1413 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) | |
1414 | rtnh->rtnh_flags |= RTNH_F_ONLINK; | |
1415 | ||
002e5c43 | 1416 | if (is_route_v4_over_v6(rtmsg->rtm_family, nexthop->type)) { |
d62a17ae | 1417 | rtnh->rtnh_flags |= RTNH_F_ONLINK; |
a757997c | 1418 | if (!nl_attr_put(nlmsg, req_size, RTA_GATEWAY, &ipv4_ll, 4)) |
0be6e7d7 | 1419 | return false; |
d62a17ae | 1420 | rtnh->rtnh_ifindex = nexthop->ifindex; |
8d27e1aa | 1421 | if (nexthop->weight) |
1422 | rtnh->rtnh_hops = nexthop->weight - 1; | |
d62a17ae | 1423 | |
975a328e | 1424 | if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) |
d62a17ae | 1425 | *src = &nexthop->rmap_src; |
975a328e | 1426 | else if (nexthop->src.ipv4.s_addr != INADDR_ANY) |
d62a17ae | 1427 | *src = &nexthop->src; |
1428 | ||
1429 | if (IS_ZEBRA_DEBUG_KERNEL) | |
1430 | zlog_debug( | |
9266b315 RZ |
1431 | "%s: 5549 (%s): %pFX nexthop via %s %s if %u vrf %s(%u)", |
1432 | __func__, routedesc, p, ipv4_ll_buf, label_buf, | |
bd47f3a3 JU |
1433 | nexthop->ifindex, VRF_LOGNAME(vrf), |
1434 | nexthop->vrf_id); | |
312a6bee | 1435 | nl_attr_rtnh_end(nlmsg, rtnh); |
0be6e7d7 | 1436 | return true; |
d62a17ae | 1437 | } |
1438 | ||
1439 | if (nexthop->type == NEXTHOP_TYPE_IPV4 | |
1440 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) { | |
0be6e7d7 JU |
1441 | if (!_netlink_route_add_gateway_info(rtmsg->rtm_family, AF_INET, |
1442 | nlmsg, req_size, bytelen, | |
1443 | nexthop)) | |
1444 | return false; | |
1445 | ||
975a328e | 1446 | if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) |
d62a17ae | 1447 | *src = &nexthop->rmap_src; |
975a328e | 1448 | else if (nexthop->src.ipv4.s_addr != INADDR_ANY) |
d62a17ae | 1449 | *src = &nexthop->src; |
1450 | ||
a50404aa RZ |
1451 | if (IS_ZEBRA_DEBUG_KERNEL) |
1452 | zlog_debug("%s: (%s): %pFX nexthop via %pI4 %s if %u vrf %s(%u)", | |
1453 | __func__, routedesc, p, &nexthop->gate.ipv4, | |
1454 | label_buf, nexthop->ifindex, | |
1455 | VRF_LOGNAME(vrf), nexthop->vrf_id); | |
d62a17ae | 1456 | } |
1457 | if (nexthop->type == NEXTHOP_TYPE_IPV6 | |
1458 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { | |
0be6e7d7 JU |
1459 | if (!_netlink_route_add_gateway_info(rtmsg->rtm_family, |
1460 | AF_INET6, nlmsg, req_size, | |
1461 | bytelen, nexthop)) | |
1462 | return false; | |
d62a17ae | 1463 | |
1464 | if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) | |
1465 | *src = &nexthop->rmap_src; | |
1466 | else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) | |
1467 | *src = &nexthop->src; | |
1468 | ||
a50404aa RZ |
1469 | if (IS_ZEBRA_DEBUG_KERNEL) |
1470 | zlog_debug("%s: (%s): %pFX nexthop via %pI6 %s if %u vrf %s(%u)", | |
1471 | __func__, routedesc, p, &nexthop->gate.ipv6, | |
1472 | label_buf, nexthop->ifindex, | |
1473 | VRF_LOGNAME(vrf), nexthop->vrf_id); | |
d62a17ae | 1474 | } |
5e210522 DS |
1475 | |
1476 | /* | |
1477 | * We have figured out the ifindex so we should always send it | |
1478 | * This is especially useful if we are doing route | |
1479 | * leaking. | |
1480 | */ | |
1481 | if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) | |
1482 | rtnh->rtnh_ifindex = nexthop->ifindex; | |
1483 | ||
d62a17ae | 1484 | /* ifindex */ |
275565fb | 1485 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX) { |
975a328e | 1486 | if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) |
d62a17ae | 1487 | *src = &nexthop->rmap_src; |
975a328e | 1488 | else if (nexthop->src.ipv4.s_addr != INADDR_ANY) |
d62a17ae | 1489 | *src = &nexthop->src; |
1490 | ||
1491 | if (IS_ZEBRA_DEBUG_KERNEL) | |
9266b315 RZ |
1492 | zlog_debug("%s: (%s): %pFX nexthop via if %u vrf %s(%u)", |
1493 | __func__, routedesc, p, nexthop->ifindex, | |
1494 | VRF_LOGNAME(vrf), nexthop->vrf_id); | |
d62a17ae | 1495 | } |
df7fb580 DS |
1496 | |
1497 | if (nexthop->weight) | |
1498 | rtnh->rtnh_hops = nexthop->weight - 1; | |
0be6e7d7 | 1499 | |
312a6bee | 1500 | nl_attr_rtnh_end(nlmsg, rtnh); |
0be6e7d7 | 1501 | return true; |
fa713d9e CF |
1502 | } |
1503 | ||
0be6e7d7 | 1504 | static inline bool _netlink_mpls_build_singlepath(const struct prefix *p, |
9a0132a5 | 1505 | const char *routedesc, |
81793ac1 | 1506 | const zebra_nhlfe_t *nhlfe, |
d62a17ae | 1507 | struct nlmsghdr *nlmsg, |
1508 | struct rtmsg *rtmsg, | |
1509 | size_t req_size, int cmd) | |
40c7bdb0 | 1510 | { |
d62a17ae | 1511 | int bytelen; |
d7c0a89a | 1512 | uint8_t family; |
40c7bdb0 | 1513 | |
d62a17ae | 1514 | family = NHLFE_FAMILY(nhlfe); |
1515 | bytelen = (family == AF_INET ? 4 : 16); | |
0be6e7d7 JU |
1516 | return _netlink_route_build_singlepath(p, routedesc, bytelen, |
1517 | nhlfe->nexthop, nlmsg, rtmsg, | |
1518 | req_size, cmd); | |
40c7bdb0 | 1519 | } |
1520 | ||
1521 | ||
0be6e7d7 | 1522 | static inline bool |
9a0132a5 | 1523 | _netlink_mpls_build_multipath(const struct prefix *p, const char *routedesc, |
312a6bee JU |
1524 | const zebra_nhlfe_t *nhlfe, |
1525 | struct nlmsghdr *nlmsg, size_t req_size, | |
1526 | struct rtmsg *rtmsg, const union g_addr **src) | |
40c7bdb0 | 1527 | { |
d62a17ae | 1528 | int bytelen; |
d7c0a89a | 1529 | uint8_t family; |
40c7bdb0 | 1530 | |
d62a17ae | 1531 | family = NHLFE_FAMILY(nhlfe); |
1532 | bytelen = (family == AF_INET ? 4 : 16); | |
0be6e7d7 JU |
1533 | return _netlink_route_build_multipath(p, routedesc, bytelen, |
1534 | nhlfe->nexthop, nlmsg, req_size, | |
1535 | rtmsg, src); | |
40c7bdb0 | 1536 | } |
1537 | ||
d7c0a89a | 1538 | static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc) |
40c7bdb0 | 1539 | { |
d62a17ae | 1540 | if (IS_ZEBRA_DEBUG_KERNEL) |
0be6e7d7 JU |
1541 | zlog_debug("netlink_mpls_multipath_msg_encode() (%s): %s %u/20", |
1542 | routedesc, nl_msg_type_to_str(cmd), label); | |
fa713d9e CF |
1543 | } |
1544 | ||
05657ec2 PG |
1545 | static int netlink_neigh_update(int cmd, int ifindex, void *addr, char *lla, |
1546 | int llalen, ns_id_t ns_id, uint8_t family, | |
1547 | bool permanent, uint8_t protocol) | |
5c610faf | 1548 | { |
d62a17ae | 1549 | struct { |
1550 | struct nlmsghdr n; | |
1551 | struct ndmsg ndm; | |
1552 | char buf[256]; | |
1553 | } req; | |
5c610faf | 1554 | |
5895d33f | 1555 | struct zebra_ns *zns = zebra_ns_lookup(ns_id); |
8f7d9fc0 | 1556 | |
5605ecfc | 1557 | memset(&req, 0, sizeof(req)); |
5c610faf | 1558 | |
d62a17ae | 1559 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); |
1560 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
1561 | req.n.nlmsg_type = cmd; // RTM_NEWNEIGH or RTM_DELNEIGH | |
1562 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
a55ba23f | 1563 | |
05657ec2 | 1564 | req.ndm.ndm_family = family; |
d62a17ae | 1565 | req.ndm.ndm_ifindex = ifindex; |
1566 | req.ndm.ndm_type = RTN_UNICAST; | |
05657ec2 PG |
1567 | if (cmd == RTM_NEWNEIGH) { |
1568 | if (!permanent) | |
1569 | req.ndm.ndm_state = NUD_REACHABLE; | |
1570 | else | |
1571 | req.ndm.ndm_state = NUD_PERMANENT; | |
1572 | } else | |
1573 | req.ndm.ndm_state = NUD_FAILED; | |
5c610faf | 1574 | |
312a6bee JU |
1575 | nl_attr_put(&req.n, sizeof(req), NDA_PROTOCOL, &protocol, |
1576 | sizeof(protocol)); | |
05657ec2 | 1577 | req.ndm.ndm_type = RTN_UNICAST; |
df948efc PG |
1578 | nl_attr_put(&req.n, sizeof(req), NDA_DST, addr, |
1579 | family2addrsize(family)); | |
05657ec2 PG |
1580 | if (lla) |
1581 | nl_attr_put(&req.n, sizeof(req), NDA_LLADDR, lla, llalen); | |
5c610faf | 1582 | |
d62a17ae | 1583 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, |
1584 | 0); | |
5c610faf DS |
1585 | } |
1586 | ||
762288f5 SW |
1587 | static bool nexthop_set_src(const struct nexthop *nexthop, int family, |
1588 | union g_addr *src) | |
1589 | { | |
1590 | if (family == AF_INET) { | |
1591 | if (nexthop->rmap_src.ipv4.s_addr != INADDR_ANY) { | |
1592 | src->ipv4 = nexthop->rmap_src.ipv4; | |
1593 | return true; | |
1594 | } else if (nexthop->src.ipv4.s_addr != INADDR_ANY) { | |
1595 | src->ipv4 = nexthop->src.ipv4; | |
1596 | return true; | |
1597 | } | |
1598 | } else if (family == AF_INET6) { | |
1599 | if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->rmap_src.ipv6)) { | |
1600 | src->ipv6 = nexthop->rmap_src.ipv6; | |
1601 | return true; | |
1602 | } else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop->src.ipv6)) { | |
1603 | src->ipv6 = nexthop->src.ipv6; | |
1604 | return true; | |
1605 | } | |
1606 | } | |
1607 | ||
1608 | return false; | |
1609 | } | |
1610 | ||
0be6e7d7 JU |
1611 | /* |
1612 | * The function returns true if the attribute could be added | |
1613 | * to the message, otherwise false is returned. | |
1614 | */ | |
1615 | static int netlink_route_nexthop_encap(struct nlmsghdr *n, size_t nlen, | |
1616 | struct nexthop *nh) | |
f2a0ba3a RZ |
1617 | { |
1618 | struct rtattr *nest; | |
1619 | ||
1620 | switch (nh->nh_encap_type) { | |
1621 | case NET_VXLAN: | |
a757997c | 1622 | if (!nl_attr_put16(n, nlen, RTA_ENCAP_TYPE, nh->nh_encap_type)) |
0be6e7d7 | 1623 | return false; |
f2a0ba3a | 1624 | |
312a6bee | 1625 | nest = nl_attr_nest(n, nlen, RTA_ENCAP); |
0be6e7d7 JU |
1626 | if (!nest) |
1627 | return false; | |
1628 | ||
1629 | if (!nl_attr_put32(n, nlen, 0 /* VXLAN_VNI */, | |
1630 | nh->nh_encap.vni)) | |
1631 | return false; | |
312a6bee | 1632 | nl_attr_nest_end(n, nest); |
f2a0ba3a RZ |
1633 | break; |
1634 | } | |
0be6e7d7 JU |
1635 | |
1636 | return true; | |
f2a0ba3a RZ |
1637 | } |
1638 | ||
7cdb1a84 MS |
1639 | /* |
1640 | * Routing table change via netlink interface, using a dataplane context object | |
0be6e7d7 JU |
1641 | * |
1642 | * Returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer | |
1643 | * otherwise the number of bytes written to buf. | |
7cdb1a84 | 1644 | */ |
0be6e7d7 JU |
1645 | ssize_t netlink_route_multipath_msg_encode(int cmd, |
1646 | struct zebra_dplane_ctx *ctx, | |
1647 | uint8_t *data, size_t datalen, | |
1648 | bool fpm, bool force_nhg) | |
7cdb1a84 MS |
1649 | { |
1650 | int bytelen; | |
7cdb1a84 MS |
1651 | struct nexthop *nexthop = NULL; |
1652 | unsigned int nexthop_num; | |
7cdb1a84 | 1653 | const char *routedesc; |
762288f5 | 1654 | bool setsrc = false; |
7cdb1a84 MS |
1655 | union g_addr src; |
1656 | const struct prefix *p, *src_p; | |
1657 | uint32_t table_id; | |
1658 | ||
1659 | struct { | |
1660 | struct nlmsghdr n; | |
1661 | struct rtmsg r; | |
e57a3fab RZ |
1662 | char buf[]; |
1663 | } *req = (void *)data; | |
7cdb1a84 MS |
1664 | |
1665 | p = dplane_ctx_get_dest(ctx); | |
1666 | src_p = dplane_ctx_get_src(ctx); | |
1667 | ||
0be6e7d7 JU |
1668 | if (datalen < sizeof(*req)) |
1669 | return 0; | |
1670 | ||
e57a3fab | 1671 | memset(req, 0, sizeof(*req)); |
7cdb1a84 | 1672 | |
b9c87515 | 1673 | bytelen = (p->family == AF_INET ? 4 : 16); |
7cdb1a84 | 1674 | |
e57a3fab RZ |
1675 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
1676 | req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
7cdb1a84 | 1677 | |
334734a8 DS |
1678 | if ((cmd == RTM_NEWROUTE) && |
1679 | ((p->family == AF_INET) || v6_rr_semantics)) | |
e57a3fab | 1680 | req->n.nlmsg_flags |= NLM_F_REPLACE; |
7cdb1a84 | 1681 | |
e57a3fab | 1682 | req->n.nlmsg_type = cmd; |
7cdb1a84 | 1683 | |
e57a3fab | 1684 | req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid; |
7cdb1a84 | 1685 | |
b9c87515 | 1686 | req->r.rtm_family = p->family; |
e57a3fab RZ |
1687 | req->r.rtm_dst_len = p->prefixlen; |
1688 | req->r.rtm_src_len = src_p ? src_p->prefixlen : 0; | |
1689 | req->r.rtm_scope = RT_SCOPE_UNIVERSE; | |
7cdb1a84 | 1690 | |
5709131c | 1691 | if (cmd == RTM_DELROUTE) |
e57a3fab | 1692 | req->r.rtm_protocol = zebra2proto(dplane_ctx_get_old_type(ctx)); |
5709131c | 1693 | else |
e57a3fab | 1694 | req->r.rtm_protocol = zebra2proto(dplane_ctx_get_type(ctx)); |
7cdb1a84 MS |
1695 | |
1696 | /* | |
1697 | * blackhole routes are not RTN_UNICAST, they are | |
1698 | * RTN_ BLACKHOLE|UNREACHABLE|PROHIBIT | |
1699 | * so setting this value as a RTN_UNICAST would | |
1700 | * cause the route lookup of just the prefix | |
1701 | * to fail. So no need to specify this for | |
1702 | * the RTM_DELROUTE case | |
1703 | */ | |
1704 | if (cmd != RTM_DELROUTE) | |
e57a3fab | 1705 | req->r.rtm_type = RTN_UNICAST; |
7cdb1a84 | 1706 | |
0be6e7d7 JU |
1707 | if (!nl_attr_put(&req->n, datalen, RTA_DST, &p->u.prefix, bytelen)) |
1708 | return 0; | |
1709 | if (src_p) { | |
1710 | if (!nl_attr_put(&req->n, datalen, RTA_SRC, &src_p->u.prefix, | |
1711 | bytelen)) | |
1712 | return 0; | |
1713 | } | |
7cdb1a84 MS |
1714 | |
1715 | /* Metric. */ | |
1716 | /* Hardcode the metric for all routes coming from zebra. Metric isn't | |
1717 | * used | |
1718 | * either by the kernel or by zebra. Its purely for calculating best | |
1719 | * path(s) | |
1720 | * by the routing protocol and for communicating with protocol peers. | |
1721 | */ | |
0be6e7d7 JU |
1722 | if (!nl_attr_put32(&req->n, datalen, RTA_PRIORITY, |
1723 | NL_DEFAULT_ROUTE_METRIC)) | |
1724 | return 0; | |
7cdb1a84 MS |
1725 | |
1726 | #if defined(SUPPORT_REALMS) | |
1727 | { | |
1728 | route_tag_t tag; | |
1729 | ||
5709131c | 1730 | if (cmd == RTM_DELROUTE) |
7cdb1a84 | 1731 | tag = dplane_ctx_get_old_tag(ctx); |
5709131c | 1732 | else |
7cdb1a84 | 1733 | tag = dplane_ctx_get_tag(ctx); |
7cdb1a84 | 1734 | |
0be6e7d7 JU |
1735 | if (tag > 0 && tag <= 255) { |
1736 | if (!nl_attr_put32(&req->n, datalen, RTA_FLOW, tag)) | |
1737 | return 0; | |
1738 | } | |
7cdb1a84 MS |
1739 | } |
1740 | #endif | |
1741 | /* Table corresponding to this route. */ | |
1742 | table_id = dplane_ctx_get_table(ctx); | |
1743 | if (table_id < 256) | |
e57a3fab | 1744 | req->r.rtm_table = table_id; |
7cdb1a84 | 1745 | else { |
e57a3fab | 1746 | req->r.rtm_table = RT_TABLE_UNSPEC; |
0be6e7d7 JU |
1747 | if (!nl_attr_put32(&req->n, datalen, RTA_TABLE, table_id)) |
1748 | return 0; | |
7cdb1a84 MS |
1749 | } |
1750 | ||
9266b315 RZ |
1751 | if (IS_ZEBRA_DEBUG_KERNEL) |
1752 | zlog_debug( | |
1753 | "%s: %s %pFX vrf %u(%u)", __func__, | |
1754 | nl_msg_type_to_str(cmd), p, dplane_ctx_get_vrf(ctx), | |
1755 | table_id); | |
7cdb1a84 MS |
1756 | |
1757 | /* | |
1758 | * If we are not updating the route and we have received | |
1759 | * a route delete, then all we need to fill in is the | |
1760 | * prefix information to tell the kernel to schwack | |
1761 | * it. | |
1762 | */ | |
1763 | if (cmd == RTM_DELROUTE) | |
0be6e7d7 | 1764 | return NLMSG_ALIGN(req->n.nlmsg_len); |
7cdb1a84 MS |
1765 | |
1766 | if (dplane_ctx_get_mtu(ctx) || dplane_ctx_get_nh_mtu(ctx)) { | |
312a6bee | 1767 | struct rtattr *nest; |
7cdb1a84 MS |
1768 | uint32_t mtu = dplane_ctx_get_mtu(ctx); |
1769 | uint32_t nexthop_mtu = dplane_ctx_get_nh_mtu(ctx); | |
5709131c | 1770 | |
7cdb1a84 MS |
1771 | if (!mtu || (nexthop_mtu && nexthop_mtu < mtu)) |
1772 | mtu = nexthop_mtu; | |
312a6bee JU |
1773 | |
1774 | nest = nl_attr_nest(&req->n, datalen, RTA_METRICS); | |
0be6e7d7 JU |
1775 | if (nest == NULL) |
1776 | return 0; | |
1777 | ||
1778 | if (!nl_attr_put(&req->n, datalen, RTAX_MTU, &mtu, sizeof(mtu))) | |
1779 | return 0; | |
312a6bee | 1780 | nl_attr_nest_end(&req->n, nest); |
7cdb1a84 MS |
1781 | } |
1782 | ||
4be03ff4 IR |
1783 | /* |
1784 | * Always install blackhole routes without using nexthops, because of | |
1785 | * the following kernel problems: | |
1786 | * 1. Kernel nexthops don't suport unreachable/prohibit route types. | |
1787 | * 2. Blackhole kernel nexthops are deleted when loopback is down. | |
1788 | */ | |
1789 | nexthop = dplane_ctx_get_ng(ctx)->nexthop; | |
1790 | if (nexthop) { | |
1791 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
1792 | nexthop = nexthop->resolved; | |
1793 | ||
1794 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) { | |
1795 | switch (nexthop->bh_type) { | |
1796 | case BLACKHOLE_ADMINPROHIB: | |
1797 | req->r.rtm_type = RTN_PROHIBIT; | |
1798 | break; | |
1799 | case BLACKHOLE_REJECT: | |
1800 | req->r.rtm_type = RTN_UNREACHABLE; | |
1801 | break; | |
1802 | default: | |
1803 | req->r.rtm_type = RTN_BLACKHOLE; | |
1804 | break; | |
1805 | } | |
1806 | return NLMSG_ALIGN(req->n.nlmsg_len); | |
1807 | } | |
1808 | } | |
1809 | ||
6c67f41f SW |
1810 | if ((!fpm && kernel_nexthops_supported() |
1811 | && (!proto_nexthops_only() | |
1812 | || is_proto_nhg(dplane_ctx_get_nhe_id(ctx), 0))) | |
1813 | || (fpm && force_nhg)) { | |
d8bfd8dc | 1814 | /* Kernel supports nexthop objects */ |
9a0132a5 | 1815 | if (IS_ZEBRA_DEBUG_KERNEL) |
0be6e7d7 JU |
1816 | zlog_debug("%s: %pFX nhg_id is %u", __func__, p, |
1817 | dplane_ctx_get_nhe_id(ctx)); | |
e57a3fab | 1818 | |
0be6e7d7 JU |
1819 | if (!nl_attr_put32(&req->n, datalen, RTA_NH_ID, |
1820 | dplane_ctx_get_nhe_id(ctx))) | |
1821 | return 0; | |
d8bfd8dc SW |
1822 | |
1823 | /* Have to determine src still */ | |
1824 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) { | |
1825 | if (setsrc) | |
1826 | break; | |
1827 | ||
e57a3fab | 1828 | setsrc = nexthop_set_src(nexthop, p->family, &src); |
d8bfd8dc SW |
1829 | } |
1830 | ||
1831 | if (setsrc) { | |
0be6e7d7 JU |
1832 | if (p->family == AF_INET) { |
1833 | if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC, | |
1834 | &src.ipv4, bytelen)) | |
1835 | return 0; | |
1836 | } else if (p->family == AF_INET6) { | |
1837 | if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC, | |
1838 | &src.ipv6, bytelen)) | |
1839 | return 0; | |
1840 | } | |
d8bfd8dc | 1841 | } |
f78fe8f3 | 1842 | |
0be6e7d7 | 1843 | return NLMSG_ALIGN(req->n.nlmsg_len); |
de3f5488 SW |
1844 | } |
1845 | ||
7cdb1a84 | 1846 | /* Count overall nexthops so we can decide whether to use singlepath |
5709131c MS |
1847 | * or multipath case. |
1848 | */ | |
7cdb1a84 MS |
1849 | nexthop_num = 0; |
1850 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) { | |
1851 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
1852 | continue; | |
b9c87515 | 1853 | if (!NEXTHOP_IS_ACTIVE(nexthop->flags)) |
7cdb1a84 MS |
1854 | continue; |
1855 | ||
1856 | nexthop_num++; | |
1857 | } | |
1858 | ||
1859 | /* Singlepath case. */ | |
220f0f42 | 1860 | if (nexthop_num == 1) { |
7cdb1a84 MS |
1861 | nexthop_num = 0; |
1862 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) { | |
7cdb1a84 MS |
1863 | if (CHECK_FLAG(nexthop->flags, |
1864 | NEXTHOP_FLAG_RECURSIVE)) { | |
5709131c MS |
1865 | |
1866 | if (setsrc) | |
1867 | continue; | |
1868 | ||
b9c87515 RZ |
1869 | setsrc = nexthop_set_src(nexthop, p->family, |
1870 | &src); | |
f183e380 | 1871 | continue; |
7cdb1a84 MS |
1872 | } |
1873 | ||
b9c87515 | 1874 | if (NEXTHOP_IS_ACTIVE(nexthop->flags)) { |
7cdb1a84 MS |
1875 | routedesc = nexthop->rparent |
1876 | ? "recursive, single-path" | |
1877 | : "single-path"; | |
1878 | ||
0be6e7d7 JU |
1879 | if (!_netlink_route_build_singlepath( |
1880 | p, routedesc, bytelen, nexthop, | |
1881 | &req->n, &req->r, datalen, cmd)) | |
1882 | return 0; | |
7cdb1a84 MS |
1883 | nexthop_num++; |
1884 | break; | |
1885 | } | |
f2a0ba3a RZ |
1886 | |
1887 | /* | |
1888 | * Add encapsulation information when installing via | |
1889 | * FPM. | |
1890 | */ | |
0be6e7d7 JU |
1891 | if (fpm) { |
1892 | if (!netlink_route_nexthop_encap( | |
1893 | &req->n, datalen, nexthop)) | |
1894 | return 0; | |
1895 | } | |
7cdb1a84 | 1896 | } |
f2a0ba3a | 1897 | |
13e0321a | 1898 | if (setsrc) { |
0be6e7d7 JU |
1899 | if (p->family == AF_INET) { |
1900 | if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC, | |
1901 | &src.ipv4, bytelen)) | |
1902 | return 0; | |
1903 | } else if (p->family == AF_INET6) { | |
1904 | if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC, | |
1905 | &src.ipv6, bytelen)) | |
1906 | return 0; | |
1907 | } | |
7cdb1a84 MS |
1908 | } |
1909 | } else { /* Multipath case */ | |
312a6bee | 1910 | struct rtattr *nest; |
81793ac1 | 1911 | const union g_addr *src1 = NULL; |
7cdb1a84 | 1912 | |
312a6bee | 1913 | nest = nl_attr_nest(&req->n, datalen, RTA_MULTIPATH); |
0be6e7d7 JU |
1914 | if (nest == NULL) |
1915 | return 0; | |
7cdb1a84 MS |
1916 | |
1917 | nexthop_num = 0; | |
1918 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) { | |
7cdb1a84 MS |
1919 | if (CHECK_FLAG(nexthop->flags, |
1920 | NEXTHOP_FLAG_RECURSIVE)) { | |
1921 | /* This only works for IPv4 now */ | |
5709131c MS |
1922 | if (setsrc) |
1923 | continue; | |
1924 | ||
b9c87515 RZ |
1925 | setsrc = nexthop_set_src(nexthop, p->family, |
1926 | &src); | |
78e54ded | 1927 | continue; |
7cdb1a84 MS |
1928 | } |
1929 | ||
b9c87515 | 1930 | if (NEXTHOP_IS_ACTIVE(nexthop->flags)) { |
7cdb1a84 MS |
1931 | routedesc = nexthop->rparent |
1932 | ? "recursive, multipath" | |
1933 | : "multipath"; | |
1934 | nexthop_num++; | |
1935 | ||
0be6e7d7 JU |
1936 | if (!_netlink_route_build_multipath( |
1937 | p, routedesc, bytelen, nexthop, | |
1938 | &req->n, datalen, &req->r, &src1)) | |
1939 | return 0; | |
7cdb1a84 MS |
1940 | |
1941 | if (!setsrc && src1) { | |
b9c87515 | 1942 | if (p->family == AF_INET) |
7cdb1a84 | 1943 | src.ipv4 = src1->ipv4; |
b9c87515 | 1944 | else if (p->family == AF_INET6) |
7cdb1a84 MS |
1945 | src.ipv6 = src1->ipv6; |
1946 | ||
1947 | setsrc = 1; | |
1948 | } | |
1949 | } | |
312a6bee | 1950 | } |
0be6e7d7 | 1951 | |
312a6bee JU |
1952 | nl_attr_nest_end(&req->n, nest); |
1953 | ||
1954 | /* | |
1955 | * Add encapsulation information when installing via | |
1956 | * FPM. | |
1957 | */ | |
1958 | if (fpm) { | |
1959 | for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), | |
1960 | nexthop)) { | |
1961 | if (CHECK_FLAG(nexthop->flags, | |
1962 | NEXTHOP_FLAG_RECURSIVE)) | |
1963 | continue; | |
0be6e7d7 JU |
1964 | if (!netlink_route_nexthop_encap( |
1965 | &req->n, datalen, nexthop)) | |
1966 | return 0; | |
312a6bee | 1967 | } |
7cdb1a84 | 1968 | } |
f2a0ba3a | 1969 | |
312a6bee | 1970 | |
13e0321a | 1971 | if (setsrc) { |
0be6e7d7 JU |
1972 | if (p->family == AF_INET) { |
1973 | if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC, | |
1974 | &src.ipv4, bytelen)) | |
1975 | return 0; | |
1976 | } else if (p->family == AF_INET6) { | |
1977 | if (!nl_attr_put(&req->n, datalen, RTA_PREFSRC, | |
1978 | &src.ipv6, bytelen)) | |
1979 | return 0; | |
1980 | } | |
7cdb1a84 MS |
1981 | if (IS_ZEBRA_DEBUG_KERNEL) |
1982 | zlog_debug("Setting source"); | |
1983 | } | |
7cdb1a84 MS |
1984 | } |
1985 | ||
1986 | /* If there is no useful nexthop then return. */ | |
1987 | if (nexthop_num == 0) { | |
1988 | if (IS_ZEBRA_DEBUG_KERNEL) | |
9266b315 | 1989 | zlog_debug("%s: No useful nexthop.", __func__); |
7cdb1a84 MS |
1990 | } |
1991 | ||
312a6bee | 1992 | return NLMSG_ALIGN(req->n.nlmsg_len); |
7cdb1a84 MS |
1993 | } |
1994 | ||
43b5cc5e | 1995 | int kernel_get_ipmr_sg_stats(struct zebra_vrf *zvrf, void *in) |
e3be0432 | 1996 | { |
5523c156 | 1997 | uint32_t actual_table; |
d62a17ae | 1998 | int suc = 0; |
1999 | struct mcast_route_data *mr = (struct mcast_route_data *)in; | |
bd8b9272 DS |
2000 | struct { |
2001 | struct nlmsghdr n; | |
2002 | struct ndmsg ndm; | |
2003 | char buf[256]; | |
2004 | } req; | |
e3be0432 | 2005 | |
d62a17ae | 2006 | mroute = mr; |
5895d33f | 2007 | struct zebra_ns *zns; |
bd8b9272 | 2008 | |
009f8ad5 | 2009 | zns = zvrf->zns; |
5605ecfc | 2010 | memset(&req, 0, sizeof(req)); |
bd8b9272 DS |
2011 | |
2012 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
2013 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
2014 | req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid; | |
2015 | ||
2016 | req.ndm.ndm_family = RTNL_FAMILY_IPMR; | |
2017 | req.n.nlmsg_type = RTM_GETROUTE; | |
2018 | ||
a757997c JU |
2019 | nl_attr_put32(&req.n, sizeof(req), RTA_IIF, mroute->ifindex); |
2020 | nl_attr_put32(&req.n, sizeof(req), RTA_OIF, mroute->ifindex); | |
2021 | nl_attr_put32(&req.n, sizeof(req), RTA_SRC, mroute->sg.src.s_addr); | |
2022 | nl_attr_put32(&req.n, sizeof(req), RTA_DST, mroute->sg.grp.s_addr); | |
5523c156 DS |
2023 | /* |
2024 | * What? | |
2025 | * | |
2026 | * So during the namespace cleanup we started storing | |
2027 | * the zvrf table_id for the default table as RT_TABLE_MAIN | |
2028 | * which is what the normal routing table for ip routing is. | |
2029 | * This change caused this to break our lookups of sg data | |
2030 | * because prior to this change the zvrf->table_id was 0 | |
2031 | * and when the pim multicast kernel code saw a 0, | |
2032 | * it was auto-translated to RT_TABLE_DEFAULT. But since | |
2033 | * we are now passing in RT_TABLE_MAIN there is no auto-translation | |
2034 | * and the kernel goes screw you and the delicious cookies you | |
2035 | * are trying to give me. So now we have this little hack. | |
2036 | */ | |
2037 | actual_table = (zvrf->table_id == RT_TABLE_MAIN) ? RT_TABLE_DEFAULT : | |
2038 | zvrf->table_id; | |
a757997c | 2039 | nl_attr_put32(&req.n, sizeof(req), RTA_TABLE, actual_table); |
e3be0432 | 2040 | |
bd8b9272 DS |
2041 | suc = netlink_talk(netlink_route_change_read_multicast, &req.n, |
2042 | &zns->netlink_cmd, zns, 0); | |
e3be0432 | 2043 | |
bd8b9272 | 2044 | mroute = NULL; |
d62a17ae | 2045 | return suc; |
e3be0432 DS |
2046 | } |
2047 | ||
8d03bc50 SW |
2048 | /* Char length to debug ID with */ |
2049 | #define ID_LENGTH 10 | |
2050 | ||
0be6e7d7 | 2051 | static bool _netlink_nexthop_build_group(struct nlmsghdr *n, size_t req_size, |
8d03bc50 | 2052 | uint32_t id, |
e22e8001 | 2053 | const struct nh_grp *z_grp, |
0c8215cb | 2054 | const uint8_t count) |
565ce0d3 | 2055 | { |
565ce0d3 | 2056 | struct nexthop_grp grp[count]; |
8d03bc50 SW |
2057 | /* Need space for max group size, "/", and null term */ |
2058 | char buf[(MULTIPATH_NUM * (ID_LENGTH + 1)) + 1]; | |
2059 | char buf1[ID_LENGTH + 2]; | |
2060 | ||
2061 | buf[0] = '\0'; | |
565ce0d3 SW |
2062 | |
2063 | memset(grp, 0, sizeof(grp)); | |
2064 | ||
2065 | if (count) { | |
0c8215cb | 2066 | for (int i = 0; i < count; i++) { |
e22e8001 | 2067 | grp[i].id = z_grp[i].id; |
df7fb580 | 2068 | grp[i].weight = z_grp[i].weight - 1; |
8d03bc50 SW |
2069 | |
2070 | if (IS_ZEBRA_DEBUG_KERNEL) { | |
2071 | if (i == 0) | |
2072 | snprintf(buf, sizeof(buf1), "group %u", | |
2073 | grp[i].id); | |
2074 | else { | |
2075 | snprintf(buf1, sizeof(buf1), "/%u", | |
2076 | grp[i].id); | |
2077 | strlcat(buf, buf1, sizeof(buf)); | |
2078 | } | |
2079 | } | |
565ce0d3 | 2080 | } |
0be6e7d7 JU |
2081 | if (!nl_attr_put(n, req_size, NHA_GROUP, grp, |
2082 | count * sizeof(*grp))) | |
2083 | return false; | |
565ce0d3 | 2084 | } |
8d03bc50 SW |
2085 | |
2086 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2087 | zlog_debug("%s: ID (%u): %s", __func__, id, buf); | |
0be6e7d7 JU |
2088 | |
2089 | return true; | |
565ce0d3 SW |
2090 | } |
2091 | ||
f820d025 | 2092 | /** |
e9a1cd93 | 2093 | * Next hop packet encoding helper function. |
f820d025 | 2094 | * |
e9a1cd93 RZ |
2095 | * \param[in] cmd netlink command. |
2096 | * \param[in] ctx dataplane context (information snapshot). | |
2097 | * \param[out] buf buffer to hold the packet. | |
2098 | * \param[in] buflen amount of buffer bytes. | |
f820d025 | 2099 | * |
0be6e7d7 JU |
2100 | * \returns -1 on failure, 0 when the msg doesn't fit entirely in the buffer |
2101 | * otherwise the number of bytes written to buf. | |
f820d025 | 2102 | */ |
0be6e7d7 JU |
2103 | ssize_t netlink_nexthop_msg_encode(uint16_t cmd, |
2104 | const struct zebra_dplane_ctx *ctx, | |
2105 | void *buf, size_t buflen) | |
f820d025 | 2106 | { |
f820d025 SW |
2107 | struct { |
2108 | struct nlmsghdr n; | |
2109 | struct nhmsg nhm; | |
e9a1cd93 RZ |
2110 | char buf[]; |
2111 | } *req = buf; | |
f820d025 | 2112 | |
8d03bc50 SW |
2113 | mpls_lse_t out_lse[MPLS_MAX_LABELS]; |
2114 | char label_buf[256]; | |
2115 | int num_labels = 0; | |
72938edf SW |
2116 | uint32_t id = dplane_ctx_get_nhe_id(ctx); |
2117 | int type = dplane_ctx_get_nhe_type(ctx); | |
bdd085a8 MS |
2118 | struct rtattr *nest; |
2119 | uint16_t encap; | |
72938edf SW |
2120 | |
2121 | if (!id) { | |
2122 | flog_err( | |
2123 | EC_ZEBRA_NHG_FIB_UPDATE, | |
2124 | "Failed trying to update a nexthop group in the kernel that does not have an ID"); | |
2125 | return -1; | |
2126 | } | |
81505946 | 2127 | |
6c67f41f SW |
2128 | /* |
2129 | * Nothing to do if the kernel doesn't support nexthop objects or | |
2130 | * we dont want to install this type of NHG | |
2131 | */ | |
72938edf SW |
2132 | if (!kernel_nexthops_supported()) { |
2133 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG) | |
2134 | zlog_debug( | |
2135 | "%s: nhg_id %u (%s): kernel nexthops not supported, ignoring", | |
2136 | __func__, id, zebra_route_string(type)); | |
6c67f41f | 2137 | return 0; |
72938edf SW |
2138 | } |
2139 | ||
2140 | if (proto_nexthops_only() && !is_proto_nhg(id, type)) { | |
2141 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG) | |
2142 | zlog_debug( | |
2143 | "%s: nhg_id %u (%s): proto-based nexthops only, ignoring", | |
2144 | __func__, id, zebra_route_string(type)); | |
2145 | return 0; | |
2146 | } | |
6c67f41f | 2147 | |
8d03bc50 SW |
2148 | label_buf[0] = '\0'; |
2149 | ||
0be6e7d7 JU |
2150 | if (buflen < sizeof(*req)) |
2151 | return 0; | |
2152 | ||
2153 | memset(req, 0, sizeof(*req)); | |
f820d025 | 2154 | |
e9a1cd93 RZ |
2155 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)); |
2156 | req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
9a1588c4 SW |
2157 | |
2158 | if (cmd == RTM_NEWNEXTHOP) | |
e9a1cd93 | 2159 | req->n.nlmsg_flags |= NLM_F_REPLACE; |
9a1588c4 | 2160 | |
e9a1cd93 RZ |
2161 | req->n.nlmsg_type = cmd; |
2162 | req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid; | |
f820d025 | 2163 | |
e9a1cd93 | 2164 | req->nhm.nh_family = AF_UNSPEC; |
fec211ad | 2165 | /* TODO: Scope? */ |
f820d025 | 2166 | |
0be6e7d7 JU |
2167 | if (!nl_attr_put32(&req->n, buflen, NHA_ID, id)) |
2168 | return 0; | |
f820d025 SW |
2169 | |
2170 | if (cmd == RTM_NEWNEXTHOP) { | |
bf1626a6 MS |
2171 | /* |
2172 | * We distinguish between a "group", which is a collection | |
2173 | * of ids, and a singleton nexthop with an id. The | |
2174 | * group is installed as an id that just refers to a list of | |
2175 | * other ids. | |
2176 | */ | |
0be6e7d7 | 2177 | if (dplane_ctx_get_nhe_nh_grp_count(ctx)) { |
d52c949b | 2178 | if (!_netlink_nexthop_build_group( |
0be6e7d7 JU |
2179 | &req->n, buflen, id, |
2180 | dplane_ctx_get_nhe_nh_grp(ctx), | |
2181 | dplane_ctx_get_nhe_nh_grp_count(ctx))) | |
2182 | return 0; | |
2183 | } else { | |
0c8215cb SW |
2184 | const struct nexthop *nh = |
2185 | dplane_ctx_get_nhe_ng(ctx)->nexthop; | |
2186 | afi_t afi = dplane_ctx_get_nhe_afi(ctx); | |
e8b0e420 | 2187 | |
0c8215cb | 2188 | if (afi == AFI_IP) |
e9a1cd93 | 2189 | req->nhm.nh_family = AF_INET; |
0c8215cb | 2190 | else if (afi == AFI_IP6) |
e9a1cd93 | 2191 | req->nhm.nh_family = AF_INET6; |
f820d025 | 2192 | |
565ce0d3 | 2193 | switch (nh->type) { |
a6e6a6d8 | 2194 | case NEXTHOP_TYPE_IPV4: |
565ce0d3 | 2195 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
0be6e7d7 JU |
2196 | if (!nl_attr_put(&req->n, buflen, NHA_GATEWAY, |
2197 | &nh->gate.ipv4, | |
2198 | IPV4_MAX_BYTELEN)) | |
2199 | return 0; | |
565ce0d3 | 2200 | break; |
a6e6a6d8 | 2201 | case NEXTHOP_TYPE_IPV6: |
565ce0d3 | 2202 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
0be6e7d7 JU |
2203 | if (!nl_attr_put(&req->n, buflen, NHA_GATEWAY, |
2204 | &nh->gate.ipv6, | |
2205 | IPV6_MAX_BYTELEN)) | |
2206 | return 0; | |
565ce0d3 SW |
2207 | break; |
2208 | case NEXTHOP_TYPE_BLACKHOLE: | |
0be6e7d7 JU |
2209 | if (!nl_attr_put(&req->n, buflen, NHA_BLACKHOLE, |
2210 | NULL, 0)) | |
2211 | return 0; | |
8d03bc50 SW |
2212 | /* Blackhole shouldn't have anymore attributes |
2213 | */ | |
2214 | goto nexthop_done; | |
565ce0d3 SW |
2215 | case NEXTHOP_TYPE_IFINDEX: |
2216 | /* Don't need anymore info for this */ | |
2217 | break; | |
a6e6a6d8 SW |
2218 | } |
2219 | ||
2220 | if (!nh->ifindex) { | |
565ce0d3 SW |
2221 | flog_err( |
2222 | EC_ZEBRA_NHG_FIB_UPDATE, | |
2223 | "Context received for kernel nexthop update without an interface"); | |
2224 | return -1; | |
565ce0d3 SW |
2225 | } |
2226 | ||
0be6e7d7 JU |
2227 | if (!nl_attr_put32(&req->n, buflen, NHA_OIF, |
2228 | nh->ifindex)) | |
2229 | return 0; | |
8d03bc50 | 2230 | |
62d2ecb2 | 2231 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK)) |
e9a1cd93 | 2232 | req->nhm.nh_flags |= RTNH_F_ONLINK; |
62d2ecb2 | 2233 | |
8d03bc50 SW |
2234 | num_labels = |
2235 | build_label_stack(nh->nh_label, out_lse, | |
2236 | label_buf, sizeof(label_buf)); | |
2237 | ||
2238 | if (num_labels) { | |
2239 | /* Set the BoS bit */ | |
2240 | out_lse[num_labels - 1] |= | |
2241 | htonl(1 << MPLS_LS_S_SHIFT); | |
2242 | ||
2243 | /* | |
2244 | * TODO: MPLS unsupported for now in kernel. | |
2245 | */ | |
e9a1cd93 | 2246 | if (req->nhm.nh_family == AF_MPLS) |
8d03bc50 | 2247 | goto nexthop_done; |
bdd085a8 MS |
2248 | |
2249 | encap = LWTUNNEL_ENCAP_MPLS; | |
2250 | if (!nl_attr_put16(&req->n, buflen, | |
2251 | NHA_ENCAP_TYPE, encap)) | |
2252 | return 0; | |
2253 | nest = nl_attr_nest(&req->n, buflen, NHA_ENCAP); | |
2254 | if (!nest) | |
2255 | return 0; | |
2256 | if (!nl_attr_put( | |
2257 | &req->n, buflen, MPLS_IPTUNNEL_DST, | |
2258 | &out_lse, | |
2259 | num_labels * sizeof(mpls_lse_t))) | |
2260 | return 0; | |
2261 | ||
2262 | nl_attr_nest_end(&req->n, nest); | |
8d03bc50 SW |
2263 | } |
2264 | ||
bf1626a6 MS |
2265 | nexthop_done: |
2266 | ||
2267 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2c77ddee DS |
2268 | zlog_debug("%s: ID (%u): %pNHv(%d) vrf %s(%u) %s ", |
2269 | __func__, id, nh, nh->ifindex, | |
bd47f3a3 JU |
2270 | vrf_id_to_name(nh->vrf_id), |
2271 | nh->vrf_id, label_buf); | |
bdd085a8 | 2272 | } |
f820d025 | 2273 | |
bdd085a8 | 2274 | req->nhm.nh_protocol = zebra2proto(type); |
f820d025 | 2275 | |
f820d025 SW |
2276 | } else if (cmd != RTM_DELNEXTHOP) { |
2277 | flog_err( | |
2278 | EC_ZEBRA_NHG_FIB_UPDATE, | |
2279 | "Nexthop group kernel update command (%d) does not exist", | |
2280 | cmd); | |
2281 | return -1; | |
2282 | } | |
2283 | ||
9266b315 RZ |
2284 | if (IS_ZEBRA_DEBUG_KERNEL) |
2285 | zlog_debug("%s: %s, id=%u", __func__, nl_msg_type_to_str(cmd), | |
2286 | id); | |
f820d025 | 2287 | |
e9a1cd93 | 2288 | return NLMSG_ALIGN(req->n.nlmsg_len); |
f820d025 SW |
2289 | } |
2290 | ||
67e3369e JU |
2291 | static ssize_t netlink_nexthop_msg_encoder(struct zebra_dplane_ctx *ctx, |
2292 | void *buf, size_t buflen) | |
f820d025 | 2293 | { |
bf1626a6 | 2294 | enum dplane_op_e op; |
98cda54a | 2295 | int cmd = 0; |
f820d025 | 2296 | |
bf1626a6 MS |
2297 | op = dplane_ctx_get_op(ctx); |
2298 | if (op == DPLANE_OP_NH_INSTALL || op == DPLANE_OP_NH_UPDATE) | |
f820d025 | 2299 | cmd = RTM_NEWNEXTHOP; |
bf1626a6 MS |
2300 | else if (op == DPLANE_OP_NH_DELETE) |
2301 | cmd = RTM_DELNEXTHOP; | |
2302 | else { | |
2303 | flog_err(EC_ZEBRA_NHG_FIB_UPDATE, | |
2304 | "Context received for kernel nexthop update with incorrect OP code (%u)", | |
2305 | op); | |
67e3369e | 2306 | return -1; |
f820d025 SW |
2307 | } |
2308 | ||
67e3369e JU |
2309 | return netlink_nexthop_msg_encode(cmd, ctx, buf, buflen); |
2310 | } | |
2311 | ||
67e3369e JU |
2312 | enum netlink_msg_status |
2313 | netlink_put_nexthop_update_msg(struct nl_batch *bth, | |
2314 | struct zebra_dplane_ctx *ctx) | |
2315 | { | |
e9a1cd93 RZ |
2316 | /* Nothing to do if the kernel doesn't support nexthop objects */ |
2317 | if (!kernel_nexthops_supported()) | |
67e3369e | 2318 | return FRR_NETLINK_SUCCESS; |
e9a1cd93 | 2319 | |
67e3369e JU |
2320 | return netlink_batch_add_msg(bth, ctx, netlink_nexthop_msg_encoder, |
2321 | false); | |
2322 | } | |
f820d025 | 2323 | |
67e3369e JU |
2324 | static ssize_t netlink_newroute_msg_encoder(struct zebra_dplane_ctx *ctx, |
2325 | void *buf, size_t buflen) | |
2326 | { | |
2327 | return netlink_route_multipath_msg_encode(RTM_NEWROUTE, ctx, buf, | |
2328 | buflen, false, false); | |
f820d025 SW |
2329 | } |
2330 | ||
67e3369e JU |
2331 | static ssize_t netlink_delroute_msg_encoder(struct zebra_dplane_ctx *ctx, |
2332 | void *buf, size_t buflen) | |
2333 | { | |
2334 | return netlink_route_multipath_msg_encode(RTM_DELROUTE, ctx, buf, | |
2335 | buflen, false, false); | |
2336 | } | |
2337 | ||
2338 | enum netlink_msg_status | |
2339 | netlink_put_route_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx) | |
7cdb1a84 | 2340 | { |
67e3369e | 2341 | int cmd; |
7cdb1a84 MS |
2342 | const struct prefix *p = dplane_ctx_get_dest(ctx); |
2343 | ||
2344 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) { | |
2345 | cmd = RTM_DELROUTE; | |
2346 | } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_INSTALL) { | |
2347 | cmd = RTM_NEWROUTE; | |
2348 | } else if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_UPDATE) { | |
2349 | ||
2350 | if (p->family == AF_INET || v6_rr_semantics) { | |
2351 | /* Single 'replace' operation */ | |
fe5f21af DS |
2352 | |
2353 | /* | |
2354 | * With route replace semantics in place | |
2355 | * for v4 routes and the new route is a system | |
2356 | * route we do not install anything. | |
2357 | * The problem here is that the new system | |
2358 | * route should cause us to withdraw from | |
2359 | * the kernel the old non-system route | |
2360 | */ | |
67e3369e JU |
2361 | if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx)) |
2362 | && !RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx))) | |
2363 | netlink_batch_add_msg( | |
2364 | bth, ctx, netlink_delroute_msg_encoder, | |
2365 | true); | |
7cdb1a84 MS |
2366 | } else { |
2367 | /* | |
2368 | * So v6 route replace semantics are not in | |
2369 | * the kernel at this point as I understand it. | |
2370 | * so let's do a delete then an add. | |
2371 | * In the future once v6 route replace semantics | |
2372 | * are in we can figure out what to do here to | |
2373 | * allow working with old and new kernels. | |
2374 | * | |
2375 | * I'm also intentionally ignoring the failure case | |
2376 | * of the route delete. If that happens yeah we're | |
2377 | * screwed. | |
2378 | */ | |
67e3369e JU |
2379 | if (!RSYSTEM_ROUTE(dplane_ctx_get_old_type(ctx))) |
2380 | netlink_batch_add_msg( | |
2381 | bth, ctx, netlink_delroute_msg_encoder, | |
2382 | true); | |
7cdb1a84 MS |
2383 | } |
2384 | ||
67e3369e JU |
2385 | cmd = RTM_NEWROUTE; |
2386 | } else | |
2387 | return FRR_NETLINK_ERROR; | |
7cdb1a84 | 2388 | |
67e3369e JU |
2389 | if (RSYSTEM_ROUTE(dplane_ctx_get_type(ctx))) |
2390 | return FRR_NETLINK_SUCCESS; | |
0be6e7d7 | 2391 | |
67e3369e JU |
2392 | return netlink_batch_add_msg(bth, ctx, |
2393 | cmd == RTM_NEWROUTE | |
2394 | ? netlink_newroute_msg_encoder | |
2395 | : netlink_delroute_msg_encoder, | |
2396 | false); | |
2397 | } | |
7cdb1a84 | 2398 | |
d9f5b2f5 SW |
2399 | /** |
2400 | * netlink_nexthop_process_nh() - Parse the gatway/if info from a new nexthop | |
2401 | * | |
2402 | * @tb: Netlink RTA data | |
2403 | * @family: Address family in the nhmsg | |
8c0a24c1 | 2404 | * @ifp: Interface connected - this should be NULL, we fill it in |
d9f5b2f5 SW |
2405 | * @ns_id: Namspace id |
2406 | * | |
2407 | * Return: New nexthop | |
2408 | */ | |
e22e8001 SW |
2409 | static struct nexthop netlink_nexthop_process_nh(struct rtattr **tb, |
2410 | unsigned char family, | |
2411 | struct interface **ifp, | |
2412 | ns_id_t ns_id) | |
d9f5b2f5 | 2413 | { |
e22e8001 | 2414 | struct nexthop nh = {}; |
d9f5b2f5 | 2415 | void *gate = NULL; |
8e401b25 | 2416 | enum nexthop_types_t type = 0; |
e22e8001 SW |
2417 | int if_index = 0; |
2418 | size_t sz = 0; | |
7134ba70 | 2419 | struct interface *ifp_lookup; |
d9f5b2f5 SW |
2420 | |
2421 | if_index = *(int *)RTA_DATA(tb[NHA_OIF]); | |
2422 | ||
8e401b25 | 2423 | |
d9f5b2f5 SW |
2424 | if (tb[NHA_GATEWAY]) { |
2425 | switch (family) { | |
2426 | case AF_INET: | |
8e401b25 | 2427 | type = NEXTHOP_TYPE_IPV4_IFINDEX; |
d9f5b2f5 SW |
2428 | sz = 4; |
2429 | break; | |
2430 | case AF_INET6: | |
8e401b25 | 2431 | type = NEXTHOP_TYPE_IPV6_IFINDEX; |
d9f5b2f5 SW |
2432 | sz = 16; |
2433 | break; | |
2434 | default: | |
2435 | flog_warn( | |
2436 | EC_ZEBRA_BAD_NHG_MESSAGE, | |
c4239c05 | 2437 | "Nexthop gateway with bad address family (%d) received from kernel", |
d9f5b2f5 | 2438 | family); |
e22e8001 | 2439 | return nh; |
d9f5b2f5 SW |
2440 | } |
2441 | gate = RTA_DATA(tb[NHA_GATEWAY]); | |
e22e8001 | 2442 | } else |
8e401b25 | 2443 | type = NEXTHOP_TYPE_IFINDEX; |
d9f5b2f5 | 2444 | |
8e401b25 | 2445 | if (type) |
e22e8001 | 2446 | nh.type = type; |
8e401b25 SW |
2447 | |
2448 | if (gate) | |
e22e8001 | 2449 | memcpy(&(nh.gate), gate, sz); |
8e401b25 SW |
2450 | |
2451 | if (if_index) | |
e22e8001 | 2452 | nh.ifindex = if_index; |
8e401b25 | 2453 | |
7134ba70 DS |
2454 | ifp_lookup = |
2455 | if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), nh.ifindex); | |
2456 | ||
e22e8001 | 2457 | if (ifp) |
7134ba70 DS |
2458 | *ifp = ifp_lookup; |
2459 | if (ifp_lookup) | |
2460 | nh.vrf_id = ifp_lookup->vrf_id; | |
e22e8001 | 2461 | else { |
d9f5b2f5 SW |
2462 | flog_warn( |
2463 | EC_ZEBRA_UNKNOWN_INTERFACE, | |
2464 | "%s: Unknown nexthop interface %u received, defaulting to VRF_DEFAULT", | |
15569c58 | 2465 | __func__, nh.ifindex); |
d9f5b2f5 | 2466 | |
e22e8001 | 2467 | nh.vrf_id = VRF_DEFAULT; |
d9f5b2f5 SW |
2468 | } |
2469 | ||
2470 | if (tb[NHA_ENCAP] && tb[NHA_ENCAP_TYPE]) { | |
2471 | uint16_t encap_type = *(uint16_t *)RTA_DATA(tb[NHA_ENCAP_TYPE]); | |
2472 | int num_labels = 0; | |
6e728764 | 2473 | |
d9f5b2f5 SW |
2474 | mpls_label_t labels[MPLS_MAX_LABELS] = {0}; |
2475 | ||
e22e8001 | 2476 | if (encap_type == LWTUNNEL_ENCAP_MPLS) |
d9f5b2f5 | 2477 | num_labels = parse_encap_mpls(tb[NHA_ENCAP], labels); |
d9f5b2f5 | 2478 | |
e22e8001 SW |
2479 | if (num_labels) |
2480 | nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels, | |
d9f5b2f5 | 2481 | labels); |
d9f5b2f5 SW |
2482 | } |
2483 | ||
2484 | return nh; | |
2485 | } | |
2486 | ||
85f5e761 | 2487 | static int netlink_nexthop_process_group(struct rtattr **tb, |
5a935f79 | 2488 | struct nh_grp *z_grp, int z_grp_size) |
d9f5b2f5 | 2489 | { |
e22e8001 SW |
2490 | uint8_t count = 0; |
2491 | /* linux/nexthop.h group struct */ | |
d9f5b2f5 SW |
2492 | struct nexthop_grp *n_grp = NULL; |
2493 | ||
85f5e761 | 2494 | n_grp = (struct nexthop_grp *)RTA_DATA(tb[NHA_GROUP]); |
d9f5b2f5 SW |
2495 | count = (RTA_PAYLOAD(tb[NHA_GROUP]) / sizeof(*n_grp)); |
2496 | ||
2497 | if (!count || (count * sizeof(*n_grp)) != RTA_PAYLOAD(tb[NHA_GROUP])) { | |
2498 | flog_warn(EC_ZEBRA_BAD_NHG_MESSAGE, | |
2499 | "Invalid nexthop group received from the kernel"); | |
85f5e761 | 2500 | return count; |
d9f5b2f5 SW |
2501 | } |
2502 | ||
5a935f79 | 2503 | for (int i = 0; ((i < count) && (i < z_grp_size)); i++) { |
e22e8001 | 2504 | z_grp[i].id = n_grp[i].id; |
df7fb580 | 2505 | z_grp[i].weight = n_grp[i].weight + 1; |
85f5e761 | 2506 | } |
d9f5b2f5 SW |
2507 | return count; |
2508 | } | |
2509 | ||
2510 | /** | |
2511 | * netlink_nexthop_change() - Read in change about nexthops from the kernel | |
2512 | * | |
2513 | * @h: Netlink message header | |
2514 | * @ns_id: Namspace id | |
2515 | * @startup: Are we reading under startup conditions? | |
2516 | * | |
2517 | * Return: Result status | |
2518 | */ | |
2519 | int netlink_nexthop_change(struct nlmsghdr *h, ns_id_t ns_id, int startup) | |
2520 | { | |
2521 | int len; | |
2522 | /* nexthop group id */ | |
2523 | uint32_t id; | |
2524 | unsigned char family; | |
38e40db1 | 2525 | int type; |
e8b0e420 | 2526 | afi_t afi = AFI_UNSPEC; |
946de1b9 | 2527 | vrf_id_t vrf_id = VRF_DEFAULT; |
8c0a24c1 | 2528 | struct interface *ifp = NULL; |
d9f5b2f5 | 2529 | struct nhmsg *nhm = NULL; |
e22e8001 SW |
2530 | struct nexthop nh = {}; |
2531 | struct nh_grp grp[MULTIPATH_NUM] = {}; | |
85f5e761 | 2532 | /* Count of nexthops in group array */ |
e22e8001 | 2533 | uint8_t grp_count = 0; |
e22e8001 | 2534 | struct rtattr *tb[NHA_MAX + 1] = {}; |
d9f5b2f5 | 2535 | |
d9f5b2f5 SW |
2536 | nhm = NLMSG_DATA(h); |
2537 | ||
88cafda7 DS |
2538 | if (ns_id) |
2539 | vrf_id = ns_id; | |
2540 | ||
d9f5b2f5 SW |
2541 | if (startup && h->nlmsg_type != RTM_NEWNEXTHOP) |
2542 | return 0; | |
2543 | ||
2544 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct nhmsg)); | |
2545 | if (len < 0) { | |
2546 | zlog_warn( | |
2547 | "%s: Message received from netlink is of a broken size %d %zu", | |
15569c58 | 2548 | __func__, h->nlmsg_len, |
d9f5b2f5 SW |
2549 | (size_t)NLMSG_LENGTH(sizeof(struct nhmsg))); |
2550 | return -1; | |
2551 | } | |
2552 | ||
d9f5b2f5 SW |
2553 | netlink_parse_rtattr(tb, NHA_MAX, RTM_NHA(nhm), len); |
2554 | ||
2555 | ||
2556 | if (!tb[NHA_ID]) { | |
2557 | flog_warn( | |
2558 | EC_ZEBRA_BAD_NHG_MESSAGE, | |
2559 | "Nexthop group without an ID received from the kernel"); | |
2560 | return -1; | |
2561 | } | |
2562 | ||
2563 | /* We use the ID key'd nhg table for kernel updates */ | |
2564 | id = *((uint32_t *)RTA_DATA(tb[NHA_ID])); | |
d9f5b2f5 | 2565 | |
506efd37 AK |
2566 | if (zebra_evpn_mh_is_fdb_nh(id)) { |
2567 | /* If this is a L2 NH just ignore it */ | |
2568 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) { | |
2569 | zlog_debug("Ignore kernel update (%u) for fdb-nh 0x%x", | |
2570 | h->nlmsg_type, id); | |
2571 | } | |
2572 | return 0; | |
2573 | } | |
2574 | ||
e8b0e420 | 2575 | family = nhm->nh_family; |
e8b0e420 SW |
2576 | afi = family2afi(family); |
2577 | ||
38e40db1 SW |
2578 | type = proto2zebra(nhm->nh_protocol, 0, true); |
2579 | ||
fdee485a SW |
2580 | if (IS_ZEBRA_DEBUG_KERNEL) |
2581 | zlog_debug("%s ID (%u) %s NS %u", | |
2582 | nl_msg_type_to_str(h->nlmsg_type), id, | |
2583 | nl_family_to_str(family), ns_id); | |
2584 | ||
2585 | ||
d9f5b2f5 SW |
2586 | if (h->nlmsg_type == RTM_NEWNEXTHOP) { |
2587 | if (tb[NHA_GROUP]) { | |
2588 | /** | |
2589 | * If this is a group message its only going to have | |
2590 | * an array of nexthop IDs associated with it | |
2591 | */ | |
5a935f79 SW |
2592 | grp_count = netlink_nexthop_process_group( |
2593 | tb, grp, array_size(grp)); | |
85f5e761 SW |
2594 | } else { |
2595 | if (tb[NHA_BLACKHOLE]) { | |
2596 | /** | |
2597 | * This nexthop is just for blackhole-ing | |
2598 | * traffic, it should not have an OIF, GATEWAY, | |
2599 | * or ENCAP | |
2600 | */ | |
e22e8001 SW |
2601 | nh.type = NEXTHOP_TYPE_BLACKHOLE; |
2602 | nh.bh_type = BLACKHOLE_UNSPEC; | |
2603 | } else if (tb[NHA_OIF]) | |
85f5e761 SW |
2604 | /** |
2605 | * This is a true new nexthop, so we need | |
2606 | * to parse the gateway and device info | |
2607 | */ | |
2608 | nh = netlink_nexthop_process_nh(tb, family, | |
2609 | &ifp, ns_id); | |
e22e8001 SW |
2610 | else { |
2611 | ||
8e401b25 SW |
2612 | flog_warn( |
2613 | EC_ZEBRA_BAD_NHG_MESSAGE, | |
2614 | "Invalid Nexthop message received from the kernel with ID (%u)", | |
2615 | id); | |
2616 | return -1; | |
2617 | } | |
e22e8001 SW |
2618 | SET_FLAG(nh.flags, NEXTHOP_FLAG_ACTIVE); |
2619 | if (nhm->nh_flags & RTNH_F_ONLINK) | |
2620 | SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK); | |
2621 | vrf_id = nh.vrf_id; | |
d9f5b2f5 SW |
2622 | } |
2623 | ||
38e40db1 SW |
2624 | if (zebra_nhg_kernel_find(id, &nh, grp, grp_count, vrf_id, afi, |
2625 | type, startup)) | |
e22e8001 | 2626 | return -1; |
8e401b25 | 2627 | |
9a1588c4 | 2628 | } else if (h->nlmsg_type == RTM_DELNEXTHOP) |
88cafda7 | 2629 | zebra_nhg_kernel_del(id, vrf_id); |
d9f5b2f5 | 2630 | |
d9f5b2f5 SW |
2631 | return 0; |
2632 | } | |
2633 | ||
2634 | /** | |
2635 | * netlink_request_nexthop() - Request nextop information from the kernel | |
2636 | * @zns: Zebra namespace | |
2637 | * @family: AF_* netlink family | |
2638 | * @type: RTM_* route type | |
2639 | * | |
2640 | * Return: Result status | |
2641 | */ | |
2642 | static int netlink_request_nexthop(struct zebra_ns *zns, int family, int type) | |
2643 | { | |
2644 | struct { | |
2645 | struct nlmsghdr n; | |
2646 | struct nhmsg nhm; | |
2647 | } req; | |
2648 | ||
2649 | /* Form the request, specifying filter (rtattr) if needed. */ | |
2650 | memset(&req, 0, sizeof(req)); | |
2651 | req.n.nlmsg_type = type; | |
2652 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; | |
2653 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)); | |
2654 | req.nhm.nh_family = family; | |
2655 | ||
fd3f8e52 | 2656 | return netlink_request(&zns->netlink_cmd, &req); |
d9f5b2f5 SW |
2657 | } |
2658 | ||
7d5bb02b | 2659 | |
d9f5b2f5 SW |
2660 | /** |
2661 | * netlink_nexthop_read() - Nexthop read function using netlink interface | |
2662 | * | |
2663 | * @zns: Zebra name space | |
2664 | * | |
2665 | * Return: Result status | |
2666 | * Only called at bootstrap time. | |
2667 | */ | |
2668 | int netlink_nexthop_read(struct zebra_ns *zns) | |
2669 | { | |
2670 | int ret; | |
2671 | struct zebra_dplane_info dp_info; | |
2672 | ||
2673 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
2674 | ||
2675 | /* Get nexthop objects */ | |
2676 | ret = netlink_request_nexthop(zns, AF_UNSPEC, RTM_GETNEXTHOP); | |
2677 | if (ret < 0) | |
2678 | return ret; | |
2679 | ret = netlink_parse_info(netlink_nexthop_change, &zns->netlink_cmd, | |
2680 | &dp_info, 0, 1); | |
81505946 SW |
2681 | |
2682 | if (!ret) | |
2683 | /* If we succesfully read in nexthop objects, | |
2684 | * this kernel must support them. | |
2685 | */ | |
2686 | supports_nh = true; | |
7c99d51b MS |
2687 | |
2688 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_NHG) | |
2689 | zlog_debug("Nexthop objects %ssupported on this kernel", | |
2690 | supports_nh ? "" : "not "); | |
81505946 | 2691 | |
60e0eaee | 2692 | return ret; |
d9f5b2f5 SW |
2693 | } |
2694 | ||
2695 | ||
05657ec2 PG |
2696 | int kernel_neigh_update(int add, int ifindex, void *addr, char *lla, int llalen, |
2697 | ns_id_t ns_id, uint8_t family, bool permanent) | |
6b8a5694 | 2698 | { |
d62a17ae | 2699 | return netlink_neigh_update(add ? RTM_NEWNEIGH : RTM_DELNEIGH, ifindex, |
05657ec2 PG |
2700 | addr, lla, llalen, ns_id, family, permanent, |
2701 | RTPROT_ZEBRA); | |
6b8a5694 | 2702 | } |
718e3744 | 2703 | |
340845e2 | 2704 | /** |
0be6e7d7 JU |
2705 | * netlink_neigh_update_msg_encode() - Common helper api for encoding |
2706 | * evpn neighbor update as netlink messages using dataplane context object. | |
bbd4285b | 2707 | * Here, a neighbor refers to a bridge forwarding database entry for |
2708 | * either unicast forwarding or head-end replication or an IP neighbor | |
2709 | * entry. | |
340845e2 JU |
2710 | * @ctx: Dataplane context |
2711 | * @cmd: Netlink command (RTM_NEWNEIGH or RTM_DELNEIGH) | |
0a27a2fe PG |
2712 | * @lla: A pointer to neighbor cache link layer address |
2713 | * @llalen: Length of the pointer to neighbor cache link layer | |
2714 | * address | |
340845e2 | 2715 | * @ip: A neighbor cache n/w layer destination address |
bbd4285b | 2716 | * In the case of bridge FDB, this represnts the remote |
2717 | * VTEP IP. | |
340845e2 JU |
2718 | * @replace_obj: Whether NEW request should replace existing object or |
2719 | * add to the end of the list | |
2720 | * @family: AF_* netlink family | |
2721 | * @type: RTN_* route type | |
2722 | * @flags: NTF_* flags | |
2723 | * @state: NUD_* states | |
d4d4ec1c RZ |
2724 | * @data: data buffer pointer |
2725 | * @datalen: total amount of data buffer space | |
0a27a2fe | 2726 | * @protocol: protocol information |
340845e2 | 2727 | * |
0be6e7d7 JU |
2728 | * Return: 0 when the msg doesn't fit entirely in the buffer |
2729 | * otherwise the number of bytes written to buf. | |
13d60d35 | 2730 | */ |
0be6e7d7 | 2731 | static ssize_t netlink_neigh_update_msg_encode( |
0a27a2fe PG |
2732 | const struct zebra_dplane_ctx *ctx, int cmd, const void *lla, |
2733 | int llalen, const struct ipaddr *ip, bool replace_obj, uint8_t family, | |
2734 | uint8_t type, uint8_t flags, uint16_t state, uint32_t nhg_id, bool nfy, | |
ccd187cd | 2735 | uint8_t nfy_flags, bool ext, uint32_t ext_flags, void *data, |
0a27a2fe | 2736 | size_t datalen, uint8_t protocol) |
13d60d35 | 2737 | { |
d62a17ae | 2738 | struct { |
2739 | struct nlmsghdr n; | |
2740 | struct ndmsg ndm; | |
d4d4ec1c RZ |
2741 | char buf[]; |
2742 | } *req = data; | |
340845e2 JU |
2743 | int ipa_len; |
2744 | enum dplane_op_e op; | |
d62a17ae | 2745 | |
0be6e7d7 JU |
2746 | if (datalen < sizeof(*req)) |
2747 | return 0; | |
45c80fbd | 2748 | memset(req, 0, sizeof(*req)); |
d62a17ae | 2749 | |
340845e2 JU |
2750 | op = dplane_ctx_get_op(ctx); |
2751 | ||
d4d4ec1c RZ |
2752 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); |
2753 | req->n.nlmsg_flags = NLM_F_REQUEST; | |
d62a17ae | 2754 | if (cmd == RTM_NEWNEIGH) |
d4d4ec1c | 2755 | req->n.nlmsg_flags |= |
340845e2 JU |
2756 | NLM_F_CREATE |
2757 | | (replace_obj ? NLM_F_REPLACE : NLM_F_APPEND); | |
d4d4ec1c RZ |
2758 | req->n.nlmsg_type = cmd; |
2759 | req->ndm.ndm_family = family; | |
2760 | req->ndm.ndm_type = type; | |
2761 | req->ndm.ndm_state = state; | |
2762 | req->ndm.ndm_flags = flags; | |
2763 | req->ndm.ndm_ifindex = dplane_ctx_get_ifindex(ctx); | |
d62a17ae | 2764 | |
45c80fbd | 2765 | if (!nl_attr_put(&req->n, datalen, NDA_PROTOCOL, &protocol, |
0be6e7d7 JU |
2766 | sizeof(protocol))) |
2767 | return 0; | |
2768 | ||
0a27a2fe PG |
2769 | if (lla) { |
2770 | if (!nl_attr_put(&req->n, datalen, NDA_LLADDR, lla, llalen)) | |
0be6e7d7 JU |
2771 | return 0; |
2772 | } | |
13d60d35 | 2773 | |
f188e68e | 2774 | if (nfy) { |
4bcdb608 NA |
2775 | struct rtattr *nest; |
2776 | ||
2777 | nest = nl_attr_nest(&req->n, datalen, | |
2778 | NDA_FDB_EXT_ATTRS | NLA_F_NESTED); | |
2779 | if (!nest) | |
2780 | return 0; | |
2781 | ||
2782 | if (!nl_attr_put(&req->n, datalen, NFEA_ACTIVITY_NOTIFY, | |
2783 | &nfy_flags, sizeof(nfy_flags))) | |
f188e68e | 2784 | return 0; |
4bcdb608 NA |
2785 | if (!nl_attr_put(&req->n, datalen, NFEA_DONT_REFRESH, NULL, 0)) |
2786 | return 0; | |
2787 | ||
2788 | nl_attr_nest_end(&req->n, nest); | |
f188e68e | 2789 | } |
506efd37 | 2790 | |
4bcdb608 | 2791 | |
ccd187cd AK |
2792 | if (ext) { |
2793 | if (!nl_attr_put(&req->n, datalen, NDA_EXT_FLAGS, &ext_flags, | |
2794 | sizeof(ext_flags))) | |
2795 | return 0; | |
2796 | } | |
2797 | ||
80e19eb7 AK |
2798 | if (nhg_id) { |
2799 | if (!nl_attr_put32(&req->n, datalen, NDA_NH_ID, nhg_id)) | |
2800 | return 0; | |
2801 | } else { | |
2802 | ipa_len = | |
2803 | IS_IPADDR_V4(ip) ? IPV4_MAX_BYTELEN : IPV6_MAX_BYTELEN; | |
2804 | if (!nl_attr_put(&req->n, datalen, NDA_DST, &ip->ip.addr, | |
2805 | ipa_len)) | |
2806 | return 0; | |
2807 | } | |
340845e2 JU |
2808 | |
2809 | if (op == DPLANE_OP_MAC_INSTALL || op == DPLANE_OP_MAC_DELETE) { | |
2810 | vlanid_t vid = dplane_ctx_mac_get_vlan(ctx); | |
13d60d35 | 2811 | |
0be6e7d7 JU |
2812 | if (vid > 0) { |
2813 | if (!nl_attr_put16(&req->n, datalen, NDA_VLAN, vid)) | |
2814 | return 0; | |
2815 | } | |
13d60d35 | 2816 | |
0be6e7d7 JU |
2817 | if (!nl_attr_put32(&req->n, datalen, NDA_MASTER, |
2818 | dplane_ctx_mac_get_br_ifindex(ctx))) | |
2819 | return 0; | |
340845e2 | 2820 | } |
13d60d35 | 2821 | |
d4d4ec1c | 2822 | return NLMSG_ALIGN(req->n.nlmsg_len); |
13d60d35 | 2823 | } |
2824 | ||
340845e2 JU |
2825 | /* |
2826 | * Add remote VTEP to the flood list for this VxLAN interface (VNI). This | |
2827 | * is done by adding an FDB entry with a MAC of 00:00:00:00:00:00. | |
2828 | */ | |
67e3369e JU |
2829 | static ssize_t |
2830 | netlink_vxlan_flood_update_ctx(const struct zebra_dplane_ctx *ctx, int cmd, | |
2831 | void *buf, size_t buflen) | |
340845e2 JU |
2832 | { |
2833 | struct ethaddr dst_mac = {.octet = {0}}; | |
d4d4ec1c | 2834 | |
67e3369e | 2835 | return netlink_neigh_update_msg_encode( |
0a27a2fe PG |
2836 | ctx, cmd, (const void *)&dst_mac, ETH_ALEN, |
2837 | dplane_ctx_neigh_get_ipaddr(ctx), false, PF_BRIDGE, 0, NTF_SELF, | |
2838 | (NUD_NOARP | NUD_PERMANENT), 0 /*nhg*/, false /*nfy*/, | |
2839 | 0 /*nfy_flags*/, false /*ext*/, 0 /*ext_flags*/, buf, buflen, | |
2840 | zebra2proto(dplane_ctx_get_type(ctx))); | |
340845e2 JU |
2841 | } |
2842 | ||
2232a77c | 2843 | #ifndef NDA_RTA |
d62a17ae | 2844 | #define NDA_RTA(r) \ |
2845 | ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg)))) | |
2232a77c | 2846 | #endif |
2847 | ||
2414abd3 | 2848 | static int netlink_macfdb_change(struct nlmsghdr *h, int len, ns_id_t ns_id) |
2232a77c | 2849 | { |
d62a17ae | 2850 | struct ndmsg *ndm; |
2851 | struct interface *ifp; | |
2852 | struct zebra_if *zif; | |
d62a17ae | 2853 | struct rtattr *tb[NDA_MAX + 1]; |
2854 | struct interface *br_if; | |
2855 | struct ethaddr mac; | |
2856 | vlanid_t vid = 0; | |
4b3f26f4 | 2857 | struct in_addr vtep_ip; |
d62a17ae | 2858 | int vid_present = 0, dst_present = 0; |
d62a17ae | 2859 | char vid_buf[20]; |
2860 | char dst_buf[30]; | |
a37f4598 | 2861 | bool sticky; |
f188e68e AK |
2862 | bool local_inactive = false; |
2863 | bool dp_static = false; | |
2864 | uint32_t nhg_id = 0; | |
d62a17ae | 2865 | |
2866 | ndm = NLMSG_DATA(h); | |
2867 | ||
2853fed6 | 2868 | /* We only process macfdb notifications if EVPN is enabled */ |
2869 | if (!is_evpn_enabled()) | |
2870 | return 0; | |
2871 | ||
4b3f26f4 | 2872 | /* Parse attributes and extract fields of interest. Do basic |
2873 | * validation of the fields. | |
2874 | */ | |
2875 | memset(tb, 0, sizeof tb); | |
4bcdb608 NA |
2876 | netlink_parse_rtattr_flags(tb, NDA_MAX, NDA_RTA(ndm), len, |
2877 | NLA_F_NESTED); | |
d62a17ae | 2878 | |
2879 | if (!tb[NDA_LLADDR]) { | |
28bd0652 | 2880 | if (IS_ZEBRA_DEBUG_KERNEL) |
4b3f26f4 | 2881 | zlog_debug("%s AF_BRIDGE IF %u - no LLADDR", |
28bd0652 | 2882 | nl_msg_type_to_str(h->nlmsg_type), |
4b3f26f4 | 2883 | ndm->ndm_ifindex); |
d62a17ae | 2884 | return 0; |
2885 | } | |
2886 | ||
ff8b7eb8 | 2887 | if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) { |
28bd0652 DS |
2888 | if (IS_ZEBRA_DEBUG_KERNEL) |
2889 | zlog_debug( | |
4b3f26f4 | 2890 | "%s AF_BRIDGE IF %u - LLADDR is not MAC, len %lu", |
2891 | nl_msg_type_to_str(h->nlmsg_type), ndm->ndm_ifindex, | |
28bd0652 | 2892 | (unsigned long)RTA_PAYLOAD(tb[NDA_LLADDR])); |
d62a17ae | 2893 | return 0; |
2894 | } | |
2895 | ||
ff8b7eb8 | 2896 | memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN); |
d62a17ae | 2897 | |
2898 | if ((NDA_VLAN <= NDA_MAX) && tb[NDA_VLAN]) { | |
2899 | vid_present = 1; | |
d7c0a89a | 2900 | vid = *(uint16_t *)RTA_DATA(tb[NDA_VLAN]); |
772270f3 | 2901 | snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid); |
d62a17ae | 2902 | } |
2903 | ||
2904 | if (tb[NDA_DST]) { | |
2905 | /* TODO: Only IPv4 supported now. */ | |
2906 | dst_present = 1; | |
4b3f26f4 | 2907 | memcpy(&vtep_ip.s_addr, RTA_DATA(tb[NDA_DST]), |
d62a17ae | 2908 | IPV4_MAX_BYTELEN); |
9bcef951 MS |
2909 | snprintfrr(dst_buf, sizeof(dst_buf), " dst %pI4", |
2910 | &vtep_ip); | |
d62a17ae | 2911 | } |
2912 | ||
f188e68e AK |
2913 | if (tb[NDA_NH_ID]) |
2914 | nhg_id = *(uint32_t *)RTA_DATA(tb[NDA_NH_ID]); | |
2915 | ||
2916 | if (ndm->ndm_state & NUD_STALE) | |
2917 | local_inactive = true; | |
2918 | ||
4bcdb608 NA |
2919 | if (tb[NDA_FDB_EXT_ATTRS]) { |
2920 | struct rtattr *attr = tb[NDA_FDB_EXT_ATTRS]; | |
2921 | struct rtattr *nfea_tb[NFEA_MAX + 1] = {0}; | |
2922 | ||
2923 | netlink_parse_rtattr_nested(nfea_tb, NFEA_MAX, attr); | |
2924 | if (nfea_tb[NFEA_ACTIVITY_NOTIFY]) { | |
2925 | uint8_t nfy_flags; | |
f188e68e | 2926 | |
4bcdb608 NA |
2927 | nfy_flags = *(uint8_t *)RTA_DATA( |
2928 | nfea_tb[NFEA_ACTIVITY_NOTIFY]); | |
2929 | if (nfy_flags & FDB_NOTIFY_BIT) | |
2930 | dp_static = true; | |
2931 | if (nfy_flags & FDB_NOTIFY_INACTIVE_BIT) | |
2932 | local_inactive = true; | |
2933 | } | |
f188e68e AK |
2934 | } |
2935 | ||
d62a17ae | 2936 | if (IS_ZEBRA_DEBUG_KERNEL) |
ef7b8be4 | 2937 | zlog_debug("Rx %s AF_BRIDGE IF %u%s st 0x%x fl 0x%x MAC %pEA%s nhg %d", |
d62a17ae | 2938 | nl_msg_type_to_str(h->nlmsg_type), |
d62a17ae | 2939 | ndm->ndm_ifindex, vid_present ? vid_buf : "", |
ef7b8be4 | 2940 | ndm->ndm_state, ndm->ndm_flags, &mac, |
f188e68e | 2941 | dst_present ? dst_buf : "", nhg_id); |
d62a17ae | 2942 | |
4b3f26f4 | 2943 | /* The interface should exist. */ |
2944 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), | |
2945 | ndm->ndm_ifindex); | |
2946 | if (!ifp || !ifp->info) | |
2947 | return 0; | |
2948 | ||
2949 | /* The interface should be something we're interested in. */ | |
2950 | if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp)) | |
2951 | return 0; | |
2952 | ||
2953 | zif = (struct zebra_if *)ifp->info; | |
2954 | if ((br_if = zif->brslave_info.br_if) == NULL) { | |
2955 | if (IS_ZEBRA_DEBUG_KERNEL) | |
2956 | zlog_debug( | |
2957 | "%s AF_BRIDGE IF %s(%u) brIF %u - no bridge master", | |
2958 | nl_msg_type_to_str(h->nlmsg_type), ifp->name, | |
2959 | ndm->ndm_ifindex, | |
2960 | zif->brslave_info.bridge_ifindex); | |
2961 | return 0; | |
2962 | } | |
2963 | ||
f188e68e | 2964 | sticky = !!(ndm->ndm_flags & NTF_STICKY); |
4b3f26f4 | 2965 | |
28bd0652 DS |
2966 | if (filter_vlan && vid != filter_vlan) { |
2967 | if (IS_ZEBRA_DEBUG_KERNEL) | |
d6951e5e | 2968 | zlog_debug(" Filtered due to filter vlan: %d", |
28bd0652 | 2969 | filter_vlan); |
d62a17ae | 2970 | return 0; |
28bd0652 | 2971 | } |
d62a17ae | 2972 | |
2973 | /* If add or update, do accordingly if learnt on a "local" interface; if | |
2974 | * the notification is over VxLAN, this has to be related to | |
2975 | * multi-homing, | |
2976 | * so perform an implicit delete of any local entry (if it exists). | |
2977 | */ | |
2978 | if (h->nlmsg_type == RTM_NEWNEIGH) { | |
4b3f26f4 | 2979 | /* Drop "permanent" entries. */ |
2980 | if (ndm->ndm_state & NUD_PERMANENT) { | |
2981 | if (IS_ZEBRA_DEBUG_KERNEL) | |
d6951e5e DL |
2982 | zlog_debug( |
2983 | " Dropping entry because of NUD_PERMANENT"); | |
2984 | return 0; | |
4b3f26f4 | 2985 | } |
2986 | ||
d62a17ae | 2987 | if (IS_ZEBRA_IF_VXLAN(ifp)) |
15400f95 AK |
2988 | return zebra_vxlan_dp_network_mac_add( |
2989 | ifp, br_if, &mac, vid, nhg_id, sticky, | |
2990 | !!(ndm->ndm_flags & NTF_EXT_LEARNED)); | |
d62a17ae | 2991 | |
2992 | return zebra_vxlan_local_mac_add_update(ifp, br_if, &mac, vid, | |
f188e68e | 2993 | sticky, local_inactive, dp_static); |
d62a17ae | 2994 | } |
2995 | ||
2996 | /* This is a delete notification. | |
4b3f26f4 | 2997 | * Ignore the notification with IP dest as it may just signify that the |
2998 | * MAC has moved from remote to local. The exception is the special | |
2999 | * all-zeros MAC that represents the BUM flooding entry; we may have | |
3000 | * to readd it. Otherwise, | |
d62a17ae | 3001 | * 1. For a MAC over VxLan, check if it needs to be refreshed(readded) |
3002 | * 2. For a MAC over "local" interface, delete the mac | |
3003 | * Note: We will get notifications from both bridge driver and VxLAN | |
3004 | * driver. | |
d62a17ae | 3005 | */ |
f188e68e AK |
3006 | if (nhg_id) |
3007 | return 0; | |
3008 | ||
28bd0652 | 3009 | if (dst_present) { |
4b3f26f4 | 3010 | u_char zero_mac[6] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
3011 | ||
3012 | if (!memcmp(zero_mac, mac.octet, ETH_ALEN)) | |
3013 | return zebra_vxlan_check_readd_vtep(ifp, vtep_ip); | |
d62a17ae | 3014 | return 0; |
28bd0652 | 3015 | } |
d62a17ae | 3016 | |
3017 | if (IS_ZEBRA_IF_VXLAN(ifp)) | |
15400f95 | 3018 | return zebra_vxlan_dp_network_mac_del(ifp, br_if, &mac, vid); |
d62a17ae | 3019 | |
3020 | return zebra_vxlan_local_mac_del(ifp, br_if, &mac, vid); | |
2232a77c | 3021 | } |
3022 | ||
2414abd3 | 3023 | static int netlink_macfdb_table(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
2232a77c | 3024 | { |
d62a17ae | 3025 | int len; |
3026 | struct ndmsg *ndm; | |
2232a77c | 3027 | |
d62a17ae | 3028 | if (h->nlmsg_type != RTM_NEWNEIGH) |
3029 | return 0; | |
2232a77c | 3030 | |
d62a17ae | 3031 | /* Length validity. */ |
3032 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg)); | |
3033 | if (len < 0) | |
3034 | return -1; | |
2232a77c | 3035 | |
d62a17ae | 3036 | /* We are interested only in AF_BRIDGE notifications. */ |
3037 | ndm = NLMSG_DATA(h); | |
3038 | if (ndm->ndm_family != AF_BRIDGE) | |
3039 | return 0; | |
2232a77c | 3040 | |
2414abd3 | 3041 | return netlink_macfdb_change(h, len, ns_id); |
2232a77c | 3042 | } |
3043 | ||
3044 | /* Request for MAC FDB information from the kernel */ | |
85a75f1e MS |
3045 | static int netlink_request_macs(struct nlsock *netlink_cmd, int family, |
3046 | int type, ifindex_t master_ifindex) | |
2232a77c | 3047 | { |
d62a17ae | 3048 | struct { |
3049 | struct nlmsghdr n; | |
3050 | struct ifinfomsg ifm; | |
3051 | char buf[256]; | |
3052 | } req; | |
3053 | ||
3054 | /* Form the request, specifying filter (rtattr) if needed. */ | |
3055 | memset(&req, 0, sizeof(req)); | |
3056 | req.n.nlmsg_type = type; | |
718f9b0f | 3057 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
d62a17ae | 3058 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
3059 | req.ifm.ifi_family = family; | |
3060 | if (master_ifindex) | |
312a6bee | 3061 | nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master_ifindex); |
d62a17ae | 3062 | |
fd3f8e52 | 3063 | return netlink_request(netlink_cmd, &req); |
2232a77c | 3064 | } |
3065 | ||
3066 | /* | |
3067 | * MAC forwarding database read using netlink interface. This is invoked | |
3068 | * at startup. | |
3069 | */ | |
d62a17ae | 3070 | int netlink_macfdb_read(struct zebra_ns *zns) |
2232a77c | 3071 | { |
d62a17ae | 3072 | int ret; |
85a75f1e MS |
3073 | struct zebra_dplane_info dp_info; |
3074 | ||
3075 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
d62a17ae | 3076 | |
3077 | /* Get bridge FDB table. */ | |
85a75f1e MS |
3078 | ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH, |
3079 | 0); | |
d62a17ae | 3080 | if (ret < 0) |
3081 | return ret; | |
3082 | /* We are reading entire table. */ | |
3083 | filter_vlan = 0; | |
85a75f1e MS |
3084 | ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, |
3085 | &dp_info, 0, 1); | |
d62a17ae | 3086 | |
3087 | return ret; | |
2232a77c | 3088 | } |
3089 | ||
3090 | /* | |
3091 | * MAC forwarding database read using netlink interface. This is for a | |
3092 | * specific bridge and matching specific access VLAN (if VLAN-aware bridge). | |
3093 | */ | |
d62a17ae | 3094 | int netlink_macfdb_read_for_bridge(struct zebra_ns *zns, struct interface *ifp, |
3095 | struct interface *br_if) | |
2232a77c | 3096 | { |
d62a17ae | 3097 | struct zebra_if *br_zif; |
3098 | struct zebra_if *zif; | |
3099 | struct zebra_l2info_vxlan *vxl; | |
85a75f1e | 3100 | struct zebra_dplane_info dp_info; |
d62a17ae | 3101 | int ret = 0; |
3102 | ||
85a75f1e | 3103 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); |
d62a17ae | 3104 | |
3105 | /* Save VLAN we're filtering on, if needed. */ | |
3106 | br_zif = (struct zebra_if *)br_if->info; | |
3107 | zif = (struct zebra_if *)ifp->info; | |
3108 | vxl = &zif->l2info.vxl; | |
3109 | if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif)) | |
3110 | filter_vlan = vxl->access_vlan; | |
3111 | ||
3112 | /* Get bridge FDB table for specific bridge - we do the VLAN filtering. | |
3113 | */ | |
85a75f1e | 3114 | ret = netlink_request_macs(&zns->netlink_cmd, AF_BRIDGE, RTM_GETNEIGH, |
d62a17ae | 3115 | br_if->ifindex); |
3116 | if (ret < 0) | |
3117 | return ret; | |
85a75f1e MS |
3118 | ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, |
3119 | &dp_info, 0, 0); | |
d62a17ae | 3120 | |
3121 | /* Reset VLAN filter. */ | |
3122 | filter_vlan = 0; | |
3123 | return ret; | |
2232a77c | 3124 | } |
3125 | ||
67fb9374 CS |
3126 | |
3127 | /* Request for MAC FDB for a specific MAC address in VLAN from the kernel */ | |
3128 | static int netlink_request_specific_mac_in_bridge(struct zebra_ns *zns, | |
3129 | int family, | |
3130 | int type, | |
3131 | struct interface *br_if, | |
3132 | struct ethaddr *mac, | |
3133 | vlanid_t vid) | |
3134 | { | |
3135 | struct { | |
3136 | struct nlmsghdr n; | |
3137 | struct ndmsg ndm; | |
3138 | char buf[256]; | |
3139 | } req; | |
3140 | struct zebra_if *br_zif; | |
67fb9374 CS |
3141 | |
3142 | memset(&req, 0, sizeof(req)); | |
3143 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
3144 | req.n.nlmsg_type = type; /* RTM_GETNEIGH */ | |
3145 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
3146 | req.ndm.ndm_family = family; /* AF_BRIDGE */ | |
3147 | /* req.ndm.ndm_state = NUD_REACHABLE; */ | |
3148 | ||
312a6bee | 3149 | nl_attr_put(&req.n, sizeof(req), NDA_LLADDR, mac, 6); |
67fb9374 CS |
3150 | |
3151 | br_zif = (struct zebra_if *)br_if->info; | |
3152 | if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif) && vid > 0) | |
312a6bee | 3153 | nl_attr_put16(&req.n, sizeof(req), NDA_VLAN, vid); |
67fb9374 | 3154 | |
312a6bee | 3155 | nl_attr_put32(&req.n, sizeof(req), NDA_MASTER, br_if->ifindex); |
67fb9374 CS |
3156 | |
3157 | if (IS_ZEBRA_DEBUG_KERNEL) | |
bd47f3a3 | 3158 | zlog_debug( |
ef7b8be4 | 3159 | "%s: Tx family %s IF %s(%u) vrf %s(%u) MAC %pEA vid %u", |
bd47f3a3 JU |
3160 | __func__, nl_family_to_str(req.ndm.ndm_family), |
3161 | br_if->name, br_if->ifindex, | |
ef7b8be4 | 3162 | vrf_id_to_name(br_if->vrf_id), br_if->vrf_id, mac, vid); |
67fb9374 | 3163 | |
fd3f8e52 | 3164 | return netlink_request(&zns->netlink_cmd, &req); |
67fb9374 CS |
3165 | } |
3166 | ||
3167 | int netlink_macfdb_read_specific_mac(struct zebra_ns *zns, | |
3168 | struct interface *br_if, | |
3169 | struct ethaddr *mac, vlanid_t vid) | |
3170 | { | |
3171 | int ret = 0; | |
3172 | struct zebra_dplane_info dp_info; | |
3173 | ||
3174 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
3175 | ||
3176 | /* Get bridge FDB table for specific bridge - we do the VLAN filtering. | |
3177 | */ | |
3178 | ret = netlink_request_specific_mac_in_bridge(zns, AF_BRIDGE, | |
3179 | RTM_GETNEIGH, | |
3180 | br_if, mac, vid); | |
3181 | if (ret < 0) | |
3182 | return ret; | |
3183 | ||
3184 | ret = netlink_parse_info(netlink_macfdb_table, &zns->netlink_cmd, | |
3185 | &dp_info, 1, 0); | |
3186 | ||
3187 | return ret; | |
3188 | } | |
036d93c0 MS |
3189 | |
3190 | /* | |
3191 | * Netlink-specific handler for MAC updates using dataplane context object. | |
3192 | */ | |
67e3369e JU |
3193 | ssize_t netlink_macfdb_update_ctx(struct zebra_dplane_ctx *ctx, void *data, |
3194 | size_t datalen) | |
2232a77c | 3195 | { |
340845e2 | 3196 | struct ipaddr vtep_ip; |
036d93c0 | 3197 | vlanid_t vid; |
d4d4ec1c RZ |
3198 | ssize_t total; |
3199 | int cmd; | |
340845e2 JU |
3200 | uint8_t flags; |
3201 | uint16_t state; | |
506efd37 | 3202 | uint32_t nhg_id; |
f188e68e AK |
3203 | uint32_t update_flags; |
3204 | bool nfy = false; | |
3205 | uint8_t nfy_flags = 0; | |
d4d4ec1c RZ |
3206 | |
3207 | cmd = dplane_ctx_get_op(ctx) == DPLANE_OP_MAC_INSTALL | |
3208 | ? RTM_NEWNEIGH : RTM_DELNEIGH; | |
036d93c0 | 3209 | |
f188e68e | 3210 | flags = NTF_MASTER; |
340845e2 | 3211 | state = NUD_REACHABLE; |
d62a17ae | 3212 | |
f188e68e AK |
3213 | update_flags = dplane_ctx_mac_get_update_flags(ctx); |
3214 | if (update_flags & DPLANE_MAC_REMOTE) { | |
3215 | flags |= NTF_SELF; | |
60e372e9 AK |
3216 | if (dplane_ctx_mac_is_sticky(ctx)) { |
3217 | /* NUD_NOARP prevents the entry from expiring */ | |
3218 | state |= NUD_NOARP; | |
3219 | /* sticky the entry from moving */ | |
f188e68e | 3220 | flags |= NTF_STICKY; |
60e372e9 | 3221 | } else { |
f188e68e | 3222 | flags |= NTF_EXT_LEARNED; |
60e372e9 | 3223 | } |
f188e68e AK |
3224 | /* if it was static-local previously we need to clear the |
3225 | * notify flags on replace with remote | |
3226 | */ | |
3227 | if (update_flags & DPLANE_MAC_WAS_STATIC) | |
3228 | nfy = true; | |
3229 | } else { | |
3230 | /* local mac */ | |
3231 | if (update_flags & DPLANE_MAC_SET_STATIC) { | |
4bcdb608 | 3232 | nfy_flags |= FDB_NOTIFY_BIT; |
f188e68e AK |
3233 | state |= NUD_NOARP; |
3234 | } | |
3235 | ||
3236 | if (update_flags & DPLANE_MAC_SET_INACTIVE) | |
4bcdb608 | 3237 | nfy_flags |= FDB_NOTIFY_INACTIVE_BIT; |
f188e68e AK |
3238 | |
3239 | nfy = true; | |
3240 | } | |
478566d6 | 3241 | |
506efd37 | 3242 | nhg_id = dplane_ctx_mac_get_nhg_id(ctx); |
340845e2 JU |
3243 | vtep_ip.ipaddr_v4 = *(dplane_ctx_mac_get_vtep_ip(ctx)); |
3244 | SET_IPADDR_V4(&vtep_ip); | |
d62a17ae | 3245 | |
036d93c0 | 3246 | if (IS_ZEBRA_DEBUG_KERNEL) { |
478566d6 | 3247 | char vid_buf[20]; |
506efd37 | 3248 | const struct ethaddr *mac = dplane_ctx_mac_get_addr(ctx); |
478566d6 | 3249 | |
340845e2 JU |
3250 | vid = dplane_ctx_mac_get_vlan(ctx); |
3251 | if (vid > 0) | |
478566d6 MS |
3252 | snprintf(vid_buf, sizeof(vid_buf), " VLAN %u", vid); |
3253 | else | |
3254 | vid_buf[0] = '\0'; | |
036d93c0 | 3255 | |
ccd187cd | 3256 | zlog_debug( |
ef7b8be4 | 3257 | "Tx %s family %s IF %s(%u)%s %sMAC %pEA dst %pIA nhg %u%s%s%s%s%s", |
ccd187cd AK |
3258 | nl_msg_type_to_str(cmd), nl_family_to_str(AF_BRIDGE), |
3259 | dplane_ctx_get_ifname(ctx), dplane_ctx_get_ifindex(ctx), | |
3260 | vid_buf, dplane_ctx_mac_is_sticky(ctx) ? "sticky " : "", | |
ef7b8be4 | 3261 | mac, &vtep_ip, nhg_id, |
ccd187cd AK |
3262 | (update_flags & DPLANE_MAC_REMOTE) ? " rem" : "", |
3263 | (update_flags & DPLANE_MAC_WAS_STATIC) ? " clr_sync" | |
3264 | : "", | |
3265 | (update_flags & DPLANE_MAC_SET_STATIC) ? " static" : "", | |
3266 | (update_flags & DPLANE_MAC_SET_INACTIVE) ? " inactive" | |
3267 | : "", | |
3268 | nfy ? " nfy" : ""); | |
036d93c0 | 3269 | } |
d62a17ae | 3270 | |
0be6e7d7 | 3271 | total = netlink_neigh_update_msg_encode( |
0a27a2fe PG |
3272 | ctx, cmd, (const void *)dplane_ctx_mac_get_addr(ctx), ETH_ALEN, |
3273 | &vtep_ip, true, AF_BRIDGE, 0, flags, state, nhg_id, nfy, | |
3274 | nfy_flags, false /*ext*/, 0 /*ext_flags*/, data, datalen, | |
3275 | zebra2proto(dplane_ctx_get_type(ctx))); | |
d4d4ec1c RZ |
3276 | |
3277 | return total; | |
2232a77c | 3278 | } |
3279 | ||
f17b99ed DS |
3280 | /* |
3281 | * In the event the kernel deletes ipv4 link-local neighbor entries created for | |
3282 | * 5549 support, re-install them. | |
3283 | */ | |
3284 | static void netlink_handle_5549(struct ndmsg *ndm, struct zebra_if *zif, | |
9b036974 DS |
3285 | struct interface *ifp, struct ipaddr *ip, |
3286 | bool handle_failed) | |
f17b99ed DS |
3287 | { |
3288 | if (ndm->ndm_family != AF_INET) | |
3289 | return; | |
3290 | ||
3291 | if (!zif->v6_2_v4_ll_neigh_entry) | |
3292 | return; | |
3293 | ||
3294 | if (ipv4_ll.s_addr != ip->ip._v4_addr.s_addr) | |
3295 | return; | |
3296 | ||
9b036974 DS |
3297 | if (handle_failed && ndm->ndm_state & NUD_FAILED) { |
3298 | zlog_info("Neighbor Entry for %s has entered a failed state, not reinstalling", | |
3299 | ifp->name); | |
3300 | return; | |
3301 | } | |
3302 | ||
f17b99ed DS |
3303 | if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, &zif->v6_2_v4_ll_addr6, true); |
3304 | } | |
3305 | ||
d62a17ae | 3306 | #define NUD_VALID \ |
3307 | (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE | NUD_PROBE | NUD_STALE \ | |
3308 | | NUD_DELAY) | |
f188e68e AK |
3309 | #define NUD_LOCAL_ACTIVE \ |
3310 | (NUD_PERMANENT | NUD_NOARP | NUD_REACHABLE) | |
2232a77c | 3311 | |
2414abd3 | 3312 | static int netlink_ipneigh_change(struct nlmsghdr *h, int len, ns_id_t ns_id) |
2232a77c | 3313 | { |
d62a17ae | 3314 | struct ndmsg *ndm; |
3315 | struct interface *ifp; | |
3316 | struct zebra_if *zif; | |
d62a17ae | 3317 | struct rtattr *tb[NDA_MAX + 1]; |
3318 | struct interface *link_if; | |
3319 | struct ethaddr mac; | |
3320 | struct ipaddr ip; | |
bd47f3a3 | 3321 | struct vrf *vrf; |
d62a17ae | 3322 | char buf[ETHER_ADDR_STRLEN]; |
d62a17ae | 3323 | int mac_present = 0; |
a37f4598 | 3324 | bool is_ext; |
3325 | bool is_router; | |
f188e68e | 3326 | bool local_inactive; |
7c0e4dc6 AK |
3327 | uint32_t ext_flags = 0; |
3328 | bool dp_static = false; | |
7723e8d3 PG |
3329 | int l2_len = 0; |
3330 | int cmd; | |
d62a17ae | 3331 | |
3332 | ndm = NLMSG_DATA(h); | |
3333 | ||
3334 | /* The interface should exist. */ | |
5895d33f | 3335 | ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), |
d62a17ae | 3336 | ndm->ndm_ifindex); |
2853fed6 | 3337 | if (!ifp || !ifp->info) |
d62a17ae | 3338 | return 0; |
3339 | ||
bd47f3a3 | 3340 | vrf = vrf_lookup_by_id(ifp->vrf_id); |
20089ae2 DS |
3341 | zif = (struct zebra_if *)ifp->info; |
3342 | ||
3343 | /* Parse attributes and extract fields of interest. */ | |
0d6f7fd6 | 3344 | memset(tb, 0, sizeof(tb)); |
20089ae2 DS |
3345 | netlink_parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len); |
3346 | ||
3347 | if (!tb[NDA_DST]) { | |
bd47f3a3 | 3348 | zlog_debug("%s family %s IF %s(%u) vrf %s(%u) - no DST", |
9df414fe QY |
3349 | nl_msg_type_to_str(h->nlmsg_type), |
3350 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
bd47f3a3 | 3351 | ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id); |
d62a17ae | 3352 | return 0; |
20089ae2 DS |
3353 | } |
3354 | ||
3355 | memset(&ip, 0, sizeof(struct ipaddr)); | |
3356 | ip.ipa_type = (ndm->ndm_family == AF_INET) ? IPADDR_V4 : IPADDR_V6; | |
3357 | memcpy(&ip.ip.addr, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST])); | |
3358 | ||
f17b99ed DS |
3359 | /* if kernel deletes our rfc5549 neighbor entry, re-install it */ |
3360 | if (h->nlmsg_type == RTM_DELNEIGH && (ndm->ndm_state & NUD_PERMANENT)) { | |
9b036974 | 3361 | netlink_handle_5549(ndm, zif, ifp, &ip, false); |
28bd0652 DS |
3362 | if (IS_ZEBRA_DEBUG_KERNEL) |
3363 | zlog_debug( | |
1d5453d6 | 3364 | " Neighbor Entry Received is a 5549 entry, finished"); |
20089ae2 DS |
3365 | return 0; |
3366 | } | |
d62a17ae | 3367 | |
f17b99ed | 3368 | /* if kernel marks our rfc5549 neighbor entry invalid, re-install it */ |
9b036974 DS |
3369 | if (h->nlmsg_type == RTM_NEWNEIGH && !(ndm->ndm_state & NUD_VALID)) |
3370 | netlink_handle_5549(ndm, zif, ifp, &ip, true); | |
f17b99ed | 3371 | |
7723e8d3 PG |
3372 | /* we send link layer information to client: |
3373 | * - nlmsg_type = RTM_DELNEIGH|NEWNEIGH|GETNEIGH | |
3374 | * - struct ipaddr ( for DEL and GET) | |
3375 | * - struct ethaddr mac; (for NEW) | |
3376 | */ | |
3377 | if (h->nlmsg_type == RTM_NEWNEIGH) | |
3378 | cmd = ZEBRA_NHRP_NEIGH_ADDED; | |
3379 | else if (h->nlmsg_type == RTM_GETNEIGH) | |
3380 | cmd = ZEBRA_NHRP_NEIGH_GET; | |
3381 | else if (h->nlmsg_type == RTM_DELNEIGH) | |
3382 | cmd = ZEBRA_NHRP_NEIGH_REMOVED; | |
3383 | else { | |
3384 | zlog_debug("%s(): unknown nlmsg type %u", __func__, | |
3385 | h->nlmsg_type); | |
3386 | return 0; | |
3387 | } | |
3388 | if (tb[NDA_LLADDR]) { | |
3389 | /* copy LLADDR information */ | |
3390 | l2_len = RTA_PAYLOAD(tb[NDA_LLADDR]); | |
3391 | memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), l2_len); | |
3392 | } | |
3393 | if (l2_len == IPV4_MAX_BYTELEN || l2_len == 0) | |
3394 | zsend_nhrp_neighbor_notify(cmd, ifp, &ip, ndm->ndm_state, | |
3395 | &mac, l2_len); | |
3396 | ||
3397 | if (h->nlmsg_type == RTM_GETNEIGH) | |
3398 | return 0; | |
3399 | ||
d62a17ae | 3400 | /* The neighbor is present on an SVI. From this, we locate the |
3401 | * underlying | |
3402 | * bridge because we're only interested in neighbors on a VxLAN bridge. | |
3403 | * The bridge is located based on the nature of the SVI: | |
3404 | * (a) In the case of a VLAN-aware bridge, the SVI is a L3 VLAN | |
3405 | * interface | |
3406 | * and is linked to the bridge | |
3407 | * (b) In the case of a VLAN-unaware bridge, the SVI is the bridge | |
3408 | * inteface | |
3409 | * itself | |
3410 | */ | |
3411 | if (IS_ZEBRA_IF_VLAN(ifp)) { | |
5895d33f | 3412 | link_if = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), |
71349e03 | 3413 | zif->link_ifindex); |
d62a17ae | 3414 | if (!link_if) |
3415 | return 0; | |
3416 | } else if (IS_ZEBRA_IF_BRIDGE(ifp)) | |
3417 | link_if = ifp; | |
28bd0652 DS |
3418 | else { |
3419 | if (IS_ZEBRA_DEBUG_KERNEL) | |
3420 | zlog_debug( | |
1d5453d6 | 3421 | " Neighbor Entry received is not on a VLAN or a BRIDGE, ignoring"); |
d62a17ae | 3422 | return 0; |
28bd0652 | 3423 | } |
d62a17ae | 3424 | |
d62a17ae | 3425 | memset(&mac, 0, sizeof(struct ethaddr)); |
d62a17ae | 3426 | if (h->nlmsg_type == RTM_NEWNEIGH) { |
3427 | if (tb[NDA_LLADDR]) { | |
ff8b7eb8 | 3428 | if (RTA_PAYLOAD(tb[NDA_LLADDR]) != ETH_ALEN) { |
28bd0652 DS |
3429 | if (IS_ZEBRA_DEBUG_KERNEL) |
3430 | zlog_debug( | |
bd47f3a3 | 3431 | "%s family %s IF %s(%u) vrf %s(%u) - LLADDR is not MAC, len %lu", |
28bd0652 DS |
3432 | nl_msg_type_to_str( |
3433 | h->nlmsg_type), | |
3434 | nl_family_to_str( | |
3435 | ndm->ndm_family), | |
3436 | ifp->name, ndm->ndm_ifindex, | |
bd47f3a3 | 3437 | VRF_LOGNAME(vrf), ifp->vrf_id, |
28bd0652 DS |
3438 | (unsigned long)RTA_PAYLOAD( |
3439 | tb[NDA_LLADDR])); | |
d62a17ae | 3440 | return 0; |
3441 | } | |
3442 | ||
3443 | mac_present = 1; | |
ff8b7eb8 | 3444 | memcpy(&mac, RTA_DATA(tb[NDA_LLADDR]), ETH_ALEN); |
d62a17ae | 3445 | } |
3446 | ||
a37f4598 | 3447 | is_ext = !!(ndm->ndm_flags & NTF_EXT_LEARNED); |
3448 | is_router = !!(ndm->ndm_flags & NTF_ROUTER); | |
d62a17ae | 3449 | |
7c0e4dc6 AK |
3450 | if (tb[NDA_EXT_FLAGS]) { |
3451 | ext_flags = *(uint32_t *)RTA_DATA(tb[NDA_EXT_FLAGS]); | |
3452 | if (ext_flags & NTF_E_MH_PEER_SYNC) | |
3453 | dp_static = true; | |
3454 | } | |
3455 | ||
d62a17ae | 3456 | if (IS_ZEBRA_DEBUG_KERNEL) |
3457 | zlog_debug( | |
ef7b8be4 | 3458 | "Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA MAC %s state 0x%x flags 0x%x ext_flags 0x%x", |
d62a17ae | 3459 | nl_msg_type_to_str(h->nlmsg_type), |
3460 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
bd47f3a3 | 3461 | ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id, |
ef7b8be4 | 3462 | &ip, |
d62a17ae | 3463 | mac_present |
3464 | ? prefix_mac2str(&mac, buf, sizeof(buf)) | |
3465 | : "", | |
7c0e4dc6 | 3466 | ndm->ndm_state, ndm->ndm_flags, ext_flags); |
d62a17ae | 3467 | |
3468 | /* If the neighbor state is valid for use, process as an add or | |
3469 | * update | |
3470 | * else process as a delete. Note that the delete handling may | |
3471 | * result | |
3472 | * in re-adding the neighbor if it is a valid "remote" neighbor. | |
3473 | */ | |
f188e68e | 3474 | if (ndm->ndm_state & NUD_VALID) { |
c7bfd085 AK |
3475 | if (zebra_evpn_mh_do_adv_reachable_neigh_only()) |
3476 | local_inactive = | |
3477 | !(ndm->ndm_state & NUD_LOCAL_ACTIVE); | |
3478 | else | |
3479 | /* If EVPN-MH is not enabled we treat STALE | |
3480 | * neighbors as locally-active and advertise | |
3481 | * them | |
3482 | */ | |
3483 | local_inactive = false; | |
f188e68e | 3484 | |
ee69da27 | 3485 | return zebra_vxlan_handle_kernel_neigh_update( |
7c0e4dc6 AK |
3486 | ifp, link_if, &ip, &mac, ndm->ndm_state, is_ext, |
3487 | is_router, local_inactive, dp_static); | |
f188e68e | 3488 | } |
d62a17ae | 3489 | |
ee69da27 | 3490 | return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip); |
d62a17ae | 3491 | } |
3492 | ||
3493 | if (IS_ZEBRA_DEBUG_KERNEL) | |
ef7b8be4 | 3494 | zlog_debug("Rx %s family %s IF %s(%u) vrf %s(%u) IP %pIA", |
d62a17ae | 3495 | nl_msg_type_to_str(h->nlmsg_type), |
3496 | nl_family_to_str(ndm->ndm_family), ifp->name, | |
bd47f3a3 | 3497 | ndm->ndm_ifindex, VRF_LOGNAME(vrf), ifp->vrf_id, |
ef7b8be4 | 3498 | &ip); |
d62a17ae | 3499 | |
3500 | /* Process the delete - it may result in re-adding the neighbor if it is | |
3501 | * a valid "remote" neighbor. | |
3502 | */ | |
ee69da27 | 3503 | return zebra_vxlan_handle_kernel_neigh_del(ifp, link_if, &ip); |
2232a77c | 3504 | } |
3505 | ||
2414abd3 | 3506 | static int netlink_neigh_table(struct nlmsghdr *h, ns_id_t ns_id, int startup) |
2232a77c | 3507 | { |
d62a17ae | 3508 | int len; |
3509 | struct ndmsg *ndm; | |
2232a77c | 3510 | |
d62a17ae | 3511 | if (h->nlmsg_type != RTM_NEWNEIGH) |
3512 | return 0; | |
2232a77c | 3513 | |
d62a17ae | 3514 | /* Length validity. */ |
3515 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg)); | |
3516 | if (len < 0) | |
3517 | return -1; | |
2232a77c | 3518 | |
d62a17ae | 3519 | /* We are interested only in AF_INET or AF_INET6 notifications. */ |
3520 | ndm = NLMSG_DATA(h); | |
3521 | if (ndm->ndm_family != AF_INET && ndm->ndm_family != AF_INET6) | |
3522 | return 0; | |
2232a77c | 3523 | |
2414abd3 | 3524 | return netlink_neigh_change(h, len); |
2232a77c | 3525 | } |
3526 | ||
3527 | /* Request for IP neighbor information from the kernel */ | |
85a75f1e MS |
3528 | static int netlink_request_neigh(struct nlsock *netlink_cmd, int family, |
3529 | int type, ifindex_t ifindex) | |
2232a77c | 3530 | { |
d62a17ae | 3531 | struct { |
3532 | struct nlmsghdr n; | |
3533 | struct ndmsg ndm; | |
3534 | char buf[256]; | |
3535 | } req; | |
3536 | ||
3537 | /* Form the request, specifying filter (rtattr) if needed. */ | |
3538 | memset(&req, 0, sizeof(req)); | |
3539 | req.n.nlmsg_type = type; | |
718f9b0f | 3540 | req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
d62a17ae | 3541 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); |
3542 | req.ndm.ndm_family = family; | |
3543 | if (ifindex) | |
312a6bee | 3544 | nl_attr_put32(&req.n, sizeof(req), NDA_IFINDEX, ifindex); |
d62a17ae | 3545 | |
fd3f8e52 | 3546 | return netlink_request(netlink_cmd, &req); |
2232a77c | 3547 | } |
3548 | ||
3549 | /* | |
3550 | * IP Neighbor table read using netlink interface. This is invoked | |
3551 | * at startup. | |
3552 | */ | |
d62a17ae | 3553 | int netlink_neigh_read(struct zebra_ns *zns) |
2232a77c | 3554 | { |
d62a17ae | 3555 | int ret; |
85a75f1e MS |
3556 | struct zebra_dplane_info dp_info; |
3557 | ||
3558 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
2232a77c | 3559 | |
d62a17ae | 3560 | /* Get IP neighbor table. */ |
85a75f1e MS |
3561 | ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH, |
3562 | 0); | |
d62a17ae | 3563 | if (ret < 0) |
3564 | return ret; | |
85a75f1e MS |
3565 | ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, |
3566 | &dp_info, 0, 1); | |
2232a77c | 3567 | |
d62a17ae | 3568 | return ret; |
2232a77c | 3569 | } |
3570 | ||
3571 | /* | |
3572 | * IP Neighbor table read using netlink interface. This is for a specific | |
3573 | * VLAN device. | |
3574 | */ | |
d62a17ae | 3575 | int netlink_neigh_read_for_vlan(struct zebra_ns *zns, struct interface *vlan_if) |
2232a77c | 3576 | { |
d62a17ae | 3577 | int ret = 0; |
85a75f1e MS |
3578 | struct zebra_dplane_info dp_info; |
3579 | ||
3580 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
2232a77c | 3581 | |
85a75f1e | 3582 | ret = netlink_request_neigh(&zns->netlink_cmd, AF_UNSPEC, RTM_GETNEIGH, |
d62a17ae | 3583 | vlan_if->ifindex); |
3584 | if (ret < 0) | |
3585 | return ret; | |
85a75f1e MS |
3586 | ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, |
3587 | &dp_info, 0, 0); | |
2232a77c | 3588 | |
d62a17ae | 3589 | return ret; |
2232a77c | 3590 | } |
3591 | ||
67fb9374 CS |
3592 | /* |
3593 | * Request for a specific IP in VLAN (SVI) device from IP Neighbor table, | |
3594 | * read using netlink interface. | |
3595 | */ | |
3596 | static int netlink_request_specific_neigh_in_vlan(struct zebra_ns *zns, | |
3597 | int type, struct ipaddr *ip, | |
3598 | ifindex_t ifindex) | |
3599 | { | |
3600 | struct { | |
3601 | struct nlmsghdr n; | |
3602 | struct ndmsg ndm; | |
3603 | char buf[256]; | |
3604 | } req; | |
3605 | int ipa_len; | |
3606 | ||
3607 | /* Form the request, specifying filter (rtattr) if needed. */ | |
3608 | memset(&req, 0, sizeof(req)); | |
3609 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
3610 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
3611 | req.n.nlmsg_type = type; /* RTM_GETNEIGH */ | |
3612 | req.ndm.ndm_ifindex = ifindex; | |
3613 | ||
3614 | if (IS_IPADDR_V4(ip)) { | |
3615 | ipa_len = IPV4_MAX_BYTELEN; | |
3616 | req.ndm.ndm_family = AF_INET; | |
3617 | ||
3618 | } else { | |
3619 | ipa_len = IPV6_MAX_BYTELEN; | |
3620 | req.ndm.ndm_family = AF_INET6; | |
3621 | } | |
3622 | ||
312a6bee | 3623 | nl_attr_put(&req.n, sizeof(req), NDA_DST, &ip->ip.addr, ipa_len); |
67fb9374 | 3624 | |
ef7b8be4 DL |
3625 | if (IS_ZEBRA_DEBUG_KERNEL) |
3626 | zlog_debug("%s: Tx %s family %s IF %u IP %pIA flags 0x%x", | |
7c26c121 | 3627 | __func__, nl_msg_type_to_str(type), |
ef7b8be4 DL |
3628 | nl_family_to_str(req.ndm.ndm_family), ifindex, ip, |
3629 | req.n.nlmsg_flags); | |
7c26c121 | 3630 | |
fd3f8e52 | 3631 | return netlink_request(&zns->netlink_cmd, &req); |
67fb9374 CS |
3632 | } |
3633 | ||
3634 | int netlink_neigh_read_specific_ip(struct ipaddr *ip, | |
3635 | struct interface *vlan_if) | |
3636 | { | |
3637 | int ret = 0; | |
3638 | struct zebra_ns *zns; | |
3639 | struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vlan_if->vrf_id); | |
67fb9374 CS |
3640 | struct zebra_dplane_info dp_info; |
3641 | ||
3642 | zns = zvrf->zns; | |
3643 | ||
3644 | zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/); | |
3645 | ||
3646 | if (IS_ZEBRA_DEBUG_KERNEL) | |
ef7b8be4 DL |
3647 | zlog_debug("%s: neigh request IF %s(%u) IP %pIA vrf %s(%u)", |
3648 | __func__, vlan_if->name, vlan_if->ifindex, ip, | |
bd47f3a3 | 3649 | vrf_id_to_name(vlan_if->vrf_id), vlan_if->vrf_id); |
67fb9374 CS |
3650 | |
3651 | ret = netlink_request_specific_neigh_in_vlan(zns, RTM_GETNEIGH, ip, | |
3652 | vlan_if->ifindex); | |
3653 | if (ret < 0) | |
3654 | return ret; | |
3655 | ||
3656 | ret = netlink_parse_info(netlink_neigh_table, &zns->netlink_cmd, | |
3657 | &dp_info, 1, 0); | |
3658 | ||
3659 | return ret; | |
3660 | } | |
3661 | ||
2414abd3 | 3662 | int netlink_neigh_change(struct nlmsghdr *h, ns_id_t ns_id) |
2232a77c | 3663 | { |
d62a17ae | 3664 | int len; |
3665 | struct ndmsg *ndm; | |
2232a77c | 3666 | |
7723e8d3 PG |
3667 | if (!(h->nlmsg_type == RTM_NEWNEIGH || h->nlmsg_type == RTM_DELNEIGH |
3668 | || h->nlmsg_type == RTM_GETNEIGH)) | |
d62a17ae | 3669 | return 0; |
2232a77c | 3670 | |
d62a17ae | 3671 | /* Length validity. */ |
3672 | len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg)); | |
9bdf8618 | 3673 | if (len < 0) { |
15569c58 DA |
3674 | zlog_err( |
3675 | "%s: Message received from netlink is of a broken size %d %zu", | |
3676 | __func__, h->nlmsg_len, | |
3677 | (size_t)NLMSG_LENGTH(sizeof(struct ndmsg))); | |
d62a17ae | 3678 | return -1; |
9bdf8618 | 3679 | } |
2232a77c | 3680 | |
d62a17ae | 3681 | /* Is this a notification for the MAC FDB or IP neighbor table? */ |
3682 | ndm = NLMSG_DATA(h); | |
3683 | if (ndm->ndm_family == AF_BRIDGE) | |
2414abd3 | 3684 | return netlink_macfdb_change(h, len, ns_id); |
2232a77c | 3685 | |
d62a17ae | 3686 | if (ndm->ndm_type != RTN_UNICAST) |
3687 | return 0; | |
2232a77c | 3688 | |
d62a17ae | 3689 | if (ndm->ndm_family == AF_INET || ndm->ndm_family == AF_INET6) |
2414abd3 | 3690 | return netlink_ipneigh_change(h, len, ns_id); |
8a1b681c | 3691 | else { |
9df414fe | 3692 | flog_warn( |
e914ccbe | 3693 | EC_ZEBRA_UNKNOWN_FAMILY, |
87b5d1b0 DS |
3694 | "Invalid address family: %u received from kernel neighbor change: %s", |
3695 | ndm->ndm_family, nl_msg_type_to_str(h->nlmsg_type)); | |
8a1b681c SW |
3696 | return 0; |
3697 | } | |
2232a77c | 3698 | |
d62a17ae | 3699 | return 0; |
2232a77c | 3700 | } |
3701 | ||
931fa60c MS |
3702 | /* |
3703 | * Utility neighbor-update function, using info from dplane context. | |
3704 | */ | |
67e3369e JU |
3705 | static ssize_t netlink_neigh_update_ctx(const struct zebra_dplane_ctx *ctx, |
3706 | int cmd, void *buf, size_t buflen) | |
2232a77c | 3707 | { |
931fa60c | 3708 | const struct ipaddr *ip; |
0a27a2fe PG |
3709 | const struct ethaddr *mac = NULL; |
3710 | const struct ipaddr *link_ip = NULL; | |
3711 | const void *link_ptr = NULL; | |
3712 | char buf2[ETHER_ADDR_STRLEN]; | |
3713 | ||
3714 | int llalen; | |
931fa60c MS |
3715 | uint8_t flags; |
3716 | uint16_t state; | |
340845e2 | 3717 | uint8_t family; |
ccd187cd AK |
3718 | uint32_t update_flags; |
3719 | uint32_t ext_flags = 0; | |
3720 | bool ext = false; | |
d62a17ae | 3721 | |
931fa60c | 3722 | ip = dplane_ctx_neigh_get_ipaddr(ctx); |
931fa60c | 3723 | |
0a27a2fe PG |
3724 | if (dplane_ctx_get_op(ctx) == DPLANE_OP_NEIGH_IP_INSTALL |
3725 | || dplane_ctx_get_op(ctx) == DPLANE_OP_NEIGH_IP_DELETE) { | |
3726 | link_ip = dplane_ctx_neigh_get_link_ip(ctx); | |
3727 | llalen = IPADDRSZ(link_ip); | |
3728 | link_ptr = (const void *)&(link_ip->ip.addr); | |
3729 | ipaddr2str(link_ip, buf2, sizeof(buf2)); | |
3730 | } else { | |
3731 | mac = dplane_ctx_neigh_get_mac(ctx); | |
3732 | llalen = ETH_ALEN; | |
3733 | link_ptr = (const void *)mac; | |
3734 | if (is_zero_mac(mac)) | |
3735 | mac = NULL; | |
3736 | if (mac) | |
3737 | prefix_mac2str(mac, buf2, sizeof(buf2)); | |
3738 | else | |
3739 | snprintf(buf2, sizeof(buf2), "null"); | |
3740 | } | |
ccd187cd | 3741 | update_flags = dplane_ctx_neigh_get_update_flags(ctx); |
931fa60c MS |
3742 | flags = neigh_flags_to_netlink(dplane_ctx_neigh_get_flags(ctx)); |
3743 | state = neigh_state_to_netlink(dplane_ctx_neigh_get_state(ctx)); | |
3744 | ||
340845e2 | 3745 | family = IS_IPADDR_V4(ip) ? AF_INET : AF_INET6; |
d62a17ae | 3746 | |
ccd187cd AK |
3747 | if (update_flags & DPLANE_NEIGH_REMOTE) { |
3748 | flags |= NTF_EXT_LEARNED; | |
3749 | /* if it was static-local previously we need to clear the | |
3750 | * ext flags on replace with remote | |
3751 | */ | |
3752 | if (update_flags & DPLANE_NEIGH_WAS_STATIC) | |
3753 | ext = true; | |
0a27a2fe | 3754 | } else if (!(update_flags & DPLANE_NEIGH_NO_EXTENSION)) { |
ccd187cd AK |
3755 | ext = true; |
3756 | /* local neigh */ | |
3757 | if (update_flags & DPLANE_NEIGH_SET_STATIC) | |
3758 | ext_flags |= NTF_E_MH_PEER_SYNC; | |
ccd187cd | 3759 | } |
ef7b8be4 | 3760 | if (IS_ZEBRA_DEBUG_KERNEL) |
340845e2 | 3761 | zlog_debug( |
0a27a2fe | 3762 | "Tx %s family %s IF %s(%u) Neigh %pIA %s %s flags 0x%x state 0x%x %sext_flags 0x%x", |
340845e2 JU |
3763 | nl_msg_type_to_str(cmd), nl_family_to_str(family), |
3764 | dplane_ctx_get_ifname(ctx), dplane_ctx_get_ifindex(ctx), | |
0a27a2fe PG |
3765 | ip, link_ip ? "Link " : "MAC ", buf2, flags, state, |
3766 | ext ? "ext " : "", ext_flags); | |
d62a17ae | 3767 | |
18f60fe9 | 3768 | return netlink_neigh_update_msg_encode( |
0a27a2fe PG |
3769 | ctx, cmd, link_ptr, llalen, ip, true, family, RTN_UNICAST, |
3770 | flags, state, 0 /*nhg*/, false /*nfy*/, 0 /*nfy_flags*/, ext, | |
3771 | ext_flags, buf, buflen, zebra2proto(dplane_ctx_get_type(ctx))); | |
2232a77c | 3772 | } |
3773 | ||
e18747a9 PG |
3774 | static int netlink_neigh_table_update_ctx(const struct zebra_dplane_ctx *ctx, |
3775 | void *data, size_t datalen) | |
3776 | { | |
3777 | struct { | |
3778 | struct nlmsghdr n; | |
3779 | struct ndtmsg ndtm; | |
3780 | char buf[]; | |
3781 | } *req = data; | |
3782 | struct rtattr *nest; | |
3783 | uint8_t family; | |
3784 | ifindex_t idx; | |
3785 | uint32_t val; | |
3786 | ||
3787 | if (datalen < sizeof(*req)) | |
3788 | return 0; | |
3789 | memset(req, 0, sizeof(*req)); | |
3790 | family = dplane_ctx_neightable_get_family(ctx); | |
3791 | idx = dplane_ctx_get_ifindex(ctx); | |
3792 | ||
3793 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)); | |
3794 | req->n.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE; | |
3795 | req->n.nlmsg_type = RTM_SETNEIGHTBL; | |
3796 | req->ndtm.ndtm_family = family; | |
3797 | ||
3798 | nl_attr_put(&req->n, datalen, NDTA_NAME, | |
3799 | family == AF_INET ? "arp_cache" : "ndisc_cache", 10); | |
3800 | nest = nl_attr_nest(&req->n, datalen, NDTA_PARMS); | |
3801 | if (nest == NULL) | |
3802 | return 0; | |
3803 | if (!nl_attr_put(&req->n, datalen, NDTPA_IFINDEX, &idx, sizeof(idx))) | |
3804 | return 0; | |
3805 | val = dplane_ctx_neightable_get_app_probes(ctx); | |
3806 | if (!nl_attr_put(&req->n, datalen, NDTPA_APP_PROBES, &val, sizeof(val))) | |
3807 | return 0; | |
3808 | val = dplane_ctx_neightable_get_mcast_probes(ctx); | |
3809 | if (!nl_attr_put(&req->n, datalen, NDTPA_MCAST_PROBES, &val, | |
3810 | sizeof(val))) | |
3811 | return 0; | |
3812 | val = dplane_ctx_neightable_get_ucast_probes(ctx); | |
3813 | if (!nl_attr_put(&req->n, datalen, NDTPA_UCAST_PROBES, &val, | |
3814 | sizeof(val))) | |
3815 | return 0; | |
3816 | nl_attr_nest_end(&req->n, nest); | |
3817 | ||
3818 | return NLMSG_ALIGN(req->n.nlmsg_len); | |
3819 | } | |
3820 | ||
67e3369e JU |
3821 | static ssize_t netlink_neigh_msg_encoder(struct zebra_dplane_ctx *ctx, |
3822 | void *buf, size_t buflen) | |
2232a77c | 3823 | { |
67e3369e | 3824 | ssize_t ret; |
2232a77c | 3825 | |
931fa60c MS |
3826 | switch (dplane_ctx_get_op(ctx)) { |
3827 | case DPLANE_OP_NEIGH_INSTALL: | |
3828 | case DPLANE_OP_NEIGH_UPDATE: | |
d68e74b4 | 3829 | case DPLANE_OP_NEIGH_DISCOVER: |
0a27a2fe | 3830 | case DPLANE_OP_NEIGH_IP_INSTALL: |
67e3369e | 3831 | ret = netlink_neigh_update_ctx(ctx, RTM_NEWNEIGH, buf, buflen); |
931fa60c MS |
3832 | break; |
3833 | case DPLANE_OP_NEIGH_DELETE: | |
0a27a2fe | 3834 | case DPLANE_OP_NEIGH_IP_DELETE: |
67e3369e | 3835 | ret = netlink_neigh_update_ctx(ctx, RTM_DELNEIGH, buf, buflen); |
931fa60c | 3836 | break; |
0bbd4ff4 | 3837 | case DPLANE_OP_VTEP_ADD: |
67e3369e JU |
3838 | ret = netlink_vxlan_flood_update_ctx(ctx, RTM_NEWNEIGH, buf, |
3839 | buflen); | |
0bbd4ff4 MS |
3840 | break; |
3841 | case DPLANE_OP_VTEP_DELETE: | |
67e3369e JU |
3842 | ret = netlink_vxlan_flood_update_ctx(ctx, RTM_DELNEIGH, buf, |
3843 | buflen); | |
0bbd4ff4 | 3844 | break; |
e18747a9 PG |
3845 | case DPLANE_OP_NEIGH_TABLE_UPDATE: |
3846 | ret = netlink_neigh_table_update_ctx(ctx, buf, buflen); | |
3847 | break; | |
931fa60c | 3848 | default: |
67e3369e | 3849 | ret = -1; |
931fa60c | 3850 | } |
2232a77c | 3851 | |
67e3369e JU |
3852 | return ret; |
3853 | } | |
3854 | ||
3855 | /* | |
3856 | * Update MAC, using dataplane context object. | |
3857 | */ | |
3858 | ||
67e3369e JU |
3859 | enum netlink_msg_status netlink_put_mac_update_msg(struct nl_batch *bth, |
3860 | struct zebra_dplane_ctx *ctx) | |
3861 | { | |
3862 | return netlink_batch_add_msg(bth, ctx, netlink_macfdb_update_ctx, | |
3863 | false); | |
3864 | } | |
3865 | ||
67e3369e JU |
3866 | enum netlink_msg_status |
3867 | netlink_put_neigh_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx) | |
3868 | { | |
3869 | return netlink_batch_add_msg(bth, ctx, netlink_neigh_msg_encoder, | |
3870 | false); | |
6fe2b0e6 CS |
3871 | } |
3872 | ||
16c628de MS |
3873 | /* |
3874 | * MPLS label forwarding table change via netlink interface, using dataplane | |
3875 | * context information. | |
3876 | */ | |
0be6e7d7 JU |
3877 | ssize_t netlink_mpls_multipath_msg_encode(int cmd, struct zebra_dplane_ctx *ctx, |
3878 | void *buf, size_t buflen) | |
16c628de MS |
3879 | { |
3880 | mpls_lse_t lse; | |
ee70f629 | 3881 | const struct nhlfe_list_head *head; |
81793ac1 | 3882 | const zebra_nhlfe_t *nhlfe; |
16c628de MS |
3883 | struct nexthop *nexthop = NULL; |
3884 | unsigned int nexthop_num; | |
3885 | const char *routedesc; | |
3886 | int route_type; | |
9a0132a5 | 3887 | struct prefix p = {0}; |
16c628de MS |
3888 | |
3889 | struct { | |
3890 | struct nlmsghdr n; | |
3891 | struct rtmsg r; | |
0be6e7d7 JU |
3892 | char buf[0]; |
3893 | } *req = buf; | |
3894 | ||
3895 | if (buflen < sizeof(*req)) | |
3896 | return 0; | |
16c628de | 3897 | |
0be6e7d7 | 3898 | memset(req, 0, sizeof(*req)); |
16c628de MS |
3899 | |
3900 | /* | |
3901 | * Count # nexthops so we can decide whether to use singlepath | |
3902 | * or multipath case. | |
3903 | */ | |
3904 | nexthop_num = 0; | |
ee70f629 MS |
3905 | head = dplane_ctx_get_nhlfe_list(ctx); |
3906 | frr_each(nhlfe_list_const, head, nhlfe) { | |
16c628de MS |
3907 | nexthop = nhlfe->nexthop; |
3908 | if (!nexthop) | |
3909 | continue; | |
3910 | if (cmd == RTM_NEWROUTE) { | |
3911 | /* Count all selected NHLFEs */ | |
3912 | if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) | |
3913 | && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
3914 | nexthop_num++; | |
3915 | } else { /* DEL */ | |
3916 | /* Count all installed NHLFEs */ | |
3917 | if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) | |
3918 | && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) | |
3919 | nexthop_num++; | |
3920 | } | |
3921 | } | |
3922 | ||
3923 | if ((nexthop_num == 0) || | |
3924 | (!dplane_ctx_get_best_nhlfe(ctx) && (cmd != RTM_DELROUTE))) | |
3925 | return 0; | |
3926 | ||
0be6e7d7 JU |
3927 | req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
3928 | req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; | |
3929 | req->n.nlmsg_type = cmd; | |
3930 | req->n.nlmsg_pid = dplane_ctx_get_ns(ctx)->nls.snl.nl_pid; | |
16c628de | 3931 | |
0be6e7d7 JU |
3932 | req->r.rtm_family = AF_MPLS; |
3933 | req->r.rtm_table = RT_TABLE_MAIN; | |
3934 | req->r.rtm_dst_len = MPLS_LABEL_LEN_BITS; | |
3935 | req->r.rtm_scope = RT_SCOPE_UNIVERSE; | |
3936 | req->r.rtm_type = RTN_UNICAST; | |
16c628de MS |
3937 | |
3938 | if (cmd == RTM_NEWROUTE) { | |
3939 | /* We do a replace to handle update. */ | |
0be6e7d7 | 3940 | req->n.nlmsg_flags |= NLM_F_REPLACE; |
16c628de MS |
3941 | |
3942 | /* set the protocol value if installing */ | |
3943 | route_type = re_type_from_lsp_type( | |
3944 | dplane_ctx_get_best_nhlfe(ctx)->type); | |
0be6e7d7 | 3945 | req->r.rtm_protocol = zebra2proto(route_type); |
16c628de MS |
3946 | } |
3947 | ||
3948 | /* Fill destination */ | |
3949 | lse = mpls_lse_encode(dplane_ctx_get_in_label(ctx), 0, 0, 1); | |
0be6e7d7 JU |
3950 | if (!nl_attr_put(&req->n, buflen, RTA_DST, &lse, sizeof(mpls_lse_t))) |
3951 | return 0; | |
16c628de MS |
3952 | |
3953 | /* Fill nexthops (paths) based on single-path or multipath. The paths | |
3954 | * chosen depend on the operation. | |
3955 | */ | |
fc608372 | 3956 | if (nexthop_num == 1) { |
16c628de MS |
3957 | routedesc = "single-path"; |
3958 | _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx), | |
3959 | routedesc); | |
3960 | ||
3961 | nexthop_num = 0; | |
ee70f629 | 3962 | frr_each(nhlfe_list_const, head, nhlfe) { |
16c628de MS |
3963 | nexthop = nhlfe->nexthop; |
3964 | if (!nexthop) | |
3965 | continue; | |
3966 | ||
3967 | if ((cmd == RTM_NEWROUTE | |
3968 | && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) | |
3969 | && CHECK_FLAG(nexthop->flags, | |
3970 | NEXTHOP_FLAG_ACTIVE))) | |
3971 | || (cmd == RTM_DELROUTE | |
3972 | && (CHECK_FLAG(nhlfe->flags, | |
3973 | NHLFE_FLAG_INSTALLED) | |
3974 | && CHECK_FLAG(nexthop->flags, | |
3975 | NEXTHOP_FLAG_FIB)))) { | |
3976 | /* Add the gateway */ | |
0be6e7d7 JU |
3977 | if (!_netlink_mpls_build_singlepath( |
3978 | &p, routedesc, nhlfe, &req->n, | |
3979 | &req->r, buflen, cmd)) | |
3980 | return false; | |
16c628de MS |
3981 | |
3982 | nexthop_num++; | |
3983 | break; | |
3984 | } | |
3985 | } | |
3986 | } else { /* Multipath case */ | |
312a6bee | 3987 | struct rtattr *nest; |
81793ac1 | 3988 | const union g_addr *src1 = NULL; |
16c628de | 3989 | |
0be6e7d7 JU |
3990 | nest = nl_attr_nest(&req->n, buflen, RTA_MULTIPATH); |
3991 | if (!nest) | |
3992 | return 0; | |
16c628de MS |
3993 | |
3994 | routedesc = "multipath"; | |
3995 | _netlink_mpls_debug(cmd, dplane_ctx_get_in_label(ctx), | |
3996 | routedesc); | |
3997 | ||
3998 | nexthop_num = 0; | |
ee70f629 | 3999 | frr_each(nhlfe_list_const, head, nhlfe) { |
16c628de MS |
4000 | nexthop = nhlfe->nexthop; |
4001 | if (!nexthop) | |
4002 | continue; | |
4003 | ||
16c628de MS |
4004 | if ((cmd == RTM_NEWROUTE |
4005 | && (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) | |
4006 | && CHECK_FLAG(nexthop->flags, | |
4007 | NEXTHOP_FLAG_ACTIVE))) | |
4008 | || (cmd == RTM_DELROUTE | |
4009 | && (CHECK_FLAG(nhlfe->flags, | |
4010 | NHLFE_FLAG_INSTALLED) | |
4011 | && CHECK_FLAG(nexthop->flags, | |
4012 | NEXTHOP_FLAG_FIB)))) { | |
4013 | nexthop_num++; | |
4014 | ||
4015 | /* Build the multipath */ | |
0be6e7d7 JU |
4016 | if (!_netlink_mpls_build_multipath( |
4017 | &p, routedesc, nhlfe, &req->n, | |
4018 | buflen, &req->r, &src1)) | |
4019 | return 0; | |
16c628de MS |
4020 | } |
4021 | } | |
4022 | ||
4023 | /* Add the multipath */ | |
0be6e7d7 | 4024 | nl_attr_nest_end(&req->n, nest); |
16c628de MS |
4025 | } |
4026 | ||
0be6e7d7 | 4027 | return NLMSG_ALIGN(req->n.nlmsg_len); |
16c628de | 4028 | } |
506efd37 AK |
4029 | |
4030 | /**************************************************************************** | |
4031 | * This code was developed in a branch that didn't have dplane APIs for | |
4032 | * MAC updates. Hence the use of the legacy style. It will be moved to | |
4033 | * the new dplane style pre-merge to master. XXX | |
4034 | */ | |
4035 | static int netlink_fdb_nh_update(uint32_t nh_id, struct in_addr vtep_ip) | |
4036 | { | |
4037 | struct { | |
4038 | struct nlmsghdr n; | |
4039 | struct nhmsg nhm; | |
4040 | char buf[256]; | |
4041 | } req; | |
4042 | int cmd = RTM_NEWNEXTHOP; | |
4043 | struct zebra_vrf *zvrf; | |
4044 | struct zebra_ns *zns; | |
4045 | ||
4046 | zvrf = zebra_vrf_get_evpn(); | |
4047 | if (!zvrf) | |
4048 | return -1; | |
4049 | zns = zvrf->zns; | |
4050 | ||
4051 | memset(&req, 0, sizeof(req)); | |
4052 | ||
4053 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)); | |
4054 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
4055 | req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE); | |
4056 | req.n.nlmsg_type = cmd; | |
4057 | req.nhm.nh_family = AF_INET; | |
4058 | ||
4059 | if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nh_id)) | |
4060 | return -1; | |
4061 | if (!nl_attr_put(&req.n, sizeof(req), NHA_FDB, NULL, 0)) | |
4062 | return -1; | |
4063 | if (!nl_attr_put(&req.n, sizeof(req), NHA_GATEWAY, | |
4064 | &vtep_ip, IPV4_MAX_BYTELEN)) | |
4065 | return -1; | |
4066 | ||
4067 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) { | |
9bcef951 MS |
4068 | zlog_debug("Tx %s fdb-nh 0x%x %pI4", |
4069 | nl_msg_type_to_str(cmd), nh_id, &vtep_ip); | |
506efd37 AK |
4070 | } |
4071 | ||
4072 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
4073 | 0); | |
4074 | } | |
4075 | ||
4076 | static int netlink_fdb_nh_del(uint32_t nh_id) | |
4077 | { | |
4078 | struct { | |
4079 | struct nlmsghdr n; | |
4080 | struct nhmsg nhm; | |
4081 | char buf[256]; | |
4082 | } req; | |
4083 | int cmd = RTM_DELNEXTHOP; | |
4084 | struct zebra_vrf *zvrf; | |
4085 | struct zebra_ns *zns; | |
4086 | ||
4087 | zvrf = zebra_vrf_get_evpn(); | |
4088 | if (!zvrf) | |
4089 | return -1; | |
4090 | zns = zvrf->zns; | |
4091 | ||
4092 | memset(&req, 0, sizeof(req)); | |
4093 | ||
4094 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)); | |
4095 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
4096 | req.n.nlmsg_type = cmd; | |
4097 | req.nhm.nh_family = AF_UNSPEC; | |
4098 | ||
4099 | if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nh_id)) | |
4100 | return -1; | |
4101 | ||
4102 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) { | |
4103 | zlog_debug("Tx %s fdb-nh 0x%x", | |
4104 | nl_msg_type_to_str(cmd), nh_id); | |
4105 | } | |
4106 | ||
4107 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
4108 | 0); | |
4109 | } | |
4110 | ||
4111 | static int netlink_fdb_nhg_update(uint32_t nhg_id, uint32_t nh_cnt, | |
4112 | struct nh_grp *nh_ids) | |
4113 | { | |
4114 | struct { | |
4115 | struct nlmsghdr n; | |
4116 | struct nhmsg nhm; | |
4117 | char buf[256]; | |
4118 | } req; | |
4119 | int cmd = RTM_NEWNEXTHOP; | |
4120 | struct zebra_vrf *zvrf; | |
4121 | struct zebra_ns *zns; | |
4122 | struct nexthop_grp grp[nh_cnt]; | |
4123 | uint32_t i; | |
4124 | ||
4125 | zvrf = zebra_vrf_get_evpn(); | |
4126 | if (!zvrf) | |
4127 | return -1; | |
4128 | zns = zvrf->zns; | |
4129 | ||
4130 | memset(&req, 0, sizeof(req)); | |
4131 | ||
4132 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct nhmsg)); | |
4133 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
4134 | req.n.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE); | |
4135 | req.n.nlmsg_type = cmd; | |
4136 | req.nhm.nh_family = AF_UNSPEC; | |
4137 | ||
4138 | if (!nl_attr_put32(&req.n, sizeof(req), NHA_ID, nhg_id)) | |
4139 | return -1; | |
4140 | if (!nl_attr_put(&req.n, sizeof(req), NHA_FDB, NULL, 0)) | |
4141 | return -1; | |
4142 | memset(&grp, 0, sizeof(grp)); | |
4143 | for (i = 0; i < nh_cnt; ++i) { | |
4144 | grp[i].id = nh_ids[i].id; | |
4145 | grp[i].weight = nh_ids[i].weight; | |
4146 | } | |
4147 | if (!nl_attr_put(&req.n, sizeof(req), NHA_GROUP, | |
4148 | grp, nh_cnt * sizeof(struct nexthop_grp))) | |
4149 | return -1; | |
4150 | ||
4151 | ||
4152 | if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_EVPN_MH_NH) { | |
4153 | char vtep_str[ES_VTEP_LIST_STR_SZ]; | |
9e0c2fd1 | 4154 | char nh_buf[16]; |
506efd37 AK |
4155 | |
4156 | vtep_str[0] = '\0'; | |
4157 | for (i = 0; i < nh_cnt; ++i) { | |
9e0c2fd1 | 4158 | snprintf(nh_buf, sizeof(nh_buf), "%u ", |
506efd37 | 4159 | grp[i].id); |
9e0c2fd1 | 4160 | strlcat(vtep_str, nh_buf, sizeof(vtep_str)); |
506efd37 AK |
4161 | } |
4162 | ||
4163 | zlog_debug("Tx %s fdb-nhg 0x%x %s", | |
4164 | nl_msg_type_to_str(cmd), nhg_id, vtep_str); | |
4165 | } | |
4166 | ||
4167 | return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, | |
4168 | 0); | |
4169 | } | |
4170 | ||
4171 | static int netlink_fdb_nhg_del(uint32_t nhg_id) | |
4172 | { | |
4173 | return netlink_fdb_nh_del(nhg_id); | |
4174 | } | |
4175 | ||
4176 | int kernel_upd_mac_nh(uint32_t nh_id, struct in_addr vtep_ip) | |
4177 | { | |
4178 | return netlink_fdb_nh_update(nh_id, vtep_ip); | |
4179 | } | |
4180 | ||
4181 | int kernel_del_mac_nh(uint32_t nh_id) | |
4182 | { | |
4183 | return netlink_fdb_nh_del(nh_id); | |
4184 | } | |
4185 | ||
4186 | int kernel_upd_mac_nhg(uint32_t nhg_id, uint32_t nh_cnt, | |
4187 | struct nh_grp *nh_ids) | |
4188 | { | |
4189 | return netlink_fdb_nhg_update(nhg_id, nh_cnt, nh_ids); | |
4190 | } | |
4191 | ||
4192 | int kernel_del_mac_nhg(uint32_t nhg_id) | |
4193 | { | |
4194 | return netlink_fdb_nhg_del(nhg_id); | |
4195 | } | |
4196 | ||
ddfeb486 | 4197 | #endif /* HAVE_NETLINK */ |