]>
Commit | Line | Data |
---|---|---|
8842468c | 1 | /* |
2 | * Common ioctl functions for 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 "linklist.h" | |
26 | #include "if.h" | |
27 | #include "prefix.h" | |
28 | #include "ioctl.h" | |
29 | #include "log.h" | |
48a46fa0 | 30 | #include "privs.h" |
8842468c | 31 | |
32 | #include "zebra/rib.h" | |
33 | #include "zebra/rt.h" | |
5c78b3d0 | 34 | #include "zebra/interface.h" |
8842468c | 35 | |
48a46fa0 | 36 | extern struct zebra_privs_t zserv_privs; |
8842468c | 37 | |
38 | /* clear and set interface name string */ | |
39 | void | |
5c78b3d0 | 40 | lifreq_set_name (struct lifreq *lifreq, const char *ifname) |
8842468c | 41 | { |
5c78b3d0 | 42 | strncpy (lifreq->lifr_name, ifname, IFNAMSIZ); |
8842468c | 43 | } |
44 | ||
45 | /* call ioctl system call */ | |
46 | int | |
47 | if_ioctl (u_long request, caddr_t buffer) | |
48 | { | |
49 | int sock; | |
4460e7a4 | 50 | int ret; |
51 | int err; | |
8842468c | 52 | |
53 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
54 | zlog (NULL, LOG_ERR, "Can't raise privileges"); | |
55 | ||
56 | sock = socket (AF_INET, SOCK_DGRAM, 0); | |
57 | if (sock < 0) | |
58 | { | |
6a52d0d1 | 59 | int save_errno = errno; |
8842468c | 60 | if (zserv_privs.change(ZPRIVS_LOWER)) |
61 | zlog (NULL, LOG_ERR, "Can't lower privileges"); | |
6a52d0d1 | 62 | zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno)); |
8842468c | 63 | exit (1); |
64 | } | |
65 | ||
4460e7a4 | 66 | if ((ret = ioctl (sock, request, buffer)) < 0) |
67 | err = errno; | |
8842468c | 68 | |
69 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
70 | zlog (NULL, LOG_ERR, "Can't lower privileges"); | |
71 | ||
8842468c | 72 | close (sock); |
73 | ||
74 | if (ret < 0) | |
75 | { | |
76 | errno = err; | |
77 | return ret; | |
78 | } | |
79 | return 0; | |
80 | } | |
81 | ||
5b73a671 | 82 | |
8842468c | 83 | int |
84 | if_ioctl_ipv6 (u_long request, caddr_t buffer) | |
85 | { | |
5b73a671 | 86 | #ifdef HAVE_IPV6 |
8842468c | 87 | int sock; |
4460e7a4 | 88 | int ret; |
89 | int err; | |
8842468c | 90 | |
91 | if (zserv_privs.change(ZPRIVS_RAISE)) | |
92 | zlog (NULL, LOG_ERR, "Can't raise privileges"); | |
93 | ||
94 | sock = socket (AF_INET6, SOCK_DGRAM, 0); | |
95 | if (sock < 0) | |
96 | { | |
6a52d0d1 | 97 | int save_errno = errno; |
8842468c | 98 | if (zserv_privs.change(ZPRIVS_LOWER)) |
99 | zlog (NULL, LOG_ERR, "Can't lower privileges"); | |
6a52d0d1 | 100 | zlog_err("Cannot create IPv6 datagram socket: %s", |
101 | safe_strerror(save_errno)); | |
8842468c | 102 | exit (1); |
103 | } | |
104 | ||
4460e7a4 | 105 | if ((ret = ioctl (sock, request, buffer)) < 0) |
106 | err = errno; | |
8842468c | 107 | |
108 | if (zserv_privs.change(ZPRIVS_LOWER)) | |
109 | zlog (NULL, LOG_ERR, "Can't lower privileges"); | |
110 | ||
8842468c | 111 | close (sock); |
112 | ||
113 | if (ret < 0) | |
114 | { | |
115 | errno = err; | |
116 | return ret; | |
117 | } | |
5b73a671 | 118 | #endif /* HAVE_IPV6 */ |
119 | ||
8842468c | 120 | return 0; |
121 | } | |
8842468c | 122 | |
123 | /* | |
124 | * get interface metric | |
125 | * -- if value is not avaliable set -1 | |
126 | */ | |
127 | void | |
128 | if_get_metric (struct interface *ifp) | |
129 | { | |
130 | struct lifreq lifreq; | |
131 | int ret; | |
132 | ||
5c78b3d0 | 133 | lifreq_set_name (&lifreq, ifp->name); |
8842468c | 134 | |
8842468c | 135 | if (ifp->flags & IFF_IPV4) |
136 | ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq); | |
5b73a671 | 137 | #ifdef SOLARIS_IPV6 |
8842468c | 138 | else if (ifp->flags & IFF_IPV6) |
139 | ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq); | |
5b73a671 | 140 | #endif /* SOLARIS_IPV6 */ |
8842468c | 141 | else |
142 | ret = -1; | |
8842468c | 143 | |
144 | if (ret < 0) | |
145 | return; | |
146 | ||
147 | ifp->metric = lifreq.lifr_metric; | |
148 | ||
149 | if (ifp->metric == 0) | |
150 | ifp->metric = 1; | |
151 | } | |
152 | ||
153 | /* get interface MTU */ | |
154 | void | |
155 | if_get_mtu (struct interface *ifp) | |
156 | { | |
157 | struct lifreq lifreq; | |
158 | int ret; | |
159 | ||
8842468c | 160 | if (ifp->flags & IFF_IPV4) |
161 | { | |
5c78b3d0 | 162 | lifreq_set_name (&lifreq, ifp->name); |
8842468c | 163 | ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq); |
164 | if (ret < 0) | |
165 | { | |
166 | zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)", | |
167 | ifp->name); | |
168 | ifp->mtu = -1; | |
169 | } | |
170 | else | |
171 | { | |
172 | ifp->mtu = lifreq.lifr_metric; | |
173 | } | |
174 | } | |
175 | ||
5b73a671 | 176 | #ifdef HAVE_IPV6 |
8842468c | 177 | if ((ifp->flags & IFF_IPV6) == 0) |
5b73a671 | 178 | return; |
179 | ||
180 | memset(&lifreq, 0, sizeof(lifreq)); | |
5c78b3d0 | 181 | lifreq_set_name (&lifreq, ifp->name); |
5b73a671 | 182 | |
8842468c | 183 | ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq); |
184 | if (ret < 0) | |
185 | { | |
186 | zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name); | |
187 | ifp->mtu6 = -1; | |
188 | } | |
189 | else | |
190 | { | |
191 | ifp->mtu6 = lifreq.lifr_metric; | |
192 | } | |
5b73a671 | 193 | #endif /* HAVE_IPV6 */ |
8842468c | 194 | } |
195 | ||
196 | /* Set up interface's address, netmask (and broadcast? ). | |
197 | Solaris uses ifname:number semantics to set IP address aliases. */ | |
198 | int | |
199 | if_set_prefix (struct interface *ifp, struct connected *ifc) | |
200 | { | |
201 | int ret; | |
202 | struct ifreq ifreq; | |
203 | struct sockaddr_in addr; | |
204 | struct sockaddr_in broad; | |
205 | struct sockaddr_in mask; | |
206 | struct prefix_ipv4 ifaddr; | |
207 | struct prefix_ipv4 *p; | |
208 | ||
209 | p = (struct prefix_ipv4 *) ifc->address; | |
210 | ||
211 | ifaddr = *p; | |
212 | ||
213 | strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ); | |
214 | ||
215 | addr.sin_addr = p->prefix; | |
216 | addr.sin_family = p->family; | |
217 | memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in)); | |
218 | ||
219 | ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq); | |
220 | ||
221 | if (ret < 0) | |
222 | return ret; | |
223 | ||
224 | /* We need mask for make broadcast addr. */ | |
225 | masklen2ip (p->prefixlen, &mask.sin_addr); | |
226 | ||
227 | if (if_is_broadcast (ifp)) | |
228 | { | |
229 | apply_mask_ipv4 (&ifaddr); | |
230 | addr.sin_addr = ifaddr.prefix; | |
231 | ||
232 | broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr); | |
233 | broad.sin_family = p->family; | |
234 | ||
235 | memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in)); | |
236 | ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq); | |
237 | if (ret < 0) | |
48a46fa0 | 238 | return ret; |
8842468c | 239 | } |
240 | ||
241 | mask.sin_family = p->family; | |
242 | #ifdef SUNOS_5 | |
243 | memcpy (&mask, &ifreq.ifr_addr, sizeof (mask)); | |
244 | #else | |
245 | memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in)); | |
48a46fa0 | 246 | #endif /* SUNOS_5 */ |
8842468c | 247 | ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq); |
248 | ||
249 | return ((ret < 0) ? ret : 0); | |
250 | } | |
251 | ||
252 | /* Set up interface's address, netmask (and broadcast). | |
253 | Solaris uses ifname:number semantics to set IP address aliases. */ | |
254 | int | |
255 | if_unset_prefix (struct interface *ifp, struct connected *ifc) | |
256 | { | |
257 | int ret; | |
258 | struct ifreq ifreq; | |
259 | struct sockaddr_in addr; | |
260 | struct prefix_ipv4 *p; | |
261 | ||
262 | p = (struct prefix_ipv4 *) ifc->address; | |
263 | ||
264 | strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ); | |
265 | ||
266 | memset (&addr, 0, sizeof (struct sockaddr_in)); | |
267 | addr.sin_family = p->family; | |
268 | memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in)); | |
269 | ||
270 | ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq); | |
271 | ||
272 | if (ret < 0) | |
273 | return ret; | |
274 | ||
275 | return 0; | |
276 | } | |
277 | ||
5c78b3d0 | 278 | /* Get just the flags for the given name. |
279 | * Used by the normal 'if_get_flags' function, as well | |
280 | * as the bootup interface-list code, which has to peek at per-address | |
281 | * flags in order to figure out which ones should be ignored.. | |
0752ef0b | 282 | */ |
5c78b3d0 | 283 | int |
284 | if_get_flags_direct (const char *ifname, uint64_t *flags, unsigned int af) | |
0752ef0b | 285 | { |
5c78b3d0 | 286 | struct lifreq lifreq; |
287 | int ret; | |
288 | ||
289 | lifreq_set_name (&lifreq, ifname); | |
290 | ||
291 | ret = AF_IOCTL (af, SIOCGLIFFLAGS, (caddr_t) &lifreq); | |
292 | ||
293 | if (ret) | |
294 | zlog_debug ("%s: ifname %s, error %s (%d)", | |
295 | __func__, ifname, safe_strerror (errno), errno); | |
296 | ||
297 | *flags = lifreq.lifr_flags; | |
298 | ||
299 | return ret; | |
0752ef0b | 300 | } |
301 | ||
8842468c | 302 | /* get interface flags */ |
303 | void | |
304 | if_get_flags (struct interface *ifp) | |
305 | { | |
0752ef0b | 306 | int ret4, ret6; |
5c78b3d0 | 307 | uint64_t newflags = 0; |
308 | uint64_t tmpflags; | |
8842468c | 309 | |
310 | if (ifp->flags & IFF_IPV4) | |
311 | { | |
5c78b3d0 | 312 | ret4 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET); |
0752ef0b | 313 | |
314 | if (!ret4) | |
5c78b3d0 | 315 | newflags |= tmpflags; |
316 | else if (errno == ENXIO) | |
317 | { | |
318 | /* it's gone */ | |
319 | UNSET_FLAG (ifp->flags, IFF_UP); | |
320 | if_flags_update (ifp, ifp->flags); | |
321 | } | |
8842468c | 322 | } |
323 | ||
324 | if (ifp->flags & IFF_IPV6) | |
325 | { | |
5c78b3d0 | 326 | ret6 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET6); |
0752ef0b | 327 | |
328 | if (!ret6) | |
5c78b3d0 | 329 | newflags |= tmpflags; |
330 | else if (errno == ENXIO) | |
331 | { | |
332 | /* it's gone */ | |
333 | UNSET_FLAG (ifp->flags, IFF_UP); | |
334 | if_flags_update (ifp, ifp->flags); | |
335 | } | |
8842468c | 336 | } |
0752ef0b | 337 | |
338 | /* only update flags if one of above succeeded */ | |
339 | if ( !(ret4 && ret6) ) | |
5c78b3d0 | 340 | if_flags_update (ifp, newflags); |
8842468c | 341 | } |
342 | ||
343 | /* Set interface flags */ | |
344 | int | |
5c78b3d0 | 345 | if_set_flags (struct interface *ifp, uint64_t flags) |
8842468c | 346 | { |
347 | int ret; | |
348 | struct lifreq lifreq; | |
349 | ||
5c78b3d0 | 350 | lifreq_set_name (&lifreq, ifp->name); |
8842468c | 351 | |
352 | lifreq.lifr_flags = ifp->flags; | |
353 | lifreq.lifr_flags |= flags; | |
354 | ||
8842468c | 355 | if (ifp->flags & IFF_IPV4) |
356 | ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq); | |
357 | else if (ifp->flags & IFF_IPV6) | |
358 | ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq); | |
359 | else | |
360 | ret = -1; | |
361 | ||
362 | if (ret < 0) | |
363 | zlog_info ("can't set interface flags on %s: %s", ifp->name, | |
6099b3b5 | 364 | safe_strerror (errno)); |
8842468c | 365 | else |
366 | ret = 0; | |
48a46fa0 | 367 | |
368 | return ret; | |
8842468c | 369 | } |
370 | ||
371 | /* Unset interface's flag. */ | |
372 | int | |
5c78b3d0 | 373 | if_unset_flags (struct interface *ifp, uint64_t flags) |
8842468c | 374 | { |
375 | int ret; | |
376 | struct lifreq lifreq; | |
377 | ||
5c78b3d0 | 378 | lifreq_set_name (&lifreq, ifp->name); |
8842468c | 379 | |
380 | lifreq.lifr_flags = ifp->flags; | |
381 | lifreq.lifr_flags &= ~flags; | |
382 | ||
8842468c | 383 | if (ifp->flags & IFF_IPV4) |
384 | ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq); | |
385 | else if (ifp->flags & IFF_IPV6) | |
386 | ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq); | |
387 | else | |
388 | ret = -1; | |
389 | ||
390 | if (ret < 0) | |
391 | zlog_info ("can't unset interface flags"); | |
392 | else | |
393 | ret = 0; | |
394 | ||
48a46fa0 | 395 | return ret; |
8842468c | 396 | } |
397 | ||
398 | #ifdef HAVE_IPV6 | |
399 | ||
400 | /* Interface's address add/delete functions. */ | |
401 | int | |
402 | if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc) | |
403 | { | |
404 | char addrbuf[INET_ADDRSTRLEN]; | |
405 | ||
406 | inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix), | |
407 | addrbuf, sizeof (addrbuf)); | |
408 | zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name); | |
409 | ||
410 | return 0; | |
411 | ||
412 | } | |
413 | ||
414 | int | |
415 | if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc) | |
416 | { | |
417 | char addrbuf[INET_ADDRSTRLEN]; | |
418 | ||
419 | inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix), | |
420 | addrbuf, sizeof (addrbuf)); | |
421 | zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name); | |
422 | ||
423 | return 0; | |
424 | ||
425 | } | |
426 | ||
427 | #endif /* HAVE_IPV6 */ |