]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_netlink.c
zebra: add command for setting protodown bit
[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 #define _LINUX_IF_H
33 #define _LINUX_IP_H
34
35 #include <netinet/if_ether.h>
36 #include <linux/if_bridge.h>
37 #include <linux/if_link.h>
38 #include <linux/if_tunnel.h>
39 #include <net/if_arp.h>
40 #include <linux/sockios.h>
41 #include <linux/ethtool.h>
42
43 #include "linklist.h"
44 #include "if.h"
45 #include "log.h"
46 #include "prefix.h"
47 #include "connected.h"
48 #include "table.h"
49 #include "memory.h"
50 #include "rib.h"
51 #include "thread.h"
52 #include "privs.h"
53 #include "nexthop.h"
54 #include "vrf.h"
55 #include "vrf_int.h"
56 #include "mpls.h"
57 #include "lib_errors.h"
58
59 #include "vty.h"
60 #include "zebra/zserv.h"
61 #include "zebra/zebra_ns.h"
62 #include "zebra/zebra_vrf.h"
63 #include "zebra/rt.h"
64 #include "zebra/redistribute.h"
65 #include "zebra/interface.h"
66 #include "zebra/debug.h"
67 #include "zebra/rtadv.h"
68 #include "zebra/zebra_ptm.h"
69 #include "zebra/zebra_mpls.h"
70 #include "zebra/kernel_netlink.h"
71 #include "zebra/rt_netlink.h"
72 #include "zebra/if_netlink.h"
73 #include "zebra/zebra_errors.h"
74 #include "zebra/zebra_vxlan.h"
75 #include "zebra/zebra_evpn_mh.h"
76 #include "zebra/zebra_l2.h"
77 #include "zebra/netconf_netlink.h"
78
79 extern struct zebra_privs_t zserv_privs;
80 uint8_t frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
81
82 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
83 names and ifindex values. */
84 static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
85 struct zebra_ns *zns)
86 {
87 struct interface *oifp;
88
89 if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
90 && (oifp != ifp)) {
91 if (ifi_index == IFINDEX_INTERNAL)
92 flog_err(
93 EC_LIB_INTERFACE,
94 "Netlink is setting interface %s ifindex to reserved internal value %u",
95 ifp->name, ifi_index);
96 else {
97 if (IS_ZEBRA_DEBUG_KERNEL)
98 zlog_debug(
99 "interface index %d was renamed from %s to %s",
100 ifi_index, oifp->name, ifp->name);
101 if (if_is_up(oifp))
102 flog_err(
103 EC_LIB_INTERFACE,
104 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
105 ifi_index, oifp->name, ifp->name);
106 if_delete_update(oifp);
107 }
108 }
109 if_set_index(ifp, ifi_index);
110 }
111
112 /* Utility function to parse hardware link-layer address and update ifp */
113 static void netlink_interface_update_hw_addr(struct rtattr **tb,
114 struct interface *ifp)
115 {
116 int i;
117
118 if (tb[IFLA_ADDRESS]) {
119 int hw_addr_len;
120
121 hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
122
123 if (hw_addr_len > INTERFACE_HWADDR_MAX)
124 zlog_debug("Hardware address is too large: %d",
125 hw_addr_len);
126 else {
127 ifp->hw_addr_len = hw_addr_len;
128 memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
129 hw_addr_len);
130
131 for (i = 0; i < hw_addr_len; i++)
132 if (ifp->hw_addr[i] != 0)
133 break;
134
135 if (i == hw_addr_len)
136 ifp->hw_addr_len = 0;
137 else
138 ifp->hw_addr_len = hw_addr_len;
139 }
140 }
141 }
142
143 static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
144 {
145 switch (hwt) {
146 case ARPHRD_ETHER:
147 return ZEBRA_LLT_ETHER;
148 case ARPHRD_EETHER:
149 return ZEBRA_LLT_EETHER;
150 case ARPHRD_AX25:
151 return ZEBRA_LLT_AX25;
152 case ARPHRD_PRONET:
153 return ZEBRA_LLT_PRONET;
154 case ARPHRD_IEEE802:
155 return ZEBRA_LLT_IEEE802;
156 case ARPHRD_ARCNET:
157 return ZEBRA_LLT_ARCNET;
158 case ARPHRD_APPLETLK:
159 return ZEBRA_LLT_APPLETLK;
160 case ARPHRD_DLCI:
161 return ZEBRA_LLT_DLCI;
162 case ARPHRD_ATM:
163 return ZEBRA_LLT_ATM;
164 case ARPHRD_METRICOM:
165 return ZEBRA_LLT_METRICOM;
166 case ARPHRD_IEEE1394:
167 return ZEBRA_LLT_IEEE1394;
168 case ARPHRD_EUI64:
169 return ZEBRA_LLT_EUI64;
170 case ARPHRD_INFINIBAND:
171 return ZEBRA_LLT_INFINIBAND;
172 case ARPHRD_SLIP:
173 return ZEBRA_LLT_SLIP;
174 case ARPHRD_CSLIP:
175 return ZEBRA_LLT_CSLIP;
176 case ARPHRD_SLIP6:
177 return ZEBRA_LLT_SLIP6;
178 case ARPHRD_CSLIP6:
179 return ZEBRA_LLT_CSLIP6;
180 case ARPHRD_RSRVD:
181 return ZEBRA_LLT_RSRVD;
182 case ARPHRD_ADAPT:
183 return ZEBRA_LLT_ADAPT;
184 case ARPHRD_ROSE:
185 return ZEBRA_LLT_ROSE;
186 case ARPHRD_X25:
187 return ZEBRA_LLT_X25;
188 case ARPHRD_PPP:
189 return ZEBRA_LLT_PPP;
190 case ARPHRD_CISCO:
191 return ZEBRA_LLT_CHDLC;
192 case ARPHRD_LAPB:
193 return ZEBRA_LLT_LAPB;
194 case ARPHRD_RAWHDLC:
195 return ZEBRA_LLT_RAWHDLC;
196 case ARPHRD_TUNNEL:
197 return ZEBRA_LLT_IPIP;
198 case ARPHRD_TUNNEL6:
199 return ZEBRA_LLT_IPIP6;
200 case ARPHRD_FRAD:
201 return ZEBRA_LLT_FRAD;
202 case ARPHRD_SKIP:
203 return ZEBRA_LLT_SKIP;
204 case ARPHRD_LOOPBACK:
205 return ZEBRA_LLT_LOOPBACK;
206 case ARPHRD_LOCALTLK:
207 return ZEBRA_LLT_LOCALTLK;
208 case ARPHRD_FDDI:
209 return ZEBRA_LLT_FDDI;
210 case ARPHRD_SIT:
211 return ZEBRA_LLT_SIT;
212 case ARPHRD_IPDDP:
213 return ZEBRA_LLT_IPDDP;
214 case ARPHRD_IPGRE:
215 return ZEBRA_LLT_IPGRE;
216 case ARPHRD_PIMREG:
217 return ZEBRA_LLT_PIMREG;
218 case ARPHRD_HIPPI:
219 return ZEBRA_LLT_HIPPI;
220 case ARPHRD_ECONET:
221 return ZEBRA_LLT_ECONET;
222 case ARPHRD_IRDA:
223 return ZEBRA_LLT_IRDA;
224 case ARPHRD_FCPP:
225 return ZEBRA_LLT_FCPP;
226 case ARPHRD_FCAL:
227 return ZEBRA_LLT_FCAL;
228 case ARPHRD_FCPL:
229 return ZEBRA_LLT_FCPL;
230 case ARPHRD_FCFABRIC:
231 return ZEBRA_LLT_FCFABRIC;
232 case ARPHRD_IEEE802_TR:
233 return ZEBRA_LLT_IEEE802_TR;
234 case ARPHRD_IEEE80211:
235 return ZEBRA_LLT_IEEE80211;
236 #ifdef ARPHRD_IEEE802154
237 case ARPHRD_IEEE802154:
238 return ZEBRA_LLT_IEEE802154;
239 #endif
240 #ifdef ARPHRD_IP6GRE
241 case ARPHRD_IP6GRE:
242 return ZEBRA_LLT_IP6GRE;
243 #endif
244 #ifdef ARPHRD_IEEE802154_PHY
245 case ARPHRD_IEEE802154_PHY:
246 return ZEBRA_LLT_IEEE802154_PHY;
247 #endif
248
249 default:
250 return ZEBRA_LLT_UNKNOWN;
251 }
252 }
253
254 static inline void zebra_if_set_ziftype(struct interface *ifp,
255 enum zebra_iftype zif_type,
256 enum zebra_slave_iftype zif_slave_type)
257 {
258 struct zebra_if *zif;
259
260 zif = (struct zebra_if *)ifp->info;
261 zif->zif_slave_type = zif_slave_type;
262
263 if (zif->zif_type != zif_type) {
264 zif->zif_type = zif_type;
265 /* If the if_type has been set to bond initialize ES info
266 * against it. XXX - note that we don't handle the case where
267 * a zif changes from bond to non-bond; it is really
268 * an unexpected/error condition.
269 */
270 zebra_evpn_if_init(zif);
271 }
272 }
273
274 static void netlink_determine_zebra_iftype(const char *kind,
275 enum zebra_iftype *zif_type)
276 {
277 *zif_type = ZEBRA_IF_OTHER;
278
279 if (!kind)
280 return;
281
282 if (strcmp(kind, "vrf") == 0)
283 *zif_type = ZEBRA_IF_VRF;
284 else if (strcmp(kind, "bridge") == 0)
285 *zif_type = ZEBRA_IF_BRIDGE;
286 else if (strcmp(kind, "vlan") == 0)
287 *zif_type = ZEBRA_IF_VLAN;
288 else if (strcmp(kind, "vxlan") == 0)
289 *zif_type = ZEBRA_IF_VXLAN;
290 else if (strcmp(kind, "macvlan") == 0)
291 *zif_type = ZEBRA_IF_MACVLAN;
292 else if (strcmp(kind, "veth") == 0)
293 *zif_type = ZEBRA_IF_VETH;
294 else if (strcmp(kind, "bond") == 0)
295 *zif_type = ZEBRA_IF_BOND;
296 else if (strcmp(kind, "bond_slave") == 0)
297 *zif_type = ZEBRA_IF_BOND_SLAVE;
298 else if (strcmp(kind, "gre") == 0)
299 *zif_type = ZEBRA_IF_GRE;
300 }
301
302 static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
303 uint32_t ns_id, const char *name)
304 {
305 struct ifinfomsg *ifi;
306 struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
307 struct rtattr *attr[IFLA_VRF_MAX + 1];
308 struct vrf *vrf = NULL;
309 struct zebra_vrf *zvrf;
310 uint32_t nl_table_id;
311
312 ifi = NLMSG_DATA(h);
313
314 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
315
316 if (!linkinfo[IFLA_INFO_DATA]) {
317 if (IS_ZEBRA_DEBUG_KERNEL)
318 zlog_debug(
319 "%s: IFLA_INFO_DATA missing from VRF message: %s",
320 __func__, name);
321 return;
322 }
323
324 netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
325 linkinfo[IFLA_INFO_DATA]);
326 if (!attr[IFLA_VRF_TABLE]) {
327 if (IS_ZEBRA_DEBUG_KERNEL)
328 zlog_debug(
329 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
330 __func__, name);
331 return;
332 }
333
334 nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
335
336 if (h->nlmsg_type == RTM_NEWLINK) {
337 if (IS_ZEBRA_DEBUG_KERNEL)
338 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
339 ifi->ifi_index, nl_table_id);
340
341 if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
342 vrf_id_t exist_id;
343
344 exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
345 if (exist_id != VRF_DEFAULT) {
346 vrf = vrf_lookup_by_id(exist_id);
347
348 flog_err(
349 EC_ZEBRA_VRF_MISCONFIGURED,
350 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
351 name, ifi->ifi_index, vrf->name);
352 exit(-1);
353 }
354 }
355
356 vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
357 if (!vrf) {
358 flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
359 name, ifi->ifi_index);
360 return;
361 }
362
363 /*
364 * This is the only place that we get the actual kernel table_id
365 * being used. We need it to set the table_id of the routes
366 * we are passing to the kernel.... And to throw some totally
367 * awesome parties. that too.
368 *
369 * At this point we *must* have a zvrf because the vrf_create
370 * callback creates one. We *must* set the table id
371 * before the vrf_enable because of( at the very least )
372 * static routes being delayed for installation until
373 * during the vrf_enable callbacks.
374 */
375 zvrf = (struct zebra_vrf *)vrf->info;
376 zvrf->table_id = nl_table_id;
377
378 /* Enable the created VRF. */
379 if (!vrf_enable(vrf)) {
380 flog_err(EC_LIB_INTERFACE,
381 "Failed to enable VRF %s id %u", name,
382 ifi->ifi_index);
383 return;
384 }
385
386 } else // h->nlmsg_type == RTM_DELLINK
387 {
388 if (IS_ZEBRA_DEBUG_KERNEL)
389 zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
390 ifi->ifi_index);
391
392 vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
393
394 if (!vrf) {
395 flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
396 __func__);
397 return;
398 }
399
400 vrf_delete(vrf);
401 }
402 }
403
404 static uint32_t get_iflink_speed(struct interface *interface, int *error)
405 {
406 struct ifreq ifdata;
407 struct ethtool_cmd ecmd;
408 int sd;
409 int rc;
410 const char *ifname = interface->name;
411
412 if (error)
413 *error = 0;
414 /* initialize struct */
415 memset(&ifdata, 0, sizeof(ifdata));
416
417 /* set interface name */
418 strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
419
420 /* initialize ethtool interface */
421 memset(&ecmd, 0, sizeof(ecmd));
422 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
423 ifdata.ifr_data = (caddr_t)&ecmd;
424
425 /* use ioctl to get IP address of an interface */
426 frr_with_privs(&zserv_privs) {
427 sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
428 interface->vrf->vrf_id, NULL);
429 if (sd < 0) {
430 if (IS_ZEBRA_DEBUG_KERNEL)
431 zlog_debug("Failure to read interface %s speed: %d %s",
432 ifname, errno, safe_strerror(errno));
433 /* no vrf socket creation may probably mean vrf issue */
434 if (error)
435 *error = -1;
436 return 0;
437 }
438 /* Get the current link state for the interface */
439 rc = vrf_ioctl(interface->vrf->vrf_id, sd, SIOCETHTOOL,
440 (char *)&ifdata);
441 }
442 if (rc < 0) {
443 if (errno != EOPNOTSUPP && IS_ZEBRA_DEBUG_KERNEL)
444 zlog_debug(
445 "IOCTL failure to read interface %s speed: %d %s",
446 ifname, errno, safe_strerror(errno));
447 /* no device means interface unreachable */
448 if (errno == ENODEV && error)
449 *error = -1;
450 ecmd.speed_hi = 0;
451 ecmd.speed = 0;
452 }
453
454 close(sd);
455
456 return ((uint32_t)ecmd.speed_hi << 16) | ecmd.speed;
457 }
458
459 uint32_t kernel_get_speed(struct interface *ifp, int *error)
460 {
461 return get_iflink_speed(ifp, error);
462 }
463
464 static ssize_t
465 netlink_gre_set_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
466 size_t buflen)
467 {
468 struct {
469 struct nlmsghdr n;
470 struct ifinfomsg ifi;
471 char buf[];
472 } *req = buf;
473 uint32_t link_idx;
474 unsigned int mtu;
475 struct rtattr *rta_info, *rta_data;
476 const struct zebra_l2info_gre *gre_info;
477
478 if (buflen < sizeof(*req))
479 return 0;
480 memset(req, 0, sizeof(*req));
481
482 req->n.nlmsg_type = RTM_NEWLINK;
483 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
484 req->n.nlmsg_flags = NLM_F_REQUEST;
485
486 req->ifi.ifi_index = dplane_ctx_get_ifindex(ctx);
487
488 gre_info = dplane_ctx_gre_get_info(ctx);
489 if (!gre_info)
490 return 0;
491
492 req->ifi.ifi_change = 0xFFFFFFFF;
493 link_idx = dplane_ctx_gre_get_link_ifindex(ctx);
494 mtu = dplane_ctx_gre_get_mtu(ctx);
495
496 if (mtu && !nl_attr_put32(&req->n, buflen, IFLA_MTU, mtu))
497 return 0;
498
499 rta_info = nl_attr_nest(&req->n, buflen, IFLA_LINKINFO);
500 if (!rta_info)
501 return 0;
502
503 if (!nl_attr_put(&req->n, buflen, IFLA_INFO_KIND, "gre", 3))
504 return 0;
505
506 rta_data = nl_attr_nest(&req->n, buflen, IFLA_INFO_DATA);
507 if (!rta_data)
508 return 0;
509
510 if (!nl_attr_put32(&req->n, buflen, IFLA_GRE_LINK, link_idx))
511 return 0;
512
513 if (gre_info->vtep_ip.s_addr &&
514 !nl_attr_put32(&req->n, buflen, IFLA_GRE_LOCAL,
515 gre_info->vtep_ip.s_addr))
516 return 0;
517
518 if (gre_info->vtep_ip_remote.s_addr &&
519 !nl_attr_put32(&req->n, buflen, IFLA_GRE_REMOTE,
520 gre_info->vtep_ip_remote.s_addr))
521 return 0;
522
523 if (gre_info->ikey &&
524 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
525 gre_info->ikey))
526 return 0;
527 if (gre_info->okey &&
528 !nl_attr_put32(&req->n, buflen, IFLA_GRE_IKEY,
529 gre_info->okey))
530 return 0;
531
532 nl_attr_nest_end(&req->n, rta_data);
533 nl_attr_nest_end(&req->n, rta_info);
534
535 return NLMSG_ALIGN(req->n.nlmsg_len);
536 }
537
538 static int netlink_extract_bridge_info(struct rtattr *link_data,
539 struct zebra_l2info_bridge *bridge_info)
540 {
541 struct rtattr *attr[IFLA_BR_MAX + 1];
542
543 memset(bridge_info, 0, sizeof(*bridge_info));
544 netlink_parse_rtattr_nested(attr, IFLA_BR_MAX, link_data);
545 if (attr[IFLA_BR_VLAN_FILTERING])
546 bridge_info->vlan_aware =
547 *(uint8_t *)RTA_DATA(attr[IFLA_BR_VLAN_FILTERING]);
548 return 0;
549 }
550
551 static int netlink_extract_vlan_info(struct rtattr *link_data,
552 struct zebra_l2info_vlan *vlan_info)
553 {
554 struct rtattr *attr[IFLA_VLAN_MAX + 1];
555 vlanid_t vid_in_msg;
556
557 memset(vlan_info, 0, sizeof(*vlan_info));
558 netlink_parse_rtattr_nested(attr, IFLA_VLAN_MAX, link_data);
559 if (!attr[IFLA_VLAN_ID]) {
560 if (IS_ZEBRA_DEBUG_KERNEL)
561 zlog_debug("IFLA_VLAN_ID missing from VLAN IF message");
562 return -1;
563 }
564
565 vid_in_msg = *(vlanid_t *)RTA_DATA(attr[IFLA_VLAN_ID]);
566 vlan_info->vid = vid_in_msg;
567 return 0;
568 }
569
570 static int netlink_extract_gre_info(struct rtattr *link_data,
571 struct zebra_l2info_gre *gre_info)
572 {
573 struct rtattr *attr[IFLA_GRE_MAX + 1];
574
575 memset(gre_info, 0, sizeof(*gre_info));
576 memset(attr, 0, sizeof(attr));
577 netlink_parse_rtattr_nested(attr, IFLA_GRE_MAX, link_data);
578
579 if (!attr[IFLA_GRE_LOCAL]) {
580 if (IS_ZEBRA_DEBUG_KERNEL)
581 zlog_debug(
582 "IFLA_GRE_LOCAL missing from GRE IF message");
583 } else
584 gre_info->vtep_ip =
585 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_LOCAL]);
586 if (!attr[IFLA_GRE_REMOTE]) {
587 if (IS_ZEBRA_DEBUG_KERNEL)
588 zlog_debug(
589 "IFLA_GRE_REMOTE missing from GRE IF message");
590 } else
591 gre_info->vtep_ip_remote =
592 *(struct in_addr *)RTA_DATA(attr[IFLA_GRE_REMOTE]);
593
594 if (!attr[IFLA_GRE_LINK]) {
595 if (IS_ZEBRA_DEBUG_KERNEL)
596 zlog_debug("IFLA_GRE_LINK missing from GRE IF message");
597 } else {
598 gre_info->ifindex_link =
599 *(ifindex_t *)RTA_DATA(attr[IFLA_GRE_LINK]);
600 if (IS_ZEBRA_DEBUG_KERNEL)
601 zlog_debug("IFLA_GRE_LINK obtained is %u",
602 gre_info->ifindex_link);
603 }
604 if (attr[IFLA_GRE_IKEY])
605 gre_info->ikey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_IKEY]);
606 if (attr[IFLA_GRE_OKEY])
607 gre_info->okey = *(uint32_t *)RTA_DATA(attr[IFLA_GRE_OKEY]);
608 return 0;
609 }
610
611 static int netlink_extract_vxlan_info(struct rtattr *link_data,
612 struct zebra_l2info_vxlan *vxl_info)
613 {
614 struct rtattr *attr[IFLA_VXLAN_MAX + 1];
615 vni_t vni_in_msg;
616 struct in_addr vtep_ip_in_msg;
617 ifindex_t ifindex_link;
618
619 memset(vxl_info, 0, sizeof(*vxl_info));
620 netlink_parse_rtattr_nested(attr, IFLA_VXLAN_MAX, link_data);
621 if (!attr[IFLA_VXLAN_ID]) {
622 if (IS_ZEBRA_DEBUG_KERNEL)
623 zlog_debug(
624 "IFLA_VXLAN_ID missing from VXLAN IF message");
625 return -1;
626 }
627
628 vni_in_msg = *(vni_t *)RTA_DATA(attr[IFLA_VXLAN_ID]);
629 vxl_info->vni = vni_in_msg;
630 if (!attr[IFLA_VXLAN_LOCAL]) {
631 if (IS_ZEBRA_DEBUG_KERNEL)
632 zlog_debug(
633 "IFLA_VXLAN_LOCAL missing from VXLAN IF message");
634 } else {
635 vtep_ip_in_msg =
636 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_LOCAL]);
637 vxl_info->vtep_ip = vtep_ip_in_msg;
638 }
639
640 if (attr[IFLA_VXLAN_GROUP]) {
641 vxl_info->mcast_grp =
642 *(struct in_addr *)RTA_DATA(attr[IFLA_VXLAN_GROUP]);
643 }
644
645 if (!attr[IFLA_VXLAN_LINK]) {
646 if (IS_ZEBRA_DEBUG_KERNEL)
647 zlog_debug("IFLA_VXLAN_LINK missing from VXLAN IF message");
648 } else {
649 ifindex_link =
650 *(ifindex_t *)RTA_DATA(attr[IFLA_VXLAN_LINK]);
651 vxl_info->ifindex_link = ifindex_link;
652 }
653 return 0;
654 }
655
656 /*
657 * Extract and save L2 params (of interest) for an interface. When a
658 * bridge interface is added or updated, take further actions to map
659 * its members. Likewise, for VxLAN interface.
660 */
661 static void netlink_interface_update_l2info(struct interface *ifp,
662 struct rtattr *link_data, int add,
663 ns_id_t link_nsid)
664 {
665 if (!link_data)
666 return;
667
668 if (IS_ZEBRA_IF_BRIDGE(ifp)) {
669 struct zebra_l2info_bridge bridge_info;
670
671 netlink_extract_bridge_info(link_data, &bridge_info);
672 zebra_l2_bridge_add_update(ifp, &bridge_info, add);
673 } else if (IS_ZEBRA_IF_VLAN(ifp)) {
674 struct zebra_l2info_vlan vlan_info;
675
676 netlink_extract_vlan_info(link_data, &vlan_info);
677 zebra_l2_vlanif_update(ifp, &vlan_info);
678 zebra_evpn_acc_bd_svi_set(ifp->info, NULL,
679 !!if_is_operative(ifp));
680 } else if (IS_ZEBRA_IF_VXLAN(ifp)) {
681 struct zebra_l2info_vxlan vxlan_info;
682
683 netlink_extract_vxlan_info(link_data, &vxlan_info);
684 vxlan_info.link_nsid = link_nsid;
685 zebra_l2_vxlanif_add_update(ifp, &vxlan_info, add);
686 if (link_nsid != NS_UNKNOWN &&
687 vxlan_info.ifindex_link)
688 zebra_if_update_link(ifp, vxlan_info.ifindex_link,
689 link_nsid);
690 } else if (IS_ZEBRA_IF_GRE(ifp)) {
691 struct zebra_l2info_gre gre_info;
692
693 netlink_extract_gre_info(link_data, &gre_info);
694 gre_info.link_nsid = link_nsid;
695 zebra_l2_greif_add_update(ifp, &gre_info, add);
696 if (link_nsid != NS_UNKNOWN &&
697 gre_info.ifindex_link)
698 zebra_if_update_link(ifp, gre_info.ifindex_link,
699 link_nsid);
700 }
701 }
702
703 static int netlink_bridge_vxlan_update(struct interface *ifp,
704 struct rtattr *af_spec)
705 {
706 struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
707 struct bridge_vlan_info *vinfo;
708 vlanid_t access_vlan;
709
710 if (!af_spec)
711 return 0;
712
713 /* There is a 1-to-1 mapping of VLAN to VxLAN - hence
714 * only 1 access VLAN is accepted.
715 */
716 netlink_parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, af_spec);
717 if (!aftb[IFLA_BRIDGE_VLAN_INFO])
718 return 0;
719
720 vinfo = RTA_DATA(aftb[IFLA_BRIDGE_VLAN_INFO]);
721 if (!(vinfo->flags & BRIDGE_VLAN_INFO_PVID))
722 return 0;
723
724 access_vlan = (vlanid_t)vinfo->vid;
725 if (IS_ZEBRA_DEBUG_KERNEL)
726 zlog_debug("Access VLAN %u for VxLAN IF %s(%u)", access_vlan,
727 ifp->name, ifp->ifindex);
728 zebra_l2_vxlanif_update_access_vlan(ifp, access_vlan);
729 return 0;
730 }
731
732 static void netlink_bridge_vlan_update(struct interface *ifp,
733 struct rtattr *af_spec)
734 {
735 struct rtattr *i;
736 int rem;
737 uint16_t vid_range_start = 0;
738 struct zebra_if *zif;
739 bitfield_t old_vlan_bitmap;
740 struct bridge_vlan_info *vinfo;
741
742 zif = (struct zebra_if *)ifp->info;
743
744 /* cache the old bitmap addrs */
745 old_vlan_bitmap = zif->vlan_bitmap;
746 /* create a new bitmap space for re-eval */
747 bf_init(zif->vlan_bitmap, IF_VLAN_BITMAP_MAX);
748
749 if (af_spec) {
750 for (i = RTA_DATA(af_spec), rem = RTA_PAYLOAD(af_spec);
751 RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
752
753 if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
754 continue;
755
756 vinfo = RTA_DATA(i);
757
758 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
759 vid_range_start = vinfo->vid;
760 continue;
761 }
762
763 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
764 vid_range_start = vinfo->vid;
765
766 zebra_vlan_bitmap_compute(ifp, vid_range_start,
767 vinfo->vid);
768 }
769 }
770
771 zebra_vlan_mbr_re_eval(ifp, old_vlan_bitmap);
772
773 bf_free(old_vlan_bitmap);
774 }
775
776 static int netlink_bridge_interface(struct nlmsghdr *h, int len, ns_id_t ns_id,
777 int startup)
778 {
779 char *name = NULL;
780 struct ifinfomsg *ifi;
781 struct rtattr *tb[IFLA_MAX + 1];
782 struct interface *ifp;
783 struct zebra_if *zif;
784 struct rtattr *af_spec;
785
786 /* Fetch name and ifindex */
787 ifi = NLMSG_DATA(h);
788 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
789
790 if (tb[IFLA_IFNAME] == NULL)
791 return -1;
792 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
793
794 /* The interface should already be known, if not discard. */
795 ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), ifi->ifi_index);
796 if (!ifp) {
797 zlog_debug("Cannot find bridge IF %s(%u)", name,
798 ifi->ifi_index);
799 return 0;
800 }
801
802 /* We are only interested in the access VLAN i.e., AF_SPEC */
803 af_spec = tb[IFLA_AF_SPEC];
804
805 if (IS_ZEBRA_IF_VXLAN(ifp))
806 return netlink_bridge_vxlan_update(ifp, af_spec);
807
808 /* build vlan bitmap associated with this interface if that
809 * device type is interested in the vlans
810 */
811 zif = (struct zebra_if *)ifp->info;
812 if (bf_is_inited(zif->vlan_bitmap))
813 netlink_bridge_vlan_update(ifp, af_spec);
814
815 return 0;
816 }
817
818 /* If the interface is an es bond member then it must follow EVPN's
819 * protodown setting
820 */
821 static void netlink_proc_dplane_if_protodown(struct zebra_if *zif,
822 bool protodown)
823 {
824 bool zif_protodown;
825
826 /* Set our reason code to note it wasn't us */
827 if (protodown)
828 zif->protodown_rc |= ZEBRA_PROTODOWN_EXTERNAL;
829 else
830 zif->protodown_rc &= ~ZEBRA_PROTODOWN_EXTERNAL;
831
832 zif_protodown = !!(zif->flags & ZIF_FLAG_PROTODOWN);
833 if (protodown == zif_protodown)
834 return;
835
836 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
837 zlog_debug("interface %s dplane change, protdown %s",
838 zif->ifp->name, protodown ? "on" : "off");
839
840 if (zebra_evpn_is_es_bond_member(zif->ifp)) {
841 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_KERNEL)
842 zlog_debug(
843 "bond mbr %s re-instate protdown %s in the dplane",
844 zif->ifp->name, zif_protodown ? "on" : "off");
845 netlink_protodown(zif->ifp, zif_protodown, zif->protodown_rc);
846 } else {
847 if (protodown)
848 zif->flags |= ZIF_FLAG_PROTODOWN;
849 else
850 zif->flags &= ~ZIF_FLAG_PROTODOWN;
851 }
852 }
853
854 static uint8_t netlink_parse_lacp_bypass(struct rtattr **linkinfo)
855 {
856 uint8_t bypass = 0;
857 struct rtattr *mbrinfo[IFLA_BOND_SLAVE_MAX + 1];
858
859 netlink_parse_rtattr_nested(mbrinfo, IFLA_BOND_SLAVE_MAX,
860 linkinfo[IFLA_INFO_SLAVE_DATA]);
861 if (mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS])
862 bypass = *(uint8_t *)RTA_DATA(
863 mbrinfo[IFLA_BOND_SLAVE_AD_RX_BYPASS]);
864
865 return bypass;
866 }
867
868 /*
869 * Called from interface_lookup_netlink(). This function is only used
870 * during bootstrap.
871 */
872 static int netlink_interface(struct nlmsghdr *h, ns_id_t ns_id, int startup)
873 {
874 int len;
875 struct ifinfomsg *ifi;
876 struct rtattr *tb[IFLA_MAX + 1];
877 struct rtattr *linkinfo[IFLA_MAX + 1];
878 struct interface *ifp;
879 char *name = NULL;
880 char *kind = NULL;
881 char *desc = NULL;
882 char *slave_kind = NULL;
883 struct zebra_ns *zns = NULL;
884 vrf_id_t vrf_id = VRF_DEFAULT;
885 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
886 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
887 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
888 ifindex_t link_ifindex = IFINDEX_INTERNAL;
889 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
890 struct zebra_if *zif;
891 ns_id_t link_nsid = ns_id;
892 uint8_t bypass = 0;
893
894 zns = zebra_ns_lookup(ns_id);
895 ifi = NLMSG_DATA(h);
896
897 if (h->nlmsg_type != RTM_NEWLINK)
898 return 0;
899
900 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
901 if (len < 0) {
902 zlog_err(
903 "%s: Message received from netlink is of a broken size: %d %zu",
904 __func__, h->nlmsg_len,
905 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
906 return -1;
907 }
908
909 /* We are interested in some AF_BRIDGE notifications. */
910 if (ifi->ifi_family == AF_BRIDGE)
911 return netlink_bridge_interface(h, len, ns_id, startup);
912
913 /* Looking up interface name. */
914 memset(linkinfo, 0, sizeof(linkinfo));
915 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
916
917 /* check for wireless messages to ignore */
918 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
919 if (IS_ZEBRA_DEBUG_KERNEL)
920 zlog_debug("%s: ignoring IFLA_WIRELESS message",
921 __func__);
922 return 0;
923 }
924
925 if (tb[IFLA_IFNAME] == NULL)
926 return -1;
927 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
928
929 if (tb[IFLA_IFALIAS])
930 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
931
932 if (tb[IFLA_LINKINFO]) {
933 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
934 tb[IFLA_LINKINFO]);
935
936 if (linkinfo[IFLA_INFO_KIND])
937 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
938
939 if (linkinfo[IFLA_INFO_SLAVE_KIND])
940 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
941
942 if ((slave_kind != NULL) && strcmp(slave_kind, "bond") == 0)
943 netlink_determine_zebra_iftype("bond_slave", &zif_type);
944 else
945 netlink_determine_zebra_iftype(kind, &zif_type);
946 }
947
948 /* If VRF, create the VRF structure itself. */
949 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
950 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
951 vrf_id = (vrf_id_t)ifi->ifi_index;
952 }
953
954 if (tb[IFLA_MASTER]) {
955 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
956 && !vrf_is_backend_netns()) {
957 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
958 vrf_id = *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
959 } else if (slave_kind && (strcmp(slave_kind, "bridge") == 0)) {
960 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
961 bridge_ifindex =
962 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
963 } else if (slave_kind && (strcmp(slave_kind, "bond") == 0)) {
964 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
965 bond_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
966 bypass = netlink_parse_lacp_bypass(linkinfo);
967 } else
968 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
969 }
970 if (vrf_is_backend_netns())
971 vrf_id = (vrf_id_t)ns_id;
972
973 /* If linking to another interface, note it. */
974 if (tb[IFLA_LINK])
975 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
976
977 if (tb[IFLA_LINK_NETNSID]) {
978 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
979 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
980 }
981
982 ifp = if_get_by_name(name, vrf_id, NULL);
983 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
984
985 ifp->flags = ifi->ifi_flags & 0x0000fffff;
986 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
987 ifp->metric = 0;
988 ifp->speed = get_iflink_speed(ifp, NULL);
989 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
990
991 /* Set zebra interface type */
992 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
993 if (IS_ZEBRA_IF_VRF(ifp))
994 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
995
996 /*
997 * Just set the @link/lower-device ifindex. During nldump interfaces are
998 * not ordered in any fashion so we may end up getting upper devices
999 * before lower devices. We will setup the real linkage once the dump
1000 * is complete.
1001 */
1002 zif = (struct zebra_if *)ifp->info;
1003 zif->link_ifindex = link_ifindex;
1004
1005 if (desc) {
1006 XFREE(MTYPE_TMP, zif->desc);
1007 zif->desc = XSTRDUP(MTYPE_TMP, desc);
1008 }
1009
1010 /* Hardware type and address. */
1011 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
1012
1013 netlink_interface_update_hw_addr(tb, ifp);
1014
1015 if_add_update(ifp);
1016
1017 /* Extract and save L2 interface information, take additional actions.
1018 */
1019 netlink_interface_update_l2info(ifp, linkinfo[IFLA_INFO_DATA],
1020 1, link_nsid);
1021 if (IS_ZEBRA_IF_BOND(ifp))
1022 zebra_l2if_update_bond(ifp, true);
1023 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1024 zebra_l2if_update_bridge_slave(ifp, bridge_ifindex, ns_id,
1025 ZEBRA_BRIDGE_NO_ACTION);
1026 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1027 zebra_l2if_update_bond_slave(ifp, bond_ifindex, !!bypass);
1028
1029 if (tb[IFLA_PROTO_DOWN]) {
1030 uint8_t protodown;
1031
1032 protodown = *(uint8_t *)RTA_DATA(tb[IFLA_PROTO_DOWN]);
1033 netlink_proc_dplane_if_protodown(zif, !!protodown);
1034 }
1035
1036 return 0;
1037 }
1038
1039 /* Request for specific interface or address information from the kernel */
1040 static int netlink_request_intf_addr(struct nlsock *netlink_cmd, int family,
1041 int type, uint32_t filter_mask)
1042 {
1043 struct {
1044 struct nlmsghdr n;
1045 struct ifinfomsg ifm;
1046 char buf[256];
1047 } req;
1048
1049 /* Form the request, specifying filter (rtattr) if needed. */
1050 memset(&req, 0, sizeof(req));
1051 req.n.nlmsg_type = type;
1052 req.n.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
1053 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1054 req.ifm.ifi_family = family;
1055
1056 /* Include filter, if specified. */
1057 if (filter_mask)
1058 nl_attr_put32(&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
1059
1060 return netlink_request(netlink_cmd, &req);
1061 }
1062
1063 enum netlink_msg_status
1064 netlink_put_gre_set_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1065 {
1066 enum dplane_op_e op;
1067 enum netlink_msg_status ret;
1068
1069 op = dplane_ctx_get_op(ctx);
1070 assert(op == DPLANE_OP_GRE_SET);
1071
1072 ret = netlink_batch_add_msg(bth, ctx, netlink_gre_set_msg_encoder, false);
1073
1074 return ret;
1075 }
1076
1077 /* Interface lookup by netlink socket. */
1078 int interface_lookup_netlink(struct zebra_ns *zns)
1079 {
1080 int ret;
1081 struct zebra_dplane_info dp_info;
1082 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1083
1084 /* Capture key info from ns struct */
1085 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1086
1087 /* Get interface information. */
1088 ret = netlink_request_intf_addr(netlink_cmd, AF_PACKET, RTM_GETLINK, 0);
1089 if (ret < 0)
1090 return ret;
1091 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1092 true);
1093 if (ret < 0)
1094 return ret;
1095
1096 /* Get interface information - for bridge interfaces. */
1097 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
1098 RTEXT_FILTER_BRVLAN);
1099 if (ret < 0)
1100 return ret;
1101 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1102 true);
1103 if (ret < 0)
1104 return ret;
1105
1106 /* fixup linkages */
1107 zebra_if_update_all_links(zns);
1108 return 0;
1109 }
1110
1111 /**
1112 * interface_addr_lookup_netlink() - Look up interface addresses
1113 *
1114 * @zns: Zebra netlink socket
1115 * Return: Result status
1116 */
1117 static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1118 {
1119 int ret;
1120 struct zebra_dplane_info dp_info;
1121 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1122
1123 /* Capture key info from ns struct */
1124 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1125
1126 /* Get IPv4 address of the interfaces. */
1127 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
1128 if (ret < 0)
1129 return ret;
1130 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1131 0, true);
1132 if (ret < 0)
1133 return ret;
1134
1135 /* Get IPv6 address of the interfaces. */
1136 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
1137 if (ret < 0)
1138 return ret;
1139 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1140 0, true);
1141 if (ret < 0)
1142 return ret;
1143
1144 return 0;
1145 }
1146
1147 int kernel_interface_set_master(struct interface *master,
1148 struct interface *slave)
1149 {
1150 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1151
1152 struct {
1153 struct nlmsghdr n;
1154 struct ifinfomsg ifa;
1155 char buf[NL_PKT_BUF_SIZE];
1156 } req;
1157
1158 memset(&req, 0, sizeof(req));
1159
1160 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1161 req.n.nlmsg_flags = NLM_F_REQUEST;
1162 req.n.nlmsg_type = RTM_SETLINK;
1163 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1164
1165 req.ifa.ifi_index = slave->ifindex;
1166
1167 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1168 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
1169
1170 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1171 false);
1172 }
1173
1174 /* Interface address modification. */
1175 static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1176 void *buf, size_t buflen)
1177 {
1178 int bytelen;
1179 const struct prefix *p;
1180 int cmd;
1181 const char *label;
1182
1183 struct {
1184 struct nlmsghdr n;
1185 struct ifaddrmsg ifa;
1186 char buf[0];
1187 } *req = buf;
1188
1189 if (buflen < sizeof(*req))
1190 return 0;
1191
1192 p = dplane_ctx_get_intf_addr(ctx);
1193 memset(req, 0, sizeof(*req));
1194
1195 bytelen = (p->family == AF_INET ? 4 : 16);
1196
1197 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1198 req->n.nlmsg_flags = NLM_F_REQUEST;
1199
1200 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1201 cmd = RTM_NEWADDR;
1202 else
1203 cmd = RTM_DELADDR;
1204
1205 req->n.nlmsg_type = cmd;
1206 req->ifa.ifa_family = p->family;
1207
1208 req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1209
1210 if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1211 return 0;
1212
1213 if (p->family == AF_INET) {
1214 if (dplane_ctx_intf_is_connected(ctx)) {
1215 p = dplane_ctx_get_intf_dest(ctx);
1216 if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1217 &p->u.prefix, bytelen))
1218 return 0;
1219 } else if (cmd == RTM_NEWADDR) {
1220 struct in_addr broad = {
1221 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1222 p->prefixlen)
1223 };
1224 if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1225 bytelen))
1226 return 0;
1227 }
1228 }
1229
1230 /* p is now either address or destination/bcast addr */
1231 req->ifa.ifa_prefixlen = p->prefixlen;
1232
1233 if (dplane_ctx_intf_is_secondary(ctx))
1234 SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1235
1236 if (dplane_ctx_intf_has_label(ctx)) {
1237 label = dplane_ctx_get_intf_label(ctx);
1238 if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1239 strlen(label) + 1))
1240 return 0;
1241 }
1242
1243 return NLMSG_ALIGN(req->n.nlmsg_len);
1244 }
1245
1246 enum netlink_msg_status
1247 netlink_put_address_update_msg(struct nl_batch *bth,
1248 struct zebra_dplane_ctx *ctx)
1249 {
1250 return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1251 false);
1252 }
1253
1254 static ssize_t netlink_intf_msg_encoder(struct zebra_dplane_ctx *ctx, void *buf,
1255 size_t buflen)
1256 {
1257 enum dplane_op_e op;
1258 int cmd = 0;
1259
1260 op = dplane_ctx_get_op(ctx);
1261
1262 switch (op) {
1263 case DPLANE_OP_INTF_UPDATE:
1264 cmd = RTM_SETLINK;
1265 break;
1266 case DPLANE_OP_INTF_INSTALL:
1267 cmd = RTM_NEWLINK;
1268 break;
1269 case DPLANE_OP_INTF_DELETE:
1270 cmd = RTM_DELLINK;
1271 break;
1272 default:
1273 flog_err(
1274 EC_ZEBRA_NHG_FIB_UPDATE,
1275 "Context received for kernel interface update with incorrect OP code (%u)",
1276 op);
1277 return -1;
1278 }
1279
1280 return netlink_intf_msg_encode(cmd, ctx, buf, buflen);
1281 }
1282
1283 enum netlink_msg_status
1284 netlink_put_intf_update_msg(struct nl_batch *bth, struct zebra_dplane_ctx *ctx)
1285 {
1286 return netlink_batch_add_msg(bth, ctx, netlink_intf_msg_encoder, false);
1287 }
1288
1289 int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1290 {
1291 int len;
1292 struct ifaddrmsg *ifa;
1293 struct rtattr *tb[IFA_MAX + 1];
1294 struct interface *ifp;
1295 void *addr;
1296 void *broad;
1297 uint8_t flags = 0;
1298 char *label = NULL;
1299 struct zebra_ns *zns;
1300 uint32_t metric = METRIC_MAX;
1301 uint32_t kernel_flags = 0;
1302
1303 zns = zebra_ns_lookup(ns_id);
1304 ifa = NLMSG_DATA(h);
1305
1306 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1307 flog_warn(
1308 EC_ZEBRA_UNKNOWN_FAMILY,
1309 "Invalid address family: %u received from kernel interface addr change: %s",
1310 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
1311 return 0;
1312 }
1313
1314 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1315 return 0;
1316
1317 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1318 if (len < 0) {
1319 zlog_err(
1320 "%s: Message received from netlink is of a broken size: %d %zu",
1321 __func__, h->nlmsg_len,
1322 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
1323 return -1;
1324 }
1325
1326 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1327
1328 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1329 if (ifp == NULL) {
1330 if (startup) {
1331 /* During startup, failure to lookup the referenced
1332 * interface should not be an error, so we have
1333 * downgraded this condition to warning, and we permit
1334 * the startup interface state retrieval to continue.
1335 */
1336 flog_warn(EC_LIB_INTERFACE,
1337 "%s: can't find interface by index %d",
1338 __func__, ifa->ifa_index);
1339 return 0;
1340 } else {
1341 flog_err(EC_LIB_INTERFACE,
1342 "%s: can't find interface by index %d",
1343 __func__, ifa->ifa_index);
1344 return -1;
1345 }
1346 }
1347
1348 /* Flags passed through */
1349 if (tb[IFA_FLAGS])
1350 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1351 else
1352 kernel_flags = ifa->ifa_flags;
1353
1354 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1355 {
1356 char buf[BUFSIZ];
1357 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1358 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
1359 kernel_flags);
1360 if (tb[IFA_LOCAL])
1361 zlog_debug(" IFA_LOCAL %s/%d",
1362 inet_ntop(ifa->ifa_family,
1363 RTA_DATA(tb[IFA_LOCAL]), buf,
1364 BUFSIZ),
1365 ifa->ifa_prefixlen);
1366 if (tb[IFA_ADDRESS])
1367 zlog_debug(" IFA_ADDRESS %s/%d",
1368 inet_ntop(ifa->ifa_family,
1369 RTA_DATA(tb[IFA_ADDRESS]), buf,
1370 BUFSIZ),
1371 ifa->ifa_prefixlen);
1372 if (tb[IFA_BROADCAST])
1373 zlog_debug(" IFA_BROADCAST %s/%d",
1374 inet_ntop(ifa->ifa_family,
1375 RTA_DATA(tb[IFA_BROADCAST]), buf,
1376 BUFSIZ),
1377 ifa->ifa_prefixlen);
1378 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1379 zlog_debug(" IFA_LABEL %s",
1380 (char *)RTA_DATA(tb[IFA_LABEL]));
1381
1382 if (tb[IFA_CACHEINFO]) {
1383 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1384 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1385 ci->ifa_prefered, ci->ifa_valid);
1386 }
1387 }
1388
1389 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1390 if (tb[IFA_LOCAL] == NULL)
1391 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1392 if (tb[IFA_ADDRESS] == NULL)
1393 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1394
1395 /* local interface address */
1396 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1397
1398 /* is there a peer address? */
1399 if (tb[IFA_ADDRESS]
1400 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1401 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1402 broad = RTA_DATA(tb[IFA_ADDRESS]);
1403 SET_FLAG(flags, ZEBRA_IFA_PEER);
1404 } else
1405 /* seeking a broadcast address */
1406 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1407 : NULL);
1408
1409 /* addr is primary key, SOL if we don't have one */
1410 if (addr == NULL) {
1411 zlog_debug("%s: Local Interface Address is NULL for %s",
1412 __func__, ifp->name);
1413 return -1;
1414 }
1415
1416 /* Flags. */
1417 if (kernel_flags & IFA_F_SECONDARY)
1418 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1419
1420 /* Label */
1421 if (tb[IFA_LABEL])
1422 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1423
1424 if (label && strcmp(ifp->name, label) == 0)
1425 label = NULL;
1426
1427 if (tb[IFA_RT_PRIORITY])
1428 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1429
1430 /* Register interface address to the interface. */
1431 if (ifa->ifa_family == AF_INET) {
1432 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1433 zlog_err(
1434 "Invalid prefix length: %u received from kernel interface addr change: %s",
1435 ifa->ifa_prefixlen,
1436 nl_msg_type_to_str(h->nlmsg_type));
1437 return -1;
1438 }
1439
1440 if (h->nlmsg_type == RTM_NEWADDR)
1441 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1442 ifa->ifa_prefixlen,
1443 (struct in_addr *)broad, label,
1444 metric);
1445 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1446 /* Delete with a peer address */
1447 connected_delete_ipv4(
1448 ifp, flags, (struct in_addr *)addr,
1449 ifa->ifa_prefixlen, broad);
1450 } else
1451 connected_delete_ipv4(
1452 ifp, flags, (struct in_addr *)addr,
1453 ifa->ifa_prefixlen, NULL);
1454 }
1455
1456 if (ifa->ifa_family == AF_INET6) {
1457 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1458 zlog_err(
1459 "Invalid prefix length: %u received from kernel interface addr change: %s",
1460 ifa->ifa_prefixlen,
1461 nl_msg_type_to_str(h->nlmsg_type));
1462 return -1;
1463 }
1464 if (h->nlmsg_type == RTM_NEWADDR) {
1465 /* Only consider valid addresses; we'll not get a
1466 * notification from
1467 * the kernel till IPv6 DAD has completed, but at init
1468 * time, Quagga
1469 * does query for and will receive all addresses.
1470 */
1471 if (!(kernel_flags
1472 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
1473 connected_add_ipv6(ifp, flags,
1474 (struct in6_addr *)addr,
1475 (struct in6_addr *)broad,
1476 ifa->ifa_prefixlen, label,
1477 metric);
1478 } else
1479 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
1480 NULL, ifa->ifa_prefixlen);
1481 }
1482
1483 /*
1484 * Linux kernel does not send route delete on interface down/addr del
1485 * so we have to re-process routes it owns (i.e. kernel routes)
1486 */
1487 if (h->nlmsg_type != RTM_NEWADDR)
1488 rib_update(RIB_UPDATE_KERNEL);
1489
1490 return 0;
1491 }
1492
1493 /*
1494 * Parse and validate an incoming interface address change message,
1495 * generating a dplane context object.
1496 * This runs in the dplane pthread; the context is enqueued to the
1497 * main pthread for processing.
1498 */
1499 int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1500 int startup /*ignored*/)
1501 {
1502 int len;
1503 struct ifaddrmsg *ifa;
1504 struct rtattr *tb[IFA_MAX + 1];
1505 void *addr;
1506 void *broad;
1507 char *label = NULL;
1508 uint32_t metric = METRIC_MAX;
1509 uint32_t kernel_flags = 0;
1510 struct zebra_dplane_ctx *ctx;
1511 struct prefix p;
1512
1513 ifa = NLMSG_DATA(h);
1514
1515 /* Validate message types */
1516 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1517 return 0;
1518
1519 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1520 if (IS_ZEBRA_DEBUG_KERNEL)
1521 zlog_debug("%s: %s: Invalid address family: %u",
1522 __func__, nl_msg_type_to_str(h->nlmsg_type),
1523 ifa->ifa_family);
1524 return 0;
1525 }
1526
1527 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1528 if (len < 0) {
1529 if (IS_ZEBRA_DEBUG_KERNEL)
1530 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1531 __func__, nl_msg_type_to_str(h->nlmsg_type),
1532 h->nlmsg_len,
1533 (size_t)NLMSG_LENGTH(
1534 sizeof(struct ifaddrmsg)));
1535 return -1;
1536 }
1537
1538 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1539
1540 /* Flags passed through */
1541 if (tb[IFA_FLAGS])
1542 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1543 else
1544 kernel_flags = ifa->ifa_flags;
1545
1546 if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1547 char buf[PREFIX_STRLEN];
1548
1549 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1550 nl_msg_type_to_str(h->nlmsg_type), ns_id,
1551 ifa->ifa_index, kernel_flags);
1552 if (tb[IFA_LOCAL])
1553 zlog_debug(" IFA_LOCAL %s/%d",
1554 inet_ntop(ifa->ifa_family,
1555 RTA_DATA(tb[IFA_LOCAL]), buf,
1556 sizeof(buf)),
1557 ifa->ifa_prefixlen);
1558 if (tb[IFA_ADDRESS])
1559 zlog_debug(" IFA_ADDRESS %s/%d",
1560 inet_ntop(ifa->ifa_family,
1561 RTA_DATA(tb[IFA_ADDRESS]), buf,
1562 sizeof(buf)),
1563 ifa->ifa_prefixlen);
1564 if (tb[IFA_BROADCAST])
1565 zlog_debug(" IFA_BROADCAST %s/%d",
1566 inet_ntop(ifa->ifa_family,
1567 RTA_DATA(tb[IFA_BROADCAST]), buf,
1568 sizeof(buf)),
1569 ifa->ifa_prefixlen);
1570 if (tb[IFA_LABEL])
1571 zlog_debug(" IFA_LABEL %s",
1572 (const char *)RTA_DATA(tb[IFA_LABEL]));
1573
1574 if (tb[IFA_CACHEINFO]) {
1575 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1576
1577 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1578 ci->ifa_prefered, ci->ifa_valid);
1579 }
1580 }
1581
1582 /* Validate prefix length */
1583
1584 if (ifa->ifa_family == AF_INET
1585 && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1586 if (IS_ZEBRA_DEBUG_KERNEL)
1587 zlog_debug("%s: %s: Invalid prefix length: %u",
1588 __func__, nl_msg_type_to_str(h->nlmsg_type),
1589 ifa->ifa_prefixlen);
1590 return -1;
1591 }
1592
1593 if (ifa->ifa_family == AF_INET6) {
1594 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1595 if (IS_ZEBRA_DEBUG_KERNEL)
1596 zlog_debug("%s: %s: Invalid prefix length: %u",
1597 __func__,
1598 nl_msg_type_to_str(h->nlmsg_type),
1599 ifa->ifa_prefixlen);
1600 return -1;
1601 }
1602
1603 /* Only consider valid addresses; we'll not get a kernel
1604 * notification till IPv6 DAD has completed, but at init
1605 * time, FRR does query for and will receive all addresses.
1606 */
1607 if (h->nlmsg_type == RTM_NEWADDR
1608 && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1609 if (IS_ZEBRA_DEBUG_KERNEL)
1610 zlog_debug("%s: %s: Invalid/tentative addr",
1611 __func__,
1612 nl_msg_type_to_str(h->nlmsg_type));
1613 return 0;
1614 }
1615 }
1616
1617 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1618 if (tb[IFA_LOCAL] == NULL)
1619 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1620 if (tb[IFA_ADDRESS] == NULL)
1621 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1622
1623 /* local interface address */
1624 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1625
1626 /* addr is primary key, SOL if we don't have one */
1627 if (addr == NULL) {
1628 if (IS_ZEBRA_DEBUG_KERNEL)
1629 zlog_debug("%s: %s: No local interface address",
1630 __func__, nl_msg_type_to_str(h->nlmsg_type));
1631 return -1;
1632 }
1633
1634 /* Allocate a context object, now that validation is done. */
1635 ctx = dplane_ctx_alloc();
1636 if (h->nlmsg_type == RTM_NEWADDR)
1637 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1638 else
1639 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1640
1641 dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1642 dplane_ctx_set_ns_id(ctx, ns_id);
1643
1644 /* Convert addr to prefix */
1645 memset(&p, 0, sizeof(p));
1646 p.family = ifa->ifa_family;
1647 p.prefixlen = ifa->ifa_prefixlen;
1648 if (p.family == AF_INET)
1649 p.u.prefix4 = *(struct in_addr *)addr;
1650 else
1651 p.u.prefix6 = *(struct in6_addr *)addr;
1652
1653 dplane_ctx_set_intf_addr(ctx, &p);
1654
1655 /* is there a peer address? */
1656 if (tb[IFA_ADDRESS]
1657 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1658 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1659 broad = RTA_DATA(tb[IFA_ADDRESS]);
1660 dplane_ctx_intf_set_connected(ctx);
1661 } else if (tb[IFA_BROADCAST]) {
1662 /* seeking a broadcast address */
1663 broad = RTA_DATA(tb[IFA_BROADCAST]);
1664 dplane_ctx_intf_set_broadcast(ctx);
1665 } else
1666 broad = NULL;
1667
1668 if (broad) {
1669 /* Convert addr to prefix */
1670 memset(&p, 0, sizeof(p));
1671 p.family = ifa->ifa_family;
1672 p.prefixlen = ifa->ifa_prefixlen;
1673 if (p.family == AF_INET)
1674 p.u.prefix4 = *(struct in_addr *)broad;
1675 else
1676 p.u.prefix6 = *(struct in6_addr *)broad;
1677
1678 dplane_ctx_set_intf_dest(ctx, &p);
1679 }
1680
1681 /* Flags. */
1682 if (kernel_flags & IFA_F_SECONDARY)
1683 dplane_ctx_intf_set_secondary(ctx);
1684
1685 /* Label */
1686 if (tb[IFA_LABEL]) {
1687 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1688 dplane_ctx_set_intf_label(ctx, label);
1689 }
1690
1691 if (tb[IFA_RT_PRIORITY])
1692 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1693
1694 dplane_ctx_set_intf_metric(ctx, metric);
1695
1696 /* Enqueue ctx for main pthread to process */
1697 dplane_provider_enqueue_to_zebra(ctx);
1698
1699 return 0;
1700 }
1701
1702 int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1703 {
1704 int len;
1705 struct ifinfomsg *ifi;
1706 struct rtattr *tb[IFLA_MAX + 1];
1707 struct rtattr *linkinfo[IFLA_MAX + 1];
1708 struct interface *ifp;
1709 char *name = NULL;
1710 char *kind = NULL;
1711 char *desc = NULL;
1712 char *slave_kind = NULL;
1713 struct zebra_ns *zns;
1714 vrf_id_t vrf_id = VRF_DEFAULT;
1715 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
1716 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1717 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1718 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1719 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1720 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
1721 struct zebra_if *zif;
1722 ns_id_t link_nsid = ns_id;
1723 ifindex_t master_infindex = IFINDEX_INTERNAL;
1724 uint8_t bypass = 0;
1725
1726 zns = zebra_ns_lookup(ns_id);
1727 ifi = NLMSG_DATA(h);
1728
1729 /* assume if not default zns, then new VRF */
1730 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1731 /* If this is not link add/delete message so print warning. */
1732 zlog_debug("netlink_link_change: wrong kernel message %s",
1733 nl_msg_type_to_str(h->nlmsg_type));
1734 return 0;
1735 }
1736
1737 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1738 || ifi->ifi_family == AF_INET6)) {
1739 flog_warn(
1740 EC_ZEBRA_UNKNOWN_FAMILY,
1741 "Invalid address family: %u received from kernel link change: %s",
1742 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
1743 return 0;
1744 }
1745
1746 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1747 if (len < 0) {
1748 zlog_err(
1749 "%s: Message received from netlink is of a broken size %d %zu",
1750 __func__, h->nlmsg_len,
1751 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
1752 return -1;
1753 }
1754
1755 /* We are interested in some AF_BRIDGE notifications. */
1756 if (ifi->ifi_family == AF_BRIDGE)
1757 return netlink_bridge_interface(h, len, ns_id, startup);
1758
1759 /* Looking up interface name. */
1760 memset(linkinfo, 0, sizeof(linkinfo));
1761 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1762
1763 /* check for wireless messages to ignore */
1764 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1765 if (IS_ZEBRA_DEBUG_KERNEL)
1766 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1767 __func__);
1768 return 0;
1769 }
1770
1771 if (tb[IFLA_IFNAME] == NULL)
1772 return -1;
1773 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1774
1775 /* Must be valid string. */
1776 len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
1777 if (len < 2 || name[len - 1] != '\0') {
1778 if (IS_ZEBRA_DEBUG_KERNEL)
1779 zlog_debug("%s: invalid intf name", __func__);
1780 return -1;
1781 }
1782
1783 if (tb[IFLA_LINKINFO]) {
1784 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1785 tb[IFLA_LINKINFO]);
1786
1787 if (linkinfo[IFLA_INFO_KIND])
1788 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1789
1790 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1791 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1792
1793 netlink_determine_zebra_iftype(kind, &zif_type);
1794 }
1795
1796 /* If linking to another interface, note it. */
1797 if (tb[IFLA_LINK])
1798 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1799
1800 if (tb[IFLA_LINK_NETNSID]) {
1801 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
1802 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1803 }
1804 if (tb[IFLA_IFALIAS]) {
1805 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1806 }
1807
1808 /* See if interface is present. */
1809 ifp = if_lookup_by_name_per_ns(zns, name);
1810
1811 if (h->nlmsg_type == RTM_NEWLINK) {
1812 /* If VRF, create or update the VRF structure itself. */
1813 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1814 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1815 vrf_id = (vrf_id_t)ifi->ifi_index;
1816 }
1817
1818 if (tb[IFLA_MASTER]) {
1819 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1820 && !vrf_is_backend_netns()) {
1821 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
1822 master_infindex = vrf_id =
1823 *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
1824 } else if (slave_kind
1825 && (strcmp(slave_kind, "bridge") == 0)) {
1826 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1827 master_infindex = bridge_ifindex =
1828 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1829 } else if (slave_kind
1830 && (strcmp(slave_kind, "bond") == 0)) {
1831 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1832 master_infindex = bond_ifindex =
1833 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1834 bypass = netlink_parse_lacp_bypass(linkinfo);
1835 } else
1836 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1837 }
1838 if (vrf_is_backend_netns())
1839 vrf_id = (vrf_id_t)ns_id;
1840 if (ifp == NULL
1841 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1842 /* Add interface notification from kernel */
1843 if (IS_ZEBRA_DEBUG_KERNEL)
1844 zlog_debug(
1845 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1846 name, ifi->ifi_index, vrf_id, zif_type,
1847 zif_slave_type, master_infindex,
1848 ifi->ifi_flags);
1849
1850 if (ifp == NULL) {
1851 /* unknown interface */
1852 ifp = if_get_by_name(name, vrf_id, NULL);
1853 } else {
1854 /* pre-configured interface, learnt now */
1855 if (ifp->vrf->vrf_id != vrf_id)
1856 if_update_to_new_vrf(ifp, vrf_id);
1857 }
1858
1859 /* Update interface information. */
1860 set_ifindex(ifp, ifi->ifi_index, zns);
1861 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1862 if (!tb[IFLA_MTU]) {
1863 zlog_debug(
1864 "RTM_NEWLINK for interface %s(%u) without MTU set",
1865 name, ifi->ifi_index);
1866 return 0;
1867 }
1868 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1869 ifp->metric = 0;
1870 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1871
1872 /* Set interface type */
1873 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1874 if (IS_ZEBRA_IF_VRF(ifp))
1875 SET_FLAG(ifp->status,
1876 ZEBRA_INTERFACE_VRF_LOOPBACK);
1877
1878 /* Update link. */
1879 zebra_if_update_link(ifp, link_ifindex, ns_id);
1880
1881 ifp->ll_type =
1882 netlink_to_zebra_link_type(ifi->ifi_type);
1883 netlink_interface_update_hw_addr(tb, ifp);
1884
1885 /* Inform clients, install any configured addresses. */
1886 if_add_update(ifp);
1887
1888 /* Extract and save L2 interface information, take
1889 * additional actions. */
1890 netlink_interface_update_l2info(
1891 ifp, linkinfo[IFLA_INFO_DATA],
1892 1, link_nsid);
1893 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1894 zebra_l2if_update_bridge_slave(
1895 ifp, bridge_ifindex, ns_id,
1896 ZEBRA_BRIDGE_NO_ACTION);
1897 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1898 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
1899 !!bypass);
1900
1901 if (tb[IFLA_PROTO_DOWN]) {
1902 uint8_t protodown;
1903
1904 protodown = *(uint8_t *)RTA_DATA(
1905 tb[IFLA_PROTO_DOWN]);
1906 netlink_proc_dplane_if_protodown(ifp->info,
1907 !!protodown);
1908 }
1909 } else if (ifp->vrf->vrf_id != vrf_id) {
1910 /* VRF change for an interface. */
1911 if (IS_ZEBRA_DEBUG_KERNEL)
1912 zlog_debug(
1913 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1914 name, ifp->ifindex, ifp->vrf->vrf_id,
1915 vrf_id, ifi->ifi_flags);
1916
1917 if_handle_vrf_change(ifp, vrf_id);
1918 } else {
1919 bool was_bridge_slave, was_bond_slave;
1920 uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
1921 zif = ifp->info;
1922
1923 /* Interface update. */
1924 if (IS_ZEBRA_DEBUG_KERNEL)
1925 zlog_debug(
1926 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1927 name, ifp->ifindex, zif_slave_type,
1928 master_infindex, ifi->ifi_flags);
1929
1930 set_ifindex(ifp, ifi->ifi_index, zns);
1931 if (!tb[IFLA_MTU]) {
1932 zlog_debug(
1933 "RTM_NEWLINK for interface %s(%u) without MTU set",
1934 name, ifi->ifi_index);
1935 return 0;
1936 }
1937 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1938 ifp->metric = 0;
1939
1940 /* Update interface type - NOTE: Only slave_type can
1941 * change. */
1942 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
1943 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
1944 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1945
1946 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
1947
1948 /* Update link. */
1949 zebra_if_update_link(ifp, link_ifindex, ns_id);
1950
1951 ifp->ll_type =
1952 netlink_to_zebra_link_type(ifi->ifi_type);
1953 netlink_interface_update_hw_addr(tb, ifp);
1954
1955 if (tb[IFLA_PROTO_DOWN]) {
1956 uint8_t protodown;
1957
1958 protodown = *(uint8_t *)RTA_DATA(
1959 tb[IFLA_PROTO_DOWN]);
1960 netlink_proc_dplane_if_protodown(zif,
1961 !!protodown);
1962 }
1963
1964 if (if_is_no_ptm_operative(ifp)) {
1965 bool is_up = if_is_operative(ifp);
1966 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1967 if (!if_is_no_ptm_operative(ifp) ||
1968 CHECK_FLAG(zif->flags,
1969 ZIF_FLAG_PROTODOWN)) {
1970 if (IS_ZEBRA_DEBUG_KERNEL)
1971 zlog_debug(
1972 "Intf %s(%u) has gone DOWN",
1973 name, ifp->ifindex);
1974 if_down(ifp);
1975 rib_update(RIB_UPDATE_KERNEL);
1976 } else if (if_is_operative(ifp)) {
1977 bool mac_updated = false;
1978
1979 /* Must notify client daemons of new
1980 * interface status. */
1981 if (IS_ZEBRA_DEBUG_KERNEL)
1982 zlog_debug(
1983 "Intf %s(%u) PTM up, notifying clients",
1984 name, ifp->ifindex);
1985 if_up(ifp, !is_up);
1986
1987 /* Update EVPN VNI when SVI MAC change
1988 */
1989 if (memcmp(old_hw_addr, ifp->hw_addr,
1990 INTERFACE_HWADDR_MAX))
1991 mac_updated = true;
1992 if (IS_ZEBRA_IF_VLAN(ifp)
1993 && mac_updated) {
1994 struct interface *link_if;
1995
1996 link_if =
1997 if_lookup_by_index_per_ns(
1998 zebra_ns_lookup(NS_DEFAULT),
1999 link_ifindex);
2000 if (link_if)
2001 zebra_vxlan_svi_up(ifp,
2002 link_if);
2003 } else if (mac_updated
2004 && IS_ZEBRA_IF_BRIDGE(ifp)) {
2005 zlog_debug(
2006 "Intf %s(%u) bridge changed MAC address",
2007 name, ifp->ifindex);
2008 chgflags =
2009 ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
2010 }
2011 }
2012 } else {
2013 ifp->flags = ifi->ifi_flags & 0x0000fffff;
2014 if (if_is_operative(ifp) &&
2015 !CHECK_FLAG(zif->flags,
2016 ZIF_FLAG_PROTODOWN)) {
2017 if (IS_ZEBRA_DEBUG_KERNEL)
2018 zlog_debug(
2019 "Intf %s(%u) has come UP",
2020 name, ifp->ifindex);
2021 if_up(ifp, true);
2022 if (IS_ZEBRA_IF_BRIDGE(ifp))
2023 chgflags =
2024 ZEBRA_BRIDGE_MASTER_UP;
2025 } else {
2026 if (IS_ZEBRA_DEBUG_KERNEL)
2027 zlog_debug(
2028 "Intf %s(%u) has gone DOWN",
2029 name, ifp->ifindex);
2030 if_down(ifp);
2031 rib_update(RIB_UPDATE_KERNEL);
2032 }
2033 }
2034
2035 /* Extract and save L2 interface information, take
2036 * additional actions. */
2037 netlink_interface_update_l2info(
2038 ifp, linkinfo[IFLA_INFO_DATA],
2039 0, link_nsid);
2040 if (IS_ZEBRA_IF_BRIDGE(ifp))
2041 zebra_l2if_update_bridge(ifp, chgflags);
2042 if (IS_ZEBRA_IF_BOND(ifp))
2043 zebra_l2if_update_bond(ifp, true);
2044 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
2045 zebra_l2if_update_bridge_slave(
2046 ifp, bridge_ifindex, ns_id, chgflags);
2047 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
2048 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
2049 !!bypass);
2050 }
2051
2052 zif = ifp->info;
2053 if (zif) {
2054 XFREE(MTYPE_TMP, zif->desc);
2055 if (desc)
2056 zif->desc = XSTRDUP(MTYPE_TMP, desc);
2057 }
2058 } else {
2059 /* Delete interface notification from kernel */
2060 if (ifp == NULL) {
2061 if (IS_ZEBRA_DEBUG_KERNEL)
2062 zlog_debug(
2063 "RTM_DELLINK for unknown interface %s(%u)",
2064 name, ifi->ifi_index);
2065 return 0;
2066 }
2067
2068 if (IS_ZEBRA_DEBUG_KERNEL)
2069 zlog_debug("RTM_DELLINK for %s(%u)", name,
2070 ifp->ifindex);
2071
2072 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
2073
2074 if (IS_ZEBRA_IF_BOND(ifp))
2075 zebra_l2if_update_bond(ifp, false);
2076 if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2077 zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
2078 /* Special handling for bridge or VxLAN interfaces. */
2079 if (IS_ZEBRA_IF_BRIDGE(ifp))
2080 zebra_l2_bridge_del(ifp);
2081 else if (IS_ZEBRA_IF_VXLAN(ifp))
2082 zebra_l2_vxlanif_del(ifp);
2083
2084 if_delete_update(ifp);
2085
2086 /* If VRF, delete the VRF structure itself. */
2087 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns())
2088 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
2089 }
2090
2091 return 0;
2092 }
2093
2094 /**
2095 * Interface encoding helper function.
2096 *
2097 * \param[in] cmd netlink command.
2098 * \param[in] ctx dataplane context (information snapshot).
2099 * \param[out] buf buffer to hold the packet.
2100 * \param[in] buflen amount of buffer bytes.
2101 */
2102
2103 ssize_t netlink_intf_msg_encode(uint16_t cmd,
2104 const struct zebra_dplane_ctx *ctx, void *buf,
2105 size_t buflen)
2106 {
2107 struct {
2108 struct nlmsghdr n;
2109 struct ifinfomsg ifa;
2110 char buf[];
2111 } *req = buf;
2112
2113 struct rtattr *nest_protodown_reason;
2114 ifindex_t ifindex = dplane_ctx_get_ifindex(ctx);
2115 uint32_t r_bitfield = dplane_ctx_get_intf_r_bitfield(ctx);
2116 bool down = dplane_ctx_intf_is_protodown(ctx);
2117 struct nlsock *nl =
2118 kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx));
2119
2120 if (buflen < sizeof(*req))
2121 return 0;
2122
2123 memset(req, 0, sizeof(*req));
2124
2125 if (cmd != RTM_SETLINK)
2126 flog_err(
2127 EC_ZEBRA_INTF_UPDATE_FAILURE,
2128 "Only RTM_SETLINK message type currently supported in dplane pthread");
2129
2130 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2131 req->n.nlmsg_flags = NLM_F_REQUEST;
2132 req->n.nlmsg_type = cmd;
2133 req->n.nlmsg_pid = nl->snl.nl_pid;
2134
2135 req->ifa.ifi_index = ifindex;
2136
2137 nl_attr_put8(&req->n, buflen, IFLA_PROTO_DOWN, down);
2138 nl_attr_put32(&req->n, buflen, IFLA_LINK, ifindex);
2139
2140 if (r_bitfield) {
2141 nest_protodown_reason =
2142 nl_attr_nest(&req->n, buflen, IFLA_PROTO_DOWN_REASON);
2143
2144 if (!nest_protodown_reason)
2145 return -1;
2146
2147 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_MASK,
2148 (1 << frr_protodown_r_bit));
2149 nl_attr_put32(&req->n, buflen, IFLA_PROTO_DOWN_REASON_VALUE,
2150 ((int)down) << frr_protodown_r_bit);
2151
2152 nl_attr_nest_end(&req->n, nest_protodown_reason);
2153 }
2154
2155 if (IS_ZEBRA_DEBUG_KERNEL)
2156 zlog_debug("%s: %s, protodown=%d ifindex=%u", __func__,
2157 nl_msg_type_to_str(cmd), down, ifindex);
2158
2159 return NLMSG_ALIGN(req->n.nlmsg_len);
2160 }
2161
2162 int netlink_protodown(struct interface *ifp, bool down, uint32_t r_bitfield)
2163 {
2164 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
2165 struct rtattr *nest_protodown_reason;
2166
2167 struct {
2168 struct nlmsghdr n;
2169 struct ifinfomsg ifa;
2170 char buf[NL_PKT_BUF_SIZE];
2171 } req;
2172
2173 memset(&req, 0, sizeof(req));
2174
2175 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2176 req.n.nlmsg_flags = NLM_F_REQUEST;
2177 req.n.nlmsg_type = RTM_SETLINK;
2178 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
2179
2180 req.ifa.ifi_index = ifp->ifindex;
2181
2182 nl_attr_put8(&req.n, sizeof(req), IFLA_PROTO_DOWN, down);
2183 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, ifp->ifindex);
2184
2185 if (r_bitfield) {
2186 nest_protodown_reason = nl_attr_nest(&req.n, sizeof(req),
2187 IFLA_PROTO_DOWN_REASON);
2188
2189 if (!nest_protodown_reason)
2190 return -1;
2191
2192 nl_attr_put32(&req.n, sizeof(req), IFLA_PROTO_DOWN_REASON_MASK,
2193 (1 << frr_protodown_r_bit));
2194 nl_attr_put32(&req.n, sizeof(req), IFLA_PROTO_DOWN_REASON_VALUE,
2195 ((int)down) << frr_protodown_r_bit);
2196
2197 nl_attr_nest_end(&req.n, nest_protodown_reason);
2198 }
2199
2200 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2201 false);
2202 }
2203
2204 /* Interface information read by netlink. */
2205 void interface_list(struct zebra_ns *zns)
2206 {
2207 interface_lookup_netlink(zns);
2208 /* We add routes for interface address,
2209 * so we need to get the nexthop info
2210 * from the kernel before we can do that
2211 */
2212 netlink_nexthop_read(zns);
2213
2214 interface_addr_lookup_netlink(zns);
2215 }
2216
2217 void if_netlink_set_frr_protodown_r_bit(uint8_t bit)
2218 {
2219 if (IS_ZEBRA_DEBUG_KERNEL)
2220 zlog_debug("FRR protodown reason bit change %u -> %u",
2221 frr_protodown_r_bit, bit);
2222
2223 frr_protodown_r_bit = bit;
2224 }
2225
2226 void if_netlink_unset_frr_protodown_r_bit(void)
2227 {
2228 if (IS_ZEBRA_DEBUG_KERNEL)
2229 zlog_debug("FRR protodown reason bit change %u -> %u",
2230 frr_protodown_r_bit,
2231 FRR_PROTODOWN_REASON_DEFAULT_BIT);
2232
2233 frr_protodown_r_bit = FRR_PROTODOWN_REASON_DEFAULT_BIT;
2234 }
2235
2236
2237 bool if_netlink_frr_protodown_r_bit_is_set(void)
2238 {
2239 return (frr_protodown_r_bit != FRR_PROTODOWN_REASON_DEFAULT_BIT);
2240 }
2241
2242 uint8_t if_netlink_get_frr_protodown_r_bit(void)
2243 {
2244 return frr_protodown_r_bit;
2245 }
2246
2247 #endif /* GNU_LINUX */