]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_netlink.c
bgpd: Allow 'no set community`
[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) {
bd7d0299
DS
402 if (IS_ZEBRA_DEBUG_KERNEL)
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;
734 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
735 req.ifm.ifi_family = family;
736
737 /* Include filter, if specified. */
738 if (filter_mask)
739 addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
740
85a75f1e 741 return netlink_request(netlink_cmd, &req.n);
289602d7 742}
743
1fdc9eae 744/* Interface lookup by netlink socket. */
d62a17ae 745int interface_lookup_netlink(struct zebra_ns *zns)
1fdc9eae 746{
d62a17ae 747 int ret;
85a75f1e
MS
748 struct zebra_dplane_info dp_info;
749 struct nlsock *netlink_cmd = &zns->netlink_cmd;
750
751 /* Capture key info from ns struct */
752 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
d62a17ae 753
754 /* Get interface information. */
85a75f1e 755 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
d62a17ae 756 if (ret < 0)
757 return ret;
85a75f1e 758 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
d62a17ae 759 1);
760 if (ret < 0)
761 return ret;
762
763 /* Get interface information - for bridge interfaces. */
85a75f1e 764 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
d62a17ae 765 RTEXT_FILTER_BRVLAN);
766 if (ret < 0)
767 return ret;
85a75f1e 768 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
d62a17ae 769 0);
770 if (ret < 0)
771 return ret;
772
773 /* Get interface information - for bridge interfaces. */
85a75f1e 774 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
d62a17ae 775 RTEXT_FILTER_BRVLAN);
776 if (ret < 0)
777 return ret;
85a75f1e 778 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
d62a17ae 779 0);
780 if (ret < 0)
781 return ret;
782
520ebf72
AK
783 /* fixup linkages */
784 zebra_if_update_all_links();
785
d62a17ae 786 /* Get IPv4 address of the interfaces. */
85a75f1e 787 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
d62a17ae 788 if (ret < 0)
789 return ret;
85a75f1e 790 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
d62a17ae 791 0, 1);
792 if (ret < 0)
793 return ret;
794
795 /* Get IPv6 address of the interfaces. */
85a75f1e 796 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
d62a17ae 797 if (ret < 0)
798 return ret;
85a75f1e 799 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
d62a17ae 800 0, 1);
801 if (ret < 0)
802 return ret;
803
804 return 0;
1fdc9eae 805}
806
e0ae31b8
DS
807int kernel_interface_set_master(struct interface *master,
808 struct interface *slave)
809{
810 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
811
812 struct {
813 struct nlmsghdr n;
814 struct ifinfomsg ifa;
815 char buf[NL_PKT_BUF_SIZE];
816 } req;
817
818 memset(&req, 0, sizeof req);
819
820 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
821 req.n.nlmsg_flags = NLM_F_REQUEST;
822 req.n.nlmsg_type = RTM_SETLINK;
823 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
824
825 req.ifa.ifi_index = slave->ifindex;
826
827 addattr_l(&req.n, sizeof req, IFLA_MASTER, &master->ifindex, 4);
828 addattr_l(&req.n, sizeof req, IFLA_LINK, &slave->ifindex, 4);
829
830 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
831 0);
832}
833
1fdc9eae 834/* Interface address modification. */
d62a17ae 835static int netlink_address(int cmd, int family, struct interface *ifp,
836 struct connected *ifc)
1fdc9eae 837{
d62a17ae 838 int bytelen;
839 struct prefix *p;
1fdc9eae 840
d62a17ae 841 struct {
842 struct nlmsghdr n;
843 struct ifaddrmsg ifa;
844 char buf[NL_PKT_BUF_SIZE];
845 } req;
1fdc9eae 846
fe533c56 847 struct zebra_ns *zns;
1fdc9eae 848
fe533c56
PG
849 if (vrf_is_backend_netns())
850 zns = zebra_ns_lookup((ns_id_t)ifp->vrf_id);
851 else
852 zns = zebra_ns_lookup(NS_DEFAULT);
d62a17ae 853 p = ifc->address;
854 memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE);
1fdc9eae 855
d62a17ae 856 bytelen = (family == AF_INET ? 4 : 16);
1fdc9eae 857
d62a17ae 858 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
859 req.n.nlmsg_flags = NLM_F_REQUEST;
860 req.n.nlmsg_type = cmd;
861 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
a55ba23f 862
d62a17ae 863 req.ifa.ifa_family = family;
1fdc9eae 864
d62a17ae 865 req.ifa.ifa_index = ifp->ifindex;
1fdc9eae 866
d62a17ae 867 addattr_l(&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1fdc9eae 868
e8d19a05
DL
869 if (family == AF_INET) {
870 if (CONNECTED_PEER(ifc)) {
871 p = ifc->destination;
60466a63
QY
872 addattr_l(&req.n, sizeof req, IFA_ADDRESS, &p->u.prefix,
873 bytelen);
e8d19a05 874 } else if (cmd == RTM_NEWADDR && ifc->destination) {
d62a17ae 875 p = ifc->destination;
876 addattr_l(&req.n, sizeof req, IFA_BROADCAST,
877 &p->u.prefix, bytelen);
878 }
879 }
1fdc9eae 880
e8d19a05
DL
881 /* p is now either ifc->address or ifc->destination */
882 req.ifa.ifa_prefixlen = p->prefixlen;
883
d62a17ae 884 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
885 SET_FLAG(req.ifa.ifa_flags, IFA_F_SECONDARY);
1fdc9eae 886
d62a17ae 887 if (ifc->label)
888 addattr_l(&req.n, sizeof req, IFA_LABEL, ifc->label,
889 strlen(ifc->label) + 1);
1fdc9eae 890
d62a17ae 891 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
892 0);
1fdc9eae 893}
894
d62a17ae 895int kernel_address_add_ipv4(struct interface *ifp, struct connected *ifc)
1fdc9eae 896{
d62a17ae 897 return netlink_address(RTM_NEWADDR, AF_INET, ifp, ifc);
1fdc9eae 898}
899
d62a17ae 900int kernel_address_delete_ipv4(struct interface *ifp, struct connected *ifc)
1fdc9eae 901{
d62a17ae 902 return netlink_address(RTM_DELADDR, AF_INET, ifp, ifc);
1fdc9eae 903}
904
996c9314 905int kernel_address_add_ipv6(struct interface *ifp, struct connected *ifc)
e86b71f1 906{
996c9314 907 return netlink_address(RTM_NEWADDR, AF_INET6, ifp, ifc);
e86b71f1
PG
908}
909
996c9314 910int kernel_address_delete_ipv6(struct interface *ifp, struct connected *ifc)
e86b71f1 911{
996c9314 912 return netlink_address(RTM_DELADDR, AF_INET6, ifp, ifc);
e86b71f1
PG
913}
914
2414abd3 915int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 916{
d62a17ae 917 int len;
918 struct ifaddrmsg *ifa;
919 struct rtattr *tb[IFA_MAX + 1];
920 struct interface *ifp;
921 void *addr;
922 void *broad;
d7c0a89a 923 uint8_t flags = 0;
d62a17ae 924 char *label = NULL;
925 struct zebra_ns *zns;
926
927 zns = zebra_ns_lookup(ns_id);
928 ifa = NLMSG_DATA(h);
929
8a1b681c 930 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
9df414fe 931 flog_warn(
e914ccbe 932 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
933 "Invalid address family: %u received from kernel interface addr change: %s",
934 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 935 return 0;
8a1b681c 936 }
d62a17ae 937
938 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
939 return 0;
940
941 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
9bdf8618
DS
942 if (len < 0) {
943 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
944 __PRETTY_FUNCTION__,
945 h->nlmsg_len,
946 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
d62a17ae 947 return -1;
9bdf8618 948 }
d62a17ae 949
950 memset(tb, 0, sizeof tb);
951 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
952
953 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
954 if (ifp == NULL) {
af4c2728 955 flog_err(
450971aa 956 EC_LIB_INTERFACE,
d62a17ae 957 "netlink_interface_addr can't find interface by index %d",
958 ifa->ifa_index);
959 return -1;
960 }
961
962 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
963 {
964 char buf[BUFSIZ];
965 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
966 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
967 ifa->ifa_flags);
968 if (tb[IFA_LOCAL])
969 zlog_debug(" IFA_LOCAL %s/%d",
970 inet_ntop(ifa->ifa_family,
971 RTA_DATA(tb[IFA_LOCAL]), buf,
972 BUFSIZ),
973 ifa->ifa_prefixlen);
974 if (tb[IFA_ADDRESS])
975 zlog_debug(" IFA_ADDRESS %s/%d",
976 inet_ntop(ifa->ifa_family,
977 RTA_DATA(tb[IFA_ADDRESS]), buf,
978 BUFSIZ),
979 ifa->ifa_prefixlen);
980 if (tb[IFA_BROADCAST])
981 zlog_debug(" IFA_BROADCAST %s/%d",
982 inet_ntop(ifa->ifa_family,
983 RTA_DATA(tb[IFA_BROADCAST]), buf,
984 BUFSIZ),
985 ifa->ifa_prefixlen);
986 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
987 zlog_debug(" IFA_LABEL %s",
988 (char *)RTA_DATA(tb[IFA_LABEL]));
989
990 if (tb[IFA_CACHEINFO]) {
991 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
992 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
993 ci->ifa_prefered, ci->ifa_valid);
994 }
995 }
996
997 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
998 if (tb[IFA_LOCAL] == NULL)
999 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1000 if (tb[IFA_ADDRESS] == NULL)
1001 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1002
1003 /* local interface address */
1004 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1005
1006 /* is there a peer address? */
1007 if (tb[IFA_ADDRESS]
1008 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1009 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1010 broad = RTA_DATA(tb[IFA_ADDRESS]);
1011 SET_FLAG(flags, ZEBRA_IFA_PEER);
1012 } else
1013 /* seeking a broadcast address */
1014 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1015 : NULL);
1016
1017 /* addr is primary key, SOL if we don't have one */
1018 if (addr == NULL) {
1019 zlog_debug("%s: NULL address", __func__);
1020 return -1;
1021 }
1022
1023 /* Flags. */
1024 if (ifa->ifa_flags & IFA_F_SECONDARY)
1025 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1026
1027 /* Label */
1028 if (tb[IFA_LABEL])
1029 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1030
2e1cc436 1031 if (label && strcmp(ifp->name, label) == 0)
d62a17ae 1032 label = NULL;
1033
1034 /* Register interface address to the interface. */
1035 if (ifa->ifa_family == AF_INET) {
930571d2 1036 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
e17d9b2d 1037 zlog_err(
87b5d1b0
DS
1038 "Invalid prefix length: %u received from kernel interface addr change: %s",
1039 ifa->ifa_prefixlen,
1040 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1041 return -1;
930571d2 1042 }
d62a17ae 1043 if (h->nlmsg_type == RTM_NEWADDR)
1044 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1045 ifa->ifa_prefixlen,
1046 (struct in_addr *)broad, label);
1047 else
1048 connected_delete_ipv4(
1049 ifp, flags, (struct in_addr *)addr,
1050 ifa->ifa_prefixlen, (struct in_addr *)broad);
1051 }
1052 if (ifa->ifa_family == AF_INET6) {
930571d2 1053 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
e17d9b2d 1054 zlog_err(
87b5d1b0
DS
1055 "Invalid prefix length: %u received from kernel interface addr change: %s",
1056 ifa->ifa_prefixlen,
1057 nl_msg_type_to_str(h->nlmsg_type));
e17d9b2d 1058 return -1;
930571d2 1059 }
d62a17ae 1060 if (h->nlmsg_type == RTM_NEWADDR) {
1061 /* Only consider valid addresses; we'll not get a
1062 * notification from
1063 * the kernel till IPv6 DAD has completed, but at init
1064 * time, Quagga
1065 * does query for and will receive all addresses.
1066 */
1067 if (!(ifa->ifa_flags
1068 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
60466a63
QY
1069 connected_add_ipv6(ifp, flags,
1070 (struct in6_addr *)addr,
60c0687a 1071 (struct in6_addr *)broad,
60466a63 1072 ifa->ifa_prefixlen, label);
d62a17ae 1073 } else
1074 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
60c0687a 1075 (struct in6_addr *)broad,
608105a7 1076 ifa->ifa_prefixlen);
d62a17ae 1077 }
1078
1079 return 0;
1fdc9eae 1080}
1081
2414abd3 1082int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1083{
d62a17ae 1084 int len;
1085 struct ifinfomsg *ifi;
1086 struct rtattr *tb[IFLA_MAX + 1];
1087 struct rtattr *linkinfo[IFLA_MAX + 1];
1088 struct interface *ifp;
1089 char *name = NULL;
1090 char *kind = NULL;
48884c6b 1091 char *desc = NULL;
d62a17ae 1092 char *slave_kind = NULL;
1093 struct zebra_ns *zns;
1094 vrf_id_t vrf_id = VRF_DEFAULT;
1095 zebra_iftype_t zif_type = ZEBRA_IF_OTHER;
1096 zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1097 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
b9368db9 1098 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
d62a17ae 1099 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1100
1101
1102 zns = zebra_ns_lookup(ns_id);
1103 ifi = NLMSG_DATA(h);
1104
fe533c56 1105 /* assume if not default zns, then new VRF */
d62a17ae 1106 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1107 /* If this is not link add/delete message so print warning. */
87b5d1b0
DS
1108 zlog_debug("netlink_link_change: wrong kernel message %s",
1109 nl_msg_type_to_str(h->nlmsg_type));
d62a17ae 1110 return 0;
1111 }
1112
8a1b681c
SW
1113 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1114 || ifi->ifi_family == AF_INET6)) {
9df414fe 1115 flog_warn(
e914ccbe 1116 EC_ZEBRA_UNKNOWN_FAMILY,
87b5d1b0
DS
1117 "Invalid address family: %u received from kernel link change: %s",
1118 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
8a1b681c
SW
1119 return 0;
1120 }
1121
d62a17ae 1122 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618
DS
1123 if (len < 0) {
1124 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
1125 __PRETTY_FUNCTION__, h->nlmsg_len,
1126 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 1127 return -1;
9bdf8618 1128 }
d62a17ae 1129
1130 /* We are interested in some AF_BRIDGE notifications. */
1131 if (ifi->ifi_family == AF_BRIDGE)
1132 return netlink_bridge_interface(h, len, ns_id, startup);
1133
1134 /* Looking up interface name. */
1135 memset(tb, 0, sizeof tb);
1136 memset(linkinfo, 0, sizeof linkinfo);
1137 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1fdc9eae 1138
d62a17ae 1139 /* check for wireless messages to ignore */
1140 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1141 if (IS_ZEBRA_DEBUG_KERNEL)
1142 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1143 __func__);
1144 return 0;
1145 }
1fdc9eae 1146
d62a17ae 1147 if (tb[IFLA_IFNAME] == NULL)
1148 return -1;
1149 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1150
d62a17ae 1151 if (tb[IFLA_LINKINFO]) {
1152 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1fdc9eae 1153
d62a17ae 1154 if (linkinfo[IFLA_INFO_KIND])
1155 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1156
d62a17ae 1157 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1158 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1159
d62a17ae 1160 netlink_determine_zebra_iftype(kind, &zif_type);
1161 }
6675513d 1162
d62a17ae 1163 /* If linking to another interface, note it. */
1164 if (tb[IFLA_LINK])
1165 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1fdc9eae 1166
48884c6b
DS
1167 if (tb[IFLA_IFALIAS]) {
1168 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1169 }
1170
d62a17ae 1171 /* If VRF, create or update the VRF structure itself. */
78dd30b2 1172 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
d62a17ae 1173 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
1174 vrf_id = (vrf_id_t)ifi->ifi_index;
1175 }
1fdc9eae 1176
d62a17ae 1177 /* See if interface is present. */
1178 ifp = if_lookup_by_name_per_ns(zns, name);
1179
48884c6b
DS
1180 if (ifp) {
1181 if (ifp->desc)
1182 XFREE(MTYPE_TMP, ifp->desc);
1183 if (desc)
1184 ifp->desc = XSTRDUP(MTYPE_TMP, desc);
1185 }
1186
d62a17ae 1187 if (h->nlmsg_type == RTM_NEWLINK) {
1188 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1189 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1190 && !vrf_is_backend_netns()) {
d62a17ae 1191 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 1192 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1193 } else if (slave_kind
1194 && (strcmp(slave_kind, "bridge") == 0)) {
1195 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1196 bridge_ifindex =
1197 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
b9368db9
DD
1198 } else if (slave_kind
1199 && (strcmp(slave_kind, "bond") == 0)) {
1200 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1201 bond_ifindex =
1202 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1203 } else
1204 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1205 }
fe533c56
PG
1206 if (vrf_is_backend_netns())
1207 vrf_id = (vrf_id_t)ns_id;
d62a17ae 1208 if (ifp == NULL
1209 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1210 /* Add interface notification from kernel */
1211 if (IS_ZEBRA_DEBUG_KERNEL)
1212 zlog_debug(
1213 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1214 "sl_type %d master %u flags 0x%x",
1215 name, ifi->ifi_index, vrf_id, zif_type,
1216 zif_slave_type, bridge_ifindex,
1217 ifi->ifi_flags);
1218
1219 if (ifp == NULL) {
1220 /* unknown interface */
8f90d89b 1221 ifp = if_get_by_name(name, vrf_id);
d62a17ae 1222 } else {
1223 /* pre-configured interface, learnt now */
1224 if (ifp->vrf_id != vrf_id)
1225 if_update_to_new_vrf(ifp, vrf_id);
1226 }
1227
1228 /* Update interface information. */
1229 set_ifindex(ifp, ifi->ifi_index, zns);
1230 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d23b983b 1231 if (!tb[IFLA_MTU]) {
9df414fe 1232 zlog_debug(
d23b983b
SW
1233 "RTM_NEWLINK for interface %s(%u) without MTU set",
1234 name, ifi->ifi_index);
1235 return 0;
1236 }
d62a17ae 1237 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1238 ifp->metric = 0;
1239 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1240
1241 /* Set interface type */
1242 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1243 if (IS_ZEBRA_IF_VRF(ifp))
1244 SET_FLAG(ifp->status,
1245 ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1246
1247 /* Update link. */
680c278f 1248 zebra_if_update_link(ifp, link_ifindex, ns_id);
d62a17ae 1249
1250 netlink_interface_update_hw_addr(tb, ifp);
1251
1252 /* Inform clients, install any configured addresses. */
1253 if_add_update(ifp);
1254
1255 /* Extract and save L2 interface information, take
1256 * additional actions. */
1257 netlink_interface_update_l2info(
1258 ifp, linkinfo[IFLA_INFO_DATA], 1);
1259 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1260 zebra_l2if_update_bridge_slave(ifp,
1261 bridge_ifindex);
b9368db9
DD
1262 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1263 zebra_l2if_update_bond_slave(ifp, bond_ifindex);
d62a17ae 1264 } else if (ifp->vrf_id != vrf_id) {
1265 /* VRF change for an interface. */
1266 if (IS_ZEBRA_DEBUG_KERNEL)
1267 zlog_debug(
1268 "RTM_NEWLINK vrf-change for %s(%u) "
1269 "vrf_id %u -> %u flags 0x%x",
1270 name, ifp->ifindex, ifp->vrf_id, vrf_id,
1271 ifi->ifi_flags);
1272
1273 if_handle_vrf_change(ifp, vrf_id);
1274 } else {
b9368db9 1275 bool was_bridge_slave, was_bond_slave;
d62a17ae 1276
1277 /* Interface update. */
1278 if (IS_ZEBRA_DEBUG_KERNEL)
1279 zlog_debug(
1280 "RTM_NEWLINK update for %s(%u) "
1281 "sl_type %d master %u flags 0x%x",
1282 name, ifp->ifindex, zif_slave_type,
1283 bridge_ifindex, ifi->ifi_flags);
1284
1285 set_ifindex(ifp, ifi->ifi_index, zns);
d23b983b 1286 if (!tb[IFLA_MTU]) {
9df414fe 1287 zlog_debug(
d23b983b
SW
1288 "RTM_NEWLINK for interface %s(%u) without MTU set",
1289 name, ifi->ifi_index);
1290 return 0;
1291 }
d62a17ae 1292 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1293 ifp->metric = 0;
1294
1295 /* Update interface type - NOTE: Only slave_type can
1296 * change. */
1297 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
b9368db9 1298 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
d62a17ae 1299 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1300
1301 netlink_interface_update_hw_addr(tb, ifp);
1302
1303 if (if_is_no_ptm_operative(ifp)) {
1304 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1305 if (!if_is_no_ptm_operative(ifp)) {
1306 if (IS_ZEBRA_DEBUG_KERNEL)
1307 zlog_debug(
1308 "Intf %s(%u) has gone DOWN",
1309 name, ifp->ifindex);
1310 if_down(ifp);
1311 } else if (if_is_operative(ifp)) {
1312 /* Must notify client daemons of new
1313 * interface status. */
1314 if (IS_ZEBRA_DEBUG_KERNEL)
1315 zlog_debug(
1316 "Intf %s(%u) PTM up, notifying clients",
1317 name, ifp->ifindex);
1318 zebra_interface_up_update(ifp);
1319 }
1320 } else {
1321 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1322 if (if_is_operative(ifp)) {
1323 if (IS_ZEBRA_DEBUG_KERNEL)
1324 zlog_debug(
1325 "Intf %s(%u) has come UP",
1326 name, ifp->ifindex);
1327 if_up(ifp);
1328 }
1329 }
1330
1331 /* Extract and save L2 interface information, take
1332 * additional actions. */
1333 netlink_interface_update_l2info(
1334 ifp, linkinfo[IFLA_INFO_DATA], 0);
1335 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
1336 zebra_l2if_update_bridge_slave(ifp,
1337 bridge_ifindex);
b9368db9
DD
1338 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
1339 zebra_l2if_update_bond_slave(ifp, bond_ifindex);
d62a17ae 1340 }
1341 } else {
1342 /* Delete interface notification from kernel */
1343 if (ifp == NULL) {
9df414fe
QY
1344 if (IS_ZEBRA_DEBUG_KERNEL)
1345 zlog_debug(
1346 "RTM_DELLINK for unknown interface %s(%u)",
1347 name, ifi->ifi_index);
d62a17ae 1348 return 0;
1349 }
1350
1351 if (IS_ZEBRA_DEBUG_KERNEL)
1352 zlog_debug("RTM_DELLINK for %s(%u)", name,
1353 ifp->ifindex);
1354
1355 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
1356
1357 /* Special handling for bridge or VxLAN interfaces. */
1358 if (IS_ZEBRA_IF_BRIDGE(ifp))
1359 zebra_l2_bridge_del(ifp);
1360 else if (IS_ZEBRA_IF_VXLAN(ifp))
1361 zebra_l2_vxlanif_del(ifp);
1362
1363 if (!IS_ZEBRA_IF_VRF(ifp))
1364 if_delete_update(ifp);
1fdc9eae 1365 }
1366
d62a17ae 1367 return 0;
1fdc9eae 1368}
718e3744 1369
1370/* Interface information read by netlink. */
d62a17ae 1371void interface_list(struct zebra_ns *zns)
718e3744 1372{
d62a17ae 1373 interface_lookup_netlink(zns);
718e3744 1374}
ddfeb486
DL
1375
1376#endif /* GNU_LINUX */