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