blob: ccb927accf701d9c69344405d8d754688c8c12d5 [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
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000158 /* propogate */
159 zebra_interface_up_update(ifp);
160
paul718e3742002-12-13 20:15:29 +0000161#else
162 zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
paul44145db2004-05-09 11:00:23 +0000163 ifp->mtu6 = ifp->mtu = -1;
paul718e3742002-12-13 20:15:29 +0000164#endif
165}
166
167#ifdef HAVE_NETLINK
168/* Interface address setting via netlink interface. */
169int
170if_set_prefix (struct interface *ifp, struct connected *ifc)
171{
172 return kernel_address_add_ipv4 (ifp, ifc);
173}
174
175/* Interface address is removed using netlink interface. */
176int
177if_unset_prefix (struct interface *ifp, struct connected *ifc)
178{
179 return kernel_address_delete_ipv4 (ifp, ifc);
180}
181#else /* ! HAVE_NETLINK */
182#ifdef HAVE_IFALIASREQ
183/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
184 has ifaliasreq structure. */
185int
186if_set_prefix (struct interface *ifp, struct connected *ifc)
187{
188 int ret;
189 struct ifaliasreq addreq;
190 struct sockaddr_in addr;
191 struct sockaddr_in mask;
192 struct prefix_ipv4 *p;
193
194 p = (struct prefix_ipv4 *) ifc->address;
195
196 memset (&addreq, 0, sizeof addreq);
197 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
198
199 memset (&addr, 0, sizeof (struct sockaddr_in));
200 addr.sin_addr = p->prefix;
201 addr.sin_family = p->family;
202#ifdef HAVE_SIN_LEN
203 addr.sin_len = sizeof (struct sockaddr_in);
204#endif
205 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
206
207 memset (&mask, 0, sizeof (struct sockaddr_in));
208 masklen2ip (p->prefixlen, &mask.sin_addr);
209 mask.sin_family = p->family;
210#ifdef HAVE_SIN_LEN
211 mask.sin_len = sizeof (struct sockaddr_in);
212#endif
213 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
214
215 ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
216 if (ret < 0)
217 return ret;
218 return 0;
219}
220
221/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
222 has ifaliasreq structure. */
223int
224if_unset_prefix (struct interface *ifp, struct connected *ifc)
225{
226 int ret;
227 struct ifaliasreq addreq;
228 struct sockaddr_in addr;
229 struct sockaddr_in mask;
230 struct prefix_ipv4 *p;
231
232 p = (struct prefix_ipv4 *)ifc->address;
233
234 memset (&addreq, 0, sizeof addreq);
235 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
236
237 memset (&addr, 0, sizeof (struct sockaddr_in));
238 addr.sin_addr = p->prefix;
239 addr.sin_family = p->family;
240#ifdef HAVE_SIN_LEN
241 addr.sin_len = sizeof (struct sockaddr_in);
242#endif
243 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
244
245 memset (&mask, 0, sizeof (struct sockaddr_in));
246 masklen2ip (p->prefixlen, &mask.sin_addr);
247 mask.sin_family = p->family;
248#ifdef HAVE_SIN_LEN
249 mask.sin_len = sizeof (struct sockaddr_in);
250#endif
251 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
252
253 ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
254 if (ret < 0)
255 return ret;
256 return 0;
257}
258#else
259/* Set up interface's address, netmask (and broadcas? ). Linux or
260 Solaris uses ifname:number semantics to set IP address aliases. */
261int
262if_set_prefix (struct interface *ifp, struct connected *ifc)
263{
264 int ret;
265 struct ifreq ifreq;
266 struct sockaddr_in addr;
267 struct sockaddr_in broad;
268 struct sockaddr_in mask;
269 struct prefix_ipv4 ifaddr;
270 struct prefix_ipv4 *p;
271
272 p = (struct prefix_ipv4 *) ifc->address;
273
274 ifaddr = *p;
275
276 ifreq_set_name (&ifreq, ifp);
277
278 addr.sin_addr = p->prefix;
279 addr.sin_family = p->family;
280 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
281 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
282 if (ret < 0)
283 return ret;
284
285 /* We need mask for make broadcast addr. */
286 masklen2ip (p->prefixlen, &mask.sin_addr);
287
288 if (if_is_broadcast (ifp))
289 {
290 apply_mask_ipv4 (&ifaddr);
291 addr.sin_addr = ifaddr.prefix;
292
293 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
294 broad.sin_family = p->family;
295
296 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
297 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
298 if (ret < 0)
299 return ret;
300 }
301
302 mask.sin_family = p->family;
303#ifdef SUNOS_5
304 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
305#else
306 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
307#endif /* SUNOS5 */
308 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
309 if (ret < 0)
310 return ret;
311
312 return 0;
313}
314
315/* Set up interface's address, netmask (and broadcas? ). Linux or
316 Solaris uses ifname:number semantics to set IP address aliases. */
317int
318if_unset_prefix (struct interface *ifp, struct connected *ifc)
319{
320 int ret;
321 struct ifreq ifreq;
322 struct sockaddr_in addr;
323 struct prefix_ipv4 *p;
324
325 p = (struct prefix_ipv4 *) ifc->address;
326
327 ifreq_set_name (&ifreq, ifp);
328
329 memset (&addr, 0, sizeof (struct sockaddr_in));
330 addr.sin_family = p->family;
331 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
332 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
333 if (ret < 0)
334 return ret;
335
336 return 0;
337}
338#endif /* HAVE_IFALIASREQ */
339#endif /* HAVE_NETLINK */
340
341/* get interface flags */
342void
343if_get_flags (struct interface *ifp)
344{
345 int ret;
346 struct ifreq ifreq;
347
348 ifreq_set_name (&ifreq, ifp);
349
350 ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
351 if (ret < 0)
352 {
ajs6a52d0d2005-01-30 18:49:28 +0000353 zlog_err("if_ioctl(SIOCGIFFLAGS) failed: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000354 return;
355 }
356
paul5c78b3d2006-01-25 04:31:40 +0000357 if_flags_update (ifp, (ifreq.ifr_flags & 0x0000ffff));
paul718e3742002-12-13 20:15:29 +0000358}
359
360/* Set interface flags */
361int
paul5c78b3d2006-01-25 04:31:40 +0000362if_set_flags (struct interface *ifp, uint64_t flags)
paul718e3742002-12-13 20:15:29 +0000363{
364 int ret;
365 struct ifreq ifreq;
366
pauld1724b62003-10-22 02:41:52 +0000367 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000368 ifreq_set_name (&ifreq, ifp);
369
370 ifreq.ifr_flags = ifp->flags;
371 ifreq.ifr_flags |= flags;
372
373 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
374
375 if (ret < 0)
376 {
377 zlog_info ("can't set interface flags");
378 return ret;
379 }
380 return 0;
381}
382
383/* Unset interface's flag. */
384int
paul5c78b3d2006-01-25 04:31:40 +0000385if_unset_flags (struct interface *ifp, uint64_t flags)
paul718e3742002-12-13 20:15:29 +0000386{
387 int ret;
388 struct ifreq ifreq;
389
pauld1724b62003-10-22 02:41:52 +0000390 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000391 ifreq_set_name (&ifreq, ifp);
392
393 ifreq.ifr_flags = ifp->flags;
394 ifreq.ifr_flags &= ~flags;
395
396 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
397
398 if (ret < 0)
399 {
400 zlog_info ("can't unset interface flags");
401 return ret;
402 }
403 return 0;
404}
405
406#ifdef HAVE_IPV6
407
408#ifdef LINUX_IPV6
409#ifndef _LINUX_IN6_H
410/* linux/include/net/ipv6.h */
411struct in6_ifreq
412{
413 struct in6_addr ifr6_addr;
414 u_int32_t ifr6_prefixlen;
415 int ifr6_ifindex;
416};
417#endif /* _LINUX_IN6_H */
418
419/* Interface's address add/delete functions. */
420int
421if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
422{
423 int ret;
424 struct prefix_ipv6 *p;
425 struct in6_ifreq ifreq;
426
427 p = (struct prefix_ipv6 *) ifc->address;
428
429 memset (&ifreq, 0, sizeof (struct in6_ifreq));
430
431 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
432 ifreq.ifr6_ifindex = ifp->ifindex;
433 ifreq.ifr6_prefixlen = p->prefixlen;
434
435 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
436
437 return ret;
438}
439
440int
441if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
442{
443 int ret;
444 struct prefix_ipv6 *p;
445 struct in6_ifreq ifreq;
446
447 p = (struct prefix_ipv6 *) ifc->address;
448
449 memset (&ifreq, 0, sizeof (struct in6_ifreq));
450
451 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
452 ifreq.ifr6_ifindex = ifp->ifindex;
453 ifreq.ifr6_prefixlen = p->prefixlen;
454
455 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
456
457 return ret;
458}
459#else /* LINUX_IPV6 */
460#ifdef HAVE_IN6_ALIASREQ
461#ifndef ND6_INFINITE_LIFETIME
462#define ND6_INFINITE_LIFETIME 0xffffffffL
463#endif /* ND6_INFINITE_LIFETIME */
464int
465if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
466{
467 int ret;
468 struct in6_aliasreq addreq;
469 struct sockaddr_in6 addr;
470 struct sockaddr_in6 mask;
471 struct prefix_ipv6 *p;
472
473 p = (struct prefix_ipv6 * ) ifc->address;
474
475 memset (&addreq, 0, sizeof addreq);
476 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
477
478 memset (&addr, 0, sizeof (struct sockaddr_in6));
479 addr.sin6_addr = p->prefix;
480 addr.sin6_family = p->family;
481#ifdef HAVE_SIN_LEN
482 addr.sin6_len = sizeof (struct sockaddr_in6);
483#endif
484 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
485
486 memset (&mask, 0, sizeof (struct sockaddr_in6));
487 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
488 mask.sin6_family = p->family;
489#ifdef HAVE_SIN_LEN
490 mask.sin6_len = sizeof (struct sockaddr_in6);
491#endif
492 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
paul0e4f1902003-06-12 02:13:45 +0000493
494 addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
495 addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
paul718e3742002-12-13 20:15:29 +0000496
hasso726f9b22003-05-25 21:04:54 +0000497#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000498 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
499 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000500#endif
paul718e3742002-12-13 20:15:29 +0000501
502 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
503 if (ret < 0)
504 return ret;
505 return 0;
506}
507
508int
509if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
510{
511 int ret;
512 struct in6_aliasreq addreq;
513 struct sockaddr_in6 addr;
514 struct sockaddr_in6 mask;
515 struct prefix_ipv6 *p;
516
517 p = (struct prefix_ipv6 *) ifc->address;
518
519 memset (&addreq, 0, sizeof addreq);
520 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
521
522 memset (&addr, 0, sizeof (struct sockaddr_in6));
523 addr.sin6_addr = p->prefix;
524 addr.sin6_family = p->family;
525#ifdef HAVE_SIN_LEN
526 addr.sin6_len = sizeof (struct sockaddr_in6);
527#endif
528 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
529
530 memset (&mask, 0, sizeof (struct sockaddr_in6));
531 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
532 mask.sin6_family = p->family;
533#ifdef HAVE_SIN_LEN
534 mask.sin6_len = sizeof (struct sockaddr_in6);
535#endif
536 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
537
hasso726f9b22003-05-25 21:04:54 +0000538#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000539 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
540 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000541#endif
paul718e3742002-12-13 20:15:29 +0000542
543 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
544 if (ret < 0)
545 return ret;
546 return 0;
547}
548#else
549int
550if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
551{
552 return 0;
553}
554
555int
556if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
557{
558 return 0;
559}
560#endif /* HAVE_IN6_ALIASREQ */
561
562#endif /* LINUX_IPV6 */
563
564#endif /* HAVE_IPV6 */