]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_netlink.c
zebra: clear dplane flags on failure for protodown
[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"
1fdc9eae 78
0268f30e 79extern struct zebra_privs_t zserv_privs;
5d414138 80uint8_t frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
1fdc9eae 81
82/* Note: on netlink systems, there should be a 1-to-1 mapping between interface
83 names and ifindex values. */
d62a17ae 84static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
85 struct zebra_ns *zns)
1fdc9eae 86{
d62a17ae 87 struct interface *oifp;
88
89 if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
90 && (oifp != ifp)) {
91 if (ifi_index == IFINDEX_INTERNAL)
af4c2728 92 flog_err(
450971aa 93 EC_LIB_INTERFACE,
4d43f68a 94 "Netlink is setting interface %s ifindex to reserved internal value %u",
d62a17ae 95 ifp->name, ifi_index);
96 else {
97 if (IS_ZEBRA_DEBUG_KERNEL)
98 zlog_debug(
99 "interface index %d was renamed from %s to %s",
100 ifi_index, oifp->name, ifp->name);
101 if (if_is_up(oifp))
af4c2728 102 flog_err(
450971aa 103 EC_LIB_INTERFACE,
4d43f68a 104 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
d62a17ae 105 ifi_index, oifp->name, ifp->name);
106 if_delete_update(oifp);
107 }
108 }
ff880b78 109 if_set_index(ifp, ifi_index);
1fdc9eae 110}
111
112/* Utility function to parse hardware link-layer address and update ifp */
d62a17ae 113static void netlink_interface_update_hw_addr(struct rtattr **tb,
114 struct interface *ifp)
1fdc9eae 115{
d62a17ae 116 int i;
117
118 if (tb[IFLA_ADDRESS]) {
119 int hw_addr_len;
120
121 hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
122
123 if (hw_addr_len > INTERFACE_HWADDR_MAX)
9df414fe
QY
124 zlog_debug("Hardware address is too large: %d",
125 hw_addr_len);
d62a17ae 126 else {
127 ifp->hw_addr_len = hw_addr_len;
128 memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
129 hw_addr_len);
130
131 for (i = 0; i < hw_addr_len; i++)
132 if (ifp->hw_addr[i] != 0)
133 break;
134
135 if (i == hw_addr_len)
136 ifp->hw_addr_len = 0;
137 else
138 ifp->hw_addr_len = hw_addr_len;
139 }
140 }
1fdc9eae 141}
142
d62a17ae 143static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
1fdc9eae 144{
d62a17ae 145 switch (hwt) {
146 case ARPHRD_ETHER:
147 return ZEBRA_LLT_ETHER;
148 case ARPHRD_EETHER:
149 return ZEBRA_LLT_EETHER;
150 case ARPHRD_AX25:
151 return ZEBRA_LLT_AX25;
152 case ARPHRD_PRONET:
153 return ZEBRA_LLT_PRONET;
154 case ARPHRD_IEEE802:
155 return ZEBRA_LLT_IEEE802;
156 case ARPHRD_ARCNET:
157 return ZEBRA_LLT_ARCNET;
158 case ARPHRD_APPLETLK:
159 return ZEBRA_LLT_APPLETLK;
160 case ARPHRD_DLCI:
161 return ZEBRA_LLT_DLCI;
162 case ARPHRD_ATM:
163 return ZEBRA_LLT_ATM;
164 case ARPHRD_METRICOM:
165 return ZEBRA_LLT_METRICOM;
166 case ARPHRD_IEEE1394:
167 return ZEBRA_LLT_IEEE1394;
168 case ARPHRD_EUI64:
169 return ZEBRA_LLT_EUI64;
170 case ARPHRD_INFINIBAND:
171 return ZEBRA_LLT_INFINIBAND;
172 case ARPHRD_SLIP:
173 return ZEBRA_LLT_SLIP;
174 case ARPHRD_CSLIP:
175 return ZEBRA_LLT_CSLIP;
176 case ARPHRD_SLIP6:
177 return ZEBRA_LLT_SLIP6;
178 case ARPHRD_CSLIP6:
179 return ZEBRA_LLT_CSLIP6;
180 case ARPHRD_RSRVD:
181 return ZEBRA_LLT_RSRVD;
182 case ARPHRD_ADAPT:
183 return ZEBRA_LLT_ADAPT;
184 case ARPHRD_ROSE:
185 return ZEBRA_LLT_ROSE;
186 case ARPHRD_X25:
187 return ZEBRA_LLT_X25;
188 case ARPHRD_PPP:
189 return ZEBRA_LLT_PPP;
190 case ARPHRD_CISCO:
191 return ZEBRA_LLT_CHDLC;
192 case ARPHRD_LAPB:
193 return ZEBRA_LLT_LAPB;
194 case ARPHRD_RAWHDLC:
195 return ZEBRA_LLT_RAWHDLC;
196 case ARPHRD_TUNNEL:
197 return ZEBRA_LLT_IPIP;
198 case ARPHRD_TUNNEL6:
199 return ZEBRA_LLT_IPIP6;
200 case ARPHRD_FRAD:
201 return ZEBRA_LLT_FRAD;
202 case ARPHRD_SKIP:
203 return ZEBRA_LLT_SKIP;
204 case ARPHRD_LOOPBACK:
205 return ZEBRA_LLT_LOOPBACK;
206 case ARPHRD_LOCALTLK:
207 return ZEBRA_LLT_LOCALTLK;
208 case ARPHRD_FDDI:
209 return ZEBRA_LLT_FDDI;
210 case ARPHRD_SIT:
211 return ZEBRA_LLT_SIT;
212 case ARPHRD_IPDDP:
213 return ZEBRA_LLT_IPDDP;
214 case ARPHRD_IPGRE:
215 return ZEBRA_LLT_IPGRE;
216 case ARPHRD_PIMREG:
217 return ZEBRA_LLT_PIMREG;
218 case ARPHRD_HIPPI:
219 return ZEBRA_LLT_HIPPI;
220 case ARPHRD_ECONET:
221 return ZEBRA_LLT_ECONET;
222 case ARPHRD_IRDA:
223 return ZEBRA_LLT_IRDA;
224 case ARPHRD_FCPP:
225 return ZEBRA_LLT_FCPP;
226 case ARPHRD_FCAL:
227 return ZEBRA_LLT_FCAL;
228 case ARPHRD_FCPL:
229 return ZEBRA_LLT_FCPL;
230 case ARPHRD_FCFABRIC:
231 return ZEBRA_LLT_FCFABRIC;
232 case ARPHRD_IEEE802_TR:
233 return ZEBRA_LLT_IEEE802_TR;
234 case ARPHRD_IEEE80211:
235 return ZEBRA_LLT_IEEE80211;
4268e09e 236#ifdef ARPHRD_IEEE802154
d62a17ae 237 case ARPHRD_IEEE802154:
238 return ZEBRA_LLT_IEEE802154;
4268e09e 239#endif
1fdc9eae 240#ifdef ARPHRD_IP6GRE
d62a17ae 241 case ARPHRD_IP6GRE:
242 return ZEBRA_LLT_IP6GRE;
1fdc9eae 243#endif
244#ifdef ARPHRD_IEEE802154_PHY
d62a17ae 245 case ARPHRD_IEEE802154_PHY:
246 return ZEBRA_LLT_IEEE802154_PHY;
1fdc9eae 247#endif
248
d62a17ae 249 default:
250 return ZEBRA_LLT_UNKNOWN;
251 }
1fdc9eae 252}
253
42b56639 254static inline void zebra_if_set_ziftype(struct interface *ifp,
e6f2bec0 255 enum zebra_iftype zif_type,
a81982fa 256 enum zebra_slave_iftype zif_slave_type)
42b56639
AK
257{
258 struct zebra_if *zif;
259
260 zif = (struct zebra_if *)ifp->info;
261 zif->zif_slave_type = zif_slave_type;
262
263 if (zif->zif_type != zif_type) {
264 zif->zif_type = zif_type;
265 /* If the if_type has been set to bond initialize ES info
266 * against it. XXX - note that we don't handle the case where
267 * a zif changes from bond to non-bond; it is really
268 * an unexpected/error condition.
269 */
270 zebra_evpn_if_init(zif);
271 }
272}
273
b9368db9 274static void netlink_determine_zebra_iftype(const char *kind,
e6f2bec0 275 enum zebra_iftype *zif_type)
6675513d 276{
d62a17ae 277 *zif_type = ZEBRA_IF_OTHER;
278
279 if (!kind)
280 return;
281
282 if (strcmp(kind, "vrf") == 0)
283 *zif_type = ZEBRA_IF_VRF;
284 else if (strcmp(kind, "bridge") == 0)
285 *zif_type = ZEBRA_IF_BRIDGE;
286 else if (strcmp(kind, "vlan") == 0)
287 *zif_type = ZEBRA_IF_VLAN;
288 else if (strcmp(kind, "vxlan") == 0)
289 *zif_type = ZEBRA_IF_VXLAN;
1a98c087
MK
290 else if (strcmp(kind, "macvlan") == 0)
291 *zif_type = ZEBRA_IF_MACVLAN;
0e4864ea
PG
292 else if (strcmp(kind, "veth") == 0)
293 *zif_type = ZEBRA_IF_VETH;
b9368db9
DD
294 else if (strcmp(kind, "bond") == 0)
295 *zif_type = ZEBRA_IF_BOND;
296 else if (strcmp(kind, "bond_slave") == 0)
297 *zif_type = ZEBRA_IF_BOND_SLAVE;
077c07cc
PG
298 else if (strcmp(kind, "gre") == 0)
299 *zif_type = ZEBRA_IF_GRE;
6675513d 300}
52d8f0d8 301
d62a17ae 302static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
5e031198 303 uint32_t ns_id, const char *name)
1fdc9eae 304{
d62a17ae 305 struct ifinfomsg *ifi;
306 struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
307 struct rtattr *attr[IFLA_VRF_MAX + 1];
75d26fb3 308 struct vrf *vrf = NULL;
d62a17ae 309 struct zebra_vrf *zvrf;
d7c0a89a 310 uint32_t nl_table_id;
d62a17ae 311
312 ifi = NLMSG_DATA(h);
313
c9d842c7 314 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
d62a17ae 315
316 if (!linkinfo[IFLA_INFO_DATA]) {
317 if (IS_ZEBRA_DEBUG_KERNEL)
318 zlog_debug(
319 "%s: IFLA_INFO_DATA missing from VRF message: %s",
320 __func__, name);
321 return;
322 }
323
c9d842c7
DS
324 netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
325 linkinfo[IFLA_INFO_DATA]);
d62a17ae 326 if (!attr[IFLA_VRF_TABLE]) {
327 if (IS_ZEBRA_DEBUG_KERNEL)
328 zlog_debug(
329 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
330 __func__, name);
331 return;
1fdc9eae 332 }
333
d7c0a89a 334 nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
d62a17ae 335
336 if (h->nlmsg_type == RTM_NEWLINK) {
337 if (IS_ZEBRA_DEBUG_KERNEL)
338 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
339 ifi->ifi_index, nl_table_id);
340
2e86d16d
RW
341 if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
342 vrf_id_t exist_id;
5e031198 343
2e86d16d
RW
344 exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
345 if (exist_id != VRF_DEFAULT) {
346 vrf = vrf_lookup_by_id(exist_id);
347
348 flog_err(
349 EC_ZEBRA_VRF_MISCONFIGURED,
350 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
351 name, ifi->ifi_index, vrf->name);
352 exit(-1);
353 }
5e031198 354 }
2e86d16d 355
75d26fb3 356 vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
d62a17ae 357 if (!vrf) {
450971aa 358 flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
1c50c1c0 359 name, ifi->ifi_index);
d62a17ae 360 return;
361 }
362
d62a17ae 363 /*
364 * This is the only place that we get the actual kernel table_id
365 * being used. We need it to set the table_id of the routes
366 * we are passing to the kernel.... And to throw some totally
367 * awesome parties. that too.
593406a1
DS
368 *
369 * At this point we *must* have a zvrf because the vrf_create
370 * callback creates one. We *must* set the table id
371 * before the vrf_enable because of( at the very least )
372 * static routes being delayed for installation until
373 * during the vrf_enable callbacks.
d62a17ae 374 */
375 zvrf = (struct zebra_vrf *)vrf->info;
376 zvrf->table_id = nl_table_id;
593406a1
DS
377
378 /* Enable the created VRF. */
379 if (!vrf_enable(vrf)) {
450971aa 380 flog_err(EC_LIB_INTERFACE,
1c50c1c0
QY
381 "Failed to enable VRF %s id %u", name,
382 ifi->ifi_index);
593406a1
DS
383 return;
384 }
385
d62a17ae 386 } else // h->nlmsg_type == RTM_DELLINK
387 {
388 if (IS_ZEBRA_DEBUG_KERNEL)
389 zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
390 ifi->ifi_index);
391
392 vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
393
394 if (!vrf) {
e914ccbe 395 flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
9df414fe 396 __func__);
d62a17ae 397 return;
398 }
399
400 vrf_delete(vrf);
401 }
1fdc9eae 402}
403
67188ca2 404static uint32_t get_iflink_speed(struct interface *interface, int *error)
535fe877 405{
d62a17ae 406 struct ifreq ifdata;
407 struct ethtool_cmd ecmd;
408 int sd;
409 int rc;
0268f30e 410 const char *ifname = interface->name;
d62a17ae 411
594c2878
JF
412 if (error)
413 *error = 0;
d62a17ae 414 /* initialize struct */
415 memset(&ifdata, 0, sizeof(ifdata));
416
417 /* set interface name */
0af35d90 418 strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
d62a17ae 419
420 /* initialize ethtool interface */
421 memset(&ecmd, 0, sizeof(ecmd));
422 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
ba85366a 423 ifdata.ifr_data = (caddr_t)&ecmd;
d62a17ae 424
425 /* use ioctl to get IP address of an interface */
0cf6db21 426 frr_with_privs(&zserv_privs) {
01b9e3fd 427 sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
096f7609 428 interface->vrf->vrf_id, NULL);
01b9e3fd
DL
429 if (sd < 0) {
430 if (IS_ZEBRA_DEBUG_KERNEL)
431 zlog_debug("Failure to read interface %s speed: %d %s",
432 ifname, errno, safe_strerror(errno));
594c2878
JF
433 /* no vrf socket creation may probably mean vrf issue */
434 if (error)
435 *error = -1;
01b9e3fd
DL
436 return 0;
437 }
d62a17ae 438 /* Get the current link state for the interface */
096f7609 439 rc = vrf_ioctl(interface->vrf->vrf_id, sd, SIOCETHTOOL,
633fc9b1 440 (char *)&ifdata);
01b9e3fd 441 }
d62a17ae 442 if (rc < 0) {
f767cee4 443 if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
bd7d0299
DS
444 zlog_debug(
445 "IOCTL failure to read interface %s speed: %d %s",
446 ifname, errno, safe_strerror(errno));
594c2878
JF
447 /* no device means interface unreachable */
448 if (errno == ENODEV && error)
449 *error = -1;
d62a17ae 450 ecmd.speed_hi = 0;
451 ecmd.speed = 0;
452 }
453
454 close(sd);
455
67188ca2 456 return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
535fe877
DS
457}
458
594c2878 459uint32_t kernel_get_speed(struct interface *ifp, int *error)
dc7b3cae 460{
594c2878 461 return get_iflink_speed(ifp, error);
dc7b3cae
DS
462}
463
62b4b7e4
PG
464static ssize_t
465netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
466 size_t buflen)
467{
468 struct {
469 struct nlmsghdr n;
470 struct ifinfomsg ifi;
471 char buf[];
472 } *req = buf;
473 uint32_t link_idx;
db51f0cd 474 unsigned int mtu;
62b4b7e4 475 struct rtattr *rta_info, *rta_data;
e3d3fa06 476 const struct zebra_l2info_gre *gre_info;
62b4b7e4
PG
477
478 if (buflen < sizeof(*req))
479 return 0;
480 memset(req, 0, sizeof(*req));
481
482 req->n.nlmsg_type = RTM_NEWLINK;
483 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
484 req->n.nlmsg_flags = NLM_F_REQUEST;
485
486 req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx);
e3d3fa06
PG
487
488 gre_info = dplane_ctx_gre_get_info(ctx);
489 if (!gre_info)
490 return 0;
491
62b4b7e4
PG
492 req->ifi.ifi_change = 0xFFFFFFFF;
493 link_idx = dplane_ctx_gre_get_link_ifindex(ctx);
db51f0cd
PG
494 mtu = dplane_ctx_gre_get_mtu(ctx);
495
496 if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu))
497 return 0;
62b4b7e4
PG
498
499 rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO);
500 if (!rta_info)
501 return 0;
e3d3fa06 502
62b4b7e4
PG
503 if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3))
504 return 0;
e3d3fa06 505
62b4b7e4 506 rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA);
e3d3fa06 507 if (!rta_data)
62b4b7e4 508 return 0;
e3d3fa06 509
62b4b7e4
PG
510 if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx))
511 return 0;
e3d3fa06
PG
512
513 if (gre_info->vtep_ip.s_addr &&
514 !nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL,
515 gre_info->vtep_ip.s_addr))
516 return 0;
517
518 if (gre_info->vtep_ip_remote.s_addr &&
519 !nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE,
520 gre_info->vtep_ip_remote.s_addr))
521 return 0;
522
523 if (gre_info->ikey &&
524 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
525 gre_info->ikey))
526 return 0;
527 if (gre_info->okey &&
528 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
529 gre_info->okey))
530 return 0;
531
62b4b7e4
PG
532 nl_attr_nest_end(&req->n, rta_data);
533 nl_attr_nest_end(&req->n, rta_info);
534
535 return NLMSG_ALIGN(req->n.nlmsg_len);
536}
537
d62a17ae 538static int netlink_extract_bridge_info(struct rtattr *link_data,
539 struct zebra_l2info_bridge *bridge_info)
6675513d 540{
d62a17ae 541 struct rtattr *attr[IFLA_BR_MAX + 1];
542
543 memset(bridge_info, 0, sizeof(*bridge_info));
c9d842c7 544 netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
d62a17ae 545 if (attr[IFLA_BR_VLAN_FILTERING])
546 bridge_info->vlan_aware =
d7c0a89a 547 *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
d62a17ae 548 return 0;
6675513d 549}
550
d62a17ae 551static int netlink_extract_vlan_info(struct rtattr *link_data,
552 struct zebra_l2info_vlan *vlan_info)
6675513d 553{
d62a17ae 554 struct rtattr *attr[IFLA_VLAN_MAX + 1];
555 vlanid_t vid_in_msg;
556
557 memset(vlan_info, 0, sizeof(*vlan_info));
c9d842c7 558 netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
d62a17ae 559 if (!attr[IFLA_VLAN_ID]) {
560 if (IS_ZEBRA_DEBUG_KERNEL)
561 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
562 return -1;
563 }
564
565 vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
566 vlan_info->vid = vid_in_msg;
567 return 0;
6675513d 568}
569
077c07cc
PG
570static int netlink_extract_gre_info(struct rtattr *link_data,
571 struct zebra_l2info_gre *gre_info)
572{
573 struct rtattr *attr[IFLA_GRE_MAX + 1];
574
575 memset(gre_info, 0, sizeof(*gre_info));
576 memset(attr, 0, sizeof(attr));
269b69d7 577 netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
077c07cc
PG
578
579 if (!attr[IFLA_GRE_LOCAL]) {
580 if (IS_ZEBRA_DEBUG_KERNEL)
581 zlog_debug(
582 "IFLA_GRE_LOCAL missing from GRE IF message");
583 } else
584 gre_info->vtep_ip =
585 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]);
586 if (!attr[IFLA_GRE_REMOTE]) {
587 if (IS_ZEBRA_DEBUG_KERNEL)
588 zlog_debug(
589 "IFLA_GRE_REMOTE missing from GRE IF message");
590 } else
591 gre_info->vtep_ip_remote =
592 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]);
593
594 if (!attr[IFLA_GRE_LINK]) {
595 if (IS_ZEBRA_DEBUG_KERNEL)
596 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
62b4b7e4 597 } else {
077c07cc
PG
598 gre_info->ifindex_link =
599 *(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]);
62b4b7e4
PG
600 if (IS_ZEBRA_DEBUG_KERNEL)
601 zlog_debug("IFLA_GRE_LINK obtained is %u",
602 gre_info->ifindex_link);
603 }
077c07cc
PG
604 if (attr[IFLA_GRE_IKEY])
605 gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]);
606 if (attr[IFLA_GRE_OKEY])
607 gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]);
608 return 0;
609}
610
d62a17ae 611static int netlink_extract_vxlan_info(struct rtattr *link_data,
612 struct zebra_l2info_vxlan *vxl_info)
6675513d 613{
d62a17ae 614 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
615 vni_t vni_in_msg;
616 struct in_addr vtep_ip_in_msg;
14ddb3d9 617 ifindex_t ifindex_link;
d62a17ae 618
619 memset(vxl_info, 0, sizeof(*vxl_info));
c9d842c7 620 netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
d62a17ae 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 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
629 vxl_info->vni = vni_in_msg;
630 if (!attr[IFLA_VXLAN_LOCAL]) {
631 if (IS_ZEBRA_DEBUG_KERNEL)
632 zlog_debug(
633 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
634 } else {
635 vtep_ip_in_msg =
636 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
637 vxl_info->vtep_ip = vtep_ip_in_msg;
638 }
639
d7fe235c
AK
640 if (attr[IFLA_VXLAN_GROUP]) {
641 vxl_info->mcast_grp =
642 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_GROUP]);
643 }
644
14ddb3d9
PG
645 if (!attr[IFLA_VXLAN_LINK]) {
646 if (IS_ZEBRA_DEBUG_KERNEL)
3efd0893 647 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
14ddb3d9
PG
648 } else {
649 ifindex_link =
650 *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
651 vxl_info->ifindex_link = ifindex_link;
652 }
d62a17ae 653 return 0;
6675513d 654}
655
656/*
657 * Extract and save L2 params (of interest) for an interface. When a
658 * bridge interface is added or updated, take further actions to map
659 * its members. Likewise, for VxLAN interface.
660 */
d62a17ae 661static void netlink_interface_update_l2info(struct interface *ifp,
14ddb3d9
PG
662 struct rtattr *link_data, int add,
663 ns_id_t link_nsid)
6675513d 664{
d62a17ae 665 if (!link_data)
666 return;
667
668 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
669 struct zebra_l2info_bridge bridge_info;
670
671 netlink_extract_bridge_info(link_data, &bridge_info);
672 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
673 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
674 struct zebra_l2info_vlan vlan_info;
675
676 netlink_extract_vlan_info(link_data, &vlan_info);
677 zebra_l2_vlanif_update(ifp, &vlan_info);
243b74ed
AK
678 zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
679 !!if_is_operative(ifp));
d62a17ae 680 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
681 struct zebra_l2info_vxlan vxlan_info;
682
683 netlink_extract_vxlan_info(link_data, &vxlan_info);
14ddb3d9 684 vxlan_info.link_nsid = link_nsid;
d62a17ae 685 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
14ddb3d9
PG
686 if (link_nsid != NS_UNKNOWN &&
687 vxlan_info.ifindex_link)
688 zebra_if_update_link(ifp, vxlan_info.ifindex_link,
689 link_nsid);
077c07cc
PG
690 } else if (IS_ZEBRA_IF_GRE(ifp)) {
691 struct zebra_l2info_gre gre_info;
692
693 netlink_extract_gre_info(link_data, &gre_info);
694 gre_info.link_nsid = link_nsid;
695 zebra_l2_greif_add_update(ifp, &gre_info, add);
696 if (link_nsid != NS_UNKNOWN &&
697 gre_info.ifindex_link)
698 zebra_if_update_link(ifp, gre_info.ifindex_link,
699 link_nsid);
d62a17ae 700 }
6675513d 701}
702
42b56639
AK
703static int netlink_bridge_vxlan_update(struct interface *ifp,
704 struct rtattr *af_spec)
705{
706 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
707 struct bridge_vlan_info *vinfo;
708 vlanid_t access_vlan;
709
ec897751 710 if (!af_spec)
711 return 0;
712
42b56639
AK
713 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
714 * only 1 access VLAN is accepted.
715 */
c9d842c7 716 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
42b56639
AK
717 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
718 return 0;
719
720 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
721 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
722 return 0;
723
724 access_vlan = (vlanid_t)vinfo->vid;
725 if (IS_ZEBRA_DEBUG_KERNEL)
726 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
727 ifp->name, ifp->ifindex);
728 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
729 return 0;
730}
731
732static void netlink_bridge_vlan_update(struct interface *ifp,
733 struct rtattr *af_spec)
734{
735 struct rtattr *i;
736 int rem;
737 uint16_t vid_range_start = 0;
738 struct zebra_if *zif;
739 bitfield_t old_vlan_bitmap;
740 struct bridge_vlan_info *vinfo;
741
742 zif = (struct zebra_if *)ifp->info;
743
744 /* cache the old bitmap addrs */
745 old_vlan_bitmap = zif->vlan_bitmap;
746 /* create a new bitmap space for re-eval */
747 bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
748
ec897751 749 if (af_spec) {
750 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
751 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
42b56639 752
ec897751 753 if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
754 continue;
42b56639 755
ec897751 756 vinfo = RTA_DATA(i);
42b56639 757
ec897751 758 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
759 vid_range_start = vinfo->vid;
760 continue;
761 }
42b56639 762
ec897751 763 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
764 vid_range_start = vinfo->vid;
42b56639 765
ec897751 766 zebra_vlan_bitmap_compute(ifp, vid_range_start,
767 vinfo->vid);
768 }
42b56639
AK
769 }
770
771 zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
772
773 bf_free(old_vlan_bitmap);
774}
775
d62a17ae 776static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
777 int startup)
6675513d 778{
d62a17ae 779 char *name = NULL;
780 struct ifinfomsg *ifi;
781 struct rtattr *tb[IFLA_MAX + 1];
782 struct interface *ifp;
42b56639
AK
783 struct zebra_if *zif;
784 struct rtattr *af_spec;
d62a17ae 785
786 /* Fetch name and ifindex */
787 ifi = NLMSG_DATA(h);
d62a17ae 788 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
789
790 if (tb[IFLA_IFNAME] == NULL)
791 return -1;
792 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
793
794 /* The interface should already be known, if not discard. */
795 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
796 if (!ifp) {
9df414fe
QY
797 zlog_debug("Cannot find bridge IF %s(%u)", name,
798 ifi->ifi_index);
d62a17ae 799 return 0;
800 }
d62a17ae 801
802 /* We are only interested in the access VLAN i.e., AF_SPEC */
42b56639 803 af_spec = tb[IFLA_AF_SPEC];
d62a17ae 804
42b56639
AK
805 if (IS_ZEBRA_IF_VXLAN(ifp))
806 return netlink_bridge_vxlan_update(ifp, af_spec);
d62a17ae 807
42b56639
AK
808 /* build vlan bitmap associated with this interface if that
809 * device type is interested in the vlans
810 */
811 zif = (struct zebra_if *)ifp->info;
812 if (bf_is_inited(zif->vlan_bitmap))
813 netlink_bridge_vlan_update(ifp, af_spec);
d62a17ae 814
d62a17ae 815 return 0;
6675513d 816}
817
0dcd8506
SW
818static bool is_if_protodown_r_only_frr(uint32_t rc_bitfield)
819{
820 /* This shouldn't be possible */
821 assert(frr_protodown_r_bit < 32);
822 return (rc_bitfield == (((uint32_t)1) << frr_protodown_r_bit));
823}
824
825/*
826 * Process interface protodown dplane update.
827 *
828 * If the interface is an es bond member then it must follow EVPN's
829 * protodown setting.
c36e442c
AK
830 */
831static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
0dcd8506 832 struct rtattr **tb)
c36e442c 833{
0dcd8506
SW
834 bool protodown;
835 bool old_protodown;
836 uint32_t rc_bitfield = 0;
837 struct rtattr *pd_reason_info[IFLA_MAX + 1];
c36e442c 838
0dcd8506
SW
839 protodown = !!*(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
840
841 if (tb[IFLA_PROTO_DOWN_REASON]) {
842 netlink_parse_rtattr_nested(pd_reason_info, IFLA_INFO_MAX,
843 tb[IFLA_PROTO_DOWN_REASON]);
844
845 if (pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE])
846 rc_bitfield = *(uint32_t *)RTA_DATA(
847 pd_reason_info[IFLA_PROTO_DOWN_REASON_VALUE]);
848 }
849
850 /*
851 * Set our reason code to note it wasn't us.
852 * If the reason we got from the kernel is ONLY frr though, don't
853 * set it.
854 */
ab465d24
SW
855 if (protodown && rc_bitfield &&
856 is_if_protodown_r_only_frr(rc_bitfield) == false)
5d414138
SW
857 zif->protodown_rc |= ZEBRA_PROTODOWN_EXTERNAL;
858 else
859 zif->protodown_rc &= ~ZEBRA_PROTODOWN_EXTERNAL;
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
0dcd8506
SW
869 if (protodown)
870 zif->flags |= ZIF_FLAG_PROTODOWN;
871 else
872 zif->flags &= ~ZIF_FLAG_PROTODOWN;
873
c36e442c 874 if (zebra_evpn_is_es_bond_member(zif->ifp)) {
4b82b954
SW
875 /* Check it's not already being sent to the dplane first */
876 if (protodown && (zif->flags & ZIF_FLAG_SET_PROTODOWN))
877 return;
878
879 if (!protodown && (zif->flags & ZIF_FLAG_UNSET_PROTODOWN))
880 return;
881
c36e442c
AK
882 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
883 zlog_debug(
884 "bond mbr %s re-instate protdown %s in the dplane",
0dcd8506
SW
885 zif->ifp->name, old_protodown ? "on" : "off");
886
887 if (old_protodown)
888 zif->flags |= ZIF_FLAG_SET_PROTODOWN;
c36e442c 889 else
0dcd8506
SW
890 zif->flags |= ZIF_FLAG_UNSET_PROTODOWN;
891
892 dplane_intf_update(zif->ifp);
c36e442c
AK
893 }
894}
895
00a7710c
AK
896static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
897{
898 uint8_t bypass = 0;
899 struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
900
c9d842c7
DS
901 netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
902 linkinfo[IFLA_INFO_SLAVE_DATA]);
00a7710c
AK
903 if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
904 bypass = *(uint8_t *)RTA_DATA(
905 mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
906
907 return bypass;
908}
909
0dcd8506 910/*
ab465d24
SW
911 * Only called at startup to cleanup leftover protodown reasons we may
912 * have not cleaned up. We leave protodown set though.
0dcd8506
SW
913 */
914static void if_sweep_protodown(struct zebra_if *zif)
915{
916 bool protodown;
917
d89b3008 918 protodown = !!ZEBRA_IF_IS_PROTODOWN(zif);
0dcd8506
SW
919
920 if (!protodown)
921 return;
922
923 if (IS_ZEBRA_DEBUG_KERNEL)
ab465d24
SW
924 zlog_debug("interface %s sweeping protodown %s reason 0x%x",
925 zif->ifp->name, protodown ? "on" : "off",
926 zif->protodown_rc);
0dcd8506
SW
927
928 /* Only clear our reason codes, leave external if it was set */
ab465d24 929 zif->protodown_rc &= ~ZEBRA_PROTODOWN_ALL;
0dcd8506
SW
930 dplane_intf_update(zif->ifp);
931}
932
2414abd3
DS
933/*
934 * Called from interface_lookup_netlink(). This function is only used
935 * during bootstrap.
936 */
937static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 938{
d62a17ae 939 int len;
940 struct ifinfomsg *ifi;
941 struct rtattr *tb[IFLA_MAX + 1];
942 struct rtattr *linkinfo[IFLA_MAX + 1];
943 struct interface *ifp;
944 char *name = NULL;
945 char *kind = NULL;
48884c6b 946 char *desc = NULL;
d62a17ae 947 char *slave_kind = NULL;
ea7ec261 948 struct zebra_ns *zns = NULL;
d62a17ae 949 vrf_id_t vrf_id = VRF_DEFAULT;
e6f2bec0 950 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
a81982fa 951 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
d62a17ae 952 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
953 ifindex_t link_ifindex = IFINDEX_INTERNAL;
b9368db9 954 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
520ebf72 955 struct zebra_if *zif;
14ddb3d9 956 ns_id_t link_nsid = ns_id;
00a7710c 957 uint8_t bypass = 0;
d62a17ae 958
959 zns = zebra_ns_lookup(ns_id);
960 ifi = NLMSG_DATA(h);
961
962 if (h->nlmsg_type != RTM_NEWLINK)
963 return 0;
964
965 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618 966 if (len < 0) {
15569c58
DA
967 zlog_err(
968 "%s: Message received from netlink is of a broken size: %d %zu",
969 __func__, h->nlmsg_len,
970 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 971 return -1;
9bdf8618 972 }
d62a17ae 973
974 /* We are interested in some AF_BRIDGE notifications. */
975 if (ifi->ifi_family == AF_BRIDGE)
976 return netlink_bridge_interface(h, len, ns_id, startup);
977
978 /* Looking up interface name. */
0d6f7fd6 979 memset(linkinfo, 0, sizeof(linkinfo));
0dcd8506
SW
980 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
981 NLA_F_NESTED);
1fdc9eae 982
d62a17ae 983 /* check for wireless messages to ignore */
984 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
985 if (IS_ZEBRA_DEBUG_KERNEL)
986 zlog_debug("%s: ignoring IFLA_WIRELESS message",
987 __func__);
988 return 0;
989 }
1fdc9eae 990
d62a17ae 991 if (tb[IFLA_IFNAME] == NULL)
992 return -1;
993 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 994
48884c6b
DS
995 if (tb[IFLA_IFALIAS])
996 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
997
d62a17ae 998 if (tb[IFLA_LINKINFO]) {
c9d842c7
DS
999 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1000 tb[IFLA_LINKINFO]);
1fdc9eae 1001
d62a17ae 1002 if (linkinfo[IFLA_INFO_KIND])
1003 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1004
d62a17ae 1005 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1006 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1007
b9368db9
DD
1008 if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
1009 netlink_determine_zebra_iftype("bond_slave", &zif_type);
1010 else
1011 netlink_determine_zebra_iftype(kind, &zif_type);
d62a17ae 1012 }
1013
1014 /* If VRF, create the VRF structure itself. */
78dd30b2 1015 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
5e031198 1016 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
d62a17ae 1017 vrf_id = (vrf_id_t)ifi->ifi_index;
1018 }
1019
1020 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1021 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1022 && !vrf_is_backend_netns()) {
d62a17ae 1023 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 1024 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1025 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
1026 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1027 bridge_ifindex =
1028 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
1029 } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
1030 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1031 bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
00a7710c 1032 bypass = netlink_parse_lacp_bypass(linkinfo);
d62a17ae 1033 } else
1034 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1035 }
78dd30b2
PG
1036 if (vrf_is_backend_netns())
1037 vrf_id = (vrf_id_t)ns_id;
a36898e7 1038
d62a17ae 1039 /* If linking to another interface, note it. */
1040 if (tb[IFLA_LINK])
1041 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1042
b1cc23b2 1043 if (tb[IFLA_LINK_NETNSID]) {
14ddb3d9 1044 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
b1cc23b2
PG
1045 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1046 }
14ddb3d9 1047
f60a1188 1048 ifp = if_get_by_name(name, vrf_id, NULL);
ea7ec261 1049 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
d5c65bf1 1050
d62a17ae 1051 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d62a17ae 1052 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
1053 ifp->metric = 0;
594c2878 1054 ifp->speed = get_iflink_speed(ifp, NULL);
d62a17ae 1055 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1056
1057 /* Set zebra interface type */
1058 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1059 if (IS_ZEBRA_IF_VRF(ifp))
1060 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1061
98efddf1
DS
1062 /*
1063 * Just set the @link/lower-device ifindex. During nldump interfaces are
520ebf72
AK
1064 * not ordered in any fashion so we may end up getting upper devices
1065 * before lower devices. We will setup the real linkage once the dump
98efddf1
DS
1066 * is complete.
1067 */
520ebf72
AK
1068 zif = (struct zebra_if *)ifp->info;
1069 zif->link_ifindex = link_ifindex;
d62a17ae 1070
ba5165ec
DS
1071 if (desc) {
1072 XFREE(MTYPE_TMP, zif->desc);
1073 zif->desc = XSTRDUP(MTYPE_TMP, desc);
1074 }
1075
d62a17ae 1076 /* Hardware type and address. */
1077 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
ebb61fca 1078
d62a17ae 1079 netlink_interface_update_hw_addr(tb, ifp);
1080
1081 if_add_update(ifp);
1082
1083 /* Extract and save L2 interface information, take additional actions.
1084 */
14ddb3d9
PG
1085 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1086 1, link_nsid);
c36e442c
AK
1087 if (IS_ZEBRA_IF_BOND(ifp))
1088 zebra_l2if_update_bond(ifp, true);
d62a17ae 1089 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
c7620108
PG
1090 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
1091 ZEBRA_BRIDGE_NO_ACTION);
b9368db9 1092 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
00a7710c 1093 zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
d62a17ae 1094
c36e442c 1095 if (tb[IFLA_PROTO_DOWN]) {
0dcd8506
SW
1096 netlink_proc_dplane_if_protodown(zif, tb);
1097 if_sweep_protodown(zif);
c36e442c
AK
1098 }
1099
d62a17ae 1100 return 0;
1fdc9eae 1101}
1102
289602d7 1103/* Request for specific interface or address information from the kernel */
85a75f1e
MS
1104static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
1105 int type, uint32_t filter_mask)
289602d7 1106{
d62a17ae 1107 struct {
1108 struct nlmsghdr n;
1109 struct ifinfomsg ifm;
1110 char buf[256];
1111 } req;
1112
1113 /* Form the request, specifying filter (rtattr) if needed. */
1114 memset(&req, 0, sizeof(req));
1115 req.n.nlmsg_type = type;
718f9b0f 1116 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
d62a17ae 1117 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1118 req.ifm.ifi_family = family;
1119
1120 /* Include filter, if specified. */
1121 if (filter_mask)
312a6bee 1122 nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
d62a17ae 1123
fd3f8e52 1124 return netlink_request(netlink_cmd, &req);
289602d7 1125}
1126
62b4b7e4
PG
1127enum netlink_msg_status
1128netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1129{
1130 enum dplane_op_e op;
1131 enum netlink_msg_status ret;
1132
1133 op = dplane_ctx_get_op(ctx);
1134 assert(op == DPLANE_OP_GRE_SET);
1135
1136 ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
1137
1138 return ret;
1139}
1140
1fdc9eae 1141/* Interface lookup by netlink socket. */
d62a17ae 1142int interface_lookup_netlink(struct zebra_ns *zns)
1fdc9eae 1143{
d62a17ae 1144 int ret;
85a75f1e
MS
1145 struct zebra_dplane_info dp_info;
1146 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1147
1148 /* Capture key info from ns struct */
1149 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 1150
1151 /* Get interface information. */
85a75f1e 1152 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
d62a17ae 1153 if (ret < 0)
1154 return ret;
85a75f1e 1155 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
9bfadae8 1156 true);
d62a17ae 1157 if (ret < 0)
1158 return ret;
1159
1160 /* Get interface information - for bridge interfaces. */
85a75f1e 1161 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
d62a17ae 1162 RTEXT_FILTER_BRVLAN);
1163 if (ret < 0)
1164 return ret;
85a75f1e 1165 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
9bfadae8 1166 true);
d62a17ae 1167 if (ret < 0)
1168 return ret;
1169
520ebf72 1170 /* fixup linkages */
357b150d 1171 zebra_if_update_all_links(zns);
d2bec88a
SW
1172 return 0;
1173}
1174
1175/**
1176 * interface_addr_lookup_netlink() - Look up interface addresses
1177 *
1178 * @zns: Zebra netlink socket
1179 * Return: Result status
1180 */
1181static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1182{
1183 int ret;
1184 struct zebra_dplane_info dp_info;
1185 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1186
1187 /* Capture key info from ns struct */
1188 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
520ebf72 1189
d62a17ae 1190 /* Get IPv4 address of the interfaces. */
85a75f1e 1191 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
d62a17ae 1192 if (ret < 0)
1193 return ret;
85a75f1e 1194 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
9bfadae8 1195 0, true);
d62a17ae 1196 if (ret < 0)
1197 return ret;
1198
1199 /* Get IPv6 address of the interfaces. */
85a75f1e 1200 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
d62a17ae 1201 if (ret < 0)
1202 return ret;
85a75f1e 1203 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
9bfadae8 1204 0, true);
d62a17ae 1205 if (ret < 0)
1206 return ret;
1207
1208 return 0;
1fdc9eae 1209}
1210
e0ae31b8
DS
1211int kernel_interface_set_master(struct interface *master,
1212 struct interface *slave)
1213{
1214 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1215
1216 struct {
1217 struct nlmsghdr n;
1218 struct ifinfomsg ifa;
1219 char buf[NL_PKT_BUF_SIZE];
1220 } req;
1221
0d6f7fd6 1222 memset(&req, 0, sizeof(req));
e0ae31b8
DS
1223
1224 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1225 req.n.nlmsg_flags = NLM_F_REQUEST;
1226 req.n.nlmsg_type = RTM_SETLINK;
1227 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1228
1229 req.ifa.ifi_index = slave->ifindex;
1230
a757997c
JU
1231 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1232 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
e0ae31b8
DS
1233
1234 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
9bfadae8 1235 false);
e0ae31b8
DS
1236}
1237
1fdc9eae 1238/* Interface address modification. */
67e3369e
JU
1239static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1240 void *buf, size_t buflen)
1fdc9eae 1241{
d62a17ae 1242 int bytelen;
64168803
MS
1243 const struct prefix *p;
1244 int cmd;
1245 const char *label;
1fdc9eae 1246
d62a17ae 1247 struct {
1248 struct nlmsghdr n;
1249 struct ifaddrmsg ifa;
67e3369e
JU
1250 char buf[0];
1251 } *req = buf;
1252
1253 if (buflen < sizeof(*req))
1254 return 0;
1fdc9eae 1255
64168803 1256 p = dplane_ctx_get_intf_addr(ctx);
67e3369e 1257 memset(req, 0, sizeof(*req));
1fdc9eae 1258
64168803 1259 bytelen = (p->family == AF_INET ? 4 : 16);
1fdc9eae 1260
67e3369e
JU
1261 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1262 req->n.nlmsg_flags = NLM_F_REQUEST;
a55ba23f 1263
64168803
MS
1264 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1265 cmd = RTM_NEWADDR;
1266 else
1267 cmd = RTM_DELADDR;
1268
67e3369e
JU
1269 req->n.nlmsg_type = cmd;
1270 req->ifa.ifa_family = p->family;
1fdc9eae 1271
67e3369e 1272 req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1fdc9eae 1273
67e3369e
JU
1274 if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1275 return 0;
1fdc9eae 1276
64168803
MS
1277 if (p->family == AF_INET) {
1278 if (dplane_ctx_intf_is_connected(ctx)) {
1279 p = dplane_ctx_get_intf_dest(ctx);
67e3369e
JU
1280 if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1281 &p->u.prefix, bytelen))
1282 return 0;
0f3af738
JW
1283 } else if (cmd == RTM_NEWADDR) {
1284 struct in_addr broad = {
1285 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1286 p->prefixlen)
1287 };
67e3369e
JU
1288 if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1289 bytelen))
1290 return 0;
d62a17ae 1291 }
1292 }
1fdc9eae 1293
64168803 1294 /* p is now either address or destination/bcast addr */
67e3369e 1295 req->ifa.ifa_prefixlen = p->prefixlen;
e8d19a05 1296
64168803 1297 if (dplane_ctx_intf_is_secondary(ctx))
67e3369e 1298 SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1fdc9eae 1299
64168803
MS
1300 if (dplane_ctx_intf_has_label(ctx)) {
1301 label = dplane_ctx_get_intf_label(ctx);
67e3369e
JU
1302 if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1303 strlen(label) + 1))
1304 return 0;
64168803 1305 }
1fdc9eae 1306
67e3369e 1307 return NLMSG_ALIGN(req->n.nlmsg_len);
e86b71f1
PG
1308}
1309
67e3369e
JU
1310enum netlink_msg_status
1311netlink_put_address_update_msg(struct nl_batch *bth,
1312 struct zebra_dplane_ctx *ctx)
1313{
1314 return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1315 false);
e86b71f1
PG
1316}
1317
5d414138
SW
1318static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
1319 size_t buflen)
1320{
1321 enum dplane_op_e op;
1322 int cmd = 0;
1323
1324 op = dplane_ctx_get_op(ctx);
1325
1326 switch (op) {
1327 case DPLANE_OP_INTF_UPDATE:
1328 cmd = RTM_SETLINK;
1329 break;
1330 case DPLANE_OP_INTF_INSTALL:
1331 cmd = RTM_NEWLINK;
1332 break;
1333 case DPLANE_OP_INTF_DELETE:
1334 cmd = RTM_DELLINK;
1335 break;
1336 default:
1337 flog_err(
1338 EC_ZEBRA_NHG_FIB_UPDATE,
1339 "Context received for kernel interface update with incorrect OP code (%u)",
1340 op);
1341 return -1;
1342 }
1343
1344 return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
1345}
1346
1347enum netlink_msg_status
1348netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1349{
1350 return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
1351}
1352
2414abd3 1353int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1354{
d62a17ae 1355 int len;
1356 struct ifaddrmsg *ifa;
1357 struct rtattr *tb[IFA_MAX + 1];
1358 struct interface *ifp;
1359 void *addr;
1360 void *broad;
d7c0a89a 1361 uint8_t flags = 0;
d62a17ae 1362 char *label = NULL;
1363 struct zebra_ns *zns;
cde1af84 1364 uint32_t metric = METRIC_MAX;
9254efed 1365 uint32_t kernel_flags = 0;
d62a17ae 1366
1367 zns = zebra_ns_lookup(ns_id);
1368 ifa = NLMSG_DATA(h);
1369
8a1b681c 1370 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
9df414fe 1371 flog_warn(
e914ccbe 1372 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
1373 "Invalid address family: %u received from kernel interface addr change: %s",
1374 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1375 return 0;
8a1b681c 1376 }
d62a17ae 1377
1378 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1379 return 0;
1380
1381 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
9bdf8618 1382 if (len < 0) {
15569c58
DA
1383 zlog_err(
1384 "%s: Message received from netlink is of a broken size: %d %zu",
1385 __func__, h->nlmsg_len,
1386 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
d62a17ae 1387 return -1;
9bdf8618 1388 }
d62a17ae 1389
d62a17ae 1390 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1391
1392 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1393 if (ifp == NULL) {
8eec31ef
YY
1394 if (startup) {
1395 /* During startup, failure to lookup the referenced
1396 * interface should not be an error, so we have
1397 * downgraded this condition to warning, and we permit
1398 * the startup interface state retrieval to continue.
1399 */
1400 flog_warn(EC_LIB_INTERFACE,
1401 "%s: can't find interface by index %d",
1402 __func__, ifa->ifa_index);
1403 return 0;
1404 } else {
1405 flog_err(EC_LIB_INTERFACE,
1406 "%s: can't find interface by index %d",
1407 __func__, ifa->ifa_index);
1408 return -1;
1409 }
d62a17ae 1410 }
1411
9254efed
DS
1412 /* Flags passed through */
1413 if (tb[IFA_FLAGS])
1414 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1415 else
1416 kernel_flags = ifa->ifa_flags;
1417
d62a17ae 1418 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1419 {
1420 char buf[BUFSIZ];
1421 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1422 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
9254efed 1423 kernel_flags);
d62a17ae 1424 if (tb[IFA_LOCAL])
1425 zlog_debug(" IFA_LOCAL %s/%d",
1426 inet_ntop(ifa->ifa_family,
1427 RTA_DATA(tb[IFA_LOCAL]), buf,
1428 BUFSIZ),
1429 ifa->ifa_prefixlen);
1430 if (tb[IFA_ADDRESS])
1431 zlog_debug(" IFA_ADDRESS %s/%d",
1432 inet_ntop(ifa->ifa_family,
1433 RTA_DATA(tb[IFA_ADDRESS]), buf,
1434 BUFSIZ),
1435 ifa->ifa_prefixlen);
1436 if (tb[IFA_BROADCAST])
1437 zlog_debug(" IFA_BROADCAST %s/%d",
1438 inet_ntop(ifa->ifa_family,
1439 RTA_DATA(tb[IFA_BROADCAST]), buf,
1440 BUFSIZ),
1441 ifa->ifa_prefixlen);
1442 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1443 zlog_debug(" IFA_LABEL %s",
1444 (char *)RTA_DATA(tb[IFA_LABEL]));
1445
1446 if (tb[IFA_CACHEINFO]) {
1447 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1448 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1449 ci->ifa_prefered, ci->ifa_valid);
1450 }
1451 }
1452
1453 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1454 if (tb[IFA_LOCAL] == NULL)
1455 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1456 if (tb[IFA_ADDRESS] == NULL)
1457 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1458
1459 /* local interface address */
1460 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1461
1462 /* is there a peer address? */
1463 if (tb[IFA_ADDRESS]
1464 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1465 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1466 broad = RTA_DATA(tb[IFA_ADDRESS]);
1467 SET_FLAG(flags, ZEBRA_IFA_PEER);
1468 } else
1469 /* seeking a broadcast address */
1470 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1471 : NULL);
1472
1473 /* addr is primary key, SOL if we don't have one */
1474 if (addr == NULL) {
14a4d9d0 1475 zlog_debug("%s: Local Interface Address is NULL for %s",
1476 __func__, ifp->name);
d62a17ae 1477 return -1;
1478 }
1479
1480 /* Flags. */
9254efed 1481 if (kernel_flags & IFA_F_SECONDARY)
d62a17ae 1482 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1483
1484 /* Label */
1485 if (tb[IFA_LABEL])
1486 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1487
2e1cc436 1488 if (label && strcmp(ifp->name, label) == 0)
d62a17ae 1489 label = NULL;
1490
cde1af84
AK
1491 if (tb[IFA_RT_PRIORITY])
1492 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1493
d62a17ae 1494 /* Register interface address to the interface. */
1495 if (ifa->ifa_family == AF_INET) {
930571d2 1496 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
e17d9b2d 1497 zlog_err(
87b5d1b0
DS
1498 "Invalid prefix length: %u received from kernel interface addr change: %s",
1499 ifa->ifa_prefixlen,
1500 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1501 return -1;
930571d2 1502 }
20e879f9 1503
d62a17ae 1504 if (h->nlmsg_type == RTM_NEWADDR)
1505 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1506 ifa->ifa_prefixlen,
cde1af84
AK
1507 (struct in_addr *)broad, label,
1508 metric);
20e879f9
MS
1509 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1510 /* Delete with a peer address */
1511 connected_delete_ipv4(
1512 ifp, flags, (struct in_addr *)addr,
1513 ifa->ifa_prefixlen, broad);
1514 } else
d62a17ae 1515 connected_delete_ipv4(
1516 ifp, flags, (struct in_addr *)addr,
fd267f08 1517 ifa->ifa_prefixlen, NULL);
d62a17ae 1518 }
20e879f9 1519
d62a17ae 1520 if (ifa->ifa_family == AF_INET6) {
930571d2 1521 if (ifa->ifa_prefixlen > IPV6_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 }
d62a17ae 1528 if (h->nlmsg_type == RTM_NEWADDR) {
1529 /* Only consider valid addresses; we'll not get a
1530 * notification from
1531 * the kernel till IPv6 DAD has completed, but at init
1532 * time, Quagga
1533 * does query for and will receive all addresses.
1534 */
9254efed 1535 if (!(kernel_flags
d62a17ae 1536 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
60466a63
QY
1537 connected_add_ipv6(ifp, flags,
1538 (struct in6_addr *)addr,
60c0687a 1539 (struct in6_addr *)broad,
cde1af84
AK
1540 ifa->ifa_prefixlen, label,
1541 metric);
d62a17ae 1542 } else
1543 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
fd267f08 1544 NULL, ifa->ifa_prefixlen);
d62a17ae 1545 }
1546
2a181147
SW
1547 /*
1548 * Linux kernel does not send route delete on interface down/addr del
1549 * so we have to re-process routes it owns (i.e. kernel routes)
1550 */
1551 if (h->nlmsg_type != RTM_NEWADDR)
1552 rib_update(RIB_UPDATE_KERNEL);
1553
d62a17ae 1554 return 0;
1fdc9eae 1555}
1556
e7c2c198
MS
1557/*
1558 * Parse and validate an incoming interface address change message,
1559 * generating a dplane context object.
1560 * This runs in the dplane pthread; the context is enqueued to the
1561 * main pthread for processing.
1562 */
1563int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1564 int startup /*ignored*/)
1565{
1566 int len;
1567 struct ifaddrmsg *ifa;
1568 struct rtattr *tb[IFA_MAX + 1];
1569 void *addr;
1570 void *broad;
1571 char *label = NULL;
1572 uint32_t metric = METRIC_MAX;
1573 uint32_t kernel_flags = 0;
1574 struct zebra_dplane_ctx *ctx;
1575 struct prefix p;
1576
1577 ifa = NLMSG_DATA(h);
1578
1579 /* Validate message types */
1580 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1581 return 0;
1582
1583 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1584 if (IS_ZEBRA_DEBUG_KERNEL)
1585 zlog_debug("%s: %s: Invalid address family: %u",
1586 __func__, nl_msg_type_to_str(h->nlmsg_type),
1587 ifa->ifa_family);
1588 return 0;
1589 }
1590
1591 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1592 if (len < 0) {
1593 if (IS_ZEBRA_DEBUG_KERNEL)
1594 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1595 __func__, nl_msg_type_to_str(h->nlmsg_type),
1596 h->nlmsg_len,
1597 (size_t)NLMSG_LENGTH(
1598 sizeof(struct ifaddrmsg)));
1599 return -1;
1600 }
1601
1602 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1603
1604 /* Flags passed through */
1605 if (tb[IFA_FLAGS])
1606 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1607 else
1608 kernel_flags = ifa->ifa_flags;
1609
1610 if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1611 char buf[PREFIX_STRLEN];
1612
1613 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1614 nl_msg_type_to_str(h->nlmsg_type), ns_id,
1615 ifa->ifa_index, kernel_flags);
1616 if (tb[IFA_LOCAL])
1617 zlog_debug(" IFA_LOCAL %s/%d",
1618 inet_ntop(ifa->ifa_family,
1619 RTA_DATA(tb[IFA_LOCAL]), buf,
1620 sizeof(buf)),
1621 ifa->ifa_prefixlen);
1622 if (tb[IFA_ADDRESS])
1623 zlog_debug(" IFA_ADDRESS %s/%d",
1624 inet_ntop(ifa->ifa_family,
1625 RTA_DATA(tb[IFA_ADDRESS]), buf,
1626 sizeof(buf)),
1627 ifa->ifa_prefixlen);
1628 if (tb[IFA_BROADCAST])
1629 zlog_debug(" IFA_BROADCAST %s/%d",
1630 inet_ntop(ifa->ifa_family,
1631 RTA_DATA(tb[IFA_BROADCAST]), buf,
1632 sizeof(buf)),
1633 ifa->ifa_prefixlen);
1634 if (tb[IFA_LABEL])
1635 zlog_debug(" IFA_LABEL %s",
1636 (const char *)RTA_DATA(tb[IFA_LABEL]));
1637
1638 if (tb[IFA_CACHEINFO]) {
1639 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1640
1641 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1642 ci->ifa_prefered, ci->ifa_valid);
1643 }
1644 }
1645
1646 /* Validate prefix length */
1647
1648 if (ifa->ifa_family == AF_INET
1649 && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1650 if (IS_ZEBRA_DEBUG_KERNEL)
1651 zlog_debug("%s: %s: Invalid prefix length: %u",
1652 __func__, nl_msg_type_to_str(h->nlmsg_type),
1653 ifa->ifa_prefixlen);
1654 return -1;
1655 }
1656
1657 if (ifa->ifa_family == AF_INET6) {
1658 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1659 if (IS_ZEBRA_DEBUG_KERNEL)
1660 zlog_debug("%s: %s: Invalid prefix length: %u",
1661 __func__,
1662 nl_msg_type_to_str(h->nlmsg_type),
1663 ifa->ifa_prefixlen);
1664 return -1;
1665 }
1666
1667 /* Only consider valid addresses; we'll not get a kernel
1668 * notification till IPv6 DAD has completed, but at init
1669 * time, FRR does query for and will receive all addresses.
1670 */
1671 if (h->nlmsg_type == RTM_NEWADDR
1672 && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1673 if (IS_ZEBRA_DEBUG_KERNEL)
1674 zlog_debug("%s: %s: Invalid/tentative addr",
1675 __func__,
1676 nl_msg_type_to_str(h->nlmsg_type));
1677 return 0;
1678 }
1679 }
1680
1681 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1682 if (tb[IFA_LOCAL] == NULL)
1683 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1684 if (tb[IFA_ADDRESS] == NULL)
1685 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1686
1687 /* local interface address */
1688 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1689
1690 /* addr is primary key, SOL if we don't have one */
1691 if (addr == NULL) {
1692 if (IS_ZEBRA_DEBUG_KERNEL)
1693 zlog_debug("%s: %s: No local interface address",
1694 __func__, nl_msg_type_to_str(h->nlmsg_type));
1695 return -1;
1696 }
1697
1698 /* Allocate a context object, now that validation is done. */
1699 ctx = dplane_ctx_alloc();
1700 if (h->nlmsg_type == RTM_NEWADDR)
1701 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1702 else
1703 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1704
1705 dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1706 dplane_ctx_set_ns_id(ctx, ns_id);
1707
1708 /* Convert addr to prefix */
1709 memset(&p, 0, sizeof(p));
1710 p.family = ifa->ifa_family;
1711 p.prefixlen = ifa->ifa_prefixlen;
1712 if (p.family == AF_INET)
1713 p.u.prefix4 = *(struct in_addr *)addr;
1714 else
1715 p.u.prefix6 = *(struct in6_addr *)addr;
1716
1717 dplane_ctx_set_intf_addr(ctx, &p);
1718
1719 /* is there a peer address? */
1720 if (tb[IFA_ADDRESS]
1721 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1722 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1723 broad = RTA_DATA(tb[IFA_ADDRESS]);
1724 dplane_ctx_intf_set_connected(ctx);
1725 } else if (tb[IFA_BROADCAST]) {
1726 /* seeking a broadcast address */
1727 broad = RTA_DATA(tb[IFA_BROADCAST]);
1728 dplane_ctx_intf_set_broadcast(ctx);
1729 } else
1730 broad = NULL;
1731
1732 if (broad) {
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 *)broad;
1739 else
1740 p.u.prefix6 = *(struct in6_addr *)broad;
1741
1742 dplane_ctx_set_intf_dest(ctx, &p);
1743 }
1744
1745 /* Flags. */
1746 if (kernel_flags & IFA_F_SECONDARY)
1747 dplane_ctx_intf_set_secondary(ctx);
1748
1749 /* Label */
1750 if (tb[IFA_LABEL]) {
1751 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1752 dplane_ctx_set_intf_label(ctx, label);
1753 }
1754
1755 if (tb[IFA_RT_PRIORITY])
1756 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1757
1758 dplane_ctx_set_intf_metric(ctx, metric);
1759
1760 /* Enqueue ctx for main pthread to process */
1761 dplane_provider_enqueue_to_zebra(ctx);
1762
1763 return 0;
1764}
1765
2414abd3 1766int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1767{
d62a17ae 1768 int len;
1769 struct ifinfomsg *ifi;
1770 struct rtattr *tb[IFLA_MAX + 1];
1771 struct rtattr *linkinfo[IFLA_MAX + 1];
1772 struct interface *ifp;
1773 char *name = NULL;
1774 char *kind = NULL;
48884c6b 1775 char *desc = NULL;
d62a17ae 1776 char *slave_kind = NULL;
1777 struct zebra_ns *zns;
1778 vrf_id_t vrf_id = VRF_DEFAULT;
e6f2bec0 1779 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
a81982fa 1780 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
d62a17ae 1781 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
b9368db9 1782 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
d62a17ae 1783 ifindex_t link_ifindex = IFINDEX_INTERNAL;
97c4e1d0 1784 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
ba5165ec 1785 struct zebra_if *zif;
14ddb3d9 1786 ns_id_t link_nsid = ns_id;
c36e442c 1787 ifindex_t master_infindex = IFINDEX_INTERNAL;
00a7710c 1788 uint8_t bypass = 0;
d62a17ae 1789
1790 zns = zebra_ns_lookup(ns_id);
1791 ifi = NLMSG_DATA(h);
1792
fe533c56 1793 /* assume if not default zns, then new VRF */
d62a17ae 1794 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1795 /* If this is not link add/delete message so print warning. */
87b5d1b0
DS
1796 zlog_debug("netlink_link_change: wrong kernel message %s",
1797 nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1798 return 0;
1799 }
1800
8a1b681c
SW
1801 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1802 || ifi->ifi_family == AF_INET6)) {
9df414fe 1803 flog_warn(
e914ccbe 1804 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
1805 "Invalid address family: %u received from kernel link change: %s",
1806 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
1807 return 0;
1808 }
1809
d62a17ae 1810 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618 1811 if (len < 0) {
15569c58
DA
1812 zlog_err(
1813 "%s: Message received from netlink is of a broken size %d %zu",
1814 __func__, h->nlmsg_len,
1815 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 1816 return -1;
9bdf8618 1817 }
d62a17ae 1818
1819 /* We are interested in some AF_BRIDGE notifications. */
1820 if (ifi->ifi_family == AF_BRIDGE)
1821 return netlink_bridge_interface(h, len, ns_id, startup);
1822
1823 /* Looking up interface name. */
0d6f7fd6 1824 memset(linkinfo, 0, sizeof(linkinfo));
0dcd8506
SW
1825 netlink_parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len,
1826 NLA_F_NESTED);
1fdc9eae 1827
d62a17ae 1828 /* check for wireless messages to ignore */
1829 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1830 if (IS_ZEBRA_DEBUG_KERNEL)
1831 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1832 __func__);
1833 return 0;
1834 }
1fdc9eae 1835
d62a17ae 1836 if (tb[IFLA_IFNAME] == NULL)
1837 return -1;
1838 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1839
e9f79fff
MS
1840 /* Must be valid string. */
1841 len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
1842 if (len < 2 || name[len - 1] != '\0') {
1843 if (IS_ZEBRA_DEBUG_KERNEL)
1844 zlog_debug("%s: invalid intf name", __func__);
1845 return -1;
1846 }
1847
d62a17ae 1848 if (tb[IFLA_LINKINFO]) {
c9d842c7
DS
1849 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1850 tb[IFLA_LINKINFO]);
1fdc9eae 1851
d62a17ae 1852 if (linkinfo[IFLA_INFO_KIND])
1853 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1854
d62a17ae 1855 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1856 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1857
d62a17ae 1858 netlink_determine_zebra_iftype(kind, &zif_type);
1859 }
6675513d 1860
d62a17ae 1861 /* If linking to another interface, note it. */
1862 if (tb[IFLA_LINK])
1863 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1fdc9eae 1864
b1cc23b2 1865 if (tb[IFLA_LINK_NETNSID]) {
14ddb3d9 1866 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
b1cc23b2
PG
1867 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1868 }
48884c6b
DS
1869 if (tb[IFLA_IFALIAS]) {
1870 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1871 }
1872
d62a17ae 1873 /* See if interface is present. */
1874 ifp = if_lookup_by_name_per_ns(zns, name);
1875
1876 if (h->nlmsg_type == RTM_NEWLINK) {
e4c5b3ba
IR
1877 /* If VRF, create or update the VRF structure itself. */
1878 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1879 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1880 vrf_id = (vrf_id_t)ifi->ifi_index;
1881 }
1882
d62a17ae 1883 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1884 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1885 && !vrf_is_backend_netns()) {
d62a17ae 1886 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
c36e442c
AK
1887 master_infindex = vrf_id =
1888 *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1889 } else if (slave_kind
1890 && (strcmp(slave_kind, "bridge") == 0)) {
1891 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
c36e442c 1892 master_infindex = bridge_ifindex =
d62a17ae 1893 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
1894 } else if (slave_kind
1895 && (strcmp(slave_kind, "bond") == 0)) {
1896 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
c36e442c 1897 master_infindex = bond_ifindex =
b9368db9 1898 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
00a7710c 1899 bypass = netlink_parse_lacp_bypass(linkinfo);
d62a17ae 1900 } else
1901 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1902 }
fe533c56
PG
1903 if (vrf_is_backend_netns())
1904 vrf_id = (vrf_id_t)ns_id;
d62a17ae 1905 if (ifp == NULL
1906 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1907 /* Add interface notification from kernel */
1908 if (IS_ZEBRA_DEBUG_KERNEL)
1909 zlog_debug(
3efd0893 1910 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
d62a17ae 1911 name, ifi->ifi_index, vrf_id, zif_type,
c36e442c 1912 zif_slave_type, master_infindex,
d62a17ae 1913 ifi->ifi_flags);
1914
1915 if (ifp == NULL) {
1916 /* unknown interface */
f60a1188 1917 ifp = if_get_by_name(name, vrf_id, NULL);
d62a17ae 1918 } else {
1919 /* pre-configured interface, learnt now */
096f7609 1920 if (ifp->vrf->vrf_id != vrf_id)
a36898e7 1921 if_update_to_new_vrf(ifp, vrf_id);
d62a17ae 1922 }
1923
1924 /* Update interface information. */
1925 set_ifindex(ifp, ifi->ifi_index, zns);
1926 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d23b983b 1927 if (!tb[IFLA_MTU]) {
9df414fe 1928 zlog_debug(
d23b983b
SW
1929 "RTM_NEWLINK for interface %s(%u) without MTU set",
1930 name, ifi->ifi_index);
1931 return 0;
1932 }
d62a17ae 1933 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1934 ifp->metric = 0;
1935 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1936
1937 /* Set interface type */
1938 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1939 if (IS_ZEBRA_IF_VRF(ifp))
1940 SET_FLAG(ifp->status,
1941 ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1942
1943 /* Update link. */
680c278f 1944 zebra_if_update_link(ifp, link_ifindex, ns_id);
d62a17ae 1945
8975bbbd
IR
1946 ifp->ll_type =
1947 netlink_to_zebra_link_type(ifi->ifi_type);
d62a17ae 1948 netlink_interface_update_hw_addr(tb, ifp);
1949
1950 /* Inform clients, install any configured addresses. */
1951 if_add_update(ifp);
1952
1953 /* Extract and save L2 interface information, take
1954 * additional actions. */
1955 netlink_interface_update_l2info(
14ddb3d9
PG
1956 ifp, linkinfo[IFLA_INFO_DATA],
1957 1, link_nsid);
d62a17ae 1958 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
c7620108
PG
1959 zebra_l2if_update_bridge_slave(
1960 ifp, bridge_ifindex, ns_id,
1961 ZEBRA_BRIDGE_NO_ACTION);
b9368db9 1962 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
00a7710c
AK
1963 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
1964 !!bypass);
c36e442c 1965
0dcd8506
SW
1966 if (tb[IFLA_PROTO_DOWN])
1967 netlink_proc_dplane_if_protodown(ifp->info, tb);
c36e442c 1968
096f7609 1969 } else if (ifp->vrf->vrf_id != vrf_id) {
d62a17ae 1970 /* VRF change for an interface. */
1971 if (IS_ZEBRA_DEBUG_KERNEL)
1972 zlog_debug(
3efd0893 1973 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
096f7609
IR
1974 name, ifp->ifindex, ifp->vrf->vrf_id,
1975 vrf_id, ifi->ifi_flags);
d62a17ae 1976
a36898e7 1977 if_handle_vrf_change(ifp, vrf_id);
d62a17ae 1978 } else {
b9368db9 1979 bool was_bridge_slave, was_bond_slave;
c7620108 1980 uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
2d04bd98 1981 zif = ifp->info;
d62a17ae 1982
1983 /* Interface update. */
1984 if (IS_ZEBRA_DEBUG_KERNEL)
1985 zlog_debug(
3efd0893 1986 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
d62a17ae 1987 name, ifp->ifindex, zif_slave_type,
c36e442c 1988 master_infindex, ifi->ifi_flags);
d62a17ae 1989
1990 set_ifindex(ifp, ifi->ifi_index, zns);
d23b983b 1991 if (!tb[IFLA_MTU]) {
9df414fe 1992 zlog_debug(
d23b983b
SW
1993 "RTM_NEWLINK for interface %s(%u) without MTU set",
1994 name, ifi->ifi_index);
1995 return 0;
1996 }
d62a17ae 1997 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1998 ifp->metric = 0;
1999
2000 /* Update interface type - NOTE: Only slave_type can
2001 * change. */
2002 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
b9368db9 2003 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
d62a17ae 2004 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
2005
97c4e1d0
CS
2006 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
2007
ecffe916
PG
2008 /* Update link. */
2009 zebra_if_update_link(ifp, link_ifindex, ns_id);
2010
8975bbbd
IR
2011 ifp->ll_type =
2012 netlink_to_zebra_link_type(ifi->ifi_type);
d62a17ae 2013 netlink_interface_update_hw_addr(tb, ifp);
2014
0dcd8506
SW
2015 if (tb[IFLA_PROTO_DOWN])
2016 netlink_proc_dplane_if_protodown(ifp->info, tb);
2d04bd98 2017
d62a17ae 2018 if (if_is_no_ptm_operative(ifp)) {
8b48cdb9 2019 bool is_up = if_is_operative(ifp);
d62a17ae 2020 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2d04bd98
CS
2021 if (!if_is_no_ptm_operative(ifp) ||
2022 CHECK_FLAG(zif->flags,
2023 ZIF_FLAG_PROTODOWN)) {
d62a17ae 2024 if (IS_ZEBRA_DEBUG_KERNEL)
2025 zlog_debug(
2026 "Intf %s(%u) has gone DOWN",
2027 name, ifp->ifindex);
2028 if_down(ifp);
2a181147 2029 rib_update(RIB_UPDATE_KERNEL);
d62a17ae 2030 } else if (if_is_operative(ifp)) {
c7620108
PG
2031 bool mac_updated = false;
2032
d62a17ae 2033 /* Must notify client daemons of new
2034 * interface status. */
2035 if (IS_ZEBRA_DEBUG_KERNEL)
2036 zlog_debug(
2037 "Intf %s(%u) PTM up, notifying clients",
2038 name, ifp->ifindex);
8b48cdb9 2039 if_up(ifp, !is_up);
97c4e1d0
CS
2040
2041 /* Update EVPN VNI when SVI MAC change
2042 */
c7620108
PG
2043 if (memcmp(old_hw_addr, ifp->hw_addr,
2044 INTERFACE_HWADDR_MAX))
2045 mac_updated = true;
2046 if (IS_ZEBRA_IF_VLAN(ifp)
2047 && mac_updated) {
97c4e1d0
CS
2048 struct interface *link_if;
2049
2050 link_if =
2051 if_lookup_by_index_per_ns(
2052 zebra_ns_lookup(NS_DEFAULT),
2053 link_ifindex);
2054 if (link_if)
2055 zebra_vxlan_svi_up(ifp,
2056 link_if);
c7620108
PG
2057 } else if (mac_updated
2058 && IS_ZEBRA_IF_BRIDGE(ifp)) {
2059 zlog_debug(
2060 "Intf %s(%u) bridge changed MAC address",
2061 name, ifp->ifindex);
2062 chgflags =
2063 ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
97c4e1d0 2064 }
d62a17ae 2065 }
2066 } else {
2067 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2d04bd98
CS
2068 if (if_is_operative(ifp) &&
2069 !CHECK_FLAG(zif->flags,
2070 ZIF_FLAG_PROTODOWN)) {
d62a17ae 2071 if (IS_ZEBRA_DEBUG_KERNEL)
2072 zlog_debug(
2073 "Intf %s(%u) has come UP",
2074 name, ifp->ifindex);
8b48cdb9 2075 if_up(ifp, true);
f56a15b5
PG
2076 if (IS_ZEBRA_IF_BRIDGE(ifp))
2077 chgflags =
2078 ZEBRA_BRIDGE_MASTER_UP;
6f908ded
QY
2079 } else {
2080 if (IS_ZEBRA_DEBUG_KERNEL)
2081 zlog_debug(
7913714e 2082 "Intf %s(%u) has gone DOWN",
6f908ded
QY
2083 name, ifp->ifindex);
2084 if_down(ifp);
2a181147 2085 rib_update(RIB_UPDATE_KERNEL);
d62a17ae 2086 }
2087 }
2088
2089 /* Extract and save L2 interface information, take
2090 * additional actions. */
2091 netlink_interface_update_l2info(
14ddb3d9
PG
2092 ifp, linkinfo[IFLA_INFO_DATA],
2093 0, link_nsid);
c7620108
PG
2094 if (IS_ZEBRA_IF_BRIDGE(ifp))
2095 zebra_l2if_update_bridge(ifp, chgflags);
c36e442c
AK
2096 if (IS_ZEBRA_IF_BOND(ifp))
2097 zebra_l2if_update_bond(ifp, true);
d62a17ae 2098 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
c7620108
PG
2099 zebra_l2if_update_bridge_slave(
2100 ifp, bridge_ifindex, ns_id, chgflags);
b9368db9 2101 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
00a7710c
AK
2102 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2103 !!bypass);
d62a17ae 2104 }
26f13577
DS
2105
2106 zif = ifp->info;
2107 if (zif) {
2108 XFREE(MTYPE_TMP, zif->desc);
2109 if (desc)
2110 zif->desc = XSTRDUP(MTYPE_TMP, desc);
2111 }
d62a17ae 2112 } else {
2113 /* Delete interface notification from kernel */
2114 if (ifp == NULL) {
9df414fe
QY
2115 if (IS_ZEBRA_DEBUG_KERNEL)
2116 zlog_debug(
2117 "RTM_DELLINK for unknown interface %s(%u)",
2118 name, ifi->ifi_index);
d62a17ae 2119 return 0;
2120 }
2121
2122 if (IS_ZEBRA_DEBUG_KERNEL)
2123 zlog_debug("RTM_DELLINK for %s(%u)", name,
2124 ifp->ifindex);
2125
2126 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
2127
c36e442c
AK
2128 if (IS_ZEBRA_IF_BOND(ifp))
2129 zebra_l2if_update_bond(ifp, false);
00a7710c
AK
2130 if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2131 zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
d62a17ae 2132 /* Special handling for bridge or VxLAN interfaces. */
2133 if (IS_ZEBRA_IF_BRIDGE(ifp))
2134 zebra_l2_bridge_del(ifp);
2135 else if (IS_ZEBRA_IF_VXLAN(ifp))
2136 zebra_l2_vxlanif_del(ifp);
2137
af736200 2138 if_delete_update(ifp);
e4c5b3ba
IR
2139
2140 /* If VRF, delete the VRF structure itself. */
2141 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
2142 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1fdc9eae 2143 }
2144
d62a17ae 2145 return 0;
1fdc9eae 2146}
718e3744 2147
5d414138
SW
2148/**
2149 * Interface encoding helper function.
2150 *
2151 * \param[in] cmd netlink command.
2152 * \param[in] ctx dataplane context (information snapshot).
2153 * \param[out] buf buffer to hold the packet.
2154 * \param[in] buflen amount of buffer bytes.
2155 */
2156
2157ssize_t netlink_intf_msg_encode(uint16_t cmd,
2158 const struct zebra_dplane_ctx *ctx, void *buf,
2159 size_t buflen)
2160{
2161 struct {
2162 struct nlmsghdr n;
2163 struct ifinfomsg ifa;
2164 char buf[];
2165 } *req = buf;
2166
2167 struct rtattr *nest_protodown_reason;
2168 ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
5d414138 2169 bool down = dplane_ctx_intf_is_protodown(ctx);
ab465d24 2170 bool pd_reason_val = dplane_ctx_get_intf_pd_reason_val(ctx);
5d414138
SW
2171 struct nlsock *nl =
2172 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
2173
2174 if (buflen < sizeof(*req))
2175 return 0;
2176
2177 memset(req, 0, sizeof(*req));
2178
2179 if (cmd != RTM_SETLINK)
2180 flog_err(
2181 EC_ZEBRA_INTF_UPDATE_FAILURE,
2182 "Only RTM_SETLINK message type currently supported in dplane pthread");
2183
2184 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2185 req->n.nlmsg_flags = NLM_F_REQUEST;
2186 req->n.nlmsg_type = cmd;
2187 req->n.nlmsg_pid = nl->snl.nl_pid;
2188
2189 req->ifa.ifi_index = ifindex;
2190
2191 nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
2192 nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
2193
0dcd8506
SW
2194 /* Reason info nest */
2195 nest_protodown_reason =
2196 nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
5d414138 2197
0dcd8506
SW
2198 if (!nest_protodown_reason)
2199 return -1;
5d414138 2200
0dcd8506
SW
2201 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
2202 (1 << frr_protodown_r_bit));
2203 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
ab465d24 2204 ((int)pd_reason_val) << frr_protodown_r_bit);
5d414138 2205
0dcd8506 2206 nl_attr_nest_end(&req->n, nest_protodown_reason);
5d414138
SW
2207
2208 if (IS_ZEBRA_DEBUG_KERNEL)
ab465d24
SW
2209 zlog_debug("%s: %s, protodown=%d reason_val=%d ifindex=%u",
2210 __func__, nl_msg_type_to_str(cmd), down,
2211 pd_reason_val, ifindex);
5d414138
SW
2212
2213 return NLMSG_ALIGN(req->n.nlmsg_len);
2214}
2215
718e3744 2216/* Interface information read by netlink. */
d62a17ae 2217void interface_list(struct zebra_ns *zns)
718e3744 2218{
d62a17ae 2219 interface_lookup_netlink(zns);
d9f5b2f5
SW
2220 /* We add routes for interface address,
2221 * so we need to get the nexthop info
2222 * from the kernel before we can do that
2223 */
81505946 2224 netlink_nexthop_read(zns);
cc4e0650 2225
d2bec88a 2226 interface_addr_lookup_netlink(zns);
718e3744 2227}
ddfeb486 2228
c40e1b1c
SW
2229void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
2230{
2231 if (IS_ZEBRA_DEBUG_KERNEL)
2232 zlog_debug("FRR protodown reason bit change %u -> %u",
2233 frr_protodown_r_bit, bit);
2234
2235 frr_protodown_r_bit = bit;
2236}
2237
2238void if_netlink_unset_frr_protodown_r_bit(void)
2239{
2240 if (IS_ZEBRA_DEBUG_KERNEL)
2241 zlog_debug("FRR protodown reason bit change %u -> %u",
2242 frr_protodown_r_bit,
2243 FRR_PROTODOWN_REASON_DEFAULT_BIT);
2244
2245 frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
2246}
2247
2248
2249bool if_netlink_frr_protodown_r_bit_is_set(void)
2250{
2251 return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
2252}
2253
2254uint8_t if_netlink_get_frr_protodown_r_bit(void)
2255{
2256 return frr_protodown_r_bit;
2257}
2258
ddfeb486 2259#endif /* GNU_LINUX */