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