]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_netlink.c
bgpd: Prevent multipathing among EVPN and non-EVPN paths
[mirror_frr.git] / zebra / if_netlink.c
CommitLineData
718e3744 1/*
2 * Interface looking up by netlink.
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
6675513d 23
ddfeb486
DL
24#ifdef GNU_LINUX
25
6675513d 26/* The following definition is to workaround an issue in the Linux kernel
27 * header files with redefinition of 'struct in6_addr' in both
28 * netinet/in.h and linux/in6.h.
29 * Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
30 */
31#define _LINUX_IN6_H
077c07cc
PG
32#define _LINUX_IF_H
33#define _LINUX_IP_H
6675513d 34
ba85366a 35#include <netinet/if_ether.h>
6675513d 36#include <linux/if_bridge.h>
ba777396 37#include <linux/if_link.h>
077c07cc 38#include <linux/if_tunnel.h>
1fdc9eae 39#include <net/if_arp.h>
535fe877
DS
40#include <linux/sockios.h>
41#include <linux/ethtool.h>
1fdc9eae 42
43#include "linklist.h"
44#include "if.h"
45#include "log.h"
46#include "prefix.h"
47#include "connected.h"
48#include "table.h"
49#include "memory.h"
1fdc9eae 50#include "rib.h"
51#include "thread.h"
52#include "privs.h"
53#include "nexthop.h"
54#include "vrf.h"
7922fc65 55#include "vrf_int.h"
1fdc9eae 56#include "mpls.h"
174482ef 57#include "lib_errors.h"
718e3744 58
5e6a74d8 59#include "vty.h"
7dbeea9d 60#include "zebra/zserv.h"
1fdc9eae 61#include "zebra/zebra_ns.h"
62#include "zebra/zebra_vrf.h"
63#include "zebra/rt.h"
64#include "zebra/redistribute.h"
65#include "zebra/interface.h"
66#include "zebra/debug.h"
67#include "zebra/rtadv.h"
68#include "zebra/zebra_ptm.h"
69#include "zebra/zebra_mpls.h"
70#include "zebra/kernel_netlink.h"
d9f5b2f5 71#include "zebra/rt_netlink.h"
1fdc9eae 72#include "zebra/if_netlink.h"
9df414fe 73#include "zebra/zebra_errors.h"
97c4e1d0 74#include "zebra/zebra_vxlan.h"
42b56639 75#include "zebra/zebra_evpn_mh.h"
e3d3fa06 76#include "zebra/zebra_l2.h"
ebb61fca 77#include "zebra/netconf_netlink.h"
d42e6142 78#include "zebra/zebra_trace.h"
1fdc9eae 79
0268f30e 80extern struct zebra_privs_t zserv_privs;
5d414138 81uint8_t frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
1fdc9eae 82
83/* Note: on netlink systems, there should be a 1-to-1 mapping between interface
84 names and ifindex values. */
d62a17ae 85static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
86 struct zebra_ns *zns)
1fdc9eae 87{
d62a17ae 88 struct interface *oifp;
89
90 if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
91 && (oifp != ifp)) {
92 if (ifi_index == IFINDEX_INTERNAL)
af4c2728 93 flog_err(
450971aa 94 EC_LIB_INTERFACE,
4d43f68a 95 "Netlink is setting interface %s ifindex to reserved internal value %u",
d62a17ae 96 ifp->name, ifi_index);
97 else {
98 if (IS_ZEBRA_DEBUG_KERNEL)
99 zlog_debug(
100 "interface index %d was renamed from %s to %s",
101 ifi_index, oifp->name, ifp->name);
102 if (if_is_up(oifp))
af4c2728 103 flog_err(
450971aa 104 EC_LIB_INTERFACE,
4d43f68a 105 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
d62a17ae 106 ifi_index, oifp->name, ifp->name);
d0438da6 107 if_delete_update(&oifp);
d62a17ae 108 }
109 }
ff880b78 110 if_set_index(ifp, ifi_index);
1fdc9eae 111}
112
113/* Utility function to parse hardware link-layer address and update ifp */
d62a17ae 114static void netlink_interface_update_hw_addr(struct rtattr **tb,
115 struct interface *ifp)
1fdc9eae 116{
d62a17ae 117 int i;
118
119 if (tb[IFLA_ADDRESS]) {
120 int hw_addr_len;
121
122 hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
123
124 if (hw_addr_len > INTERFACE_HWADDR_MAX)
9df414fe
QY
125 zlog_debug("Hardware address is too large: %d",
126 hw_addr_len);
d62a17ae 127 else {
128 ifp->hw_addr_len = hw_addr_len;
129 memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
130 hw_addr_len);
131
132 for (i = 0; i < hw_addr_len; i++)
133 if (ifp->hw_addr[i] != 0)
134 break;
135
136 if (i == hw_addr_len)
137 ifp->hw_addr_len = 0;
138 else
139 ifp->hw_addr_len = hw_addr_len;
140 }
141 }
1fdc9eae 142}
143
d62a17ae 144static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
1fdc9eae 145{
d62a17ae 146 switch (hwt) {
147 case ARPHRD_ETHER:
148 return ZEBRA_LLT_ETHER;
149 case ARPHRD_EETHER:
150 return ZEBRA_LLT_EETHER;
151 case ARPHRD_AX25:
152 return ZEBRA_LLT_AX25;
153 case ARPHRD_PRONET:
154 return ZEBRA_LLT_PRONET;
155 case ARPHRD_IEEE802:
156 return ZEBRA_LLT_IEEE802;
157 case ARPHRD_ARCNET:
158 return ZEBRA_LLT_ARCNET;
159 case ARPHRD_APPLETLK:
160 return ZEBRA_LLT_APPLETLK;
161 case ARPHRD_DLCI:
162 return ZEBRA_LLT_DLCI;
163 case ARPHRD_ATM:
164 return ZEBRA_LLT_ATM;
165 case ARPHRD_METRICOM:
166 return ZEBRA_LLT_METRICOM;
167 case ARPHRD_IEEE1394:
168 return ZEBRA_LLT_IEEE1394;
169 case ARPHRD_EUI64:
170 return ZEBRA_LLT_EUI64;
171 case ARPHRD_INFINIBAND:
172 return ZEBRA_LLT_INFINIBAND;
173 case ARPHRD_SLIP:
174 return ZEBRA_LLT_SLIP;
175 case ARPHRD_CSLIP:
176 return ZEBRA_LLT_CSLIP;
177 case ARPHRD_SLIP6:
178 return ZEBRA_LLT_SLIP6;
179 case ARPHRD_CSLIP6:
180 return ZEBRA_LLT_CSLIP6;
181 case ARPHRD_RSRVD:
182 return ZEBRA_LLT_RSRVD;
183 case ARPHRD_ADAPT:
184 return ZEBRA_LLT_ADAPT;
185 case ARPHRD_ROSE:
186 return ZEBRA_LLT_ROSE;
187 case ARPHRD_X25:
188 return ZEBRA_LLT_X25;
189 case ARPHRD_PPP:
190 return ZEBRA_LLT_PPP;
191 case ARPHRD_CISCO:
192 return ZEBRA_LLT_CHDLC;
193 case ARPHRD_LAPB:
194 return ZEBRA_LLT_LAPB;
195 case ARPHRD_RAWHDLC:
196 return ZEBRA_LLT_RAWHDLC;
197 case ARPHRD_TUNNEL:
198 return ZEBRA_LLT_IPIP;
199 case ARPHRD_TUNNEL6:
200 return ZEBRA_LLT_IPIP6;
201 case ARPHRD_FRAD:
202 return ZEBRA_LLT_FRAD;
203 case ARPHRD_SKIP:
204 return ZEBRA_LLT_SKIP;
205 case ARPHRD_LOOPBACK:
206 return ZEBRA_LLT_LOOPBACK;
207 case ARPHRD_LOCALTLK:
208 return ZEBRA_LLT_LOCALTLK;
209 case ARPHRD_FDDI:
210 return ZEBRA_LLT_FDDI;
211 case ARPHRD_SIT:
212 return ZEBRA_LLT_SIT;
213 case ARPHRD_IPDDP:
214 return ZEBRA_LLT_IPDDP;
215 case ARPHRD_IPGRE:
216 return ZEBRA_LLT_IPGRE;
217 case ARPHRD_PIMREG:
218 return ZEBRA_LLT_PIMREG;
219 case ARPHRD_HIPPI:
220 return ZEBRA_LLT_HIPPI;
221 case ARPHRD_ECONET:
222 return ZEBRA_LLT_ECONET;
223 case ARPHRD_IRDA:
224 return ZEBRA_LLT_IRDA;
225 case ARPHRD_FCPP:
226 return ZEBRA_LLT_FCPP;
227 case ARPHRD_FCAL:
228 return ZEBRA_LLT_FCAL;
229 case ARPHRD_FCPL:
230 return ZEBRA_LLT_FCPL;
231 case ARPHRD_FCFABRIC:
232 return ZEBRA_LLT_FCFABRIC;
233 case ARPHRD_IEEE802_TR:
234 return ZEBRA_LLT_IEEE802_TR;
235 case ARPHRD_IEEE80211:
236 return ZEBRA_LLT_IEEE80211;
4268e09e 237#ifdef ARPHRD_IEEE802154
d62a17ae 238 case ARPHRD_IEEE802154:
239 return ZEBRA_LLT_IEEE802154;
4268e09e 240#endif
1fdc9eae 241#ifdef ARPHRD_IP6GRE
d62a17ae 242 case ARPHRD_IP6GRE:
243 return ZEBRA_LLT_IP6GRE;
1fdc9eae 244#endif
245#ifdef ARPHRD_IEEE802154_PHY
d62a17ae 246 case ARPHRD_IEEE802154_PHY:
247 return ZEBRA_LLT_IEEE802154_PHY;
1fdc9eae 248#endif
249
d62a17ae 250 default:
251 return ZEBRA_LLT_UNKNOWN;
252 }
1fdc9eae 253}
254
42b56639 255static inline void zebra_if_set_ziftype(struct interface *ifp,
e6f2bec0 256 enum zebra_iftype zif_type,
a81982fa 257 enum zebra_slave_iftype zif_slave_type)
42b56639
AK
258{
259 struct zebra_if *zif;
260
261 zif = (struct zebra_if *)ifp->info;
262 zif->zif_slave_type = zif_slave_type;
263
264 if (zif->zif_type != zif_type) {
265 zif->zif_type = zif_type;
266 /* If the if_type has been set to bond initialize ES info
267 * against it. XXX - note that we don't handle the case where
268 * a zif changes from bond to non-bond; it is really
269 * an unexpected/error condition.
270 */
271 zebra_evpn_if_init(zif);
272 }
273}
274
b9368db9 275static void netlink_determine_zebra_iftype(const char *kind,
e6f2bec0 276 enum zebra_iftype *zif_type)
6675513d 277{
d62a17ae 278 *zif_type = ZEBRA_IF_OTHER;
279
280 if (!kind)
281 return;
282
283 if (strcmp(kind, "vrf") == 0)
284 *zif_type = ZEBRA_IF_VRF;
285 else if (strcmp(kind, "bridge") == 0)
286 *zif_type = ZEBRA_IF_BRIDGE;
287 else if (strcmp(kind, "vlan") == 0)
288 *zif_type = ZEBRA_IF_VLAN;
289 else if (strcmp(kind, "vxlan") == 0)
290 *zif_type = ZEBRA_IF_VXLAN;
1a98c087
MK
291 else if (strcmp(kind, "macvlan") == 0)
292 *zif_type = ZEBRA_IF_MACVLAN;
0e4864ea
PG
293 else if (strcmp(kind, "veth") == 0)
294 *zif_type = ZEBRA_IF_VETH;
b9368db9
DD
295 else if (strcmp(kind, "bond") == 0)
296 *zif_type = ZEBRA_IF_BOND;
297 else if (strcmp(kind, "bond_slave") == 0)
298 *zif_type = ZEBRA_IF_BOND_SLAVE;
077c07cc
PG
299 else if (strcmp(kind, "gre") == 0)
300 *zif_type = ZEBRA_IF_GRE;
6675513d 301}
52d8f0d8 302
d62a17ae 303static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
5e031198 304 uint32_t ns_id, const char *name)
1fdc9eae 305{
d62a17ae 306 struct ifinfomsg *ifi;
307 struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
308 struct rtattr *attr[IFLA_VRF_MAX + 1];
75d26fb3 309 struct vrf *vrf = NULL;
d62a17ae 310 struct zebra_vrf *zvrf;
d7c0a89a 311 uint32_t nl_table_id;
d62a17ae 312
313 ifi = NLMSG_DATA(h);
314
c9d842c7 315 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
d62a17ae 316
317 if (!linkinfo[IFLA_INFO_DATA]) {
318 if (IS_ZEBRA_DEBUG_KERNEL)
319 zlog_debug(
320 "%s: IFLA_INFO_DATA missing from VRF message: %s",
321 __func__, name);
322 return;
323 }
324
c9d842c7
DS
325 netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
326 linkinfo[IFLA_INFO_DATA]);
d62a17ae 327 if (!attr[IFLA_VRF_TABLE]) {
328 if (IS_ZEBRA_DEBUG_KERNEL)
329 zlog_debug(
330 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
331 __func__, name);
332 return;
1fdc9eae 333 }
334
d7c0a89a 335 nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
d62a17ae 336
337 if (h->nlmsg_type == RTM_NEWLINK) {
338 if (IS_ZEBRA_DEBUG_KERNEL)
339 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
340 ifi->ifi_index, nl_table_id);
341
2e86d16d
RW
342 if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
343 vrf_id_t exist_id;
5e031198 344
2e86d16d
RW
345 exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
346 if (exist_id != VRF_DEFAULT) {
347 vrf = vrf_lookup_by_id(exist_id);
348
349 flog_err(
350 EC_ZEBRA_VRF_MISCONFIGURED,
351 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
352 name, ifi->ifi_index, vrf->name);
353 exit(-1);
354 }
5e031198 355 }
2e86d16d 356
75d26fb3 357 vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
d62a17ae 358 if (!vrf) {
450971aa 359 flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
1c50c1c0 360 name, ifi->ifi_index);
d62a17ae 361 return;
362 }
363
d62a17ae 364 /*
365 * This is the only place that we get the actual kernel table_id
366 * being used. We need it to set the table_id of the routes
367 * we are passing to the kernel.... And to throw some totally
368 * awesome parties. that too.
593406a1
DS
369 *
370 * At this point we *must* have a zvrf because the vrf_create
371 * callback creates one. We *must* set the table id
372 * before the vrf_enable because of( at the very least )
373 * static routes being delayed for installation until
374 * during the vrf_enable callbacks.
d62a17ae 375 */
376 zvrf = (struct zebra_vrf *)vrf->info;
377 zvrf->table_id = nl_table_id;
593406a1
DS
378
379 /* Enable the created VRF. */
380 if (!vrf_enable(vrf)) {
450971aa 381 flog_err(EC_LIB_INTERFACE,
1c50c1c0
QY
382 "Failed to enable VRF %s id %u", name,
383 ifi->ifi_index);
593406a1
DS
384 return;
385 }
386
d62a17ae 387 } else // h->nlmsg_type == RTM_DELLINK
388 {
389 if (IS_ZEBRA_DEBUG_KERNEL)
390 zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
391 ifi->ifi_index);
392
393 vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
394
395 if (!vrf) {
e914ccbe 396 flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
9df414fe 397 __func__);
d62a17ae 398 return;
399 }
400
401 vrf_delete(vrf);
402 }
1fdc9eae 403}
404
67188ca2 405static uint32_t get_iflink_speed(struct interface *interface, int *error)
535fe877 406{
d62a17ae 407 struct ifreq ifdata;
408 struct ethtool_cmd ecmd;
409 int sd;
410 int rc;
0268f30e 411 const char *ifname = interface->name;
d62a17ae 412
594c2878
JF
413 if (error)
414 *error = 0;
d62a17ae 415 /* initialize struct */
416 memset(&ifdata, 0, sizeof(ifdata));
417
418 /* set interface name */
0af35d90 419 strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
d62a17ae 420
421 /* initialize ethtool interface */
422 memset(&ecmd, 0, sizeof(ecmd));
423 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
ba85366a 424 ifdata.ifr_data = (caddr_t)&ecmd;
d62a17ae 425
d2fb26ef 426 /* use ioctl to get speed of an interface */
0cf6db21 427 frr_with_privs(&zserv_privs) {
01b9e3fd 428 sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
096f7609 429 interface->vrf->vrf_id, NULL);
01b9e3fd
DL
430 if (sd < 0) {
431 if (IS_ZEBRA_DEBUG_KERNEL)
432 zlog_debug("Failure to read interface %s speed: %d %s",
433 ifname, errno, safe_strerror(errno));
594c2878
JF
434 /* no vrf socket creation may probably mean vrf issue */
435 if (error)
436 *error = -1;
01b9e3fd
DL
437 return 0;
438 }
d2fb26ef 439 /* Get the current link state for the interface */
096f7609 440 rc = vrf_ioctl(interface->vrf->vrf_id, sd, SIOCETHTOOL,
633fc9b1 441 (char *)&ifdata);
01b9e3fd 442 }
d62a17ae 443 if (rc < 0) {
f767cee4 444 if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
bd7d0299
DS
445 zlog_debug(
446 "IOCTL failure to read interface %s speed: %d %s",
447 ifname, errno, safe_strerror(errno));
594c2878
JF
448 /* no device means interface unreachable */
449 if (errno == ENODEV && error)
450 *error = -1;
d62a17ae 451 ecmd.speed_hi = 0;
452 ecmd.speed = 0;
453 }
454
455 close(sd);
456
67188ca2 457 return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
535fe877
DS
458}
459
594c2878 460uint32_t kernel_get_speed(struct interface *ifp, int *error)
dc7b3cae 461{
594c2878 462 return get_iflink_speed(ifp, error);
dc7b3cae
DS
463}
464
62b4b7e4
PG
465static ssize_t
466netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
467 size_t buflen)
468{
469 struct {
470 struct nlmsghdr n;
471 struct ifinfomsg ifi;
472 char buf[];
473 } *req = buf;
474 uint32_t link_idx;
db51f0cd 475 unsigned int mtu;
62b4b7e4 476 struct rtattr *rta_info, *rta_data;
e3d3fa06 477 const struct zebra_l2info_gre *gre_info;
62b4b7e4
PG
478
479 if (buflen < sizeof(*req))
480 return 0;
481 memset(req, 0, sizeof(*req));
482
483 req->n.nlmsg_type = RTM_NEWLINK;
484 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
485 req->n.nlmsg_flags = NLM_F_REQUEST;
486
487 req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx);
e3d3fa06
PG
488
489 gre_info = dplane_ctx_gre_get_info(ctx);
490 if (!gre_info)
491 return 0;
492
62b4b7e4
PG
493 req->ifi.ifi_change = 0xFFFFFFFF;
494 link_idx = dplane_ctx_gre_get_link_ifindex(ctx);
db51f0cd
PG
495 mtu = dplane_ctx_gre_get_mtu(ctx);
496
497 if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu))
498 return 0;
62b4b7e4
PG
499
500 rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO);
501 if (!rta_info)
502 return 0;
e3d3fa06 503
62b4b7e4
PG
504 if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3))
505 return 0;
e3d3fa06 506
62b4b7e4 507 rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA);
e3d3fa06 508 if (!rta_data)
62b4b7e4 509 return 0;
e3d3fa06 510
62b4b7e4
PG
511 if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx))
512 return 0;
e3d3fa06
PG
513
514 if (gre_info->vtep_ip.s_addr &&
515 !nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL,
516 gre_info->vtep_ip.s_addr))
517 return 0;
518
519 if (gre_info->vtep_ip_remote.s_addr &&
520 !nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE,
521 gre_info->vtep_ip_remote.s_addr))
522 return 0;
523
524 if (gre_info->ikey &&
525 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
526 gre_info->ikey))
527 return 0;
528 if (gre_info->okey &&
529 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
530 gre_info->okey))
531 return 0;
532
62b4b7e4
PG
533 nl_attr_nest_end(&req->n, rta_data);
534 nl_attr_nest_end(&req->n, rta_info);
535
536 return NLMSG_ALIGN(req->n.nlmsg_len);
537}
538
d62a17ae 539static int netlink_extract_bridge_info(struct rtattr *link_data,
540 struct zebra_l2info_bridge *bridge_info)
6675513d 541{
d62a17ae 542 struct rtattr *attr[IFLA_BR_MAX + 1];
543
544 memset(bridge_info, 0, sizeof(*bridge_info));
c9d842c7 545 netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
d62a17ae 546 if (attr[IFLA_BR_VLAN_FILTERING])
784d88aa 547 bridge_info->bridge.vlan_aware =
d7c0a89a 548 *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
d62a17ae 549 return 0;
6675513d 550}
551
d62a17ae 552static int netlink_extract_vlan_info(struct rtattr *link_data,
553 struct zebra_l2info_vlan *vlan_info)
6675513d 554{
d62a17ae 555 struct rtattr *attr[IFLA_VLAN_MAX + 1];
556 vlanid_t vid_in_msg;
557
558 memset(vlan_info, 0, sizeof(*vlan_info));
c9d842c7 559 netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
d62a17ae 560 if (!attr[IFLA_VLAN_ID]) {
561 if (IS_ZEBRA_DEBUG_KERNEL)
562 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
563 return -1;
564 }
565
566 vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
567 vlan_info->vid = vid_in_msg;
568 return 0;
6675513d 569}
570
077c07cc
PG
571static int netlink_extract_gre_info(struct rtattr *link_data,
572 struct zebra_l2info_gre *gre_info)
573{
574 struct rtattr *attr[IFLA_GRE_MAX + 1];
575
576 memset(gre_info, 0, sizeof(*gre_info));
577 memset(attr, 0, sizeof(attr));
269b69d7 578 netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
077c07cc
PG
579
580 if (!attr[IFLA_GRE_LOCAL]) {
581 if (IS_ZEBRA_DEBUG_KERNEL)
582 zlog_debug(
583 "IFLA_GRE_LOCAL missing from GRE IF message");
584 } else
585 gre_info->vtep_ip =
586 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]);
587 if (!attr[IFLA_GRE_REMOTE]) {
588 if (IS_ZEBRA_DEBUG_KERNEL)
589 zlog_debug(
590 "IFLA_GRE_REMOTE missing from GRE IF message");
591 } else
592 gre_info->vtep_ip_remote =
593 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]);
594
595 if (!attr[IFLA_GRE_LINK]) {
596 if (IS_ZEBRA_DEBUG_KERNEL)
597 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
62b4b7e4 598 } else {
077c07cc
PG
599 gre_info->ifindex_link =
600 *(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]);
62b4b7e4
PG
601 if (IS_ZEBRA_DEBUG_KERNEL)
602 zlog_debug("IFLA_GRE_LINK obtained is %u",
603 gre_info->ifindex_link);
604 }
077c07cc
PG
605 if (attr[IFLA_GRE_IKEY])
606 gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]);
607 if (attr[IFLA_GRE_OKEY])
608 gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]);
609 return 0;
610}
611
d62a17ae 612static int netlink_extract_vxlan_info(struct rtattr *link_data,
613 struct zebra_l2info_vxlan *vxl_info)
6675513d 614{
784d88aa 615 uint8_t svd = 0;
d62a17ae 616 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
617 vni_t vni_in_msg;
618 struct in_addr vtep_ip_in_msg;
14ddb3d9 619 ifindex_t ifindex_link;
d62a17ae 620
621 memset(vxl_info, 0, sizeof(*vxl_info));
c9d842c7 622 netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
784d88aa
SR
623 if (attr[IFLA_VXLAN_COLLECT_METADATA]) {
624 svd = *(uint8_t *)RTA_DATA(attr[IFLA_VXLAN_COLLECT_METADATA]);
d62a17ae 625 if (IS_ZEBRA_DEBUG_KERNEL)
626 zlog_debug(
784d88aa
SR
627 "IFLA_VXLAN_COLLECT_METADATA=%u in VXLAN IF message",
628 svd);
629 }
630
631 if (!svd) {
9464e5b8
SR
632 /*
633 * In case of svd we will not get vni info directly from the
634 * device
635 */
784d88aa
SR
636 if (!attr[IFLA_VXLAN_ID]) {
637 if (IS_ZEBRA_DEBUG_KERNEL)
638 zlog_debug(
639 "IFLA_VXLAN_ID missing from VXLAN IF message");
640 return -1;
641 }
642
643 vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_VNI;
644 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
645 vxl_info->vni_info.vni.vni = vni_in_msg;
646 } else {
647 vxl_info->vni_info.iftype = ZEBRA_VXLAN_IF_SVD;
d62a17ae 648 }
649
d62a17ae 650 if (!attr[IFLA_VXLAN_LOCAL]) {
651 if (IS_ZEBRA_DEBUG_KERNEL)
652 zlog_debug(
653 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
654 } else {
655 vtep_ip_in_msg =
656 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
657 vxl_info->vtep_ip = vtep_ip_in_msg;
658 }
659
d7fe235c 660 if (attr[IFLA_VXLAN_GROUP]) {
784d88aa
SR
661 if (!svd)
662 vxl_info->vni_info.vni.mcast_grp =
663 *(struct in_addr *)RTA_DATA(
664 attr[IFLA_VXLAN_GROUP]);
d7fe235c
AK
665 }
666
14ddb3d9
PG
667 if (!attr[IFLA_VXLAN_LINK]) {
668 if (IS_ZEBRA_DEBUG_KERNEL)
3efd0893 669 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
14ddb3d9
PG
670 } else {
671 ifindex_link =
672 *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
673 vxl_info->ifindex_link = ifindex_link;
674 }
d62a17ae 675 return 0;
6675513d 676}
677
678/*
679 * Extract and save L2 params (of interest) for an interface. When a
680 * bridge interface is added or updated, take further actions to map
681 * its members. Likewise, for VxLAN interface.
682 */
d62a17ae 683static void netlink_interface_update_l2info(struct interface *ifp,
14ddb3d9
PG
684 struct rtattr *link_data, int add,
685 ns_id_t link_nsid)
6675513d 686{
d62a17ae 687 if (!link_data)
688 return;
689
690 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
691 struct zebra_l2info_bridge bridge_info;
692
693 netlink_extract_bridge_info(link_data, &bridge_info);
694 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
695 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
696 struct zebra_l2info_vlan vlan_info;
697
698 netlink_extract_vlan_info(link_data, &vlan_info);
699 zebra_l2_vlanif_update(ifp, &vlan_info);
243b74ed
AK
700 zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
701 !!if_is_operative(ifp));
d62a17ae 702 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
703 struct zebra_l2info_vxlan vxlan_info;
704
705 netlink_extract_vxlan_info(link_data, &vxlan_info);
14ddb3d9 706 vxlan_info.link_nsid = link_nsid;
d62a17ae 707 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
14ddb3d9
PG
708 if (link_nsid != NS_UNKNOWN &&
709 vxlan_info.ifindex_link)
710 zebra_if_update_link(ifp, vxlan_info.ifindex_link,
711 link_nsid);
077c07cc
PG
712 } else if (IS_ZEBRA_IF_GRE(ifp)) {
713 struct zebra_l2info_gre gre_info;
714
715 netlink_extract_gre_info(link_data, &gre_info);
716 gre_info.link_nsid = link_nsid;
717 zebra_l2_greif_add_update(ifp, &gre_info, add);
718 if (link_nsid != NS_UNKNOWN &&
719 gre_info.ifindex_link)
720 zebra_if_update_link(ifp, gre_info.ifindex_link,
721 link_nsid);
d62a17ae 722 }
6675513d 723}
724
131a9a2e
SR
725static int netlink_bridge_vxlan_vlan_vni_map_update(struct interface *ifp,
726 struct rtattr *af_spec)
727{
728 int rem;
729 vni_t vni_id;
730 vlanid_t vid;
731 uint16_t flags;
732 struct rtattr *i;
733 struct zebra_vxlan_vni vni;
734 struct zebra_vxlan_vni *vnip;
735 struct hash *vni_table = NULL;
736 struct zebra_vxlan_vni vni_end;
737 struct zebra_vxlan_vni vni_start;
738 struct rtattr *aftb[IFLA_BRIDGE_VLAN_TUNNEL_MAX + 1];
739
e8a392d9
SR
740 memset(&vni_start, 0, sizeof(vni_start));
741 memset(&vni_end, 0, sizeof(vni_end));
742
131a9a2e
SR
743 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec); RTA_OK(i, rem);
744 i = RTA_NEXT(i, rem)) {
745
746 if (i->rta_type != IFLA_BRIDGE_VLAN_TUNNEL_INFO)
747 continue;
748
749 memset(aftb, 0, sizeof(aftb));
750 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_VLAN_TUNNEL_MAX,
751 i);
0bbad9d1
SW
752 if (!aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID] ||
753 !aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID])
131a9a2e
SR
754 /* vlan-vni info missing */
755 return 0;
756
757 flags = 0;
758 memset(&vni, 0, sizeof(vni));
759
760 vni.vni = *(vni_t *)RTA_DATA(aftb[IFLA_BRIDGE_VLAN_TUNNEL_ID]);
761 vni.access_vlan = *(vlanid_t *)RTA_DATA(
762 aftb[IFLA_BRIDGE_VLAN_TUNNEL_VID]);
763
764 if (aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS])
765 flags = *(uint16_t *)RTA_DATA(
766 aftb[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS]);
767
768 if (flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
769 vni_start = vni;
770 continue;
771 }
772
773 if (flags & BRIDGE_VLAN_INFO_RANGE_END)
774 vni_end = vni;
775
776 if (!(flags & BRIDGE_VLAN_INFO_RANGE_END)) {
777 vni_start = vni;
778 vni_end = vni;
779 }
780
781 if (IS_ZEBRA_DEBUG_KERNEL)
782 zlog_debug(
783 "Vlan-Vni(%d:%d-%d:%d) update for VxLAN IF %s(%u)",
784 vni_start.access_vlan, vni_end.access_vlan,
785 vni_start.vni, vni_end.vni, ifp->name,
786 ifp->ifindex);
787
788 if (!vni_table) {
789 vni_table = zebra_vxlan_vni_table_create();
790 if (!vni_table)
791 return 0;
792 }
793
794 for (vid = vni_start.access_vlan, vni_id = vni_start.vni;
795 vid <= vni_end.access_vlan; vid++, vni_id++) {
796
797 memset(&vni, 0, sizeof(vni));
798 vni.vni = vni_id;
799 vni.access_vlan = vid;
800 vnip = hash_get(vni_table, &vni, zebra_vxlan_vni_alloc);
801 if (!vnip)
802 return 0;
803 }
804
805 memset(&vni_start, 0, sizeof(vni_start));
806 memset(&vni_end, 0, sizeof(vni_end));
807 }
808
809 if (vni_table)
810 zebra_vxlan_if_vni_table_add_update(ifp, vni_table);
811
812 return 0;
813}
814
42b56639
AK
815static int netlink_bridge_vxlan_update(struct interface *ifp,
816 struct rtattr *af_spec)
817{
818 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
819 struct bridge_vlan_info *vinfo;
131a9a2e 820 struct zebra_if *zif;
42b56639
AK
821 vlanid_t access_vlan;
822
ec897751 823 if (!af_spec)
824 return 0;
825
131a9a2e
SR
826 zif = (struct zebra_if *)ifp->info;
827
828 /* Single vxlan devices has vni-vlan range to update */
829 if (IS_ZEBRA_VXLAN_IF_SVD(zif))
830 return netlink_bridge_vxlan_vlan_vni_map_update(ifp, af_spec);
831
42b56639
AK
832 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
833 * only 1 access VLAN is accepted.
834 */
c9d842c7 835 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
42b56639
AK
836 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
837 return 0;
838
839 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
840 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
841 return 0;
842
843 access_vlan = (vlanid_t)vinfo->vid;
844 if (IS_ZEBRA_DEBUG_KERNEL)
845 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
846 ifp->name, ifp->ifindex);
847 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
848 return 0;
849}
850
851static void netlink_bridge_vlan_update(struct interface *ifp,
852 struct rtattr *af_spec)
853{
854 struct rtattr *i;
855 int rem;
856 uint16_t vid_range_start = 0;
857 struct zebra_if *zif;
858 bitfield_t old_vlan_bitmap;
859 struct bridge_vlan_info *vinfo;
860
861 zif = (struct zebra_if *)ifp->info;
862
863 /* cache the old bitmap addrs */
864 old_vlan_bitmap = zif->vlan_bitmap;
865 /* create a new bitmap space for re-eval */
866 bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
867
ec897751 868 if (af_spec) {
869 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
870 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
42b56639 871
ec897751 872 if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
873 continue;
42b56639 874
ec897751 875 vinfo = RTA_DATA(i);
42b56639 876
ec897751 877 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
878 vid_range_start = vinfo->vid;
879 continue;
880 }
42b56639 881
ec897751 882 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
883 vid_range_start = vinfo->vid;
42b56639 884
ec897751 885 zebra_vlan_bitmap_compute(ifp, vid_range_start,
886 vinfo->vid);
887 }
42b56639
AK
888 }
889
890 zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
891
892 bf_free(old_vlan_bitmap);
893}
894
d62a17ae 895static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
896 int startup)
6675513d 897{
d62a17ae 898 char *name = NULL;
899 struct ifinfomsg *ifi;
900 struct rtattr *tb[IFLA_MAX + 1];
901 struct interface *ifp;
42b56639
AK
902 struct zebra_if *zif;
903 struct rtattr *af_spec;
d62a17ae 904
905 /* Fetch name and ifindex */
906 ifi = NLMSG_DATA(h);
d62a17ae 907 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
908
909 if (tb[IFLA_IFNAME] == NULL)
910 return -1;
911 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
912
913 /* The interface should already be known, if not discard. */
914 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
915 if (!ifp) {
9df414fe
QY
916 zlog_debug("Cannot find bridge IF %s(%u)", name,
917 ifi->ifi_index);
d62a17ae 918 return 0;
919 }
d62a17ae 920
921 /* We are only interested in the access VLAN i.e., AF_SPEC */
42b56639 922 af_spec = tb[IFLA_AF_SPEC];
d62a17ae 923
42b56639
AK
924 if (IS_ZEBRA_IF_VXLAN(ifp))
925 return netlink_bridge_vxlan_update(ifp, af_spec);
d62a17ae 926
42b56639
AK
927 /* build vlan bitmap associated with this interface if that
928 * device type is interested in the vlans
929 */
930 zif = (struct zebra_if *)ifp->info;
931 if (bf_is_inited(zif->vlan_bitmap))
932 netlink_bridge_vlan_update(ifp, af_spec);
d62a17ae 933
d62a17ae 934 return 0;
6675513d 935}
936
ed7a1622 937static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield)
0dcd8506
SW
938{
939 /* This shouldn't be possible */
940 assert(frr_protodown_r_bit < 32);
941 return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
942}
943
944/*
945 * Process interface protodown dplane update.
946 *
947 * If the interface is an es bond member then it must follow EVPN's
948 * protodown setting.
c36e442c
AK
949 */
950static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
0dcd8506 951 struct rtattr **tb)
c36e442c 952{
0dcd8506
SW
953 bool protodown;
954 bool old_protodown;
955 uint32_t rc_bitfield = 0;
956 struct rtattr *pd_reason_info[IFLA_MAX + 1];
c36e442c 957
0dcd8506
SW
958 protodown = !!*(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
959
960 if (tb[IFLA_PROTO_DOWN_REASON]) {
961 netlink_parse_rtattr_nested(pd_reason_info, IFLA_INFO_MAX,
962 tb[IFLA_PROTO_DOWN_REASON]);
c36e442c 963
0dcd8506
SW
964 if (pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE])
965 rc_bitfield = *(uint32_t *)RTA_DATA(
966 pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE]);
967 }
968
969 /*
970 * Set our reason code to note it wasn't us.
971 * If the reason we got from the kernel is ONLY frr though, don't
972 * set it.
973 */
7140b00c
SW
974 COND_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_EXTERNAL,
975 protodown && rc_bitfield &&
976 !is_if_protodown_reason_only_frr(rc_bitfield));
977
5d414138 978
d89b3008 979 old_protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
0dcd8506 980 if (protodown == old_protodown)
c36e442c
AK
981 return;
982
983 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
984 zlog_debug("interface %s dplane change, protdown %s",
985 zif->ifp->name, protodown ? "on" : "off");
986
7140b00c
SW
987 /* Set protodown, respectively */
988 COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, protodown);
0dcd8506 989
c36e442c 990 if (zebra_evpn_is_es_bond_member(zif->ifp)) {
4b82b954 991 /* Check it's not already being sent to the dplane first */
47c1d76a
SW
992 if (protodown &&
993 CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN)) {
994 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
995 zlog_debug(
996 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
997 zif->ifp->name);
4b82b954 998 return;
47c1d76a 999 }
4b82b954 1000
47c1d76a
SW
1001 if (!protodown &&
1002 CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN)) {
1003 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
1004 zlog_debug(
1005 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
1006 zif->ifp->name);
4b82b954 1007 return;
47c1d76a 1008 }
4b82b954 1009
c36e442c
AK
1010 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
1011 zlog_debug(
47c1d76a 1012 "bond mbr %s reinstate protodown %s in the dplane",
0dcd8506
SW
1013 zif->ifp->name, old_protodown ? "on" : "off");
1014
1015 if (old_protodown)
7140b00c 1016 SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
c36e442c 1017 else
7140b00c 1018 SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);
0dcd8506
SW
1019
1020 dplane_intf_update(zif->ifp);
c36e442c
AK
1021 }
1022}
1023
00a7710c
AK
1024static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
1025{
1026 uint8_t bypass = 0;
1027 struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
1028
c9d842c7
DS
1029 netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
1030 linkinfo[IFLA_INFO_SLAVE_DATA]);
00a7710c
AK
1031 if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
1032 bypass = *(uint8_t *)RTA_DATA(
1033 mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
1034
1035 return bypass;
1036}
1037
0dcd8506 1038/*
ab465d24
SW
1039 * Only called at startup to cleanup leftover protodown reasons we may
1040 * have not cleaned up. We leave protodown set though.
0dcd8506
SW
1041 */
1042static void if_sweep_protodown(struct zebra_if *zif)
1043{
1044 bool protodown;
1045
d89b3008 1046 protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
0dcd8506
SW
1047
1048 if (!protodown)
1049 return;
1050
1051 if (IS_ZEBRA_DEBUG_KERNEL)
ab465d24
SW
1052 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
1053 zif->ifp->name, protodown ? "on" : "off",
1054 zif->protodown_rc);
0dcd8506
SW
1055
1056 /* Only clear our reason codes, leave external if it was set */
7140b00c 1057 UNSET_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_ALL);
0dcd8506
SW
1058 dplane_intf_update(zif->ifp);
1059}
1060
2414abd3
DS
1061/*
1062 * Called from interface_lookup_netlink(). This function is only used
1063 * during bootstrap.
1064 */
1065static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1066{
d62a17ae 1067 int len;
1068 struct ifinfomsg *ifi;
1069 struct rtattr *tb[IFLA_MAX + 1];
1070 struct rtattr *linkinfo[IFLA_MAX + 1];
1071 struct interface *ifp;
1072 char *name = NULL;
1073 char *kind = NULL;
48884c6b 1074 char *desc = NULL;
d62a17ae 1075 char *slave_kind = NULL;
ea7ec261 1076 struct zebra_ns *zns = NULL;
d62a17ae 1077 vrf_id_t vrf_id = VRF_DEFAULT;
e6f2bec0 1078 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
a81982fa 1079 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
d62a17ae 1080 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1081 ifindex_t link_ifindex = IFINDEX_INTERNAL;
b9368db9 1082 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
520ebf72 1083 struct zebra_if *zif;
14ddb3d9 1084 ns_id_t link_nsid = ns_id;
00a7710c 1085 uint8_t bypass = 0;
d62a17ae 1086
d42e6142
DS
1087 frrtrace(3, frr_zebra, netlink_interface, h, ns_id, startup);
1088
d62a17ae 1089 zns = zebra_ns_lookup(ns_id);
1090 ifi = NLMSG_DATA(h);
1091
1092 if (h->nlmsg_type != RTM_NEWLINK)
1093 return 0;
1094
1095 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618 1096 if (len < 0) {
15569c58
DA
1097 zlog_err(
1098 "%s: Message received from netlink is of a broken size: %d %zu",
1099 __func__, h->nlmsg_len,
1100 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 1101 return -1;
9bdf8618 1102 }
d62a17ae 1103
1104 /* We are interested in some AF_BRIDGE notifications. */
1105 if (ifi->ifi_family == AF_BRIDGE)
1106 return netlink_bridge_interface(h, len, ns_id, startup);
1107
1108 /* Looking up interface name. */
0d6f7fd6 1109 memset(linkinfo, 0, sizeof(linkinfo));
0dcd8506
SW
1110 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
1111 NLA_F_NESTED);
1fdc9eae 1112
d62a17ae 1113 /* check for wireless messages to ignore */
1114 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1115 if (IS_ZEBRA_DEBUG_KERNEL)
1116 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1117 __func__);
1118 return 0;
1119 }
1fdc9eae 1120
d62a17ae 1121 if (tb[IFLA_IFNAME] == NULL)
1122 return -1;
1123 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1124
48884c6b
DS
1125 if (tb[IFLA_IFALIAS])
1126 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1127
d62a17ae 1128 if (tb[IFLA_LINKINFO]) {
c9d842c7
DS
1129 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1130 tb[IFLA_LINKINFO]);
1fdc9eae 1131
d62a17ae 1132 if (linkinfo[IFLA_INFO_KIND])
1133 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1134
d62a17ae 1135 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1136 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1137
b9368db9
DD
1138 if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
1139 netlink_determine_zebra_iftype("bond_slave", &zif_type);
1140 else
1141 netlink_determine_zebra_iftype(kind, &zif_type);
d62a17ae 1142 }
1143
1144 /* If VRF, create the VRF structure itself. */
78dd30b2 1145 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
5e031198 1146 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
d62a17ae 1147 vrf_id = (vrf_id_t)ifi->ifi_index;
1148 }
1149
1150 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1151 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1152 && !vrf_is_backend_netns()) {
d62a17ae 1153 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 1154 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1155 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
1156 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1157 bridge_ifindex =
1158 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
1159 } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
1160 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1161 bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
00a7710c 1162 bypass = netlink_parse_lacp_bypass(linkinfo);
d62a17ae 1163 } else
1164 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1165 }
78dd30b2
PG
1166 if (vrf_is_backend_netns())
1167 vrf_id = (vrf_id_t)ns_id;
a36898e7 1168
d62a17ae 1169 /* If linking to another interface, note it. */
1170 if (tb[IFLA_LINK])
1171 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1172
b1cc23b2 1173 if (tb[IFLA_LINK_NETNSID]) {
14ddb3d9 1174 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
b1cc23b2
PG
1175 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1176 }
14ddb3d9 1177
f60a1188 1178 ifp = if_get_by_name(name, vrf_id, NULL);
ea7ec261 1179 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
d5c65bf1 1180
d62a17ae 1181 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d62a17ae 1182 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
1183 ifp->metric = 0;
594c2878 1184 ifp->speed = get_iflink_speed(ifp, NULL);
d62a17ae 1185 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1186
1187 /* Set zebra interface type */
1188 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1189 if (IS_ZEBRA_IF_VRF(ifp))
1190 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1191
98efddf1
DS
1192 /*
1193 * Just set the @link/lower-device ifindex. During nldump interfaces are
520ebf72
AK
1194 * not ordered in any fashion so we may end up getting upper devices
1195 * before lower devices. We will setup the real linkage once the dump
98efddf1
DS
1196 * is complete.
1197 */
520ebf72
AK
1198 zif = (struct zebra_if *)ifp->info;
1199 zif->link_ifindex = link_ifindex;
d62a17ae 1200
ba5165ec 1201 if (desc) {
c0ce4875
MS
1202 XFREE(MTYPE_ZIF_DESC, zif->desc);
1203 zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
ba5165ec
DS
1204 }
1205
d62a17ae 1206 /* Hardware type and address. */
1207 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
ebb61fca 1208
d62a17ae 1209 netlink_interface_update_hw_addr(tb, ifp);
1210
1211 if_add_update(ifp);
1212
1213 /* Extract and save L2 interface information, take additional actions.
1214 */
14ddb3d9
PG
1215 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1216 1, link_nsid);
c36e442c
AK
1217 if (IS_ZEBRA_IF_BOND(ifp))
1218 zebra_l2if_update_bond(ifp, true);
d62a17ae 1219 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
c7620108
PG
1220 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
1221 ZEBRA_BRIDGE_NO_ACTION);
b9368db9 1222 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
00a7710c 1223 zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
d62a17ae 1224
c36e442c 1225 if (tb[IFLA_PROTO_DOWN]) {
0dcd8506
SW
1226 netlink_proc_dplane_if_protodown(zif, tb);
1227 if_sweep_protodown(zif);
c36e442c
AK
1228 }
1229
d62a17ae 1230 return 0;
1fdc9eae 1231}
1232
289602d7 1233/* Request for specific interface or address information from the kernel */
85a75f1e
MS
1234static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
1235 int type, uint32_t filter_mask)
289602d7 1236{
d62a17ae 1237 struct {
1238 struct nlmsghdr n;
1239 struct ifinfomsg ifm;
1240 char buf[256];
1241 } req;
1242
097ef2af
DS
1243 frrtrace(4, frr_zebra, netlink_request_intf_addr, netlink_cmd, family,
1244 type, filter_mask);
1245
d62a17ae 1246 /* Form the request, specifying filter (rtattr) if needed. */
1247 memset(&req, 0, sizeof(req));
1248 req.n.nlmsg_type = type;
718f9b0f 1249 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 1250 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1251 req.ifm.ifi_family = family;
1252
1253 /* Include filter, if specified. */
1254 if (filter_mask)
312a6bee 1255 nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
d62a17ae 1256
fd3f8e52 1257 return netlink_request(netlink_cmd, &req);
289602d7 1258}
1259
62b4b7e4
PG
1260enum netlink_msg_status
1261netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1262{
1263 enum dplane_op_e op;
1264 enum netlink_msg_status ret;
1265
1266 op = dplane_ctx_get_op(ctx);
1267 assert(op == DPLANE_OP_GRE_SET);
1268
1269 ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
1270
1271 return ret;
1272}
1273
1fdc9eae 1274/* Interface lookup by netlink socket. */
d62a17ae 1275int interface_lookup_netlink(struct zebra_ns *zns)
1fdc9eae 1276{
d62a17ae 1277 int ret;
85a75f1e
MS
1278 struct zebra_dplane_info dp_info;
1279 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1280
1281 /* Capture key info from ns struct */
1282 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 1283
1284 /* Get interface information. */
85a75f1e 1285 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
d62a17ae 1286 if (ret < 0)
1287 return ret;
85a75f1e 1288 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
9bfadae8 1289 true);
d62a17ae 1290 if (ret < 0)
1291 return ret;
1292
1293 /* Get interface information - for bridge interfaces. */
85a75f1e 1294 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
d62a17ae 1295 RTEXT_FILTER_BRVLAN);
1296 if (ret < 0)
1297 return ret;
85a75f1e 1298 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
9bfadae8 1299 true);
d62a17ae 1300 if (ret < 0)
1301 return ret;
1302
451165eb
DS
1303 /*
1304 * So netlink_tunneldump_read will initiate a request
1305 * per tunnel to get data. If we are on a kernel that
1306 * does not support this then we will get X error messages
1307 * (one per tunnel request )back which netlink_parse_info will
1308 * stop after the first one. So we need to read equivalent
1309 * error messages per tunnel then we can continue.
1310 * if we do not gather all the read failures then
1311 * later requests will not work right.
1312 */
acc8e687
CS
1313 ret = netlink_tunneldump_read(zns);
1314 if (ret < 0)
1315 return ret;
acc8e687 1316
520ebf72 1317 /* fixup linkages */
357b150d 1318 zebra_if_update_all_links(zns);
d2bec88a
SW
1319 return 0;
1320}
1321
1322/**
1323 * interface_addr_lookup_netlink() - Look up interface addresses
1324 *
1325 * @zns: Zebra netlink socket
1326 * Return: Result status
1327 */
1328static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1329{
1330 int ret;
1331 struct zebra_dplane_info dp_info;
1332 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1333
1334 /* Capture key info from ns struct */
1335 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
520ebf72 1336
d62a17ae 1337 /* Get IPv4 address of the interfaces. */
85a75f1e 1338 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
d62a17ae 1339 if (ret < 0)
1340 return ret;
85a75f1e 1341 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
9bfadae8 1342 0, true);
d62a17ae 1343 if (ret < 0)
1344 return ret;
1345
1346 /* Get IPv6 address of the interfaces. */
85a75f1e 1347 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
d62a17ae 1348 if (ret < 0)
1349 return ret;
85a75f1e 1350 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
9bfadae8 1351 0, true);
d62a17ae 1352 if (ret < 0)
1353 return ret;
1354
1355 return 0;
1fdc9eae 1356}
1357
e0ae31b8
DS
1358int kernel_interface_set_master(struct interface *master,
1359 struct interface *slave)
1360{
1361 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1362
1363 struct {
1364 struct nlmsghdr n;
1365 struct ifinfomsg ifa;
1366 char buf[NL_PKT_BUF_SIZE];
1367 } req;
1368
0d6f7fd6 1369 memset(&req, 0, sizeof(req));
e0ae31b8
DS
1370
1371 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1372 req.n.nlmsg_flags = NLM_F_REQUEST;
1373 req.n.nlmsg_type = RTM_SETLINK;
1374 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1375
1376 req.ifa.ifi_index = slave->ifindex;
1377
a757997c
JU
1378 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1379 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
e0ae31b8
DS
1380
1381 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
9bfadae8 1382 false);
e0ae31b8
DS
1383}
1384
1fdc9eae 1385/* Interface address modification. */
67e3369e
JU
1386static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1387 void *buf, size_t buflen)
1fdc9eae 1388{
d62a17ae 1389 int bytelen;
64168803
MS
1390 const struct prefix *p;
1391 int cmd;
1392 const char *label;
1fdc9eae 1393
d62a17ae 1394 struct {
1395 struct nlmsghdr n;
1396 struct ifaddrmsg ifa;
67e3369e
JU
1397 char buf[0];
1398 } *req = buf;
1399
1400 if (buflen < sizeof(*req))
1401 return 0;
1fdc9eae 1402
64168803 1403 p = dplane_ctx_get_intf_addr(ctx);
67e3369e 1404 memset(req, 0, sizeof(*req));
1fdc9eae 1405
64168803 1406 bytelen = (p->family == AF_INET ? 4 : 16);
1fdc9eae 1407
67e3369e
JU
1408 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1409 req->n.nlmsg_flags = NLM_F_REQUEST;
a55ba23f 1410
64168803
MS
1411 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1412 cmd = RTM_NEWADDR;
1413 else
1414 cmd = RTM_DELADDR;
1415
67e3369e
JU
1416 req->n.nlmsg_type = cmd;
1417 req->ifa.ifa_family = p->family;
1fdc9eae 1418
67e3369e 1419 req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1fdc9eae 1420
67e3369e
JU
1421 if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1422 return 0;
1fdc9eae 1423
64168803
MS
1424 if (p->family == AF_INET) {
1425 if (dplane_ctx_intf_is_connected(ctx)) {
1426 p = dplane_ctx_get_intf_dest(ctx);
67e3369e
JU
1427 if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1428 &p->u.prefix, bytelen))
1429 return 0;
0f3af738
JW
1430 } else if (cmd == RTM_NEWADDR) {
1431 struct in_addr broad = {
1432 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1433 p->prefixlen)
1434 };
67e3369e
JU
1435 if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1436 bytelen))
1437 return 0;
d62a17ae 1438 }
1439 }
1fdc9eae 1440
64168803 1441 /* p is now either address or destination/bcast addr */
67e3369e 1442 req->ifa.ifa_prefixlen = p->prefixlen;
e8d19a05 1443
64168803 1444 if (dplane_ctx_intf_is_secondary(ctx))
67e3369e 1445 SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1fdc9eae 1446
64168803
MS
1447 if (dplane_ctx_intf_has_label(ctx)) {
1448 label = dplane_ctx_get_intf_label(ctx);
67e3369e
JU
1449 if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1450 strlen(label) + 1))
1451 return 0;
64168803 1452 }
1fdc9eae 1453
67e3369e 1454 return NLMSG_ALIGN(req->n.nlmsg_len);
e86b71f1
PG
1455}
1456
67e3369e
JU
1457enum netlink_msg_status
1458netlink_put_address_update_msg(struct nl_batch *bth,
1459 struct zebra_dplane_ctx *ctx)
1460{
1461 return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1462 false);
e86b71f1
PG
1463}
1464
5d414138
SW
1465static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
1466 size_t buflen)
1467{
1468 enum dplane_op_e op;
1469 int cmd = 0;
1470
1471 op = dplane_ctx_get_op(ctx);
1472
1473 switch (op) {
1474 case DPLANE_OP_INTF_UPDATE:
1475 cmd = RTM_SETLINK;
1476 break;
1477 case DPLANE_OP_INTF_INSTALL:
1478 cmd = RTM_NEWLINK;
1479 break;
1480 case DPLANE_OP_INTF_DELETE:
1481 cmd = RTM_DELLINK;
1482 break;
a98701f0
DS
1483 case DPLANE_OP_NONE:
1484 case DPLANE_OP_ROUTE_INSTALL:
1485 case DPLANE_OP_ROUTE_UPDATE:
1486 case DPLANE_OP_ROUTE_DELETE:
1487 case DPLANE_OP_ROUTE_NOTIFY:
1488 case DPLANE_OP_NH_INSTALL:
1489 case DPLANE_OP_NH_UPDATE:
1490 case DPLANE_OP_NH_DELETE:
1491 case DPLANE_OP_LSP_INSTALL:
1492 case DPLANE_OP_LSP_DELETE:
1493 case DPLANE_OP_LSP_NOTIFY:
1494 case DPLANE_OP_LSP_UPDATE:
1495 case DPLANE_OP_PW_INSTALL:
1496 case DPLANE_OP_PW_UNINSTALL:
1497 case DPLANE_OP_SYS_ROUTE_ADD:
1498 case DPLANE_OP_SYS_ROUTE_DELETE:
1499 case DPLANE_OP_ADDR_INSTALL:
1500 case DPLANE_OP_ADDR_UNINSTALL:
1501 case DPLANE_OP_MAC_INSTALL:
1502 case DPLANE_OP_MAC_DELETE:
1503 case DPLANE_OP_NEIGH_INSTALL:
1504 case DPLANE_OP_NEIGH_UPDATE:
1505 case DPLANE_OP_NEIGH_DELETE:
1506 case DPLANE_OP_NEIGH_DISCOVER:
1507 case DPLANE_OP_VTEP_ADD:
1508 case DPLANE_OP_VTEP_DELETE:
1509 case DPLANE_OP_RULE_ADD:
1510 case DPLANE_OP_RULE_DELETE:
1511 case DPLANE_OP_RULE_UPDATE:
1512 case DPLANE_OP_BR_PORT_UPDATE:
1513 case DPLANE_OP_IPTABLE_ADD:
1514 case DPLANE_OP_IPTABLE_DELETE:
1515 case DPLANE_OP_IPSET_ADD:
1516 case DPLANE_OP_IPSET_ENTRY_ADD:
1517 case DPLANE_OP_IPSET_ENTRY_DELETE:
1518 case DPLANE_OP_IPSET_DELETE:
1519 case DPLANE_OP_NEIGH_IP_INSTALL:
1520 case DPLANE_OP_NEIGH_IP_DELETE:
1521 case DPLANE_OP_NEIGH_TABLE_UPDATE:
1522 case DPLANE_OP_GRE_SET:
1523 case DPLANE_OP_INTF_ADDR_ADD:
1524 case DPLANE_OP_INTF_ADDR_DEL:
1525 case DPLANE_OP_INTF_NETCONFIG:
1526 case DPLANE_OP_TC_QDISC_INSTALL:
1527 case DPLANE_OP_TC_QDISC_UNINSTALL:
1528 case DPLANE_OP_TC_CLASS_ADD:
1529 case DPLANE_OP_TC_CLASS_DELETE:
1530 case DPLANE_OP_TC_CLASS_UPDATE:
1531 case DPLANE_OP_TC_FILTER_ADD:
1532 case DPLANE_OP_TC_FILTER_DELETE:
1533 case DPLANE_OP_TC_FILTER_UPDATE:
5d414138
SW
1534 flog_err(
1535 EC_ZEBRA_NHG_FIB_UPDATE,
1536 "Context received for kernel interface update with incorrect OP code (%u)",
1537 op);
1538 return -1;
1539 }
1540
1541 return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
1542}
1543
1544enum netlink_msg_status
1545netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1546{
1547 return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
1548}
1549
2414abd3 1550int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1551{
d62a17ae 1552 int len;
1553 struct ifaddrmsg *ifa;
1554 struct rtattr *tb[IFA_MAX + 1];
1555 struct interface *ifp;
1556 void *addr;
1557 void *broad;
d7c0a89a 1558 uint8_t flags = 0;
d62a17ae 1559 char *label = NULL;
1560 struct zebra_ns *zns;
cde1af84 1561 uint32_t metric = METRIC_MAX;
9254efed 1562 uint32_t kernel_flags = 0;
d62a17ae 1563
14ed0615
DS
1564 frrtrace(3, frr_zebra, netlink_interface_addr, h, ns_id, startup);
1565
d62a17ae 1566 zns = zebra_ns_lookup(ns_id);
1567 ifa = NLMSG_DATA(h);
1568
8a1b681c 1569 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
9df414fe 1570 flog_warn(
e914ccbe 1571 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
1572 "Invalid address family: %u received from kernel interface addr change: %s",
1573 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1574 return 0;
8a1b681c 1575 }
d62a17ae 1576
1577 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1578 return 0;
1579
1580 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
9bdf8618 1581 if (len < 0) {
15569c58
DA
1582 zlog_err(
1583 "%s: Message received from netlink is of a broken size: %d %zu",
1584 __func__, h->nlmsg_len,
1585 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
d62a17ae 1586 return -1;
9bdf8618 1587 }
d62a17ae 1588
d62a17ae 1589 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1590
1591 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1592 if (ifp == NULL) {
8eec31ef
YY
1593 if (startup) {
1594 /* During startup, failure to lookup the referenced
1595 * interface should not be an error, so we have
1596 * downgraded this condition to warning, and we permit
1597 * the startup interface state retrieval to continue.
1598 */
1599 flog_warn(EC_LIB_INTERFACE,
1600 "%s: can't find interface by index %d",
1601 __func__, ifa->ifa_index);
1602 return 0;
1603 } else {
1604 flog_err(EC_LIB_INTERFACE,
1605 "%s: can't find interface by index %d",
1606 __func__, ifa->ifa_index);
1607 return -1;
1608 }
d62a17ae 1609 }
1610
9254efed
DS
1611 /* Flags passed through */
1612 if (tb[IFA_FLAGS])
1613 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1614 else
1615 kernel_flags = ifa->ifa_flags;
1616
d62a17ae 1617 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1618 {
1619 char buf[BUFSIZ];
6751c0f3 1620 zlog_debug("%s %s %s flags 0x%x:", __func__,
d62a17ae 1621 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
9254efed 1622 kernel_flags);
d62a17ae 1623 if (tb[IFA_LOCAL])
1624 zlog_debug(" IFA_LOCAL %s/%d",
1625 inet_ntop(ifa->ifa_family,
1626 RTA_DATA(tb[IFA_LOCAL]), buf,
1627 BUFSIZ),
1628 ifa->ifa_prefixlen);
1629 if (tb[IFA_ADDRESS])
1630 zlog_debug(" IFA_ADDRESS %s/%d",
1631 inet_ntop(ifa->ifa_family,
1632 RTA_DATA(tb[IFA_ADDRESS]), buf,
1633 BUFSIZ),
1634 ifa->ifa_prefixlen);
1635 if (tb[IFA_BROADCAST])
1636 zlog_debug(" IFA_BROADCAST %s/%d",
1637 inet_ntop(ifa->ifa_family,
1638 RTA_DATA(tb[IFA_BROADCAST]), buf,
1639 BUFSIZ),
1640 ifa->ifa_prefixlen);
1641 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1642 zlog_debug(" IFA_LABEL %s",
1643 (char *)RTA_DATA(tb[IFA_LABEL]));
1644
1645 if (tb[IFA_CACHEINFO]) {
1646 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1647 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1648 ci->ifa_prefered, ci->ifa_valid);
1649 }
1650 }
1651
1652 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1653 if (tb[IFA_LOCAL] == NULL)
1654 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1655 if (tb[IFA_ADDRESS] == NULL)
1656 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1657
1658 /* local interface address */
1659 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1660
1661 /* is there a peer address? */
1662 if (tb[IFA_ADDRESS]
1663 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1664 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1665 broad = RTA_DATA(tb[IFA_ADDRESS]);
1666 SET_FLAG(flags, ZEBRA_IFA_PEER);
1667 } else
1668 /* seeking a broadcast address */
1669 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1670 : NULL);
1671
1672 /* addr is primary key, SOL if we don't have one */
1673 if (addr == NULL) {
14a4d9d0 1674 zlog_debug("%s: Local Interface Address is NULL for %s",
1675 __func__, ifp->name);
d62a17ae 1676 return -1;
1677 }
1678
1679 /* Flags. */
9254efed 1680 if (kernel_flags & IFA_F_SECONDARY)
d62a17ae 1681 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1682
1683 /* Label */
1684 if (tb[IFA_LABEL])
1685 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1686
2e1cc436 1687 if (label && strcmp(ifp->name, label) == 0)
d62a17ae 1688 label = NULL;
1689
cde1af84
AK
1690 if (tb[IFA_RT_PRIORITY])
1691 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1692
d62a17ae 1693 /* Register interface address to the interface. */
1694 if (ifa->ifa_family == AF_INET) {
930571d2 1695 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
e17d9b2d 1696 zlog_err(
87b5d1b0
DS
1697 "Invalid prefix length: %u received from kernel interface addr change: %s",
1698 ifa->ifa_prefixlen,
1699 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1700 return -1;
930571d2 1701 }
20e879f9 1702
d62a17ae 1703 if (h->nlmsg_type == RTM_NEWADDR)
1704 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1705 ifa->ifa_prefixlen,
cde1af84
AK
1706 (struct in_addr *)broad, label,
1707 metric);
20e879f9
MS
1708 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1709 /* Delete with a peer address */
1710 connected_delete_ipv4(
1711 ifp, flags, (struct in_addr *)addr,
1712 ifa->ifa_prefixlen, broad);
1713 } else
d62a17ae 1714 connected_delete_ipv4(
1715 ifp, flags, (struct in_addr *)addr,
fd267f08 1716 ifa->ifa_prefixlen, NULL);
d62a17ae 1717 }
20e879f9 1718
d62a17ae 1719 if (ifa->ifa_family == AF_INET6) {
930571d2 1720 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
e17d9b2d 1721 zlog_err(
87b5d1b0
DS
1722 "Invalid prefix length: %u received from kernel interface addr change: %s",
1723 ifa->ifa_prefixlen,
1724 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1725 return -1;
930571d2 1726 }
d62a17ae 1727 if (h->nlmsg_type == RTM_NEWADDR) {
1728 /* Only consider valid addresses; we'll not get a
1729 * notification from
1730 * the kernel till IPv6 DAD has completed, but at init
1731 * time, Quagga
1732 * does query for and will receive all addresses.
1733 */
9254efed 1734 if (!(kernel_flags
d62a17ae 1735 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
60466a63
QY
1736 connected_add_ipv6(ifp, flags,
1737 (struct in6_addr *)addr,
60c0687a 1738 (struct in6_addr *)broad,
cde1af84
AK
1739 ifa->ifa_prefixlen, label,
1740 metric);
d62a17ae 1741 } else
1742 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
fd267f08 1743 NULL, ifa->ifa_prefixlen);
d62a17ae 1744 }
1745
2a181147
SW
1746 /*
1747 * Linux kernel does not send route delete on interface down/addr del
1748 * so we have to re-process routes it owns (i.e. kernel routes)
1749 */
1750 if (h->nlmsg_type != RTM_NEWADDR)
1751 rib_update(RIB_UPDATE_KERNEL);
1752
d62a17ae 1753 return 0;
1fdc9eae 1754}
1755
e7c2c198
MS
1756/*
1757 * Parse and validate an incoming interface address change message,
1758 * generating a dplane context object.
1759 * This runs in the dplane pthread; the context is enqueued to the
1760 * main pthread for processing.
1761 */
1762int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1763 int startup /*ignored*/)
1764{
1765 int len;
1766 struct ifaddrmsg *ifa;
1767 struct rtattr *tb[IFA_MAX + 1];
1768 void *addr;
1769 void *broad;
1770 char *label = NULL;
1771 uint32_t metric = METRIC_MAX;
1772 uint32_t kernel_flags = 0;
1773 struct zebra_dplane_ctx *ctx;
1774 struct prefix p;
1775
1776 ifa = NLMSG_DATA(h);
1777
1778 /* Validate message types */
1779 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1780 return 0;
1781
1782 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1783 if (IS_ZEBRA_DEBUG_KERNEL)
1784 zlog_debug("%s: %s: Invalid address family: %u",
1785 __func__, nl_msg_type_to_str(h->nlmsg_type),
1786 ifa->ifa_family);
1787 return 0;
1788 }
1789
1790 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1791 if (len < 0) {
1792 if (IS_ZEBRA_DEBUG_KERNEL)
1793 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1794 __func__, nl_msg_type_to_str(h->nlmsg_type),
1795 h->nlmsg_len,
1796 (size_t)NLMSG_LENGTH(
1797 sizeof(struct ifaddrmsg)));
1798 return -1;
1799 }
1800
1801 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1802
1803 /* Flags passed through */
1804 if (tb[IFA_FLAGS])
1805 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1806 else
1807 kernel_flags = ifa->ifa_flags;
1808
1809 if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1810 char buf[PREFIX_STRLEN];
1811
1812 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1813 nl_msg_type_to_str(h->nlmsg_type), ns_id,
1814 ifa->ifa_index, kernel_flags);
1815 if (tb[IFA_LOCAL])
1816 zlog_debug(" IFA_LOCAL %s/%d",
1817 inet_ntop(ifa->ifa_family,
1818 RTA_DATA(tb[IFA_LOCAL]), buf,
1819 sizeof(buf)),
1820 ifa->ifa_prefixlen);
1821 if (tb[IFA_ADDRESS])
1822 zlog_debug(" IFA_ADDRESS %s/%d",
1823 inet_ntop(ifa->ifa_family,
1824 RTA_DATA(tb[IFA_ADDRESS]), buf,
1825 sizeof(buf)),
1826 ifa->ifa_prefixlen);
1827 if (tb[IFA_BROADCAST])
1828 zlog_debug(" IFA_BROADCAST %s/%d",
1829 inet_ntop(ifa->ifa_family,
1830 RTA_DATA(tb[IFA_BROADCAST]), buf,
1831 sizeof(buf)),
1832 ifa->ifa_prefixlen);
1833 if (tb[IFA_LABEL])
1834 zlog_debug(" IFA_LABEL %s",
1835 (const char *)RTA_DATA(tb[IFA_LABEL]));
1836
1837 if (tb[IFA_CACHEINFO]) {
1838 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1839
1840 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1841 ci->ifa_prefered, ci->ifa_valid);
1842 }
1843 }
1844
1845 /* Validate prefix length */
1846
1847 if (ifa->ifa_family == AF_INET
1848 && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1849 if (IS_ZEBRA_DEBUG_KERNEL)
1850 zlog_debug("%s: %s: Invalid prefix length: %u",
1851 __func__, nl_msg_type_to_str(h->nlmsg_type),
1852 ifa->ifa_prefixlen);
1853 return -1;
1854 }
1855
1856 if (ifa->ifa_family == AF_INET6) {
1857 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1858 if (IS_ZEBRA_DEBUG_KERNEL)
1859 zlog_debug("%s: %s: Invalid prefix length: %u",
1860 __func__,
1861 nl_msg_type_to_str(h->nlmsg_type),
1862 ifa->ifa_prefixlen);
1863 return -1;
1864 }
1865
1866 /* Only consider valid addresses; we'll not get a kernel
1867 * notification till IPv6 DAD has completed, but at init
1868 * time, FRR does query for and will receive all addresses.
1869 */
1870 if (h->nlmsg_type == RTM_NEWADDR
1871 && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1872 if (IS_ZEBRA_DEBUG_KERNEL)
1873 zlog_debug("%s: %s: Invalid/tentative addr",
1874 __func__,
1875 nl_msg_type_to_str(h->nlmsg_type));
1876 return 0;
1877 }
1878 }
1879
1880 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1881 if (tb[IFA_LOCAL] == NULL)
1882 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1883 if (tb[IFA_ADDRESS] == NULL)
1884 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1885
1886 /* local interface address */
1887 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1888
1889 /* addr is primary key, SOL if we don't have one */
1890 if (addr == NULL) {
1891 if (IS_ZEBRA_DEBUG_KERNEL)
1892 zlog_debug("%s: %s: No local interface address",
1893 __func__, nl_msg_type_to_str(h->nlmsg_type));
1894 return -1;
1895 }
1896
1897 /* Allocate a context object, now that validation is done. */
1898 ctx = dplane_ctx_alloc();
1899 if (h->nlmsg_type == RTM_NEWADDR)
1900 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1901 else
1902 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1903
1904 dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1905 dplane_ctx_set_ns_id(ctx, ns_id);
1906
1907 /* Convert addr to prefix */
1908 memset(&p, 0, sizeof(p));
1909 p.family = ifa->ifa_family;
1910 p.prefixlen = ifa->ifa_prefixlen;
1911 if (p.family == AF_INET)
1912 p.u.prefix4 = *(struct in_addr *)addr;
1913 else
1914 p.u.prefix6 = *(struct in6_addr *)addr;
1915
1916 dplane_ctx_set_intf_addr(ctx, &p);
1917
1918 /* is there a peer address? */
1919 if (tb[IFA_ADDRESS]
1920 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1921 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1922 broad = RTA_DATA(tb[IFA_ADDRESS]);
1923 dplane_ctx_intf_set_connected(ctx);
1924 } else if (tb[IFA_BROADCAST]) {
1925 /* seeking a broadcast address */
1926 broad = RTA_DATA(tb[IFA_BROADCAST]);
1927 dplane_ctx_intf_set_broadcast(ctx);
1928 } else
1929 broad = NULL;
1930
1931 if (broad) {
1932 /* Convert addr to prefix */
1933 memset(&p, 0, sizeof(p));
1934 p.family = ifa->ifa_family;
1935 p.prefixlen = ifa->ifa_prefixlen;
1936 if (p.family == AF_INET)
1937 p.u.prefix4 = *(struct in_addr *)broad;
1938 else
1939 p.u.prefix6 = *(struct in6_addr *)broad;
1940
1941 dplane_ctx_set_intf_dest(ctx, &p);
1942 }
1943
1944 /* Flags. */
1945 if (kernel_flags & IFA_F_SECONDARY)
1946 dplane_ctx_intf_set_secondary(ctx);
1947
1948 /* Label */
1949 if (tb[IFA_LABEL]) {
1950 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1951 dplane_ctx_set_intf_label(ctx, label);
1952 }
1953
1954 if (tb[IFA_RT_PRIORITY])
1955 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1956
1957 dplane_ctx_set_intf_metric(ctx, metric);
1958
1959 /* Enqueue ctx for main pthread to process */
1960 dplane_provider_enqueue_to_zebra(ctx);
1961
1962 return 0;
1963}
1964
2414abd3 1965int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1966{
d62a17ae 1967 int len;
1968 struct ifinfomsg *ifi;
1969 struct rtattr *tb[IFLA_MAX + 1];
1970 struct rtattr *linkinfo[IFLA_MAX + 1];
1971 struct interface *ifp;
1972 char *name = NULL;
1973 char *kind = NULL;
48884c6b 1974 char *desc = NULL;
d62a17ae 1975 char *slave_kind = NULL;
1976 struct zebra_ns *zns;
1977 vrf_id_t vrf_id = VRF_DEFAULT;
e6f2bec0 1978 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
a81982fa 1979 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
d62a17ae 1980 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
b9368db9 1981 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
d62a17ae 1982 ifindex_t link_ifindex = IFINDEX_INTERNAL;
97c4e1d0 1983 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
ba5165ec 1984 struct zebra_if *zif;
14ddb3d9 1985 ns_id_t link_nsid = ns_id;
c36e442c 1986 ifindex_t master_infindex = IFINDEX_INTERNAL;
00a7710c 1987 uint8_t bypass = 0;
d62a17ae 1988
1989 zns = zebra_ns_lookup(ns_id);
1990 ifi = NLMSG_DATA(h);
1991
fe533c56 1992 /* assume if not default zns, then new VRF */
d62a17ae 1993 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1994 /* If this is not link add/delete message so print warning. */
6751c0f3 1995 zlog_debug("%s: wrong kernel message %s", __func__,
87b5d1b0 1996 nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1997 return 0;
1998 }
1999
8a1b681c
SW
2000 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
2001 || ifi->ifi_family == AF_INET6)) {
9df414fe 2002 flog_warn(
e914ccbe 2003 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
2004 "Invalid address family: %u received from kernel link change: %s",
2005 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
2006 return 0;
2007 }
2008
d62a17ae 2009 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618 2010 if (len < 0) {
15569c58
DA
2011 zlog_err(
2012 "%s: Message received from netlink is of a broken size %d %zu",
2013 __func__, h->nlmsg_len,
2014 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 2015 return -1;
9bdf8618 2016 }
d62a17ae 2017
2018 /* We are interested in some AF_BRIDGE notifications. */
2019 if (ifi->ifi_family == AF_BRIDGE)
2020 return netlink_bridge_interface(h, len, ns_id, startup);
2021
2022 /* Looking up interface name. */
0d6f7fd6 2023 memset(linkinfo, 0, sizeof(linkinfo));
0dcd8506
SW
2024 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
2025 NLA_F_NESTED);
1fdc9eae 2026
d62a17ae 2027 /* check for wireless messages to ignore */
2028 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
2029 if (IS_ZEBRA_DEBUG_KERNEL)
2030 zlog_debug("%s: ignoring IFLA_WIRELESS message",
2031 __func__);
2032 return 0;
2033 }
1fdc9eae 2034
d62a17ae 2035 if (tb[IFLA_IFNAME] == NULL)
2036 return -1;
2037 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 2038
e9f79fff
MS
2039 /* Must be valid string. */
2040 len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
2041 if (len < 2 || name[len - 1] != '\0') {
2042 if (IS_ZEBRA_DEBUG_KERNEL)
2043 zlog_debug("%s: invalid intf name", __func__);
2044 return -1;
2045 }
2046
d62a17ae 2047 if (tb[IFLA_LINKINFO]) {
c9d842c7
DS
2048 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
2049 tb[IFLA_LINKINFO]);
1fdc9eae 2050
d62a17ae 2051 if (linkinfo[IFLA_INFO_KIND])
2052 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 2053
d62a17ae 2054 if (linkinfo[IFLA_INFO_SLAVE_KIND])
2055 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 2056
d62a17ae 2057 netlink_determine_zebra_iftype(kind, &zif_type);
2058 }
6675513d 2059
d62a17ae 2060 /* If linking to another interface, note it. */
2061 if (tb[IFLA_LINK])
2062 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1fdc9eae 2063
b1cc23b2 2064 if (tb[IFLA_LINK_NETNSID]) {
14ddb3d9 2065 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
b1cc23b2
PG
2066 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
2067 }
48884c6b
DS
2068 if (tb[IFLA_IFALIAS]) {
2069 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
2070 }
2071
d62a17ae 2072 /* See if interface is present. */
2073 ifp = if_lookup_by_name_per_ns(zns, name);
2074
2075 if (h->nlmsg_type == RTM_NEWLINK) {
e4c5b3ba
IR
2076 /* If VRF, create or update the VRF structure itself. */
2077 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
2078 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
2079 vrf_id = (vrf_id_t)ifi->ifi_index;
2080 }
2081
d62a17ae 2082 if (tb[IFLA_MASTER]) {
78dd30b2
PG
2083 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
2084 && !vrf_is_backend_netns()) {
d62a17ae 2085 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
c36e442c
AK
2086 master_infindex = vrf_id =
2087 *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 2088 } else if (slave_kind
2089 && (strcmp(slave_kind, "bridge") == 0)) {
2090 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
c36e442c 2091 master_infindex = bridge_ifindex =
d62a17ae 2092 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
2093 } else if (slave_kind
2094 && (strcmp(slave_kind, "bond") == 0)) {
2095 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
c36e442c 2096 master_infindex = bond_ifindex =
b9368db9 2097 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
00a7710c 2098 bypass = netlink_parse_lacp_bypass(linkinfo);
d62a17ae 2099 } else
2100 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
2101 }
fe533c56
PG
2102 if (vrf_is_backend_netns())
2103 vrf_id = (vrf_id_t)ns_id;
d62a17ae 2104 if (ifp == NULL
2105 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
2106 /* Add interface notification from kernel */
2107 if (IS_ZEBRA_DEBUG_KERNEL)
2108 zlog_debug(
3efd0893 2109 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
d62a17ae 2110 name, ifi->ifi_index, vrf_id, zif_type,
c36e442c 2111 zif_slave_type, master_infindex,
d62a17ae 2112 ifi->ifi_flags);
2113
2114 if (ifp == NULL) {
2115 /* unknown interface */
f60a1188 2116 ifp = if_get_by_name(name, vrf_id, NULL);
d62a17ae 2117 } else {
2118 /* pre-configured interface, learnt now */
096f7609 2119 if (ifp->vrf->vrf_id != vrf_id)
a36898e7 2120 if_update_to_new_vrf(ifp, vrf_id);
d62a17ae 2121 }
2122
2123 /* Update interface information. */
2124 set_ifindex(ifp, ifi->ifi_index, zns);
2125 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d23b983b 2126 if (!tb[IFLA_MTU]) {
9df414fe 2127 zlog_debug(
d23b983b
SW
2128 "RTM_NEWLINK for interface %s(%u) without MTU set",
2129 name, ifi->ifi_index);
2130 return 0;
2131 }
d62a17ae 2132 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2133 ifp->metric = 0;
2134 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
2135
2136 /* Set interface type */
2137 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
2138 if (IS_ZEBRA_IF_VRF(ifp))
2139 SET_FLAG(ifp->status,
2140 ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 2141
2142 /* Update link. */
86bad1cb 2143 zebra_if_update_link(ifp, link_ifindex, link_nsid);
d62a17ae 2144
8975bbbd
IR
2145 ifp->ll_type =
2146 netlink_to_zebra_link_type(ifi->ifi_type);
d62a17ae 2147 netlink_interface_update_hw_addr(tb, ifp);
2148
2149 /* Inform clients, install any configured addresses. */
2150 if_add_update(ifp);
2151
2152 /* Extract and save L2 interface information, take
2153 * additional actions. */
2154 netlink_interface_update_l2info(
14ddb3d9
PG
2155 ifp, linkinfo[IFLA_INFO_DATA],
2156 1, link_nsid);
d62a17ae 2157 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
c7620108
PG
2158 zebra_l2if_update_bridge_slave(
2159 ifp, bridge_ifindex, ns_id,
2160 ZEBRA_BRIDGE_NO_ACTION);
b9368db9 2161 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
00a7710c
AK
2162 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2163 !!bypass);
c36e442c 2164
0dcd8506
SW
2165 if (tb[IFLA_PROTO_DOWN])
2166 netlink_proc_dplane_if_protodown(ifp->info, tb);
c36e442c 2167
096f7609 2168 } else if (ifp->vrf->vrf_id != vrf_id) {
d62a17ae 2169 /* VRF change for an interface. */
2170 if (IS_ZEBRA_DEBUG_KERNEL)
2171 zlog_debug(
3efd0893 2172 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
096f7609
IR
2173 name, ifp->ifindex, ifp->vrf->vrf_id,
2174 vrf_id, ifi->ifi_flags);
d62a17ae 2175
a36898e7 2176 if_handle_vrf_change(ifp, vrf_id);
d62a17ae 2177 } else {
b9368db9 2178 bool was_bridge_slave, was_bond_slave;
c7620108 2179 uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
2d04bd98 2180 zif = ifp->info;
d62a17ae 2181
2182 /* Interface update. */
2183 if (IS_ZEBRA_DEBUG_KERNEL)
2184 zlog_debug(
3efd0893 2185 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
d62a17ae 2186 name, ifp->ifindex, zif_slave_type,
c36e442c 2187 master_infindex, ifi->ifi_flags);
d62a17ae 2188
2189 set_ifindex(ifp, ifi->ifi_index, zns);
d23b983b 2190 if (!tb[IFLA_MTU]) {
9df414fe 2191 zlog_debug(
d23b983b
SW
2192 "RTM_NEWLINK for interface %s(%u) without MTU set",
2193 name, ifi->ifi_index);
2194 return 0;
2195 }
d62a17ae 2196 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2197 ifp->metric = 0;
2198
2199 /* Update interface type - NOTE: Only slave_type can
2200 * change. */
2201 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
b9368db9 2202 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
d62a17ae 2203 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2204
97c4e1d0
CS
2205 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
2206
ecffe916 2207 /* Update link. */
86bad1cb 2208 zebra_if_update_link(ifp, link_ifindex, link_nsid);
ecffe916 2209
8975bbbd
IR
2210 ifp->ll_type =
2211 netlink_to_zebra_link_type(ifi->ifi_type);
d62a17ae 2212 netlink_interface_update_hw_addr(tb, ifp);
2213
0dcd8506
SW
2214 if (tb[IFLA_PROTO_DOWN])
2215 netlink_proc_dplane_if_protodown(ifp->info, tb);
2d04bd98 2216
d62a17ae 2217 if (if_is_no_ptm_operative(ifp)) {
8b48cdb9 2218 bool is_up = if_is_operative(ifp);
d62a17ae 2219 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2d04bd98
CS
2220 if (!if_is_no_ptm_operative(ifp) ||
2221 CHECK_FLAG(zif->flags,
2222 ZIF_FLAG_PROTODOWN)) {
d62a17ae 2223 if (IS_ZEBRA_DEBUG_KERNEL)
2224 zlog_debug(
2225 "Intf %s(%u) has gone DOWN",
2226 name, ifp->ifindex);
2227 if_down(ifp);
2a181147 2228 rib_update(RIB_UPDATE_KERNEL);
d62a17ae 2229 } else if (if_is_operative(ifp)) {
c7620108
PG
2230 bool mac_updated = false;
2231
d62a17ae 2232 /* Must notify client daemons of new
2233 * interface status. */
2234 if (IS_ZEBRA_DEBUG_KERNEL)
2235 zlog_debug(
2236 "Intf %s(%u) PTM up, notifying clients",
2237 name, ifp->ifindex);
8b48cdb9 2238 if_up(ifp, !is_up);
97c4e1d0
CS
2239
2240 /* Update EVPN VNI when SVI MAC change
2241 */
c7620108
PG
2242 if (memcmp(old_hw_addr, ifp->hw_addr,
2243 INTERFACE_HWADDR_MAX))
2244 mac_updated = true;
2245 if (IS_ZEBRA_IF_VLAN(ifp)
2246 && mac_updated) {
97c4e1d0
CS
2247 struct interface *link_if;
2248
2249 link_if =
2250 if_lookup_by_index_per_ns(
2251 zebra_ns_lookup(NS_DEFAULT),
2252 link_ifindex);
2253 if (link_if)
2254 zebra_vxlan_svi_up(ifp,
2255 link_if);
c7620108
PG
2256 } else if (mac_updated
2257 && IS_ZEBRA_IF_BRIDGE(ifp)) {
2258 zlog_debug(
2259 "Intf %s(%u) bridge changed MAC address",
2260 name, ifp->ifindex);
2261 chgflags =
2262 ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
97c4e1d0 2263 }
d62a17ae 2264 }
2265 } else {
2266 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2d04bd98
CS
2267 if (if_is_operative(ifp) &&
2268 !CHECK_FLAG(zif->flags,
2269 ZIF_FLAG_PROTODOWN)) {
d62a17ae 2270 if (IS_ZEBRA_DEBUG_KERNEL)
2271 zlog_debug(
2272 "Intf %s(%u) has come UP",
2273 name, ifp->ifindex);
8b48cdb9 2274 if_up(ifp, true);
f56a15b5
PG
2275 if (IS_ZEBRA_IF_BRIDGE(ifp))
2276 chgflags =
2277 ZEBRA_BRIDGE_MASTER_UP;
6f908ded
QY
2278 } else {
2279 if (IS_ZEBRA_DEBUG_KERNEL)
2280 zlog_debug(
7913714e 2281 "Intf %s(%u) has gone DOWN",
6f908ded
QY
2282 name, ifp->ifindex);
2283 if_down(ifp);
2a181147 2284 rib_update(RIB_UPDATE_KERNEL);
d62a17ae 2285 }
2286 }
2287
2288 /* Extract and save L2 interface information, take
2289 * additional actions. */
2290 netlink_interface_update_l2info(
14ddb3d9
PG
2291 ifp, linkinfo[IFLA_INFO_DATA],
2292 0, link_nsid);
c7620108
PG
2293 if (IS_ZEBRA_IF_BRIDGE(ifp))
2294 zebra_l2if_update_bridge(ifp, chgflags);
c36e442c
AK
2295 if (IS_ZEBRA_IF_BOND(ifp))
2296 zebra_l2if_update_bond(ifp, true);
d62a17ae 2297 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
c7620108
PG
2298 zebra_l2if_update_bridge_slave(
2299 ifp, bridge_ifindex, ns_id, chgflags);
b9368db9 2300 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
00a7710c
AK
2301 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2302 !!bypass);
d62a17ae 2303 }
26f13577
DS
2304
2305 zif = ifp->info;
2306 if (zif) {
c0ce4875 2307 XFREE(MTYPE_ZIF_DESC, zif->desc);
26f13577 2308 if (desc)
c0ce4875 2309 zif->desc = XSTRDUP(MTYPE_ZIF_DESC, desc);
26f13577 2310 }
d62a17ae 2311 } else {
2312 /* Delete interface notification from kernel */
2313 if (ifp == NULL) {
9df414fe
QY
2314 if (IS_ZEBRA_DEBUG_KERNEL)
2315 zlog_debug(
2316 "RTM_DELLINK for unknown interface %s(%u)",
2317 name, ifi->ifi_index);
d62a17ae 2318 return 0;
2319 }
2320
2321 if (IS_ZEBRA_DEBUG_KERNEL)
2322 zlog_debug("RTM_DELLINK for %s(%u)", name,
2323 ifp->ifindex);
2324
2325 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
2326
c36e442c
AK
2327 if (IS_ZEBRA_IF_BOND(ifp))
2328 zebra_l2if_update_bond(ifp, false);
00a7710c
AK
2329 if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2330 zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
d62a17ae 2331 /* Special handling for bridge or VxLAN interfaces. */
2332 if (IS_ZEBRA_IF_BRIDGE(ifp))
2333 zebra_l2_bridge_del(ifp);
2334 else if (IS_ZEBRA_IF_VXLAN(ifp))
2335 zebra_l2_vxlanif_del(ifp);
2336
d0438da6 2337 if_delete_update(&ifp);
e4c5b3ba
IR
2338
2339 /* If VRF, delete the VRF structure itself. */
2340 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
2341 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1fdc9eae 2342 }
2343
d62a17ae 2344 return 0;
1fdc9eae 2345}
718e3744 2346
5d414138
SW
2347/**
2348 * Interface encoding helper function.
2349 *
2350 * \param[in] cmd netlink command.
2351 * \param[in] ctx dataplane context (information snapshot).
2352 * \param[out] buf buffer to hold the packet.
2353 * \param[in] buflen amount of buffer bytes.
2354 */
c3bd894e 2355
5d414138
SW
2356ssize_t netlink_intf_msg_encode(uint16_t cmd,
2357 const struct zebra_dplane_ctx *ctx, void *buf,
2358 size_t buflen)
2359{
c3bd894e
QY
2360 struct {
2361 struct nlmsghdr n;
2362 struct ifinfomsg ifa;
5d414138
SW
2363 char buf[];
2364 } *req = buf;
c3bd894e 2365
5d414138
SW
2366 struct rtattr *nest_protodown_reason;
2367 ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
5d414138 2368 bool down = dplane_ctx_intf_is_protodown(ctx);
ab465d24 2369 bool pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
5d414138
SW
2370 struct nlsock *nl =
2371 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
c3bd894e 2372
5d414138
SW
2373 if (buflen < sizeof(*req))
2374 return 0;
c3bd894e 2375
5d414138 2376 memset(req, 0, sizeof(*req));
c3bd894e 2377
5d414138
SW
2378 if (cmd != RTM_SETLINK)
2379 flog_err(
2380 EC_ZEBRA_INTF_UPDATE_FAILURE,
2381 "Only RTM_SETLINK message type currently supported in dplane pthread");
c3bd894e 2382
5d414138
SW
2383 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2384 req->n.nlmsg_flags = NLM_F_REQUEST;
2385 req->n.nlmsg_type = cmd;
2386 req->n.nlmsg_pid = nl->snl.nl_pid;
2387
2388 req->ifa.ifi_index = ifindex;
2389
2390 nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
2391 nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
2392
0dcd8506
SW
2393 /* Reason info nest */
2394 nest_protodown_reason =
2395 nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
5d414138 2396
0dcd8506
SW
2397 if (!nest_protodown_reason)
2398 return -1;
5d414138 2399
0dcd8506
SW
2400 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
2401 (1 << frr_protodown_r_bit));
2402 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
ab465d24 2403 ((int)pd_reason_val) << frr_protodown_r_bit);
5d414138 2404
0dcd8506 2405 nl_attr_nest_end(&req->n, nest_protodown_reason);
5d414138
SW
2406
2407 if (IS_ZEBRA_DEBUG_KERNEL)
ab465d24
SW
2408 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2409 __func__, nl_msg_type_to_str(cmd), down,
2410 pd_reason_val, ifindex);
5d414138
SW
2411
2412 return NLMSG_ALIGN(req->n.nlmsg_len);
c3bd894e
QY
2413}
2414
718e3744 2415/* Interface information read by netlink. */
d62a17ae 2416void interface_list(struct zebra_ns *zns)
718e3744 2417{
d62a17ae 2418 interface_lookup_netlink(zns);
d9f5b2f5
SW
2419 /* We add routes for interface address,
2420 * so we need to get the nexthop info
2421 * from the kernel before we can do that
2422 */
81505946 2423 netlink_nexthop_read(zns);
cc4e0650 2424
d2bec88a 2425 interface_addr_lookup_netlink(zns);
718e3744 2426}
ddfeb486 2427
c40e1b1c
SW
2428void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
2429{
2430 if (IS_ZEBRA_DEBUG_KERNEL)
47c1d76a
SW
2431 zlog_debug(
2432 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2433 frr_protodown_r_bit, bit);
c40e1b1c
SW
2434
2435 frr_protodown_r_bit = bit;
2436}
2437
2438void if_netlink_unset_frr_protodown_r_bit(void)
2439{
2440 if (IS_ZEBRA_DEBUG_KERNEL)
47c1d76a
SW
2441 zlog_debug(
2442 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2443 frr_protodown_r_bit, FRR_PROTODOWN_REASON_DEFAULT_BIT);
c40e1b1c
SW
2444
2445 frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
2446}
2447
2448
2449bool if_netlink_frr_protodown_r_bit_is_set(void)
2450{
2451 return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
2452}
2453
2454uint8_t if_netlink_get_frr_protodown_r_bit(void)
2455{
2456 return frr_protodown_r_bit;
2457}
2458
acc8e687
CS
2459/**
2460 * netlink_request_tunneldump() - Request all tunnels from the linux kernel
2461 *
2462 * @zns: Zebra namespace
2463 * @family: AF_* netlink family
2464 * @type: RTM_* (RTM_GETTUNNEL) route type
2465 *
2466 * Return: Result status
2467 */
2468static int netlink_request_tunneldump(struct zebra_ns *zns, int family,
2469 int ifindex)
2470{
2471 struct {
2472 struct nlmsghdr n;
2473 struct tunnel_msg tmsg;
2474 char buf[256];
2475 } req;
2476
2477 /* Form the request */
2478 memset(&req, 0, sizeof(req));
2479 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg));
2480 req.n.nlmsg_type = RTM_GETTUNNEL;
2481 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2482 req.tmsg.family = family;
2483 req.tmsg.ifindex = ifindex;
2484
2485 return netlink_request(&zns->netlink_cmd, &req);
2486}
2487
2488/*
2489 * Currently we only ask for vxlan l3svd vni information.
2490 * In the future this can be expanded.
2491 */
2492int netlink_tunneldump_read(struct zebra_ns *zns)
2493{
2494 int ret = 0;
2495 struct zebra_dplane_info dp_info;
2496 struct route_node *rn;
2497 struct interface *tmp_if = NULL;
2498 struct zebra_if *zif;
451165eb 2499 struct nlsock *netlink_cmd = &zns->netlink_cmd;
acc8e687
CS
2500
2501 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2502
2503 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2504 tmp_if = (struct interface *)rn->info;
2505 if (!tmp_if)
2506 continue;
2507 zif = tmp_if->info;
2508 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2509 continue;
2510
2511 ret = netlink_request_tunneldump(zns, PF_BRIDGE,
2512 tmp_if->ifindex);
2513 if (ret < 0)
2514 return ret;
451165eb
DS
2515
2516 ret = netlink_parse_info(netlink_interface, netlink_cmd,
2517 &dp_info, 0, true);
2518
2519 if (ret < 0)
2520 return ret;
acc8e687 2521 }
451165eb 2522
acc8e687
CS
2523 return 0;
2524}
a26daa77
SW
2525
2526static const char *port_state2str(uint8_t state)
2527{
2528 switch (state) {
2529 case BR_STATE_DISABLED:
2530 return "DISABLED";
2531 case BR_STATE_LISTENING:
2532 return "LISTENING";
2533 case BR_STATE_LEARNING:
2534 return "LEARNING";
2535 case BR_STATE_FORWARDING:
2536 return "FORWARDING";
2537 case BR_STATE_BLOCKING:
2538 return "BLOCKING";
2539 }
2540
2541 return "UNKNOWN";
2542}
2543
2544static void vxlan_vni_state_change(struct zebra_if *zif, uint16_t id,
2545 uint8_t state)
2546{
2547 struct zebra_vxlan_vni *vnip;
2548
2549 vnip = zebra_vxlan_if_vlanid_vni_find(zif, id);
2550
2551 if (!vnip) {
2552 if (IS_ZEBRA_DEBUG_VXLAN)
2553 zlog_debug(
2554 "Cannot find VNI for VID (%u) IF %s for vlan state update",
2555 id, zif->ifp->name);
2556
2557 return;
2558 }
2559
2560 switch (state) {
2561 case BR_STATE_FORWARDING:
2562 zebra_vxlan_if_vni_up(zif->ifp, vnip);
2563 break;
2564 case BR_STATE_BLOCKING:
2565 zebra_vxlan_if_vni_down(zif->ifp, vnip);
2566 break;
2567 case BR_STATE_DISABLED:
2568 case BR_STATE_LISTENING:
2569 case BR_STATE_LEARNING:
2570 default:
2571 /* Not used for anything at the moment */
2572 break;
2573 }
2574}
2575
2576static void vlan_id_range_state_change(struct interface *ifp, uint16_t id_start,
2577 uint16_t id_end, uint8_t state)
2578{
2579 struct zebra_if *zif;
2580
2581 zif = (struct zebra_if *)ifp->info;
2582
2583 if (!zif)
2584 return;
2585
2586 for (uint16_t i = id_start; i <= id_end; i++)
2587 vxlan_vni_state_change(zif, i, state);
2588}
2589
2590/**
2591 * netlink_vlan_change() - Read in change about vlans from the kernel
2592 *
2593 * @h: Netlink message header
2594 * @ns_id: Namspace id
2595 * @startup: Are we reading under startup conditions?
2596 *
2597 * Return: Result status
2598 */
2599int netlink_vlan_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
2600{
2601 int len, rem;
2602 struct interface *ifp;
2603 struct br_vlan_msg *bvm;
2604 struct bridge_vlan_info *vinfo;
2605 struct rtattr *vtb[BRIDGE_VLANDB_ENTRY_MAX + 1] = {};
2606 struct rtattr *attr;
2607 uint8_t state;
2608 uint32_t vrange;
2609 int type;
2610
2611 /* We only care about state changes for now */
2612 if (!(h->nlmsg_type == RTM_NEWVLAN))
2613 return 0;
2614
2615 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct br_vlan_msg));
2616 if (len < 0) {
2617 zlog_warn(
2618 "%s: Message received from netlink is of a broken size %d %zu",
2619 __func__, h->nlmsg_len,
2620 (size_t)NLMSG_LENGTH(sizeof(struct br_vlan_msg)));
2621 return -1;
2622 }
2623
2624 bvm = NLMSG_DATA(h);
2625
2626 if (bvm->family != AF_BRIDGE)
2627 return 0;
2628
2629 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), bvm->ifindex);
2630 if (!ifp) {
2631 zlog_debug("Cannot find bridge-vlan IF (%u) for vlan update",
2632 bvm->ifindex);
2633 return 0;
2634 }
2635
2636 if (!IS_ZEBRA_IF_VXLAN(ifp)) {
2637 if (IS_ZEBRA_DEBUG_KERNEL)
2638 zlog_debug("Ignoring non-vxlan IF (%s) for vlan update",
2639 ifp->name);
2640
2641 return 0;
2642 }
2643
2644 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN)
2645 zlog_debug("%s %s IF %s NS %u",
2646 nl_msg_type_to_str(h->nlmsg_type),
2647 nl_family_to_str(bvm->family), ifp->name, ns_id);
2648
2649 /* Loop over "ALL" BRIDGE_VLANDB_ENTRY */
2650 rem = len;
2651 for (attr = BRVLAN_RTA(bvm); RTA_OK(attr, rem);
2652 attr = RTA_NEXT(attr, rem)) {
2653 vinfo = NULL;
a26daa77
SW
2654 vrange = 0;
2655
2656 type = attr->rta_type & NLA_TYPE_MASK;
2657
2658 if (type != BRIDGE_VLANDB_ENTRY)
2659 continue;
2660
2661 /* Parse nested entry data */
2662 netlink_parse_rtattr_nested(vtb, BRIDGE_VLANDB_ENTRY_MAX, attr);
2663
2664 /* It must have info for the ID */
2665 if (!vtb[BRIDGE_VLANDB_ENTRY_INFO])
2666 continue;
2667
2668 vinfo = (struct bridge_vlan_info *)RTA_DATA(
2669 vtb[BRIDGE_VLANDB_ENTRY_INFO]);
2670
2671 /*
2672 * We only care about state info, if there is none, just ignore
2673 * it.
2674 */
2675 if (!vtb[BRIDGE_VLANDB_ENTRY_STATE])
2676 continue;
2677
2678 state = *(uint8_t *)RTA_DATA(vtb[BRIDGE_VLANDB_ENTRY_STATE]);
2679
2680 if (vtb[BRIDGE_VLANDB_ENTRY_RANGE])
2681 vrange = *(uint32_t *)RTA_DATA(
2682 vtb[BRIDGE_VLANDB_ENTRY_RANGE]);
2683
2684 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_VXLAN) {
2685 if (vrange)
2686 zlog_debug("VLANDB_ENTRY: VID (%u-%u) state=%s",
2687 vinfo->vid, vrange,
2688 port_state2str(state));
2689 else
2690 zlog_debug("VLANDB_ENTRY: VID (%u) state=%s",
2691 vinfo->vid, port_state2str(state));
2692 }
2693
2694 vlan_id_range_state_change(
2695 ifp, vinfo->vid, (vrange ? vrange : vinfo->vid), state);
2696 }
2697
2698 return 0;
2699}
2700
2701/**
2702 * netlink_request_vlan() - Request vlan information from the kernel
2703 * @zns: Zebra namespace
2704 * @family: AF_* netlink family
2705 * @type: RTM_* type
2706 *
2707 * Return: Result status
2708 */
2709static int netlink_request_vlan(struct zebra_ns *zns, int family, int type)
2710{
2711 struct {
2712 struct nlmsghdr n;
2713 struct br_vlan_msg bvm;
2714 char buf[256];
2715 } req;
2716
2717 /* Form the request, specifying filter (rtattr) if needed. */
2718 memset(&req, 0, sizeof(req));
2719 req.n.nlmsg_type = type;
2720 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2721 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct br_vlan_msg));
2722 req.bvm.family = family;
2723
2724 nl_attr_put32(&req.n, sizeof(req), BRIDGE_VLANDB_DUMP_FLAGS,
2725 BRIDGE_VLANDB_DUMPF_STATS);
2726
2727 return netlink_request(&zns->netlink_cmd, &req);
2728}
2729
2730/**
2731 * netlink_vlan_read() - Vlan read function using netlink interface
2732 *
2733 * @zns: Zebra name space
2734 *
2735 * Return: Result status
2736 * Only called at bootstrap time.
2737 */
2738int netlink_vlan_read(struct zebra_ns *zns)
2739{
2740 int ret;
2741 struct zebra_dplane_info dp_info;
2742
2743 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2744
2745 /* Get bridg vlan info */
2746 ret = netlink_request_vlan(zns, PF_BRIDGE, RTM_GETVLAN);
2747 if (ret < 0)
2748 return ret;
2749
2750 ret = netlink_parse_info(netlink_vlan_change, &zns->netlink_cmd,
2751 &dp_info, 0, 1);
2752
2753 return ret;
2754}
2755
ddfeb486 2756#endif /* GNU_LINUX */