blob: 3e5d1d2fbb75fbc02e8090f5671419f013cb3475 [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"
30
31#include "zebra/rib.h"
32#include "zebra/rt.h"
33
34/* clear and set interface name string */
35void
36ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
37{
38 strncpy (ifreq->ifr_name, ifp->name, IFNAMSIZ);
39}
40
41/* call ioctl system call */
42int
43if_ioctl (u_long request, caddr_t buffer)
44{
45 int sock;
46 int ret = 0;
47 int err = 0;
48
49 sock = socket (AF_INET, SOCK_DGRAM, 0);
50 if (sock < 0)
51 {
52 perror ("socket");
53 exit (1);
54 }
55
56 ret = ioctl (sock, request, buffer);
57 if (ret < 0)
58 {
59 err = errno;
60 }
61 close (sock);
62
63 if (ret < 0)
64 {
65 errno = err;
66 return ret;
67 }
68 return 0;
69}
70
71#ifdef HAVE_IPV6
72int
73if_ioctl_ipv6 (u_long request, caddr_t buffer)
74{
75 int sock;
76 int ret = 0;
77 int err = 0;
78
79 sock = socket (AF_INET6, SOCK_DGRAM, 0);
80 if (sock < 0)
81 {
82 perror ("socket");
83 exit (1);
84 }
85
86 ret = ioctl (sock, request, buffer);
87 if (ret < 0)
88 {
89 err = errno;
90 }
91 close (sock);
92
93 if (ret < 0)
94 {
95 errno = err;
96 return ret;
97 }
98 return 0;
99}
100#endif /* HAVE_IPV6 */
101
102/*
103 * get interface metric
104 * -- if value is not avaliable set -1
105 */
106void
107if_get_metric (struct interface *ifp)
108{
109#ifdef SIOCGIFMETRIC
110 struct ifreq ifreq;
111
112 ifreq_set_name (&ifreq, ifp);
113
114 if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
115 return;
116 ifp->metric = ifreq.ifr_metric;
117 if (ifp->metric == 0)
118 ifp->metric = 1;
119#else /* SIOCGIFMETRIC */
120 ifp->metric = -1;
121#endif /* SIOCGIFMETRIC */
122}
123
124/* get interface MTU */
125void
126if_get_mtu (struct interface *ifp)
127{
128 struct ifreq ifreq;
129
130 ifreq_set_name (&ifreq, ifp);
131
132#if defined(SIOCGIFMTU)
133 if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
134 {
135 zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
136 ifp->mtu = -1;
137 return;
138 }
139
140#ifdef SUNOS_5
141 ifp->mtu = ifreq.ifr_metric;
142#else
143 ifp->mtu = ifreq.ifr_mtu;
144#endif /* SUNOS_5 */
145
146#else
147 zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
148 ifp->mtu = -1;
149#endif
150}
151
152#ifdef HAVE_NETLINK
153/* Interface address setting via netlink interface. */
154int
155if_set_prefix (struct interface *ifp, struct connected *ifc)
156{
157 return kernel_address_add_ipv4 (ifp, ifc);
158}
159
160/* Interface address is removed using netlink interface. */
161int
162if_unset_prefix (struct interface *ifp, struct connected *ifc)
163{
164 return kernel_address_delete_ipv4 (ifp, ifc);
165}
166#else /* ! HAVE_NETLINK */
167#ifdef HAVE_IFALIASREQ
168/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
169 has ifaliasreq structure. */
170int
171if_set_prefix (struct interface *ifp, struct connected *ifc)
172{
173 int ret;
174 struct ifaliasreq addreq;
175 struct sockaddr_in addr;
176 struct sockaddr_in mask;
177 struct prefix_ipv4 *p;
178
179 p = (struct prefix_ipv4 *) ifc->address;
180
181 memset (&addreq, 0, sizeof addreq);
182 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
183
184 memset (&addr, 0, sizeof (struct sockaddr_in));
185 addr.sin_addr = p->prefix;
186 addr.sin_family = p->family;
187#ifdef HAVE_SIN_LEN
188 addr.sin_len = sizeof (struct sockaddr_in);
189#endif
190 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
191
192 memset (&mask, 0, sizeof (struct sockaddr_in));
193 masklen2ip (p->prefixlen, &mask.sin_addr);
194 mask.sin_family = p->family;
195#ifdef HAVE_SIN_LEN
196 mask.sin_len = sizeof (struct sockaddr_in);
197#endif
198 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
199
200 ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
201 if (ret < 0)
202 return ret;
203 return 0;
204}
205
206/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
207 has ifaliasreq structure. */
208int
209if_unset_prefix (struct interface *ifp, struct connected *ifc)
210{
211 int ret;
212 struct ifaliasreq addreq;
213 struct sockaddr_in addr;
214 struct sockaddr_in mask;
215 struct prefix_ipv4 *p;
216
217 p = (struct prefix_ipv4 *)ifc->address;
218
219 memset (&addreq, 0, sizeof addreq);
220 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
221
222 memset (&addr, 0, sizeof (struct sockaddr_in));
223 addr.sin_addr = p->prefix;
224 addr.sin_family = p->family;
225#ifdef HAVE_SIN_LEN
226 addr.sin_len = sizeof (struct sockaddr_in);
227#endif
228 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
229
230 memset (&mask, 0, sizeof (struct sockaddr_in));
231 masklen2ip (p->prefixlen, &mask.sin_addr);
232 mask.sin_family = p->family;
233#ifdef HAVE_SIN_LEN
234 mask.sin_len = sizeof (struct sockaddr_in);
235#endif
236 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
237
238 ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
239 if (ret < 0)
240 return ret;
241 return 0;
242}
243#else
244/* Set up interface's address, netmask (and broadcas? ). Linux or
245 Solaris uses ifname:number semantics to set IP address aliases. */
246int
247if_set_prefix (struct interface *ifp, struct connected *ifc)
248{
249 int ret;
250 struct ifreq ifreq;
251 struct sockaddr_in addr;
252 struct sockaddr_in broad;
253 struct sockaddr_in mask;
254 struct prefix_ipv4 ifaddr;
255 struct prefix_ipv4 *p;
256
257 p = (struct prefix_ipv4 *) ifc->address;
258
259 ifaddr = *p;
260
261 ifreq_set_name (&ifreq, ifp);
262
263 addr.sin_addr = p->prefix;
264 addr.sin_family = p->family;
265 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
266 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
267 if (ret < 0)
268 return ret;
269
270 /* We need mask for make broadcast addr. */
271 masklen2ip (p->prefixlen, &mask.sin_addr);
272
273 if (if_is_broadcast (ifp))
274 {
275 apply_mask_ipv4 (&ifaddr);
276 addr.sin_addr = ifaddr.prefix;
277
278 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
279 broad.sin_family = p->family;
280
281 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
282 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
283 if (ret < 0)
284 return ret;
285 }
286
287 mask.sin_family = p->family;
288#ifdef SUNOS_5
289 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
290#else
291 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
292#endif /* SUNOS5 */
293 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
294 if (ret < 0)
295 return ret;
296
297 return 0;
298}
299
300/* Set up interface's address, netmask (and broadcas? ). Linux or
301 Solaris uses ifname:number semantics to set IP address aliases. */
302int
303if_unset_prefix (struct interface *ifp, struct connected *ifc)
304{
305 int ret;
306 struct ifreq ifreq;
307 struct sockaddr_in addr;
308 struct prefix_ipv4 *p;
309
310 p = (struct prefix_ipv4 *) ifc->address;
311
312 ifreq_set_name (&ifreq, ifp);
313
314 memset (&addr, 0, sizeof (struct sockaddr_in));
315 addr.sin_family = p->family;
316 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
317 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
318 if (ret < 0)
319 return ret;
320
321 return 0;
322}
323#endif /* HAVE_IFALIASREQ */
324#endif /* HAVE_NETLINK */
325
326/* get interface flags */
327void
328if_get_flags (struct interface *ifp)
329{
330 int ret;
331 struct ifreq ifreq;
332
333 ifreq_set_name (&ifreq, ifp);
334
335 ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
336 if (ret < 0)
337 {
338 perror ("ioctl");
339 return;
340 }
341
342 ifp->flags = ifreq.ifr_flags & 0x0000ffff;
343}
344
345/* Set interface flags */
346int
347if_set_flags (struct interface *ifp, unsigned long flags)
348{
349 int ret;
350 struct ifreq ifreq;
351
352 ifreq_set_name (&ifreq, ifp);
353
354 ifreq.ifr_flags = ifp->flags;
355 ifreq.ifr_flags |= flags;
356
357 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
358
359 if (ret < 0)
360 {
361 zlog_info ("can't set interface flags");
362 return ret;
363 }
364 return 0;
365}
366
367/* Unset interface's flag. */
368int
369if_unset_flags (struct interface *ifp, unsigned long flags)
370{
371 int ret;
372 struct ifreq ifreq;
373
374 ifreq_set_name (&ifreq, ifp);
375
376 ifreq.ifr_flags = ifp->flags;
377 ifreq.ifr_flags &= ~flags;
378
379 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
380
381 if (ret < 0)
382 {
383 zlog_info ("can't unset interface flags");
384 return ret;
385 }
386 return 0;
387}
388
389#ifdef HAVE_IPV6
390
391#ifdef LINUX_IPV6
392#ifndef _LINUX_IN6_H
393/* linux/include/net/ipv6.h */
394struct in6_ifreq
395{
396 struct in6_addr ifr6_addr;
397 u_int32_t ifr6_prefixlen;
398 int ifr6_ifindex;
399};
400#endif /* _LINUX_IN6_H */
401
402/* Interface's address add/delete functions. */
403int
404if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
405{
406 int ret;
407 struct prefix_ipv6 *p;
408 struct in6_ifreq ifreq;
409
410 p = (struct prefix_ipv6 *) ifc->address;
411
412 memset (&ifreq, 0, sizeof (struct in6_ifreq));
413
414 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
415 ifreq.ifr6_ifindex = ifp->ifindex;
416 ifreq.ifr6_prefixlen = p->prefixlen;
417
418 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
419
420 return ret;
421}
422
423int
424if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
425{
426 int ret;
427 struct prefix_ipv6 *p;
428 struct in6_ifreq ifreq;
429
430 p = (struct prefix_ipv6 *) ifc->address;
431
432 memset (&ifreq, 0, sizeof (struct in6_ifreq));
433
434 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
435 ifreq.ifr6_ifindex = ifp->ifindex;
436 ifreq.ifr6_prefixlen = p->prefixlen;
437
438 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
439
440 return ret;
441}
442#else /* LINUX_IPV6 */
443#ifdef HAVE_IN6_ALIASREQ
444#ifndef ND6_INFINITE_LIFETIME
445#define ND6_INFINITE_LIFETIME 0xffffffffL
446#endif /* ND6_INFINITE_LIFETIME */
447int
448if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
449{
450 int ret;
451 struct in6_aliasreq addreq;
452 struct sockaddr_in6 addr;
453 struct sockaddr_in6 mask;
454 struct prefix_ipv6 *p;
455
456 p = (struct prefix_ipv6 * ) ifc->address;
457
458 memset (&addreq, 0, sizeof addreq);
459 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
460
461 memset (&addr, 0, sizeof (struct sockaddr_in6));
462 addr.sin6_addr = p->prefix;
463 addr.sin6_family = p->family;
464#ifdef HAVE_SIN_LEN
465 addr.sin6_len = sizeof (struct sockaddr_in6);
466#endif
467 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
468
469 memset (&mask, 0, sizeof (struct sockaddr_in6));
470 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
471 mask.sin6_family = p->family;
472#ifdef HAVE_SIN_LEN
473 mask.sin6_len = sizeof (struct sockaddr_in6);
474#endif
475 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
476
hasso726f9b22003-05-25 21:04:54 +0000477#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000478 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
479 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000480#endif
paul718e3742002-12-13 20:15:29 +0000481
482 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
483 if (ret < 0)
484 return ret;
485 return 0;
486}
487
488int
489if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
490{
491 int ret;
492 struct in6_aliasreq addreq;
493 struct sockaddr_in6 addr;
494 struct sockaddr_in6 mask;
495 struct prefix_ipv6 *p;
496
497 p = (struct prefix_ipv6 *) ifc->address;
498
499 memset (&addreq, 0, sizeof addreq);
500 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
501
502 memset (&addr, 0, sizeof (struct sockaddr_in6));
503 addr.sin6_addr = p->prefix;
504 addr.sin6_family = p->family;
505#ifdef HAVE_SIN_LEN
506 addr.sin6_len = sizeof (struct sockaddr_in6);
507#endif
508 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
509
510 memset (&mask, 0, sizeof (struct sockaddr_in6));
511 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
512 mask.sin6_family = p->family;
513#ifdef HAVE_SIN_LEN
514 mask.sin6_len = sizeof (struct sockaddr_in6);
515#endif
516 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
517
hasso726f9b22003-05-25 21:04:54 +0000518#ifdef HAVE_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000519 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
520 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000521#endif
paul718e3742002-12-13 20:15:29 +0000522
523 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
524 if (ret < 0)
525 return ret;
526 return 0;
527}
528#else
529int
530if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
531{
532 return 0;
533}
534
535int
536if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
537{
538 return 0;
539}
540#endif /* HAVE_IN6_ALIASREQ */
541
542#endif /* LINUX_IPV6 */
543
544#endif /* HAVE_IPV6 */