-/* $OpenBSD: if.c,v 1.39 2016/03/01 20:51:05 jca Exp $ */
+/* $OpenBSD: if.c,v 1.40 2016/06/29 14:19:38 jca Exp $ */
/* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */
/*
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+
#include "rtadvd.h"
#include "if.h"
#include "log.h"
(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
#define NEXT_SA(ap) (ap) = (struct sockaddr *) \
- ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
+ ((char *)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
sizeof(u_long)) :\
sizeof(u_long)))
struct if_msghdr **iflist;
-int iflist_init_ok;
-size_t ifblock_size;
-char *ifblock;
-
static void get_iflist(char **buf, size_t *size);
static void parse_iflist(struct if_msghdr ***ifmlist_p, char *buf,
size_t bufsize);
int
if_getmtu(char *name)
{
- struct ifaddrs *ifap, *ifa;
- struct if_data *ifd;
- u_long mtu = 0;
-
- if (getifaddrs(&ifap) < 0)
- return(0);
- for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
- if (strcmp(ifa->ifa_name, name) == 0) {
- ifd = ifa->ifa_data;
- if (ifd)
- mtu = ifd->ifi_mtu;
- break;
- }
- }
- freeifaddrs(ifap);
-
-#ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
- if (mtu == 0) {
- struct ifreq ifr;
- int s;
-
- if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
- return(0);
+ int s;
+ struct ifreq ifr;
+ u_long mtu = 0;
+ if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0) {
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_addr.sa_family = AF_INET6;
if (strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)) >=
sizeof(ifr.ifr_name))
fatalx("strlcpy");
- if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
- close(s);
- return(0);
- }
+ if (ioctl(s, SIOCGIFMTU, (char *)&ifr) >= 0)
+ mtu = ifr.ifr_mtu;
close(s);
-
- mtu = ifr.ifr_mtu;
}
-#endif
- return(mtu);
+ return (mtu);
}
/* give interface index and its old flags, then new flags returned */
}
if_indextoname(ifindex, ifr.ifr_name);
- if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
+ if (ioctl(s, SIOCGIFFLAGS, (char *)&ifr) < 0) {
log_warn("ioctl:SIOCGIFFLAGS: failed for %s", ifr.ifr_name);
close(s);
return (oifflags & ~IFF_UP);
switch (sdl->sdl_type) {
case IFT_CARP:
case IFT_ETHER:
- case IFT_FDDI:
return(ROUNDUP8(ETHER_ADDR_LEN + 2));
default:
return(0);
switch (sdl->sdl_type) {
case IFT_CARP:
case IFT_ETHER:
- case IFT_FDDI:
ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
addr = (char *)(ndopt + 1);
memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
void
init_iflist(void)
{
+ static size_t ifblock_size;
+ static char *ifblock;
+
if (ifblock) {
free(ifblock);
ifblock_size = 0;
-/* $OpenBSD: rtadvd.c,v 1.73 2016/03/01 20:51:05 jca Exp $ */
+/* $OpenBSD: rtadvd.c,v 1.74 2016/06/29 14:19:38 jca Exp $ */
/* $KAME: rtadvd.c,v 1.66 2002/05/29 14:18:36 itojun Exp $ */
/*
#include <net/if.h>
#include <net/route.h>
-#include <net/if_dl.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
static void ra_output(struct rainfo *);
static void rtmsg_input(void);
static void rtadvd_set_dump(int);
+static struct rainfo *if_indextorainfo(int);
int
main(int argc, char *argv[])
sock_open();
- if (sflag == 0) {
+ if (sflag == 0)
rtsock_open();
- }
if ((pw = getpwnam(RTADVD_USER)) == NULL)
fatal("getpwnam(" RTADVD_USER ")");
int i;
const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
- if (dflag)
- log_debug("cease to be an advertising router");
+ log_debug("cease to be an advertising router");
SLIST_FOREACH(ra, &ralist, entry) {
ra->lifetime = 0;
char addrbuf[INET6_ADDRSTRLEN];
n = read(rtsock, msg, sizeof(msg));
- if (dflag)
- log_debug("received a routing message "
- "(type = %d, len = %d)", rtmsg_type(msg), n);
+ log_debug("received a routing message "
+ "(type = %d, len = %d)", rtmsg_type(msg), n);
if (n > rtmsg_len(msg)) {
/*
* This usually won't happen for messages received on
* a routing socket.
*/
- if (dflag)
- log_debug("received data length is larger than "
- "1st routing message len. multiple messages? "
- "read %d bytes, but 1st msg len = %d",
- n, rtmsg_len(msg));
+ log_debug("received data length is larger than "
+ "1st routing message len. multiple messages? "
+ "read %d bytes, but 1st msg len = %d",
+ n, rtmsg_len(msg));
#if 0
/* adjust length */
n = rtmsg_len(msg);
break;
default:
/* should not reach here */
- if (dflag)
- log_debug("unknown rtmsg %d on %s",
- type, if_indextoname(ifindex, ifname));
+ log_debug("unknown rtmsg %d on %s",
+ type, if_indextoname(ifindex, ifname));
continue;
}
if ((rai = if_indextorainfo(ifindex)) == NULL) {
- if (dflag)
- log_debug("route changed on "
- "non advertising interface(%s)",
- if_indextoname(ifindex, ifname));
+ log_debug("route changed on "
+ "non advertising interface(%s)",
+ if_indextoname(ifindex, ifname));
continue;
}
oldifflags = iflist[ifindex]->ifm_flags;
}
prefix = find_prefix(rai, addr, plen);
if (prefix) {
- if (dflag)
- log_debug("new prefix(%s/%d) "
- "added on %s, "
- "but it was already in list",
- inet_ntop(AF_INET6, addr,
- (char *)addrbuf, INET6_ADDRSTRLEN),
- plen, rai->ifname);
+ log_debug("new prefix(%s/%d) "
+ "added on %s, "
+ "but it was already in list",
+ inet_ntop(AF_INET6, addr,
+ addrbuf, INET6_ADDRSTRLEN),
+ plen, rai->ifname);
break;
}
make_prefix(rai, ifindex, addr, plen);
}
prefix = find_prefix(rai, addr, plen);
if (prefix == NULL) {
- if (dflag)
- log_debug("prefix(%s/%d) was "
- "deleted on %s, "
- "but it was not in list",
- inet_ntop(AF_INET6, addr,
- (char *)addrbuf, INET6_ADDRSTRLEN),
- plen, rai->ifname);
+ log_debug("prefix(%s/%d) was "
+ "deleted on %s, "
+ "but it was not in list",
+ inet_ntop(AF_INET6, addr,
+ addrbuf, INET6_ADDRSTRLEN),
+ plen, rai->ifname);
break;
}
delete_prefix(rai, prefix);
break;
default:
/* should not reach here */
- if (dflag)
- log_debug("unknown rtmsg %d on %s",
- type, if_indextoname(ifindex, ifname));
+ log_debug("unknown rtmsg %d on %s",
+ type, if_indextoname(ifindex, ifname));
return;
}
rai->waiting = 0; /* XXX */
rai->timer = rtadvd_add_timer(ra_timeout,
ra_timer_update, rai, rai);
- ra_timer_update((void *)rai, &rai->timer->tm);
+ ra_timer_update(rai, &rai->timer->tm);
rtadvd_set_timer(&rai->timer->tm, rai->timer);
}
}
bytelen = plen / 8;
bitlen = plen % 8;
bitmask = 0xff << (8 - bitlen);
- if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
+ if (memcmp(prefix, &pp->prefix, bytelen))
continue;
if (bitlen == 0 ||
((prefix->s6_addr[bytelen] & bitmask) ==
goto bad;
}
- hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
+ hdr = (struct nd_opt_hdr *)((char *)hdr + optlen);
if (hdr->nd_opt_len == 0) {
log_warnx("bad ND option length(0) (type = %d)",
hdr->nd_opt_type);
}
}
-void
+static void
sock_open(void)
{
struct rainfo *ra;
*/
if (inet_pton(AF_INET6, ALLROUTERS_LINK, &mreq.ipv6mr_multiaddr.s6_addr)
!= 1)
- fatal("inet_pton failed(library bug?)");
+ fatal("inet_pton");
SLIST_FOREACH(ra, &ralist, entry) {
mreq.ipv6mr_interface = ra->ifindex;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
}
}
- ra = SLIST_FIRST(&ralist);
-
/* initialize msghdr for receiving packets */
- rcviov[0].iov_base = (caddr_t)answer;
+ rcviov[0].iov_base = answer;
rcviov[0].iov_len = sizeof(answer);
- rcvmhdr.msg_name = (caddr_t)&from;
+ rcvmhdr.msg_name = &from;
rcvmhdr.msg_namelen = sizeof(from);
rcvmhdr.msg_iov = rcviov;
rcvmhdr.msg_iovlen = 1;
- rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
+ rcvmhdr.msg_control = rcvcmsgbuf;
rcvmhdr.msg_controllen = rcvcmsgbuflen;
/* 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;
+ sndmhdr.msg_control = sndcmsgbuf;
sndmhdr.msg_controllen = sndcmsgbuflen;
}
fatal("setsockopt(ROUTE_MSGFILTER)");
}
-struct rainfo *
+static struct rainfo *
if_indextorainfo(int index)
{
struct rainfo *rai;
static void
ra_output(struct rainfo *rainfo)
{
- int i;
struct cmsghdr *cm;
struct in6_pktinfo *pi;
+ size_t len;
if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
log_debug("%s is not up, skip sending RA", rainfo->ifname);
make_packet(rainfo); /* XXX: inefficient */
- sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
- sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
+ sndmhdr.msg_name = &sin6_allnodes;
+ sndmhdr.msg_iov[0].iov_base = rainfo->ra_data;
sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
cm = CMSG_FIRSTHDR(&sndmhdr);
memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
}
- log_debug("send RA on %s, # of waitings = %d",
+ log_debug("send RA on %s, # of waitings = %u",
rainfo->ifname, rainfo->waiting);
- i = sendmsg(sock, &sndmhdr, 0);
+ len = sendmsg(sock, &sndmhdr, 0);
- if (i < 0 || i != rainfo->ra_datalen)
- if (i < 0)
- log_warn("sendmsg on %s", rainfo->ifname);
+ if (len < 0)
+ log_warn("sendmsg on %s", rainfo->ifname);
/* update counter */
if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
{
struct rainfo *rai = (struct rainfo *)data;
-#ifdef notyet
- /* if necessary, reconstruct the packet. */
-#endif
-
log_debug("RA timer on %s is expired", rai->ifname);
ra_output(rai);