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