]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_ioctl.c
isisd: implement the 'lsp-too-large' notification
[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 #include "lib_errors.h"
37
38 #include "zebra/interface.h"
39 #include "zebra/rib.h"
40 #include "zebra/rt.h"
41 #include "zebra/zebra_errors.h"
42
43 #include <ifaddrs.h>
44
45 /* Interface looking up using infamous SIOCGIFCONF. */
46 static int interface_list_ioctl(void)
47 {
48 int ret;
49 int sock;
50 #define IFNUM_BASE 32
51 int ifnum;
52 struct ifreq *ifreq;
53 struct ifconf ifconf;
54 struct interface *ifp;
55 int n;
56 int lastlen;
57
58 /* Normally SIOCGIFCONF works with AF_INET socket. */
59 sock = socket(AF_INET, SOCK_DGRAM, 0);
60 if (sock < 0) {
61 flog_err_sys(EC_LIB_SOCKET,
62 "Can't make AF_INET socket stream: %s",
63 safe_strerror(errno));
64 return -1;
65 }
66
67 /* Set initial ifreq count. This will be double when SIOCGIFCONF
68 fail. Solaris has SIOCGIFNUM. */
69 #ifdef SIOCGIFNUM
70 ret = ioctl(sock, SIOCGIFNUM, &ifnum);
71 if (ret < 0)
72 ifnum = IFNUM_BASE;
73 else
74 ifnum++;
75 #else
76 ifnum = IFNUM_BASE;
77 #endif /* SIOCGIFNUM */
78
79 ifconf.ifc_buf = NULL;
80
81 lastlen = 0;
82 /* Loop until SIOCGIFCONF success. */
83 for (;;) {
84 ifconf.ifc_len = sizeof(struct ifreq) * ifnum;
85 ifconf.ifc_buf =
86 XREALLOC(MTYPE_TMP, ifconf.ifc_buf, ifconf.ifc_len);
87
88 ret = ioctl(sock, SIOCGIFCONF, &ifconf);
89
90 if (ret < 0) {
91 flog_err_sys(EC_LIB_SYSTEM_CALL, "SIOCGIFCONF: %s",
92 safe_strerror(errno));
93 goto end;
94 }
95 /* Repeatedly get info til buffer fails to grow. */
96 if (ifconf.ifc_len > lastlen) {
97 lastlen = ifconf.ifc_len;
98 ifnum += 10;
99 continue;
100 }
101 /* Success. */
102 break;
103 }
104
105 /* Allocate interface. */
106 ifreq = ifconf.ifc_req;
107
108 #ifdef OPEN_BSD
109 for (n = 0; n < ifconf.ifc_len;) {
110 unsigned int size;
111
112 ifreq = (struct ifreq *)((caddr_t)ifconf.ifc_req + n);
113 ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT);
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 }
121 #else
122 for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq)) {
123 ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT);
124 if_add_update(ifp);
125 ifreq++;
126 }
127 #endif /* OPEN_BSD */
128
129 end:
130 close(sock);
131 XFREE(MTYPE_TMP, ifconf.ifc_buf);
132
133 return ret;
134 }
135
136 /* Get interface's index by ioctl. */
137 static int if_get_index(struct interface *ifp)
138 {
139 if_set_index(ifp, if_nametoindex(ifp->name));
140 return ifp->ifindex;
141 }
142
143 #ifdef SIOCGIFHWADDR
144 static int if_get_hwaddr(struct interface *ifp)
145 {
146 int ret;
147 struct ifreq ifreq;
148 int i;
149
150 strncpy(ifreq.ifr_name, ifp->name, IFNAMSIZ);
151 ifreq.ifr_addr.sa_family = AF_INET;
152
153 /* Fetch Hardware address if available. */
154 ret = vrf_if_ioctl(SIOCGIFHWADDR, (caddr_t)&ifreq, ifp->vrf_id);
155 if (ret < 0)
156 ifp->hw_addr_len = 0;
157 else {
158 memcpy(ifp->hw_addr, ifreq.ifr_hwaddr.sa_data, 6);
159
160 for (i = 0; i < 6; i++)
161 if (ifp->hw_addr[i] != 0)
162 break;
163
164 if (i == 6)
165 ifp->hw_addr_len = 0;
166 else
167 ifp->hw_addr_len = 6;
168 }
169 return 0;
170 }
171 #endif /* SIOCGIFHWADDR */
172
173 static int if_getaddrs(void)
174 {
175 int ret;
176 struct ifaddrs *ifap;
177 struct ifaddrs *ifapfree;
178 struct interface *ifp;
179 int prefixlen;
180
181 ret = getifaddrs(&ifap);
182 if (ret != 0) {
183 flog_err_sys(EC_LIB_SYSTEM_CALL, "getifaddrs(): %s",
184 safe_strerror(errno));
185 return -1;
186 }
187
188 for (ifapfree = ifap; ifap; ifap = ifap->ifa_next) {
189 if (ifap->ifa_addr == NULL) {
190 flog_err(
191 EC_LIB_INTERFACE,
192 "%s: nonsensical ifaddr with NULL ifa_addr, ifname %s",
193 __func__,
194 (ifap->ifa_name ? ifap->ifa_name : "(null)"));
195 continue;
196 }
197
198 ifp = if_lookup_by_name(ifap->ifa_name, VRF_DEFAULT);
199 if (ifp == NULL) {
200 flog_err(EC_LIB_INTERFACE,
201 "if_getaddrs(): Can't lookup interface %s\n",
202 ifap->ifa_name);
203 continue;
204 }
205
206 if (ifap->ifa_addr->sa_family == AF_INET) {
207 struct sockaddr_in *addr;
208 struct sockaddr_in *mask;
209 struct sockaddr_in *dest;
210 struct in_addr *dest_pnt;
211 int flags = 0;
212
213 addr = (struct sockaddr_in *)ifap->ifa_addr;
214 mask = (struct sockaddr_in *)ifap->ifa_netmask;
215 prefixlen = ip_masklen(mask->sin_addr);
216
217 dest_pnt = NULL;
218
219 if (if_is_pointopoint(ifp) && ifap->ifa_dstaddr
220 && !IPV4_ADDR_SAME(&addr->sin_addr,
221 &((struct sockaddr_in *)
222 ifap->ifa_dstaddr)
223 ->sin_addr)) {
224 dest = (struct sockaddr_in *)ifap->ifa_dstaddr;
225 dest_pnt = &dest->sin_addr;
226 flags = ZEBRA_IFA_PEER;
227 } else if (ifap->ifa_broadaddr
228 && !IPV4_ADDR_SAME(
229 &addr->sin_addr,
230 &((struct sockaddr_in *)
231 ifap->ifa_broadaddr)
232 ->sin_addr)) {
233 dest = (struct sockaddr_in *)
234 ifap->ifa_broadaddr;
235 dest_pnt = &dest->sin_addr;
236 }
237
238 connected_add_ipv4(ifp, flags, &addr->sin_addr,
239 prefixlen, dest_pnt, NULL);
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);
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 */