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