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