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