blob: 06be5bec9987ca46ee919010834104ea27dd1f1c [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Common ioctl functions.
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"
pauledd7c242003-06-04 13:59:38 +000030#include "privs.h"
paul718e3742002-12-13 20:15:29 +000031
32#include "zebra/rib.h"
33#include "zebra/rt.h"
paul5c78b3d2006-01-25 04:31:40 +000034#include "zebra/interface.h"
paul718e3742002-12-13 20:15:29 +000035
pauledd7c242003-06-04 13:59:38 +000036extern struct zebra_privs_t zserv_privs;
37
paul718e3742002-12-13 20:15:29 +000038/* clear and set interface name string */
39void
40ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
41{
42 strncpy (ifreq->ifr_name, ifp->name, IFNAMSIZ);
43}
44
45/* call ioctl system call */
46int
47if_ioctl (u_long request, caddr_t buffer)
48{
49 int sock;
ajs4460e7a2005-01-29 17:07:40 +000050 int ret;
51 int err;
paul718e3742002-12-13 20:15:29 +000052
pauledd7c242003-06-04 13:59:38 +000053 if (zserv_privs.change(ZPRIVS_RAISE))
54 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +000055 sock = socket (AF_INET, SOCK_DGRAM, 0);
56 if (sock < 0)
57 {
ajs6a52d0d2005-01-30 18:49:28 +000058 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +000059 if (zserv_privs.change(ZPRIVS_LOWER))
60 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000061 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
paul718e3742002-12-13 20:15:29 +000062 exit (1);
63 }
ajs4460e7a2005-01-29 17:07:40 +000064 if ((ret = ioctl (sock, request, buffer)) < 0)
65 err = errno;
pauledd7c242003-06-04 13:59:38 +000066 if (zserv_privs.change(ZPRIVS_LOWER))
67 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +000068 close (sock);
69
70 if (ret < 0)
71 {
72 errno = err;
73 return ret;
74 }
75 return 0;
76}
77
78#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +000079static int
paul718e3742002-12-13 20:15:29 +000080if_ioctl_ipv6 (u_long request, caddr_t buffer)
81{
82 int sock;
ajs4460e7a2005-01-29 17:07:40 +000083 int ret;
84 int err;
paul718e3742002-12-13 20:15:29 +000085
pauledd7c242003-06-04 13:59:38 +000086 if (zserv_privs.change(ZPRIVS_RAISE))
87 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +000088 sock = socket (AF_INET6, SOCK_DGRAM, 0);
89 if (sock < 0)
90 {
ajs6a52d0d2005-01-30 18:49:28 +000091 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +000092 if (zserv_privs.change(ZPRIVS_LOWER))
93 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000094 zlog_err("Cannot create IPv6 datagram socket: %s",
95 safe_strerror(save_errno));
paul718e3742002-12-13 20:15:29 +000096 exit (1);
97 }
98
ajs4460e7a2005-01-29 17:07:40 +000099 if ((ret = ioctl (sock, request, buffer)) < 0)
100 err = errno;
pauledd7c242003-06-04 13:59:38 +0000101 if (zserv_privs.change(ZPRIVS_LOWER))
102 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000103 close (sock);
104
105 if (ret < 0)
106 {
107 errno = err;
108 return ret;
109 }
110 return 0;
111}
112#endif /* HAVE_IPV6 */
113
114/*
115 * get interface metric
116 * -- if value is not avaliable set -1
117 */
118void
119if_get_metric (struct interface *ifp)
120{
121#ifdef SIOCGIFMETRIC
122 struct ifreq ifreq;
123
124 ifreq_set_name (&ifreq, ifp);
125
126 if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
127 return;
128 ifp->metric = ifreq.ifr_metric;
129 if (ifp->metric == 0)
130 ifp->metric = 1;
131#else /* SIOCGIFMETRIC */
132 ifp->metric = -1;
133#endif /* SIOCGIFMETRIC */
134}
135
136/* get interface MTU */
137void
138if_get_mtu (struct interface *ifp)
139{
140 struct ifreq ifreq;
141
142 ifreq_set_name (&ifreq, ifp);
143
144#if defined(SIOCGIFMTU)
145 if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
146 {
147 zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
paul44145db2004-05-09 11:00:23 +0000148 ifp->mtu6 = ifp->mtu = -1;
paul718e3742002-12-13 20:15:29 +0000149 return;
150 }
151
152#ifdef SUNOS_5
paul44145db2004-05-09 11:00:23 +0000153 ifp->mtu6 = ifp->mtu = ifreq.ifr_metric;
paul718e3742002-12-13 20:15:29 +0000154#else
paul44145db2004-05-09 11:00:23 +0000155 ifp->mtu6 = ifp->mtu = ifreq.ifr_mtu;
paul718e3742002-12-13 20:15:29 +0000156#endif /* SUNOS_5 */
157
158#else
159 zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
paul44145db2004-05-09 11:00:23 +0000160 ifp->mtu6 = ifp->mtu = -1;
paul718e3742002-12-13 20:15:29 +0000161#endif
162}
163
164#ifdef HAVE_NETLINK
165/* Interface address setting via netlink interface. */
166int
167if_set_prefix (struct interface *ifp, struct connected *ifc)
168{
169 return kernel_address_add_ipv4 (ifp, ifc);
170}
171
172/* Interface address is removed using netlink interface. */
173int
174if_unset_prefix (struct interface *ifp, struct connected *ifc)
175{
176 return kernel_address_delete_ipv4 (ifp, ifc);
177}
178#else /* ! HAVE_NETLINK */
179#ifdef HAVE_IFALIASREQ
180/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
181 has ifaliasreq structure. */
182int
183if_set_prefix (struct interface *ifp, struct connected *ifc)
184{
185 int ret;
186 struct ifaliasreq addreq;
187 struct sockaddr_in addr;
188 struct sockaddr_in mask;
189 struct prefix_ipv4 *p;
190
191 p = (struct prefix_ipv4 *) ifc->address;
192
193 memset (&addreq, 0, sizeof addreq);
194 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
195
196 memset (&addr, 0, sizeof (struct sockaddr_in));
197 addr.sin_addr = p->prefix;
198 addr.sin_family = p->family;
199#ifdef HAVE_SIN_LEN
200 addr.sin_len = sizeof (struct sockaddr_in);
201#endif
202 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
203
204 memset (&mask, 0, sizeof (struct sockaddr_in));
205 masklen2ip (p->prefixlen, &mask.sin_addr);
206 mask.sin_family = p->family;
207#ifdef HAVE_SIN_LEN
208 mask.sin_len = sizeof (struct sockaddr_in);
209#endif
210 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
211
212 ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
213 if (ret < 0)
214 return ret;
215 return 0;
216}
217
218/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
219 has ifaliasreq structure. */
220int
221if_unset_prefix (struct interface *ifp, struct connected *ifc)
222{
223 int ret;
224 struct ifaliasreq addreq;
225 struct sockaddr_in addr;
226 struct sockaddr_in mask;
227 struct prefix_ipv4 *p;
228
229 p = (struct prefix_ipv4 *)ifc->address;
230
231 memset (&addreq, 0, sizeof addreq);
232 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
233
234 memset (&addr, 0, sizeof (struct sockaddr_in));
235 addr.sin_addr = p->prefix;
236 addr.sin_family = p->family;
237#ifdef HAVE_SIN_LEN
238 addr.sin_len = sizeof (struct sockaddr_in);
239#endif
240 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
241
242 memset (&mask, 0, sizeof (struct sockaddr_in));
243 masklen2ip (p->prefixlen, &mask.sin_addr);
244 mask.sin_family = p->family;
245#ifdef HAVE_SIN_LEN
246 mask.sin_len = sizeof (struct sockaddr_in);
247#endif
248 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
249
250 ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
251 if (ret < 0)
252 return ret;
253 return 0;
254}
255#else
256/* Set up interface's address, netmask (and broadcas? ). Linux or
257 Solaris uses ifname:number semantics to set IP address aliases. */
258int
259if_set_prefix (struct interface *ifp, struct connected *ifc)
260{
261 int ret;
262 struct ifreq ifreq;
263 struct sockaddr_in addr;
264 struct sockaddr_in broad;
265 struct sockaddr_in mask;
266 struct prefix_ipv4 ifaddr;
267 struct prefix_ipv4 *p;
268
269 p = (struct prefix_ipv4 *) ifc->address;
270
271 ifaddr = *p;
272
273 ifreq_set_name (&ifreq, ifp);
274
275 addr.sin_addr = p->prefix;
276 addr.sin_family = p->family;
277 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
278 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
279 if (ret < 0)
280 return ret;
281
282 /* We need mask for make broadcast addr. */
283 masklen2ip (p->prefixlen, &mask.sin_addr);
284
285 if (if_is_broadcast (ifp))
286 {
287 apply_mask_ipv4 (&ifaddr);
288 addr.sin_addr = ifaddr.prefix;
289
290 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
291 broad.sin_family = p->family;
292
293 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
294 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
295 if (ret < 0)
296 return ret;
297 }
298
299 mask.sin_family = p->family;
300#ifdef SUNOS_5
301 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
302#else
303 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
304#endif /* SUNOS5 */
305 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
306 if (ret < 0)
307 return ret;
308
309 return 0;
310}
311
312/* Set up interface's address, netmask (and broadcas? ). Linux or
313 Solaris uses ifname:number semantics to set IP address aliases. */
314int
315if_unset_prefix (struct interface *ifp, struct connected *ifc)
316{
317 int ret;
318 struct ifreq ifreq;
319 struct sockaddr_in addr;
320 struct prefix_ipv4 *p;
321
322 p = (struct prefix_ipv4 *) ifc->address;
323
324 ifreq_set_name (&ifreq, ifp);
325
326 memset (&addr, 0, sizeof (struct sockaddr_in));
327 addr.sin_family = p->family;
328 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
329 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
330 if (ret < 0)
331 return ret;
332
333 return 0;
334}
335#endif /* HAVE_IFALIASREQ */
336#endif /* HAVE_NETLINK */
337
338/* get interface flags */
339void
340if_get_flags (struct interface *ifp)
341{
342 int ret;
343 struct ifreq ifreq;
344
345 ifreq_set_name (&ifreq, ifp);
346
347 ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
348 if (ret < 0)
349 {
ajs6a52d0d2005-01-30 18:49:28 +0000350 zlog_err("if_ioctl(SIOCGIFFLAGS) failed: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000351 return;
352 }
353
paul5c78b3d2006-01-25 04:31:40 +0000354 if_flags_update (ifp, (ifreq.ifr_flags & 0x0000ffff));
paul718e3742002-12-13 20:15:29 +0000355}
356
357/* Set interface flags */
358int
paul5c78b3d2006-01-25 04:31:40 +0000359if_set_flags (struct interface *ifp, uint64_t flags)
paul718e3742002-12-13 20:15:29 +0000360{
361 int ret;
362 struct ifreq ifreq;
363
pauld1724b62003-10-22 02:41:52 +0000364 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000365 ifreq_set_name (&ifreq, ifp);
366
367 ifreq.ifr_flags = ifp->flags;
368 ifreq.ifr_flags |= flags;
369
370 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
371
372 if (ret < 0)
373 {
374 zlog_info ("can't set interface flags");
375 return ret;
376 }
377 return 0;
378}
379
380/* Unset interface's flag. */
381int
paul5c78b3d2006-01-25 04:31:40 +0000382if_unset_flags (struct interface *ifp, uint64_t flags)
paul718e3742002-12-13 20:15:29 +0000383{
384 int ret;
385 struct ifreq ifreq;
386
pauld1724b62003-10-22 02:41:52 +0000387 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000388 ifreq_set_name (&ifreq, ifp);
389
390 ifreq.ifr_flags = ifp->flags;
391 ifreq.ifr_flags &= ~flags;
392
393 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
394
395 if (ret < 0)
396 {
397 zlog_info ("can't unset interface flags");
398 return ret;
399 }
400 return 0;
401}
402
403#ifdef HAVE_IPV6
404
405#ifdef LINUX_IPV6
406#ifndef _LINUX_IN6_H
407/* linux/include/net/ipv6.h */
408struct in6_ifreq
409{
410 struct in6_addr ifr6_addr;
411 u_int32_t ifr6_prefixlen;
412 int ifr6_ifindex;
413};
414#endif /* _LINUX_IN6_H */
415
416/* Interface's address add/delete functions. */
417int
418if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
419{
420 int ret;
421 struct prefix_ipv6 *p;
422 struct in6_ifreq ifreq;
423
424 p = (struct prefix_ipv6 *) ifc->address;
425
426 memset (&ifreq, 0, sizeof (struct in6_ifreq));
427
428 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
429 ifreq.ifr6_ifindex = ifp->ifindex;
430 ifreq.ifr6_prefixlen = p->prefixlen;
431
432 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
433
434 return ret;
435}
436
437int
438if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
439{
440 int ret;
441 struct prefix_ipv6 *p;
442 struct in6_ifreq ifreq;
443
444 p = (struct prefix_ipv6 *) ifc->address;
445
446 memset (&ifreq, 0, sizeof (struct in6_ifreq));
447
448 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
449 ifreq.ifr6_ifindex = ifp->ifindex;
450 ifreq.ifr6_prefixlen = p->prefixlen;
451
452 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
453
454 return ret;
455}
456#else /* LINUX_IPV6 */
457#ifdef HAVE_IN6_ALIASREQ
458#ifndef ND6_INFINITE_LIFETIME
459#define ND6_INFINITE_LIFETIME 0xffffffffL
460#endif /* ND6_INFINITE_LIFETIME */
461int
462if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
463{
464 int ret;
465 struct in6_aliasreq addreq;
466 struct sockaddr_in6 addr;
467 struct sockaddr_in6 mask;
468 struct prefix_ipv6 *p;
469
470 p = (struct prefix_ipv6 * ) ifc->address;
471
472 memset (&addreq, 0, sizeof addreq);
473 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
474
475 memset (&addr, 0, sizeof (struct sockaddr_in6));
476 addr.sin6_addr = p->prefix;
477 addr.sin6_family = p->family;
478#ifdef HAVE_SIN_LEN
479 addr.sin6_len = sizeof (struct sockaddr_in6);
480#endif
481 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
482
483 memset (&mask, 0, sizeof (struct sockaddr_in6));
484 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
485 mask.sin6_family = p->family;
486#ifdef HAVE_SIN_LEN
487 mask.sin6_len = sizeof (struct sockaddr_in6);
488#endif
489 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
paul0e4f1902003-06-12 02:13:45 +0000490
491 addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
492 addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
paul718e3742002-12-13 20:15:29 +0000493
hasso726f9b22003-05-25 21:04:54 +0000494#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000495 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
496 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000497#endif
paul718e3742002-12-13 20:15:29 +0000498
499 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
500 if (ret < 0)
501 return ret;
502 return 0;
503}
504
505int
506if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
507{
508 int ret;
509 struct in6_aliasreq addreq;
510 struct sockaddr_in6 addr;
511 struct sockaddr_in6 mask;
512 struct prefix_ipv6 *p;
513
514 p = (struct prefix_ipv6 *) ifc->address;
515
516 memset (&addreq, 0, sizeof addreq);
517 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
518
519 memset (&addr, 0, sizeof (struct sockaddr_in6));
520 addr.sin6_addr = p->prefix;
521 addr.sin6_family = p->family;
522#ifdef HAVE_SIN_LEN
523 addr.sin6_len = sizeof (struct sockaddr_in6);
524#endif
525 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
526
527 memset (&mask, 0, sizeof (struct sockaddr_in6));
528 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
529 mask.sin6_family = p->family;
530#ifdef HAVE_SIN_LEN
531 mask.sin6_len = sizeof (struct sockaddr_in6);
532#endif
533 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
534
hasso726f9b22003-05-25 21:04:54 +0000535#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000536 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
537 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000538#endif
paul718e3742002-12-13 20:15:29 +0000539
540 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
541 if (ret < 0)
542 return ret;
543 return 0;
544}
545#else
546int
547if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
548{
549 return 0;
550}
551
552int
553if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
554{
555 return 0;
556}
557#endif /* HAVE_IN6_ALIASREQ */
558
559#endif /* LINUX_IPV6 */
560
561#endif /* HAVE_IPV6 */