2004-01-14 17:16:19 +00:00
|
|
|
/* $KAME: rtsol.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */
|
2000-10-06 23:46:52 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
2011-06-06 03:06:43 +00:00
|
|
|
* Copyright (C) 2011 Hiroki Sato
|
1999-12-28 02:37:14 +00:00
|
|
|
* 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>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/time.h>
|
2000-10-06 23:46:52 +00:00
|
|
|
#include <sys/queue.h>
|
2003-08-08 16:42:37 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
#define __BSD_VISIBLE 1 /* IN6ADDR_LINKLOCAL_ALLROUTERS_INIT */
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <netinet/in.h>
|
2011-06-06 03:06:43 +00:00
|
|
|
#undef __BSD_VISIBLE
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#include <netinet/icmp6.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
#include <netdb.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <time.h>
|
2003-08-17 11:11:32 +00:00
|
|
|
#include <fcntl.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include "rtsold.h"
|
|
|
|
|
|
|
|
static struct msghdr rcvmhdr;
|
|
|
|
static struct msghdr sndmhdr;
|
|
|
|
static struct iovec rcviov[2];
|
|
|
|
static struct iovec sndiov[2];
|
|
|
|
static struct sockaddr_in6 from;
|
2003-08-17 11:11:32 +00:00
|
|
|
static int rcvcmsglen;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-05 10:14:11 +00:00
|
|
|
int rssock;
|
2011-09-12 23:55:23 +00:00
|
|
|
static char rsid[IFNAMSIZ + 1 + sizeof(DNSINFO_ORIGIN_LABEL) + 1 + NI_MAXHOST];
|
2011-06-06 03:06:43 +00:00
|
|
|
struct ifinfo_head_t ifinfo_head =
|
|
|
|
TAILQ_HEAD_INITIALIZER(ifinfo_head);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
static const struct sockaddr_in6 sin6_allrouters = {
|
2010-02-27 10:19:39 +00:00
|
|
|
.sin6_len = sizeof(sin6_allrouters),
|
|
|
|
.sin6_family = AF_INET6,
|
2011-06-06 03:06:43 +00:00
|
|
|
.sin6_addr = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT,
|
2010-02-27 10:19:39 +00:00
|
|
|
};
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-09-12 23:55:23 +00:00
|
|
|
static void call_script(const int, const char *const *,
|
|
|
|
struct script_msg_head_t *);
|
2011-06-06 03:06:43 +00:00
|
|
|
static size_t dname_labeldec(char *, size_t, const char *);
|
2007-11-07 10:53:41 +00:00
|
|
|
static int safefile(const char *);
|
2011-06-08 16:03:29 +00:00
|
|
|
static struct ra_opt *find_raopt(struct rainfo *, int, void *, size_t);
|
2011-09-12 23:55:23 +00:00
|
|
|
static int ra_opt_rdnss_dispatch(struct ifinfo *, struct rainfo *,
|
|
|
|
struct script_msg_head_t *, struct script_msg_head_t *);
|
|
|
|
static char *make_rsid(const char *, const char *, struct rainfo *);
|
2003-08-08 16:42:37 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
#define _ARGS_OTHER otherconf_script, ifi->ifname
|
2011-09-12 23:55:23 +00:00
|
|
|
#define _ARGS_RESADD resolvconf_script, "-a", rsid
|
|
|
|
#define _ARGS_RESDEL resolvconf_script, "-d", rsid
|
2011-06-06 03:06:43 +00:00
|
|
|
|
|
|
|
#define CALL_SCRIPT(name, sm_head) \
|
|
|
|
do { \
|
|
|
|
const char *const sarg[] = { _ARGS_##name, NULL }; \
|
|
|
|
call_script(sizeof(sarg), sarg, sm_head); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define ELM_MALLOC(p,error_action) \
|
|
|
|
do { \
|
|
|
|
p = malloc(sizeof(*p)); \
|
|
|
|
if (p == NULL) { \
|
|
|
|
warnmsg(LOG_ERR, __func__, "malloc failed: %s", \
|
|
|
|
strerror(errno)); \
|
|
|
|
error_action; \
|
|
|
|
} \
|
|
|
|
memset(p, 0, sizeof(*p)); \
|
|
|
|
} while(0)
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
int
|
2004-01-14 17:16:19 +00:00
|
|
|
sockopen(void)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
2000-07-05 10:14:11 +00:00
|
|
|
static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL;
|
2003-08-17 11:11:32 +00:00
|
|
|
int sndcmsglen, on;
|
|
|
|
static u_char answer[1500];
|
|
|
|
struct icmp6_filter filt;
|
2000-07-05 10:14:11 +00:00
|
|
|
|
|
|
|
sndcmsglen = rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
|
2003-08-08 16:56:01 +00:00
|
|
|
CMSG_SPACE(sizeof(int));
|
2000-07-05 10:14:11 +00:00
|
|
|
if (rcvcmsgbuf == NULL && (rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"malloc for receive msghdr failed");
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
2000-07-05 10:14:11 +00:00
|
|
|
}
|
2003-08-08 16:56:01 +00:00
|
|
|
if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"malloc for send msghdr failed");
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
return (-1);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* specify to tell receiving interface */
|
|
|
|
on = 1;
|
2000-07-05 10:14:11 +00:00
|
|
|
if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
|
2003-08-08 16:56:01 +00:00
|
|
|
sizeof(on)) < 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "IPV6_RECVPKTINFO: %s",
|
2003-08-08 16:56:01 +00:00
|
|
|
strerror(errno));
|
2000-07-05 10:14:11 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* specify to tell value of hoplimit field of received IP6 hdr */
|
2011-06-06 03:06:43 +00:00
|
|
|
on = 1;
|
2000-07-05 10:14:11 +00:00
|
|
|
if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
|
2003-08-08 16:56:01 +00:00
|
|
|
sizeof(on)) < 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "IPV6_RECVHOPLIMIT: %s",
|
2003-08-08 16:56:01 +00:00
|
|
|
strerror(errno));
|
2000-07-05 10:14:11 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* specfiy to accept only router advertisements on the socket */
|
|
|
|
ICMP6_FILTER_SETBLOCKALL(&filt);
|
|
|
|
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
|
|
|
|
if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
|
2003-08-08 16:56:01 +00:00
|
|
|
sizeof(filt)) == -1) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "setsockopt(ICMP6_FILTER): %s",
|
2003-08-08 16:56:01 +00:00
|
|
|
strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize msghdr for receiving packets */
|
|
|
|
rcviov[0].iov_base = (caddr_t)answer;
|
|
|
|
rcviov[0].iov_len = sizeof(answer);
|
|
|
|
rcvmhdr.msg_name = (caddr_t)&from;
|
|
|
|
rcvmhdr.msg_iov = rcviov;
|
|
|
|
rcvmhdr.msg_iovlen = 1;
|
|
|
|
rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
|
|
|
|
|
|
|
|
/* initialize msghdr for sending packets */
|
|
|
|
sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
|
|
|
|
sndmhdr.msg_iov = sndiov;
|
|
|
|
sndmhdr.msg_iovlen = 1;
|
|
|
|
sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
|
2000-07-05 10:14:11 +00:00
|
|
|
sndmhdr.msg_controllen = sndcmsglen;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
return (rssock);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-06 03:06:43 +00:00
|
|
|
sendpacket(struct ifinfo *ifi)
|
1999-12-28 02:37:14 +00:00
|
|
|
{
|
|
|
|
struct in6_pktinfo *pi;
|
2003-08-08 16:56:01 +00:00
|
|
|
struct cmsghdr *cm;
|
|
|
|
int hoplimit = 255;
|
2010-02-27 10:19:39 +00:00
|
|
|
ssize_t i;
|
2003-08-17 11:11:32 +00:00
|
|
|
struct sockaddr_in6 dst;
|
|
|
|
|
|
|
|
dst = sin6_allrouters;
|
2011-06-06 03:06:43 +00:00
|
|
|
dst.sin6_scope_id = ifi->linkid;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
sndmhdr.msg_name = (caddr_t)&dst;
|
2011-06-06 03:06:43 +00:00
|
|
|
sndmhdr.msg_iov[0].iov_base = (caddr_t)ifi->rs_data;
|
|
|
|
sndmhdr.msg_iov[0].iov_len = ifi->rs_datalen;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
cm = CMSG_FIRSTHDR(&sndmhdr);
|
|
|
|
/* specify the outgoing interface */
|
|
|
|
cm->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cm->cmsg_type = IPV6_PKTINFO;
|
|
|
|
cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
|
|
|
|
pi = (struct in6_pktinfo *)CMSG_DATA(cm);
|
|
|
|
memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
|
2011-06-06 03:06:43 +00:00
|
|
|
pi->ipi6_ifindex = ifi->sdl->sdl_index;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
/* specify the hop limit of the packet */
|
2003-08-08 16:56:01 +00:00
|
|
|
cm = CMSG_NXTHDR(&sndmhdr, cm);
|
|
|
|
cm->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cm->cmsg_type = IPV6_HOPLIMIT;
|
|
|
|
cm->cmsg_len = CMSG_LEN(sizeof(int));
|
|
|
|
memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
|
|
|
"send RS on %s, whose state is %d",
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->ifname, ifi->state);
|
1999-12-28 02:37:14 +00:00
|
|
|
i = sendmsg(rssock, &sndmhdr, 0);
|
2011-06-06 03:06:43 +00:00
|
|
|
if (i < 0 || (size_t)i != ifi->rs_datalen) {
|
1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* ENETDOWN is not so serious, especially when using several
|
|
|
|
* network cards on a mobile node. We ignore it.
|
|
|
|
*/
|
|
|
|
if (errno != ENETDOWN || dflag > 0)
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->ifname, strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update counter */
|
2011-06-06 03:06:43 +00:00
|
|
|
ifi->probes++;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rtsol_input(int s)
|
|
|
|
{
|
2003-08-17 11:11:32 +00:00
|
|
|
u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
|
2011-06-06 03:06:43 +00:00
|
|
|
int l, ifindex = 0, *hlimp = NULL;
|
|
|
|
ssize_t msglen;
|
1999-12-28 02:37:14 +00:00
|
|
|
struct in6_pktinfo *pi = NULL;
|
|
|
|
struct ifinfo *ifi = NULL;
|
2011-06-06 03:06:43 +00:00
|
|
|
struct ra_opt *rao = NULL;
|
2003-08-17 11:11:32 +00:00
|
|
|
struct icmp6_hdr *icp;
|
2003-08-08 16:42:37 +00:00
|
|
|
struct nd_router_advert *nd_ra;
|
2003-08-17 11:11:32 +00:00
|
|
|
struct cmsghdr *cm;
|
2011-06-08 16:03:29 +00:00
|
|
|
struct rainfo *rai;
|
2011-06-06 03:06:43 +00:00
|
|
|
char *raoptp;
|
|
|
|
char *p;
|
|
|
|
struct in6_addr *addr;
|
|
|
|
struct nd_opt_hdr *ndo;
|
|
|
|
struct nd_opt_rdnss *rdnss;
|
|
|
|
struct nd_opt_dnssl *dnssl;
|
|
|
|
size_t len;
|
|
|
|
char nsbuf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ + 1];
|
|
|
|
char dname[NI_MAXHOST];
|
|
|
|
struct timeval now;
|
|
|
|
struct timeval lifetime;
|
2011-06-08 16:03:29 +00:00
|
|
|
int newent_rai;
|
|
|
|
int newent_rao;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-17 11:11:32 +00:00
|
|
|
/* get message. namelen and controllen must always be initialized. */
|
|
|
|
rcvmhdr.msg_namelen = sizeof(from);
|
|
|
|
rcvmhdr.msg_controllen = rcvcmsglen;
|
2011-06-06 03:06:43 +00:00
|
|
|
if ((msglen = recvmsg(s, &rcvmhdr, 0)) < 0) {
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "recvmsg: %s", strerror(errno));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract optional information via Advanced API */
|
2003-08-08 16:56:01 +00:00
|
|
|
for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr); cm;
|
|
|
|
cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
|
1999-12-28 02:37:14 +00:00
|
|
|
if (cm->cmsg_level == IPPROTO_IPV6 &&
|
|
|
|
cm->cmsg_type == IPV6_PKTINFO &&
|
|
|
|
cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
|
|
|
|
pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
|
|
|
|
ifindex = pi->ipi6_ifindex;
|
|
|
|
}
|
|
|
|
if (cm->cmsg_level == IPPROTO_IPV6 &&
|
|
|
|
cm->cmsg_type == IPV6_HOPLIMIT &&
|
|
|
|
cm->cmsg_len == CMSG_LEN(sizeof(int)))
|
|
|
|
hlimp = (int *)CMSG_DATA(cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifindex == 0) {
|
2003-08-08 16:56:01 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"failed to get receiving interface");
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (hlimp == NULL) {
|
2003-08-08 16:56:01 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"failed to get receiving hop limit");
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
if ((size_t)msglen < sizeof(struct nd_router_advert)) {
|
2003-08-14 15:21:55 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2011-06-06 03:06:43 +00:00
|
|
|
"packet size(%zd) is too short", msglen);
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
|
|
|
|
|
|
|
|
if (icp->icmp6_type != ND_ROUTER_ADVERT) {
|
2003-08-14 15:21:55 +00:00
|
|
|
/*
|
|
|
|
* this should not happen because we configured a filter
|
|
|
|
* that only passes RAs on the receiving socket.
|
|
|
|
*/
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_ERR, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"invalid icmp type(%d) from %s on %s", icp->icmp6_type,
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
|
2011-09-12 23:55:23 +00:00
|
|
|
sizeof(ntopbuf)),
|
2003-08-08 16:56:01 +00:00
|
|
|
if_indextoname(pi->ipi6_ifindex, ifnamebuf));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icp->icmp6_code != 0) {
|
2003-08-14 15:21:55 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"invalid icmp code(%d) from %s on %s", icp->icmp6_code,
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
|
2011-09-12 23:55:23 +00:00
|
|
|
sizeof(ntopbuf)),
|
2003-08-08 16:56:01 +00:00
|
|
|
if_indextoname(pi->ipi6_ifindex, ifnamebuf));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*hlimp != 255) {
|
2003-08-14 15:21:55 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"invalid RA with hop limit(%d) from %s on %s",
|
|
|
|
*hlimp,
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
|
2011-09-12 23:55:23 +00:00
|
|
|
sizeof(ntopbuf)),
|
2003-08-08 16:56:01 +00:00
|
|
|
if_indextoname(pi->ipi6_ifindex, ifnamebuf));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
|
2003-08-14 15:21:55 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"invalid RA with non link-local source from %s on %s",
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
|
2011-09-12 23:55:23 +00:00
|
|
|
sizeof(ntopbuf)),
|
2003-08-08 16:56:01 +00:00
|
|
|
if_indextoname(pi->ipi6_ifindex, ifnamebuf));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* xxx: more validation? */
|
|
|
|
|
|
|
|
if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
|
2003-08-14 15:21:55 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"received RA from %s on an unexpected IF(%s)",
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
|
2011-09-12 23:55:23 +00:00
|
|
|
sizeof(ntopbuf)),
|
2003-08-08 16:56:01 +00:00
|
|
|
if_indextoname(pi->ipi6_ifindex, ifnamebuf));
|
1999-12-28 02:37:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-08-08 16:38:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
2003-08-08 16:56:01 +00:00
|
|
|
"received RA from %s on %s, state is %d",
|
2011-09-12 23:55:23 +00:00
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, sizeof(ntopbuf)),
|
2003-08-08 16:56:01 +00:00
|
|
|
ifi->ifname, ifi->state);
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
nd_ra = (struct nd_router_advert *)icp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the "O bit."
|
|
|
|
* If the value of OtherConfigFlag changes from FALSE to TRUE, the
|
|
|
|
* host should invoke the stateful autoconfiguration protocol,
|
|
|
|
* requesting information.
|
|
|
|
* [RFC 2462 Section 5.5.3]
|
|
|
|
*/
|
|
|
|
if (((nd_ra->nd_ra_flags_reserved) & ND_RA_FLAG_OTHER) &&
|
|
|
|
!ifi->otherconfig) {
|
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
|
|
|
"OtherConfigFlag on %s is turned on", ifi->ifname);
|
|
|
|
ifi->otherconfig = 1;
|
2011-06-06 03:06:43 +00:00
|
|
|
CALL_SCRIPT(OTHER, NULL);
|
2003-08-08 16:42:37 +00:00
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
gettimeofday(&now, NULL);
|
2011-06-08 16:03:29 +00:00
|
|
|
newent_rai = 0;
|
|
|
|
rai = find_rainfo(ifi, &from);
|
|
|
|
if (rai == NULL) {
|
|
|
|
ELM_MALLOC(rai, exit(1));
|
|
|
|
rai->rai_ifinfo = ifi;
|
|
|
|
TAILQ_INIT(&rai->rai_ra_opt);
|
2011-09-12 23:55:23 +00:00
|
|
|
rai->rai_saddr.sin6_family = AF_INET6;
|
|
|
|
rai->rai_saddr.sin6_len = sizeof(rai->rai_saddr);
|
2011-06-08 16:03:29 +00:00
|
|
|
memcpy(&rai->rai_saddr.sin6_addr, &from.sin6_addr,
|
|
|
|
sizeof(rai->rai_saddr.sin6_addr));
|
|
|
|
newent_rai = 1;
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
|
|
|
|
#define RA_OPT_NEXT_HDR(x) (struct nd_opt_hdr *)((char *)x + \
|
|
|
|
(((struct nd_opt_hdr *)x)->nd_opt_len * 8))
|
|
|
|
/* Process RA options. */
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "Processing RA");
|
|
|
|
raoptp = (char *)icp + sizeof(struct nd_router_advert);
|
|
|
|
while (raoptp < (char *)icp + msglen) {
|
|
|
|
ndo = (struct nd_opt_hdr *)raoptp;
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "ndo = %p", raoptp);
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_type = %d",
|
|
|
|
ndo->nd_opt_type);
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_len = %d",
|
|
|
|
ndo->nd_opt_len);
|
|
|
|
|
|
|
|
switch (ndo->nd_opt_type) {
|
|
|
|
case ND_OPT_RDNSS:
|
|
|
|
rdnss = (struct nd_opt_rdnss *)raoptp;
|
|
|
|
|
|
|
|
/* Optlen sanity check (Section 5.3.1 in RFC 6106) */
|
|
|
|
if (rdnss->nd_opt_rdnss_len < 3) {
|
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"too short RDNSS option"
|
|
|
|
"in RA from %s was ignored.",
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr,
|
2011-09-12 23:55:23 +00:00
|
|
|
ntopbuf, sizeof(ntopbuf)));
|
2011-06-06 03:06:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = (struct in6_addr *)(raoptp + sizeof(*rdnss));
|
|
|
|
while ((char *)addr < (char *)RA_OPT_NEXT_HDR(raoptp)) {
|
|
|
|
if (inet_ntop(AF_INET6, addr, ntopbuf,
|
2011-09-12 23:55:23 +00:00
|
|
|
sizeof(ntopbuf)) == NULL) {
|
2011-06-06 03:06:43 +00:00
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"an invalid address in RDNSS option"
|
|
|
|
" in RA from %s was ignored.",
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr,
|
2011-09-12 23:55:23 +00:00
|
|
|
ntopbuf, sizeof(ntopbuf)));
|
2011-06-06 03:06:43 +00:00
|
|
|
addr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(addr))
|
|
|
|
/* XXX: % has to be escaped here */
|
|
|
|
l = snprintf(nsbuf, sizeof(nsbuf),
|
|
|
|
"%s%c%s", ntopbuf,
|
|
|
|
SCOPE_DELIMITER,
|
|
|
|
ifi->ifname);
|
|
|
|
else
|
|
|
|
l = snprintf(nsbuf, sizeof(nsbuf),
|
|
|
|
"%s", ntopbuf);
|
|
|
|
if (l < 0 || (size_t)l >= sizeof(nsbuf)) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"address copying error in "
|
|
|
|
"RDNSS option: %d.", l);
|
|
|
|
addr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "nsbuf = %s",
|
|
|
|
nsbuf);
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
newent_rao = 0;
|
|
|
|
rao = find_raopt(rai, ndo->nd_opt_type, nsbuf,
|
|
|
|
strlen(nsbuf));
|
|
|
|
if (rao == NULL) {
|
|
|
|
ELM_MALLOC(rao, break);
|
|
|
|
rao->rao_type = ndo->nd_opt_type;
|
|
|
|
rao->rao_len = strlen(nsbuf);
|
|
|
|
rao->rao_msg = strdup(nsbuf);
|
|
|
|
if (rao->rao_msg == NULL) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"strdup failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
free(rao);
|
|
|
|
addr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newent_rao = 1;
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
|
|
|
/* Set expiration timer */
|
2011-06-08 16:03:29 +00:00
|
|
|
memset(&rao->rao_expire, 0,
|
|
|
|
sizeof(rao->rao_expire));
|
2011-06-06 03:06:43 +00:00
|
|
|
memset(&lifetime, 0, sizeof(lifetime));
|
2011-06-08 16:03:29 +00:00
|
|
|
lifetime.tv_sec =
|
|
|
|
ntohl(rdnss->nd_opt_rdnss_lifetime);
|
2011-06-06 03:06:43 +00:00
|
|
|
timeradd(&now, &lifetime, &rao->rao_expire);
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
if (newent_rao)
|
|
|
|
TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
|
|
|
|
rao, rao_next);
|
2011-06-06 03:06:43 +00:00
|
|
|
addr++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ND_OPT_DNSSL:
|
|
|
|
dnssl = (struct nd_opt_dnssl *)raoptp;
|
|
|
|
|
|
|
|
/* Optlen sanity check (Section 5.3.1 in RFC 6106) */
|
|
|
|
if (dnssl->nd_opt_dnssl_len < 2) {
|
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"too short DNSSL option"
|
|
|
|
"in RA from %s was ignored.",
|
|
|
|
inet_ntop(AF_INET6, &from.sin6_addr,
|
2011-09-12 23:55:23 +00:00
|
|
|
ntopbuf, sizeof(ntopbuf)));
|
2011-06-06 03:06:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure NUL-termination in DNSSL in case of
|
|
|
|
* malformed field.
|
|
|
|
*/
|
|
|
|
p = (char *)RA_OPT_NEXT_HDR(raoptp);
|
|
|
|
*(p - 1) = '\0';
|
|
|
|
|
|
|
|
p = raoptp + sizeof(*dnssl);
|
|
|
|
while (1 < (len = dname_labeldec(dname, sizeof(dname),
|
|
|
|
p))) {
|
|
|
|
/* length == 1 means empty string */
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "dname = %s",
|
|
|
|
dname);
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
newent_rao = 0;
|
|
|
|
rao = find_raopt(rai, ndo->nd_opt_type, dname,
|
|
|
|
strlen(dname));
|
|
|
|
if (rao == NULL) {
|
|
|
|
ELM_MALLOC(rao, break);
|
|
|
|
rao->rao_type = ndo->nd_opt_type;
|
|
|
|
rao->rao_len = strlen(dname);
|
|
|
|
rao->rao_msg = strdup(dname);
|
|
|
|
if (rao->rao_msg == NULL) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"strdup failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
free(rao);
|
|
|
|
addr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
newent_rao = 1;
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
|
|
|
/* Set expiration timer */
|
2011-06-08 16:03:29 +00:00
|
|
|
memset(&rao->rao_expire, 0,
|
|
|
|
sizeof(rao->rao_expire));
|
2011-06-06 03:06:43 +00:00
|
|
|
memset(&lifetime, 0, sizeof(lifetime));
|
2011-06-08 16:03:29 +00:00
|
|
|
lifetime.tv_sec =
|
|
|
|
ntohl(dnssl->nd_opt_dnssl_lifetime);
|
2011-06-06 03:06:43 +00:00
|
|
|
timeradd(&now, &lifetime, &rao->rao_expire);
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
if (newent_rao)
|
|
|
|
TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
|
|
|
|
rao, rao_next);
|
2011-06-06 03:06:43 +00:00
|
|
|
p += len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* nothing to do for other options */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
raoptp = (char *)RA_OPT_NEXT_HDR(raoptp);
|
|
|
|
}
|
2011-06-08 16:03:29 +00:00
|
|
|
if (newent_rai)
|
|
|
|
TAILQ_INSERT_TAIL(&ifi->ifi_rainfo, rai, rai_next);
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
ra_opt_handler(ifi);
|
1999-12-28 02:37:14 +00:00
|
|
|
ifi->racnt++;
|
|
|
|
|
2003-08-08 16:56:01 +00:00
|
|
|
switch (ifi->state) {
|
|
|
|
case IFS_IDLE: /* should be ignored */
|
|
|
|
case IFS_DELAY: /* right? */
|
|
|
|
break;
|
|
|
|
case IFS_PROBE:
|
|
|
|
ifi->state = IFS_IDLE;
|
|
|
|
ifi->probes = 0;
|
|
|
|
rtsol_timer_update(ifi);
|
|
|
|
break;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-08 16:42:37 +00:00
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
static char resstr_ns_prefix[] = "nameserver ";
|
|
|
|
static char resstr_sh_prefix[] = "search ";
|
|
|
|
static char resstr_nl[] = "\n";
|
|
|
|
static char resstr_sp[] = " ";
|
|
|
|
|
|
|
|
int
|
|
|
|
ra_opt_handler(struct ifinfo *ifi)
|
|
|
|
{
|
|
|
|
struct ra_opt *rao;
|
2011-06-08 16:03:29 +00:00
|
|
|
struct rainfo *rai;
|
2011-06-06 03:06:43 +00:00
|
|
|
struct script_msg *smp1, *smp2, *smp3;
|
|
|
|
struct timeval now;
|
2011-09-12 23:55:23 +00:00
|
|
|
struct script_msg_head_t sm_rdnss_head =
|
|
|
|
TAILQ_HEAD_INITIALIZER(sm_rdnss_head);
|
|
|
|
struct script_msg_head_t sm_dnssl_head =
|
|
|
|
TAILQ_HEAD_INITIALIZER(sm_dnssl_head);
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
int dcount, dlen;
|
|
|
|
|
|
|
|
dcount = 0;
|
|
|
|
dlen = strlen(resstr_sh_prefix) + strlen(resstr_nl);
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
/*
|
|
|
|
* All options from multiple RAs with the same or different
|
|
|
|
* source addresses on a single interface will be gathered and
|
|
|
|
* handled, not overridden. [RFC 4861 6.3.4]
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
|
|
|
|
TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
|
|
|
|
switch (rao->rao_type) {
|
|
|
|
case ND_OPT_RDNSS:
|
|
|
|
if (timercmp(&now, &rao->rao_expire, >)) {
|
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"expired rdnss entry: %s",
|
|
|
|
(char *)rao->rao_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ELM_MALLOC(smp1, continue);
|
|
|
|
ELM_MALLOC(smp2, goto free1);
|
|
|
|
ELM_MALLOC(smp3, goto free2);
|
|
|
|
smp1->sm_msg = resstr_ns_prefix;
|
|
|
|
TAILQ_INSERT_TAIL(&sm_rdnss_head, smp1,
|
|
|
|
sm_next);
|
|
|
|
smp2->sm_msg = rao->rao_msg;
|
|
|
|
TAILQ_INSERT_TAIL(&sm_rdnss_head, smp2,
|
|
|
|
sm_next);
|
|
|
|
smp3->sm_msg = resstr_nl;
|
|
|
|
TAILQ_INSERT_TAIL(&sm_rdnss_head, smp3,
|
|
|
|
sm_next);
|
|
|
|
ifi->ifi_rdnss = IFI_DNSOPT_STATE_RECEIVED;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
break;
|
2011-06-08 16:03:29 +00:00
|
|
|
case ND_OPT_DNSSL:
|
|
|
|
if (timercmp(&now, &rao->rao_expire, >)) {
|
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"expired dnssl entry: %s",
|
|
|
|
(char *)rao->rao_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dcount++;
|
|
|
|
/* Check resolv.conf(5) restrictions. */
|
|
|
|
if (dcount > 6) {
|
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"dnssl entry exceeding maximum count (%d>6)"
|
|
|
|
": %s", dcount, (char *)rao->rao_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (256 < dlen + strlen(rao->rao_msg) +
|
|
|
|
strlen(resstr_sp)) {
|
|
|
|
warnmsg(LOG_INFO, __func__,
|
|
|
|
"dnssl entry exceeding maximum length "
|
|
|
|
"(>256): %s", (char *)rao->rao_msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ELM_MALLOC(smp1, continue);
|
|
|
|
ELM_MALLOC(smp2, goto free1);
|
|
|
|
if (TAILQ_EMPTY(&sm_dnssl_head)) {
|
|
|
|
ELM_MALLOC(smp3, goto free2);
|
|
|
|
smp3->sm_msg = resstr_sh_prefix;
|
|
|
|
TAILQ_INSERT_TAIL(&sm_dnssl_head, smp3,
|
|
|
|
sm_next);
|
|
|
|
}
|
|
|
|
smp1->sm_msg = rao->rao_msg;
|
|
|
|
TAILQ_INSERT_TAIL(&sm_dnssl_head, smp1,
|
|
|
|
sm_next);
|
|
|
|
smp2->sm_msg = resstr_sp;
|
|
|
|
TAILQ_INSERT_TAIL(&sm_dnssl_head, smp2,
|
|
|
|
sm_next);
|
|
|
|
dlen += strlen(rao->rao_msg) +
|
|
|
|
strlen(resstr_sp);
|
2011-06-06 03:06:43 +00:00
|
|
|
break;
|
2011-06-08 16:03:29 +00:00
|
|
|
|
|
|
|
ifi->ifi_dnssl = IFI_DNSOPT_STATE_RECEIVED;
|
|
|
|
default:
|
2011-06-06 03:06:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-06-08 16:03:29 +00:00
|
|
|
continue;
|
2011-06-06 03:06:43 +00:00
|
|
|
free2:
|
2011-06-08 16:03:29 +00:00
|
|
|
free(smp2);
|
2011-06-06 03:06:43 +00:00
|
|
|
free1:
|
2011-06-08 16:03:29 +00:00
|
|
|
free(smp1);
|
|
|
|
}
|
2011-09-12 23:55:23 +00:00
|
|
|
/* Call the script for each information source. */
|
|
|
|
if (uflag)
|
|
|
|
ra_opt_rdnss_dispatch(ifi, rai, &sm_rdnss_head,
|
|
|
|
&sm_dnssl_head);
|
|
|
|
}
|
|
|
|
/* Call the script for each interface. */
|
|
|
|
if (!uflag)
|
|
|
|
ra_opt_rdnss_dispatch(ifi, NULL, &sm_rdnss_head,
|
|
|
|
&sm_dnssl_head);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
make_rsid(const char *ifname, const char *origin, struct rainfo *rai)
|
|
|
|
{
|
|
|
|
char hbuf[NI_MAXHOST];
|
|
|
|
|
|
|
|
if (rai == NULL)
|
|
|
|
sprintf(rsid, "%s:%s", ifname, origin);
|
|
|
|
else {
|
|
|
|
if (!IN6_IS_ADDR_LINKLOCAL(&rai->rai_saddr.sin6_addr))
|
|
|
|
return (NULL);
|
|
|
|
if (getnameinfo((struct sockaddr *)&rai->rai_saddr,
|
|
|
|
rai->rai_saddr.sin6_len, hbuf, sizeof(hbuf), NULL, 0,
|
|
|
|
NI_NUMERICHOST) != 0)
|
|
|
|
return (NULL);
|
|
|
|
sprintf(rsid, "%s:%s:[%s]", ifname, origin, hbuf);
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
2011-09-12 23:55:23 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__, "rsid = [%s]", rsid);
|
|
|
|
return (rsid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ra_opt_rdnss_dispatch(struct ifinfo *ifi,
|
|
|
|
struct rainfo *rai,
|
|
|
|
struct script_msg_head_t *sm_rdnss_head,
|
|
|
|
struct script_msg_head_t *sm_dnssl_head)
|
|
|
|
{
|
|
|
|
const char *r;
|
|
|
|
struct script_msg *smp1;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = 0;
|
2011-06-06 03:06:43 +00:00
|
|
|
/* Add \n for DNSSL list. */
|
2011-09-12 23:55:23 +00:00
|
|
|
if (!TAILQ_EMPTY(sm_dnssl_head)) {
|
|
|
|
ELM_MALLOC(smp1, goto ra_opt_rdnss_freeit);
|
2011-06-06 03:06:43 +00:00
|
|
|
smp1->sm_msg = resstr_nl;
|
2011-09-12 23:55:23 +00:00
|
|
|
TAILQ_INSERT_TAIL(sm_dnssl_head, smp1, sm_next);
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
2011-09-12 23:55:23 +00:00
|
|
|
TAILQ_CONCAT(sm_rdnss_head, sm_dnssl_head, sm_next);
|
|
|
|
|
|
|
|
if (rai != NULL && uflag)
|
|
|
|
r = make_rsid(ifi->ifname, DNSINFO_ORIGIN_LABEL, rai);
|
|
|
|
else
|
|
|
|
r = make_rsid(ifi->ifname, DNSINFO_ORIGIN_LABEL, NULL);
|
|
|
|
if (r == NULL) {
|
|
|
|
warnmsg(LOG_ERR, __func__, "make_rsid() failed. "
|
|
|
|
"Script was not invoked.");
|
|
|
|
error = 1;
|
|
|
|
goto ra_opt_rdnss_freeit;
|
|
|
|
}
|
|
|
|
if (!TAILQ_EMPTY(sm_rdnss_head))
|
|
|
|
CALL_SCRIPT(RESADD, sm_rdnss_head);
|
2011-06-08 16:03:29 +00:00
|
|
|
else if (ifi->ifi_rdnss == IFI_DNSOPT_STATE_RECEIVED ||
|
|
|
|
ifi->ifi_dnssl == IFI_DNSOPT_STATE_RECEIVED) {
|
2011-06-06 03:06:43 +00:00
|
|
|
CALL_SCRIPT(RESDEL, NULL);
|
2011-06-08 16:03:29 +00:00
|
|
|
ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
|
|
|
|
ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
|
2011-09-12 23:55:23 +00:00
|
|
|
ra_opt_rdnss_freeit:
|
2011-06-06 03:06:43 +00:00
|
|
|
/* Clear script message queue. */
|
2011-09-12 23:55:23 +00:00
|
|
|
if (!TAILQ_EMPTY(sm_rdnss_head)) {
|
|
|
|
while ((smp1 = TAILQ_FIRST(sm_rdnss_head)) != NULL) {
|
|
|
|
TAILQ_REMOVE(sm_rdnss_head, smp1, sm_next);
|
2011-06-06 03:06:43 +00:00
|
|
|
free(smp1);
|
|
|
|
}
|
|
|
|
}
|
2011-09-12 23:55:23 +00:00
|
|
|
if (!TAILQ_EMPTY(sm_dnssl_head)) {
|
|
|
|
while ((smp1 = TAILQ_FIRST(sm_dnssl_head)) != NULL) {
|
|
|
|
TAILQ_REMOVE(sm_dnssl_head, smp1, sm_next);
|
2011-06-08 16:03:29 +00:00
|
|
|
free(smp1);
|
|
|
|
}
|
|
|
|
}
|
2011-09-12 23:55:23 +00:00
|
|
|
return (error);
|
2011-06-06 03:06:43 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 16:03:29 +00:00
|
|
|
static struct ra_opt *
|
|
|
|
find_raopt(struct rainfo *rai, int type, void *msg, size_t len)
|
|
|
|
{
|
|
|
|
struct ra_opt *rao;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
|
|
|
|
if (rao->rao_type == type &&
|
|
|
|
rao->rao_len == strlen(msg) &&
|
|
|
|
memcmp(rao->rao_msg, msg, len) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rao);
|
|
|
|
}
|
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
static void
|
2011-09-12 23:55:23 +00:00
|
|
|
call_script(const int argc, const char *const argv[],
|
|
|
|
struct script_msg_head_t *sm_head)
|
2003-08-08 16:42:37 +00:00
|
|
|
{
|
2011-06-06 03:06:43 +00:00
|
|
|
const char *scriptpath;
|
|
|
|
int fd[2];
|
|
|
|
int error;
|
2003-08-08 16:42:37 +00:00
|
|
|
pid_t pid, wpid;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
if ((scriptpath = argv[0]) == NULL)
|
2003-08-08 16:42:37 +00:00
|
|
|
return;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
fd[0] = fd[1] = -1;
|
|
|
|
if (sm_head != NULL && !TAILQ_EMPTY(sm_head)) {
|
|
|
|
error = pipe(fd);
|
|
|
|
if (error) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"failed to create a pipe: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-08 16:42:37 +00:00
|
|
|
/* launch the script */
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"failed to fork: %s", strerror(errno));
|
|
|
|
return;
|
2011-06-06 03:06:43 +00:00
|
|
|
} else if (pid) { /* parent */
|
2003-08-08 16:42:37 +00:00
|
|
|
int wstatus;
|
|
|
|
|
2011-06-06 03:06:43 +00:00
|
|
|
if (fd[0] != -1) { /* Send message to the child if any. */
|
|
|
|
ssize_t len;
|
|
|
|
struct script_msg *smp;
|
|
|
|
|
|
|
|
close(fd[0]);
|
|
|
|
TAILQ_FOREACH(smp, sm_head, sm_next) {
|
|
|
|
len = strlen(smp->sm_msg);
|
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
|
|
|
"write to child = %s(%zd)",
|
|
|
|
smp->sm_msg, len);
|
|
|
|
if (write(fd[1], smp->sm_msg, len) != len) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"write to child failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd[1]);
|
|
|
|
}
|
2003-08-08 16:42:37 +00:00
|
|
|
do {
|
|
|
|
wpid = wait(&wstatus);
|
|
|
|
} while (wpid != pid && wpid > 0);
|
|
|
|
|
|
|
|
if (wpid < 0)
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"wait: %s", strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
else
|
2003-08-08 16:42:37 +00:00
|
|
|
warnmsg(LOG_DEBUG, __func__,
|
|
|
|
"script \"%s\" terminated", scriptpath);
|
2011-06-06 03:06:43 +00:00
|
|
|
} else { /* child */
|
|
|
|
int nullfd;
|
|
|
|
char **_argv;
|
2003-08-08 16:42:37 +00:00
|
|
|
|
|
|
|
if (safefile(scriptpath)) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"script \"%s\" cannot be executed safely",
|
|
|
|
scriptpath);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
nullfd = open("/dev/null", O_RDWR);
|
|
|
|
if (nullfd < 0) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"open /dev/null: %s", strerror(errno));
|
|
|
|
exit(1);
|
2003-08-08 16:42:37 +00:00
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
if (fd[0] != -1) { /* Receive message from STDIN if any. */
|
|
|
|
close(fd[1]);
|
|
|
|
if (fd[0] != STDIN_FILENO) {
|
|
|
|
/* Connect a pipe read-end to child's STDIN. */
|
|
|
|
if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"dup2 STDIN: %s", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd[0]);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
dup2(nullfd, STDIN_FILENO);
|
|
|
|
|
|
|
|
dup2(nullfd, STDOUT_FILENO);
|
|
|
|
dup2(nullfd, STDERR_FILENO);
|
|
|
|
if (nullfd > STDERR_FILENO)
|
|
|
|
close(nullfd);
|
|
|
|
|
|
|
|
_argv = malloc(sizeof(*_argv) * argc);
|
|
|
|
if (_argv == NULL) {
|
|
|
|
warnmsg(LOG_ERR, __func__,
|
|
|
|
"malloc: %s", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
memcpy(_argv, argv, (size_t)argc);
|
|
|
|
execv(scriptpath, (char *const *)_argv);
|
2003-08-08 16:42:37 +00:00
|
|
|
warnmsg(LOG_ERR, __func__, "child: exec failed: %s",
|
|
|
|
strerror(errno));
|
2011-06-06 03:06:43 +00:00
|
|
|
exit(1);
|
2003-08-08 16:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-01-14 17:16:19 +00:00
|
|
|
safefile(const char *path)
|
2003-08-08 16:42:37 +00:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
uid_t myuid;
|
|
|
|
|
|
|
|
/* no setuid */
|
|
|
|
if (getuid() != geteuid()) {
|
|
|
|
warnmsg(LOG_NOTICE, __func__,
|
|
|
|
"setuid'ed execution not allowed\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lstat(path, &s) != 0) {
|
|
|
|
warnmsg(LOG_NOTICE, __func__, "lstat failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the file must be owned by the running uid */
|
|
|
|
myuid = getuid();
|
|
|
|
if (s.st_uid != myuid) {
|
|
|
|
warnmsg(LOG_NOTICE, __func__,
|
|
|
|
"%s has invalid owner uid\n", path);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s.st_mode & S_IFMT) {
|
|
|
|
case S_IFREG:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
warnmsg(LOG_NOTICE, __func__,
|
|
|
|
"%s is an invalid file type 0x%o\n",
|
|
|
|
path, (s.st_mode & S_IFMT));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2011-06-06 03:06:43 +00:00
|
|
|
|
|
|
|
/* Decode domain name label encoding in RFC 1035 Section 3.1 */
|
|
|
|
static size_t
|
|
|
|
dname_labeldec(char *dst, size_t dlen, const char *src)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char *src_origin;
|
|
|
|
const char *src_last;
|
|
|
|
const char *dst_origin;
|
|
|
|
|
|
|
|
src_origin = src;
|
|
|
|
src_last = strchr(src, '\0');
|
|
|
|
dst_origin = dst;
|
|
|
|
memset(dst, '\0', dlen);
|
|
|
|
while (src && (len = (uint8_t)(*src++) & 0x3f) &&
|
|
|
|
(src + len) <= src_last) {
|
|
|
|
if (dst != dst_origin)
|
|
|
|
*dst++ = '.';
|
|
|
|
warnmsg(LOG_DEBUG, __func__, "labellen = %zd", len);
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
src += len;
|
|
|
|
dst += len;
|
|
|
|
}
|
|
|
|
*dst = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX validate that domain name only contains valid characters
|
|
|
|
* for two reasons: 1) correctness, 2) we do not want to pass
|
|
|
|
* possible malicious, unescaped characters like `` to a script
|
|
|
|
* or program that could be exploited that way.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (src - src_origin);
|
|
|
|
}
|