]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_ioctl.c
Merge pull request #1058 from chiragshah6/mdev
[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 "zebra_memory.h"
33 #include "log.h"
34 #include "vrf.h"
35 #include "vty.h"
36
37 #include "zebra/interface.h"
38 #include "zebra/rib.h"
39
40 #include <ifaddrs.h>
41
42 /* Interface looking up using infamous SIOCGIFCONF. */
43 static int interface_list_ioctl(void)
44 {
45 int ret;
46 int sock;
47 #define IFNUM_BASE 32
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. */
65 #ifdef SIOCGIFNUM
66 ret = ioctl(sock, SIOCGIFNUM, &ifnum);
67 if (ret < 0)
68 ifnum = IFNUM_BASE;
69 else
70 ifnum++;
71 #else
72 ifnum = IFNUM_BASE;
73 #endif /* SIOCGIFNUM */
74
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;
98 }
99
100 /* Allocate interface. */
101 ifreq = ifconf.ifc_req;
102
103 #ifdef OPEN_BSD
104 for (n = 0; n < ifconf.ifc_len;) {
105 unsigned int size;
106
107 ifreq = (struct ifreq *)((caddr_t)ifconf.ifc_req + n);
108 ifp = if_get_by_name_len(
109 ifreq->ifr_name,
110 strnlen(ifreq->ifr_name, sizeof(ifreq->ifr_name)),
111 VRF_DEFAULT, 0);
112 if_add_update(ifp);
113 size = ifreq->ifr_addr.sa_len;
114 if (size < sizeof(ifreq->ifr_addr))
115 size = sizeof(ifreq->ifr_addr);
116 size += sizeof(ifreq->ifr_name);
117 n += size;
118 }
119 #else
120 for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq)) {
121 ifp = if_get_by_name_len(
122 ifreq->ifr_name,
123 strnlen(ifreq->ifr_name, sizeof(ifreq->ifr_name)),
124 VRF_DEFAULT, 0);
125 if_add_update(ifp);
126 ifreq++;
127 }
128 #endif /* OPEN_BSD */
129
130 end:
131 close(sock);
132 XFREE(MTYPE_TMP, ifconf.ifc_buf);
133
134 return ret;
135 }
136
137 /* Get interface's index by ioctl. */
138 static int if_get_index(struct interface *ifp)
139 {
140 ifp->ifindex = if_nametoindex(ifp->name);
141 return ifp->ifindex;
142 }
143
144 #ifdef SIOCGIFHWADDR
145 static int if_get_hwaddr(struct interface *ifp)
146 {
147 int ret;
148 struct ifreq ifreq;
149 int i;
150
151 strncpy(ifreq.ifr_name, ifp->name, IFNAMSIZ);
152 ifreq.ifr_addr.sa_family = AF_INET;
153
154 /* Fetch Hardware address if available. */
155 ret = if_ioctl(SIOCGIFHWADDR, (caddr_t)&ifreq);
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;
171 }
172 #endif /* SIOCGIFHWADDR */
173
174 static int if_getaddrs(void)
175 {
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) {
184 zlog_err("getifaddrs(): %s", safe_strerror(errno));
185 return -1;
186 }
187
188 for (ifapfree = ifap; ifap; ifap = ifap->ifa_next) {
189 if (ifap->ifa_addr == NULL) {
190 zlog_err(
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 zlog_err("if_getaddrs(): Can't lookup interface %s\n",
200 ifap->ifa_name);
201 continue;
202 }
203
204 if (ifap->ifa_addr->sa_family == AF_INET) {
205 struct sockaddr_in *addr;
206 struct sockaddr_in *mask;
207 struct sockaddr_in *dest;
208 struct in_addr *dest_pnt;
209 int flags = 0;
210
211 addr = (struct sockaddr_in *)ifap->ifa_addr;
212 mask = (struct sockaddr_in *)ifap->ifa_netmask;
213 prefixlen = ip_masklen(mask->sin_addr);
214
215 dest_pnt = NULL;
216
217 if (if_is_pointopoint(ifp) && ifap->ifa_dstaddr
218 && !IPV4_ADDR_SAME(&addr->sin_addr,
219 &((struct sockaddr_in *)
220 ifap->ifa_dstaddr)
221 ->sin_addr)) {
222 dest = (struct sockaddr_in *)ifap->ifa_dstaddr;
223 dest_pnt = &dest->sin_addr;
224 flags = ZEBRA_IFA_PEER;
225 } else if (ifap->ifa_broadaddr
226 && !IPV4_ADDR_SAME(
227 &addr->sin_addr,
228 &((struct sockaddr_in *)
229 ifap->ifa_broadaddr)
230 ->sin_addr)) {
231 dest = (struct sockaddr_in *)
232 ifap->ifa_broadaddr;
233 dest_pnt = &dest->sin_addr;
234 }
235
236 connected_add_ipv4(ifp, flags, &addr->sin_addr,
237 prefixlen, dest_pnt, NULL);
238 }
239 if (ifap->ifa_addr->sa_family == AF_INET6) {
240 struct sockaddr_in6 *addr;
241 struct sockaddr_in6 *mask;
242 int flags = 0;
243
244 addr = (struct sockaddr_in6 *)ifap->ifa_addr;
245 mask = (struct sockaddr_in6 *)ifap->ifa_netmask;
246 prefixlen = ip6_masklen(mask->sin6_addr);
247
248 #if defined(KAME)
249 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
250 addr->sin6_scope_id =
251 ntohs(*(u_int16_t *)&addr->sin6_addr
252 .s6_addr[2]);
253 addr->sin6_addr.s6_addr[2] =
254 addr->sin6_addr.s6_addr[3] = 0;
255 }
256 #endif
257
258 connected_add_ipv6(ifp, flags, &addr->sin6_addr,
259 prefixlen, NULL);
260 }
261 }
262
263 freeifaddrs(ifapfree);
264
265 return 0;
266 }
267
268 /* Fetch interface information via ioctl(). */
269 static void interface_info_ioctl()
270 {
271 struct listnode *node, *nnode;
272 struct interface *ifp;
273
274 for (ALL_LIST_ELEMENTS(vrf_iflist(VRF_DEFAULT), node, nnode, ifp)) {
275 if_get_index(ifp);
276 #ifdef SIOCGIFHWADDR
277 if_get_hwaddr(ifp);
278 #endif /* SIOCGIFHWADDR */
279 if_get_flags(ifp);
280 if_get_mtu(ifp);
281 if_get_metric(ifp);
282 }
283 }
284
285 /* Lookup all interface information. */
286 void interface_list(struct zebra_ns *zns)
287 {
288
289 zlog_info("interface_list: NS %u", zns->ns_id);
290
291 /* Linux can do both proc & ioctl, ioctl is the only way to get
292 interface aliases in 2.2 series kernels. */
293 #ifdef HAVE_PROC_NET_DEV
294 interface_list_proc();
295 #endif /* HAVE_PROC_NET_DEV */
296 interface_list_ioctl();
297
298 /* After listing is done, get index, address, flags and other
299 interface's information. */
300 interface_info_ioctl();
301
302 if_getaddrs();
303
304 #if defined(HAVE_PROC_NET_IF_INET6)
305 /* Linux provides interface's IPv6 address via
306 /proc/net/if_inet6. */
307 ifaddr_proc_ipv6();
308 #endif /* HAVE_PROC_NET_IF_INET6 */
309 }
310
311 #endif /* OPEN_BSD */