]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_netlink.c
zebra: fix warnings from frr_elevate_privs()
[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 */
01b9e3fd
DL
389 rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL, (char *)&ifdata);
390 }
d62a17ae 391 if (rc < 0) {
bd7d0299
DS
392 if (IS_ZEBRA_DEBUG_KERNEL)
393 zlog_debug(
394 "IOCTL failure to read interface %s speed: %d %s",
395 ifname, errno, safe_strerror(errno));
d62a17ae 396 ecmd.speed_hi = 0;
397 ecmd.speed = 0;
398 }
399
400 close(sd);
401
402 return (ecmd.speed_hi << 16) | ecmd.speed;
535fe877
DS
403}
404
dc7b3cae
DS
405uint32_t kernel_get_speed(struct interface *ifp)
406{
0268f30e 407 return get_iflink_speed(ifp);
dc7b3cae
DS
408}
409
d62a17ae 410static int netlink_extract_bridge_info(struct rtattr *link_data,
411 struct zebra_l2info_bridge *bridge_info)
6675513d 412{
d62a17ae 413 struct rtattr *attr[IFLA_BR_MAX + 1];
414
415 memset(bridge_info, 0, sizeof(*bridge_info));
416 memset(attr, 0, sizeof attr);
417 parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
418 if (attr[IFLA_BR_VLAN_FILTERING])
419 bridge_info->vlan_aware =
d7c0a89a 420 *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
d62a17ae 421 return 0;
6675513d 422}
423
d62a17ae 424static int netlink_extract_vlan_info(struct rtattr *link_data,
425 struct zebra_l2info_vlan *vlan_info)
6675513d 426{
d62a17ae 427 struct rtattr *attr[IFLA_VLAN_MAX + 1];
428 vlanid_t vid_in_msg;
429
430 memset(vlan_info, 0, sizeof(*vlan_info));
431 memset(attr, 0, sizeof attr);
432 parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
433 if (!attr[IFLA_VLAN_ID]) {
434 if (IS_ZEBRA_DEBUG_KERNEL)
435 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
436 return -1;
437 }
438
439 vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
440 vlan_info->vid = vid_in_msg;
441 return 0;
6675513d 442}
443
d62a17ae 444static int netlink_extract_vxlan_info(struct rtattr *link_data,
445 struct zebra_l2info_vxlan *vxl_info)
6675513d 446{
d62a17ae 447 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
448 vni_t vni_in_msg;
449 struct in_addr vtep_ip_in_msg;
450
451 memset(vxl_info, 0, sizeof(*vxl_info));
452 memset(attr, 0, sizeof attr);
453 parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
454 if (!attr[IFLA_VXLAN_ID]) {
455 if (IS_ZEBRA_DEBUG_KERNEL)
456 zlog_debug(
457 "IFLA_VXLAN_ID missing from VXLAN IF message");
458 return -1;
459 }
460
461 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
462 vxl_info->vni = vni_in_msg;
463 if (!attr[IFLA_VXLAN_LOCAL]) {
464 if (IS_ZEBRA_DEBUG_KERNEL)
465 zlog_debug(
466 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
467 } else {
468 vtep_ip_in_msg =
469 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
470 vxl_info->vtep_ip = vtep_ip_in_msg;
471 }
472
473 return 0;
6675513d 474}
475
476/*
477 * Extract and save L2 params (of interest) for an interface. When a
478 * bridge interface is added or updated, take further actions to map
479 * its members. Likewise, for VxLAN interface.
480 */
d62a17ae 481static void netlink_interface_update_l2info(struct interface *ifp,
482 struct rtattr *link_data, int add)
6675513d 483{
d62a17ae 484 if (!link_data)
485 return;
486
487 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
488 struct zebra_l2info_bridge bridge_info;
489
490 netlink_extract_bridge_info(link_data, &bridge_info);
491 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
492 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
493 struct zebra_l2info_vlan vlan_info;
494
495 netlink_extract_vlan_info(link_data, &vlan_info);
496 zebra_l2_vlanif_update(ifp, &vlan_info);
497 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
498 struct zebra_l2info_vxlan vxlan_info;
499
500 netlink_extract_vxlan_info(link_data, &vxlan_info);
501 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
502 }
6675513d 503}
504
d62a17ae 505static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
506 int startup)
6675513d 507{
d62a17ae 508 char *name = NULL;
509 struct ifinfomsg *ifi;
510 struct rtattr *tb[IFLA_MAX + 1];
511 struct interface *ifp;
512 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
513 struct {
d7c0a89a
QY
514 uint16_t flags;
515 uint16_t vid;
d62a17ae 516 } * vinfo;
517 vlanid_t access_vlan;
518
519 /* Fetch name and ifindex */
520 ifi = NLMSG_DATA(h);
521 memset(tb, 0, sizeof tb);
522 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
523
524 if (tb[IFLA_IFNAME] == NULL)
525 return -1;
526 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
527
528 /* The interface should already be known, if not discard. */
529 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
530 if (!ifp) {
531 zlog_warn("Cannot find bridge IF %s(%u)", name, ifi->ifi_index);
532 return 0;
533 }
534 if (!IS_ZEBRA_IF_VXLAN(ifp))
535 return 0;
536
537 /* We are only interested in the access VLAN i.e., AF_SPEC */
538 if (!tb[IFLA_AF_SPEC])
539 return 0;
540
541 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
542 * only 1 access VLAN is accepted.
543 */
544 memset(aftb, 0, sizeof aftb);
545 parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]);
546 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
547 return 0;
548
549 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
550 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
551 return 0;
552
553 access_vlan = (vlanid_t)vinfo->vid;
554 if (IS_ZEBRA_DEBUG_KERNEL)
555 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
556 name, ifi->ifi_index);
557 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
558 return 0;
6675513d 559}
560
2414abd3
DS
561/*
562 * Called from interface_lookup_netlink(). This function is only used
563 * during bootstrap.
564 */
565static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 566{
d62a17ae 567 int len;
568 struct ifinfomsg *ifi;
569 struct rtattr *tb[IFLA_MAX + 1];
570 struct rtattr *linkinfo[IFLA_MAX + 1];
571 struct interface *ifp;
572 char *name = NULL;
573 char *kind = NULL;
48884c6b 574 char *desc = NULL;
d62a17ae 575 char *slave_kind = NULL;
576 struct zebra_ns *zns;
577 vrf_id_t vrf_id = VRF_DEFAULT;
578 zebra_iftype_t zif_type = ZEBRA_IF_OTHER;
579 zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE;
580 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
581 ifindex_t link_ifindex = IFINDEX_INTERNAL;
582
583 zns = zebra_ns_lookup(ns_id);
584 ifi = NLMSG_DATA(h);
585
586 if (h->nlmsg_type != RTM_NEWLINK)
587 return 0;
588
589 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618
DS
590 if (len < 0) {
591 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
592 __PRETTY_FUNCTION__,
593 h->nlmsg_len,
594 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 595 return -1;
9bdf8618 596 }
d62a17ae 597
598 /* We are interested in some AF_BRIDGE notifications. */
599 if (ifi->ifi_family == AF_BRIDGE)
600 return netlink_bridge_interface(h, len, ns_id, startup);
601
602 /* Looking up interface name. */
603 memset(tb, 0, sizeof tb);
604 memset(linkinfo, 0, sizeof linkinfo);
605 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1fdc9eae 606
d62a17ae 607 /* check for wireless messages to ignore */
608 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
609 if (IS_ZEBRA_DEBUG_KERNEL)
610 zlog_debug("%s: ignoring IFLA_WIRELESS message",
611 __func__);
612 return 0;
613 }
1fdc9eae 614
d62a17ae 615 if (tb[IFLA_IFNAME] == NULL)
616 return -1;
617 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 618
48884c6b
DS
619 if (tb[IFLA_IFALIAS])
620 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
621
d62a17ae 622 if (tb[IFLA_LINKINFO]) {
623 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1fdc9eae 624
d62a17ae 625 if (linkinfo[IFLA_INFO_KIND])
626 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 627
d62a17ae 628 if (linkinfo[IFLA_INFO_SLAVE_KIND])
629 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 630
d62a17ae 631 netlink_determine_zebra_iftype(kind, &zif_type);
632 }
633
634 /* If VRF, create the VRF structure itself. */
78dd30b2 635 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
d62a17ae 636 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
637 vrf_id = (vrf_id_t)ifi->ifi_index;
638 }
639
640 if (tb[IFLA_MASTER]) {
78dd30b2
PG
641 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
642 && !vrf_is_backend_netns()) {
d62a17ae 643 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 644 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 645 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
646 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
647 bridge_ifindex =
648 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
649 } else
650 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
651 }
78dd30b2
PG
652 if (vrf_is_backend_netns())
653 vrf_id = (vrf_id_t)ns_id;
d62a17ae 654
655 /* If linking to another interface, note it. */
656 if (tb[IFLA_LINK])
657 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
658
659 /* Add interface. */
bcc24579 660 ifp = if_get_by_name(name, vrf_id, 0);
d62a17ae 661 set_ifindex(ifp, ifi->ifi_index, zns);
662 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d62a17ae 663 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
664 ifp->metric = 0;
0268f30e 665 ifp->speed = get_iflink_speed(ifp);
d62a17ae 666 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
667
48884c6b
DS
668 if (desc)
669 ifp->desc = XSTRDUP(MTYPE_TMP, desc);
670
d62a17ae 671 /* Set zebra interface type */
672 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
673 if (IS_ZEBRA_IF_VRF(ifp))
674 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 675
676 /* Update link. */
677 zebra_if_update_link(ifp, link_ifindex);
678
679 /* Hardware type and address. */
680 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
681 netlink_interface_update_hw_addr(tb, ifp);
682
683 if_add_update(ifp);
684
685 /* Extract and save L2 interface information, take additional actions.
686 */
687 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA], 1);
688 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
689 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex);
690
691 return 0;
1fdc9eae 692}
693
289602d7 694/* Request for specific interface or address information from the kernel */
d62a17ae 695static int netlink_request_intf_addr(struct zebra_ns *zns, int family, int type,
d7c0a89a 696 uint32_t filter_mask)
289602d7 697{
d62a17ae 698 struct {
699 struct nlmsghdr n;
700 struct ifinfomsg ifm;
701 char buf[256];
702 } req;
703
704 /* Form the request, specifying filter (rtattr) if needed. */
705 memset(&req, 0, sizeof(req));
706 req.n.nlmsg_type = type;
707 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
708 req.ifm.ifi_family = family;
709
710 /* Include filter, if specified. */
711 if (filter_mask)
712 addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
713
714 return netlink_request(&zns->netlink_cmd, &req.n);
289602d7 715}
716
1fdc9eae 717/* Interface lookup by netlink socket. */
d62a17ae 718int interface_lookup_netlink(struct zebra_ns *zns)
1fdc9eae 719{
d62a17ae 720 int ret;
721
722 /* Get interface information. */
723 ret = netlink_request_intf_addr(zns, AF_PACKET, RTM_GETLINK, 0);
724 if (ret < 0)
725 return ret;
726 ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0,
727 1);
728 if (ret < 0)
729 return ret;
730
731 /* Get interface information - for bridge interfaces. */
732 ret = netlink_request_intf_addr(zns, AF_BRIDGE, RTM_GETLINK,
733 RTEXT_FILTER_BRVLAN);
734 if (ret < 0)
735 return ret;
736 ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0,
737 0);
738 if (ret < 0)
739 return ret;
740
741 /* Get interface information - for bridge interfaces. */
742 ret = netlink_request_intf_addr(zns, AF_BRIDGE, RTM_GETLINK,
743 RTEXT_FILTER_BRVLAN);
744 if (ret < 0)
745 return ret;
746 ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0,
747 0);
748 if (ret < 0)
749 return ret;
750
751 /* Get IPv4 address of the interfaces. */
752 ret = netlink_request_intf_addr(zns, AF_INET, RTM_GETADDR, 0);
753 if (ret < 0)
754 return ret;
755 ret = netlink_parse_info(netlink_interface_addr, &zns->netlink_cmd, zns,
756 0, 1);
757 if (ret < 0)
758 return ret;
759
760 /* Get IPv6 address of the interfaces. */
761 ret = netlink_request_intf_addr(zns, AF_INET6, RTM_GETADDR, 0);
762 if (ret < 0)
763 return ret;
764 ret = netlink_parse_info(netlink_interface_addr, &zns->netlink_cmd, zns,
765 0, 1);
766 if (ret < 0)
767 return ret;
768
769 return 0;
1fdc9eae 770}
771
e0ae31b8
DS
772int kernel_interface_set_master(struct interface *master,
773 struct interface *slave)
774{
775 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
776
777 struct {
778 struct nlmsghdr n;
779 struct ifinfomsg ifa;
780 char buf[NL_PKT_BUF_SIZE];
781 } req;
782
783 memset(&req, 0, sizeof req);
784
785 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
786 req.n.nlmsg_flags = NLM_F_REQUEST;
787 req.n.nlmsg_type = RTM_SETLINK;
788 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
789
790 req.ifa.ifi_index = slave->ifindex;
791
792 addattr_l(&req.n, sizeof req, IFLA_MASTER, &master->ifindex, 4);
793 addattr_l(&req.n, sizeof req, IFLA_LINK, &slave->ifindex, 4);
794
795 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
796 0);
797}
798
1fdc9eae 799/* Interface address modification. */
d62a17ae 800static int netlink_address(int cmd, int family, struct interface *ifp,
801 struct connected *ifc)
1fdc9eae 802{
d62a17ae 803 int bytelen;
804 struct prefix *p;
1fdc9eae 805
d62a17ae 806 struct {
807 struct nlmsghdr n;
808 struct ifaddrmsg ifa;
809 char buf[NL_PKT_BUF_SIZE];
810 } req;
1fdc9eae 811
fe533c56 812 struct zebra_ns *zns;
1fdc9eae 813
fe533c56
PG
814 if (vrf_is_backend_netns())
815 zns = zebra_ns_lookup((ns_id_t)ifp->vrf_id);
816 else
817 zns = zebra_ns_lookup(NS_DEFAULT);
d62a17ae 818 p = ifc->address;
819 memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE);
1fdc9eae 820
d62a17ae 821 bytelen = (family == AF_INET ? 4 : 16);
1fdc9eae 822
d62a17ae 823 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
824 req.n.nlmsg_flags = NLM_F_REQUEST;
825 req.n.nlmsg_type = cmd;
826 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
a55ba23f 827
d62a17ae 828 req.ifa.ifa_family = family;
1fdc9eae 829
d62a17ae 830 req.ifa.ifa_index = ifp->ifindex;
1fdc9eae 831
d62a17ae 832 addattr_l(&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
1fdc9eae 833
e8d19a05
DL
834 if (family == AF_INET) {
835 if (CONNECTED_PEER(ifc)) {
836 p = ifc->destination;
60466a63
QY
837 addattr_l(&req.n, sizeof req, IFA_ADDRESS, &p->u.prefix,
838 bytelen);
e8d19a05 839 } else if (cmd == RTM_NEWADDR && ifc->destination) {
d62a17ae 840 p = ifc->destination;
841 addattr_l(&req.n, sizeof req, IFA_BROADCAST,
842 &p->u.prefix, bytelen);
843 }
844 }
1fdc9eae 845
e8d19a05
DL
846 /* p is now either ifc->address or ifc->destination */
847 req.ifa.ifa_prefixlen = p->prefixlen;
848
d62a17ae 849 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
850 SET_FLAG(req.ifa.ifa_flags, IFA_F_SECONDARY);
1fdc9eae 851
d62a17ae 852 if (ifc->label)
853 addattr_l(&req.n, sizeof req, IFA_LABEL, ifc->label,
854 strlen(ifc->label) + 1);
1fdc9eae 855
d62a17ae 856 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
857 0);
1fdc9eae 858}
859
d62a17ae 860int kernel_address_add_ipv4(struct interface *ifp, struct connected *ifc)
1fdc9eae 861{
d62a17ae 862 return netlink_address(RTM_NEWADDR, AF_INET, ifp, ifc);
1fdc9eae 863}
864
d62a17ae 865int kernel_address_delete_ipv4(struct interface *ifp, struct connected *ifc)
1fdc9eae 866{
d62a17ae 867 return netlink_address(RTM_DELADDR, AF_INET, ifp, ifc);
1fdc9eae 868}
869
996c9314 870int kernel_address_add_ipv6(struct interface *ifp, struct connected *ifc)
e86b71f1 871{
996c9314 872 return netlink_address(RTM_NEWADDR, AF_INET6, ifp, ifc);
e86b71f1
PG
873}
874
996c9314 875int kernel_address_delete_ipv6(struct interface *ifp, struct connected *ifc)
e86b71f1 876{
996c9314 877 return netlink_address(RTM_DELADDR, AF_INET6, ifp, ifc);
e86b71f1
PG
878}
879
2414abd3 880int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 881{
d62a17ae 882 int len;
883 struct ifaddrmsg *ifa;
884 struct rtattr *tb[IFA_MAX + 1];
885 struct interface *ifp;
886 void *addr;
887 void *broad;
d7c0a89a 888 uint8_t flags = 0;
d62a17ae 889 char *label = NULL;
890 struct zebra_ns *zns;
891
892 zns = zebra_ns_lookup(ns_id);
893 ifa = NLMSG_DATA(h);
894
8a1b681c
SW
895 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
896 zlog_warn(
81227874 897 "Invalid address family: %u received from kernel interface addr change: %u",
8a1b681c 898 ifa->ifa_family, h->nlmsg_type);
d62a17ae 899 return 0;
8a1b681c 900 }
d62a17ae 901
902 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
903 return 0;
904
905 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
9bdf8618
DS
906 if (len < 0) {
907 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
908 __PRETTY_FUNCTION__,
909 h->nlmsg_len,
910 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
d62a17ae 911 return -1;
9bdf8618 912 }
d62a17ae 913
914 memset(tb, 0, sizeof tb);
915 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
916
917 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
918 if (ifp == NULL) {
af4c2728 919 flog_err(
4d43f68a 920 LIB_ERR_INTERFACE,
d62a17ae 921 "netlink_interface_addr can't find interface by index %d",
922 ifa->ifa_index);
923 return -1;
924 }
925
926 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
927 {
928 char buf[BUFSIZ];
929 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
930 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
931 ifa->ifa_flags);
932 if (tb[IFA_LOCAL])
933 zlog_debug(" IFA_LOCAL %s/%d",
934 inet_ntop(ifa->ifa_family,
935 RTA_DATA(tb[IFA_LOCAL]), buf,
936 BUFSIZ),
937 ifa->ifa_prefixlen);
938 if (tb[IFA_ADDRESS])
939 zlog_debug(" IFA_ADDRESS %s/%d",
940 inet_ntop(ifa->ifa_family,
941 RTA_DATA(tb[IFA_ADDRESS]), buf,
942 BUFSIZ),
943 ifa->ifa_prefixlen);
944 if (tb[IFA_BROADCAST])
945 zlog_debug(" IFA_BROADCAST %s/%d",
946 inet_ntop(ifa->ifa_family,
947 RTA_DATA(tb[IFA_BROADCAST]), buf,
948 BUFSIZ),
949 ifa->ifa_prefixlen);
950 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
951 zlog_debug(" IFA_LABEL %s",
952 (char *)RTA_DATA(tb[IFA_LABEL]));
953
954 if (tb[IFA_CACHEINFO]) {
955 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
956 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
957 ci->ifa_prefered, ci->ifa_valid);
958 }
959 }
960
961 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
962 if (tb[IFA_LOCAL] == NULL)
963 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
964 if (tb[IFA_ADDRESS] == NULL)
965 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
966
967 /* local interface address */
968 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
969
970 /* is there a peer address? */
971 if (tb[IFA_ADDRESS]
972 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
973 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
974 broad = RTA_DATA(tb[IFA_ADDRESS]);
975 SET_FLAG(flags, ZEBRA_IFA_PEER);
976 } else
977 /* seeking a broadcast address */
978 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
979 : NULL);
980
981 /* addr is primary key, SOL if we don't have one */
982 if (addr == NULL) {
983 zlog_debug("%s: NULL address", __func__);
984 return -1;
985 }
986
987 /* Flags. */
988 if (ifa->ifa_flags & IFA_F_SECONDARY)
989 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
990
991 /* Label */
992 if (tb[IFA_LABEL])
993 label = (char *)RTA_DATA(tb[IFA_LABEL]);
994
2e1cc436 995 if (label && strcmp(ifp->name, label) == 0)
d62a17ae 996 label = NULL;
997
998 /* Register interface address to the interface. */
999 if (ifa->ifa_family == AF_INET) {
930571d2 1000 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
e17d9b2d 1001 zlog_err(
75829703 1002 "Invalid prefix length: %u received from kernel interface addr change: %u",
930571d2 1003 ifa->ifa_prefixlen, h->nlmsg_type);
e17d9b2d 1004 return -1;
930571d2 1005 }
d62a17ae 1006 if (h->nlmsg_type == RTM_NEWADDR)
1007 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1008 ifa->ifa_prefixlen,
1009 (struct in_addr *)broad, label);
1010 else
1011 connected_delete_ipv4(
1012 ifp, flags, (struct in_addr *)addr,
1013 ifa->ifa_prefixlen, (struct in_addr *)broad);
1014 }
1015 if (ifa->ifa_family == AF_INET6) {
930571d2 1016 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
e17d9b2d 1017 zlog_err(
75829703 1018 "Invalid prefix length: %u received from kernel interface addr change: %u",
930571d2 1019 ifa->ifa_prefixlen, h->nlmsg_type);
e17d9b2d 1020 return -1;
930571d2 1021 }
d62a17ae 1022 if (h->nlmsg_type == RTM_NEWADDR) {
1023 /* Only consider valid addresses; we'll not get a
1024 * notification from
1025 * the kernel till IPv6 DAD has completed, but at init
1026 * time, Quagga
1027 * does query for and will receive all addresses.
1028 */
1029 if (!(ifa->ifa_flags
1030 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
60466a63
QY
1031 connected_add_ipv6(ifp, flags,
1032 (struct in6_addr *)addr,
60c0687a 1033 (struct in6_addr *)broad,
60466a63 1034 ifa->ifa_prefixlen, label);
d62a17ae 1035 } else
1036 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
60c0687a 1037 (struct in6_addr *)broad,
608105a7 1038 ifa->ifa_prefixlen);
d62a17ae 1039 }
1040
1041 return 0;
1fdc9eae 1042}
1043
b53686c5
PG
1044/* helper function called by if_netlink_change
1045 * to delete interfaces in case the interface moved
1046 * to an other netns
1047 */
1048static void if_netlink_check_ifp_instance_consistency(uint16_t cmd,
1049 struct interface *ifp,
1050 ns_id_t ns_id)
1051{
9373219c 1052 struct interface *other_ifp;
b53686c5
PG
1053
1054 /*
1055 * look if interface name is also found on other netns
1056 * - only if vrf backend is netns
1057 * - do not concern lo interface
1058 * - then remove previous one
1059 * - for new link case, check found interface is not active
1060 */
1061 if (!vrf_is_backend_netns() ||
1062 !strcmp(ifp->name, "lo"))
1063 return;
9373219c
PG
1064 other_ifp = if_lookup_by_name_not_ns(ns_id, ifp->name);
1065 if (!other_ifp)
b53686c5 1066 return;
5efbf310
PG
1067 /* because previous interface may be inactive,
1068 * interface is moved back to default vrf
1069 * then one may find the same pointer; ignore
1070 */
1071 if (other_ifp == ifp)
1072 return;
b53686c5 1073 if ((cmd == RTM_NEWLINK)
9373219c 1074 && (CHECK_FLAG(other_ifp->status, ZEBRA_INTERFACE_ACTIVE)))
b53686c5 1075 return;
9373219c
PG
1076 if (IS_ZEBRA_DEBUG_KERNEL && cmd == RTM_NEWLINK) {
1077 zlog_debug("RTM_NEWLINK %s(%u, VRF %u) replaces %s(%u, VRF %u)\n",
1078 ifp->name,
1079 ifp->ifindex,
1080 ifp->vrf_id,
1081 other_ifp->name,
1082 other_ifp->ifindex,
1083 other_ifp->vrf_id);
1084 } else if (IS_ZEBRA_DEBUG_KERNEL && cmd == RTM_DELLINK) {
1085 zlog_debug("RTM_DELLINK %s(%u, VRF %u) is replaced by %s(%u, VRF %u)\n",
b53686c5 1086 ifp->name,
9373219c
PG
1087 ifp->ifindex,
1088 ifp->vrf_id,
1089 other_ifp->name,
1090 other_ifp->ifindex,
1091 other_ifp->vrf_id);
1092 }
b53686c5
PG
1093 /* the found interface replaces the current one
1094 * remove it
1095 */
1096 if (cmd == RTM_DELLINK)
1097 if_delete(ifp);
1098 else
9373219c 1099 if_delete(other_ifp);
b53686c5
PG
1100 /* the found interface is replaced by the current one
1101 * suppress it
1102 */
1103}
1104
2414abd3 1105int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1106{
d62a17ae 1107 int len;
1108 struct ifinfomsg *ifi;
1109 struct rtattr *tb[IFLA_MAX + 1];
1110 struct rtattr *linkinfo[IFLA_MAX + 1];
1111 struct interface *ifp;
1112 char *name = NULL;
1113 char *kind = NULL;
48884c6b 1114 char *desc = NULL;
d62a17ae 1115 char *slave_kind = NULL;
1116 struct zebra_ns *zns;
1117 vrf_id_t vrf_id = VRF_DEFAULT;
1118 zebra_iftype_t zif_type = ZEBRA_IF_OTHER;
1119 zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1120 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1121 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1122
1123
1124 zns = zebra_ns_lookup(ns_id);
1125 ifi = NLMSG_DATA(h);
1126
fe533c56 1127 /* assume if not default zns, then new VRF */
d62a17ae 1128 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1129 /* If this is not link add/delete message so print warning. */
1130 zlog_warn("netlink_link_change: wrong kernel message %d",
1131 h->nlmsg_type);
1132 return 0;
1133 }
1134
8a1b681c
SW
1135 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1136 || ifi->ifi_family == AF_INET6)) {
1137 zlog_warn(
81227874 1138 "Invalid address family: %u received from kernel link change: %u",
8a1b681c
SW
1139 ifi->ifi_family, h->nlmsg_type);
1140 return 0;
1141 }
1142
d62a17ae 1143 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618
DS
1144 if (len < 0) {
1145 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
1146 __PRETTY_FUNCTION__, h->nlmsg_len,
1147 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 1148 return -1;
9bdf8618 1149 }
d62a17ae 1150
1151 /* We are interested in some AF_BRIDGE notifications. */
1152 if (ifi->ifi_family == AF_BRIDGE)
1153 return netlink_bridge_interface(h, len, ns_id, startup);
1154
1155 /* Looking up interface name. */
1156 memset(tb, 0, sizeof tb);
1157 memset(linkinfo, 0, sizeof linkinfo);
1158 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1fdc9eae 1159
d62a17ae 1160 /* check for wireless messages to ignore */
1161 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1162 if (IS_ZEBRA_DEBUG_KERNEL)
1163 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1164 __func__);
1165 return 0;
1166 }
1fdc9eae 1167
d62a17ae 1168 if (tb[IFLA_IFNAME] == NULL)
1169 return -1;
1170 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1171
d62a17ae 1172 if (tb[IFLA_LINKINFO]) {
1173 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1fdc9eae 1174
d62a17ae 1175 if (linkinfo[IFLA_INFO_KIND])
1176 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1177
d62a17ae 1178 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1179 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1180
d62a17ae 1181 netlink_determine_zebra_iftype(kind, &zif_type);
1182 }
6675513d 1183
d62a17ae 1184 /* If linking to another interface, note it. */
1185 if (tb[IFLA_LINK])
1186 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1fdc9eae 1187
48884c6b
DS
1188 if (tb[IFLA_IFALIAS]) {
1189 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1190 }
1191
d62a17ae 1192 /* If VRF, create or update the VRF structure itself. */
78dd30b2 1193 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
d62a17ae 1194 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
1195 vrf_id = (vrf_id_t)ifi->ifi_index;
1196 }
1fdc9eae 1197
d62a17ae 1198 /* See if interface is present. */
1199 ifp = if_lookup_by_name_per_ns(zns, name);
1200
48884c6b
DS
1201 if (ifp) {
1202 if (ifp->desc)
1203 XFREE(MTYPE_TMP, ifp->desc);
1204 if (desc)
1205 ifp->desc = XSTRDUP(MTYPE_TMP, desc);
1206 }
1207
d62a17ae 1208 if (h->nlmsg_type == RTM_NEWLINK) {
1209 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1210 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1211 && !vrf_is_backend_netns()) {
d62a17ae 1212 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 1213 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1214 } else if (slave_kind
1215 && (strcmp(slave_kind, "bridge") == 0)) {
1216 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1217 bridge_ifindex =
1218 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1219 } else
1220 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1221 }
fe533c56
PG
1222 if (vrf_is_backend_netns())
1223 vrf_id = (vrf_id_t)ns_id;
d62a17ae 1224 if (ifp == NULL
1225 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1226 /* Add interface notification from kernel */
1227 if (IS_ZEBRA_DEBUG_KERNEL)
1228 zlog_debug(
1229 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1230 "sl_type %d master %u flags 0x%x",
1231 name, ifi->ifi_index, vrf_id, zif_type,
1232 zif_slave_type, bridge_ifindex,
1233 ifi->ifi_flags);
1234
1235 if (ifp == NULL) {
1236 /* unknown interface */
bcc24579 1237 ifp = if_get_by_name(name, vrf_id, 0);
d62a17ae 1238 } else {
1239 /* pre-configured interface, learnt now */
1240 if (ifp->vrf_id != vrf_id)
1241 if_update_to_new_vrf(ifp, vrf_id);
1242 }
1243
1244 /* Update interface information. */
1245 set_ifindex(ifp, ifi->ifi_index, zns);
1246 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d23b983b
SW
1247 if (!tb[IFLA_MTU]) {
1248 zlog_warn(
1249 "RTM_NEWLINK for interface %s(%u) without MTU set",
1250 name, ifi->ifi_index);
1251 return 0;
1252 }
d62a17ae 1253 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1254 ifp->metric = 0;
1255 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1256
1257 /* Set interface type */
1258 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1259 if (IS_ZEBRA_IF_VRF(ifp))
1260 SET_FLAG(ifp->status,
1261 ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1262
1263 /* Update link. */
1264 zebra_if_update_link(ifp, link_ifindex);
1265
1266 netlink_interface_update_hw_addr(tb, ifp);
1267
1268 /* Inform clients, install any configured addresses. */
1269 if_add_update(ifp);
1270
1271 /* Extract and save L2 interface information, take
1272 * additional actions. */
1273 netlink_interface_update_l2info(
1274 ifp, linkinfo[IFLA_INFO_DATA], 1);
1275 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1276 zebra_l2if_update_bridge_slave(ifp,
1277 bridge_ifindex);
b53686c5
PG
1278 if_netlink_check_ifp_instance_consistency(RTM_NEWLINK,
1279 ifp, ns_id);
d62a17ae 1280 } else if (ifp->vrf_id != vrf_id) {
1281 /* VRF change for an interface. */
1282 if (IS_ZEBRA_DEBUG_KERNEL)
1283 zlog_debug(
1284 "RTM_NEWLINK vrf-change for %s(%u) "
1285 "vrf_id %u -> %u flags 0x%x",
1286 name, ifp->ifindex, ifp->vrf_id, vrf_id,
1287 ifi->ifi_flags);
1288
1289 if_handle_vrf_change(ifp, vrf_id);
1290 } else {
1291 int was_bridge_slave;
1292
1293 /* Interface update. */
1294 if (IS_ZEBRA_DEBUG_KERNEL)
1295 zlog_debug(
1296 "RTM_NEWLINK update for %s(%u) "
1297 "sl_type %d master %u flags 0x%x",
1298 name, ifp->ifindex, zif_slave_type,
1299 bridge_ifindex, ifi->ifi_flags);
1300
1301 set_ifindex(ifp, ifi->ifi_index, zns);
d23b983b
SW
1302 if (!tb[IFLA_MTU]) {
1303 zlog_warn(
1304 "RTM_NEWLINK for interface %s(%u) without MTU set",
1305 name, ifi->ifi_index);
1306 return 0;
1307 }
d62a17ae 1308 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1309 ifp->metric = 0;
1310
1311 /* Update interface type - NOTE: Only slave_type can
1312 * change. */
1313 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
1314 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1315
1316 netlink_interface_update_hw_addr(tb, ifp);
1317
1318 if (if_is_no_ptm_operative(ifp)) {
1319 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1320 if (!if_is_no_ptm_operative(ifp)) {
1321 if (IS_ZEBRA_DEBUG_KERNEL)
1322 zlog_debug(
1323 "Intf %s(%u) has gone DOWN",
1324 name, ifp->ifindex);
1325 if_down(ifp);
1326 } else if (if_is_operative(ifp)) {
1327 /* Must notify client daemons of new
1328 * interface status. */
1329 if (IS_ZEBRA_DEBUG_KERNEL)
1330 zlog_debug(
1331 "Intf %s(%u) PTM up, notifying clients",
1332 name, ifp->ifindex);
1333 zebra_interface_up_update(ifp);
1334 }
1335 } else {
1336 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1337 if (if_is_operative(ifp)) {
1338 if (IS_ZEBRA_DEBUG_KERNEL)
1339 zlog_debug(
1340 "Intf %s(%u) has come UP",
1341 name, ifp->ifindex);
1342 if_up(ifp);
1343 }
1344 }
1345
1346 /* Extract and save L2 interface information, take
1347 * additional actions. */
1348 netlink_interface_update_l2info(
1349 ifp, linkinfo[IFLA_INFO_DATA], 0);
1350 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
1351 zebra_l2if_update_bridge_slave(ifp,
1352 bridge_ifindex);
b53686c5
PG
1353 if_netlink_check_ifp_instance_consistency(RTM_NEWLINK,
1354 ifp, ns_id);
d62a17ae 1355 }
1356 } else {
1357 /* Delete interface notification from kernel */
1358 if (ifp == NULL) {
1359 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1360 name, ifi->ifi_index);
1361 return 0;
1362 }
1363
1364 if (IS_ZEBRA_DEBUG_KERNEL)
1365 zlog_debug("RTM_DELLINK for %s(%u)", name,
1366 ifp->ifindex);
1367
1368 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
1369
1370 /* Special handling for bridge or VxLAN interfaces. */
1371 if (IS_ZEBRA_IF_BRIDGE(ifp))
1372 zebra_l2_bridge_del(ifp);
1373 else if (IS_ZEBRA_IF_VXLAN(ifp))
1374 zebra_l2_vxlanif_del(ifp);
1375
1376 if (!IS_ZEBRA_IF_VRF(ifp))
1377 if_delete_update(ifp);
b53686c5
PG
1378 if_netlink_check_ifp_instance_consistency(RTM_DELLINK,
1379 ifp, ns_id);
1fdc9eae 1380 }
1381
d62a17ae 1382 return 0;
1fdc9eae 1383}
718e3744 1384
1385/* Interface information read by netlink. */
d62a17ae 1386void interface_list(struct zebra_ns *zns)
718e3744 1387{
d62a17ae 1388 interface_lookup_netlink(zns);
718e3744 1389}
ddfeb486
DL
1390
1391#endif /* GNU_LINUX */