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