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