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