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