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