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