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