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