]> git.proxmox.com Git - mirror_frr.git/blame - zebra/if_netlink.c
Merge pull request #2898 from donaldsharp/vrf_bitmap_is_whack
[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;
892
893 zns = zebra_ns_lookup(ns_id);
894 ifa = NLMSG_DATA(h);
895
8a1b681c
SW
896 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
897 zlog_warn(
81227874 898 "Invalid address family: %u received from kernel interface addr change: %u",
8a1b681c 899 ifa->ifa_family, h->nlmsg_type);
d62a17ae 900 return 0;
8a1b681c 901 }
d62a17ae 902
903 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
904 return 0;
905
906 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
9bdf8618
DS
907 if (len < 0) {
908 zlog_err("%s: Message received from netlink is of a broken size: %d %zu",
909 __PRETTY_FUNCTION__,
910 h->nlmsg_len,
911 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
d62a17ae 912 return -1;
9bdf8618 913 }
d62a17ae 914
915 memset(tb, 0, sizeof tb);
916 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
917
918 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
919 if (ifp == NULL) {
af4c2728 920 flog_err(
4d43f68a 921 LIB_ERR_INTERFACE,
d62a17ae 922 "netlink_interface_addr can't find interface by index %d",
923 ifa->ifa_index);
924 return -1;
925 }
926
927 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
928 {
929 char buf[BUFSIZ];
930 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
931 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
932 ifa->ifa_flags);
933 if (tb[IFA_LOCAL])
934 zlog_debug(" IFA_LOCAL %s/%d",
935 inet_ntop(ifa->ifa_family,
936 RTA_DATA(tb[IFA_LOCAL]), buf,
937 BUFSIZ),
938 ifa->ifa_prefixlen);
939 if (tb[IFA_ADDRESS])
940 zlog_debug(" IFA_ADDRESS %s/%d",
941 inet_ntop(ifa->ifa_family,
942 RTA_DATA(tb[IFA_ADDRESS]), buf,
943 BUFSIZ),
944 ifa->ifa_prefixlen);
945 if (tb[IFA_BROADCAST])
946 zlog_debug(" IFA_BROADCAST %s/%d",
947 inet_ntop(ifa->ifa_family,
948 RTA_DATA(tb[IFA_BROADCAST]), buf,
949 BUFSIZ),
950 ifa->ifa_prefixlen);
951 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
952 zlog_debug(" IFA_LABEL %s",
953 (char *)RTA_DATA(tb[IFA_LABEL]));
954
955 if (tb[IFA_CACHEINFO]) {
956 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
957 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
958 ci->ifa_prefered, ci->ifa_valid);
959 }
960 }
961
962 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
963 if (tb[IFA_LOCAL] == NULL)
964 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
965 if (tb[IFA_ADDRESS] == NULL)
966 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
967
968 /* local interface address */
969 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
970
971 /* is there a peer address? */
972 if (tb[IFA_ADDRESS]
973 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
974 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
975 broad = RTA_DATA(tb[IFA_ADDRESS]);
976 SET_FLAG(flags, ZEBRA_IFA_PEER);
977 } else
978 /* seeking a broadcast address */
979 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
980 : NULL);
981
982 /* addr is primary key, SOL if we don't have one */
983 if (addr == NULL) {
984 zlog_debug("%s: NULL address", __func__);
985 return -1;
986 }
987
988 /* Flags. */
989 if (ifa->ifa_flags & IFA_F_SECONDARY)
990 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
991
992 /* Label */
993 if (tb[IFA_LABEL])
994 label = (char *)RTA_DATA(tb[IFA_LABEL]);
995
2e1cc436 996 if (label && strcmp(ifp->name, label) == 0)
d62a17ae 997 label = NULL;
998
999 /* Register interface address to the interface. */
1000 if (ifa->ifa_family == AF_INET) {
930571d2 1001 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
e17d9b2d 1002 zlog_err(
75829703 1003 "Invalid prefix length: %u received from kernel interface addr change: %u",
930571d2 1004 ifa->ifa_prefixlen, h->nlmsg_type);
e17d9b2d 1005 return -1;
930571d2 1006 }
d62a17ae 1007 if (h->nlmsg_type == RTM_NEWADDR)
1008 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1009 ifa->ifa_prefixlen,
1010 (struct in_addr *)broad, label);
1011 else
1012 connected_delete_ipv4(
1013 ifp, flags, (struct in_addr *)addr,
1014 ifa->ifa_prefixlen, (struct in_addr *)broad);
1015 }
1016 if (ifa->ifa_family == AF_INET6) {
930571d2 1017 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
e17d9b2d 1018 zlog_err(
75829703 1019 "Invalid prefix length: %u received from kernel interface addr change: %u",
930571d2 1020 ifa->ifa_prefixlen, h->nlmsg_type);
e17d9b2d 1021 return -1;
930571d2 1022 }
d62a17ae 1023 if (h->nlmsg_type == RTM_NEWADDR) {
1024 /* Only consider valid addresses; we'll not get a
1025 * notification from
1026 * the kernel till IPv6 DAD has completed, but at init
1027 * time, Quagga
1028 * does query for and will receive all addresses.
1029 */
1030 if (!(ifa->ifa_flags
1031 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
60466a63
QY
1032 connected_add_ipv6(ifp, flags,
1033 (struct in6_addr *)addr,
60c0687a 1034 (struct in6_addr *)broad,
60466a63 1035 ifa->ifa_prefixlen, label);
d62a17ae 1036 } else
1037 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
60c0687a 1038 (struct in6_addr *)broad,
608105a7 1039 ifa->ifa_prefixlen);
d62a17ae 1040 }
1041
1042 return 0;
1fdc9eae 1043}
1044
b53686c5
PG
1045/* helper function called by if_netlink_change
1046 * to delete interfaces in case the interface moved
1047 * to an other netns
1048 */
1049static void if_netlink_check_ifp_instance_consistency(uint16_t cmd,
1050 struct interface *ifp,
1051 ns_id_t ns_id)
1052{
9373219c 1053 struct interface *other_ifp;
b53686c5
PG
1054
1055 /*
1056 * look if interface name is also found on other netns
1057 * - only if vrf backend is netns
1058 * - do not concern lo interface
1059 * - then remove previous one
1060 * - for new link case, check found interface is not active
1061 */
1062 if (!vrf_is_backend_netns() ||
1063 !strcmp(ifp->name, "lo"))
1064 return;
9373219c
PG
1065 other_ifp = if_lookup_by_name_not_ns(ns_id, ifp->name);
1066 if (!other_ifp)
b53686c5 1067 return;
5efbf310
PG
1068 /* because previous interface may be inactive,
1069 * interface is moved back to default vrf
1070 * then one may find the same pointer; ignore
1071 */
1072 if (other_ifp == ifp)
1073 return;
b53686c5 1074 if ((cmd == RTM_NEWLINK)
9373219c 1075 && (CHECK_FLAG(other_ifp->status, ZEBRA_INTERFACE_ACTIVE)))
b53686c5 1076 return;
9373219c
PG
1077 if (IS_ZEBRA_DEBUG_KERNEL && cmd == RTM_NEWLINK) {
1078 zlog_debug("RTM_NEWLINK %s(%u, VRF %u) replaces %s(%u, VRF %u)\n",
1079 ifp->name,
1080 ifp->ifindex,
1081 ifp->vrf_id,
1082 other_ifp->name,
1083 other_ifp->ifindex,
1084 other_ifp->vrf_id);
1085 } else if (IS_ZEBRA_DEBUG_KERNEL && cmd == RTM_DELLINK) {
1086 zlog_debug("RTM_DELLINK %s(%u, VRF %u) is replaced by %s(%u, VRF %u)\n",
b53686c5 1087 ifp->name,
9373219c
PG
1088 ifp->ifindex,
1089 ifp->vrf_id,
1090 other_ifp->name,
1091 other_ifp->ifindex,
1092 other_ifp->vrf_id);
1093 }
b53686c5
PG
1094 /* the found interface replaces the current one
1095 * remove it
1096 */
1097 if (cmd == RTM_DELLINK)
1098 if_delete(ifp);
1099 else
9373219c 1100 if_delete(other_ifp);
b53686c5
PG
1101 /* the found interface is replaced by the current one
1102 * suppress it
1103 */
1104}
1105
2414abd3 1106int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1fdc9eae 1107{
d62a17ae 1108 int len;
1109 struct ifinfomsg *ifi;
1110 struct rtattr *tb[IFLA_MAX + 1];
1111 struct rtattr *linkinfo[IFLA_MAX + 1];
1112 struct interface *ifp;
1113 char *name = NULL;
1114 char *kind = NULL;
48884c6b 1115 char *desc = NULL;
d62a17ae 1116 char *slave_kind = NULL;
1117 struct zebra_ns *zns;
1118 vrf_id_t vrf_id = VRF_DEFAULT;
1119 zebra_iftype_t zif_type = ZEBRA_IF_OTHER;
1120 zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1121 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1122 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1123
1124
1125 zns = zebra_ns_lookup(ns_id);
1126 ifi = NLMSG_DATA(h);
1127
fe533c56 1128 /* assume if not default zns, then new VRF */
d62a17ae 1129 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1130 /* If this is not link add/delete message so print warning. */
1131 zlog_warn("netlink_link_change: wrong kernel message %d",
1132 h->nlmsg_type);
1133 return 0;
1134 }
1135
8a1b681c
SW
1136 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1137 || ifi->ifi_family == AF_INET6)) {
1138 zlog_warn(
81227874 1139 "Invalid address family: %u received from kernel link change: %u",
8a1b681c
SW
1140 ifi->ifi_family, h->nlmsg_type);
1141 return 0;
1142 }
1143
d62a17ae 1144 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
9bdf8618
DS
1145 if (len < 0) {
1146 zlog_err("%s: Message received from netlink is of a broken size %d %zu",
1147 __PRETTY_FUNCTION__, h->nlmsg_len,
1148 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
d62a17ae 1149 return -1;
9bdf8618 1150 }
d62a17ae 1151
1152 /* We are interested in some AF_BRIDGE notifications. */
1153 if (ifi->ifi_family == AF_BRIDGE)
1154 return netlink_bridge_interface(h, len, ns_id, startup);
1155
1156 /* Looking up interface name. */
1157 memset(tb, 0, sizeof tb);
1158 memset(linkinfo, 0, sizeof linkinfo);
1159 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1fdc9eae 1160
d62a17ae 1161 /* check for wireless messages to ignore */
1162 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1163 if (IS_ZEBRA_DEBUG_KERNEL)
1164 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1165 __func__);
1166 return 0;
1167 }
1fdc9eae 1168
d62a17ae 1169 if (tb[IFLA_IFNAME] == NULL)
1170 return -1;
1171 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1fdc9eae 1172
d62a17ae 1173 if (tb[IFLA_LINKINFO]) {
1174 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1fdc9eae 1175
d62a17ae 1176 if (linkinfo[IFLA_INFO_KIND])
1177 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1fdc9eae 1178
d62a17ae 1179 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1180 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1fdc9eae 1181
d62a17ae 1182 netlink_determine_zebra_iftype(kind, &zif_type);
1183 }
6675513d 1184
d62a17ae 1185 /* If linking to another interface, note it. */
1186 if (tb[IFLA_LINK])
1187 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1fdc9eae 1188
48884c6b
DS
1189 if (tb[IFLA_IFALIAS]) {
1190 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1191 }
1192
d62a17ae 1193 /* If VRF, create or update the VRF structure itself. */
78dd30b2 1194 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
d62a17ae 1195 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
1196 vrf_id = (vrf_id_t)ifi->ifi_index;
1197 }
1fdc9eae 1198
d62a17ae 1199 /* See if interface is present. */
1200 ifp = if_lookup_by_name_per_ns(zns, name);
1201
48884c6b
DS
1202 if (ifp) {
1203 if (ifp->desc)
1204 XFREE(MTYPE_TMP, ifp->desc);
1205 if (desc)
1206 ifp->desc = XSTRDUP(MTYPE_TMP, desc);
1207 }
1208
d62a17ae 1209 if (h->nlmsg_type == RTM_NEWLINK) {
1210 if (tb[IFLA_MASTER]) {
78dd30b2
PG
1211 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1212 && !vrf_is_backend_netns()) {
d62a17ae 1213 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
d7c0a89a 1214 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
d62a17ae 1215 } else if (slave_kind
1216 && (strcmp(slave_kind, "bridge") == 0)) {
1217 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1218 bridge_ifindex =
1219 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1220 } else
1221 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1222 }
fe533c56
PG
1223 if (vrf_is_backend_netns())
1224 vrf_id = (vrf_id_t)ns_id;
d62a17ae 1225 if (ifp == NULL
1226 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1227 /* Add interface notification from kernel */
1228 if (IS_ZEBRA_DEBUG_KERNEL)
1229 zlog_debug(
1230 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d "
1231 "sl_type %d master %u flags 0x%x",
1232 name, ifi->ifi_index, vrf_id, zif_type,
1233 zif_slave_type, bridge_ifindex,
1234 ifi->ifi_flags);
1235
1236 if (ifp == NULL) {
1237 /* unknown interface */
bcc24579 1238 ifp = if_get_by_name(name, vrf_id, 0);
d62a17ae 1239 } else {
1240 /* pre-configured interface, learnt now */
1241 if (ifp->vrf_id != vrf_id)
1242 if_update_to_new_vrf(ifp, vrf_id);
1243 }
1244
1245 /* Update interface information. */
1246 set_ifindex(ifp, ifi->ifi_index, zns);
1247 ifp->flags = ifi->ifi_flags & 0x0000fffff;
d23b983b
SW
1248 if (!tb[IFLA_MTU]) {
1249 zlog_warn(
1250 "RTM_NEWLINK for interface %s(%u) without MTU set",
1251 name, ifi->ifi_index);
1252 return 0;
1253 }
d62a17ae 1254 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1255 ifp->metric = 0;
1256 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1257
1258 /* Set interface type */
1259 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
b0fa6f6a
CS
1260 if (IS_ZEBRA_IF_VRF(ifp))
1261 SET_FLAG(ifp->status,
1262 ZEBRA_INTERFACE_VRF_LOOPBACK);
d62a17ae 1263
1264 /* Update link. */
1265 zebra_if_update_link(ifp, link_ifindex);
1266
1267 netlink_interface_update_hw_addr(tb, ifp);
1268
1269 /* Inform clients, install any configured addresses. */
1270 if_add_update(ifp);
1271
1272 /* Extract and save L2 interface information, take
1273 * additional actions. */
1274 netlink_interface_update_l2info(
1275 ifp, linkinfo[IFLA_INFO_DATA], 1);
1276 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1277 zebra_l2if_update_bridge_slave(ifp,
1278 bridge_ifindex);
b53686c5
PG
1279 if_netlink_check_ifp_instance_consistency(RTM_NEWLINK,
1280 ifp, ns_id);
d62a17ae 1281 } else if (ifp->vrf_id != vrf_id) {
1282 /* VRF change for an interface. */
1283 if (IS_ZEBRA_DEBUG_KERNEL)
1284 zlog_debug(
1285 "RTM_NEWLINK vrf-change for %s(%u) "
1286 "vrf_id %u -> %u flags 0x%x",
1287 name, ifp->ifindex, ifp->vrf_id, vrf_id,
1288 ifi->ifi_flags);
1289
1290 if_handle_vrf_change(ifp, vrf_id);
1291 } else {
1292 int was_bridge_slave;
1293
1294 /* Interface update. */
1295 if (IS_ZEBRA_DEBUG_KERNEL)
1296 zlog_debug(
1297 "RTM_NEWLINK update for %s(%u) "
1298 "sl_type %d master %u flags 0x%x",
1299 name, ifp->ifindex, zif_slave_type,
1300 bridge_ifindex, ifi->ifi_flags);
1301
1302 set_ifindex(ifp, ifi->ifi_index, zns);
d23b983b
SW
1303 if (!tb[IFLA_MTU]) {
1304 zlog_warn(
1305 "RTM_NEWLINK for interface %s(%u) without MTU set",
1306 name, ifi->ifi_index);
1307 return 0;
1308 }
d62a17ae 1309 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1310 ifp->metric = 0;
1311
1312 /* Update interface type - NOTE: Only slave_type can
1313 * change. */
1314 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
1315 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1316
1317 netlink_interface_update_hw_addr(tb, ifp);
1318
1319 if (if_is_no_ptm_operative(ifp)) {
1320 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1321 if (!if_is_no_ptm_operative(ifp)) {
1322 if (IS_ZEBRA_DEBUG_KERNEL)
1323 zlog_debug(
1324 "Intf %s(%u) has gone DOWN",
1325 name, ifp->ifindex);
1326 if_down(ifp);
1327 } else if (if_is_operative(ifp)) {
1328 /* Must notify client daemons of new
1329 * interface status. */
1330 if (IS_ZEBRA_DEBUG_KERNEL)
1331 zlog_debug(
1332 "Intf %s(%u) PTM up, notifying clients",
1333 name, ifp->ifindex);
1334 zebra_interface_up_update(ifp);
1335 }
1336 } else {
1337 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1338 if (if_is_operative(ifp)) {
1339 if (IS_ZEBRA_DEBUG_KERNEL)
1340 zlog_debug(
1341 "Intf %s(%u) has come UP",
1342 name, ifp->ifindex);
1343 if_up(ifp);
1344 }
1345 }
1346
1347 /* Extract and save L2 interface information, take
1348 * additional actions. */
1349 netlink_interface_update_l2info(
1350 ifp, linkinfo[IFLA_INFO_DATA], 0);
1351 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
1352 zebra_l2if_update_bridge_slave(ifp,
1353 bridge_ifindex);
b53686c5
PG
1354 if_netlink_check_ifp_instance_consistency(RTM_NEWLINK,
1355 ifp, ns_id);
d62a17ae 1356 }
1357 } else {
1358 /* Delete interface notification from kernel */
1359 if (ifp == NULL) {
1360 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
1361 name, ifi->ifi_index);
1362 return 0;
1363 }
1364
1365 if (IS_ZEBRA_DEBUG_KERNEL)
1366 zlog_debug("RTM_DELLINK for %s(%u)", name,
1367 ifp->ifindex);
1368
1369 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
1370
1371 /* Special handling for bridge or VxLAN interfaces. */
1372 if (IS_ZEBRA_IF_BRIDGE(ifp))
1373 zebra_l2_bridge_del(ifp);
1374 else if (IS_ZEBRA_IF_VXLAN(ifp))
1375 zebra_l2_vxlanif_del(ifp);
1376
1377 if (!IS_ZEBRA_IF_VRF(ifp))
1378 if_delete_update(ifp);
b53686c5
PG
1379 if_netlink_check_ifp_instance_consistency(RTM_DELLINK,
1380 ifp, ns_id);
1fdc9eae 1381 }
1382
d62a17ae 1383 return 0;
1fdc9eae 1384}
718e3744 1385
1386/* Interface information read by netlink. */
d62a17ae 1387void interface_list(struct zebra_ns *zns)
718e3744 1388{
d62a17ae 1389 interface_lookup_netlink(zns);
718e3744 1390}
ddfeb486
DL
1391
1392#endif /* GNU_LINUX */