]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_netlink.c
Merge commit '3d22338f04d9554fa' into evpn-prep
[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 "vrf_int.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 ecmd.speed_hi = 0;
336 ecmd.speed = 0;
337 }
338
339 close(sd);
340
341 return (ecmd.speed_hi << 16 ) | ecmd.speed;
342 }
343
344 /* Called from interface_lookup_netlink(). This function is only used
345 during bootstrap. */
346 static int
347 netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h,
348 ns_id_t ns_id, int startup)
349 {
350 int len;
351 struct ifinfomsg *ifi;
352 struct rtattr *tb[IFLA_MAX + 1];
353 struct rtattr *linkinfo[IFLA_MAX + 1];
354 struct interface *ifp;
355 char *name = NULL;
356 char *kind = NULL;
357 char *slave_kind = NULL;
358 int vrf_device = 0;
359 struct zebra_ns *zns;
360 vrf_id_t vrf_id = VRF_DEFAULT;
361
362 zns = zebra_ns_lookup (ns_id);
363 ifi = NLMSG_DATA (h);
364
365 if (h->nlmsg_type != RTM_NEWLINK)
366 return 0;
367
368 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
369 if (len < 0)
370 return -1;
371
372 if (ifi->ifi_family == AF_BRIDGE)
373 return 0;
374
375 /* Looking up interface name. */
376 memset (tb, 0, sizeof tb);
377 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
378
379 #ifdef IFLA_WIRELESS
380 /* check for wireless messages to ignore */
381 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
382 {
383 if (IS_ZEBRA_DEBUG_KERNEL)
384 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
385 return 0;
386 }
387 #endif /* IFLA_WIRELESS */
388
389 if (tb[IFLA_IFNAME] == NULL)
390 return -1;
391 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
392
393 if (tb[IFLA_LINKINFO])
394 {
395 memset (linkinfo, 0, sizeof linkinfo);
396 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
397
398 if (linkinfo[IFLA_INFO_KIND])
399 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
400
401 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
402 if (linkinfo[IFLA_INFO_SLAVE_KIND])
403 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
404 #endif
405
406 if (kind && strcmp(kind, "vrf") == 0)
407 {
408 vrf_device = 1;
409 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
410 vrf_id = (vrf_id_t)ifi->ifi_index;
411 }
412 }
413
414 if (tb[IFLA_MASTER])
415 {
416 if (slave_kind && (strcmp(slave_kind, "vrf") == 0))
417 vrf_id = *(u_int32_t *)RTA_DATA(tb[IFLA_MASTER]);
418 }
419
420 /* Add interface. */
421 ifp = if_get_by_name (name, vrf_id);
422 set_ifindex(ifp, ifi->ifi_index, zns);
423 ifp->flags = ifi->ifi_flags & 0x0000fffff;
424 if (vrf_device)
425 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
426 ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]);
427 ifp->metric = 0;
428 ifp->speed = get_iflink_speed (name);
429 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
430
431 /* Hardware type and address. */
432 ifp->ll_type = netlink_to_zebra_link_type (ifi->ifi_type);
433 netlink_interface_update_hw_addr (tb, ifp);
434
435 if_add_update (ifp);
436
437 return 0;
438 }
439
440 /* Request for specific interface or address information from the kernel */
441 static int
442 netlink_request_intf_addr (struct zebra_ns *zns,
443 int family, int type,
444 u_int32_t filter_mask)
445 {
446 struct
447 {
448 struct nlmsghdr n;
449 struct ifinfomsg ifm;
450 char buf[256];
451 } req;
452
453 /* Form the request, specifying filter (rtattr) if needed. */
454 memset (&req, 0, sizeof (req));
455 req.n.nlmsg_type = type;
456 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
457 req.ifm.ifi_family = family;
458
459 /* Include filter, if specified. */
460 if (filter_mask)
461 addattr32 (&req.n, sizeof(req), IFLA_EXT_MASK, filter_mask);
462
463 return netlink_request (&zns->netlink_cmd, &req.n);
464 }
465
466 /* Interface lookup by netlink socket. */
467 int
468 interface_lookup_netlink (struct zebra_ns *zns)
469 {
470 int ret;
471
472 /* Get interface information. */
473 ret = netlink_request_intf_addr (zns, AF_PACKET, RTM_GETLINK, 0);
474 if (ret < 0)
475 return ret;
476 ret = netlink_parse_info (netlink_interface, &zns->netlink_cmd, zns, 0, 1);
477 if (ret < 0)
478 return ret;
479
480 /* Get IPv4 address of the interfaces. */
481 ret = netlink_request_intf_addr (zns, AF_INET, RTM_GETADDR, 0);
482 if (ret < 0)
483 return ret;
484 ret = netlink_parse_info (netlink_interface_addr, &zns->netlink_cmd, zns, 0, 1);
485 if (ret < 0)
486 return ret;
487
488 /* Get IPv6 address of the interfaces. */
489 ret = netlink_request_intf_addr (zns, AF_INET6, RTM_GETADDR, 0);
490 if (ret < 0)
491 return ret;
492 ret = netlink_parse_info (netlink_interface_addr, &zns->netlink_cmd, zns, 0, 1);
493 if (ret < 0)
494 return ret;
495
496 return 0;
497 }
498
499 /* Interface address modification. */
500 static int
501 netlink_address (int cmd, int family, struct interface *ifp,
502 struct connected *ifc)
503 {
504 int bytelen;
505 struct prefix *p;
506
507 struct
508 {
509 struct nlmsghdr n;
510 struct ifaddrmsg ifa;
511 char buf[NL_PKT_BUF_SIZE];
512 } req;
513
514 struct zebra_ns *zns = zebra_ns_lookup (NS_DEFAULT);
515
516 p = ifc->address;
517 memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE);
518
519 bytelen = (family == AF_INET ? 4 : 16);
520
521 req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
522 req.n.nlmsg_flags = NLM_F_REQUEST;
523 req.n.nlmsg_type = cmd;
524 req.n.nlmsg_pid = zns->netlink_cmd.snl.nl_pid;
525
526 req.ifa.ifa_family = family;
527
528 req.ifa.ifa_index = ifp->ifindex;
529 req.ifa.ifa_prefixlen = p->prefixlen;
530
531 addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen);
532
533 if (family == AF_INET && cmd == RTM_NEWADDR)
534 {
535 if (!CONNECTED_PEER(ifc) && ifc->destination)
536 {
537 p = ifc->destination;
538 addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix,
539 bytelen);
540 }
541 }
542
543 if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
544 SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY);
545
546 if (ifc->label)
547 addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label,
548 strlen (ifc->label) + 1);
549
550 return netlink_talk (netlink_talk_filter, &req.n, &zns->netlink_cmd, zns, 0);
551 }
552
553 int
554 kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc)
555 {
556 return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc);
557 }
558
559 int
560 kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc)
561 {
562 return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc);
563 }
564
565 int
566 netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h,
567 ns_id_t ns_id, int startup)
568 {
569 int len;
570 struct ifaddrmsg *ifa;
571 struct rtattr *tb[IFA_MAX + 1];
572 struct interface *ifp;
573 void *addr;
574 void *broad;
575 u_char flags = 0;
576 char *label = NULL;
577 struct zebra_ns *zns;
578
579 zns = zebra_ns_lookup (ns_id);
580 ifa = NLMSG_DATA (h);
581
582 if (ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6)
583 return 0;
584
585 if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)
586 return 0;
587
588 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg));
589 if (len < 0)
590 return -1;
591
592 memset (tb, 0, sizeof tb);
593 netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len);
594
595 ifp = if_lookup_by_index_per_ns (zns, ifa->ifa_index);
596 if (ifp == NULL)
597 {
598 zlog_err ("netlink_interface_addr can't find interface by index %d",
599 ifa->ifa_index);
600 return -1;
601 }
602
603 if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */
604 {
605 char buf[BUFSIZ];
606 zlog_debug ("netlink_interface_addr %s %s flags 0x%x:",
607 nl_msg_type_to_str (h->nlmsg_type), ifp->name,
608 ifa->ifa_flags);
609 if (tb[IFA_LOCAL])
610 zlog_debug (" IFA_LOCAL %s/%d",
611 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]),
612 buf, BUFSIZ), ifa->ifa_prefixlen);
613 if (tb[IFA_ADDRESS])
614 zlog_debug (" IFA_ADDRESS %s/%d",
615 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]),
616 buf, BUFSIZ), ifa->ifa_prefixlen);
617 if (tb[IFA_BROADCAST])
618 zlog_debug (" IFA_BROADCAST %s/%d",
619 inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]),
620 buf, BUFSIZ), ifa->ifa_prefixlen);
621 if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL])))
622 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL]));
623
624 if (tb[IFA_CACHEINFO])
625 {
626 struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]);
627 zlog_debug (" IFA_CACHEINFO pref %d, valid %d",
628 ci->ifa_prefered, ci->ifa_valid);
629 }
630 }
631
632 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
633 if (tb[IFA_LOCAL] == NULL)
634 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
635 if (tb[IFA_ADDRESS] == NULL)
636 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
637
638 /* local interface address */
639 addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL);
640
641 /* is there a peer address? */
642 if (tb[IFA_ADDRESS] &&
643 memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_ADDRESS])))
644 {
645 broad = RTA_DATA(tb[IFA_ADDRESS]);
646 SET_FLAG (flags, ZEBRA_IFA_PEER);
647 }
648 else
649 /* seeking a broadcast address */
650 broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL);
651
652 /* addr is primary key, SOL if we don't have one */
653 if (addr == NULL)
654 {
655 zlog_debug ("%s: NULL address", __func__);
656 return -1;
657 }
658
659 /* Flags. */
660 if (ifa->ifa_flags & IFA_F_SECONDARY)
661 SET_FLAG (flags, ZEBRA_IFA_SECONDARY);
662
663 /* Label */
664 if (tb[IFA_LABEL])
665 label = (char *) RTA_DATA (tb[IFA_LABEL]);
666
667 if (ifp && label && strcmp (ifp->name, label) == 0)
668 label = NULL;
669
670 /* Register interface address to the interface. */
671 if (ifa->ifa_family == AF_INET)
672 {
673 if (h->nlmsg_type == RTM_NEWADDR)
674 connected_add_ipv4 (ifp, flags,
675 (struct in_addr *) addr, ifa->ifa_prefixlen,
676 (struct in_addr *) broad, label);
677 else
678 connected_delete_ipv4 (ifp, flags,
679 (struct in_addr *) addr, ifa->ifa_prefixlen,
680 (struct in_addr *) broad);
681 }
682 if (ifa->ifa_family == AF_INET6)
683 {
684 if (h->nlmsg_type == RTM_NEWADDR)
685 {
686 /* Only consider valid addresses; we'll not get a notification from
687 * the kernel till IPv6 DAD has completed, but at init time, Quagga
688 * does query for and will receive all addresses.
689 */
690 if (!(ifa->ifa_flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)))
691 connected_add_ipv6 (ifp, flags, (struct in6_addr *) addr,
692 ifa->ifa_prefixlen, (struct in6_addr *) broad, label);
693 }
694 else
695 connected_delete_ipv6 (ifp,
696 (struct in6_addr *) addr, ifa->ifa_prefixlen,
697 (struct in6_addr *) broad);
698 }
699
700 return 0;
701 }
702
703 int
704 netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h,
705 ns_id_t ns_id, int startup)
706 {
707 int len;
708 struct ifinfomsg *ifi;
709 struct rtattr *tb[IFLA_MAX + 1];
710 struct rtattr *linkinfo[IFLA_MAX + 1];
711 struct interface *ifp;
712 char *name = NULL;
713 char *kind = NULL;
714 char *slave_kind = NULL;
715 int vrf_device = 0;
716 struct zebra_ns *zns;
717 vrf_id_t vrf_id = VRF_DEFAULT;
718
719 zns = zebra_ns_lookup (ns_id);
720 ifi = NLMSG_DATA (h);
721
722 if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK))
723 {
724 /* If this is not link add/delete message so print warning. */
725 zlog_warn ("netlink_link_change: wrong kernel message %d",
726 h->nlmsg_type);
727 return 0;
728 }
729
730 len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
731 if (len < 0)
732 return -1;
733
734 if (ifi->ifi_family == AF_BRIDGE)
735 return 0;
736
737 /* Looking up interface name. */
738 memset (tb, 0, sizeof tb);
739 netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
740
741 #ifdef IFLA_WIRELESS
742 /* check for wireless messages to ignore */
743 if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
744 {
745 if (IS_ZEBRA_DEBUG_KERNEL)
746 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
747 return 0;
748 }
749 #endif /* IFLA_WIRELESS */
750
751 if (tb[IFLA_IFNAME] == NULL)
752 return -1;
753 name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
754
755 if (tb[IFLA_LINKINFO])
756 {
757 memset (linkinfo, 0, sizeof linkinfo);
758 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
759
760 if (linkinfo[IFLA_INFO_KIND])
761 kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
762
763 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
764 if (linkinfo[IFLA_INFO_SLAVE_KIND])
765 slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
766 #endif
767
768 if (kind && strcmp(kind, "vrf") == 0)
769 {
770 vrf_device = 1;
771 netlink_vrf_change(h, tb[IFLA_LINKINFO], name);
772 vrf_id = (vrf_id_t)ifi->ifi_index;
773 }
774 }
775
776 /* See if interface is present. */
777 ifp = if_lookup_by_name_per_ns (zns, name);
778
779 if (h->nlmsg_type == RTM_NEWLINK)
780 {
781 if (tb[IFLA_MASTER])
782 {
783 if (slave_kind && (strcmp(slave_kind, "vrf") == 0))
784 vrf_id = *(u_int32_t *)RTA_DATA(tb[IFLA_MASTER]);
785 }
786
787 if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
788 {
789 /* Add interface notification from kernel */
790 if (IS_ZEBRA_DEBUG_KERNEL)
791 zlog_debug ("RTM_NEWLINK for %s(%u) (ifp %p) vrf_id %u flags 0x%x",
792 name, ifi->ifi_index, ifp, vrf_id, ifi->ifi_flags);
793
794 if (ifp == NULL)
795 {
796 /* unknown interface */
797 ifp = if_get_by_name (name, vrf_id);
798 }
799 else
800 {
801 /* pre-configured interface, learnt now */
802 if (ifp->vrf_id != vrf_id)
803 if_update_to_new_vrf (ifp, vrf_id);
804 }
805
806 /* Update interface information. */
807 set_ifindex(ifp, ifi->ifi_index, zns);
808 ifp->flags = ifi->ifi_flags & 0x0000fffff;
809 if (vrf_device)
810 SET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
811 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
812 ifp->metric = 0;
813 ifp->ptm_status = ZEBRA_PTM_STATUS_UNKNOWN;
814
815 netlink_interface_update_hw_addr (tb, ifp);
816
817 /* Inform clients, install any configured addresses. */
818 if_add_update (ifp);
819 }
820 else if (ifp->vrf_id != vrf_id)
821 {
822 /* VRF change for an interface. */
823 if (IS_ZEBRA_DEBUG_KERNEL)
824 zlog_debug ("RTM_NEWLINK vrf-change for %s(%u) "
825 "vrf_id %u -> %u flags 0x%x",
826 name, ifp->ifindex, ifp->vrf_id,
827 vrf_id, ifi->ifi_flags);
828
829 if_handle_vrf_change (ifp, vrf_id);
830 }
831 else
832 {
833 /* Interface status change. */
834 if (IS_ZEBRA_DEBUG_KERNEL)
835 zlog_debug ("RTM_NEWLINK status for %s(%u) flags 0x%x",
836 name, ifp->ifindex, ifi->ifi_flags);
837
838 set_ifindex(ifp, ifi->ifi_index, zns);
839 ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
840 ifp->metric = 0;
841
842 netlink_interface_update_hw_addr (tb, ifp);
843
844 if (if_is_no_ptm_operative (ifp))
845 {
846 ifp->flags = ifi->ifi_flags & 0x0000fffff;
847 if (!if_is_no_ptm_operative (ifp))
848 if_down (ifp);
849 else if (if_is_operative (ifp))
850 /* Must notify client daemons of new interface status. */
851 zebra_interface_up_update (ifp);
852 }
853 else
854 {
855 ifp->flags = ifi->ifi_flags & 0x0000fffff;
856 if (if_is_operative (ifp))
857 if_up (ifp);
858 }
859 }
860 }
861 else
862 {
863 /* Delete interface notification from kernel */
864 if (ifp == NULL)
865 {
866 zlog_warn ("RTM_DELLINK for unknown interface %s(%u)",
867 name, ifi->ifi_index);
868 return 0;
869 }
870
871 if (IS_ZEBRA_DEBUG_KERNEL)
872 zlog_debug ("RTM_DELLINK for %s(%u)", name, ifp->ifindex);
873
874 UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_VRF_LOOPBACK);
875
876 if (!vrf_device)
877 if_delete_update (ifp);
878 }
879
880 return 0;
881 }
882
883 /* Interface information read by netlink. */
884 void
885 interface_list (struct zebra_ns *zns)
886 {
887 interface_lookup_netlink (zns);
888 }