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