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