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