2004-01-14 17:16:19 +00:00
|
|
|
/* $KAME: if.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */
|
2000-10-06 23:46:52 +00:00
|
|
|
|
2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-07-05 10:14:11 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-07-05 10:14:11 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
#include <sys/capsicum.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <sys/ioctl.h>
|
2000-10-06 23:46:52 +00:00
|
|
|
#include <sys/queue.h>
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_media.h>
|
2003-08-11 15:51:54 +00:00
|
|
|
#include <net/ethernet.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/icmp6.h>
|
|
|
|
|
|
|
|
#include <netinet6/in6_var.h>
|
2009-09-12 22:14:58 +00:00
|
|
|
#include <netinet6/nd6.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
#include <capsicum_helpers.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
2000-07-05 10:14:11 +00:00
|
|
|
#include <ifaddrs.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include "rtsold.h"
|
|
|
|
|
|
|
|
static int ifsock;
|
2007-11-07 10:53:41 +00:00
|
|
|
static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
int
|
2004-01-14 17:16:19 +00:00
|
|
|
ifinit(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
cap_rights_t rights;
|
|
|
|
int sock;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
|
|
|
|
if (sock < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "socket(): %s",
|
|
|
|
strerror(errno));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (caph_rights_limit(sock, cap_rights_init(&rights, CAP_IOCTL)) < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "caph_rights_limit(): %s",
|
|
|
|
strerror(errno));
|
|
|
|
(void)close(sock);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
ifsock = sock;
|
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
interface_up(char *name)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
2009-09-12 22:14:58 +00:00
|
|
|
struct in6_ndireq nd;
|
2000-07-05 10:14:11 +00:00
|
|
|
int llflag;
|
2009-09-12 22:14:58 +00:00
|
|
|
int s;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2009-09-12 22:14:58 +00:00
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2016-05-15 22:17:41 +00:00
|
|
|
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
2009-09-12 22:14:58 +00:00
|
|
|
memset(&nd, 0, sizeof(nd));
|
|
|
|
strlcpy(nd.ifname, name, sizeof(nd.ifname));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s",
|
2003-08-08 16:56:01 +00:00
|
|
|
strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if (!(ifr.ifr_flags & IFF_UP)) {
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
2003-08-08 16:56:01 +00:00
|
|
|
if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"ioctl(SIOCSIFFLAGS): %s", strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2009-09-12 22:14:58 +00:00
|
|
|
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
warnmsg(LOG_WARNING, __func__, "socket(AF_INET6, SOCK_DGRAM): %s",
|
|
|
|
strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2009-09-12 22:14:58 +00:00
|
|
|
}
|
|
|
|
if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
|
|
|
|
warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFINFO_IN6): %s",
|
|
|
|
strerror(errno));
|
|
|
|
close(s);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2009-09-12 22:14:58 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2009-09-12 22:14:58 +00:00
|
|
|
if (nd.ndi.flags & ND6_IFF_IFDISABLED) {
|
|
|
|
if (Fflag) {
|
|
|
|
nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
|
|
|
|
if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
|
|
|
|
warnmsg(LOG_WARNING, __func__,
|
|
|
|
"ioctl(SIOCSIFINFO_IN6): %s",
|
|
|
|
strerror(errno));
|
|
|
|
close(s);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2009-09-12 22:14:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warnmsg(LOG_WARNING, __func__,
|
|
|
|
"%s is disabled.", name);
|
|
|
|
close(s);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2009-09-12 22:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) {
|
|
|
|
if (Fflag) {
|
|
|
|
nd.ndi.flags |= ND6_IFF_ACCEPT_RTADV;
|
|
|
|
if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd)) {
|
|
|
|
warnmsg(LOG_WARNING, __func__,
|
|
|
|
"ioctl(SIOCSIFINFO_IN6): %s",
|
|
|
|
strerror(errno));
|
|
|
|
close(s);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2009-09-12 22:14:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warnmsg(LOG_WARNING, __func__,
|
|
|
|
"%s does not accept Router Advertisement.", name);
|
|
|
|
close(s);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2009-09-12 22:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
if (cap_llflags_get(capllflags, name, &llflag) != 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_WARNING, __func__,
|
Capsicumize rtsol(8) and rtsold(8).
These programs parse ND6 Router Advertisement messages; rtsold(8) has
required an SA, SA-14:20.rtsold, for a bug in this code. Thus, they
are good candidates for sandboxing.
The approach taken is to run the main executable in capability mode
and use Casper services to provide functionality that cannot be
implemented within the sandbox. In particular, several custom services
were required.
- A Casper service is used to send Router Solicitation messages on a
raw ICMP6 socket. Initially I took the approach of creating a
socket for each interface upon startup, and connect(2)ing it to
the all-routers multicast group for the interface. This permits
the use of sendmsg(2) in capability mode, but only works if the
interface's link is up when rtsol(d) starts. So, instead, the
rtsold.sendmsg service is used to transmit RS messages on behalf
of the main process. One could alternately define a service
which simply creates and connects a socket for each destination
address, and returns the socket to the sandboxed process. However,
to implement rtsold's -m option we also need to read the ND6 default
router list, and this cannot be done in capability mode.
- rtsold may execute resolvconf(8) in response to RDNSS and DNSSL
options in received RA messages. A Casper service is used to
fork and exec resolvconf(8), and to reap the child process.
- A service is used to determine whether a given interface's
link-local address is useable (i.e., not duplicated or undergoing
DAD). This information is supplied by getifaddrs(3), which reads
a sysctl not available in capability mode. The SIOCGIFCONF socket
ioctl provides equivalent information and can be used in capability
mode, but I decided against it for now because of some limitations
of that interface.
In addition to these new services, cap_syslog(3) is used to send
messages to syslogd.
Reviewed by: oshogbo
Tested by: bz (previous versions)
MFC after: 2 months
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D17572
2019-01-05 16:05:39 +00:00
|
|
|
"cap_llflags_get() failed, anyway I'll try");
|
2011-06-06 03:06:43 +00:00
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
if (!(llflag & IN6_IFF_NOTREADY)) {
|
2003-08-08 16:56:01 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "%s is ready", name);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (0);
|
2000-07-05 10:14:11 +00:00
|
|
|
} else {
|
|
|
|
if (llflag & IN6_IFF_TENTATIVE) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "%s is tentative",
|
2003-08-08 16:56:01 +00:00
|
|
|
name);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (IFS_TENTATIVE);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
2000-07-05 10:14:11 +00:00
|
|
|
if (llflag & IN6_IFF_DUPLICATED)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "%s is duplicated",
|
2003-08-08 16:56:01 +00:00
|
|
|
name);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
interface_status(struct ifinfo *ifinfo)
|
|
|
|
{
|
|
|
|
char *ifname = ifinfo->ifname;
|
|
|
|
struct ifreq ifr;
|
|
|
|
struct ifmediareq ifmr;
|
2003-08-08 16:56:01 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/* get interface flags */
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2016-05-15 22:17:41 +00:00
|
|
|
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s",
|
2003-08-08 16:56:01 +00:00
|
|
|
ifname, strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if one of UP and RUNNING flags is dropped,
|
|
|
|
* the interface is not active.
|
|
|
|
*/
|
2011-06-06 03:06:43 +00:00
|
|
|
if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
|
1999-12-28 02:37:14 +00:00
|
|
|
goto inactive;
|
|
|
|
/* Next, check carrier on the interface, if possible */
|
|
|
|
if (!ifinfo->mediareqok)
|
|
|
|
goto active;
|
|
|
|
memset(&ifmr, 0, sizeof(ifmr));
|
2016-05-15 22:17:41 +00:00
|
|
|
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
|
|
|
|
if (errno != EINVAL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"ioctl(SIOCGIFMEDIA) on %s: %s",
|
|
|
|
ifname, strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* EINVAL simply means that the interface does not support
|
|
|
|
* the SIOCGIFMEDIA ioctl. We regard it alive.
|
|
|
|
*/
|
|
|
|
ifinfo->mediareqok = 0;
|
|
|
|
goto active;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifmr.ifm_status & IFM_AVALID) {
|
2003-08-08 16:56:01 +00:00
|
|
|
switch (ifmr.ifm_active & IFM_NMASK) {
|
|
|
|
case IFM_ETHER:
|
2003-08-16 19:00:32 +00:00
|
|
|
case IFM_IEEE80211:
|
2003-08-08 16:56:01 +00:00
|
|
|
if (ifmr.ifm_status & IFM_ACTIVE)
|
|
|
|
goto active;
|
|
|
|
else
|
|
|
|
goto inactive;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto inactive;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inactive:
|
2011-06-06 03:06:43 +00:00
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
active:
|
2011-06-06 03:06:43 +00:00
|
|
|
return (1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#define ROUNDUP(a, size) \
|
1999-12-28 02:37:14 +00:00
|
|
|
(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
|
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
#define NEXT_SA(ap) (ap) = (struct sockaddr *) \
|
1999-12-28 02:37:14 +00:00
|
|
|
((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
|
2003-08-08 16:56:01 +00:00
|
|
|
sizeof(u_long)) : sizeof(u_long)))
|
2000-07-05 10:14:11 +00:00
|
|
|
#define ROUNDUP8(a) (1 + (((a) - 1) | 7))
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
lladdropt_length(struct sockaddr_dl *sdl)
|
|
|
|
{
|
2003-08-08 16:56:01 +00:00
|
|
|
switch (sdl->sdl_type) {
|
|
|
|
case IFT_ETHER:
|
2011-06-06 03:06:43 +00:00
|
|
|
return (ROUNDUP8(ETHER_ADDR_LEN + 2));
|
2003-08-08 16:56:01 +00:00
|
|
|
default:
|
2011-06-06 03:06:43 +00:00
|
|
|
return (0);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
|
|
|
|
{
|
|
|
|
char *addr;
|
|
|
|
|
|
|
|
ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
|
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
switch (sdl->sdl_type) {
|
|
|
|
case IFT_ETHER:
|
|
|
|
ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
|
|
|
|
addr = (char *)(ndopt + 1);
|
|
|
|
memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"unsupported link type(%d)", sdl->sdl_type);
|
|
|
|
exit(1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_dl *
|
|
|
|
if_nametosdl(char *name)
|
|
|
|
{
|
2015-09-10 06:40:28 +00:00
|
|
|
int mib[] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
|
1999-12-28 02:37:14 +00:00
|
|
|
char *buf, *next, *lim;
|
|
|
|
size_t len;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct sockaddr *sa, *rti_info[RTAX_MAX];
|
|
|
|
struct sockaddr_dl *sdl = NULL, *ret_sdl;
|
|
|
|
|
2015-09-10 06:40:28 +00:00
|
|
|
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
|
1999-12-28 02:37:14 +00:00
|
|
|
return(NULL);
|
|
|
|
if ((buf = malloc(len)) == NULL)
|
|
|
|
return(NULL);
|
2015-09-10 06:40:28 +00:00
|
|
|
if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0) {
|
1999-12-28 02:37:14 +00:00
|
|
|
free(buf);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (NULL);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lim = buf + len;
|
|
|
|
for (next = buf; next < lim; next += ifm->ifm_msglen) {
|
2013-08-17 19:23:35 +00:00
|
|
|
ifm = (struct if_msghdr *)(void *)next;
|
1999-12-28 02:37:14 +00:00
|
|
|
if (ifm->ifm_type == RTM_IFINFO) {
|
|
|
|
sa = (struct sockaddr *)(ifm + 1);
|
|
|
|
get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
|
|
|
|
if ((sa = rti_info[RTAX_IFP]) != NULL) {
|
|
|
|
if (sa->sa_family == AF_LINK) {
|
2013-08-17 19:23:35 +00:00
|
|
|
sdl = (struct sockaddr_dl *)(void *)sa;
|
2000-07-05 10:14:11 +00:00
|
|
|
if (strlen(name) != sdl->sdl_nlen)
|
|
|
|
continue; /* not same len */
|
1999-12-28 02:37:14 +00:00
|
|
|
if (strncmp(&sdl->sdl_data[0],
|
|
|
|
name,
|
|
|
|
sdl->sdl_nlen) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (next == lim) {
|
|
|
|
/* search failed */
|
|
|
|
free(buf);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (NULL);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2006-03-24 23:59:51 +00:00
|
|
|
if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) {
|
|
|
|
free(buf);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (NULL);
|
2006-03-24 23:59:51 +00:00
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
|
|
|
|
|
2001-06-11 12:39:29 +00:00
|
|
|
free(buf);
|
2011-06-06 03:06:43 +00:00
|
|
|
return (ret_sdl);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
|
|
|
|
{
|
|
|
|
int i;
|
2003-08-08 16:56:01 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
for (i = 0; i < RTAX_MAX; i++) {
|
|
|
|
if (addrs & (1 << i)) {
|
|
|
|
rti_info[i] = sa;
|
|
|
|
NEXT_SA(sa);
|
2003-08-08 16:56:01 +00:00
|
|
|
} else
|
1999-12-28 02:37:14 +00:00
|
|
|
rti_info[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|