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