]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_netlink.c
Merge pull request #12047 from donaldsharp/bgp_nexthop_individual
[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])
547 bridge_info->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{
d62a17ae 615 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
616 vni_t vni_in_msg;
617 struct in_addr vtep_ip_in_msg;
14ddb3d9 618 ifindex_t ifindex_link;
d62a17ae 619
620 memset(vxl_info, 0, sizeof(*vxl_info));
c9d842c7 621 netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
d62a17ae 622 if (!attr[IFLA_VXLAN_ID]) {
623 if (IS_ZEBRA_DEBUG_KERNEL)
624 zlog_debug(
625 "IFLA_VXLAN_ID missing from VXLAN IF message");
626 return -1;
627 }
628
629 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
630 vxl_info->vni = vni_in_msg;
631 if (!attr[IFLA_VXLAN_LOCAL]) {
632 if (IS_ZEBRA_DEBUG_KERNEL)
633 zlog_debug(
634 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
635 } else {
636 vtep_ip_in_msg =
637 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
638 vxl_info->vtep_ip = vtep_ip_in_msg;
639 }
640
d7fe235c
AK
641 if (attr[IFLA_VXLAN_GROUP]) {
642 vxl_info->mcast_grp =
643 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_GROUP]);
644 }
645
14ddb3d9
PG
646 if (!attr[IFLA_VXLAN_LINK]) {
647 if (IS_ZEBRA_DEBUG_KERNEL)
3efd0893 648 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
14ddb3d9
PG
649 } else {
650 ifindex_link =
651 *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
652 vxl_info->ifindex_link = ifindex_link;
653 }
d62a17ae 654 return 0;
6675513d 655}
656
657/*
658 * Extract and save L2 params (of interest) for an interface. When a
659 * bridge interface is added or updated, take further actions to map
660 * its members. Likewise, for VxLAN interface.
661 */
d62a17ae 662static void netlink_interface_update_l2info(struct interface *ifp,
14ddb3d9
PG
663 struct rtattr *link_data, int add,
664 ns_id_t link_nsid)
6675513d 665{
d62a17ae 666 if (!link_data)
667 return;
668
669 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
670 struct zebra_l2info_bridge bridge_info;
671
672 netlink_extract_bridge_info(link_data, &bridge_info);
673 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
674 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
675 struct zebra_l2info_vlan vlan_info;
676
677 netlink_extract_vlan_info(link_data, &vlan_info);
678 zebra_l2_vlanif_update(ifp, &vlan_info);
243b74ed
AK
679 zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
680 !!if_is_operative(ifp));
d62a17ae 681 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
682 struct zebra_l2info_vxlan vxlan_info;
683
684 netlink_extract_vxlan_info(link_data, &vxlan_info);
14ddb3d9 685 vxlan_info.link_nsid = link_nsid;
d62a17ae 686 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
14ddb3d9
PG
687 if (link_nsid != NS_UNKNOWN &&
688 vxlan_info.ifindex_link)
689 zebra_if_update_link(ifp, vxlan_info.ifindex_link,
690 link_nsid);
077c07cc
PG
691 } else if (IS_ZEBRA_IF_GRE(ifp)) {
692 struct zebra_l2info_gre gre_info;
693
694 netlink_extract_gre_info(link_data, &gre_info);
695 gre_info.link_nsid = link_nsid;
696 zebra_l2_greif_add_update(ifp, &gre_info, add);
697 if (link_nsid != NS_UNKNOWN &&
698 gre_info.ifindex_link)
699 zebra_if_update_link(ifp, gre_info.ifindex_link,
700 link_nsid);
d62a17ae 701 }
6675513d 702}
703
42b56639
AK
704static int netlink_bridge_vxlan_update(struct interface *ifp,
705 struct rtattr *af_spec)
706{
707 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
708 struct bridge_vlan_info *vinfo;
709 vlanid_t access_vlan;
710
ec897751 711 if (!af_spec)
712 return 0;
713
42b56639
AK
714 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
715 * only 1 access VLAN is accepted.
716 */
c9d842c7 717 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
42b56639
AK
718 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
719 return 0;
720
721 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
722 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
723 return 0;
724
725 access_vlan = (vlanid_t)vinfo->vid;
726 if (IS_ZEBRA_DEBUG_KERNEL)
727 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
728 ifp->name, ifp->ifindex);
729 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
730 return 0;
731}
732
733static void netlink_bridge_vlan_update(struct interface *ifp,
734 struct rtattr *af_spec)
735{
736 struct rtattr *i;
737 int rem;
738 uint16_t vid_range_start = 0;
739 struct zebra_if *zif;
740 bitfield_t old_vlan_bitmap;
741 struct bridge_vlan_info *vinfo;
742
743 zif = (struct zebra_if *)ifp->info;
744
745 /* cache the old bitmap addrs */
746 old_vlan_bitmap = zif->vlan_bitmap;
747 /* create a new bitmap space for re-eval */
748 bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
749
ec897751 750 if (af_spec) {
751 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
752 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
42b56639 753
ec897751 754 if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
755 continue;
42b56639 756
ec897751 757 vinfo = RTA_DATA(i);
42b56639 758
ec897751 759 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
760 vid_range_start = vinfo->vid;
761 continue;
762 }
42b56639 763
ec897751 764 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
765 vid_range_start = vinfo->vid;
42b56639 766
ec897751 767 zebra_vlan_bitmap_compute(ifp, vid_range_start,
768 vinfo->vid);
769 }
42b56639
AK
770 }
771
772 zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
773
774 bf_free(old_vlan_bitmap);
775}
776
d62a17ae 777static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
778 int startup)
6675513d 779{
d62a17ae 780 char *name = NULL;
781 struct ifinfomsg *ifi;
782 struct rtattr *tb[IFLA_MAX + 1];
783 struct interface *ifp;
42b56639
AK
784 struct zebra_if *zif;
785 struct rtattr *af_spec;
d62a17ae 786
787 /* Fetch name and ifindex */
788 ifi = NLMSG_DATA(h);
d62a17ae 789 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
790
791 if (tb[IFLA_IFNAME] == NULL)
792 return -1;
793 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
794
795 /* The interface should already be known, if not discard. */
796 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
797 if (!ifp) {
9df414fe
QY
798 zlog_debug("Cannot find bridge IF %s(%u)", name,
799 ifi->ifi_index);
d62a17ae 800 return 0;
801 }
d62a17ae 802
803 /* We are only interested in the access VLAN i.e., AF_SPEC */
42b56639 804 af_spec = tb[IFLA_AF_SPEC];
d62a17ae 805
42b56639
AK
806 if (IS_ZEBRA_IF_VXLAN(ifp))
807 return netlink_bridge_vxlan_update(ifp, af_spec);
d62a17ae 808
42b56639
AK
809 /* build vlan bitmap associated with this interface if that
810 * device type is interested in the vlans
811 */
812 zif = (struct zebra_if *)ifp->info;
813 if (bf_is_inited(zif->vlan_bitmap))
814 netlink_bridge_vlan_update(ifp, af_spec);
d62a17ae 815
d62a17ae 816 return 0;
6675513d 817}
818
ed7a1622 819static bool is_if_protodown_reason_only_frr(uint32_t rc_bitfield)
0dcd8506
SW
820{
821 /* This shouldn't be possible */
822 assert(frr_protodown_r_bit < 32);
823 return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
824}
825
826/*
827 * Process interface protodown dplane update.
828 *
829 * If the interface is an es bond member then it must follow EVPN's
830 * protodown setting.
c36e442c
AK
831 */
832static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
0dcd8506 833 struct rtattr **tb)
c36e442c 834{
0dcd8506
SW
835 bool protodown;
836 bool old_protodown;
837 uint32_t rc_bitfield = 0;
838 struct rtattr *pd_reason_info[IFLA_MAX + 1];
c36e442c 839
0dcd8506
SW
840 protodown = !!*(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
841
842 if (tb[IFLA_PROTO_DOWN_REASON]) {
843 netlink_parse_rtattr_nested(pd_reason_info, IFLA_INFO_MAX,
844 tb[IFLA_PROTO_DOWN_REASON]);
c36e442c 845
0dcd8506
SW
846 if (pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE])
847 rc_bitfield = *(uint32_t *)RTA_DATA(
848 pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE]);
849 }
850
851 /*
852 * Set our reason code to note it wasn't us.
853 * If the reason we got from the kernel is ONLY frr though, don't
854 * set it.
855 */
7140b00c
SW
856 COND_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_EXTERNAL,
857 protodown && rc_bitfield &&
858 !is_if_protodown_reason_only_frr(rc_bitfield));
859
5d414138 860
d89b3008 861 old_protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
0dcd8506 862 if (protodown == old_protodown)
c36e442c
AK
863 return;
864
865 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
866 zlog_debug("interface %s dplane change, protdown %s",
867 zif->ifp->name, protodown ? "on" : "off");
868
7140b00c
SW
869 /* Set protodown, respectively */
870 COND_FLAG(zif->flags, ZIF_FLAG_PROTODOWN, protodown);
0dcd8506 871
c36e442c 872 if (zebra_evpn_is_es_bond_member(zif->ifp)) {
4b82b954 873 /* Check it's not already being sent to the dplane first */
47c1d76a
SW
874 if (protodown &&
875 CHECK_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN)) {
876 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
877 zlog_debug(
878 "bond mbr %s protodown on recv'd but already sent protodown on to the dplane",
879 zif->ifp->name);
4b82b954 880 return;
47c1d76a 881 }
4b82b954 882
47c1d76a
SW
883 if (!protodown &&
884 CHECK_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN)) {
885 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
886 zlog_debug(
887 "bond mbr %s protodown off recv'd but already sent protodown off to the dplane",
888 zif->ifp->name);
4b82b954 889 return;
47c1d76a 890 }
4b82b954 891
c36e442c
AK
892 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
893 zlog_debug(
47c1d76a 894 "bond mbr %s reinstate protodown %s in the dplane",
0dcd8506
SW
895 zif->ifp->name, old_protodown ? "on" : "off");
896
897 if (old_protodown)
7140b00c 898 SET_FLAG(zif->flags, ZIF_FLAG_SET_PROTODOWN);
c36e442c 899 else
7140b00c 900 SET_FLAG(zif->flags, ZIF_FLAG_UNSET_PROTODOWN);
0dcd8506
SW
901
902 dplane_intf_update(zif->ifp);
c36e442c
AK
903 }
904}
905
00a7710c
AK
906static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
907{
908 uint8_t bypass = 0;
909 struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
910
c9d842c7
DS
911 netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
912 linkinfo[IFLA_INFO_SLAVE_DATA]);
00a7710c
AK
913 if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
914 bypass = *(uint8_t *)RTA_DATA(
915 mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
916
917 return bypass;
918}
919
0dcd8506 920/*
ab465d24
SW
921 * Only called at startup to cleanup leftover protodown reasons we may
922 * have not cleaned up. We leave protodown set though.
0dcd8506
SW
923 */
924static void if_sweep_protodown(struct zebra_if *zif)
925{
926 bool protodown;
927
d89b3008 928 protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
0dcd8506
SW
929
930 if (!protodown)
931 return;
932
933 if (IS_ZEBRA_DEBUG_KERNEL)
ab465d24
SW
934 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
935 zif->ifp->name, protodown ? "on" : "off",
936 zif->protodown_rc);
0dcd8506
SW
937
938 /* Only clear our reason codes, leave external if it was set */
7140b00c 939 UNSET_FLAG(zif->protodown_rc, ZEBRA_PROTODOWN_ALL);
0dcd8506
SW
940 dplane_intf_update(zif->ifp);
941}
942
2414abd3
DS
943/*
944 * Called from interface_lookup_netlink(). This function is only used
945 * during bootstrap.
946 */
947static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 948{
d62a17ae 949 int len;
950 struct ifinfomsg *ifi;
951 struct rtattr *tb[IFLA_MAX + 1];
952 struct rtattr *linkinfo[IFLA_MAX + 1];
953 struct interface *ifp;
954 char *name = NULL;
955 char *kind = NULL;
48884c6b 956 char *desc = NULL;
d62a17ae 957 char *slave_kind = NULL;
ea7ec261 958 struct zebra_ns *zns = NULL;
d62a17ae 959 vrf_id_t vrf_id = VRF_DEFAULT;
e6f2bec0 960 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
a81982fa 961 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
d62a17ae 962 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
963 ifindex_t link_ifindex = IFINDEX_INTERNAL;
b9368db9 964 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
520ebf72 965 struct zebra_if *zif;
14ddb3d9 966 ns_id_t link_nsid = ns_id;
00a7710c 967 uint8_t bypass = 0;
d62a17ae 968
d42e6142
DS
969 frrtrace(3, frr_zebra, netlink_interface, h, ns_id, startup);
970
d62a17ae 971 zns = zebra_ns_lookup(ns_id);
972 ifi = NLMSG_DATA(h);
973
974 if (h->nlmsg_type != RTM_NEWLINK)
975 return 0;
976
977 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618 978 if (len < 0) {
15569c58
DA
979 zlog_err(
980 "%s: Message received from netlink is of a broken size: %d %zu",
981 __func__, h->nlmsg_len,
982 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 983 return -1;
9bdf8618 984 }
d62a17ae 985
986 /* We are interested in some AF_BRIDGE notifications. */
987 if (ifi->ifi_family == AF_BRIDGE)
988 return netlink_bridge_interface(h, len, ns_id, startup);
989
990 /* Looking up interface name. */
0d6f7fd6 991 memset(linkinfo, 0, sizeof(linkinfo));
0dcd8506
SW
992 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
993 NLA_F_NESTED);
1fdc9eae 994
d62a17ae 995 /* check for wireless messages to ignore */
996 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
997 if (IS_ZEBRA_DEBUG_KERNEL)
998 zlog_debug("%s: ignoring IFLA_WIRELESS message",
999 __func__);
1000 return 0;
1001 }
1fdc9eae 1002
d62a17ae 1003 if (tb[IFLA_IFNAME] == NULL)
1004 return -1;
1005 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1006
48884c6b
DS
1007 if (tb[IFLA_IFALIAS])
1008 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1009
d62a17ae 1010 if (tb[IFLA_LINKINFO]) {
c9d842c7
DS
1011 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1012 tb[IFLA_LINKINFO]);
1fdc9eae 1013
d62a17ae 1014 if (linkinfo[IFLA_INFO_KIND])
1015 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1016
d62a17ae 1017 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1018 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1019
b9368db9
DD
1020 if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
1021 netlink_determine_zebra_iftype("bond_slave", &zif_type);
1022 else
1023 netlink_determine_zebra_iftype(kind, &zif_type);
d62a17ae 1024 }
1025
1026 /* If VRF, create the VRF structure itself. */
78dd30b2 1027 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
5e031198 1028 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
d62a17ae 1029 vrf_id = (vrf_id_t)ifi->ifi_index;
1030 }
1031
1032 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1033 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1034 && !vrf_is_backend_netns()) {
d62a17ae 1035 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 1036 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1037 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
1038 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1039 bridge_ifindex =
1040 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
1041 } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
1042 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1043 bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
00a7710c 1044 bypass = netlink_parse_lacp_bypass(linkinfo);
d62a17ae 1045 } else
1046 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1047 }
78dd30b2
PG
1048 if (vrf_is_backend_netns())
1049 vrf_id = (vrf_id_t)ns_id;
a36898e7 1050
d62a17ae 1051 /* If linking to another interface, note it. */
1052 if (tb[IFLA_LINK])
1053 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1054
b1cc23b2 1055 if (tb[IFLA_LINK_NETNSID]) {
14ddb3d9 1056 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
b1cc23b2
PG
1057 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1058 }
14ddb3d9 1059
f60a1188 1060 ifp = if_get_by_name(name, vrf_id, NULL);
ea7ec261 1061 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
d5c65bf1 1062
d62a17ae 1063 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d62a17ae 1064 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
1065 ifp->metric = 0;
594c2878 1066 ifp->speed = get_iflink_speed(ifp, NULL);
d62a17ae 1067 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1068
1069 /* Set zebra interface type */
1070 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1071 if (IS_ZEBRA_IF_VRF(ifp))
1072 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1073
98efddf1
DS
1074 /*
1075 * Just set the @link/lower-device ifindex. During nldump interfaces are
520ebf72
AK
1076 * not ordered in any fashion so we may end up getting upper devices
1077 * before lower devices. We will setup the real linkage once the dump
98efddf1
DS
1078 * is complete.
1079 */
520ebf72
AK
1080 zif = (struct zebra_if *)ifp->info;
1081 zif->link_ifindex = link_ifindex;
d62a17ae 1082
ba5165ec
DS
1083 if (desc) {
1084 XFREE(MTYPE_TMP, zif->desc);
1085 zif->desc = XSTRDUP(MTYPE_TMP, desc);
1086 }
1087
d62a17ae 1088 /* Hardware type and address. */
1089 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
ebb61fca 1090
d62a17ae 1091 netlink_interface_update_hw_addr(tb, ifp);
1092
1093 if_add_update(ifp);
1094
1095 /* Extract and save L2 interface information, take additional actions.
1096 */
14ddb3d9
PG
1097 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1098 1, link_nsid);
c36e442c
AK
1099 if (IS_ZEBRA_IF_BOND(ifp))
1100 zebra_l2if_update_bond(ifp, true);
d62a17ae 1101 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
c7620108
PG
1102 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
1103 ZEBRA_BRIDGE_NO_ACTION);
b9368db9 1104 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
00a7710c 1105 zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
d62a17ae 1106
c36e442c 1107 if (tb[IFLA_PROTO_DOWN]) {
0dcd8506
SW
1108 netlink_proc_dplane_if_protodown(zif, tb);
1109 if_sweep_protodown(zif);
c36e442c
AK
1110 }
1111
d62a17ae 1112 return 0;
1fdc9eae 1113}
1114
289602d7 1115/* Request for specific interface or address information from the kernel */
85a75f1e
MS
1116static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
1117 int type, uint32_t filter_mask)
289602d7 1118{
d62a17ae 1119 struct {
1120 struct nlmsghdr n;
1121 struct ifinfomsg ifm;
1122 char buf[256];
1123 } req;
1124
097ef2af
DS
1125 frrtrace(4, frr_zebra, netlink_request_intf_addr, netlink_cmd, family,
1126 type, filter_mask);
1127
d62a17ae 1128 /* Form the request, specifying filter (rtattr) if needed. */
1129 memset(&req, 0, sizeof(req));
1130 req.n.nlmsg_type = type;
718f9b0f 1131 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 1132 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1133 req.ifm.ifi_family = family;
1134
1135 /* Include filter, if specified. */
1136 if (filter_mask)
312a6bee 1137 nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
d62a17ae 1138
fd3f8e52 1139 return netlink_request(netlink_cmd, &req);
289602d7 1140}
1141
62b4b7e4
PG
1142enum netlink_msg_status
1143netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1144{
1145 enum dplane_op_e op;
1146 enum netlink_msg_status ret;
1147
1148 op = dplane_ctx_get_op(ctx);
1149 assert(op == DPLANE_OP_GRE_SET);
1150
1151 ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
1152
1153 return ret;
1154}
1155
1fdc9eae 1156/* Interface lookup by netlink socket. */
d62a17ae 1157int interface_lookup_netlink(struct zebra_ns *zns)
1fdc9eae 1158{
d62a17ae 1159 int ret;
85a75f1e
MS
1160 struct zebra_dplane_info dp_info;
1161 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1162
1163 /* Capture key info from ns struct */
1164 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 1165
1166 /* Get interface information. */
85a75f1e 1167 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
d62a17ae 1168 if (ret < 0)
1169 return ret;
85a75f1e 1170 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
9bfadae8 1171 true);
d62a17ae 1172 if (ret < 0)
1173 return ret;
1174
1175 /* Get interface information - for bridge interfaces. */
85a75f1e 1176 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
d62a17ae 1177 RTEXT_FILTER_BRVLAN);
1178 if (ret < 0)
1179 return ret;
85a75f1e 1180 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
9bfadae8 1181 true);
d62a17ae 1182 if (ret < 0)
1183 return ret;
1184
acc8e687
CS
1185 ret = netlink_tunneldump_read(zns);
1186 if (ret < 0)
1187 return ret;
1188 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1189 true);
1190 if (ret < 0)
1191 return ret;
1192
520ebf72 1193 /* fixup linkages */
357b150d 1194 zebra_if_update_all_links(zns);
d2bec88a
SW
1195 return 0;
1196}
1197
1198/**
1199 * interface_addr_lookup_netlink() - Look up interface addresses
1200 *
1201 * @zns: Zebra netlink socket
1202 * Return: Result status
1203 */
1204static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1205{
1206 int ret;
1207 struct zebra_dplane_info dp_info;
1208 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1209
1210 /* Capture key info from ns struct */
1211 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
520ebf72 1212
d62a17ae 1213 /* Get IPv4 address of the interfaces. */
85a75f1e 1214 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
d62a17ae 1215 if (ret < 0)
1216 return ret;
85a75f1e 1217 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
9bfadae8 1218 0, true);
d62a17ae 1219 if (ret < 0)
1220 return ret;
1221
1222 /* Get IPv6 address of the interfaces. */
85a75f1e 1223 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
d62a17ae 1224 if (ret < 0)
1225 return ret;
85a75f1e 1226 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
9bfadae8 1227 0, true);
d62a17ae 1228 if (ret < 0)
1229 return ret;
1230
1231 return 0;
1fdc9eae 1232}
1233
e0ae31b8
DS
1234int kernel_interface_set_master(struct interface *master,
1235 struct interface *slave)
1236{
1237 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1238
1239 struct {
1240 struct nlmsghdr n;
1241 struct ifinfomsg ifa;
1242 char buf[NL_PKT_BUF_SIZE];
1243 } req;
1244
0d6f7fd6 1245 memset(&req, 0, sizeof(req));
e0ae31b8
DS
1246
1247 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1248 req.n.nlmsg_flags = NLM_F_REQUEST;
1249 req.n.nlmsg_type = RTM_SETLINK;
1250 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1251
1252 req.ifa.ifi_index = slave->ifindex;
1253
a757997c
JU
1254 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1255 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
e0ae31b8
DS
1256
1257 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
9bfadae8 1258 false);
e0ae31b8
DS
1259}
1260
1fdc9eae 1261/* Interface address modification. */
67e3369e
JU
1262static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1263 void *buf, size_t buflen)
1fdc9eae 1264{
d62a17ae 1265 int bytelen;
64168803
MS
1266 const struct prefix *p;
1267 int cmd;
1268 const char *label;
1fdc9eae 1269
d62a17ae 1270 struct {
1271 struct nlmsghdr n;
1272 struct ifaddrmsg ifa;
67e3369e
JU
1273 char buf[0];
1274 } *req = buf;
1275
1276 if (buflen < sizeof(*req))
1277 return 0;
1fdc9eae 1278
64168803 1279 p = dplane_ctx_get_intf_addr(ctx);
67e3369e 1280 memset(req, 0, sizeof(*req));
1fdc9eae 1281
64168803 1282 bytelen = (p->family == AF_INET ? 4 : 16);
1fdc9eae 1283
67e3369e
JU
1284 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1285 req->n.nlmsg_flags = NLM_F_REQUEST;
a55ba23f 1286
64168803
MS
1287 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1288 cmd = RTM_NEWADDR;
1289 else
1290 cmd = RTM_DELADDR;
1291
67e3369e
JU
1292 req->n.nlmsg_type = cmd;
1293 req->ifa.ifa_family = p->family;
1fdc9eae 1294
67e3369e 1295 req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1fdc9eae 1296
67e3369e
JU
1297 if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1298 return 0;
1fdc9eae 1299
64168803
MS
1300 if (p->family == AF_INET) {
1301 if (dplane_ctx_intf_is_connected(ctx)) {
1302 p = dplane_ctx_get_intf_dest(ctx);
67e3369e
JU
1303 if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1304 &p->u.prefix, bytelen))
1305 return 0;
0f3af738
JW
1306 } else if (cmd == RTM_NEWADDR) {
1307 struct in_addr broad = {
1308 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1309 p->prefixlen)
1310 };
67e3369e
JU
1311 if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1312 bytelen))
1313 return 0;
d62a17ae 1314 }
1315 }
1fdc9eae 1316
64168803 1317 /* p is now either address or destination/bcast addr */
67e3369e 1318 req->ifa.ifa_prefixlen = p->prefixlen;
e8d19a05 1319
64168803 1320 if (dplane_ctx_intf_is_secondary(ctx))
67e3369e 1321 SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1fdc9eae 1322
64168803
MS
1323 if (dplane_ctx_intf_has_label(ctx)) {
1324 label = dplane_ctx_get_intf_label(ctx);
67e3369e
JU
1325 if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1326 strlen(label) + 1))
1327 return 0;
64168803 1328 }
1fdc9eae 1329
67e3369e 1330 return NLMSG_ALIGN(req->n.nlmsg_len);
e86b71f1
PG
1331}
1332
67e3369e
JU
1333enum netlink_msg_status
1334netlink_put_address_update_msg(struct nl_batch *bth,
1335 struct zebra_dplane_ctx *ctx)
1336{
1337 return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1338 false);
e86b71f1
PG
1339}
1340
5d414138
SW
1341static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
1342 size_t buflen)
1343{
1344 enum dplane_op_e op;
1345 int cmd = 0;
1346
1347 op = dplane_ctx_get_op(ctx);
1348
1349 switch (op) {
1350 case DPLANE_OP_INTF_UPDATE:
1351 cmd = RTM_SETLINK;
1352 break;
1353 case DPLANE_OP_INTF_INSTALL:
1354 cmd = RTM_NEWLINK;
1355 break;
1356 case DPLANE_OP_INTF_DELETE:
1357 cmd = RTM_DELLINK;
1358 break;
1359 default:
1360 flog_err(
1361 EC_ZEBRA_NHG_FIB_UPDATE,
1362 "Context received for kernel interface update with incorrect OP code (%u)",
1363 op);
1364 return -1;
1365 }
1366
1367 return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
1368}
1369
1370enum netlink_msg_status
1371netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1372{
1373 return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
1374}
1375
2414abd3 1376int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1377{
d62a17ae 1378 int len;
1379 struct ifaddrmsg *ifa;
1380 struct rtattr *tb[IFA_MAX + 1];
1381 struct interface *ifp;
1382 void *addr;
1383 void *broad;
d7c0a89a 1384 uint8_t flags = 0;
d62a17ae 1385 char *label = NULL;
1386 struct zebra_ns *zns;
cde1af84 1387 uint32_t metric = METRIC_MAX;
9254efed 1388 uint32_t kernel_flags = 0;
d62a17ae 1389
14ed0615
DS
1390 frrtrace(3, frr_zebra, netlink_interface_addr, h, ns_id, startup);
1391
d62a17ae 1392 zns = zebra_ns_lookup(ns_id);
1393 ifa = NLMSG_DATA(h);
1394
8a1b681c 1395 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
9df414fe 1396 flog_warn(
e914ccbe 1397 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
1398 "Invalid address family: %u received from kernel interface addr change: %s",
1399 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1400 return 0;
8a1b681c 1401 }
d62a17ae 1402
1403 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1404 return 0;
1405
1406 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
9bdf8618 1407 if (len < 0) {
15569c58
DA
1408 zlog_err(
1409 "%s: Message received from netlink is of a broken size: %d %zu",
1410 __func__, h->nlmsg_len,
1411 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
d62a17ae 1412 return -1;
9bdf8618 1413 }
d62a17ae 1414
d62a17ae 1415 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1416
1417 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1418 if (ifp == NULL) {
8eec31ef
YY
1419 if (startup) {
1420 /* During startup, failure to lookup the referenced
1421 * interface should not be an error, so we have
1422 * downgraded this condition to warning, and we permit
1423 * the startup interface state retrieval to continue.
1424 */
1425 flog_warn(EC_LIB_INTERFACE,
1426 "%s: can't find interface by index %d",
1427 __func__, ifa->ifa_index);
1428 return 0;
1429 } else {
1430 flog_err(EC_LIB_INTERFACE,
1431 "%s: can't find interface by index %d",
1432 __func__, ifa->ifa_index);
1433 return -1;
1434 }
d62a17ae 1435 }
1436
9254efed
DS
1437 /* Flags passed through */
1438 if (tb[IFA_FLAGS])
1439 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1440 else
1441 kernel_flags = ifa->ifa_flags;
1442
d62a17ae 1443 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1444 {
1445 char buf[BUFSIZ];
6751c0f3 1446 zlog_debug("%s %s %s flags 0x%x:", __func__,
d62a17ae 1447 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
9254efed 1448 kernel_flags);
d62a17ae 1449 if (tb[IFA_LOCAL])
1450 zlog_debug(" IFA_LOCAL %s/%d",
1451 inet_ntop(ifa->ifa_family,
1452 RTA_DATA(tb[IFA_LOCAL]), buf,
1453 BUFSIZ),
1454 ifa->ifa_prefixlen);
1455 if (tb[IFA_ADDRESS])
1456 zlog_debug(" IFA_ADDRESS %s/%d",
1457 inet_ntop(ifa->ifa_family,
1458 RTA_DATA(tb[IFA_ADDRESS]), buf,
1459 BUFSIZ),
1460 ifa->ifa_prefixlen);
1461 if (tb[IFA_BROADCAST])
1462 zlog_debug(" IFA_BROADCAST %s/%d",
1463 inet_ntop(ifa->ifa_family,
1464 RTA_DATA(tb[IFA_BROADCAST]), buf,
1465 BUFSIZ),
1466 ifa->ifa_prefixlen);
1467 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1468 zlog_debug(" IFA_LABEL %s",
1469 (char *)RTA_DATA(tb[IFA_LABEL]));
1470
1471 if (tb[IFA_CACHEINFO]) {
1472 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1473 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1474 ci->ifa_prefered, ci->ifa_valid);
1475 }
1476 }
1477
1478 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1479 if (tb[IFA_LOCAL] == NULL)
1480 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1481 if (tb[IFA_ADDRESS] == NULL)
1482 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1483
1484 /* local interface address */
1485 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1486
1487 /* is there a peer address? */
1488 if (tb[IFA_ADDRESS]
1489 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1490 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1491 broad = RTA_DATA(tb[IFA_ADDRESS]);
1492 SET_FLAG(flags, ZEBRA_IFA_PEER);
1493 } else
1494 /* seeking a broadcast address */
1495 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1496 : NULL);
1497
1498 /* addr is primary key, SOL if we don't have one */
1499 if (addr == NULL) {
14a4d9d0 1500 zlog_debug("%s: Local Interface Address is NULL for %s",
1501 __func__, ifp->name);
d62a17ae 1502 return -1;
1503 }
1504
1505 /* Flags. */
9254efed 1506 if (kernel_flags & IFA_F_SECONDARY)
d62a17ae 1507 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1508
1509 /* Label */
1510 if (tb[IFA_LABEL])
1511 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1512
2e1cc436 1513 if (label && strcmp(ifp->name, label) == 0)
d62a17ae 1514 label = NULL;
1515
cde1af84
AK
1516 if (tb[IFA_RT_PRIORITY])
1517 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1518
d62a17ae 1519 /* Register interface address to the interface. */
1520 if (ifa->ifa_family == AF_INET) {
930571d2 1521 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
e17d9b2d 1522 zlog_err(
87b5d1b0
DS
1523 "Invalid prefix length: %u received from kernel interface addr change: %s",
1524 ifa->ifa_prefixlen,
1525 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1526 return -1;
930571d2 1527 }
20e879f9 1528
d62a17ae 1529 if (h->nlmsg_type == RTM_NEWADDR)
1530 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1531 ifa->ifa_prefixlen,
cde1af84
AK
1532 (struct in_addr *)broad, label,
1533 metric);
20e879f9
MS
1534 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1535 /* Delete with a peer address */
1536 connected_delete_ipv4(
1537 ifp, flags, (struct in_addr *)addr,
1538 ifa->ifa_prefixlen, broad);
1539 } else
d62a17ae 1540 connected_delete_ipv4(
1541 ifp, flags, (struct in_addr *)addr,
fd267f08 1542 ifa->ifa_prefixlen, NULL);
d62a17ae 1543 }
20e879f9 1544
d62a17ae 1545 if (ifa->ifa_family == AF_INET6) {
930571d2 1546 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
e17d9b2d 1547 zlog_err(
87b5d1b0
DS
1548 "Invalid prefix length: %u received from kernel interface addr change: %s",
1549 ifa->ifa_prefixlen,
1550 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1551 return -1;
930571d2 1552 }
d62a17ae 1553 if (h->nlmsg_type == RTM_NEWADDR) {
1554 /* Only consider valid addresses; we'll not get a
1555 * notification from
1556 * the kernel till IPv6 DAD has completed, but at init
1557 * time, Quagga
1558 * does query for and will receive all addresses.
1559 */
9254efed 1560 if (!(kernel_flags
d62a17ae 1561 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
60466a63
QY
1562 connected_add_ipv6(ifp, flags,
1563 (struct in6_addr *)addr,
60c0687a 1564 (struct in6_addr *)broad,
cde1af84
AK
1565 ifa->ifa_prefixlen, label,
1566 metric);
d62a17ae 1567 } else
1568 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
fd267f08 1569 NULL, ifa->ifa_prefixlen);
d62a17ae 1570 }
1571
2a181147
SW
1572 /*
1573 * Linux kernel does not send route delete on interface down/addr del
1574 * so we have to re-process routes it owns (i.e. kernel routes)
1575 */
1576 if (h->nlmsg_type != RTM_NEWADDR)
1577 rib_update(RIB_UPDATE_KERNEL);
1578
d62a17ae 1579 return 0;
1fdc9eae 1580}
1581
e7c2c198
MS
1582/*
1583 * Parse and validate an incoming interface address change message,
1584 * generating a dplane context object.
1585 * This runs in the dplane pthread; the context is enqueued to the
1586 * main pthread for processing.
1587 */
1588int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1589 int startup /*ignored*/)
1590{
1591 int len;
1592 struct ifaddrmsg *ifa;
1593 struct rtattr *tb[IFA_MAX + 1];
1594 void *addr;
1595 void *broad;
1596 char *label = NULL;
1597 uint32_t metric = METRIC_MAX;
1598 uint32_t kernel_flags = 0;
1599 struct zebra_dplane_ctx *ctx;
1600 struct prefix p;
1601
1602 ifa = NLMSG_DATA(h);
1603
1604 /* Validate message types */
1605 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1606 return 0;
1607
1608 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1609 if (IS_ZEBRA_DEBUG_KERNEL)
1610 zlog_debug("%s: %s: Invalid address family: %u",
1611 __func__, nl_msg_type_to_str(h->nlmsg_type),
1612 ifa->ifa_family);
1613 return 0;
1614 }
1615
1616 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1617 if (len < 0) {
1618 if (IS_ZEBRA_DEBUG_KERNEL)
1619 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1620 __func__, nl_msg_type_to_str(h->nlmsg_type),
1621 h->nlmsg_len,
1622 (size_t)NLMSG_LENGTH(
1623 sizeof(struct ifaddrmsg)));
1624 return -1;
1625 }
1626
1627 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1628
1629 /* Flags passed through */
1630 if (tb[IFA_FLAGS])
1631 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1632 else
1633 kernel_flags = ifa->ifa_flags;
1634
1635 if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1636 char buf[PREFIX_STRLEN];
1637
1638 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1639 nl_msg_type_to_str(h->nlmsg_type), ns_id,
1640 ifa->ifa_index, kernel_flags);
1641 if (tb[IFA_LOCAL])
1642 zlog_debug(" IFA_LOCAL %s/%d",
1643 inet_ntop(ifa->ifa_family,
1644 RTA_DATA(tb[IFA_LOCAL]), buf,
1645 sizeof(buf)),
1646 ifa->ifa_prefixlen);
1647 if (tb[IFA_ADDRESS])
1648 zlog_debug(" IFA_ADDRESS %s/%d",
1649 inet_ntop(ifa->ifa_family,
1650 RTA_DATA(tb[IFA_ADDRESS]), buf,
1651 sizeof(buf)),
1652 ifa->ifa_prefixlen);
1653 if (tb[IFA_BROADCAST])
1654 zlog_debug(" IFA_BROADCAST %s/%d",
1655 inet_ntop(ifa->ifa_family,
1656 RTA_DATA(tb[IFA_BROADCAST]), buf,
1657 sizeof(buf)),
1658 ifa->ifa_prefixlen);
1659 if (tb[IFA_LABEL])
1660 zlog_debug(" IFA_LABEL %s",
1661 (const char *)RTA_DATA(tb[IFA_LABEL]));
1662
1663 if (tb[IFA_CACHEINFO]) {
1664 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1665
1666 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1667 ci->ifa_prefered, ci->ifa_valid);
1668 }
1669 }
1670
1671 /* Validate prefix length */
1672
1673 if (ifa->ifa_family == AF_INET
1674 && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1675 if (IS_ZEBRA_DEBUG_KERNEL)
1676 zlog_debug("%s: %s: Invalid prefix length: %u",
1677 __func__, nl_msg_type_to_str(h->nlmsg_type),
1678 ifa->ifa_prefixlen);
1679 return -1;
1680 }
1681
1682 if (ifa->ifa_family == AF_INET6) {
1683 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1684 if (IS_ZEBRA_DEBUG_KERNEL)
1685 zlog_debug("%s: %s: Invalid prefix length: %u",
1686 __func__,
1687 nl_msg_type_to_str(h->nlmsg_type),
1688 ifa->ifa_prefixlen);
1689 return -1;
1690 }
1691
1692 /* Only consider valid addresses; we'll not get a kernel
1693 * notification till IPv6 DAD has completed, but at init
1694 * time, FRR does query for and will receive all addresses.
1695 */
1696 if (h->nlmsg_type == RTM_NEWADDR
1697 && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1698 if (IS_ZEBRA_DEBUG_KERNEL)
1699 zlog_debug("%s: %s: Invalid/tentative addr",
1700 __func__,
1701 nl_msg_type_to_str(h->nlmsg_type));
1702 return 0;
1703 }
1704 }
1705
1706 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1707 if (tb[IFA_LOCAL] == NULL)
1708 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1709 if (tb[IFA_ADDRESS] == NULL)
1710 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1711
1712 /* local interface address */
1713 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1714
1715 /* addr is primary key, SOL if we don't have one */
1716 if (addr == NULL) {
1717 if (IS_ZEBRA_DEBUG_KERNEL)
1718 zlog_debug("%s: %s: No local interface address",
1719 __func__, nl_msg_type_to_str(h->nlmsg_type));
1720 return -1;
1721 }
1722
1723 /* Allocate a context object, now that validation is done. */
1724 ctx = dplane_ctx_alloc();
1725 if (h->nlmsg_type == RTM_NEWADDR)
1726 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1727 else
1728 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1729
1730 dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1731 dplane_ctx_set_ns_id(ctx, ns_id);
1732
1733 /* Convert addr to prefix */
1734 memset(&p, 0, sizeof(p));
1735 p.family = ifa->ifa_family;
1736 p.prefixlen = ifa->ifa_prefixlen;
1737 if (p.family == AF_INET)
1738 p.u.prefix4 = *(struct in_addr *)addr;
1739 else
1740 p.u.prefix6 = *(struct in6_addr *)addr;
1741
1742 dplane_ctx_set_intf_addr(ctx, &p);
1743
1744 /* is there a peer address? */
1745 if (tb[IFA_ADDRESS]
1746 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1747 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1748 broad = RTA_DATA(tb[IFA_ADDRESS]);
1749 dplane_ctx_intf_set_connected(ctx);
1750 } else if (tb[IFA_BROADCAST]) {
1751 /* seeking a broadcast address */
1752 broad = RTA_DATA(tb[IFA_BROADCAST]);
1753 dplane_ctx_intf_set_broadcast(ctx);
1754 } else
1755 broad = NULL;
1756
1757 if (broad) {
1758 /* Convert addr to prefix */
1759 memset(&p, 0, sizeof(p));
1760 p.family = ifa->ifa_family;
1761 p.prefixlen = ifa->ifa_prefixlen;
1762 if (p.family == AF_INET)
1763 p.u.prefix4 = *(struct in_addr *)broad;
1764 else
1765 p.u.prefix6 = *(struct in6_addr *)broad;
1766
1767 dplane_ctx_set_intf_dest(ctx, &p);
1768 }
1769
1770 /* Flags. */
1771 if (kernel_flags & IFA_F_SECONDARY)
1772 dplane_ctx_intf_set_secondary(ctx);
1773
1774 /* Label */
1775 if (tb[IFA_LABEL]) {
1776 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1777 dplane_ctx_set_intf_label(ctx, label);
1778 }
1779
1780 if (tb[IFA_RT_PRIORITY])
1781 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1782
1783 dplane_ctx_set_intf_metric(ctx, metric);
1784
1785 /* Enqueue ctx for main pthread to process */
1786 dplane_provider_enqueue_to_zebra(ctx);
1787
1788 return 0;
1789}
1790
2414abd3 1791int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1792{
d62a17ae 1793 int len;
1794 struct ifinfomsg *ifi;
1795 struct rtattr *tb[IFLA_MAX + 1];
1796 struct rtattr *linkinfo[IFLA_MAX + 1];
1797 struct interface *ifp;
1798 char *name = NULL;
1799 char *kind = NULL;
48884c6b 1800 char *desc = NULL;
d62a17ae 1801 char *slave_kind = NULL;
1802 struct zebra_ns *zns;
1803 vrf_id_t vrf_id = VRF_DEFAULT;
e6f2bec0 1804 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
a81982fa 1805 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
d62a17ae 1806 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
b9368db9 1807 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
d62a17ae 1808 ifindex_t link_ifindex = IFINDEX_INTERNAL;
97c4e1d0 1809 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
ba5165ec 1810 struct zebra_if *zif;
14ddb3d9 1811 ns_id_t link_nsid = ns_id;
c36e442c 1812 ifindex_t master_infindex = IFINDEX_INTERNAL;
00a7710c 1813 uint8_t bypass = 0;
d62a17ae 1814
1815 zns = zebra_ns_lookup(ns_id);
1816 ifi = NLMSG_DATA(h);
1817
fe533c56 1818 /* assume if not default zns, then new VRF */
d62a17ae 1819 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1820 /* If this is not link add/delete message so print warning. */
6751c0f3 1821 zlog_debug("%s: wrong kernel message %s", __func__,
87b5d1b0 1822 nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1823 return 0;
1824 }
1825
8a1b681c
SW
1826 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1827 || ifi->ifi_family == AF_INET6)) {
9df414fe 1828 flog_warn(
e914ccbe 1829 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
1830 "Invalid address family: %u received from kernel link change: %s",
1831 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
1832 return 0;
1833 }
1834
d62a17ae 1835 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618 1836 if (len < 0) {
15569c58
DA
1837 zlog_err(
1838 "%s: Message received from netlink is of a broken size %d %zu",
1839 __func__, h->nlmsg_len,
1840 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 1841 return -1;
9bdf8618 1842 }
d62a17ae 1843
1844 /* We are interested in some AF_BRIDGE notifications. */
1845 if (ifi->ifi_family == AF_BRIDGE)
1846 return netlink_bridge_interface(h, len, ns_id, startup);
1847
1848 /* Looking up interface name. */
0d6f7fd6 1849 memset(linkinfo, 0, sizeof(linkinfo));
0dcd8506
SW
1850 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
1851 NLA_F_NESTED);
1fdc9eae 1852
d62a17ae 1853 /* check for wireless messages to ignore */
1854 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1855 if (IS_ZEBRA_DEBUG_KERNEL)
1856 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1857 __func__);
1858 return 0;
1859 }
1fdc9eae 1860
d62a17ae 1861 if (tb[IFLA_IFNAME] == NULL)
1862 return -1;
1863 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1864
e9f79fff
MS
1865 /* Must be valid string. */
1866 len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
1867 if (len < 2 || name[len - 1] != '\0') {
1868 if (IS_ZEBRA_DEBUG_KERNEL)
1869 zlog_debug("%s: invalid intf name", __func__);
1870 return -1;
1871 }
1872
d62a17ae 1873 if (tb[IFLA_LINKINFO]) {
c9d842c7
DS
1874 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1875 tb[IFLA_LINKINFO]);
1fdc9eae 1876
d62a17ae 1877 if (linkinfo[IFLA_INFO_KIND])
1878 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1879
d62a17ae 1880 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1881 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1882
d62a17ae 1883 netlink_determine_zebra_iftype(kind, &zif_type);
1884 }
6675513d 1885
d62a17ae 1886 /* If linking to another interface, note it. */
1887 if (tb[IFLA_LINK])
1888 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1fdc9eae 1889
b1cc23b2 1890 if (tb[IFLA_LINK_NETNSID]) {
14ddb3d9 1891 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
b1cc23b2
PG
1892 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1893 }
48884c6b
DS
1894 if (tb[IFLA_IFALIAS]) {
1895 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1896 }
1897
d62a17ae 1898 /* See if interface is present. */
1899 ifp = if_lookup_by_name_per_ns(zns, name);
1900
1901 if (h->nlmsg_type == RTM_NEWLINK) {
e4c5b3ba
IR
1902 /* If VRF, create or update the VRF structure itself. */
1903 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1904 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1905 vrf_id = (vrf_id_t)ifi->ifi_index;
1906 }
1907
d62a17ae 1908 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1909 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1910 && !vrf_is_backend_netns()) {
d62a17ae 1911 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
c36e442c
AK
1912 master_infindex = vrf_id =
1913 *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1914 } else if (slave_kind
1915 && (strcmp(slave_kind, "bridge") == 0)) {
1916 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
c36e442c 1917 master_infindex = bridge_ifindex =
d62a17ae 1918 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
1919 } else if (slave_kind
1920 && (strcmp(slave_kind, "bond") == 0)) {
1921 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
c36e442c 1922 master_infindex = bond_ifindex =
b9368db9 1923 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
00a7710c 1924 bypass = netlink_parse_lacp_bypass(linkinfo);
d62a17ae 1925 } else
1926 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1927 }
fe533c56
PG
1928 if (vrf_is_backend_netns())
1929 vrf_id = (vrf_id_t)ns_id;
d62a17ae 1930 if (ifp == NULL
1931 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1932 /* Add interface notification from kernel */
1933 if (IS_ZEBRA_DEBUG_KERNEL)
1934 zlog_debug(
3efd0893 1935 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
d62a17ae 1936 name, ifi->ifi_index, vrf_id, zif_type,
c36e442c 1937 zif_slave_type, master_infindex,
d62a17ae 1938 ifi->ifi_flags);
1939
1940 if (ifp == NULL) {
1941 /* unknown interface */
f60a1188 1942 ifp = if_get_by_name(name, vrf_id, NULL);
d62a17ae 1943 } else {
1944 /* pre-configured interface, learnt now */
096f7609 1945 if (ifp->vrf->vrf_id != vrf_id)
a36898e7 1946 if_update_to_new_vrf(ifp, vrf_id);
d62a17ae 1947 }
1948
1949 /* Update interface information. */
1950 set_ifindex(ifp, ifi->ifi_index, zns);
1951 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d23b983b 1952 if (!tb[IFLA_MTU]) {
9df414fe 1953 zlog_debug(
d23b983b
SW
1954 "RTM_NEWLINK for interface %s(%u) without MTU set",
1955 name, ifi->ifi_index);
1956 return 0;
1957 }
d62a17ae 1958 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1959 ifp->metric = 0;
1960 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1961
1962 /* Set interface type */
1963 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1964 if (IS_ZEBRA_IF_VRF(ifp))
1965 SET_FLAG(ifp->status,
1966 ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1967
1968 /* Update link. */
86bad1cb 1969 zebra_if_update_link(ifp, link_ifindex, link_nsid);
d62a17ae 1970
8975bbbd
IR
1971 ifp->ll_type =
1972 netlink_to_zebra_link_type(ifi->ifi_type);
d62a17ae 1973 netlink_interface_update_hw_addr(tb, ifp);
1974
1975 /* Inform clients, install any configured addresses. */
1976 if_add_update(ifp);
1977
1978 /* Extract and save L2 interface information, take
1979 * additional actions. */
1980 netlink_interface_update_l2info(
14ddb3d9
PG
1981 ifp, linkinfo[IFLA_INFO_DATA],
1982 1, link_nsid);
d62a17ae 1983 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
c7620108
PG
1984 zebra_l2if_update_bridge_slave(
1985 ifp, bridge_ifindex, ns_id,
1986 ZEBRA_BRIDGE_NO_ACTION);
b9368db9 1987 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
00a7710c
AK
1988 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
1989 !!bypass);
c36e442c 1990
0dcd8506
SW
1991 if (tb[IFLA_PROTO_DOWN])
1992 netlink_proc_dplane_if_protodown(ifp->info, tb);
c36e442c 1993
096f7609 1994 } else if (ifp->vrf->vrf_id != vrf_id) {
d62a17ae 1995 /* VRF change for an interface. */
1996 if (IS_ZEBRA_DEBUG_KERNEL)
1997 zlog_debug(
3efd0893 1998 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
096f7609
IR
1999 name, ifp->ifindex, ifp->vrf->vrf_id,
2000 vrf_id, ifi->ifi_flags);
d62a17ae 2001
a36898e7 2002 if_handle_vrf_change(ifp, vrf_id);
d62a17ae 2003 } else {
b9368db9 2004 bool was_bridge_slave, was_bond_slave;
c7620108 2005 uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
2d04bd98 2006 zif = ifp->info;
d62a17ae 2007
2008 /* Interface update. */
2009 if (IS_ZEBRA_DEBUG_KERNEL)
2010 zlog_debug(
3efd0893 2011 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
d62a17ae 2012 name, ifp->ifindex, zif_slave_type,
c36e442c 2013 master_infindex, ifi->ifi_flags);
d62a17ae 2014
2015 set_ifindex(ifp, ifi->ifi_index, zns);
d23b983b 2016 if (!tb[IFLA_MTU]) {
9df414fe 2017 zlog_debug(
d23b983b
SW
2018 "RTM_NEWLINK for interface %s(%u) without MTU set",
2019 name, ifi->ifi_index);
2020 return 0;
2021 }
d62a17ae 2022 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
2023 ifp->metric = 0;
2024
2025 /* Update interface type - NOTE: Only slave_type can
2026 * change. */
2027 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
b9368db9 2028 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
d62a17ae 2029 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2030
97c4e1d0
CS
2031 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
2032
ecffe916 2033 /* Update link. */
86bad1cb 2034 zebra_if_update_link(ifp, link_ifindex, link_nsid);
ecffe916 2035
8975bbbd
IR
2036 ifp->ll_type =
2037 netlink_to_zebra_link_type(ifi->ifi_type);
d62a17ae 2038 netlink_interface_update_hw_addr(tb, ifp);
2039
0dcd8506
SW
2040 if (tb[IFLA_PROTO_DOWN])
2041 netlink_proc_dplane_if_protodown(ifp->info, tb);
2d04bd98 2042
d62a17ae 2043 if (if_is_no_ptm_operative(ifp)) {
8b48cdb9 2044 bool is_up = if_is_operative(ifp);
d62a17ae 2045 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2d04bd98
CS
2046 if (!if_is_no_ptm_operative(ifp) ||
2047 CHECK_FLAG(zif->flags,
2048 ZIF_FLAG_PROTODOWN)) {
d62a17ae 2049 if (IS_ZEBRA_DEBUG_KERNEL)
2050 zlog_debug(
2051 "Intf %s(%u) has gone DOWN",
2052 name, ifp->ifindex);
2053 if_down(ifp);
2a181147 2054 rib_update(RIB_UPDATE_KERNEL);
d62a17ae 2055 } else if (if_is_operative(ifp)) {
c7620108
PG
2056 bool mac_updated = false;
2057
d62a17ae 2058 /* Must notify client daemons of new
2059 * interface status. */
2060 if (IS_ZEBRA_DEBUG_KERNEL)
2061 zlog_debug(
2062 "Intf %s(%u) PTM up, notifying clients",
2063 name, ifp->ifindex);
8b48cdb9 2064 if_up(ifp, !is_up);
97c4e1d0
CS
2065
2066 /* Update EVPN VNI when SVI MAC change
2067 */
c7620108
PG
2068 if (memcmp(old_hw_addr, ifp->hw_addr,
2069 INTERFACE_HWADDR_MAX))
2070 mac_updated = true;
2071 if (IS_ZEBRA_IF_VLAN(ifp)
2072 && mac_updated) {
97c4e1d0
CS
2073 struct interface *link_if;
2074
2075 link_if =
2076 if_lookup_by_index_per_ns(
2077 zebra_ns_lookup(NS_DEFAULT),
2078 link_ifindex);
2079 if (link_if)
2080 zebra_vxlan_svi_up(ifp,
2081 link_if);
c7620108
PG
2082 } else if (mac_updated
2083 && IS_ZEBRA_IF_BRIDGE(ifp)) {
2084 zlog_debug(
2085 "Intf %s(%u) bridge changed MAC address",
2086 name, ifp->ifindex);
2087 chgflags =
2088 ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
97c4e1d0 2089 }
d62a17ae 2090 }
2091 } else {
2092 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2d04bd98
CS
2093 if (if_is_operative(ifp) &&
2094 !CHECK_FLAG(zif->flags,
2095 ZIF_FLAG_PROTODOWN)) {
d62a17ae 2096 if (IS_ZEBRA_DEBUG_KERNEL)
2097 zlog_debug(
2098 "Intf %s(%u) has come UP",
2099 name, ifp->ifindex);
8b48cdb9 2100 if_up(ifp, true);
f56a15b5
PG
2101 if (IS_ZEBRA_IF_BRIDGE(ifp))
2102 chgflags =
2103 ZEBRA_BRIDGE_MASTER_UP;
6f908ded
QY
2104 } else {
2105 if (IS_ZEBRA_DEBUG_KERNEL)
2106 zlog_debug(
7913714e 2107 "Intf %s(%u) has gone DOWN",
6f908ded
QY
2108 name, ifp->ifindex);
2109 if_down(ifp);
2a181147 2110 rib_update(RIB_UPDATE_KERNEL);
d62a17ae 2111 }
2112 }
2113
2114 /* Extract and save L2 interface information, take
2115 * additional actions. */
2116 netlink_interface_update_l2info(
14ddb3d9
PG
2117 ifp, linkinfo[IFLA_INFO_DATA],
2118 0, link_nsid);
c7620108
PG
2119 if (IS_ZEBRA_IF_BRIDGE(ifp))
2120 zebra_l2if_update_bridge(ifp, chgflags);
c36e442c
AK
2121 if (IS_ZEBRA_IF_BOND(ifp))
2122 zebra_l2if_update_bond(ifp, true);
d62a17ae 2123 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
c7620108
PG
2124 zebra_l2if_update_bridge_slave(
2125 ifp, bridge_ifindex, ns_id, chgflags);
b9368db9 2126 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
00a7710c
AK
2127 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2128 !!bypass);
d62a17ae 2129 }
26f13577
DS
2130
2131 zif = ifp->info;
2132 if (zif) {
2133 XFREE(MTYPE_TMP, zif->desc);
2134 if (desc)
2135 zif->desc = XSTRDUP(MTYPE_TMP, desc);
2136 }
d62a17ae 2137 } else {
2138 /* Delete interface notification from kernel */
2139 if (ifp == NULL) {
9df414fe
QY
2140 if (IS_ZEBRA_DEBUG_KERNEL)
2141 zlog_debug(
2142 "RTM_DELLINK for unknown interface %s(%u)",
2143 name, ifi->ifi_index);
d62a17ae 2144 return 0;
2145 }
2146
2147 if (IS_ZEBRA_DEBUG_KERNEL)
2148 zlog_debug("RTM_DELLINK for %s(%u)", name,
2149 ifp->ifindex);
2150
2151 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
2152
c36e442c
AK
2153 if (IS_ZEBRA_IF_BOND(ifp))
2154 zebra_l2if_update_bond(ifp, false);
00a7710c
AK
2155 if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2156 zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
d62a17ae 2157 /* Special handling for bridge or VxLAN interfaces. */
2158 if (IS_ZEBRA_IF_BRIDGE(ifp))
2159 zebra_l2_bridge_del(ifp);
2160 else if (IS_ZEBRA_IF_VXLAN(ifp))
2161 zebra_l2_vxlanif_del(ifp);
2162
d0438da6 2163 if_delete_update(&ifp);
e4c5b3ba
IR
2164
2165 /* If VRF, delete the VRF structure itself. */
2166 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
2167 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1fdc9eae 2168 }
2169
d62a17ae 2170 return 0;
1fdc9eae 2171}
718e3744 2172
5d414138
SW
2173/**
2174 * Interface encoding helper function.
2175 *
2176 * \param[in] cmd netlink command.
2177 * \param[in] ctx dataplane context (information snapshot).
2178 * \param[out] buf buffer to hold the packet.
2179 * \param[in] buflen amount of buffer bytes.
2180 */
c3bd894e 2181
5d414138
SW
2182ssize_t netlink_intf_msg_encode(uint16_t cmd,
2183 const struct zebra_dplane_ctx *ctx, void *buf,
2184 size_t buflen)
2185{
c3bd894e
QY
2186 struct {
2187 struct nlmsghdr n;
2188 struct ifinfomsg ifa;
5d414138
SW
2189 char buf[];
2190 } *req = buf;
c3bd894e 2191
5d414138
SW
2192 struct rtattr *nest_protodown_reason;
2193 ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
5d414138 2194 bool down = dplane_ctx_intf_is_protodown(ctx);
ab465d24 2195 bool pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
5d414138
SW
2196 struct nlsock *nl =
2197 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
c3bd894e 2198
5d414138
SW
2199 if (buflen < sizeof(*req))
2200 return 0;
c3bd894e 2201
5d414138 2202 memset(req, 0, sizeof(*req));
c3bd894e 2203
5d414138
SW
2204 if (cmd != RTM_SETLINK)
2205 flog_err(
2206 EC_ZEBRA_INTF_UPDATE_FAILURE,
2207 "Only RTM_SETLINK message type currently supported in dplane pthread");
c3bd894e 2208
5d414138
SW
2209 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2210 req->n.nlmsg_flags = NLM_F_REQUEST;
2211 req->n.nlmsg_type = cmd;
2212 req->n.nlmsg_pid = nl->snl.nl_pid;
2213
2214 req->ifa.ifi_index = ifindex;
2215
2216 nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
2217 nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
2218
0dcd8506
SW
2219 /* Reason info nest */
2220 nest_protodown_reason =
2221 nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
5d414138 2222
0dcd8506
SW
2223 if (!nest_protodown_reason)
2224 return -1;
5d414138 2225
0dcd8506
SW
2226 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
2227 (1 << frr_protodown_r_bit));
2228 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
ab465d24 2229 ((int)pd_reason_val) << frr_protodown_r_bit);
5d414138 2230
0dcd8506 2231 nl_attr_nest_end(&req->n, nest_protodown_reason);
5d414138
SW
2232
2233 if (IS_ZEBRA_DEBUG_KERNEL)
ab465d24
SW
2234 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2235 __func__, nl_msg_type_to_str(cmd), down,
2236 pd_reason_val, ifindex);
5d414138
SW
2237
2238 return NLMSG_ALIGN(req->n.nlmsg_len);
c3bd894e
QY
2239}
2240
718e3744 2241/* Interface information read by netlink. */
d62a17ae 2242void interface_list(struct zebra_ns *zns)
718e3744 2243{
d62a17ae 2244 interface_lookup_netlink(zns);
d9f5b2f5
SW
2245 /* We add routes for interface address,
2246 * so we need to get the nexthop info
2247 * from the kernel before we can do that
2248 */
81505946 2249 netlink_nexthop_read(zns);
cc4e0650 2250
d2bec88a 2251 interface_addr_lookup_netlink(zns);
718e3744 2252}
ddfeb486 2253
c40e1b1c
SW
2254void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
2255{
2256 if (IS_ZEBRA_DEBUG_KERNEL)
47c1d76a
SW
2257 zlog_debug(
2258 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2259 frr_protodown_r_bit, bit);
c40e1b1c
SW
2260
2261 frr_protodown_r_bit = bit;
2262}
2263
2264void if_netlink_unset_frr_protodown_r_bit(void)
2265{
2266 if (IS_ZEBRA_DEBUG_KERNEL)
47c1d76a
SW
2267 zlog_debug(
2268 "Protodown reason bit index changed: bit-index %u -> bit-index %u",
2269 frr_protodown_r_bit, FRR_PROTODOWN_REASON_DEFAULT_BIT);
c40e1b1c
SW
2270
2271 frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
2272}
2273
2274
2275bool if_netlink_frr_protodown_r_bit_is_set(void)
2276{
2277 return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
2278}
2279
2280uint8_t if_netlink_get_frr_protodown_r_bit(void)
2281{
2282 return frr_protodown_r_bit;
2283}
2284
acc8e687
CS
2285/**
2286 * netlink_request_tunneldump() - Request all tunnels from the linux kernel
2287 *
2288 * @zns: Zebra namespace
2289 * @family: AF_* netlink family
2290 * @type: RTM_* (RTM_GETTUNNEL) route type
2291 *
2292 * Return: Result status
2293 */
2294static int netlink_request_tunneldump(struct zebra_ns *zns, int family,
2295 int ifindex)
2296{
2297 struct {
2298 struct nlmsghdr n;
2299 struct tunnel_msg tmsg;
2300 char buf[256];
2301 } req;
2302
2303 /* Form the request */
2304 memset(&req, 0, sizeof(req));
2305 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tunnel_msg));
2306 req.n.nlmsg_type = RTM_GETTUNNEL;
2307 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
2308 req.tmsg.family = family;
2309 req.tmsg.ifindex = ifindex;
2310
2311 return netlink_request(&zns->netlink_cmd, &req);
2312}
2313
2314/*
2315 * Currently we only ask for vxlan l3svd vni information.
2316 * In the future this can be expanded.
2317 */
2318int netlink_tunneldump_read(struct zebra_ns *zns)
2319{
2320 int ret = 0;
2321 struct zebra_dplane_info dp_info;
2322 struct route_node *rn;
2323 struct interface *tmp_if = NULL;
2324 struct zebra_if *zif;
2325
2326 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
2327
2328 for (rn = route_top(zns->if_table); rn; rn = route_next(rn)) {
2329 tmp_if = (struct interface *)rn->info;
2330 if (!tmp_if)
2331 continue;
2332 zif = tmp_if->info;
2333 if (!zif || zif->zif_type != ZEBRA_IF_VXLAN)
2334 continue;
2335
2336 ret = netlink_request_tunneldump(zns, PF_BRIDGE,
2337 tmp_if->ifindex);
2338 if (ret < 0)
2339 return ret;
2340 }
2341 return 0;
2342}
ddfeb486 2343#endif /* GNU_LINUX */