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