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