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