]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_ioctl.c
Merge pull request #8167 from LabNConsulting/chopps/tests-add-gdb
[mirror_frr.git] / zebra / if_ioctl.c
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 *
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
24 #ifdef OPEN_BSD
25
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"
33 #include "vrf.h"
34 #include "vty.h"
35 #include "lib_errors.h"
36
37 #include "zebra/interface.h"
38 #include "zebra/rib.h"
39 #include "zebra/rt.h"
40 #include "zebra/zebra_errors.h"
41
42 #include <ifaddrs.h>
43
44 /* Interface looking up using infamous SIOCGIFCONF. */
45 static int interface_list_ioctl(void)
46 {
47 int ret;
48 int sock;
49 #define IFNUM_BASE 32
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) {
60 flog_err_sys(EC_LIB_SOCKET,
61 "Can't make AF_INET socket stream: %s",
62 safe_strerror(errno));
63 return -1;
64 }
65
66 /* Set initial ifreq count. This will be double when SIOCGIFCONF
67 fail. Solaris has SIOCGIFNUM. */
68 #ifdef SIOCGIFNUM
69 ret = ioctl(sock, SIOCGIFNUM, &ifnum);
70 if (ret < 0)
71 ifnum = IFNUM_BASE;
72 else
73 ifnum++;
74 #else
75 ifnum = IFNUM_BASE;
76 #endif /* SIOCGIFNUM */
77
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) {
90 flog_err_sys(EC_LIB_SYSTEM_CALL, "SIOCGIFCONF: %s",
91 safe_strerror(errno));
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;
102 }
103
104 /* Allocate interface. */
105 ifreq = ifconf.ifc_req;
106
107 #ifdef OPEN_BSD
108 for (n = 0; n < ifconf.ifc_len;) {
109 unsigned int size;
110
111 ifreq = (struct ifreq *)((caddr_t)ifconf.ifc_req + n);
112 ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT);
113 if_add_update(ifp);
114 size = ifreq->ifr_addr.sa_len;
115 if (size < sizeof(ifreq->ifr_addr))
116 size = sizeof(ifreq->ifr_addr);
117 size += sizeof(ifreq->ifr_name);
118 n += size;
119 }
120 #else
121 for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq)) {
122 ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT);
123 if_add_update(ifp);
124 ifreq++;
125 }
126 #endif /* OPEN_BSD */
127
128 end:
129 close(sock);
130 XFREE(MTYPE_TMP, ifconf.ifc_buf);
131
132 return ret;
133 }
134
135 /* Get interface's index by ioctl. */
136 static int if_get_index(struct interface *ifp)
137 {
138 if_set_index(ifp, if_nametoindex(ifp->name));
139 return ifp->ifindex;
140 }
141
142 #ifdef SIOCGIFHWADDR
143 static int if_get_hwaddr(struct interface *ifp)
144 {
145 int ret;
146 struct ifreq ifreq;
147 int i;
148
149 strlcpy(ifreq.ifr_name, ifp->name, sizeof(ifreq.ifr_name));
150 ifreq.ifr_addr.sa_family = AF_INET;
151
152 /* Fetch Hardware address if available. */
153 ret = vrf_if_ioctl(SIOCGIFHWADDR, (caddr_t)&ifreq, ifp->vrf_id);
154 if (ret < 0)
155 ifp->hw_addr_len = 0;
156 else {
157 memcpy(ifp->hw_addr, ifreq.ifr_hwaddr.sa_data, 6);
158
159 for (i = 0; i < 6; i++)
160 if (ifp->hw_addr[i] != 0)
161 break;
162
163 if (i == 6)
164 ifp->hw_addr_len = 0;
165 else
166 ifp->hw_addr_len = 6;
167 }
168 return 0;
169 }
170 #endif /* SIOCGIFHWADDR */
171
172 static int if_getaddrs(void)
173 {
174 int ret;
175 struct ifaddrs *ifap;
176 struct ifaddrs *ifapfree;
177 struct interface *ifp;
178 int prefixlen;
179
180 ret = getifaddrs(&ifap);
181 if (ret != 0) {
182 flog_err_sys(EC_LIB_SYSTEM_CALL, "getifaddrs(): %s",
183 safe_strerror(errno));
184 return -1;
185 }
186
187 for (ifapfree = ifap; ifap; ifap = ifap->ifa_next) {
188 if (ifap->ifa_addr == NULL) {
189 flog_err(
190 EC_LIB_INTERFACE,
191 "%s: nonsensical ifaddr with NULL ifa_addr, ifname %s",
192 __func__,
193 (ifap->ifa_name ? ifap->ifa_name : "(null)"));
194 continue;
195 }
196
197 ifp = if_lookup_by_name(ifap->ifa_name, VRF_DEFAULT);
198 if (ifp == NULL) {
199 flog_err(EC_LIB_INTERFACE,
200 "if_getaddrs(): Can't lookup interface %s\n",
201 ifap->ifa_name);
202 continue;
203 }
204
205 if (ifap->ifa_addr->sa_family == AF_INET) {
206 struct sockaddr_in *addr;
207 struct sockaddr_in *mask;
208 struct sockaddr_in *dest;
209 struct in_addr *dest_pnt;
210 int flags = 0;
211
212 addr = (struct sockaddr_in *)ifap->ifa_addr;
213 mask = (struct sockaddr_in *)ifap->ifa_netmask;
214 prefixlen = ip_masklen(mask->sin_addr);
215
216 dest_pnt = NULL;
217
218 if (if_is_pointopoint(ifp) && ifap->ifa_dstaddr
219 && !IPV4_ADDR_SAME(&addr->sin_addr,
220 &((struct sockaddr_in *)
221 ifap->ifa_dstaddr)
222 ->sin_addr)) {
223 dest = (struct sockaddr_in *)ifap->ifa_dstaddr;
224 dest_pnt = &dest->sin_addr;
225 flags = ZEBRA_IFA_PEER;
226 } else if (ifap->ifa_broadaddr
227 && !IPV4_ADDR_SAME(
228 &addr->sin_addr,
229 &((struct sockaddr_in *)
230 ifap->ifa_broadaddr)
231 ->sin_addr)) {
232 dest = (struct sockaddr_in *)
233 ifap->ifa_broadaddr;
234 dest_pnt = &dest->sin_addr;
235 }
236
237 connected_add_ipv4(ifp, flags, &addr->sin_addr,
238 prefixlen, dest_pnt, NULL,
239 METRIC_MAX);
240 }
241 if (ifap->ifa_addr->sa_family == AF_INET6) {
242 struct sockaddr_in6 *addr;
243 struct sockaddr_in6 *mask;
244 int flags = 0;
245
246 addr = (struct sockaddr_in6 *)ifap->ifa_addr;
247 mask = (struct sockaddr_in6 *)ifap->ifa_netmask;
248 prefixlen = ip6_masklen(mask->sin6_addr);
249
250 #if defined(KAME)
251 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
252 addr->sin6_scope_id =
253 ntohs(*(uint16_t *)&addr->sin6_addr
254 .s6_addr[2]);
255 addr->sin6_addr.s6_addr[2] =
256 addr->sin6_addr.s6_addr[3] = 0;
257 }
258 #endif
259
260 connected_add_ipv6(ifp, flags, &addr->sin6_addr, NULL,
261 prefixlen, NULL, METRIC_MAX);
262 }
263 }
264
265 freeifaddrs(ifapfree);
266
267 return 0;
268 }
269
270 /* Fetch interface information via ioctl(). */
271 static void interface_info_ioctl()
272 {
273 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
274 struct interface *ifp;
275
276 FOR_ALL_INTERFACES (vrf, ifp) {
277 if_get_index(ifp);
278 #ifdef SIOCGIFHWADDR
279 if_get_hwaddr(ifp);
280 #endif /* SIOCGIFHWADDR */
281 if_get_flags(ifp);
282 if_get_mtu(ifp);
283 if_get_metric(ifp);
284 }
285 }
286
287 /* Lookup all interface information. */
288 void interface_list(struct zebra_ns *zns)
289 {
290
291 zlog_info("interface_list: NS %u", zns->ns_id);
292
293 /* Linux can do both proc & ioctl, ioctl is the only way to get
294 interface aliases in 2.2 series kernels. */
295 #ifdef HAVE_PROC_NET_DEV
296 interface_list_proc();
297 #endif /* HAVE_PROC_NET_DEV */
298 interface_list_ioctl();
299
300 /* After listing is done, get index, address, flags and other
301 interface's information. */
302 interface_info_ioctl();
303
304 if_getaddrs();
305
306 #if defined(HAVE_PROC_NET_IF_INET6)
307 /* Linux provides interface's IPv6 address via
308 /proc/net/if_inet6. */
309 ifaddr_proc_ipv6();
310 #endif /* HAVE_PROC_NET_IF_INET6 */
311 }
312
313 #endif /* OPEN_BSD */