]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Interface looking up by ioctl (). | |
3 | * Copyright (C) 1997, 98 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 | * | |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
ddfeb486 DL |
24 | #ifdef OPEN_BSD |
25 | ||
718e3744 | 26 | #include "if.h" |
27 | #include "sockunion.h" | |
28 | #include "prefix.h" | |
29 | #include "ioctl.h" | |
30 | #include "connected.h" | |
31 | #include "memory.h" | |
32 | #include "log.h" | |
8f7d9fc0 | 33 | #include "vrf.h" |
82283592 | 34 | #include "vty.h" |
43e52561 | 35 | #include "lib_errors.h" |
718e3744 | 36 | |
37 | #include "zebra/interface.h" | |
8f7d9fc0 | 38 | #include "zebra/rib.h" |
f7dae312 | 39 | #include "zebra/rt.h" |
364fed6b | 40 | #include "zebra/zebra_errors.h" |
718e3744 | 41 | |
24f5e2fc DL |
42 | #include <ifaddrs.h> |
43 | ||
718e3744 | 44 | /* Interface looking up using infamous SIOCGIFCONF. */ |
d62a17ae | 45 | static int interface_list_ioctl(void) |
718e3744 | 46 | { |
d62a17ae | 47 | int ret; |
48 | int sock; | |
718e3744 | 49 | #define IFNUM_BASE 32 |
d62a17ae | 50 | int ifnum; |
51 | struct ifreq *ifreq; | |
52 | struct ifconf ifconf; | |
53 | struct interface *ifp; | |
54 | int n; | |
55 | int lastlen; | |
56 | ||
57 | /* Normally SIOCGIFCONF works with AF_INET socket. */ | |
58 | sock = socket(AF_INET, SOCK_DGRAM, 0); | |
59 | if (sock < 0) { | |
450971aa | 60 | flog_err_sys(EC_LIB_SOCKET, |
9df414fe QY |
61 | "Can't make AF_INET socket stream: %s", |
62 | safe_strerror(errno)); | |
d62a17ae | 63 | return -1; |
64 | } | |
65 | ||
66 | /* Set initial ifreq count. This will be double when SIOCGIFCONF | |
67 | fail. Solaris has SIOCGIFNUM. */ | |
718e3744 | 68 | #ifdef SIOCGIFNUM |
d62a17ae | 69 | ret = ioctl(sock, SIOCGIFNUM, &ifnum); |
70 | if (ret < 0) | |
71 | ifnum = IFNUM_BASE; | |
72 | else | |
73 | ifnum++; | |
718e3744 | 74 | #else |
d62a17ae | 75 | ifnum = IFNUM_BASE; |
718e3744 | 76 | #endif /* SIOCGIFNUM */ |
77 | ||
d62a17ae | 78 | ifconf.ifc_buf = NULL; |
79 | ||
80 | lastlen = 0; | |
81 | /* Loop until SIOCGIFCONF success. */ | |
82 | for (;;) { | |
83 | ifconf.ifc_len = sizeof(struct ifreq) * ifnum; | |
84 | ifconf.ifc_buf = | |
85 | XREALLOC(MTYPE_TMP, ifconf.ifc_buf, ifconf.ifc_len); | |
86 | ||
87 | ret = ioctl(sock, SIOCGIFCONF, &ifconf); | |
88 | ||
89 | if (ret < 0) { | |
450971aa | 90 | flog_err_sys(EC_LIB_SYSTEM_CALL, "SIOCGIFCONF: %s", |
9df414fe | 91 | safe_strerror(errno)); |
d62a17ae | 92 | goto end; |
93 | } | |
94 | /* Repeatedly get info til buffer fails to grow. */ | |
95 | if (ifconf.ifc_len > lastlen) { | |
96 | lastlen = ifconf.ifc_len; | |
97 | ifnum += 10; | |
98 | continue; | |
99 | } | |
100 | /* Success. */ | |
101 | break; | |
718e3744 | 102 | } |
718e3744 | 103 | |
d62a17ae | 104 | /* Allocate interface. */ |
105 | ifreq = ifconf.ifc_req; | |
718e3744 | 106 | |
107 | #ifdef OPEN_BSD | |
d62a17ae | 108 | for (n = 0; n < ifconf.ifc_len;) { |
109 | unsigned int size; | |
110 | ||
111 | ifreq = (struct ifreq *)((caddr_t)ifconf.ifc_req + n); | |
f60a1188 IR |
112 | ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT, |
113 | VRF_DEFAULT_NAME); | |
d62a17ae | 114 | if_add_update(ifp); |
115 | size = ifreq->ifr_addr.sa_len; | |
116 | if (size < sizeof(ifreq->ifr_addr)) | |
117 | size = sizeof(ifreq->ifr_addr); | |
118 | size += sizeof(ifreq->ifr_name); | |
119 | n += size; | |
120 | } | |
718e3744 | 121 | #else |
d62a17ae | 122 | for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq)) { |
f60a1188 IR |
123 | ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT, |
124 | VRF_DEFAULT_NAME); | |
d62a17ae | 125 | if_add_update(ifp); |
126 | ifreq++; | |
127 | } | |
718e3744 | 128 | #endif /* OPEN_BSD */ |
129 | ||
d62a17ae | 130 | end: |
131 | close(sock); | |
132 | XFREE(MTYPE_TMP, ifconf.ifc_buf); | |
718e3744 | 133 | |
d62a17ae | 134 | return ret; |
718e3744 | 135 | } |
136 | ||
137 | /* Get interface's index by ioctl. */ | |
d62a17ae | 138 | static int if_get_index(struct interface *ifp) |
718e3744 | 139 | { |
ff880b78 | 140 | if_set_index(ifp, if_nametoindex(ifp->name)); |
d62a17ae | 141 | return ifp->ifindex; |
718e3744 | 142 | } |
143 | ||
144 | #ifdef SIOCGIFHWADDR | |
d62a17ae | 145 | static int if_get_hwaddr(struct interface *ifp) |
718e3744 | 146 | { |
d62a17ae | 147 | int ret; |
148 | struct ifreq ifreq; | |
149 | int i; | |
150 | ||
138a4965 | 151 | strlcpy(ifreq.ifr_name, ifp->name, sizeof(ifreq.ifr_name)); |
d62a17ae | 152 | ifreq.ifr_addr.sa_family = AF_INET; |
153 | ||
154 | /* Fetch Hardware address if available. */ | |
096f7609 | 155 | ret = vrf_if_ioctl(SIOCGIFHWADDR, (caddr_t)&ifreq, ifp->vrf->vrf_id); |
d62a17ae | 156 | if (ret < 0) |
157 | ifp->hw_addr_len = 0; | |
158 | else { | |
159 | memcpy(ifp->hw_addr, ifreq.ifr_hwaddr.sa_data, 6); | |
160 | ||
161 | for (i = 0; i < 6; i++) | |
162 | if (ifp->hw_addr[i] != 0) | |
163 | break; | |
164 | ||
165 | if (i == 6) | |
166 | ifp->hw_addr_len = 0; | |
167 | else | |
168 | ifp->hw_addr_len = 6; | |
169 | } | |
170 | return 0; | |
718e3744 | 171 | } |
172 | #endif /* SIOCGIFHWADDR */ | |
173 | ||
d62a17ae | 174 | static int if_getaddrs(void) |
718e3744 | 175 | { |
d62a17ae | 176 | int ret; |
177 | struct ifaddrs *ifap; | |
178 | struct ifaddrs *ifapfree; | |
179 | struct interface *ifp; | |
180 | int prefixlen; | |
181 | ||
182 | ret = getifaddrs(&ifap); | |
183 | if (ret != 0) { | |
450971aa | 184 | flog_err_sys(EC_LIB_SYSTEM_CALL, "getifaddrs(): %s", |
09c866e3 | 185 | safe_strerror(errno)); |
d62a17ae | 186 | return -1; |
718e3744 | 187 | } |
188 | ||
d62a17ae | 189 | for (ifapfree = ifap; ifap; ifap = ifap->ifa_next) { |
190 | if (ifap->ifa_addr == NULL) { | |
af4c2728 | 191 | flog_err( |
450971aa | 192 | EC_LIB_INTERFACE, |
d62a17ae | 193 | "%s: nonsensical ifaddr with NULL ifa_addr, ifname %s", |
194 | __func__, | |
195 | (ifap->ifa_name ? ifap->ifa_name : "(null)")); | |
196 | continue; | |
197 | } | |
198 | ||
a36898e7 | 199 | ifp = if_lookup_by_name(ifap->ifa_name, VRF_DEFAULT); |
d62a17ae | 200 | if (ifp == NULL) { |
450971aa | 201 | flog_err(EC_LIB_INTERFACE, |
1c50c1c0 QY |
202 | "if_getaddrs(): Can't lookup interface %s\n", |
203 | ifap->ifa_name); | |
d62a17ae | 204 | continue; |
205 | } | |
206 | ||
207 | if (ifap->ifa_addr->sa_family == AF_INET) { | |
208 | struct sockaddr_in *addr; | |
209 | struct sockaddr_in *mask; | |
210 | struct sockaddr_in *dest; | |
211 | struct in_addr *dest_pnt; | |
212 | int flags = 0; | |
213 | ||
214 | addr = (struct sockaddr_in *)ifap->ifa_addr; | |
215 | mask = (struct sockaddr_in *)ifap->ifa_netmask; | |
216 | prefixlen = ip_masklen(mask->sin_addr); | |
217 | ||
218 | dest_pnt = NULL; | |
219 | ||
608105a7 | 220 | if (if_is_pointopoint(ifp) && ifap->ifa_dstaddr |
d62a17ae | 221 | && !IPV4_ADDR_SAME(&addr->sin_addr, |
222 | &((struct sockaddr_in *) | |
223 | ifap->ifa_dstaddr) | |
224 | ->sin_addr)) { | |
225 | dest = (struct sockaddr_in *)ifap->ifa_dstaddr; | |
226 | dest_pnt = &dest->sin_addr; | |
227 | flags = ZEBRA_IFA_PEER; | |
228 | } else if (ifap->ifa_broadaddr | |
229 | && !IPV4_ADDR_SAME( | |
230 | &addr->sin_addr, | |
231 | &((struct sockaddr_in *) | |
232 | ifap->ifa_broadaddr) | |
233 | ->sin_addr)) { | |
234 | dest = (struct sockaddr_in *) | |
235 | ifap->ifa_broadaddr; | |
236 | dest_pnt = &dest->sin_addr; | |
237 | } | |
238 | ||
239 | connected_add_ipv4(ifp, flags, &addr->sin_addr, | |
cde1af84 AK |
240 | prefixlen, dest_pnt, NULL, |
241 | METRIC_MAX); | |
d62a17ae | 242 | } |
243 | if (ifap->ifa_addr->sa_family == AF_INET6) { | |
244 | struct sockaddr_in6 *addr; | |
245 | struct sockaddr_in6 *mask; | |
d62a17ae | 246 | int flags = 0; |
247 | ||
248 | addr = (struct sockaddr_in6 *)ifap->ifa_addr; | |
249 | mask = (struct sockaddr_in6 *)ifap->ifa_netmask; | |
250 | prefixlen = ip6_masklen(mask->sin6_addr); | |
251 | ||
ab836aab | 252 | #if defined(KAME) |
d62a17ae | 253 | if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { |
254 | addr->sin6_scope_id = | |
d7c0a89a | 255 | ntohs(*(uint16_t *)&addr->sin6_addr |
d62a17ae | 256 | .s6_addr[2]); |
257 | addr->sin6_addr.s6_addr[2] = | |
258 | addr->sin6_addr.s6_addr[3] = 0; | |
259 | } | |
260 | #endif | |
261 | ||
60c0687a | 262 | connected_add_ipv6(ifp, flags, &addr->sin6_addr, NULL, |
cde1af84 | 263 | prefixlen, NULL, METRIC_MAX); |
d62a17ae | 264 | } |
718e3744 | 265 | } |
718e3744 | 266 | |
d62a17ae | 267 | freeifaddrs(ifapfree); |
718e3744 | 268 | |
d62a17ae | 269 | return 0; |
718e3744 | 270 | } |
718e3744 | 271 | |
272 | /* Fetch interface information via ioctl(). */ | |
d62a17ae | 273 | static void interface_info_ioctl() |
718e3744 | 274 | { |
f4e14fdb | 275 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
d62a17ae | 276 | struct interface *ifp; |
277 | ||
451fda4f | 278 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 279 | if_get_index(ifp); |
718e3744 | 280 | #ifdef SIOCGIFHWADDR |
d62a17ae | 281 | if_get_hwaddr(ifp); |
718e3744 | 282 | #endif /* SIOCGIFHWADDR */ |
d62a17ae | 283 | if_get_flags(ifp); |
284 | if_get_mtu(ifp); | |
285 | if_get_metric(ifp); | |
286 | } | |
718e3744 | 287 | } |
288 | ||
289 | /* Lookup all interface information. */ | |
d62a17ae | 290 | void interface_list(struct zebra_ns *zns) |
718e3744 | 291 | { |
12f6fb97 | 292 | |
d62a17ae | 293 | zlog_info("interface_list: NS %u", zns->ns_id); |
12f6fb97 | 294 | |
d62a17ae | 295 | /* Linux can do both proc & ioctl, ioctl is the only way to get |
296 | interface aliases in 2.2 series kernels. */ | |
718e3744 | 297 | #ifdef HAVE_PROC_NET_DEV |
d62a17ae | 298 | interface_list_proc(); |
718e3744 | 299 | #endif /* HAVE_PROC_NET_DEV */ |
d62a17ae | 300 | interface_list_ioctl(); |
718e3744 | 301 | |
d62a17ae | 302 | /* After listing is done, get index, address, flags and other |
303 | interface's information. */ | |
304 | interface_info_ioctl(); | |
718e3744 | 305 | |
d62a17ae | 306 | if_getaddrs(); |
718e3744 | 307 | |
56c1f7d8 | 308 | #if defined(HAVE_PROC_NET_IF_INET6) |
d62a17ae | 309 | /* Linux provides interface's IPv6 address via |
310 | /proc/net/if_inet6. */ | |
311 | ifaddr_proc_ipv6(); | |
56c1f7d8 | 312 | #endif /* HAVE_PROC_NET_IF_INET6 */ |
718e3744 | 313 | } |
ddfeb486 DL |
314 | |
315 | #endif /* OPEN_BSD */ |