blob: 9441abed844762affb3c901291bd5feade61e0fc [file] [log] [blame]
paul88424682004-05-09 18:21:35 +00001/*
2 * Interface looking up by ioctl () on Solaris.
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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "if.h"
26#include "sockunion.h"
27#include "prefix.h"
28#include "ioctl.h"
29#include "connected.h"
30#include "memory.h"
31#include "log.h"
paul48a46fa2004-05-11 10:55:22 +000032#include "privs.h"
paul88424682004-05-09 18:21:35 +000033
34#include "zebra/interface.h"
35
36void lifreq_set_name (struct lifreq *, struct interface *);
37static int if_get_addr (struct interface *, struct sockaddr *);
38static void interface_info_ioctl (struct interface *);
paul48a46fa2004-05-11 10:55:22 +000039extern struct zebra_privs_t zserv_privs;
paul88424682004-05-09 18:21:35 +000040
41int
42interface_list_ioctl (int af)
43{
44 int ret;
45 int sock;
46#define IFNUM_BASE 32
47 struct lifnum lifn;
48 int ifnum;
49 struct lifreq *lifreq;
50 struct lifconf lifconf;
51 struct interface *ifp;
52 int n;
53 size_t needed, lastneeded = 0;
54 char *buf = NULL;
55
56 if (zserv_privs.change(ZPRIVS_RAISE))
57 zlog (NULL, LOG_ERR, "Can't raise privileges");
58
59 sock = socket (af, SOCK_DGRAM, 0);
60 if (sock < 0)
61 {
62 zlog_warn ("Can't make %s socket stream: %s",
63 (af == AF_INET ? "AF_INET" : "AF_INET6"), strerror (errno));
64
65 if (zserv_privs.change(ZPRIVS_LOWER))
66 zlog (NULL, LOG_ERR, "Can't lower privileges");
67
68 return -1;
69 }
70
71calculate_lifc_len: /* must hold privileges to enter here */
72 lifn.lifn_family = af;
73 lifn.lifn_flags = 0;
74 ret = ioctl (sock, SIOCGLIFNUM, &lifn);
75
76 if (zserv_privs.change(ZPRIVS_LOWER))
77 zlog (NULL, LOG_ERR, "Can't lower privileges");
78
79 if (ret < 0)
80 {
81 zlog_warn ("interface_list_ioctl: SIOCGLIFNUM failed %s",
82 strerror (errno));
83 close (sock);
84 return -1;
85 }
86 ifnum = lifn.lifn_count;
87
88 /*
89 * When calculating the buffer size needed, add a small number
90 * of interfaces to those we counted. We do this to capture
91 * the interface status of potential interfaces which may have
92 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
93 */
94 needed = (ifnum + 4) * sizeof (struct lifreq);
95 if (needed > lastneeded || needed < lastneeded / 2)
96 {
97 if (buf != NULL)
98 XFREE (MTYPE_TMP, buf);
99 if ((buf = XMALLOC (MTYPE_TMP, needed)) == NULL)
100 {
101 zlog_warn ("interface_list_ioctl: malloc failed");
102 close (sock);
103 return -1;
104 }
105 }
106 lastneeded = needed;
107
108 lifconf.lifc_family = af;
109 lifconf.lifc_flags = 0;
110 lifconf.lifc_len = needed;
111 lifconf.lifc_buf = buf;
112
113 if (zserv_privs.change(ZPRIVS_RAISE))
114 zlog (NULL, LOG_ERR, "Can't raise privileges");
115
116 ret = ioctl (sock, SIOCGLIFCONF, &lifconf);
117
118 if (ret < 0)
119 {
120 if (errno == EINVAL)
121 goto calculate_lifc_len; /* deliberately hold privileges */
122
123 zlog_warn ("SIOCGLIFCONF: %s", strerror (errno));
124
125 if (zserv_privs.change(ZPRIVS_LOWER))
126 zlog (NULL, LOG_ERR, "Can't lower privileges");
127
128 goto end;
129 }
130
131 if (zserv_privs.change(ZPRIVS_LOWER))
132 zlog (NULL, LOG_ERR, "Can't lower privileges");
133
134 /* Allocate interface. */
135 lifreq = lifconf.lifc_req;
136
137 for (n = 0; n < lifconf.lifc_len; n += sizeof (struct lifreq))
138 {
139 ifp = if_get_by_name (lifreq->lifr_name);
140 if (lifreq->lifr_addr.ss_family == AF_INET)
141 ifp->flags |= IFF_IPV4;
142 if (lifreq->lifr_addr.ss_family == AF_INET6)
143 ifp->flags |= IFF_IPV6;
144 if_add_update (ifp);
145 interface_info_ioctl (ifp);
146 if_get_addr (ifp, (struct sockaddr *) &lifreq->lifr_addr);
147 lifreq++;
148 }
149
150end:
151 close (sock);
152 XFREE (MTYPE_TMP, lifconf.lifc_buf);
153 return ret;
154}
155
156/* Get interface's index by ioctl. */
157int
158if_get_index (struct interface *ifp)
159{
160 int ret;
161 struct lifreq lifreq;
162
163 lifreq_set_name (&lifreq, ifp);
164
paul88424682004-05-09 18:21:35 +0000165 if (ifp->flags & IFF_IPV4)
166 ret = AF_IOCTL (AF_INET, SIOCGLIFINDEX, (caddr_t) & lifreq);
167 else if (ifp->flags & IFF_IPV6)
168 ret = AF_IOCTL (AF_INET6, SIOCGLIFINDEX, (caddr_t) & lifreq);
169 else
170 ret = -1;
171
paul88424682004-05-09 18:21:35 +0000172 if (ret < 0)
173 {
174 zlog_warn ("SIOCGLIFINDEX(%s) failed", ifp->name);
175 return ret;
176 }
177
178 /* OK we got interface index. */
179#ifdef ifr_ifindex
180 ifp->ifindex = lifreq.lifr_ifindex;
181#else
182 ifp->ifindex = lifreq.lifr_index;
183#endif
184 return ifp->ifindex;
185
186}
187
188
189/* Interface address lookup by ioctl. This function only looks up
190 IPv4 address. */
191#define ADDRLEN(sa) (((sa)->sa_family == AF_INET ? \
192 sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)))
193
194#define SIN(s) ((struct sockaddr_in *)(s))
195#define SIN6(s) ((struct sockaddr_in6 *)(s))
196
197static int
198if_get_addr (struct interface *ifp, struct sockaddr *addr)
199{
200 int ret;
201 struct lifreq lifreq;
202 struct sockaddr_storage mask, dest;
203 char *dest_pnt = NULL;
204 u_char prefixlen = 0;
205 afi_t af;
206
207 /* Interface's name and address family. */
208 strncpy (lifreq.lifr_name, ifp->name, IFNAMSIZ);
209
210 /* Interface's address. */
211 memcpy (&lifreq.lifr_addr, addr, ADDRLEN (addr));
212 af = addr->sa_family;
213
214 /* Point to point or broad cast address pointer init. */
215 dest_pnt = NULL;
216
217 if (ifp->flags & IFF_POINTOPOINT)
218 {
paul88424682004-05-09 18:21:35 +0000219 ret = AF_IOCTL (af, SIOCGLIFDSTADDR, (caddr_t) & lifreq);
paul88424682004-05-09 18:21:35 +0000220
221 if (ret < 0)
222 {
223 zlog_warn ("SIOCGLIFDSTADDR (%s) fail: %s",
224 ifp->name, strerror (errno));
225 return ret;
226 }
227 memcpy (&dest, &lifreq.lifr_dstaddr, ADDRLEN (addr));
228 if (af == AF_INET)
229 dest_pnt = (char *) &(SIN (&dest)->sin_addr);
230 else
231 dest_pnt = (char *) &(SIN6 (&dest)->sin6_addr);
232 }
233
234 if (af == AF_INET)
235 {
236 ret = if_ioctl (SIOCGLIFNETMASK, (caddr_t) & lifreq);
237
238 if (ret < 0)
239 {
240 if (errno != EADDRNOTAVAIL)
241 {
242 zlog_warn ("SIOCGLIFNETMASK (%s) fail: %s", ifp->name,
243 strerror (errno));
244 return ret;
245 }
246 return 0;
247 }
248 memcpy (&mask, &lifreq.lifr_addr, ADDRLEN (addr));
249
250 prefixlen = ip_masklen (SIN (&mask)->sin_addr);
251 if (ifp->flags & IFF_BROADCAST)
252 {
253 ret = if_ioctl (SIOCGLIFBRDADDR, (caddr_t) & lifreq);
254 if (ret < 0)
255 {
256 if (errno != EADDRNOTAVAIL)
257 {
258 zlog_warn ("SIOCGLIFBRDADDR (%s) fail: %s",
259 ifp->name, strerror (errno));
260 return ret;
261 }
262 return 0;
263 }
264 memcpy (&dest, &lifreq.lifr_broadaddr, sizeof (struct sockaddr_in));
265 dest_pnt = (char *) &SIN (&dest)->sin_addr;
266 }
267 }
268 else
269 {
270 if (ifp->flags & IFF_POINTOPOINT)
271 {
272 prefixlen = IPV6_MAX_BITLEN;
273 }
274 else
275 {
276 ret = if_ioctl_ipv6 (SIOCGLIFSUBNET, (caddr_t) & lifreq);
277 if (ret < 0)
278 {
279 zlog_warn ("SIOCGLIFSUBNET (%s) fail: %s",
280 ifp->name, strerror (errno));
281 }
282 else
283 {
284 prefixlen = lifreq.lifr_addrlen;
285 }
286 }
287 }
288
289
290 /* Set address to the interface. */
291 if (af == AF_INET)
292 connected_add_ipv4 (ifp, 0, &SIN (addr)->sin_addr, prefixlen,
293 (struct in_addr *) dest_pnt, NULL);
294 else
295 connected_add_ipv6 (ifp, &SIN6 (addr)->sin6_addr, prefixlen,
296 (struct in6_addr *) dest_pnt);
297
298 return 0;
299}
300
301/* Fetch interface information via ioctl(). */
302static void
303interface_info_ioctl (struct interface *ifp)
304{
305 if_get_index (ifp);
306 if_get_flags (ifp);
307 if_get_mtu (ifp);
308 if_get_metric (ifp);
309}
310
311/* Lookup all interface information. */
312void
313interface_list ()
314{
315 interface_list_ioctl (AF_INET);
316 interface_list_ioctl (AF_INET6);
317}
318
319struct connected *
320if_lookup_linklocal (struct interface *ifp)
321{
322 listnode node;
323 struct connected *ifc;
324
325 if (ifp == NULL)
326 return NULL;
327
328 for (node = listhead (ifp->connected); node; node = nextnode (node))
329 {
330 ifc = getdata (node);
331
332 if ((ifc->address->family == AF_INET6) &&
333 (IN6_IS_ADDR_LINKLOCAL (&ifc->address->u.prefix6)))
334 return ifc;
335 }
336 return NULL;
337}