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