2 * Interface looking up by netlink.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
23 #include <net/if_arp.h>
24 #include <linux/sockios.h>
25 #include <linux/ethtool.h>
31 #include "connected.h"
34 #include "zebra_memory.h"
44 #include "zebra/zserv.h"
45 #include "zebra/zebra_ns.h"
46 #include "zebra/zebra_vrf.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"
58 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
59 names and ifindex values. */
61 set_ifindex(struct interface
*ifp
, ifindex_t ifi_index
, struct zebra_ns
*zns
)
63 struct interface
*oifp
;
65 if (((oifp
= if_lookup_by_index_per_ns (zns
, ifi_index
)) != NULL
) && (oifp
!= ifp
))
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
);
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
);
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
);
82 ifp
->ifindex
= ifi_index
;
85 /* Utility function to parse hardware link-layer address and update ifp */
87 netlink_interface_update_hw_addr (struct rtattr
**tb
, struct interface
*ifp
)
95 hw_addr_len
= RTA_PAYLOAD (tb
[IFLA_ADDRESS
]);
97 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
98 zlog_warn ("Hardware address is too large: %d", hw_addr_len
);
101 ifp
->hw_addr_len
= hw_addr_len
;
102 memcpy (ifp
->hw_addr
, RTA_DATA (tb
[IFLA_ADDRESS
]), hw_addr_len
);
104 for (i
= 0; i
< hw_addr_len
; i
++)
105 if (ifp
->hw_addr
[i
] != 0)
108 if (i
== hw_addr_len
)
109 ifp
->hw_addr_len
= 0;
111 ifp
->hw_addr_len
= hw_addr_len
;
116 static enum zebra_link_type
117 netlink_to_zebra_link_type (unsigned int hwt
)
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
;
168 case ARPHRD_IP6GRE
: return ZEBRA_LLT_IP6GRE
;
170 #ifdef ARPHRD_IEEE802154_PHY
171 case ARPHRD_IEEE802154_PHY
: return ZEBRA_LLT_IEEE802154_PHY
;
174 default: return ZEBRA_LLT_UNKNOWN
;
179 //Temporary Assignments to compile on older platforms.
181 #define IFLA_BR_MAX 39
184 #ifndef IFLA_VXLAN_ID
185 #define IFLA_VXLAN_ID 1
188 #ifndef IFLA_VXLAN_LOCAL
189 #define IFLA_VXLAN_LOCAL 4
192 #ifndef IFLA_VXLAN_MAX
193 #define IFLA_VXLAN_MAX 26
196 #ifndef IFLA_BRIDGE_MAX
197 #define IFLA_BRIDGE_MAX 2
200 #ifndef IFLA_BRIDGE_VLAN_INFO
201 #define IFLA_BRIDGE_VLAN_INFO 2
204 #ifndef BRIDGE_VLAN_INFO_PVID
205 #define BRIDGE_VLAN_INFO_PVID (1<<1)
208 #ifndef RTEXT_FILTER_BRVLAN
209 #define RTEXT_FILTER_BRVLAN (1<<1)
213 #define NTF_SELF 0x02
216 #ifndef IFLA_BR_VLAN_FILTERING
217 #define IFLA_BR_VLAN_FILTERING 7
220 #define parse_rtattr_nested(tb, max, rta) \
221 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
224 netlink_vrf_change (struct nlmsghdr
*h
, struct rtattr
*tb
, const char *name
)
226 struct ifinfomsg
*ifi
;
227 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
228 struct rtattr
*attr
[IFLA_VRF_MAX
+1];
230 struct zebra_vrf
*zvrf
;
231 u_int32_t nl_table_id
;
233 ifi
= NLMSG_DATA (h
);
235 memset (linkinfo
, 0, sizeof linkinfo
);
236 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
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
);
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
);
252 nl_table_id
= *(u_int32_t
*)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
254 if (h
->nlmsg_type
== RTM_NEWLINK
)
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
);
261 * vrf_get is implied creation if it does not exist
263 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
, name
); // It would create vrf
266 zlog_err ("VRF %s id %u not created", name
, ifi
->ifi_index
);
270 /* Enable the created VRF. */
271 if (!vrf_enable (vrf
))
273 zlog_err ("Failed to enable VRF %s id %u", name
, ifi
->ifi_index
);
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.
283 zvrf
= (struct zebra_vrf
*)vrf
->info
;
284 zvrf
->table_id
= nl_table_id
;
286 else //h->nlmsg_type == RTM_DELLINK
288 if (IS_ZEBRA_DEBUG_KERNEL
)
289 zlog_debug ("RTM_DELLINK for VRF %s(%u)", name
, ifi
->ifi_index
);
291 vrf
= vrf_lookup_by_id ((vrf_id_t
)ifi
->ifi_index
);
295 zlog_warn ("%s: vrf not found", __func__
);
304 get_iflink_speed (const char *ifname
)
307 struct ethtool_cmd ecmd
;
311 /* initialize struct */
312 memset(&ifdata
, 0, sizeof(ifdata
));
314 /* set interface name */
315 strcpy(ifdata
.ifr_name
, ifname
);
317 /* initialize ethtool interface */
318 memset(&ecmd
, 0, sizeof(ecmd
));
319 ecmd
.cmd
= ETHTOOL_GSET
; /* ETHTOOL_GLINK */
320 ifdata
.ifr_data
= (__caddr_t
) &ecmd
;
322 /* use ioctl to get IP address of an interface */
323 sd
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
325 zlog_debug ("Failure to read interface %s speed: %d %s",
326 ifname
, errno
, safe_strerror(errno
));
330 /* Get the current link state for the interface */
331 rc
= ioctl(sd
, SIOCETHTOOL
, (char *)&ifdata
);
333 zlog_debug("IOCTL failure to read interface %s speed: %d %s",
334 ifname
, errno
, safe_strerror(errno
));
341 return (ecmd
.speed_hi
<< 16 ) | ecmd
.speed
;
344 /* Called from interface_lookup_netlink(). This function is only used
347 netlink_interface (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
348 ns_id_t ns_id
, int startup
)
351 struct ifinfomsg
*ifi
;
352 struct rtattr
*tb
[IFLA_MAX
+ 1];
353 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
354 struct interface
*ifp
;
357 char *slave_kind
= NULL
;
359 struct zebra_ns
*zns
;
360 vrf_id_t vrf_id
= VRF_DEFAULT
;
362 zns
= zebra_ns_lookup (ns_id
);
363 ifi
= NLMSG_DATA (h
);
365 if (h
->nlmsg_type
!= RTM_NEWLINK
)
368 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifinfomsg
));
372 if (ifi
->ifi_family
== AF_BRIDGE
)
375 /* Looking up interface name. */
376 memset (tb
, 0, sizeof tb
);
377 netlink_parse_rtattr (tb
, IFLA_MAX
, IFLA_RTA (ifi
), len
);
380 /* check for wireless messages to ignore */
381 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0))
383 if (IS_ZEBRA_DEBUG_KERNEL
)
384 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__
);
387 #endif /* IFLA_WIRELESS */
389 if (tb
[IFLA_IFNAME
] == NULL
)
391 name
= (char *) RTA_DATA (tb
[IFLA_IFNAME
]);
393 if (tb
[IFLA_LINKINFO
])
395 memset (linkinfo
, 0, sizeof linkinfo
);
396 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
398 if (linkinfo
[IFLA_INFO_KIND
])
399 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
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
]);
406 if (kind
&& strcmp(kind
, "vrf") == 0)
409 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
410 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
416 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0))
417 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
421 ifp
= if_get_by_name (name
, vrf_id
);
422 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
423 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
425 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
426 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *) RTA_DATA (tb
[IFLA_MTU
]);
428 ifp
->speed
= get_iflink_speed (name
);
429 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
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
);
440 /* Interface lookup by netlink socket. */
442 interface_lookup_netlink (struct zebra_ns
*zns
)
446 /* Get interface information. */
447 ret
= netlink_request (AF_PACKET
, RTM_GETLINK
, &zns
->netlink_cmd
);
450 ret
= netlink_parse_info (netlink_interface
, &zns
->netlink_cmd
, zns
, 0, 1);
454 /* Get IPv4 address of the interfaces. */
455 ret
= netlink_request (AF_INET
, RTM_GETADDR
, &zns
->netlink_cmd
);
458 ret
= netlink_parse_info (netlink_interface_addr
, &zns
->netlink_cmd
, zns
, 0, 1);
462 /* Get IPv6 address of the interfaces. */
463 ret
= netlink_request (AF_INET6
, RTM_GETADDR
, &zns
->netlink_cmd
);
466 ret
= netlink_parse_info (netlink_interface_addr
, &zns
->netlink_cmd
, zns
, 0, 1);
473 /* Interface address modification. */
475 netlink_address (int cmd
, int family
, struct interface
*ifp
,
476 struct connected
*ifc
)
484 struct ifaddrmsg ifa
;
485 char buf
[NL_PKT_BUF_SIZE
];
488 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
491 memset (&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
493 bytelen
= (family
== AF_INET
? 4 : 16);
495 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct ifaddrmsg
));
496 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
497 req
.n
.nlmsg_type
= cmd
;
498 req
.n
.nlmsg_pid
= zns
->netlink_cmd
.snl
.nl_pid
;
500 req
.ifa
.ifa_family
= family
;
502 req
.ifa
.ifa_index
= ifp
->ifindex
;
503 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
505 addattr_l (&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
507 if (family
== AF_INET
&& cmd
== RTM_NEWADDR
)
509 if (!CONNECTED_PEER(ifc
) && ifc
->destination
)
511 p
= ifc
->destination
;
512 addattr_l (&req
.n
, sizeof req
, IFA_BROADCAST
, &p
->u
.prefix
,
517 if (CHECK_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
))
518 SET_FLAG (req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
521 addattr_l (&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
522 strlen (ifc
->label
) + 1);
524 return netlink_talk (netlink_talk_filter
, &req
.n
, &zns
->netlink_cmd
, zns
, 0);
528 kernel_address_add_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
530 return netlink_address (RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
534 kernel_address_delete_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
536 return netlink_address (RTM_DELADDR
, AF_INET
, ifp
, ifc
);
540 netlink_interface_addr (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
541 ns_id_t ns_id
, int startup
)
544 struct ifaddrmsg
*ifa
;
545 struct rtattr
*tb
[IFA_MAX
+ 1];
546 struct interface
*ifp
;
551 struct zebra_ns
*zns
;
553 zns
= zebra_ns_lookup (ns_id
);
554 ifa
= NLMSG_DATA (h
);
556 if (ifa
->ifa_family
!= AF_INET
&& ifa
->ifa_family
!= AF_INET6
)
559 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
562 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifaddrmsg
));
566 memset (tb
, 0, sizeof tb
);
567 netlink_parse_rtattr (tb
, IFA_MAX
, IFA_RTA (ifa
), len
);
569 ifp
= if_lookup_by_index_per_ns (zns
, ifa
->ifa_index
);
572 zlog_err ("netlink_interface_addr can't find interface by index %d",
577 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
580 zlog_debug ("netlink_interface_addr %s %s flags 0x%x:",
581 nl_msg_type_to_str (h
->nlmsg_type
), ifp
->name
,
584 zlog_debug (" IFA_LOCAL %s/%d",
585 inet_ntop (ifa
->ifa_family
, RTA_DATA (tb
[IFA_LOCAL
]),
586 buf
, BUFSIZ
), ifa
->ifa_prefixlen
);
588 zlog_debug (" IFA_ADDRESS %s/%d",
589 inet_ntop (ifa
->ifa_family
, RTA_DATA (tb
[IFA_ADDRESS
]),
590 buf
, BUFSIZ
), ifa
->ifa_prefixlen
);
591 if (tb
[IFA_BROADCAST
])
592 zlog_debug (" IFA_BROADCAST %s/%d",
593 inet_ntop (ifa
->ifa_family
, RTA_DATA (tb
[IFA_BROADCAST
]),
594 buf
, BUFSIZ
), ifa
->ifa_prefixlen
);
595 if (tb
[IFA_LABEL
] && strcmp (ifp
->name
, RTA_DATA (tb
[IFA_LABEL
])))
596 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb
[IFA_LABEL
]));
598 if (tb
[IFA_CACHEINFO
])
600 struct ifa_cacheinfo
*ci
= RTA_DATA (tb
[IFA_CACHEINFO
]);
601 zlog_debug (" IFA_CACHEINFO pref %d, valid %d",
602 ci
->ifa_prefered
, ci
->ifa_valid
);
606 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
607 if (tb
[IFA_LOCAL
] == NULL
)
608 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
609 if (tb
[IFA_ADDRESS
] == NULL
)
610 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
612 /* local interface address */
613 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
615 /* is there a peer address? */
616 if (tb
[IFA_ADDRESS
] &&
617 memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_ADDRESS
])))
619 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
620 SET_FLAG (flags
, ZEBRA_IFA_PEER
);
623 /* seeking a broadcast address */
624 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
]) : NULL
);
626 /* addr is primary key, SOL if we don't have one */
629 zlog_debug ("%s: NULL address", __func__
);
634 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
635 SET_FLAG (flags
, ZEBRA_IFA_SECONDARY
);
639 label
= (char *) RTA_DATA (tb
[IFA_LABEL
]);
641 if (ifp
&& label
&& strcmp (ifp
->name
, label
) == 0)
644 /* Register interface address to the interface. */
645 if (ifa
->ifa_family
== AF_INET
)
647 if (h
->nlmsg_type
== RTM_NEWADDR
)
648 connected_add_ipv4 (ifp
, flags
,
649 (struct in_addr
*) addr
, ifa
->ifa_prefixlen
,
650 (struct in_addr
*) broad
, label
);
652 connected_delete_ipv4 (ifp
, flags
,
653 (struct in_addr
*) addr
, ifa
->ifa_prefixlen
,
654 (struct in_addr
*) broad
);
656 if (ifa
->ifa_family
== AF_INET6
)
658 if (h
->nlmsg_type
== RTM_NEWADDR
)
660 /* Only consider valid addresses; we'll not get a notification from
661 * the kernel till IPv6 DAD has completed, but at init time, Quagga
662 * does query for and will receive all addresses.
664 if (!(ifa
->ifa_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
665 connected_add_ipv6 (ifp
, flags
, (struct in6_addr
*) addr
,
666 ifa
->ifa_prefixlen
, (struct in6_addr
*) broad
, label
);
669 connected_delete_ipv6 (ifp
,
670 (struct in6_addr
*) addr
, ifa
->ifa_prefixlen
,
671 (struct in6_addr
*) broad
);
678 netlink_link_change (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
679 ns_id_t ns_id
, int startup
)
682 struct ifinfomsg
*ifi
;
683 struct rtattr
*tb
[IFLA_MAX
+ 1];
684 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
685 struct interface
*ifp
;
688 char *slave_kind
= NULL
;
690 struct zebra_ns
*zns
;
691 vrf_id_t vrf_id
= VRF_DEFAULT
;
693 zns
= zebra_ns_lookup (ns_id
);
694 ifi
= NLMSG_DATA (h
);
696 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
))
698 /* If this is not link add/delete message so print warning. */
699 zlog_warn ("netlink_link_change: wrong kernel message %d",
704 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifinfomsg
));
708 if (ifi
->ifi_family
== AF_BRIDGE
)
711 /* Looking up interface name. */
712 memset (tb
, 0, sizeof tb
);
713 netlink_parse_rtattr (tb
, IFLA_MAX
, IFLA_RTA (ifi
), len
);
716 /* check for wireless messages to ignore */
717 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0))
719 if (IS_ZEBRA_DEBUG_KERNEL
)
720 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__
);
723 #endif /* IFLA_WIRELESS */
725 if (tb
[IFLA_IFNAME
] == NULL
)
727 name
= (char *) RTA_DATA (tb
[IFLA_IFNAME
]);
729 if (tb
[IFLA_LINKINFO
])
731 memset (linkinfo
, 0, sizeof linkinfo
);
732 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
734 if (linkinfo
[IFLA_INFO_KIND
])
735 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
737 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
738 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
739 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
742 if (kind
&& strcmp(kind
, "vrf") == 0)
745 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
746 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
750 /* See if interface is present. */
751 ifp
= if_lookup_by_name_per_ns (zns
, name
);
753 if (h
->nlmsg_type
== RTM_NEWLINK
)
757 if (slave_kind
&& (strcmp(slave_kind
, "vrf") == 0))
758 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
761 if (ifp
== NULL
|| !CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
763 /* Add interface notification from kernel */
764 if (IS_ZEBRA_DEBUG_KERNEL
)
765 zlog_debug ("RTM_NEWLINK for %s(%u) (ifp %p) vrf_id %u flags 0x%x",
766 name
, ifi
->ifi_index
, ifp
, vrf_id
, ifi
->ifi_flags
);
770 /* unknown interface */
771 ifp
= if_get_by_name (name
, vrf_id
);
775 /* pre-configured interface, learnt now */
776 if (ifp
->vrf_id
!= vrf_id
)
777 if_update (ifp
, name
, strlen(name
), vrf_id
);
780 /* Update interface information. */
781 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
782 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
784 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
785 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
787 ifp
->ptm_status
= ZEBRA_PTM_STATUS_UNKNOWN
;
789 netlink_interface_update_hw_addr (tb
, ifp
);
791 /* Inform clients, install any configured addresses. */
794 else if (ifp
->vrf_id
!= vrf_id
)
796 /* VRF change for an interface. */
797 if (IS_ZEBRA_DEBUG_KERNEL
)
798 zlog_debug ("RTM_NEWLINK vrf-change for %s(%u) "
799 "vrf_id %u -> %u flags 0x%x",
800 name
, ifp
->ifindex
, ifp
->vrf_id
,
801 vrf_id
, ifi
->ifi_flags
);
803 if_handle_vrf_change (ifp
, vrf_id
);
807 /* Interface status change. */
808 if (IS_ZEBRA_DEBUG_KERNEL
)
809 zlog_debug ("RTM_NEWLINK status for %s(%u) flags 0x%x",
810 name
, ifp
->ifindex
, ifi
->ifi_flags
);
812 set_ifindex(ifp
, ifi
->ifi_index
, zns
);
813 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
816 netlink_interface_update_hw_addr (tb
, ifp
);
818 if (if_is_no_ptm_operative (ifp
))
820 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
821 if (!if_is_no_ptm_operative (ifp
))
823 else if (if_is_operative (ifp
))
824 /* Must notify client daemons of new interface status. */
825 zebra_interface_up_update (ifp
);
829 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
830 if (if_is_operative (ifp
))
837 /* Delete interface notification from kernel */
840 zlog_warn ("RTM_DELLINK for unknown interface %s(%u)",
841 name
, ifi
->ifi_index
);
845 if (IS_ZEBRA_DEBUG_KERNEL
)
846 zlog_debug ("RTM_DELLINK for %s(%u)", name
, ifp
->ifindex
);
848 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
851 if_delete_update (ifp
);
857 /* Interface information read by netlink. */
859 interface_list (struct zebra_ns
*zns
)
861 interface_lookup_netlink (zns
);