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