]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_netlink.c
debian: add pkg-config to build-depends
[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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24 #include <net/if_arp.h>
25 #include <linux/sockios.h>
26 #include <linux/ethtool.h>
27
28 #include "linklist.h"
29 #include "if.h"
30 #include "log.h"
31 #include "prefix.h"
32 #include "connected.h"
33 #include "table.h"
34 #include "memory.h"
35 #include "zebra_memory.h"
36 #include "rib.h"
37 #include "thread.h"
38 #include "privs.h"
39 #include "nexthop.h"
40 #include "vrf.h"
41 #include "mpls.h"
42
43 #include "vty.h"
44 #include "zebra/zserv.h"
45 #include "zebra/zebra_ns.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/rt.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/interface.h"
50 #include "zebra/debug.h"
51 #include "zebra/rtadv.h"
52 #include "zebra/zebra_ptm.h"
53 #include "zebra/zebra_mpls.h"
54 #include "zebra/kernel_netlink.h"
55 #include "zebra/if_netlink.h"
56
57
58 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
59 names and ifindex values. */
60 static void set_ifindex(struct interface *ifp, ifindex_t ifi_index,
61 struct zebra_ns *zns)
62 {
63 struct interface *oifp;
64
65 if (((oifp = if_lookup_by_index_per_ns(zns, ifi_index)) != NULL)
66 && (oifp != ifp)) {
67 if (ifi_index == IFINDEX_INTERNAL)
68 zlog_err(
69 "Netlink is setting interface %s ifindex to reserved "
70 "internal value %u",
71 ifp->name, ifi_index);
72 else {
73 if (IS_ZEBRA_DEBUG_KERNEL)
74 zlog_debug(
75 "interface index %d was renamed from %s to %s",
76 ifi_index, oifp->name, ifp->name);
77 if (if_is_up(oifp))
78 zlog_err(
79 "interface rename detected on up interface: index %d "
80 "was renamed from %s to %s, results are uncertain!",
81 ifi_index, oifp->name, ifp->name);
82 if_delete_update(oifp);
83 }
84 }
85 ifp->ifindex = ifi_index;
86 }
87
88 /* Utility function to parse hardware link-layer address and update ifp */
89 static void netlink_interface_update_hw_addr(struct rtattr **tb,
90 struct interface *ifp)
91 {
92 int i;
93
94 if (tb[IFLA_ADDRESS]) {
95 int hw_addr_len;
96
97 hw_addr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
98
99 if (hw_addr_len > INTERFACE_HWADDR_MAX)
100 zlog_warn("Hardware address is too large: %d",
101 hw_addr_len);
102 else {
103 ifp->hw_addr_len = hw_addr_len;
104 memcpy(ifp->hw_addr, RTA_DATA(tb[IFLA_ADDRESS]),
105 hw_addr_len);
106
107 for (i = 0; i < hw_addr_len; i++)
108 if (ifp->hw_addr[i] != 0)
109 break;
110
111 if (i == hw_addr_len)
112 ifp->hw_addr_len = 0;
113 else
114 ifp->hw_addr_len = hw_addr_len;
115 }
116 }
117 }
118
119 static enum zebra_link_type netlink_to_zebra_link_type(unsigned int hwt)
120 {
121 switch (hwt) {
122 case ARPHRD_ETHER:
123 return ZEBRA_LLT_ETHER;
124 case ARPHRD_EETHER:
125 return ZEBRA_LLT_EETHER;
126 case ARPHRD_AX25:
127 return ZEBRA_LLT_AX25;
128 case ARPHRD_PRONET:
129 return ZEBRA_LLT_PRONET;
130 case ARPHRD_IEEE802:
131 return ZEBRA_LLT_IEEE802;
132 case ARPHRD_ARCNET:
133 return ZEBRA_LLT_ARCNET;
134 case ARPHRD_APPLETLK:
135 return ZEBRA_LLT_APPLETLK;
136 case ARPHRD_DLCI:
137 return ZEBRA_LLT_DLCI;
138 case ARPHRD_ATM:
139 return ZEBRA_LLT_ATM;
140 case ARPHRD_METRICOM:
141 return ZEBRA_LLT_METRICOM;
142 case ARPHRD_IEEE1394:
143 return ZEBRA_LLT_IEEE1394;
144 case ARPHRD_EUI64:
145 return ZEBRA_LLT_EUI64;
146 case ARPHRD_INFINIBAND:
147 return ZEBRA_LLT_INFINIBAND;
148 case ARPHRD_SLIP:
149 return ZEBRA_LLT_SLIP;
150 case ARPHRD_CSLIP:
151 return ZEBRA_LLT_CSLIP;
152 case ARPHRD_SLIP6:
153 return ZEBRA_LLT_SLIP6;
154 case ARPHRD_CSLIP6:
155 return ZEBRA_LLT_CSLIP6;
156 case ARPHRD_RSRVD:
157 return ZEBRA_LLT_RSRVD;
158 case ARPHRD_ADAPT:
159 return ZEBRA_LLT_ADAPT;
160 case ARPHRD_ROSE:
161 return ZEBRA_LLT_ROSE;
162 case ARPHRD_X25:
163 return ZEBRA_LLT_X25;
164 case ARPHRD_PPP:
165 return ZEBRA_LLT_PPP;
166 case ARPHRD_CISCO:
167 return ZEBRA_LLT_CHDLC;
168 case ARPHRD_LAPB:
169 return ZEBRA_LLT_LAPB;
170 case ARPHRD_RAWHDLC:
171 return ZEBRA_LLT_RAWHDLC;
172 case ARPHRD_TUNNEL:
173 return ZEBRA_LLT_IPIP;
174 case ARPHRD_TUNNEL6:
175 return ZEBRA_LLT_IPIP6;
176 case ARPHRD_FRAD:
177 return ZEBRA_LLT_FRAD;
178 case ARPHRD_SKIP:
179 return ZEBRA_LLT_SKIP;
180 case ARPHRD_LOOPBACK:
181 return ZEBRA_LLT_LOOPBACK;
182 case ARPHRD_LOCALTLK:
183 return ZEBRA_LLT_LOCALTLK;
184 case ARPHRD_FDDI:
185 return ZEBRA_LLT_FDDI;
186 case ARPHRD_SIT:
187 return ZEBRA_LLT_SIT;
188 case ARPHRD_IPDDP:
189 return ZEBRA_LLT_IPDDP;
190 case ARPHRD_IPGRE:
191 return ZEBRA_LLT_IPGRE;
192 case ARPHRD_PIMREG:
193 return ZEBRA_LLT_PIMREG;
194 case ARPHRD_HIPPI:
195 return ZEBRA_LLT_HIPPI;
196 case ARPHRD_ECONET:
197 return ZEBRA_LLT_ECONET;
198 case ARPHRD_IRDA:
199 return ZEBRA_LLT_IRDA;
200 case ARPHRD_FCPP:
201 return ZEBRA_LLT_FCPP;
202 case ARPHRD_FCAL:
203 return ZEBRA_LLT_FCAL;
204 case ARPHRD_FCPL:
205 return ZEBRA_LLT_FCPL;
206 case ARPHRD_FCFABRIC:
207 return ZEBRA_LLT_FCFABRIC;
208 case ARPHRD_IEEE802_TR:
209 return ZEBRA_LLT_IEEE802_TR;
210 case ARPHRD_IEEE80211:
211 return ZEBRA_LLT_IEEE80211;
212 case ARPHRD_IEEE802154:
213 return ZEBRA_LLT_IEEE802154;
214 #ifdef ARPHRD_IP6GRE
215 case ARPHRD_IP6GRE:
216 return ZEBRA_LLT_IP6GRE;
217 #endif
218 #ifdef ARPHRD_IEEE802154_PHY
219 case ARPHRD_IEEE802154_PHY:
220 return ZEBRA_LLT_IEEE802154_PHY;
221 #endif
222
223 default:
224 return ZEBRA_LLT_UNKNOWN;
225 }
226 }
227
228
229 // Temporary Assignments to compile on older platforms.
230 #ifndef IFLA_BR_MAX
231 #define IFLA_BR_MAX 39
232 #endif
233
234 #ifndef IFLA_VXLAN_ID
235 #define IFLA_VXLAN_ID 1
236 #endif
237
238 #ifndef IFLA_VXLAN_LOCAL
239 #define IFLA_VXLAN_LOCAL 4
240 #endif
241
242 #ifndef IFLA_VXLAN_MAX
243 #define IFLA_VXLAN_MAX 26
244 #endif
245
246 #ifndef IFLA_BRIDGE_MAX
247 #define IFLA_BRIDGE_MAX 2
248 #endif
249
250 #ifndef IFLA_BRIDGE_VLAN_INFO
251 #define IFLA_BRIDGE_VLAN_INFO 2
252 #endif
253
254 #ifndef BRIDGE_VLAN_INFO_PVID
255 #define BRIDGE_VLAN_INFO_PVID (1<<1)
256 #endif
257
258 #ifndef RTEXT_FILTER_BRVLAN
259 #define RTEXT_FILTER_BRVLAN (1<<1)
260 #endif
261
262 #ifndef NTF_SELF
263 #define NTF_SELF 0x02
264 #endif
265
266 #ifndef IFLA_BR_VLAN_FILTERING
267 #define IFLA_BR_VLAN_FILTERING 7
268 #endif
269
270 #define parse_rtattr_nested(tb, max, rta) \
271 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
272
273 static void netlink_vrf_change(struct nlmsghdr *h, struct rtattr *tb,
274 const char *name)
275 {
276 struct ifinfomsg *ifi;
277 struct rtattr *linkinfo[IFLA_INFO_MAX + 1];
278 struct rtattr *attr[IFLA_VRF_MAX + 1];
279 struct vrf *vrf;
280 struct zebra_vrf *zvrf;
281 u_int32_t nl_table_id;
282
283 ifi = NLMSG_DATA(h);
284
285 memset(linkinfo, 0, sizeof linkinfo);
286 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
287
288 if (!linkinfo[IFLA_INFO_DATA]) {
289 if (IS_ZEBRA_DEBUG_KERNEL)
290 zlog_debug(
291 "%s: IFLA_INFO_DATA missing from VRF message: %s",
292 __func__, name);
293 return;
294 }
295
296 memset(attr, 0, sizeof attr);
297 parse_rtattr_nested(attr, IFLA_VRF_MAX, linkinfo[IFLA_INFO_DATA]);
298 if (!attr[IFLA_VRF_TABLE]) {
299 if (IS_ZEBRA_DEBUG_KERNEL)
300 zlog_debug(
301 "%s: IFLA_VRF_TABLE missing from VRF message: %s",
302 __func__, name);
303 return;
304 }
305
306 nl_table_id = *(u_int32_t *)RTA_DATA(attr[IFLA_VRF_TABLE]);
307
308 if (h->nlmsg_type == RTM_NEWLINK) {
309 if (IS_ZEBRA_DEBUG_KERNEL)
310 zlog_debug("RTM_NEWLINK for VRF %s(%u) table %u", name,
311 ifi->ifi_index, nl_table_id);
312
313 /*
314 * vrf_get is implied creation if it does not exist
315 */
316 vrf = vrf_get((vrf_id_t)ifi->ifi_index,
317 name); // It would create vrf
318 if (!vrf) {
319 zlog_err("VRF %s id %u not created", name,
320 ifi->ifi_index);
321 return;
322 }
323
324 /* Enable the created VRF. */
325 if (!vrf_enable(vrf)) {
326 zlog_err("Failed to enable VRF %s id %u", name,
327 ifi->ifi_index);
328 return;
329 }
330
331 /*
332 * This is the only place that we get the actual kernel table_id
333 * being used. We need it to set the table_id of the routes
334 * we are passing to the kernel.... And to throw some totally
335 * awesome parties. that too.
336 */
337 zvrf = (struct zebra_vrf *)vrf->info;
338 zvrf->table_id = nl_table_id;
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(const char *ifname)
357 {
358 struct ifreq ifdata;
359 struct ethtool_cmd ecmd;
360 int sd;
361 int rc;
362
363 /* initialize struct */
364 memset(&ifdata, 0, sizeof(ifdata));
365
366 /* set interface name */
367 strcpy(ifdata.ifr_name, ifname);
368
369 /* initialize ethtool interface */
370 memset(&ecmd, 0, sizeof(ecmd));
371 ecmd.cmd = ETHTOOL_GSET; /* ETHTOOL_GLINK */
372 ifdata.ifr_data = (__caddr_t)&ecmd;
373
374 /* use ioctl to get IP address of an interface */
375 sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
376 if (sd < 0) {
377 zlog_debug("Failure to read interface %s speed: %d %s", ifname,
378 errno, safe_strerror(errno));
379 return 0;
380 }
381
382 /* Get the current link state for the interface */
383 rc = ioctl(sd, SIOCETHTOOL, (char *)&ifdata);
384 if (rc < 0) {
385 zlog_debug("IOCTL failure to read interface %s speed: %d %s",
386 ifname, errno, safe_strerror(errno));
387 ecmd.speed_hi = 0;
388 ecmd.speed = 0;
389 }
390
391 close(sd);
392
393 return (ecmd.speed_hi << 16) | ecmd.speed;
394 }
395
396 /* Called from interface_lookup_netlink(). This function is only used
397 during bootstrap. */
398 static int netlink_interface(struct sockaddr_nl *snl, struct nlmsghdr *h,
399 ns_id_t ns_id, int startup)
400 {
401 int len;
402 struct ifinfomsg *ifi;
403 struct rtattr *tb[IFLA_MAX + 1];
404 struct rtattr *linkinfo[IFLA_MAX + 1];
405 struct interface *ifp;
406 char *name = NULL;
407 char *kind = NULL;
408 char *slave_kind = NULL;
409 int vrf_device = 0;
410 struct zebra_ns *zns;
411 vrf_id_t vrf_id = VRF_DEFAULT;
412
413 zns = zebra_ns_lookup(ns_id);
414 ifi = NLMSG_DATA(h);
415
416 if (h->nlmsg_type != RTM_NEWLINK)
417 return 0;
418
419 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
420 if (len < 0)
421 return -1;
422
423 if (ifi->ifi_family == AF_BRIDGE)
424 return 0;
425
426 /* Looking up interface name. */
427 memset(tb, 0, sizeof tb);
428 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
429
430 #ifdef IFLA_WIRELESS
431 /* check for wireless messages to ignore */
432 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
433 if (IS_ZEBRA_DEBUG_KERNEL)
434 zlog_debug("%s: ignoring IFLA_WIRELESS message",
435 __func__);
436 return 0;
437 }
438 #endif /* IFLA_WIRELESS */
439
440 if (tb[IFLA_IFNAME] == NULL)
441 return -1;
442 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
443
444 if (tb[IFLA_LINKINFO]) {
445 memset(linkinfo, 0, sizeof linkinfo);
446 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
447
448 if (linkinfo[IFLA_INFO_KIND])
449 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
450
451 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
452 if (linkinfo[IFLA_INFO_SLAVE_KIND])
453 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
454 #endif
455
456 if (kind && strcmp(kind, "vrf") == 0) {
457 vrf_device = 1;
458 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
459 vrf_id = (vrf_id_t)ifi->ifi_index;
460 }
461 }
462
463 if (tb[IFLA_MASTER]) {
464 if (slave_kind && (strcmp(slave_kind, "vrf") == 0))
465 vrf_id = *(u_int32_t *)RTA_DATA(tb[IFLA_MASTER]);
466 }
467
468 /* Add interface. */
469 ifp = if_get_by_name(name, vrf_id);
470 set_ifindex(ifp, ifi->ifi_index, zns);
471 ifp->flags = ifi->ifi_flags & 0x0000fffff;
472 if (vrf_device)
473 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
474 ifp->mtu6 = ifp->mtu = *(uint32_t *)RTA_DATA(tb[IFLA_MTU]);
475 ifp->metric = 0;
476 ifp->speed = get_iflink_speed(name);
477 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
478
479 /* Hardware type and address. */
480 ifp->ll_type = netlink_to_zebra_link_type(ifi->ifi_type);
481 netlink_interface_update_hw_addr(tb, ifp);
482
483 if_add_update(ifp);
484
485 return 0;
486 }
487
488 /* Interface lookup by netlink socket. */
489 int interface_lookup_netlink(struct zebra_ns *zns)
490 {
491 int ret;
492
493 /* Get interface information. */
494 ret = netlink_request(AF_PACKET, RTM_GETLINK, &zns->netlink_cmd);
495 if (ret < 0)
496 return ret;
497 ret = netlink_parse_info(netlink_interface, &zns->netlink_cmd, zns, 0,
498 1);
499 if (ret < 0)
500 return ret;
501
502 /* Get IPv4 address of the interfaces. */
503 ret = netlink_request(AF_INET, RTM_GETADDR, &zns->netlink_cmd);
504 if (ret < 0)
505 return ret;
506 ret = netlink_parse_info(netlink_interface_addr, &zns->netlink_cmd, zns,
507 0, 1);
508 if (ret < 0)
509 return ret;
510
511 /* Get IPv6 address of the interfaces. */
512 ret = netlink_request(AF_INET6, RTM_GETADDR, &zns->netlink_cmd);
513 if (ret < 0)
514 return ret;
515 ret = netlink_parse_info(netlink_interface_addr, &zns->netlink_cmd, zns,
516 0, 1);
517 if (ret < 0)
518 return ret;
519
520 return 0;
521 }
522
523 /* Interface address modification. */
524 static int netlink_address(int cmd, int family, struct interface *ifp,
525 struct connected *ifc)
526 {
527 int bytelen;
528 struct prefix *p;
529
530 struct {
531 struct nlmsghdr n;
532 struct ifaddrmsg ifa;
533 char buf[NL_PKT_BUF_SIZE];
534 } req;
535
536 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
537
538 p = ifc->address;
539 memset(&req, 0, sizeof req - NL_PKT_BUF_SIZE);
540
541 bytelen = (family == AF_INET ? 4 : 16);
542
543 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
544 req.n.nlmsg_flags = NLM_F_REQUEST;
545 req.n.nlmsg_type = cmd;
546 req.ifa.ifa_family = family;
547
548 req.ifa.ifa_index = ifp->ifindex;
549 req.ifa.ifa_prefixlen = p->prefixlen;
550
551 addattr_l(&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
552
553 if (family == AF_INET && cmd == RTM_NEWADDR) {
554 if (!CONNECTED_PEER(ifc) && ifc->destination) {
555 p = ifc->destination;
556 addattr_l(&req.n, sizeof req, IFA_BROADCAST,
557 &p->u.prefix, bytelen);
558 }
559 }
560
561 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
562 SET_FLAG(req.ifa.ifa_flags, IFA_F_SECONDARY);
563
564 if (ifc->label)
565 addattr_l(&req.n, sizeof req, IFA_LABEL, ifc->label,
566 strlen(ifc->label) + 1);
567
568 return netlink_talk(netlink_talk_filter, &req.n, &zns->netlink_cmd, zns,
569 0);
570 }
571
572 int kernel_address_add_ipv4(struct interface *ifp, struct connected *ifc)
573 {
574 return netlink_address(RTM_NEWADDR, AF_INET, ifp, ifc);
575 }
576
577 int kernel_address_delete_ipv4(struct interface *ifp, struct connected *ifc)
578 {
579 return netlink_address(RTM_DELADDR, AF_INET, ifp, ifc);
580 }
581
582 int netlink_interface_addr(struct sockaddr_nl *snl, struct nlmsghdr *h,
583 ns_id_t ns_id, int startup)
584 {
585 int len;
586 struct ifaddrmsg *ifa;
587 struct rtattr *tb[IFA_MAX + 1];
588 struct interface *ifp;
589 void *addr;
590 void *broad;
591 u_char flags = 0;
592 char *label = NULL;
593 struct zebra_ns *zns;
594
595 zns = zebra_ns_lookup(ns_id);
596 ifa = NLMSG_DATA(h);
597
598 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6)
599 return 0;
600
601 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
602 return 0;
603
604 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
605 if (len < 0)
606 return -1;
607
608 memset(tb, 0, sizeof tb);
609 netlink_parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
610
611 ifp = if_lookup_by_index_per_ns(zns, ifa->ifa_index);
612 if (ifp == NULL) {
613 zlog_err(
614 "netlink_interface_addr can't find interface by index %d",
615 ifa->ifa_index);
616 return -1;
617 }
618
619 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
620 {
621 char buf[BUFSIZ];
622 zlog_debug("netlink_interface_addr %s %s flags 0x%x:",
623 nl_msg_type_to_str(h->nlmsg_type), ifp->name,
624 ifa->ifa_flags);
625 if (tb[IFA_LOCAL])
626 zlog_debug(" IFA_LOCAL %s/%d",
627 inet_ntop(ifa->ifa_family,
628 RTA_DATA(tb[IFA_LOCAL]), buf,
629 BUFSIZ),
630 ifa->ifa_prefixlen);
631 if (tb[IFA_ADDRESS])
632 zlog_debug(" IFA_ADDRESS %s/%d",
633 inet_ntop(ifa->ifa_family,
634 RTA_DATA(tb[IFA_ADDRESS]), buf,
635 BUFSIZ),
636 ifa->ifa_prefixlen);
637 if (tb[IFA_BROADCAST])
638 zlog_debug(" IFA_BROADCAST %s/%d",
639 inet_ntop(ifa->ifa_family,
640 RTA_DATA(tb[IFA_BROADCAST]), buf,
641 BUFSIZ),
642 ifa->ifa_prefixlen);
643 if (tb[IFA_LABEL] && strcmp(ifp->name, RTA_DATA(tb[IFA_LABEL])))
644 zlog_debug(" IFA_LABEL %s",
645 (char *)RTA_DATA(tb[IFA_LABEL]));
646
647 if (tb[IFA_CACHEINFO]) {
648 struct ifa_cacheinfo *ci = RTA_DATA(tb[IFA_CACHEINFO]);
649 zlog_debug(" IFA_CACHEINFO pref %d, valid %d",
650 ci->ifa_prefered, ci->ifa_valid);
651 }
652 }
653
654 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
655 if (tb[IFA_LOCAL] == NULL)
656 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
657 if (tb[IFA_ADDRESS] == NULL)
658 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
659
660 /* local interface address */
661 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
662
663 /* is there a peer address? */
664 if (tb[IFA_ADDRESS]
665 && memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]),
666 RTA_PAYLOAD(tb[IFA_ADDRESS]))) {
667 broad = RTA_DATA(tb[IFA_ADDRESS]);
668 SET_FLAG(flags, ZEBRA_IFA_PEER);
669 } else
670 /* seeking a broadcast address */
671 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST])
672 : NULL);
673
674 /* addr is primary key, SOL if we don't have one */
675 if (addr == NULL) {
676 zlog_debug("%s: NULL address", __func__);
677 return -1;
678 }
679
680 /* Flags. */
681 if (ifa->ifa_flags & IFA_F_SECONDARY)
682 SET_FLAG(flags, ZEBRA_IFA_SECONDARY);
683
684 /* Label */
685 if (tb[IFA_LABEL])
686 label = (char *)RTA_DATA(tb[IFA_LABEL]);
687
688 if (ifp && label && strcmp(ifp->name, label) == 0)
689 label = NULL;
690
691 /* Register interface address to the interface. */
692 if (ifa->ifa_family == AF_INET) {
693 if (h->nlmsg_type == RTM_NEWADDR)
694 connected_add_ipv4(ifp, flags, (struct in_addr *)addr,
695 ifa->ifa_prefixlen,
696 (struct in_addr *)broad, label);
697 else
698 connected_delete_ipv4(
699 ifp, flags, (struct in_addr *)addr,
700 ifa->ifa_prefixlen, (struct in_addr *)broad);
701 }
702 if (ifa->ifa_family == AF_INET6) {
703 if (h->nlmsg_type == RTM_NEWADDR) {
704 /* Only consider valid addresses; we'll not get a
705 * notification from
706 * the kernel till IPv6 DAD has completed, but at init
707 * time, Quagga
708 * does query for and will receive all addresses.
709 */
710 if (!(ifa->ifa_flags
711 & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
712 connected_add_ipv6(
713 ifp, flags, (struct in6_addr *)addr,
714 ifa->ifa_prefixlen,
715 (struct in6_addr *)broad, label);
716 } else
717 connected_delete_ipv6(ifp, (struct in6_addr *)addr,
718 ifa->ifa_prefixlen,
719 (struct in6_addr *)broad);
720 }
721
722 return 0;
723 }
724
725 int netlink_link_change(struct sockaddr_nl *snl, struct nlmsghdr *h,
726 ns_id_t ns_id, int startup)
727 {
728 int len;
729 struct ifinfomsg *ifi;
730 struct rtattr *tb[IFLA_MAX + 1];
731 struct rtattr *linkinfo[IFLA_MAX + 1];
732 struct interface *ifp;
733 char *name = NULL;
734 char *kind = NULL;
735 char *slave_kind = NULL;
736 int vrf_device = 0;
737 struct zebra_ns *zns;
738 vrf_id_t vrf_id = VRF_DEFAULT;
739
740 zns = zebra_ns_lookup(ns_id);
741 ifi = NLMSG_DATA(h);
742
743 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) {
744 /* If this is not link add/delete message so print warning. */
745 zlog_warn("netlink_link_change: wrong kernel message %d",
746 h->nlmsg_type);
747 return 0;
748 }
749
750 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
751 if (len < 0)
752 return -1;
753
754 if (ifi->ifi_family == AF_BRIDGE)
755 return 0;
756
757 /* Looking up interface name. */
758 memset(tb, 0, sizeof tb);
759 netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
760
761 #ifdef IFLA_WIRELESS
762 /* check for wireless messages to ignore */
763 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) {
764 if (IS_ZEBRA_DEBUG_KERNEL)
765 zlog_debug("%s: ignoring IFLA_WIRELESS message",
766 __func__);
767 return 0;
768 }
769 #endif /* IFLA_WIRELESS */
770
771 if (tb[IFLA_IFNAME] == NULL)
772 return -1;
773 name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
774
775 if (tb[IFLA_LINKINFO]) {
776 memset(linkinfo, 0, sizeof linkinfo);
777 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
778
779 if (linkinfo[IFLA_INFO_KIND])
780 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
781
782 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
783 if (linkinfo[IFLA_INFO_SLAVE_KIND])
784 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
785 #endif
786
787 if (kind && strcmp(kind, "vrf") == 0) {
788 vrf_device = 1;
789 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
790 vrf_id = (vrf_id_t)ifi->ifi_index;
791 }
792 }
793
794 /* See if interface is present. */
795 ifp = if_lookup_by_name_per_ns(zns, name);
796
797 if (h->nlmsg_type == RTM_NEWLINK) {
798 if (tb[IFLA_MASTER]) {
799 if (slave_kind && (strcmp(slave_kind, "vrf") == 0))
800 vrf_id =
801 *(u_int32_t *)RTA_DATA(tb[IFLA_MASTER]);
802 }
803
804 if (ifp == NULL
805 || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
806 /* Add interface notification from kernel */
807 if (IS_ZEBRA_DEBUG_KERNEL)
808 zlog_debug(
809 "RTM_NEWLINK for %s(%u) (ifp %p) vrf_id %u flags 0x%x",
810 name, ifi->ifi_index, ifp, vrf_id,
811 ifi->ifi_flags);
812
813 if (ifp == NULL) {
814 /* unknown interface */
815 ifp = if_get_by_name(name, vrf_id);
816 } else {
817 /* pre-configured interface, learnt now */
818 if (ifp->vrf_id != vrf_id)
819 if_update(ifp, name, strlen(name),
820 vrf_id);
821 }
822
823 /* Update interface information. */
824 set_ifindex(ifp, ifi->ifi_index, zns);
825 ifp->flags = ifi->ifi_flags & 0x0000fffff;
826 if (vrf_device)
827 SET_FLAG(ifp->status,
828 ZEBRA_INTERFACE_VRF_LOOPBACK);
829 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
830 ifp->metric = 0;
831 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
832
833 netlink_interface_update_hw_addr(tb, ifp);
834
835 /* Inform clients, install any configured addresses. */
836 if_add_update(ifp);
837 } else if (ifp->vrf_id != vrf_id) {
838 /* VRF change for an interface. */
839 if (IS_ZEBRA_DEBUG_KERNEL)
840 zlog_debug(
841 "RTM_NEWLINK vrf-change for %s(%u) "
842 "vrf_id %u -> %u flags 0x%x",
843 name, ifp->ifindex, ifp->vrf_id, vrf_id,
844 ifi->ifi_flags);
845
846 if_handle_vrf_change(ifp, vrf_id);
847 } else {
848 /* Interface status change. */
849 if (IS_ZEBRA_DEBUG_KERNEL)
850 zlog_debug(
851 "RTM_NEWLINK status for %s(%u) flags 0x%x",
852 name, ifp->ifindex, ifi->ifi_flags);
853
854 set_ifindex(ifp, ifi->ifi_index, zns);
855 ifp->mtu6 = ifp->mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
856 ifp->metric = 0;
857
858 netlink_interface_update_hw_addr(tb, ifp);
859
860 if (if_is_no_ptm_operative(ifp)) {
861 ifp->flags = ifi->ifi_flags & 0x0000fffff;
862 if (!if_is_no_ptm_operative(ifp))
863 if_down(ifp);
864 else if (if_is_operative(ifp))
865 /* Must notify client daemons of new
866 * interface status. */
867 zebra_interface_up_update(ifp);
868 } else {
869 ifp->flags = ifi->ifi_flags & 0x0000fffff;
870 if (if_is_operative(ifp))
871 if_up(ifp);
872 }
873 }
874 } else {
875 /* Delete interface notification from kernel */
876 if (ifp == NULL) {
877 zlog_warn("RTM_DELLINK for unknown interface %s(%u)",
878 name, ifi->ifi_index);
879 return 0;
880 }
881
882 if (IS_ZEBRA_DEBUG_KERNEL)
883 zlog_debug("RTM_DELLINK for %s(%u)", name,
884 ifp->ifindex);
885
886 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
887
888 if (!vrf_device)
889 if_delete_update(ifp);
890 }
891
892 return 0;
893 }
894
895 /* Interface information read by netlink. */
896 void interface_list(struct zebra_ns *zns)
897 {
898 interface_lookup_netlink(zns);
899 }