blob: d536771a1147211c96e32aaa0f65a05d594e9cbf [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
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +000036#ifdef HAVE_BSD_LINK_DETECT
37#include <net/if_media.h>
38#endif /* HAVE_BSD_LINK_DETECT*/
39
pauledd7c242003-06-04 13:59:38 +000040extern struct zebra_privs_t zserv_privs;
41
paul718e3742002-12-13 20:15:29 +000042/* clear and set interface name string */
43void
44ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
45{
46 strncpy (ifreq->ifr_name, ifp->name, IFNAMSIZ);
47}
48
49/* call ioctl system call */
50int
51if_ioctl (u_long request, caddr_t buffer)
52{
53 int sock;
ajs4460e7a2005-01-29 17:07:40 +000054 int ret;
55 int err;
paul718e3742002-12-13 20:15:29 +000056
pauledd7c242003-06-04 13:59:38 +000057 if (zserv_privs.change(ZPRIVS_RAISE))
58 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +000059 sock = socket (AF_INET, SOCK_DGRAM, 0);
60 if (sock < 0)
61 {
ajs6a52d0d2005-01-30 18:49:28 +000062 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +000063 if (zserv_privs.change(ZPRIVS_LOWER))
64 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000065 zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
paul718e3742002-12-13 20:15:29 +000066 exit (1);
67 }
ajs4460e7a2005-01-29 17:07:40 +000068 if ((ret = ioctl (sock, request, buffer)) < 0)
69 err = errno;
pauledd7c242003-06-04 13:59:38 +000070 if (zserv_privs.change(ZPRIVS_LOWER))
71 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +000072 close (sock);
73
74 if (ret < 0)
75 {
76 errno = err;
77 return ret;
78 }
79 return 0;
80}
81
82#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +000083static int
paul718e3742002-12-13 20:15:29 +000084if_ioctl_ipv6 (u_long request, caddr_t buffer)
85{
86 int sock;
ajs4460e7a2005-01-29 17:07:40 +000087 int ret;
88 int err;
paul718e3742002-12-13 20:15:29 +000089
pauledd7c242003-06-04 13:59:38 +000090 if (zserv_privs.change(ZPRIVS_RAISE))
91 zlog (NULL, LOG_ERR, "Can't raise privileges");
paul718e3742002-12-13 20:15:29 +000092 sock = socket (AF_INET6, SOCK_DGRAM, 0);
93 if (sock < 0)
94 {
ajs6a52d0d2005-01-30 18:49:28 +000095 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +000096 if (zserv_privs.change(ZPRIVS_LOWER))
97 zlog (NULL, LOG_ERR, "Can't lower privileges");
ajs6a52d0d2005-01-30 18:49:28 +000098 zlog_err("Cannot create IPv6 datagram socket: %s",
99 safe_strerror(save_errno));
paul718e3742002-12-13 20:15:29 +0000100 exit (1);
101 }
102
ajs4460e7a2005-01-29 17:07:40 +0000103 if ((ret = ioctl (sock, request, buffer)) < 0)
104 err = errno;
pauledd7c242003-06-04 13:59:38 +0000105 if (zserv_privs.change(ZPRIVS_LOWER))
106 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +0000107 close (sock);
108
109 if (ret < 0)
110 {
111 errno = err;
112 return ret;
113 }
114 return 0;
115}
116#endif /* HAVE_IPV6 */
117
118/*
119 * get interface metric
120 * -- if value is not avaliable set -1
121 */
122void
123if_get_metric (struct interface *ifp)
124{
125#ifdef SIOCGIFMETRIC
126 struct ifreq ifreq;
127
128 ifreq_set_name (&ifreq, ifp);
129
130 if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
131 return;
132 ifp->metric = ifreq.ifr_metric;
133 if (ifp->metric == 0)
134 ifp->metric = 1;
135#else /* SIOCGIFMETRIC */
136 ifp->metric = -1;
137#endif /* SIOCGIFMETRIC */
138}
139
140/* get interface MTU */
141void
142if_get_mtu (struct interface *ifp)
143{
144 struct ifreq ifreq;
145
146 ifreq_set_name (&ifreq, ifp);
147
148#if defined(SIOCGIFMTU)
149 if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
150 {
151 zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
paul44145db2004-05-09 11:00:23 +0000152 ifp->mtu6 = ifp->mtu = -1;
paul718e3742002-12-13 20:15:29 +0000153 return;
154 }
155
156#ifdef SUNOS_5
paul44145db2004-05-09 11:00:23 +0000157 ifp->mtu6 = ifp->mtu = ifreq.ifr_metric;
paul718e3742002-12-13 20:15:29 +0000158#else
paul44145db2004-05-09 11:00:23 +0000159 ifp->mtu6 = ifp->mtu = ifreq.ifr_mtu;
paul718e3742002-12-13 20:15:29 +0000160#endif /* SUNOS_5 */
161
Paul Jakmaf5e004f2007-04-10 19:43:43 +0000162 /* propogate */
163 zebra_interface_up_update(ifp);
164
paul718e3742002-12-13 20:15:29 +0000165#else
166 zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
paul44145db2004-05-09 11:00:23 +0000167 ifp->mtu6 = ifp->mtu = -1;
paul718e3742002-12-13 20:15:29 +0000168#endif
169}
170
171#ifdef HAVE_NETLINK
172/* Interface address setting via netlink interface. */
173int
174if_set_prefix (struct interface *ifp, struct connected *ifc)
175{
176 return kernel_address_add_ipv4 (ifp, ifc);
177}
178
179/* Interface address is removed using netlink interface. */
180int
181if_unset_prefix (struct interface *ifp, struct connected *ifc)
182{
183 return kernel_address_delete_ipv4 (ifp, ifc);
184}
185#else /* ! HAVE_NETLINK */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000186#ifdef HAVE_STRUCT_IFALIASREQ
paul718e3742002-12-13 20:15:29 +0000187/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
188 has ifaliasreq structure. */
189int
190if_set_prefix (struct interface *ifp, struct connected *ifc)
191{
192 int ret;
193 struct ifaliasreq addreq;
194 struct sockaddr_in addr;
195 struct sockaddr_in mask;
196 struct prefix_ipv4 *p;
197
198 p = (struct prefix_ipv4 *) ifc->address;
Denis Ovsienko20e5ff02008-02-26 14:02:24 +0000199 rib_lookup_and_pushup (p);
paul718e3742002-12-13 20:15:29 +0000200
201 memset (&addreq, 0, sizeof addreq);
202 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
203
204 memset (&addr, 0, sizeof (struct sockaddr_in));
205 addr.sin_addr = p->prefix;
206 addr.sin_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000207#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000208 addr.sin_len = sizeof (struct sockaddr_in);
209#endif
210 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
211
212 memset (&mask, 0, sizeof (struct sockaddr_in));
213 masklen2ip (p->prefixlen, &mask.sin_addr);
214 mask.sin_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000215#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000216 mask.sin_len = sizeof (struct sockaddr_in);
217#endif
218 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
219
220 ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
221 if (ret < 0)
222 return ret;
223 return 0;
224}
225
226/* Set up interface's IP address, netmask (and broadcas? ). *BSD may
227 has ifaliasreq structure. */
228int
229if_unset_prefix (struct interface *ifp, struct connected *ifc)
230{
231 int ret;
232 struct ifaliasreq addreq;
233 struct sockaddr_in addr;
234 struct sockaddr_in mask;
235 struct prefix_ipv4 *p;
236
237 p = (struct prefix_ipv4 *)ifc->address;
238
239 memset (&addreq, 0, sizeof addreq);
240 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
241
242 memset (&addr, 0, sizeof (struct sockaddr_in));
243 addr.sin_addr = p->prefix;
244 addr.sin_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000245#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000246 addr.sin_len = sizeof (struct sockaddr_in);
247#endif
248 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
249
250 memset (&mask, 0, sizeof (struct sockaddr_in));
251 masklen2ip (p->prefixlen, &mask.sin_addr);
252 mask.sin_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000253#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000254 mask.sin_len = sizeof (struct sockaddr_in);
255#endif
256 memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
257
258 ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
259 if (ret < 0)
260 return ret;
261 return 0;
262}
263#else
264/* Set up interface's address, netmask (and broadcas? ). Linux or
265 Solaris uses ifname:number semantics to set IP address aliases. */
266int
267if_set_prefix (struct interface *ifp, struct connected *ifc)
268{
269 int ret;
270 struct ifreq ifreq;
271 struct sockaddr_in addr;
272 struct sockaddr_in broad;
273 struct sockaddr_in mask;
274 struct prefix_ipv4 ifaddr;
275 struct prefix_ipv4 *p;
276
277 p = (struct prefix_ipv4 *) ifc->address;
278
279 ifaddr = *p;
280
281 ifreq_set_name (&ifreq, ifp);
282
283 addr.sin_addr = p->prefix;
284 addr.sin_family = p->family;
285 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
286 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
287 if (ret < 0)
288 return ret;
289
290 /* We need mask for make broadcast addr. */
291 masklen2ip (p->prefixlen, &mask.sin_addr);
292
293 if (if_is_broadcast (ifp))
294 {
295 apply_mask_ipv4 (&ifaddr);
296 addr.sin_addr = ifaddr.prefix;
297
298 broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
299 broad.sin_family = p->family;
300
301 memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
302 ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
303 if (ret < 0)
304 return ret;
305 }
306
307 mask.sin_family = p->family;
308#ifdef SUNOS_5
309 memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
310#else
311 memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
312#endif /* SUNOS5 */
313 ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
314 if (ret < 0)
315 return ret;
316
317 return 0;
318}
319
320/* Set up interface's address, netmask (and broadcas? ). Linux or
321 Solaris uses ifname:number semantics to set IP address aliases. */
322int
323if_unset_prefix (struct interface *ifp, struct connected *ifc)
324{
325 int ret;
326 struct ifreq ifreq;
327 struct sockaddr_in addr;
328 struct prefix_ipv4 *p;
329
330 p = (struct prefix_ipv4 *) ifc->address;
331
332 ifreq_set_name (&ifreq, ifp);
333
334 memset (&addr, 0, sizeof (struct sockaddr_in));
335 addr.sin_family = p->family;
336 memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
337 ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
338 if (ret < 0)
339 return ret;
340
341 return 0;
342}
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000343#endif /* HAVE_STRUCT_IFALIASREQ */
paul718e3742002-12-13 20:15:29 +0000344#endif /* HAVE_NETLINK */
345
346/* get interface flags */
347void
348if_get_flags (struct interface *ifp)
349{
350 int ret;
351 struct ifreq ifreq;
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000352#ifdef HAVE_BSD_LINK_DETECT
353 struct ifmediareq ifmr;
354#endif /* HAVE_BSD_LINK_DETECT */
paul718e3742002-12-13 20:15:29 +0000355
356 ifreq_set_name (&ifreq, ifp);
357
358 ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
359 if (ret < 0)
360 {
ajs6a52d0d2005-01-30 18:49:28 +0000361 zlog_err("if_ioctl(SIOCGIFFLAGS) failed: %s", safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000362 return;
363 }
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000364#ifdef HAVE_BSD_LINK_DETECT /* Detect BSD link-state at start-up */
365 (void) memset(&ifmr, 0, sizeof(ifmr));
366 strncpy (&ifmr.ifm_name, ifp->name, IFNAMSIZ);
367 if (if_ioctl(SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
368 {
369 zlog_err("if_ioctl(SIOCGIFMEDIA) failed: %s", safe_strerror(errno));
370 return;
371 }
372 if (ifmr.ifm_status & IFM_AVALID) /* Link state is valid */
373 {
374 if (ifmr.ifm_status & IFM_ACTIVE)
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000375 SET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000376 else
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000377 UNSET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000378 }
379 else /* Force always up */
Andrew J. Schorr55edb0d2008-01-11 15:57:13 +0000380 SET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
Andrew J. Schorrc543a172008-01-10 15:24:32 +0000381#endif /* HAVE_BSD_LINK_DETECT */
paul718e3742002-12-13 20:15:29 +0000382
paul5c78b3d2006-01-25 04:31:40 +0000383 if_flags_update (ifp, (ifreq.ifr_flags & 0x0000ffff));
paul718e3742002-12-13 20:15:29 +0000384}
385
386/* Set interface flags */
387int
paul5c78b3d2006-01-25 04:31:40 +0000388if_set_flags (struct interface *ifp, uint64_t flags)
paul718e3742002-12-13 20:15:29 +0000389{
390 int ret;
391 struct ifreq ifreq;
392
pauld1724b62003-10-22 02:41:52 +0000393 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000394 ifreq_set_name (&ifreq, ifp);
395
396 ifreq.ifr_flags = ifp->flags;
397 ifreq.ifr_flags |= flags;
398
399 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
400
401 if (ret < 0)
402 {
403 zlog_info ("can't set interface flags");
404 return ret;
405 }
406 return 0;
407}
408
409/* Unset interface's flag. */
410int
paul5c78b3d2006-01-25 04:31:40 +0000411if_unset_flags (struct interface *ifp, uint64_t flags)
paul718e3742002-12-13 20:15:29 +0000412{
413 int ret;
414 struct ifreq ifreq;
415
pauld1724b62003-10-22 02:41:52 +0000416 memset (&ifreq, 0, sizeof(struct ifreq));
paul718e3742002-12-13 20:15:29 +0000417 ifreq_set_name (&ifreq, ifp);
418
419 ifreq.ifr_flags = ifp->flags;
420 ifreq.ifr_flags &= ~flags;
421
422 ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
423
424 if (ret < 0)
425 {
426 zlog_info ("can't unset interface flags");
427 return ret;
428 }
429 return 0;
430}
431
432#ifdef HAVE_IPV6
433
434#ifdef LINUX_IPV6
435#ifndef _LINUX_IN6_H
436/* linux/include/net/ipv6.h */
437struct in6_ifreq
438{
439 struct in6_addr ifr6_addr;
440 u_int32_t ifr6_prefixlen;
441 int ifr6_ifindex;
442};
443#endif /* _LINUX_IN6_H */
444
445/* Interface's address add/delete functions. */
446int
447if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
448{
449 int ret;
450 struct prefix_ipv6 *p;
451 struct in6_ifreq ifreq;
452
453 p = (struct prefix_ipv6 *) ifc->address;
454
455 memset (&ifreq, 0, sizeof (struct in6_ifreq));
456
457 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
458 ifreq.ifr6_ifindex = ifp->ifindex;
459 ifreq.ifr6_prefixlen = p->prefixlen;
460
461 ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
462
463 return ret;
464}
465
466int
467if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
468{
469 int ret;
470 struct prefix_ipv6 *p;
471 struct in6_ifreq ifreq;
472
473 p = (struct prefix_ipv6 *) ifc->address;
474
475 memset (&ifreq, 0, sizeof (struct in6_ifreq));
476
477 memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
478 ifreq.ifr6_ifindex = ifp->ifindex;
479 ifreq.ifr6_prefixlen = p->prefixlen;
480
481 ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
482
483 return ret;
484}
485#else /* LINUX_IPV6 */
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000486#ifdef HAVE_STRUCT_IN6_ALIASREQ
paul718e3742002-12-13 20:15:29 +0000487#ifndef ND6_INFINITE_LIFETIME
488#define ND6_INFINITE_LIFETIME 0xffffffffL
489#endif /* ND6_INFINITE_LIFETIME */
490int
491if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
492{
493 int ret;
494 struct in6_aliasreq addreq;
495 struct sockaddr_in6 addr;
496 struct sockaddr_in6 mask;
497 struct prefix_ipv6 *p;
498
499 p = (struct prefix_ipv6 * ) ifc->address;
500
501 memset (&addreq, 0, sizeof addreq);
502 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
503
504 memset (&addr, 0, sizeof (struct sockaddr_in6));
505 addr.sin6_addr = p->prefix;
506 addr.sin6_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000507#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000508 addr.sin6_len = sizeof (struct sockaddr_in6);
509#endif
510 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
511
512 memset (&mask, 0, sizeof (struct sockaddr_in6));
513 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
514 mask.sin6_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000515#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000516 mask.sin6_len = sizeof (struct sockaddr_in6);
517#endif
518 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
paul0e4f1902003-06-12 02:13:45 +0000519
520 addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
521 addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
paul718e3742002-12-13 20:15:29 +0000522
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000523#ifdef HAVE_STRUCT_IF6_ALIASREQ_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000524 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
525 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000526#endif
paul718e3742002-12-13 20:15:29 +0000527
528 ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
529 if (ret < 0)
530 return ret;
531 return 0;
532}
533
534int
535if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
536{
537 int ret;
538 struct in6_aliasreq addreq;
539 struct sockaddr_in6 addr;
540 struct sockaddr_in6 mask;
541 struct prefix_ipv6 *p;
542
543 p = (struct prefix_ipv6 *) ifc->address;
544
545 memset (&addreq, 0, sizeof addreq);
546 strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
547
548 memset (&addr, 0, sizeof (struct sockaddr_in6));
549 addr.sin6_addr = p->prefix;
550 addr.sin6_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000551#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000552 addr.sin6_len = sizeof (struct sockaddr_in6);
553#endif
554 memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
555
556 memset (&mask, 0, sizeof (struct sockaddr_in6));
557 masklen2ip6 (p->prefixlen, &mask.sin6_addr);
558 mask.sin6_family = p->family;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000559#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000560 mask.sin6_len = sizeof (struct sockaddr_in6);
561#endif
562 memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
563
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000564#ifdef HAVE_STRUCT_IF6_ALIASREQ_IFRA_LIFETIME
paul718e3742002-12-13 20:15:29 +0000565 addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
566 addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
hasso726f9b22003-05-25 21:04:54 +0000567#endif
paul718e3742002-12-13 20:15:29 +0000568
569 ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
570 if (ret < 0)
571 return ret;
572 return 0;
573}
574#else
575int
576if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
577{
578 return 0;
579}
580
581int
582if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
583{
584 return 0;
585}
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000586#endif /* HAVE_STRUCT_IN6_ALIASREQ */
paul718e3742002-12-13 20:15:29 +0000587
588#endif /* LINUX_IPV6 */
589
590#endif /* HAVE_IPV6 */