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