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