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