]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_netlink.c
Merge pull request #9416 from pguibert6WIND/vxlan_evpn_updates
[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
78 extern struct zebra_privs_t zserv_privs;
79
80 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
81 names and ifindex values. */
82 static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
83 struct zebra_ns *zns)
84 {
85 struct interface *oifp;
86
87 if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
88 && (oifp != ifp)) {
89 if (ifi_index == IFINDEX_INTERNAL)
90 flog_err(
91 EC_LIB_INTERFACE,
92 "Netlink is setting interface %s ifindex to reserved internal value %u",
93 ifp->name, ifi_index);
94 else {
95 if (IS_ZEBRA_DEBUG_KERNEL)
96 zlog_debug(
97 "interface index %d was renamed from %s to %s",
98 ifi_index, oifp->name, ifp->name);
99 if (if_is_up(oifp))
100 flog_err(
101 EC_LIB_INTERFACE,
102 "interface rename detected on up interface: index %d was renamed from %s to %s, results are uncertain!",
103 ifi_index, oifp->name, ifp->name);
104 if_delete_update(oifp);
105 }
106 }
107 if_set_index(ifp, ifi_index);
108 }
109
110 /* Utility function to parse hardware link-layer address and update ifp */
111 static void netlink_interface_update_hw_addr(struct rtattr **tb,
112 struct interface *ifp)
113 {
114 int i;
115
116 if (tb[IFLA_ADDRESS]) {
117 int hw_addr_len;
118
119 hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
120
121 if (hw_addr_len > INTERFACE_HWADDR_MAX)
122 zlog_debug("Hardware address is too large: %d",
123 hw_addr_len);
124 else {
125 ifp->hw_addr_len = hw_addr_len;
126 memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
127 hw_addr_len);
128
129 for (i = 0; i < hw_addr_len; i++)
130 if (ifp->hw_addr[i] != 0)
131 break;
132
133 if (i == hw_addr_len)
134 ifp->hw_addr_len = 0;
135 else
136 ifp->hw_addr_len = hw_addr_len;
137 }
138 }
139 }
140
141 static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
142 {
143 switch (hwt) {
144 case ARPHRD_ETHER:
145 return ZEBRA_LLT_ETHER;
146 case ARPHRD_EETHER:
147 return ZEBRA_LLT_EETHER;
148 case ARPHRD_AX25:
149 return ZEBRA_LLT_AX25;
150 case ARPHRD_PRONET:
151 return ZEBRA_LLT_PRONET;
152 case ARPHRD_IEEE802:
153 return ZEBRA_LLT_IEEE802;
154 case ARPHRD_ARCNET:
155 return ZEBRA_LLT_ARCNET;
156 case ARPHRD_APPLETLK:
157 return ZEBRA_LLT_APPLETLK;
158 case ARPHRD_DLCI:
159 return ZEBRA_LLT_DLCI;
160 case ARPHRD_ATM:
161 return ZEBRA_LLT_ATM;
162 case ARPHRD_METRICOM:
163 return ZEBRA_LLT_METRICOM;
164 case ARPHRD_IEEE1394:
165 return ZEBRA_LLT_IEEE1394;
166 case ARPHRD_EUI64:
167 return ZEBRA_LLT_EUI64;
168 case ARPHRD_INFINIBAND:
169 return ZEBRA_LLT_INFINIBAND;
170 case ARPHRD_SLIP:
171 return ZEBRA_LLT_SLIP;
172 case ARPHRD_CSLIP:
173 return ZEBRA_LLT_CSLIP;
174 case ARPHRD_SLIP6:
175 return ZEBRA_LLT_SLIP6;
176 case ARPHRD_CSLIP6:
177 return ZEBRA_LLT_CSLIP6;
178 case ARPHRD_RSRVD:
179 return ZEBRA_LLT_RSRVD;
180 case ARPHRD_ADAPT:
181 return ZEBRA_LLT_ADAPT;
182 case ARPHRD_ROSE:
183 return ZEBRA_LLT_ROSE;
184 case ARPHRD_X25:
185 return ZEBRA_LLT_X25;
186 case ARPHRD_PPP:
187 return ZEBRA_LLT_PPP;
188 case ARPHRD_CISCO:
189 return ZEBRA_LLT_CHDLC;
190 case ARPHRD_LAPB:
191 return ZEBRA_LLT_LAPB;
192 case ARPHRD_RAWHDLC:
193 return ZEBRA_LLT_RAWHDLC;
194 case ARPHRD_TUNNEL:
195 return ZEBRA_LLT_IPIP;
196 case ARPHRD_TUNNEL6:
197 return ZEBRA_LLT_IPIP6;
198 case ARPHRD_FRAD:
199 return ZEBRA_LLT_FRAD;
200 case ARPHRD_SKIP:
201 return ZEBRA_LLT_SKIP;
202 case ARPHRD_LOOPBACK:
203 return ZEBRA_LLT_LOOPBACK;
204 case ARPHRD_LOCALTLK:
205 return ZEBRA_LLT_LOCALTLK;
206 case ARPHRD_FDDI:
207 return ZEBRA_LLT_FDDI;
208 case ARPHRD_SIT:
209 return ZEBRA_LLT_SIT;
210 case ARPHRD_IPDDP:
211 return ZEBRA_LLT_IPDDP;
212 case ARPHRD_IPGRE:
213 return ZEBRA_LLT_IPGRE;
214 case ARPHRD_PIMREG:
215 return ZEBRA_LLT_PIMREG;
216 case ARPHRD_HIPPI:
217 return ZEBRA_LLT_HIPPI;
218 case ARPHRD_ECONET:
219 return ZEBRA_LLT_ECONET;
220 case ARPHRD_IRDA:
221 return ZEBRA_LLT_IRDA;
222 case ARPHRD_FCPP:
223 return ZEBRA_LLT_FCPP;
224 case ARPHRD_FCAL:
225 return ZEBRA_LLT_FCAL;
226 case ARPHRD_FCPL:
227 return ZEBRA_LLT_FCPL;
228 case ARPHRD_FCFABRIC:
229 return ZEBRA_LLT_FCFABRIC;
230 case ARPHRD_IEEE802_TR:
231 return ZEBRA_LLT_IEEE802_TR;
232 case ARPHRD_IEEE80211:
233 return ZEBRA_LLT_IEEE80211;
234 #ifdef ARPHRD_IEEE802154
235 case ARPHRD_IEEE802154:
236 return ZEBRA_LLT_IEEE802154;
237 #endif
238 #ifdef ARPHRD_IP6GRE
239 case ARPHRD_IP6GRE:
240 return ZEBRA_LLT_IP6GRE;
241 #endif
242 #ifdef ARPHRD_IEEE802154_PHY
243 case ARPHRD_IEEE802154_PHY:
244 return ZEBRA_LLT_IEEE802154_PHY;
245 #endif
246
247 default:
248 return ZEBRA_LLT_UNKNOWN;
249 }
250 }
251
252 static inline void zebra_if_set_ziftype(struct interface *ifp,
253 enum zebra_iftype zif_type,
254 enum zebra_slave_iftype zif_slave_type)
255 {
256 struct zebra_if *zif;
257
258 zif = (struct zebra_if *)ifp->info;
259 zif->zif_slave_type = zif_slave_type;
260
261 if (zif->zif_type != zif_type) {
262 zif->zif_type = zif_type;
263 /* If the if_type has been set to bond initialize ES info
264 * against it. XXX - note that we don't handle the case where
265 * a zif changes from bond to non-bond; it is really
266 * an unexpected/error condition.
267 */
268 zebra_evpn_if_init(zif);
269 }
270 }
271
272 static void netlink_determine_zebra_iftype(const char *kind,
273 enum zebra_iftype *zif_type)
274 {
275 *zif_type = ZEBRA_IF_OTHER;
276
277 if (!kind)
278 return;
279
280 if (strcmp(kind, "vrf") == 0)
281 *zif_type = ZEBRA_IF_VRF;
282 else if (strcmp(kind, "bridge") == 0)
283 *zif_type = ZEBRA_IF_BRIDGE;
284 else if (strcmp(kind, "vlan") == 0)
285 *zif_type = ZEBRA_IF_VLAN;
286 else if (strcmp(kind, "vxlan") == 0)
287 *zif_type = ZEBRA_IF_VXLAN;
288 else if (strcmp(kind, "macvlan") == 0)
289 *zif_type = ZEBRA_IF_MACVLAN;
290 else if (strcmp(kind, "veth") == 0)
291 *zif_type = ZEBRA_IF_VETH;
292 else if (strcmp(kind, "bond") == 0)
293 *zif_type = ZEBRA_IF_BOND;
294 else if (strcmp(kind, "bond_slave") == 0)
295 *zif_type = ZEBRA_IF_BOND_SLAVE;
296 else if (strcmp(kind, "gre") == 0)
297 *zif_type = ZEBRA_IF_GRE;
298 }
299
300 static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
301 uint32_t ns_id, const char *name)
302 {
303 struct ifinfomsg *ifi;
304 struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
305 struct rtattr *attr[IFLA_VRF_MAX + 1];
306 struct vrf *vrf = NULL;
307 struct zebra_vrf *zvrf;
308 uint32_t nl_table_id;
309
310 ifi = NLMSG_DATA(h);
311
312 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
313
314 if (!linkinfo[IFLA_INFO_DATA]) {
315 if (IS_ZEBRA_DEBUG_KERNEL)
316 zlog_debug(
317 "%s: IFLA_INFO_DATA missing from VRF message: %s",
318 __func__, name);
319 return;
320 }
321
322 netlink_parse_rtattr_nested(attr, IFLA_VRF_MAX,
323 linkinfo[IFLA_INFO_DATA]);
324 if (!attr[IFLA_VRF_TABLE]) {
325 if (IS_ZEBRA_DEBUG_KERNEL)
326 zlog_debug(
327 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
328 __func__, name);
329 return;
330 }
331
332 nl_table_id = *(uint32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
333
334 if (h->nlmsg_type == RTM_NEWLINK) {
335 if (IS_ZEBRA_DEBUG_KERNEL)
336 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
337 ifi->ifi_index, nl_table_id);
338
339 if (!vrf_lookup_by_id((vrf_id_t)ifi->ifi_index)) {
340 vrf_id_t exist_id;
341
342 exist_id = vrf_lookup_by_table(nl_table_id, ns_id);
343 if (exist_id != VRF_DEFAULT) {
344 vrf = vrf_lookup_by_id(exist_id);
345
346 flog_err(
347 EC_ZEBRA_VRF_MISCONFIGURED,
348 "VRF %s id %u table id overlaps existing vrf %s, misconfiguration exiting",
349 name, ifi->ifi_index, vrf->name);
350 exit(-1);
351 }
352 }
353
354 vrf = vrf_update((vrf_id_t)ifi->ifi_index, name);
355 if (!vrf) {
356 flog_err(EC_LIB_INTERFACE, "VRF %s id %u not created",
357 name, ifi->ifi_index);
358 return;
359 }
360
361 /*
362 * This is the only place that we get the actual kernel table_id
363 * being used. We need it to set the table_id of the routes
364 * we are passing to the kernel.... And to throw some totally
365 * awesome parties. that too.
366 *
367 * At this point we *must* have a zvrf because the vrf_create
368 * callback creates one. We *must* set the table id
369 * before the vrf_enable because of( at the very least )
370 * static routes being delayed for installation until
371 * during the vrf_enable callbacks.
372 */
373 zvrf = (struct zebra_vrf *)vrf->info;
374 zvrf->table_id = nl_table_id;
375
376 /* Enable the created VRF. */
377 if (!vrf_enable(vrf)) {
378 flog_err(EC_LIB_INTERFACE,
379 "Failed to enable VRF %s id %u", name,
380 ifi->ifi_index);
381 return;
382 }
383
384 } else // h->nlmsg_type == RTM_DELLINK
385 {
386 if (IS_ZEBRA_DEBUG_KERNEL)
387 zlog_debug("RTM_DELLINK for VRF %s(%u)", name,
388 ifi->ifi_index);
389
390 vrf = vrf_lookup_by_id((vrf_id_t)ifi->ifi_index);
391
392 if (!vrf) {
393 flog_warn(EC_ZEBRA_VRF_NOT_FOUND, "%s: vrf not found",
394 __func__);
395 return;
396 }
397
398 vrf_delete(vrf);
399 }
400 }
401
402 static uint32_t get_iflink_speed(struct interface *interface, int *error)
403 {
404 struct ifreq ifdata;
405 struct ethtool_cmd ecmd;
406 int sd;
407 int rc;
408 const char *ifname = interface->name;
409
410 if (error)
411 *error = 0;
412 /* initialize struct */
413 memset(&ifdata, 0, sizeof(ifdata));
414
415 /* set interface name */
416 strlcpy(ifdata.ifr_name, ifname, sizeof(ifdata.ifr_name));
417
418 /* initialize ethtool interface */
419 memset(&ecmd, 0, sizeof(ecmd));
420 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
421 ifdata.ifr_data = (caddr_t)&ecmd;
422
423 /* use ioctl to get IP address of an interface */
424 frr_with_privs(&zserv_privs) {
425 sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
426 interface->vrf_id,
427 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_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 /* Add interface.
976 * We add by index first because in some cases such as the master
977 * interface, we have the index before we have the name. Fixing
978 * back references on the slave interfaces is painful if not done
979 * this way, i.e. by creating by ifindex.
980 */
981 ifp = if_get_by_ifindex(ifi->ifi_index, vrf_id);
982 set_ifindex(ifp, ifi->ifi_index, zns); /* add it to ns struct */
983
984 if_set_name(ifp, name);
985
986 ifp->flags = ifi->ifi_flags & 0x0000fffff;
987 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
988 ifp->metric = 0;
989 ifp->speed = get_iflink_speed(ifp, NULL);
990 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
991
992 /* Set zebra interface type */
993 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
994 if (IS_ZEBRA_IF_VRF(ifp))
995 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
996
997 /*
998 * Just set the @link/lower-device ifindex. During nldump interfaces are
999 * not ordered in any fashion so we may end up getting upper devices
1000 * before lower devices. We will setup the real linkage once the dump
1001 * is complete.
1002 */
1003 zif = (struct zebra_if *)ifp->info;
1004 zif->link_ifindex = link_ifindex;
1005
1006 if (desc) {
1007 XFREE(MTYPE_TMP, zif->desc);
1008 zif->desc = XSTRDUP(MTYPE_TMP, desc);
1009 }
1010
1011 /* Hardware type and address. */
1012 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
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 1);
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 0);
1103 if (ret < 0)
1104 return ret;
1105
1106 /* Get interface information - for bridge interfaces. */
1107 ret = netlink_request_intf_addr(netlink_cmd, AF_BRIDGE, RTM_GETLINK,
1108 RTEXT_FILTER_BRVLAN);
1109 if (ret < 0)
1110 return ret;
1111 ret = netlink_parse_info(netlink_interface, netlink_cmd, &dp_info, 0,
1112 0);
1113 if (ret < 0)
1114 return ret;
1115
1116 /* fixup linkages */
1117 zebra_if_update_all_links(zns);
1118 return 0;
1119 }
1120
1121 /**
1122 * interface_addr_lookup_netlink() - Look up interface addresses
1123 *
1124 * @zns: Zebra netlink socket
1125 * Return: Result status
1126 */
1127 static int interface_addr_lookup_netlink(struct zebra_ns *zns)
1128 {
1129 int ret;
1130 struct zebra_dplane_info dp_info;
1131 struct nlsock *netlink_cmd = &zns->netlink_cmd;
1132
1133 /* Capture key info from ns struct */
1134 zebra_dplane_info_from_zns(&dp_info, zns, true /*is_cmd*/);
1135
1136 /* Get IPv4 address of the interfaces. */
1137 ret = netlink_request_intf_addr(netlink_cmd, AF_INET, RTM_GETADDR, 0);
1138 if (ret < 0)
1139 return ret;
1140 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1141 0, 1);
1142 if (ret < 0)
1143 return ret;
1144
1145 /* Get IPv6 address of the interfaces. */
1146 ret = netlink_request_intf_addr(netlink_cmd, AF_INET6, RTM_GETADDR, 0);
1147 if (ret < 0)
1148 return ret;
1149 ret = netlink_parse_info(netlink_interface_addr, netlink_cmd, &dp_info,
1150 0, 1);
1151 if (ret < 0)
1152 return ret;
1153
1154 return 0;
1155 }
1156
1157 int kernel_interface_set_master(struct interface *master,
1158 struct interface *slave)
1159 {
1160 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1161
1162 struct {
1163 struct nlmsghdr n;
1164 struct ifinfomsg ifa;
1165 char buf[NL_PKT_BUF_SIZE];
1166 } req;
1167
1168 memset(&req, 0, sizeof(req));
1169
1170 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1171 req.n.nlmsg_flags = NLM_F_REQUEST;
1172 req.n.nlmsg_type = RTM_SETLINK;
1173 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
1174
1175 req.ifa.ifi_index = slave->ifindex;
1176
1177 nl_attr_put32(&req.n, sizeof(req), IFLA_MASTER, master->ifindex);
1178 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, slave->ifindex);
1179
1180 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
1181 0);
1182 }
1183
1184 /* Interface address modification. */
1185 static ssize_t netlink_address_msg_encoder(struct zebra_dplane_ctx *ctx,
1186 void *buf, size_t buflen)
1187 {
1188 int bytelen;
1189 const struct prefix *p;
1190 int cmd;
1191 const char *label;
1192
1193 struct {
1194 struct nlmsghdr n;
1195 struct ifaddrmsg ifa;
1196 char buf[0];
1197 } *req = buf;
1198
1199 if (buflen < sizeof(*req))
1200 return 0;
1201
1202 p = dplane_ctx_get_intf_addr(ctx);
1203 memset(req, 0, sizeof(*req));
1204
1205 bytelen = (p->family == AF_INET ? 4 : 16);
1206
1207 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1208 req->n.nlmsg_flags = NLM_F_REQUEST;
1209
1210 if (dplane_ctx_get_op(ctx) == DPLANE_OP_ADDR_INSTALL)
1211 cmd = RTM_NEWADDR;
1212 else
1213 cmd = RTM_DELADDR;
1214
1215 req->n.nlmsg_type = cmd;
1216 req->ifa.ifa_family = p->family;
1217
1218 req->ifa.ifa_index = dplane_ctx_get_ifindex(ctx);
1219
1220 if (!nl_attr_put(&req->n, buflen, IFA_LOCAL, &p->u.prefix, bytelen))
1221 return 0;
1222
1223 if (p->family == AF_INET) {
1224 if (dplane_ctx_intf_is_connected(ctx)) {
1225 p = dplane_ctx_get_intf_dest(ctx);
1226 if (!nl_attr_put(&req->n, buflen, IFA_ADDRESS,
1227 &p->u.prefix, bytelen))
1228 return 0;
1229 } else if (cmd == RTM_NEWADDR) {
1230 struct in_addr broad = {
1231 .s_addr = ipv4_broadcast_addr(p->u.prefix4.s_addr,
1232 p->prefixlen)
1233 };
1234 if (!nl_attr_put(&req->n, buflen, IFA_BROADCAST, &broad,
1235 bytelen))
1236 return 0;
1237 }
1238 }
1239
1240 /* p is now either address or destination/bcast addr */
1241 req->ifa.ifa_prefixlen = p->prefixlen;
1242
1243 if (dplane_ctx_intf_is_secondary(ctx))
1244 SET_FLAG(req->ifa.ifa_flags, IFA_F_SECONDARY);
1245
1246 if (dplane_ctx_intf_has_label(ctx)) {
1247 label = dplane_ctx_get_intf_label(ctx);
1248 if (!nl_attr_put(&req->n, buflen, IFA_LABEL, label,
1249 strlen(label) + 1))
1250 return 0;
1251 }
1252
1253 return NLMSG_ALIGN(req->n.nlmsg_len);
1254 }
1255
1256 enum netlink_msg_status
1257 netlink_put_address_update_msg(struct nl_batch *bth,
1258 struct zebra_dplane_ctx *ctx)
1259 {
1260 return netlink_batch_add_msg(bth, ctx, netlink_address_msg_encoder,
1261 false);
1262 }
1263
1264 int netlink_interface_addr(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1265 {
1266 int len;
1267 struct ifaddrmsg *ifa;
1268 struct rtattr *tb[IFA_MAX + 1];
1269 struct interface *ifp;
1270 void *addr;
1271 void *broad;
1272 uint8_t flags = 0;
1273 char *label = NULL;
1274 struct zebra_ns *zns;
1275 uint32_t metric = METRIC_MAX;
1276 uint32_t kernel_flags = 0;
1277
1278 zns = zebra_ns_lookup(ns_id);
1279 ifa = NLMSG_DATA(h);
1280
1281 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1282 flog_warn(
1283 EC_ZEBRA_UNKNOWN_FAMILY,
1284 "Invalid address family: %u received from kernel interface addr change: %s",
1285 ifa->ifa_family, nl_msg_type_to_str(h->nlmsg_type));
1286 return 0;
1287 }
1288
1289 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1290 return 0;
1291
1292 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1293 if (len < 0) {
1294 zlog_err(
1295 "%s: Message received from netlink is of a broken size: %d %zu",
1296 __func__, h->nlmsg_len,
1297 (size_t)NLMSG_LENGTH(sizeof(struct ifaddrmsg)));
1298 return -1;
1299 }
1300
1301 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1302
1303 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
1304 if (ifp == NULL) {
1305 flog_err(
1306 EC_LIB_INTERFACE,
1307 "netlink_interface_addr can't find interface by index %d",
1308 ifa->ifa_index);
1309 return -1;
1310 }
1311
1312 /* Flags passed through */
1313 if (tb[IFA_FLAGS])
1314 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1315 else
1316 kernel_flags = ifa->ifa_flags;
1317
1318 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
1319 {
1320 char buf[BUFSIZ];
1321 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
1322 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
1323 kernel_flags);
1324 if (tb[IFA_LOCAL])
1325 zlog_debug(" IFA_LOCAL %s/%d",
1326 inet_ntop(ifa->ifa_family,
1327 RTA_DATA(tb[IFA_LOCAL]), buf,
1328 BUFSIZ),
1329 ifa->ifa_prefixlen);
1330 if (tb[IFA_ADDRESS])
1331 zlog_debug(" IFA_ADDRESS %s/%d",
1332 inet_ntop(ifa->ifa_family,
1333 RTA_DATA(tb[IFA_ADDRESS]), buf,
1334 BUFSIZ),
1335 ifa->ifa_prefixlen);
1336 if (tb[IFA_BROADCAST])
1337 zlog_debug(" IFA_BROADCAST %s/%d",
1338 inet_ntop(ifa->ifa_family,
1339 RTA_DATA(tb[IFA_BROADCAST]), buf,
1340 BUFSIZ),
1341 ifa->ifa_prefixlen);
1342 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
1343 zlog_debug(" IFA_LABEL %s",
1344 (char *)RTA_DATA(tb[IFA_LABEL]));
1345
1346 if (tb[IFA_CACHEINFO]) {
1347 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1348 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1349 ci->ifa_prefered, ci->ifa_valid);
1350 }
1351 }
1352
1353 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1354 if (tb[IFA_LOCAL] == NULL)
1355 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1356 if (tb[IFA_ADDRESS] == NULL)
1357 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1358
1359 /* local interface address */
1360 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1361
1362 /* is there a peer address? */
1363 if (tb[IFA_ADDRESS]
1364 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1365 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1366 broad = RTA_DATA(tb[IFA_ADDRESS]);
1367 SET_FLAG(flags, ZEBRA_IFA_PEER);
1368 } else
1369 /* seeking a broadcast address */
1370 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
1371 : NULL);
1372
1373 /* addr is primary key, SOL if we don't have one */
1374 if (addr == NULL) {
1375 zlog_debug("%s: Local Interface Address is NULL for %s",
1376 __func__, ifp->name);
1377 return -1;
1378 }
1379
1380 /* Flags. */
1381 if (kernel_flags & IFA_F_SECONDARY)
1382 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
1383
1384 /* Label */
1385 if (tb[IFA_LABEL])
1386 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1387
1388 if (label && strcmp(ifp->name, label) == 0)
1389 label = NULL;
1390
1391 if (tb[IFA_RT_PRIORITY])
1392 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1393
1394 /* Register interface address to the interface. */
1395 if (ifa->ifa_family == AF_INET) {
1396 if (ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1397 zlog_err(
1398 "Invalid prefix length: %u received from kernel interface addr change: %s",
1399 ifa->ifa_prefixlen,
1400 nl_msg_type_to_str(h->nlmsg_type));
1401 return -1;
1402 }
1403
1404 if (h->nlmsg_type == RTM_NEWADDR)
1405 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
1406 ifa->ifa_prefixlen,
1407 (struct in_addr *)broad, label,
1408 metric);
1409 else if (CHECK_FLAG(flags, ZEBRA_IFA_PEER)) {
1410 /* Delete with a peer address */
1411 connected_delete_ipv4(
1412 ifp, flags, (struct in_addr *)addr,
1413 ifa->ifa_prefixlen, broad);
1414 } else
1415 connected_delete_ipv4(
1416 ifp, flags, (struct in_addr *)addr,
1417 ifa->ifa_prefixlen, NULL);
1418 }
1419
1420 if (ifa->ifa_family == AF_INET6) {
1421 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1422 zlog_err(
1423 "Invalid prefix length: %u received from kernel interface addr change: %s",
1424 ifa->ifa_prefixlen,
1425 nl_msg_type_to_str(h->nlmsg_type));
1426 return -1;
1427 }
1428 if (h->nlmsg_type == RTM_NEWADDR) {
1429 /* Only consider valid addresses; we'll not get a
1430 * notification from
1431 * the kernel till IPv6 DAD has completed, but at init
1432 * time, Quagga
1433 * does query for and will receive all addresses.
1434 */
1435 if (!(kernel_flags
1436 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
1437 connected_add_ipv6(ifp, flags,
1438 (struct in6_addr *)addr,
1439 (struct in6_addr *)broad,
1440 ifa->ifa_prefixlen, label,
1441 metric);
1442 } else
1443 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
1444 NULL, ifa->ifa_prefixlen);
1445 }
1446
1447 /*
1448 * Linux kernel does not send route delete on interface down/addr del
1449 * so we have to re-process routes it owns (i.e. kernel routes)
1450 */
1451 if (h->nlmsg_type != RTM_NEWADDR)
1452 rib_update(RIB_UPDATE_KERNEL);
1453
1454 return 0;
1455 }
1456
1457 /*
1458 * Parse and validate an incoming interface address change message,
1459 * generating a dplane context object.
1460 * This runs in the dplane pthread; the context is enqueued to the
1461 * main pthread for processing.
1462 */
1463 int netlink_interface_addr_dplane(struct nlmsghdr *h, ns_id_t ns_id,
1464 int startup /*ignored*/)
1465 {
1466 int len;
1467 struct ifaddrmsg *ifa;
1468 struct rtattr *tb[IFA_MAX + 1];
1469 void *addr;
1470 void *broad;
1471 char *label = NULL;
1472 uint32_t metric = METRIC_MAX;
1473 uint32_t kernel_flags = 0;
1474 struct zebra_dplane_ctx *ctx;
1475 struct prefix p;
1476
1477 ifa = NLMSG_DATA(h);
1478
1479 /* Validate message types */
1480 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
1481 return 0;
1482
1483 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6) {
1484 if (IS_ZEBRA_DEBUG_KERNEL)
1485 zlog_debug("%s: %s: Invalid address family: %u",
1486 __func__, nl_msg_type_to_str(h->nlmsg_type),
1487 ifa->ifa_family);
1488 return 0;
1489 }
1490
1491 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1492 if (len < 0) {
1493 if (IS_ZEBRA_DEBUG_KERNEL)
1494 zlog_debug("%s: %s: netlink msg bad size: %d %zu",
1495 __func__, nl_msg_type_to_str(h->nlmsg_type),
1496 h->nlmsg_len,
1497 (size_t)NLMSG_LENGTH(
1498 sizeof(struct ifaddrmsg)));
1499 return -1;
1500 }
1501
1502 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
1503
1504 /* Flags passed through */
1505 if (tb[IFA_FLAGS])
1506 kernel_flags = *(int *)RTA_DATA(tb[IFA_FLAGS]);
1507 else
1508 kernel_flags = ifa->ifa_flags;
1509
1510 if (IS_ZEBRA_DEBUG_KERNEL) { /* remove this line to see initial ifcfg */
1511 char buf[PREFIX_STRLEN];
1512
1513 zlog_debug("%s: %s nsid %u ifindex %u flags 0x%x:", __func__,
1514 nl_msg_type_to_str(h->nlmsg_type), ns_id,
1515 ifa->ifa_index, kernel_flags);
1516 if (tb[IFA_LOCAL])
1517 zlog_debug(" IFA_LOCAL %s/%d",
1518 inet_ntop(ifa->ifa_family,
1519 RTA_DATA(tb[IFA_LOCAL]), buf,
1520 sizeof(buf)),
1521 ifa->ifa_prefixlen);
1522 if (tb[IFA_ADDRESS])
1523 zlog_debug(" IFA_ADDRESS %s/%d",
1524 inet_ntop(ifa->ifa_family,
1525 RTA_DATA(tb[IFA_ADDRESS]), buf,
1526 sizeof(buf)),
1527 ifa->ifa_prefixlen);
1528 if (tb[IFA_BROADCAST])
1529 zlog_debug(" IFA_BROADCAST %s/%d",
1530 inet_ntop(ifa->ifa_family,
1531 RTA_DATA(tb[IFA_BROADCAST]), buf,
1532 sizeof(buf)),
1533 ifa->ifa_prefixlen);
1534 if (tb[IFA_LABEL])
1535 zlog_debug(" IFA_LABEL %s",
1536 (const char *)RTA_DATA(tb[IFA_LABEL]));
1537
1538 if (tb[IFA_CACHEINFO]) {
1539 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
1540
1541 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
1542 ci->ifa_prefered, ci->ifa_valid);
1543 }
1544 }
1545
1546 /* Validate prefix length */
1547
1548 if (ifa->ifa_family == AF_INET
1549 && ifa->ifa_prefixlen > IPV4_MAX_BITLEN) {
1550 if (IS_ZEBRA_DEBUG_KERNEL)
1551 zlog_debug("%s: %s: Invalid prefix length: %u",
1552 __func__, nl_msg_type_to_str(h->nlmsg_type),
1553 ifa->ifa_prefixlen);
1554 return -1;
1555 }
1556
1557 if (ifa->ifa_family == AF_INET6) {
1558 if (ifa->ifa_prefixlen > IPV6_MAX_BITLEN) {
1559 if (IS_ZEBRA_DEBUG_KERNEL)
1560 zlog_debug("%s: %s: Invalid prefix length: %u",
1561 __func__,
1562 nl_msg_type_to_str(h->nlmsg_type),
1563 ifa->ifa_prefixlen);
1564 return -1;
1565 }
1566
1567 /* Only consider valid addresses; we'll not get a kernel
1568 * notification till IPv6 DAD has completed, but at init
1569 * time, FRR does query for and will receive all addresses.
1570 */
1571 if (h->nlmsg_type == RTM_NEWADDR
1572 && (kernel_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))) {
1573 if (IS_ZEBRA_DEBUG_KERNEL)
1574 zlog_debug("%s: %s: Invalid/tentative addr",
1575 __func__,
1576 nl_msg_type_to_str(h->nlmsg_type));
1577 return 0;
1578 }
1579 }
1580
1581 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
1582 if (tb[IFA_LOCAL] == NULL)
1583 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
1584 if (tb[IFA_ADDRESS] == NULL)
1585 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
1586
1587 /* local interface address */
1588 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
1589
1590 /* addr is primary key, SOL if we don't have one */
1591 if (addr == NULL) {
1592 if (IS_ZEBRA_DEBUG_KERNEL)
1593 zlog_debug("%s: %s: No local interface address",
1594 __func__, nl_msg_type_to_str(h->nlmsg_type));
1595 return -1;
1596 }
1597
1598 /* Allocate a context object, now that validation is done. */
1599 ctx = dplane_ctx_alloc();
1600 if (h->nlmsg_type == RTM_NEWADDR)
1601 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_ADD);
1602 else
1603 dplane_ctx_set_op(ctx, DPLANE_OP_INTF_ADDR_DEL);
1604
1605 dplane_ctx_set_ifindex(ctx, ifa->ifa_index);
1606 dplane_ctx_set_ns_id(ctx, ns_id);
1607
1608 /* Convert addr to prefix */
1609 memset(&p, 0, sizeof(p));
1610 p.family = ifa->ifa_family;
1611 p.prefixlen = ifa->ifa_prefixlen;
1612 if (p.family == AF_INET)
1613 p.u.prefix4 = *(struct in_addr *)addr;
1614 else
1615 p.u.prefix6 = *(struct in6_addr *)addr;
1616
1617 dplane_ctx_set_intf_addr(ctx, &p);
1618
1619 /* is there a peer address? */
1620 if (tb[IFA_ADDRESS]
1621 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
1622 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
1623 broad = RTA_DATA(tb[IFA_ADDRESS]);
1624 dplane_ctx_intf_set_connected(ctx);
1625 } else if (tb[IFA_BROADCAST]) {
1626 /* seeking a broadcast address */
1627 broad = RTA_DATA(tb[IFA_BROADCAST]);
1628 dplane_ctx_intf_set_broadcast(ctx);
1629 } else
1630 broad = NULL;
1631
1632 if (broad) {
1633 /* Convert addr to prefix */
1634 memset(&p, 0, sizeof(p));
1635 p.family = ifa->ifa_family;
1636 p.prefixlen = ifa->ifa_prefixlen;
1637 if (p.family == AF_INET)
1638 p.u.prefix4 = *(struct in_addr *)broad;
1639 else
1640 p.u.prefix6 = *(struct in6_addr *)broad;
1641
1642 dplane_ctx_set_intf_dest(ctx, &p);
1643 }
1644
1645 /* Flags. */
1646 if (kernel_flags & IFA_F_SECONDARY)
1647 dplane_ctx_intf_set_secondary(ctx);
1648
1649 /* Label */
1650 if (tb[IFA_LABEL]) {
1651 label = (char *)RTA_DATA(tb[IFA_LABEL]);
1652 dplane_ctx_set_intf_label(ctx, label);
1653 }
1654
1655 if (tb[IFA_RT_PRIORITY])
1656 metric = *(uint32_t *)RTA_DATA(tb[IFA_RT_PRIORITY]);
1657
1658 dplane_ctx_set_intf_metric(ctx, metric);
1659
1660 /* Enqueue ctx for main pthread to process */
1661 dplane_provider_enqueue_to_zebra(ctx);
1662
1663 return 0;
1664 }
1665
1666 int netlink_link_change(struct nlmsghdr *h, ns_id_t ns_id, int startup)
1667 {
1668 int len;
1669 struct ifinfomsg *ifi;
1670 struct rtattr *tb[IFLA_MAX + 1];
1671 struct rtattr *linkinfo[IFLA_MAX + 1];
1672 struct interface *ifp;
1673 char *name = NULL;
1674 char *kind = NULL;
1675 char *desc = NULL;
1676 char *slave_kind = NULL;
1677 struct zebra_ns *zns;
1678 vrf_id_t vrf_id = VRF_DEFAULT;
1679 enum zebra_iftype zif_type = ZEBRA_IF_OTHER;
1680 enum zebra_slave_iftype zif_slave_type = ZEBRA_IF_SLAVE_NONE;
1681 ifindex_t bridge_ifindex = IFINDEX_INTERNAL;
1682 ifindex_t bond_ifindex = IFINDEX_INTERNAL;
1683 ifindex_t link_ifindex = IFINDEX_INTERNAL;
1684 uint8_t old_hw_addr[INTERFACE_HWADDR_MAX];
1685 struct zebra_if *zif;
1686 ns_id_t link_nsid = ns_id;
1687 ifindex_t master_infindex = IFINDEX_INTERNAL;
1688 uint8_t bypass = 0;
1689
1690 zns = zebra_ns_lookup(ns_id);
1691 ifi = NLMSG_DATA(h);
1692
1693 /* assume if not default zns, then new VRF */
1694 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
1695 /* If this is not link add/delete message so print warning. */
1696 zlog_debug("netlink_link_change: wrong kernel message %s",
1697 nl_msg_type_to_str(h->nlmsg_type));
1698 return 0;
1699 }
1700
1701 if (!(ifi->ifi_family == AF_UNSPEC || ifi->ifi_family == AF_BRIDGE
1702 || ifi->ifi_family == AF_INET6)) {
1703 flog_warn(
1704 EC_ZEBRA_UNKNOWN_FAMILY,
1705 "Invalid address family: %u received from kernel link change: %s",
1706 ifi->ifi_family, nl_msg_type_to_str(h->nlmsg_type));
1707 return 0;
1708 }
1709
1710 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
1711 if (len < 0) {
1712 zlog_err(
1713 "%s: Message received from netlink is of a broken size %d %zu",
1714 __func__, h->nlmsg_len,
1715 (size_t)NLMSG_LENGTH(sizeof(struct ifinfomsg)));
1716 return -1;
1717 }
1718
1719 /* We are interested in some AF_BRIDGE notifications. */
1720 if (ifi->ifi_family == AF_BRIDGE)
1721 return netlink_bridge_interface(h, len, ns_id, startup);
1722
1723 /* Looking up interface name. */
1724 memset(linkinfo, 0, sizeof(linkinfo));
1725 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
1726
1727 /* check for wireless messages to ignore */
1728 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
1729 if (IS_ZEBRA_DEBUG_KERNEL)
1730 zlog_debug("%s: ignoring IFLA_WIRELESS message",
1731 __func__);
1732 return 0;
1733 }
1734
1735 if (tb[IFLA_IFNAME] == NULL)
1736 return -1;
1737 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
1738
1739 /* Must be valid string. */
1740 len = RTA_PAYLOAD(tb[IFLA_IFNAME]);
1741 if (len < 2 || name[len - 1] != '\0') {
1742 if (IS_ZEBRA_DEBUG_KERNEL)
1743 zlog_debug("%s: invalid intf name", __func__);
1744 return -1;
1745 }
1746
1747 if (tb[IFLA_LINKINFO]) {
1748 netlink_parse_rtattr_nested(linkinfo, IFLA_INFO_MAX,
1749 tb[IFLA_LINKINFO]);
1750
1751 if (linkinfo[IFLA_INFO_KIND])
1752 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
1753
1754 if (linkinfo[IFLA_INFO_SLAVE_KIND])
1755 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
1756
1757 netlink_determine_zebra_iftype(kind, &zif_type);
1758 }
1759
1760 /* If linking to another interface, note it. */
1761 if (tb[IFLA_LINK])
1762 link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
1763
1764 if (tb[IFLA_LINK_NETNSID]) {
1765 link_nsid = *(ns_id_t *)RTA_DATA(tb[IFLA_LINK_NETNSID]);
1766 link_nsid = ns_id_get_absolute(ns_id, link_nsid);
1767 }
1768 if (tb[IFLA_IFALIAS]) {
1769 desc = (char *)RTA_DATA(tb[IFLA_IFALIAS]);
1770 }
1771
1772 /* If VRF, create or update the VRF structure itself. */
1773 if (zif_type == ZEBRA_IF_VRF && !vrf_is_backend_netns()) {
1774 netlink_vrf_change(h, tb[IFLA_LINKINFO], ns_id, name);
1775 vrf_id = (vrf_id_t)ifi->ifi_index;
1776 }
1777
1778 /* See if interface is present. */
1779 ifp = if_lookup_by_name_per_ns(zns, name);
1780
1781 if (h->nlmsg_type == RTM_NEWLINK) {
1782 if (tb[IFLA_MASTER]) {
1783 if (slave_kind && (strcmp(slave_kind, "vrf") == 0)
1784 && !vrf_is_backend_netns()) {
1785 zif_slave_type = ZEBRA_IF_SLAVE_VRF;
1786 master_infindex = vrf_id =
1787 *(uint32_t *)RTA_DATA(tb[IFLA_MASTER]);
1788 } else if (slave_kind
1789 && (strcmp(slave_kind, "bridge") == 0)) {
1790 zif_slave_type = ZEBRA_IF_SLAVE_BRIDGE;
1791 master_infindex = bridge_ifindex =
1792 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1793 } else if (slave_kind
1794 && (strcmp(slave_kind, "bond") == 0)) {
1795 zif_slave_type = ZEBRA_IF_SLAVE_BOND;
1796 master_infindex = bond_ifindex =
1797 *(ifindex_t *)RTA_DATA(tb[IFLA_MASTER]);
1798 bypass = netlink_parse_lacp_bypass(linkinfo);
1799 } else
1800 zif_slave_type = ZEBRA_IF_SLAVE_OTHER;
1801 }
1802 if (vrf_is_backend_netns())
1803 vrf_id = (vrf_id_t)ns_id;
1804 if (ifp == NULL
1805 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
1806 /* Add interface notification from kernel */
1807 if (IS_ZEBRA_DEBUG_KERNEL)
1808 zlog_debug(
1809 "RTM_NEWLINK ADD for %s(%u) vrf_id %u type %d sl_type %d master %u flags 0x%x",
1810 name, ifi->ifi_index, vrf_id, zif_type,
1811 zif_slave_type, master_infindex,
1812 ifi->ifi_flags);
1813
1814 if (ifp == NULL) {
1815 /* unknown interface */
1816 ifp = if_get_by_name(name, vrf_id);
1817 } else {
1818 /* pre-configured interface, learnt now */
1819 if (ifp->vrf_id != vrf_id)
1820 if_update_to_new_vrf(ifp, vrf_id);
1821 }
1822
1823 /* Update interface information. */
1824 set_ifindex(ifp, ifi->ifi_index, zns);
1825 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1826 if (!tb[IFLA_MTU]) {
1827 zlog_debug(
1828 "RTM_NEWLINK for interface %s(%u) without MTU set",
1829 name, ifi->ifi_index);
1830 return 0;
1831 }
1832 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1833 ifp->metric = 0;
1834 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
1835
1836 /* Set interface type */
1837 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1838 if (IS_ZEBRA_IF_VRF(ifp))
1839 SET_FLAG(ifp->status,
1840 ZEBRA_INTERFACE_VRF_LOOPBACK);
1841
1842 /* Update link. */
1843 zebra_if_update_link(ifp, link_ifindex, ns_id);
1844
1845 netlink_interface_update_hw_addr(tb, ifp);
1846
1847 /* Inform clients, install any configured addresses. */
1848 if_add_update(ifp);
1849
1850 /* Extract and save L2 interface information, take
1851 * additional actions. */
1852 netlink_interface_update_l2info(
1853 ifp, linkinfo[IFLA_INFO_DATA],
1854 1, link_nsid);
1855 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
1856 zebra_l2if_update_bridge_slave(
1857 ifp, bridge_ifindex, ns_id,
1858 ZEBRA_BRIDGE_NO_ACTION);
1859 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
1860 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
1861 !!bypass);
1862
1863 if (tb[IFLA_PROTO_DOWN]) {
1864 uint8_t protodown;
1865
1866 protodown = *(uint8_t *)RTA_DATA(
1867 tb[IFLA_PROTO_DOWN]);
1868 netlink_proc_dplane_if_protodown(ifp->info,
1869 !!protodown);
1870 }
1871 } else if (ifp->vrf_id != vrf_id) {
1872 /* VRF change for an interface. */
1873 if (IS_ZEBRA_DEBUG_KERNEL)
1874 zlog_debug(
1875 "RTM_NEWLINK vrf-change for %s(%u) vrf_id %u -> %u flags 0x%x",
1876 name, ifp->ifindex, ifp->vrf_id, vrf_id,
1877 ifi->ifi_flags);
1878
1879 if_handle_vrf_change(ifp, vrf_id);
1880 } else {
1881 bool was_bridge_slave, was_bond_slave;
1882 uint8_t chgflags = ZEBRA_BRIDGE_NO_ACTION;
1883
1884 /* Interface update. */
1885 if (IS_ZEBRA_DEBUG_KERNEL)
1886 zlog_debug(
1887 "RTM_NEWLINK update for %s(%u) sl_type %d master %u flags 0x%x",
1888 name, ifp->ifindex, zif_slave_type,
1889 master_infindex, ifi->ifi_flags);
1890
1891 set_ifindex(ifp, ifi->ifi_index, zns);
1892 if (!tb[IFLA_MTU]) {
1893 zlog_debug(
1894 "RTM_NEWLINK for interface %s(%u) without MTU set",
1895 name, ifi->ifi_index);
1896 return 0;
1897 }
1898 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
1899 ifp->metric = 0;
1900
1901 /* Update interface type - NOTE: Only slave_type can
1902 * change. */
1903 was_bridge_slave = IS_ZEBRA_IF_BRIDGE_SLAVE(ifp);
1904 was_bond_slave = IS_ZEBRA_IF_BOND_SLAVE(ifp);
1905 zebra_if_set_ziftype(ifp, zif_type, zif_slave_type);
1906
1907 memcpy(old_hw_addr, ifp->hw_addr, INTERFACE_HWADDR_MAX);
1908
1909 /* Update link. */
1910 zebra_if_update_link(ifp, link_ifindex, ns_id);
1911
1912 netlink_interface_update_hw_addr(tb, ifp);
1913
1914 if (if_is_no_ptm_operative(ifp)) {
1915 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1916 if (!if_is_no_ptm_operative(ifp)) {
1917 if (IS_ZEBRA_DEBUG_KERNEL)
1918 zlog_debug(
1919 "Intf %s(%u) has gone DOWN",
1920 name, ifp->ifindex);
1921 if_down(ifp);
1922 rib_update(RIB_UPDATE_KERNEL);
1923 } else if (if_is_operative(ifp)) {
1924 bool mac_updated = false;
1925
1926 /* Must notify client daemons of new
1927 * interface status. */
1928 if (IS_ZEBRA_DEBUG_KERNEL)
1929 zlog_debug(
1930 "Intf %s(%u) PTM up, notifying clients",
1931 name, ifp->ifindex);
1932 zebra_interface_up_update(ifp);
1933
1934 /* Update EVPN VNI when SVI MAC change
1935 */
1936 if (memcmp(old_hw_addr, ifp->hw_addr,
1937 INTERFACE_HWADDR_MAX))
1938 mac_updated = true;
1939 if (IS_ZEBRA_IF_VLAN(ifp)
1940 && mac_updated) {
1941 struct interface *link_if;
1942
1943 link_if =
1944 if_lookup_by_index_per_ns(
1945 zebra_ns_lookup(NS_DEFAULT),
1946 link_ifindex);
1947 if (link_if)
1948 zebra_vxlan_svi_up(ifp,
1949 link_if);
1950 } else if (mac_updated
1951 && IS_ZEBRA_IF_BRIDGE(ifp)) {
1952 zlog_debug(
1953 "Intf %s(%u) bridge changed MAC address",
1954 name, ifp->ifindex);
1955 chgflags =
1956 ZEBRA_BRIDGE_MASTER_MAC_CHANGE;
1957 }
1958 }
1959 } else {
1960 ifp->flags = ifi->ifi_flags & 0x0000fffff;
1961 if (if_is_operative(ifp)) {
1962 if (IS_ZEBRA_DEBUG_KERNEL)
1963 zlog_debug(
1964 "Intf %s(%u) has come UP",
1965 name, ifp->ifindex);
1966 if_up(ifp);
1967 if (IS_ZEBRA_IF_BRIDGE(ifp))
1968 chgflags =
1969 ZEBRA_BRIDGE_MASTER_UP;
1970 } else {
1971 if (IS_ZEBRA_DEBUG_KERNEL)
1972 zlog_debug(
1973 "Intf %s(%u) has gone DOWN",
1974 name, ifp->ifindex);
1975 if_down(ifp);
1976 rib_update(RIB_UPDATE_KERNEL);
1977 }
1978 }
1979
1980 /* Extract and save L2 interface information, take
1981 * additional actions. */
1982 netlink_interface_update_l2info(
1983 ifp, linkinfo[IFLA_INFO_DATA],
1984 0, link_nsid);
1985 if (IS_ZEBRA_IF_BRIDGE(ifp))
1986 zebra_l2if_update_bridge(ifp, chgflags);
1987 if (IS_ZEBRA_IF_BOND(ifp))
1988 zebra_l2if_update_bond(ifp, true);
1989 if (IS_ZEBRA_IF_BRIDGE_SLAVE(ifp) || was_bridge_slave)
1990 zebra_l2if_update_bridge_slave(
1991 ifp, bridge_ifindex, ns_id, chgflags);
1992 else if (IS_ZEBRA_IF_BOND_SLAVE(ifp) || was_bond_slave)
1993 zebra_l2if_update_bond_slave(ifp, bond_ifindex,
1994 !!bypass);
1995
1996 if (tb[IFLA_PROTO_DOWN]) {
1997 uint8_t protodown;
1998
1999 protodown = *(uint8_t *)RTA_DATA(
2000 tb[IFLA_PROTO_DOWN]);
2001 netlink_proc_dplane_if_protodown(ifp->info,
2002 !!protodown);
2003 }
2004 }
2005
2006 zif = ifp->info;
2007 if (zif) {
2008 XFREE(MTYPE_TMP, zif->desc);
2009 if (desc)
2010 zif->desc = XSTRDUP(MTYPE_TMP, desc);
2011 }
2012 } else {
2013 /* Delete interface notification from kernel */
2014 if (ifp == NULL) {
2015 if (IS_ZEBRA_DEBUG_KERNEL)
2016 zlog_debug(
2017 "RTM_DELLINK for unknown interface %s(%u)",
2018 name, ifi->ifi_index);
2019 return 0;
2020 }
2021
2022 if (IS_ZEBRA_DEBUG_KERNEL)
2023 zlog_debug("RTM_DELLINK for %s(%u)", name,
2024 ifp->ifindex);
2025
2026 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
2027
2028 if (IS_ZEBRA_IF_BOND(ifp))
2029 zebra_l2if_update_bond(ifp, false);
2030 if (IS_ZEBRA_IF_BOND_SLAVE(ifp))
2031 zebra_l2if_update_bond_slave(ifp, bond_ifindex, false);
2032 /* Special handling for bridge or VxLAN interfaces. */
2033 if (IS_ZEBRA_IF_BRIDGE(ifp))
2034 zebra_l2_bridge_del(ifp);
2035 else if (IS_ZEBRA_IF_VXLAN(ifp))
2036 zebra_l2_vxlanif_del(ifp);
2037
2038 if_delete_update(ifp);
2039 }
2040
2041 return 0;
2042 }
2043
2044 int netlink_protodown(struct interface *ifp, bool down)
2045 {
2046 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
2047
2048 struct {
2049 struct nlmsghdr n;
2050 struct ifinfomsg ifa;
2051 char buf[NL_PKT_BUF_SIZE];
2052 } req;
2053
2054 memset(&req, 0, sizeof(req));
2055
2056 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
2057 req.n.nlmsg_flags = NLM_F_REQUEST;
2058 req.n.nlmsg_type = RTM_SETLINK;
2059 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
2060
2061 req.ifa.ifi_index = ifp->ifindex;
2062
2063 nl_attr_put(&req.n, sizeof(req), IFLA_PROTO_DOWN, &down, sizeof(down));
2064 nl_attr_put32(&req.n, sizeof(req), IFLA_LINK, ifp->ifindex);
2065
2066 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
2067 0);
2068 }
2069
2070 /* Interface information read by netlink. */
2071 void interface_list(struct zebra_ns *zns)
2072 {
2073 interface_lookup_netlink(zns);
2074 /* We add routes for interface address,
2075 * so we need to get the nexthop info
2076 * from the kernel before we can do that
2077 */
2078 netlink_nexthop_read(zns);
2079
2080 interface_addr_lookup_netlink(zns);
2081 }
2082
2083 #endif /* GNU_LINUX */