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