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