]> git.proxmox.com Git - mirror_frr.git/blob - zebra/if_ioctl_solaris.c
Merge pull request #2823 from opensourcerouting/snap-staticd
[mirror_frr.git] / zebra / if_ioctl_solaris.c
1 /*
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 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 SUNOS_5
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 "privs.h"
35 #include "vrf.h"
36 #include "vty.h"
37
38 #include "zebra/interface.h"
39 #include "zebra/ioctl_solaris.h"
40 #include "zebra/rib.h"
41
42 static int if_get_addr(struct interface *, struct sockaddr *, const char *);
43 static void interface_info_ioctl(struct interface *);
44 extern struct zebra_privs_t zserv_privs;
45
46 static int interface_list_ioctl(int af)
47 {
48 int ret;
49 int sock;
50 #define IFNUM_BASE 32
51 struct lifnum lifn;
52 int ifnum;
53 struct lifreq *lifreq;
54 struct lifconf lifconf;
55 struct interface *ifp;
56 int n;
57 int save_errno;
58 size_t needed, lastneeded = 0;
59 char *buf = NULL;
60
61 if (zserv_privs.change(ZPRIVS_RAISE))
62 zlog_err("Can't raise privileges");
63
64 sock = socket(af, SOCK_DGRAM, 0);
65 if (sock < 0) {
66 zlog_warn("Can't make %s socket stream: %s",
67 (af == AF_INET ? "AF_INET" : "AF_INET6"),
68 safe_strerror(errno));
69
70 if (zserv_privs.change(ZPRIVS_LOWER))
71 zlog_err("Can't lower privileges");
72
73 return -1;
74 }
75
76 calculate_lifc_len: /* must hold privileges to enter here */
77 lifn.lifn_family = af;
78 lifn.lifn_flags = LIFC_NOXMIT; /* we want NOXMIT interfaces too */
79 ret = ioctl(sock, SIOCGLIFNUM, &lifn);
80 save_errno = errno;
81
82 if (zserv_privs.change(ZPRIVS_LOWER))
83 zlog_err("Can't lower privileges");
84
85 if (ret < 0) {
86 zlog_warn("interface_list_ioctl: SIOCGLIFNUM failed %s",
87 safe_strerror(save_errno));
88 close(sock);
89 return -1;
90 }
91 ifnum = lifn.lifn_count;
92
93 /*
94 * When calculating the buffer size needed, add a small number
95 * of interfaces to those we counted. We do this to capture
96 * the interface status of potential interfaces which may have
97 * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
98 */
99 needed = (ifnum + 4) * sizeof(struct lifreq);
100 if (needed > lastneeded || needed < lastneeded / 2) {
101 if (buf != NULL)
102 XFREE(MTYPE_TMP, buf);
103 buf = XMALLOC(MTYPE_TMP, needed);
104 }
105 lastneeded = needed;
106
107 lifconf.lifc_family = af;
108 lifconf.lifc_flags = LIFC_NOXMIT;
109 lifconf.lifc_len = needed;
110 lifconf.lifc_buf = buf;
111
112 if (zserv_privs.change(ZPRIVS_RAISE))
113 zlog_err("Can't raise privileges");
114
115 ret = ioctl(sock, SIOCGLIFCONF, &lifconf);
116
117 if (ret < 0) {
118 if (errno == EINVAL)
119 goto calculate_lifc_len; /* deliberately hold privileges
120 */
121
122 zlog_warn("SIOCGLIFCONF: %s", safe_strerror(errno));
123
124 if (zserv_privs.change(ZPRIVS_LOWER))
125 zlog_err("Can't lower privileges");
126
127 goto end;
128 }
129
130 if (zserv_privs.change(ZPRIVS_LOWER))
131 zlog_err("Can't lower privileges");
132
133 /* Allocate interface. */
134 lifreq = lifconf.lifc_req;
135
136 for (n = 0; n < lifconf.lifc_len; n += sizeof(struct lifreq)) {
137 /* we treat Solaris logical interfaces as addresses, because
138 * that is
139 * how PF_ROUTE on Solaris treats them. Hence we can not
140 * directly use
141 * the lifreq_name to get the ifp. We need to normalise the
142 * name
143 * before attempting get.
144 *
145 * Solaris logical interface names are in the form of:
146 * <interface name>:<logical interface id>
147 */
148 unsigned int normallen = 0;
149 uint64_t lifflags;
150
151 /* We should exclude ~IFF_UP interfaces, as we'll find out about
152 * them
153 * coming up later through RTM_NEWADDR message on the route
154 * socket.
155 */
156 if (if_get_flags_direct(lifreq->lifr_name, &lifflags,
157 lifreq->lifr_addr.ss_family)
158 || !CHECK_FLAG(lifflags, IFF_UP)) {
159 lifreq++;
160 continue;
161 }
162
163 /* Find the normalised name */
164 while ((normallen < sizeof(lifreq->lifr_name))
165 && (*(lifreq->lifr_name + normallen) != '\0')
166 && (*(lifreq->lifr_name + normallen) != ':'))
167 normallen++;
168
169 ifp = if_get_by_name(lifreq->lifr_name, VRF_DEFAULT, 0);
170
171 if (lifreq->lifr_addr.ss_family == AF_INET)
172 ifp->flags |= IFF_IPV4;
173
174 if (lifreq->lifr_addr.ss_family == AF_INET6) {
175 ifp->flags |= IFF_IPV6;
176 }
177
178 if_add_update(ifp);
179
180 interface_info_ioctl(ifp);
181
182 /* If a logical interface pass the full name so it can be
183 * as a label on the address
184 */
185 if (*(lifreq->lifr_name + normallen) != '\0')
186 if_get_addr(ifp, (struct sockaddr *)&lifreq->lifr_addr,
187 lifreq->lifr_name);
188 else
189 if_get_addr(ifp, (struct sockaddr *)&lifreq->lifr_addr,
190 NULL);
191
192 /* Poke the interface flags. Lets IFF_UP mangling kick in */
193 if_flags_update(ifp, ifp->flags);
194
195 lifreq++;
196 }
197
198 end:
199 close(sock);
200 XFREE(MTYPE_TMP, lifconf.lifc_buf);
201 return ret;
202 }
203
204 /* Get interface's index by ioctl. */
205 static int if_get_index(struct interface *ifp)
206 {
207 int ret;
208 struct lifreq lifreq;
209
210 lifreq_set_name(&lifreq, ifp->name);
211
212 if (ifp->flags & IFF_IPV4)
213 ret = AF_IOCTL(AF_INET, SIOCGLIFINDEX, (caddr_t)&lifreq);
214 else if (ifp->flags & IFF_IPV6)
215 ret = AF_IOCTL(AF_INET6, SIOCGLIFINDEX, (caddr_t)&lifreq);
216 else
217 ret = -1;
218
219 if (ret < 0) {
220 zlog_warn("SIOCGLIFINDEX(%s) failed", ifp->name);
221 return ret;
222 }
223
224 /* OK we got interface index. */
225 #ifdef ifr_ifindex
226 if_set_index(ifp, lifreq.lifr_ifindex);
227 #else
228 if_set_index(ifp, lifreq.lifr_index);
229 #endif
230 return ifp->ifindex;
231 }
232
233
234 /* Interface address lookup by ioctl. This function only looks up
235 IPv4 address. */
236 #define ADDRLEN(sa) \
237 (((sa)->sa_family == AF_INET ? sizeof(struct sockaddr_in) \
238 : sizeof(struct sockaddr_in6)))
239
240 #define SIN(s) ((struct sockaddr_in *)(s))
241 #define SIN6(s) ((struct sockaddr_in6 *)(s))
242
243 /* Retrieve address information for the given ifp */
244 static int if_get_addr(struct interface *ifp, struct sockaddr *addr,
245 const char *label)
246 {
247 int ret;
248 struct lifreq lifreq;
249 struct sockaddr_storage mask, dest;
250 char *dest_pnt = NULL;
251 uint8_t prefixlen = 0;
252 afi_t af;
253 int flags = 0;
254
255 /* Interface's name and address family.
256 * We need to use the logical interface name / label, if we've been
257 * given one, in order to get the right address
258 */
259 strncpy(lifreq.lifr_name, (label ? label : ifp->name), IFNAMSIZ);
260
261 /* Interface's address. */
262 memcpy(&lifreq.lifr_addr, addr, ADDRLEN(addr));
263 af = addr->sa_family;
264
265 /* Point to point or broad cast address pointer init. */
266 dest_pnt = NULL;
267
268 if (AF_IOCTL(af, SIOCGLIFDSTADDR, (caddr_t)&lifreq) >= 0) {
269 memcpy(&dest, &lifreq.lifr_dstaddr, ADDRLEN(addr));
270 if (af == AF_INET)
271 dest_pnt = (char *)&(SIN(&dest)->sin_addr);
272 else
273 dest_pnt = (char *)&(SIN6(&dest)->sin6_addr);
274 flags = ZEBRA_IFA_PEER;
275 }
276
277 if (af == AF_INET) {
278 ret = if_ioctl(SIOCGLIFNETMASK, (caddr_t)&lifreq);
279
280 if (ret < 0) {
281 if (errno != EADDRNOTAVAIL) {
282 zlog_warn("SIOCGLIFNETMASK (%s) fail: %s",
283 ifp->name, safe_strerror(errno));
284 return ret;
285 }
286 return 0;
287 }
288 memcpy(&mask, &lifreq.lifr_addr, ADDRLEN(addr));
289
290 prefixlen = ip_masklen(SIN(&mask)->sin_addr);
291 if (!dest_pnt
292 && (if_ioctl(SIOCGLIFBRDADDR, (caddr_t)&lifreq) >= 0)) {
293 memcpy(&dest, &lifreq.lifr_broadaddr,
294 sizeof(struct sockaddr_in));
295 dest_pnt = (char *)&SIN(&dest)->sin_addr;
296 }
297 } else if (af == AF_INET6) {
298 if (if_ioctl_ipv6(SIOCGLIFSUBNET, (caddr_t)&lifreq) < 0) {
299 if (ifp->flags & IFF_POINTOPOINT)
300 prefixlen = IPV6_MAX_BITLEN;
301 else
302 zlog_warn("SIOCGLIFSUBNET (%s) fail: %s",
303 ifp->name, safe_strerror(errno));
304 } else {
305 prefixlen = lifreq.lifr_addrlen;
306 }
307 }
308
309 /* Set address to the interface. */
310 if (af == AF_INET)
311 connected_add_ipv4(ifp, flags, &SIN(addr)->sin_addr, prefixlen,
312 (struct in_addr *)dest_pnt, label);
313 else if (af == AF_INET6)
314 connected_add_ipv6(ifp, flags, &SIN6(addr)->sin6_addr, NULL,
315 prefixlen, label);
316
317 return 0;
318 }
319
320 /* Fetch interface information via ioctl(). */
321 static void interface_info_ioctl(struct interface *ifp)
322 {
323 if_get_index(ifp);
324 if_get_flags(ifp);
325 if_get_mtu(ifp);
326 if_get_metric(ifp);
327 }
328
329 /* Lookup all interface information. */
330 void interface_list(struct zebra_ns *zns)
331 {
332 if (zns->ns_id != NS_DEFAULT) {
333 zlog_warn("interface_list: ignore NS %u", zns->ns_id);
334 return;
335 }
336 interface_list_ioctl(AF_INET);
337 interface_list_ioctl(AF_INET6);
338 interface_list_ioctl(AF_UNSPEC);
339 }
340
341 struct connected *if_lookup_linklocal(struct interface *ifp)
342 {
343 struct listnode *node;
344 struct connected *ifc;
345
346 if (ifp == NULL)
347 return NULL;
348
349 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
350 if ((ifc->address->family == AF_INET6)
351 && (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6)))
352 return ifc;
353 }
354
355 return NULL;
356 }
357
358 #endif /* SUNOS_5 */