]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_netlink.c
Merge pull request #6753 from mjstapp/fix_zebra_backup_sa
[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 uint32_t ns_id, 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 if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
318 vrf_id_t exist_id;
319
320 exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
321 if (exist_id != VRF_DEFAULT) {
322 vrf = vrf_lookup_by_id(exist_id);
323
324 flog_err(
325 EC_ZEBRA_VRF_MISCONFIGURED,
326 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
327 name, ifi->ifi_index, vrf->name);
328 exit(-1);
329 }
330 }
331
332 /*
333 * vrf_get is implied creation if it does not exist
334 */
335 vrf = vrf_get((vrf_id_t)ifi->ifi_index,
336 name); // It would create vrf
337 if (!vrf) {
338 flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
339 name, ifi->ifi_index);
340 return;
341 }
342
343 /*
344 * This is the only place that we get the actual kernel table_id
345 * being used. We need it to set the table_id of the routes
346 * we are passing to the kernel.... And to throw some totally
347 * awesome parties. that too.
348 *
349 * At this point we *must* have a zvrf because the vrf_create
350 * callback creates one. We *must* set the table id
351 * before the vrf_enable because of( at the very least )
352 * static routes being delayed for installation until
353 * during the vrf_enable callbacks.
354 */
355 zvrf = (struct zebra_vrf *)vrf->info;
356 zvrf->table_id = nl_table_id;
357
358 /* Enable the created VRF. */
359 if (!vrf_enable(vrf)) {
360 flog_err(EC_LIB_INTERFACE,
361 "Failed to enable VRF %s id %u", name,
362 ifi->ifi_index);
363 return;
364 }
365
366 } else // h->nlmsg_type == RTM_DELLINK
367 {
368 if (IS_ZEBRA_DEBUG_KERNEL)
369 zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
370 ifi->ifi_index);
371
372 vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
373
374 if (!vrf) {
375 flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
376 __func__);
377 return;
378 }
379
380 vrf_delete(vrf);
381 }
382 }
383
384 static uint32_t get_iflink_speed(struct interface *interface, int *error)
385 {
386 struct ifreq ifdata;
387 struct ethtool_cmd ecmd;
388 int sd;
389 int rc;
390 const char *ifname = interface->name;
391
392 if (error)
393 *error = 0;
394 /* initialize struct */
395 memset(&ifdata, 0, sizeof(ifdata));
396
397 /* set interface name */
398 strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
399
400 /* initialize ethtool interface */
401 memset(&ecmd, 0, sizeof(ecmd));
402 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
403 ifdata.ifr_data = (caddr_t)&ecmd;
404
405 /* use ioctl to get IP address of an interface */
406 frr_with_privs(&zserv_privs) {
407 sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
408 interface->vrf_id,
409 NULL);
410 if (sd < 0) {
411 if (IS_ZEBRA_DEBUG_KERNEL)
412 zlog_debug("Failure to read interface %s speed: %d %s",
413 ifname, errno, safe_strerror(errno));
414 /* no vrf socket creation may probably mean vrf issue */
415 if (error)
416 *error = -1;
417 return 0;
418 }
419 /* Get the current link state for the interface */
420 rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL,
421 (char *)&ifdata);
422 }
423 if (rc < 0) {
424 if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
425 zlog_debug(
426 "IOCTL failure to read interface %s speed: %d %s",
427 ifname, errno, safe_strerror(errno));
428 /* no device means interface unreachable */
429 if (errno == ENODEV && error)
430 *error = -1;
431 ecmd.speed_hi = 0;
432 ecmd.speed = 0;
433 }
434
435 close(sd);
436
437 return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
438 }
439
440 uint32_t kernel_get_speed(struct interface *ifp, int *error)
441 {
442 return get_iflink_speed(ifp, error);
443 }
444
445 static int netlink_extract_bridge_info(struct rtattr *link_data,
446 struct zebra_l2info_bridge *bridge_info)
447 {
448 struct rtattr *attr[IFLA_BR_MAX + 1];
449
450 memset(bridge_info, 0, sizeof(*bridge_info));
451 memset(attr, 0, sizeof(attr));
452 parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
453 if (attr[IFLA_BR_VLAN_FILTERING])
454 bridge_info->vlan_aware =
455 *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
456 return 0;
457 }
458
459 static int netlink_extract_vlan_info(struct rtattr *link_data,
460 struct zebra_l2info_vlan *vlan_info)
461 {
462 struct rtattr *attr[IFLA_VLAN_MAX + 1];
463 vlanid_t vid_in_msg;
464
465 memset(vlan_info, 0, sizeof(*vlan_info));
466 memset(attr, 0, sizeof(attr));
467 parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
468 if (!attr[IFLA_VLAN_ID]) {
469 if (IS_ZEBRA_DEBUG_KERNEL)
470 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
471 return -1;
472 }
473
474 vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
475 vlan_info->vid = vid_in_msg;
476 return 0;
477 }
478
479 static int netlink_extract_vxlan_info(struct rtattr *link_data,
480 struct zebra_l2info_vxlan *vxl_info)
481 {
482 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
483 vni_t vni_in_msg;
484 struct in_addr vtep_ip_in_msg;
485 ifindex_t ifindex_link;
486
487 memset(vxl_info, 0, sizeof(*vxl_info));
488 memset(attr, 0, sizeof(attr));
489 parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
490 if (!attr[IFLA_VXLAN_ID]) {
491 if (IS_ZEBRA_DEBUG_KERNEL)
492 zlog_debug(
493 "IFLA_VXLAN_ID missing from VXLAN IF message");
494 return -1;
495 }
496
497 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
498 vxl_info->vni = vni_in_msg;
499 if (!attr[IFLA_VXLAN_LOCAL]) {
500 if (IS_ZEBRA_DEBUG_KERNEL)
501 zlog_debug(
502 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
503 } else {
504 vtep_ip_in_msg =
505 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
506 vxl_info->vtep_ip = vtep_ip_in_msg;
507 }
508
509 if (attr[IFLA_VXLAN_GROUP]) {
510 vxl_info->mcast_grp =
511 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_GROUP]);
512 }
513
514 if (!attr[IFLA_VXLAN_LINK]) {
515 if (IS_ZEBRA_DEBUG_KERNEL)
516 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
517 } else {
518 ifindex_link =
519 *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
520 vxl_info->ifindex_link = ifindex_link;
521 }
522 return 0;
523 }
524
525 /*
526 * Extract and save L2 params (of interest) for an interface. When a
527 * bridge interface is added or updated, take further actions to map
528 * its members. Likewise, for VxLAN interface.
529 */
530 static void netlink_interface_update_l2info(struct interface *ifp,
531 struct rtattr *link_data, int add,
532 ns_id_t link_nsid)
533 {
534 if (!link_data)
535 return;
536
537 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
538 struct zebra_l2info_bridge bridge_info;
539
540 netlink_extract_bridge_info(link_data, &bridge_info);
541 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
542 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
543 struct zebra_l2info_vlan vlan_info;
544
545 netlink_extract_vlan_info(link_data, &vlan_info);
546 zebra_l2_vlanif_update(ifp, &vlan_info);
547 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
548 struct zebra_l2info_vxlan vxlan_info;
549
550 netlink_extract_vxlan_info(link_data, &vxlan_info);
551 vxlan_info.link_nsid = link_nsid;
552 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
553 if (link_nsid != NS_UNKNOWN &&
554 vxlan_info.ifindex_link)
555 zebra_if_update_link(ifp, vxlan_info.ifindex_link,
556 link_nsid);
557 }
558 }
559
560 static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
561 int startup)
562 {
563 char *name = NULL;
564 struct ifinfomsg *ifi;
565 struct rtattr *tb[IFLA_MAX + 1];
566 struct interface *ifp;
567 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
568 struct {
569 uint16_t flags;
570 uint16_t vid;
571 } * vinfo;
572 vlanid_t access_vlan;
573
574 /* Fetch name and ifindex */
575 ifi = NLMSG_DATA(h);
576 memset(tb, 0, sizeof(tb));
577 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
578
579 if (tb[IFLA_IFNAME] == NULL)
580 return -1;
581 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
582
583 /* The interface should already be known, if not discard. */
584 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
585 if (!ifp) {
586 zlog_debug("Cannot find bridge IF %s(%u)", name,
587 ifi->ifi_index);
588 return 0;
589 }
590 if (!IS_ZEBRA_IF_VXLAN(ifp))
591 return 0;
592
593 /* We are only interested in the access VLAN i.e., AF_SPEC */
594 if (!tb[IFLA_AF_SPEC])
595 return 0;
596
597 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
598 * only 1 access VLAN is accepted.
599 */
600 memset(aftb, 0, sizeof(aftb));
601 parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]);
602 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
603 return 0;
604
605 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
606 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
607 return 0;
608
609 access_vlan = (vlanid_t)vinfo->vid;
610 if (IS_ZEBRA_DEBUG_KERNEL)
611 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
612 name, ifi->ifi_index);
613 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
614 return 0;
615 }
616
617 /*
618 * Called from interface_lookup_netlink(). This function is only used
619 * during bootstrap.
620 */
621 static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
622 {
623 int len;
624 struct ifinfomsg *ifi;
625 struct rtattr *tb[IFLA_MAX + 1];
626 struct rtattr *linkinfo[IFLA_MAX + 1];
627 struct interface *ifp;
628 char *name = NULL;
629 char *kind = NULL;
630 char *desc = NULL;
631 char *slave_kind = NULL;
632 struct zebra_ns *zns = NULL;
633 vrf_id_t vrf_id = VRF_DEFAULT;
634 zebra_iftype_t zif_type = ZEBRA_IF_OTHER;
635 zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE;
636 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
637 ifindex_t link_ifindex = IFINDEX_INTERNAL;
638 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
639 struct zebra_if *zif;
640 ns_id_t link_nsid = ns_id;
641
642 zns = zebra_ns_lookup(ns_id);
643 ifi = NLMSG_DATA(h);
644
645 if (h->nlmsg_type != RTM_NEWLINK)
646 return 0;
647
648 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
649 if (len < 0) {
650 zlog_err(
651 "%s: Message received from netlink is of a broken size: %d %zu",
652 __func__, h->nlmsg_len,
653 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
654 return -1;
655 }
656
657 /* We are interested in some AF_BRIDGE notifications. */
658 if (ifi->ifi_family == AF_BRIDGE)
659 return netlink_bridge_interface(h, len, ns_id, startup);
660
661 /* Looking up interface name. */
662 memset(tb, 0, sizeof(tb));
663 memset(linkinfo, 0, sizeof(linkinfo));
664 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
665
666 /* check for wireless messages to ignore */
667 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
668 if (IS_ZEBRA_DEBUG_KERNEL)
669 zlog_debug("%s: ignoring IFLA_WIRELESS message",
670 __func__);
671 return 0;
672 }
673
674 if (tb[IFLA_IFNAME] == NULL)
675 return -1;
676 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
677
678 if (tb[IFLA_IFALIAS])
679 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
680
681 if (tb[IFLA_LINKINFO]) {
682 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
683
684 if (linkinfo[IFLA_INFO_KIND])
685 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
686
687 if (linkinfo[IFLA_INFO_SLAVE_KIND])
688 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
689
690 if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
691 netlink_determine_zebra_iftype("bond_slave", &zif_type);
692 else
693 netlink_determine_zebra_iftype(kind, &zif_type);
694 }
695
696 /* If VRF, create the VRF structure itself. */
697 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
698 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
699 vrf_id = (vrf_id_t)ifi->ifi_index;
700 }
701
702 if (tb[IFLA_MASTER]) {
703 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
704 && !vrf_is_backend_netns()) {
705 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
706 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
707 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
708 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
709 bridge_ifindex =
710 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
711 } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
712 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
713 bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
714 } else
715 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
716 }
717 if (vrf_is_backend_netns())
718 vrf_id = (vrf_id_t)ns_id;
719
720 /* If linking to another interface, note it. */
721 if (tb[IFLA_LINK])
722 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
723
724 if (tb[IFLA_LINK_NETNSID]) {
725 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
726 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
727 }
728
729 /* Add interface.
730 * We add by index first because in some cases such as the master
731 * interface, we have the index before we have the name. Fixing
732 * back references on the slave interfaces is painful if not done
733 * this way, i.e. by creating by ifindex.
734 */
735 ifp = if_get_by_ifindex(ifi->ifi_index, vrf_id, name);
736 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
737
738 ifp->flags = ifi->ifi_flags & 0x0000fffff;
739 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
740 ifp->metric = 0;
741 ifp->speed = get_iflink_speed(ifp, NULL);
742 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
743
744 /* Set zebra interface type */
745 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
746 if (IS_ZEBRA_IF_VRF(ifp))
747 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
748
749 /*
750 * Just set the @link/lower-device ifindex. During nldump interfaces are
751 * not ordered in any fashion so we may end up getting upper devices
752 * before lower devices. We will setup the real linkage once the dump
753 * is complete.
754 */
755 zif = (struct zebra_if *)ifp->info;
756 zif->link_ifindex = link_ifindex;
757
758 if (desc) {
759 XFREE(MTYPE_TMP, zif->desc);
760 zif->desc = XSTRDUP(MTYPE_TMP, desc);
761 }
762
763 /* Hardware type and address. */
764 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
765 netlink_interface_update_hw_addr(tb, ifp);
766
767 if_add_update(ifp);
768
769 /* Extract and save L2 interface information, take additional actions.
770 */
771 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
772 1, link_nsid);
773 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
774 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id);
775 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
776 zebra_l2if_update_bond_slave(ifp, bond_ifindex);
777
778 return 0;
779 }
780
781 /* Request for specific interface or address information from the kernel */
782 static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
783 int type, uint32_t filter_mask)
784 {
785 struct {
786 struct nlmsghdr n;
787 struct ifinfomsg ifm;
788 char buf[256];
789 } req;
790
791 /* Form the request, specifying filter (rtattr) if needed. */
792 memset(&req, 0, sizeof(req));
793 req.n.nlmsg_type = type;
794 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
795 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
796 req.ifm.ifi_family = family;
797
798 /* Include filter, if specified. */
799 if (filter_mask)
800 nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
801
802 return netlink_request(netlink_cmd, &req);
803 }
804
805 /* Interface lookup by netlink socket. */
806 int interface_lookup_netlink(struct zebra_ns *zns)
807 {
808 int ret;
809 struct zebra_dplane_info dp_info;
810 struct nlsock *netlink_cmd = &zns->netlink_cmd;
811
812 /* Capture key info from ns struct */
813 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
814
815 /* Get interface information. */
816 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
817 if (ret < 0)
818 return ret;
819 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
820 1);
821 if (ret < 0)
822 return ret;
823
824 /* Get interface information - for bridge interfaces. */
825 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
826 RTEXT_FILTER_BRVLAN);
827 if (ret < 0)
828 return ret;
829 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
830 0);
831 if (ret < 0)
832 return ret;
833
834 /* Get interface information - for bridge interfaces. */
835 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
836 RTEXT_FILTER_BRVLAN);
837 if (ret < 0)
838 return ret;
839 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
840 0);
841 if (ret < 0)
842 return ret;
843
844 /* fixup linkages */
845 zebra_if_update_all_links();
846 return 0;
847 }
848
849 /**
850 * interface_addr_lookup_netlink() - Look up interface addresses
851 *
852 * @zns: Zebra netlink socket
853 * Return: Result status
854 */
855 static int interface_addr_lookup_netlink(struct zebra_ns *zns)
856 {
857 int ret;
858 struct zebra_dplane_info dp_info;
859 struct nlsock *netlink_cmd = &zns->netlink_cmd;
860
861 /* Capture key info from ns struct */
862 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
863
864 /* Get IPv4 address of the interfaces. */
865 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
866 if (ret < 0)
867 return ret;
868 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
869 0, 1);
870 if (ret < 0)
871 return ret;
872
873 /* Get IPv6 address of the interfaces. */
874 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
875 if (ret < 0)
876 return ret;
877 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
878 0, 1);
879 if (ret < 0)
880 return ret;
881
882 return 0;
883 }
884
885 int kernel_interface_set_master(struct interface *master,
886 struct interface *slave)
887 {
888 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
889
890 struct {
891 struct nlmsghdr n;
892 struct ifinfomsg ifa;
893 char buf[NL_PKT_BUF_SIZE];
894 } req;
895
896 memset(&req, 0, sizeof(req));
897
898 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
899 req.n.nlmsg_flags = NLM_F_REQUEST;
900 req.n.nlmsg_type = RTM_SETLINK;
901 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
902
903 req.ifa.ifi_index = slave->ifindex;
904
905 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
906 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
907
908 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
909 0);
910 }
911
912 /* Interface address modification. */
913 static int netlink_address_ctx(const struct zebra_dplane_ctx *ctx)
914 {
915 int bytelen;
916 const struct prefix *p;
917 int cmd;
918 const char *label;
919
920 struct {
921 struct nlmsghdr n;
922 struct ifaddrmsg ifa;
923 char buf[NL_PKT_BUF_SIZE];
924 } req;
925
926 p = dplane_ctx_get_intf_addr(ctx);
927 memset(&req, 0, sizeof(req) - NL_PKT_BUF_SIZE);
928
929 bytelen = (p->family == AF_INET ? 4 : 16);
930
931 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
932 req.n.nlmsg_flags = NLM_F_REQUEST;
933
934 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
935 cmd = RTM_NEWADDR;
936 else
937 cmd = RTM_DELADDR;
938
939 req.n.nlmsg_type = cmd;
940 req.ifa.ifa_family = p->family;
941
942 req.ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
943
944 nl_attr_put(&req.n, sizeof(req), IFA_LOCAL, &p->u.prefix, bytelen);
945
946 if (p->family == AF_INET) {
947 if (dplane_ctx_intf_is_connected(ctx)) {
948 p = dplane_ctx_get_intf_dest(ctx);
949 nl_attr_put(&req.n, sizeof(req), IFA_ADDRESS,
950 &p->u.prefix, bytelen);
951 } else if (cmd == RTM_NEWADDR) {
952 struct in_addr broad = {
953 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
954 p->prefixlen)
955 };
956 nl_attr_put(&req.n, sizeof(req), IFA_BROADCAST, &broad,
957 bytelen);
958 }
959 }
960
961 /* p is now either address or destination/bcast addr */
962 req.ifa.ifa_prefixlen = p->prefixlen;
963
964 if (dplane_ctx_intf_is_secondary(ctx))
965 SET_FLAG(req.ifa.ifa_flags, IFA_F_SECONDARY);
966
967 if (dplane_ctx_intf_has_label(ctx)) {
968 label = dplane_ctx_get_intf_label(ctx);
969 nl_attr_put(&req.n, sizeof(req), IFA_LABEL, label,
970 strlen(label) + 1);
971 }
972
973 return netlink_talk_info(netlink_talk_filter, &req.n,
974 dplane_ctx_get_ns(ctx), 0);
975 }
976
977 enum zebra_dplane_result kernel_address_update_ctx(struct zebra_dplane_ctx *ctx)
978 {
979 return (netlink_address_ctx(ctx) == 0 ?
980 ZEBRA_DPLANE_REQUEST_SUCCESS : ZEBRA_DPLANE_REQUEST_FAILURE);
981 }
982
983 int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
984 {
985 int len;
986 struct ifaddrmsg *ifa;
987 struct rtattr *tb[IFA_MAX + 1];
988 struct interface *ifp;
989 void *addr;
990 void *broad;
991 uint8_t flags = 0;
992 char *label = NULL;
993 struct zebra_ns *zns;
994 uint32_t metric = METRIC_MAX;
995 uint32_t kernel_flags = 0;
996
997 zns = zebra_ns_lookup(ns_id);
998 ifa = NLMSG_DATA(h);
999
1000 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1001 flog_warn(
1002 EC_ZEBRA_UNKNOWN_FAMILY,
1003 "Invalid address family: %u received from kernel interface addr change: %s",
1004 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
1005 return 0;
1006 }
1007
1008 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1009 return 0;
1010
1011 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1012 if (len < 0) {
1013 zlog_err(
1014 "%s: Message received from netlink is of a broken size: %d %zu",
1015 __func__, h->nlmsg_len,
1016 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
1017 return -1;
1018 }
1019
1020 memset(tb, 0, sizeof(tb));
1021 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1022
1023 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1024 if (ifp == NULL) {
1025 flog_err(
1026 EC_LIB_INTERFACE,
1027 "netlink_interface_addr can't find interface by index %d",
1028 ifa->ifa_index);
1029 return -1;
1030 }
1031
1032 /* Flags passed through */
1033 if (tb[IFA_FLAGS])
1034 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1035 else
1036 kernel_flags = ifa->ifa_flags;
1037
1038 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1039 {
1040 char buf[BUFSIZ];
1041 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1042 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
1043 kernel_flags);
1044 if (tb[IFA_LOCAL])
1045 zlog_debug(" IFA_LOCAL %s/%d",
1046 inet_ntop(ifa->ifa_family,
1047 RTA_DATA(tb[IFA_LOCAL]), buf,
1048 BUFSIZ),
1049 ifa->ifa_prefixlen);
1050 if (tb[IFA_ADDRESS])
1051 zlog_debug(" IFA_ADDRESS %s/%d",
1052 inet_ntop(ifa->ifa_family,
1053 RTA_DATA(tb[IFA_ADDRESS]), buf,
1054 BUFSIZ),
1055 ifa->ifa_prefixlen);
1056 if (tb[IFA_BROADCAST])
1057 zlog_debug(" IFA_BROADCAST %s/%d",
1058 inet_ntop(ifa->ifa_family,
1059 RTA_DATA(tb[IFA_BROADCAST]), buf,
1060 BUFSIZ),
1061 ifa->ifa_prefixlen);
1062 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1063 zlog_debug(" IFA_LABEL %s",
1064 (char *)RTA_DATA(tb[IFA_LABEL]));
1065
1066 if (tb[IFA_CACHEINFO]) {
1067 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1068 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1069 ci->ifa_prefered, ci->ifa_valid);
1070 }
1071 }
1072
1073 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1074 if (tb[IFA_LOCAL] == NULL)
1075 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1076 if (tb[IFA_ADDRESS] == NULL)
1077 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1078
1079 /* local interface address */
1080 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1081
1082 /* is there a peer address? */
1083 if (tb[IFA_ADDRESS]
1084 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1085 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1086 broad = RTA_DATA(tb[IFA_ADDRESS]);
1087 SET_FLAG(flags, ZEBRA_IFA_PEER);
1088 } else
1089 /* seeking a broadcast address */
1090 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1091 : NULL);
1092
1093 /* addr is primary key, SOL if we don't have one */
1094 if (addr == NULL) {
1095 zlog_debug("%s: Local Interface Address is NULL for %s",
1096 __func__, ifp->name);
1097 return -1;
1098 }
1099
1100 /* Flags. */
1101 if (kernel_flags & IFA_F_SECONDARY)
1102 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1103
1104 /* Label */
1105 if (tb[IFA_LABEL])
1106 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1107
1108 if (label && strcmp(ifp->name, label) == 0)
1109 label = NULL;
1110
1111 if (tb[IFA_RT_PRIORITY])
1112 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1113
1114 /* Register interface address to the interface. */
1115 if (ifa->ifa_family == AF_INET) {
1116 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1117 zlog_err(
1118 "Invalid prefix length: %u received from kernel interface addr change: %s",
1119 ifa->ifa_prefixlen,
1120 nl_msg_type_to_str(h->nlmsg_type));
1121 return -1;
1122 }
1123
1124 if (h->nlmsg_type == RTM_NEWADDR)
1125 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1126 ifa->ifa_prefixlen,
1127 (struct in_addr *)broad, label,
1128 metric);
1129 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1130 /* Delete with a peer address */
1131 connected_delete_ipv4(
1132 ifp, flags, (struct in_addr *)addr,
1133 ifa->ifa_prefixlen, broad);
1134 } else
1135 connected_delete_ipv4(
1136 ifp, flags, (struct in_addr *)addr,
1137 ifa->ifa_prefixlen, NULL);
1138 }
1139
1140 if (ifa->ifa_family == AF_INET6) {
1141 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1142 zlog_err(
1143 "Invalid prefix length: %u received from kernel interface addr change: %s",
1144 ifa->ifa_prefixlen,
1145 nl_msg_type_to_str(h->nlmsg_type));
1146 return -1;
1147 }
1148 if (h->nlmsg_type == RTM_NEWADDR) {
1149 /* Only consider valid addresses; we'll not get a
1150 * notification from
1151 * the kernel till IPv6 DAD has completed, but at init
1152 * time, Quagga
1153 * does query for and will receive all addresses.
1154 */
1155 if (!(kernel_flags
1156 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
1157 connected_add_ipv6(ifp, flags,
1158 (struct in6_addr *)addr,
1159 (struct in6_addr *)broad,
1160 ifa->ifa_prefixlen, label,
1161 metric);
1162 } else
1163 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
1164 NULL, ifa->ifa_prefixlen);
1165 }
1166
1167
1168 /*
1169 * Linux kernel does not send route delete on interface down/addr del
1170 * so we have to re-process routes it owns (i.e. kernel routes)
1171 */
1172 if (h->nlmsg_type != RTM_NEWADDR)
1173 rib_update(RIB_UPDATE_KERNEL);
1174
1175 return 0;
1176 }
1177
1178 int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1179 {
1180 int len;
1181 struct ifinfomsg *ifi;
1182 struct rtattr *tb[IFLA_MAX + 1];
1183 struct rtattr *linkinfo[IFLA_MAX + 1];
1184 struct interface *ifp;
1185 char *name = NULL;
1186 char *kind = NULL;
1187 char *desc = NULL;
1188 char *slave_kind = NULL;
1189 struct zebra_ns *zns;
1190 vrf_id_t vrf_id = VRF_DEFAULT;
1191 zebra_iftype_t zif_type = ZEBRA_IF_OTHER;
1192 zebra_slave_iftype_t zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1193 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1194 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1195 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1196 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
1197 struct zebra_if *zif;
1198 ns_id_t link_nsid = ns_id;
1199
1200 zns = zebra_ns_lookup(ns_id);
1201 ifi = NLMSG_DATA(h);
1202
1203 /* assume if not default zns, then new VRF */
1204 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1205 /* If this is not link add/delete message so print warning. */
1206 zlog_debug("netlink_link_change: wrong kernel message %s",
1207 nl_msg_type_to_str(h->nlmsg_type));
1208 return 0;
1209 }
1210
1211 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1212 || ifi->ifi_family == AF_INET6)) {
1213 flog_warn(
1214 EC_ZEBRA_UNKNOWN_FAMILY,
1215 "Invalid address family: %u received from kernel link change: %s",
1216 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
1217 return 0;
1218 }
1219
1220 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1221 if (len < 0) {
1222 zlog_err(
1223 "%s: Message received from netlink is of a broken size %d %zu",
1224 __func__, h->nlmsg_len,
1225 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
1226 return -1;
1227 }
1228
1229 /* We are interested in some AF_BRIDGE notifications. */
1230 if (ifi->ifi_family == AF_BRIDGE)
1231 return netlink_bridge_interface(h, len, ns_id, startup);
1232
1233 /* Looking up interface name. */
1234 memset(tb, 0, sizeof(tb));
1235 memset(linkinfo, 0, sizeof(linkinfo));
1236 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1237
1238 /* check for wireless messages to ignore */
1239 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1240 if (IS_ZEBRA_DEBUG_KERNEL)
1241 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1242 __func__);
1243 return 0;
1244 }
1245
1246 if (tb[IFLA_IFNAME] == NULL)
1247 return -1;
1248 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1249
1250 if (tb[IFLA_LINKINFO]) {
1251 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1252
1253 if (linkinfo[IFLA_INFO_KIND])
1254 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1255
1256 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1257 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1258
1259 netlink_determine_zebra_iftype(kind, &zif_type);
1260 }
1261
1262 /* If linking to another interface, note it. */
1263 if (tb[IFLA_LINK])
1264 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1265
1266 if (tb[IFLA_LINK_NETNSID]) {
1267 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
1268 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1269 }
1270 if (tb[IFLA_IFALIAS]) {
1271 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1272 }
1273
1274 /* If VRF, create or update the VRF structure itself. */
1275 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1276 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1277 vrf_id = (vrf_id_t)ifi->ifi_index;
1278 }
1279
1280 /* See if interface is present. */
1281 ifp = if_lookup_by_name_per_ns(zns, name);
1282
1283 if (h->nlmsg_type == RTM_NEWLINK) {
1284 if (tb[IFLA_MASTER]) {
1285 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1286 && !vrf_is_backend_netns()) {
1287 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
1288 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
1289 } else if (slave_kind
1290 && (strcmp(slave_kind, "bridge") == 0)) {
1291 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1292 bridge_ifindex =
1293 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1294 } else if (slave_kind
1295 && (strcmp(slave_kind, "bond") == 0)) {
1296 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1297 bond_ifindex =
1298 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1299 } else
1300 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1301 }
1302 if (vrf_is_backend_netns())
1303 vrf_id = (vrf_id_t)ns_id;
1304 if (ifp == NULL
1305 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1306 /* Add interface notification from kernel */
1307 if (IS_ZEBRA_DEBUG_KERNEL)
1308 zlog_debug(
1309 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1310 name, ifi->ifi_index, vrf_id, zif_type,
1311 zif_slave_type, bridge_ifindex,
1312 ifi->ifi_flags);
1313
1314 if (ifp == NULL) {
1315 /* unknown interface */
1316 ifp = if_get_by_name(name, vrf_id);
1317 } else {
1318 /* pre-configured interface, learnt now */
1319 if (ifp->vrf_id != vrf_id)
1320 if_update_to_new_vrf(ifp, vrf_id);
1321 }
1322
1323 /* Update interface information. */
1324 set_ifindex(ifp, ifi->ifi_index, zns);
1325 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1326 if (!tb[IFLA_MTU]) {
1327 zlog_debug(
1328 "RTM_NEWLINK for interface %s(%u) without MTU set",
1329 name, ifi->ifi_index);
1330 return 0;
1331 }
1332 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1333 ifp->metric = 0;
1334 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1335
1336 /* Set interface type */
1337 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1338 if (IS_ZEBRA_IF_VRF(ifp))
1339 SET_FLAG(ifp->status,
1340 ZEBRA_INTERFACE_VRF_LOOPBACK);
1341
1342 /* Update link. */
1343 zebra_if_update_link(ifp, link_ifindex, ns_id);
1344
1345 netlink_interface_update_hw_addr(tb, ifp);
1346
1347 /* Inform clients, install any configured addresses. */
1348 if_add_update(ifp);
1349
1350 /* Extract and save L2 interface information, take
1351 * additional actions. */
1352 netlink_interface_update_l2info(
1353 ifp, linkinfo[IFLA_INFO_DATA],
1354 1, link_nsid);
1355 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1356 zebra_l2if_update_bridge_slave(ifp,
1357 bridge_ifindex,
1358 ns_id);
1359 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1360 zebra_l2if_update_bond_slave(ifp, bond_ifindex);
1361 } else if (ifp->vrf_id != vrf_id) {
1362 /* VRF change for an interface. */
1363 if (IS_ZEBRA_DEBUG_KERNEL)
1364 zlog_debug(
1365 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1366 name, ifp->ifindex, ifp->vrf_id, vrf_id,
1367 ifi->ifi_flags);
1368
1369 if_handle_vrf_change(ifp, vrf_id);
1370 } else {
1371 bool was_bridge_slave, was_bond_slave;
1372
1373 /* Interface update. */
1374 if (IS_ZEBRA_DEBUG_KERNEL)
1375 zlog_debug(
1376 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1377 name, ifp->ifindex, zif_slave_type,
1378 bridge_ifindex, ifi->ifi_flags);
1379
1380 set_ifindex(ifp, ifi->ifi_index, zns);
1381 if (!tb[IFLA_MTU]) {
1382 zlog_debug(
1383 "RTM_NEWLINK for interface %s(%u) without MTU set",
1384 name, ifi->ifi_index);
1385 return 0;
1386 }
1387 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1388 ifp->metric = 0;
1389
1390 /* Update interface type - NOTE: Only slave_type can
1391 * change. */
1392 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
1393 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
1394 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1395
1396 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
1397
1398 netlink_interface_update_hw_addr(tb, ifp);
1399
1400 if (if_is_no_ptm_operative(ifp)) {
1401 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1402 if (!if_is_no_ptm_operative(ifp)) {
1403 if (IS_ZEBRA_DEBUG_KERNEL)
1404 zlog_debug(
1405 "Intf %s(%u) has gone DOWN",
1406 name, ifp->ifindex);
1407 if_down(ifp);
1408 rib_update(RIB_UPDATE_KERNEL);
1409 } else if (if_is_operative(ifp)) {
1410 /* Must notify client daemons of new
1411 * interface status. */
1412 if (IS_ZEBRA_DEBUG_KERNEL)
1413 zlog_debug(
1414 "Intf %s(%u) PTM up, notifying clients",
1415 name, ifp->ifindex);
1416 zebra_interface_up_update(ifp);
1417
1418 /* Update EVPN VNI when SVI MAC change
1419 */
1420 if (IS_ZEBRA_IF_VLAN(ifp) &&
1421 memcmp(old_hw_addr, ifp->hw_addr,
1422 INTERFACE_HWADDR_MAX)) {
1423 struct interface *link_if;
1424
1425 link_if =
1426 if_lookup_by_index_per_ns(
1427 zebra_ns_lookup(NS_DEFAULT),
1428 link_ifindex);
1429 if (link_if)
1430 zebra_vxlan_svi_up(ifp,
1431 link_if);
1432 }
1433 }
1434 } else {
1435 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1436 if (if_is_operative(ifp)) {
1437 if (IS_ZEBRA_DEBUG_KERNEL)
1438 zlog_debug(
1439 "Intf %s(%u) has come UP",
1440 name, ifp->ifindex);
1441 if_up(ifp);
1442 } else {
1443 if (IS_ZEBRA_DEBUG_KERNEL)
1444 zlog_debug(
1445 "Intf %s(%u) has gone DOWN",
1446 name, ifp->ifindex);
1447 if_down(ifp);
1448 rib_update(RIB_UPDATE_KERNEL);
1449 }
1450 }
1451
1452 /* Extract and save L2 interface information, take
1453 * additional actions. */
1454 netlink_interface_update_l2info(
1455 ifp, linkinfo[IFLA_INFO_DATA],
1456 0, link_nsid);
1457 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
1458 zebra_l2if_update_bridge_slave(ifp,
1459 bridge_ifindex,
1460 ns_id);
1461 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
1462 zebra_l2if_update_bond_slave(ifp, bond_ifindex);
1463 }
1464
1465 zif = ifp->info;
1466 if (zif) {
1467 XFREE(MTYPE_TMP, zif->desc);
1468 if (desc)
1469 zif->desc = XSTRDUP(MTYPE_TMP, desc);
1470 }
1471 } else {
1472 /* Delete interface notification from kernel */
1473 if (ifp == NULL) {
1474 if (IS_ZEBRA_DEBUG_KERNEL)
1475 zlog_debug(
1476 "RTM_DELLINK for unknown interface %s(%u)",
1477 name, ifi->ifi_index);
1478 return 0;
1479 }
1480
1481 if (IS_ZEBRA_DEBUG_KERNEL)
1482 zlog_debug("RTM_DELLINK for %s(%u)", name,
1483 ifp->ifindex);
1484
1485 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
1486
1487 /* Special handling for bridge or VxLAN interfaces. */
1488 if (IS_ZEBRA_IF_BRIDGE(ifp))
1489 zebra_l2_bridge_del(ifp);
1490 else if (IS_ZEBRA_IF_VXLAN(ifp))
1491 zebra_l2_vxlanif_del(ifp);
1492
1493 if (!IS_ZEBRA_IF_VRF(ifp))
1494 if_delete_update(ifp);
1495 }
1496
1497 return 0;
1498 }
1499
1500 int netlink_protodown(struct interface *ifp, bool down)
1501 {
1502 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1503
1504 struct {
1505 struct nlmsghdr n;
1506 struct ifinfomsg ifa;
1507 char buf[NL_PKT_BUF_SIZE];
1508 } req;
1509
1510 memset(&req, 0, sizeof(req));
1511
1512 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1513 req.n.nlmsg_flags = NLM_F_REQUEST;
1514 req.n.nlmsg_type = RTM_SETLINK;
1515 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1516
1517 req.ifa.ifi_index = ifp->ifindex;
1518
1519 nl_attr_put(&req.n, sizeof(req), IFLA_PROTO_DOWN, &down, sizeof(down));
1520 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, ifp->ifindex);
1521
1522 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1523 0);
1524 }
1525
1526 /* Interface information read by netlink. */
1527 void interface_list(struct zebra_ns *zns)
1528 {
1529 interface_lookup_netlink(zns);
1530 /* We add routes for interface address,
1531 * so we need to get the nexthop info
1532 * from the kernel before we can do that
1533 */
1534 netlink_nexthop_read(zns);
1535
1536 interface_addr_lookup_netlink(zns);
1537 }
1538
1539 #endif /* GNU_LINUX */