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