sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
if (sock < 0)
- return -1;
+ goto failure;
// Detect interface
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
- return -1;
+ goto failure;
ifindex = ifr.ifr_ifindex;
// Configure IPv6-options
int val = 1;
- setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
- setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val));
- setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)) < 0)
+ goto failure;
+
+ if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
+ goto failure;
+
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val, sizeof(val)) < 0)
+ goto failure;
+
+ if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)) < 0)
+ goto failure;
struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
.sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)) < 0)
- return -1;
+ goto failure;
return 0;
+
+failure:
+ if (sock >= 0)
+ close(sock);
+
+ return -1;
}
enum {
// Set timeout for receiving
uint64_t t = round_end - round_start;
struct timeval tv = {t / 1000, (t % 1000) * 1000};
- setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
- &tv, sizeof(tv));
+ if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
+ &tv, sizeof(tv)) < 0)
+ syslog(LOG_ERR, "setsockopt SO_RCVTIMEO failed (%s)",
+ strerror(errno));
// Receive cycle
len = recvmsg(sock, &msg, 0);
*
*/
+#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
const pid_t ourpid = getpid();
sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
if (sock < 0)
- return -1;
+ goto failure;
if_index = if_nametoindex(ifname);
if (!if_index)
- return -1;
+ goto failure;
strncpy(if_name, ifname, sizeof(if_name) - 1);
lladdr = *ifid;
rtnl = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
if (rtnl < 0)
- return -1;
+ goto failure;
struct sockaddr_nl rtnl_kernel = { .nl_family = AF_NETLINK };
if (connect(rtnl, (const struct sockaddr*)&rtnl_kernel, sizeof(rtnl_kernel)) < 0)
- return -1;
+ goto failure;
int val = RTNLGRP_LINK;
- setsockopt(rtnl, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &val, sizeof(val));
- fcntl(rtnl, F_SETOWN, ourpid);
- fcntl(rtnl, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
+ if (setsockopt(rtnl, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &val, sizeof(val)) < 0)
+ goto failure;
+
+ if (fcntl(rtnl, F_SETOWN, ourpid) < 0)
+ goto failure;
+
+ if (fcntl(rtnl, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC) < 0)
+ goto failure;
struct {
struct nlmsghdr hdr;
.hdr = {sizeof(req), RTM_GETLINK, NLM_F_REQUEST, 1, 0},
.ifi = {.ifi_index = if_index}
};
- send(rtnl, &req, sizeof(req), 0);
+ if (send(rtnl, &req, sizeof(req), 0) < 0)
+ goto failure;
+
ra_link_up();
// Filter ICMPv6 package types
struct icmp6_filter filt;
ICMP6_FILTER_SETBLOCKALL(&filt);
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
- setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
+ if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt)) < 0)
+ goto failure;
// Bind to all-nodes
struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
- setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an));
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an)) < 0)
+ goto failure;
// Let the kernel compute our checksums
val = 2;
- setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
+ if (setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val)) < 0)
+ goto failure;
// This is required by RFC 4861
val = 255;
- setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) < 0)
+ goto failure;
// Receive multicast hops
val = 1;
- setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val));
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val)) < 0)
+ goto failure;
// Bind to one device
- setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
+ if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)) < 0)
+ goto failure;
// Add async-mode
- fcntl(sock, F_SETOWN, ourpid);
- fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
+ if (fcntl(sock, F_SETOWN, ourpid) < 0)
+ goto failure;
+
+ val = fcntl(sock, F_GETFL);
+ if (val < 0)
+ goto failure;
+
+ if (fcntl(sock, F_SETFL, val | O_ASYNC) < 0)
+ goto failure;
// Send RS
signal(SIGALRM, ra_send_rs);
ra_send_rs(SIGALRM);
return 0;
+
+failure:
+ if (sock >= 0)
+ close(sock);
+
+ if (rtnl >= 0)
+ close(rtnl);
+
+ return -1;
}
static void ra_send_rs(int signal __attribute__((unused)))
else
len = sizeof(struct icmp6_hdr);
- sendto(sock, &rs, len, MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest));
+ if (sendto(sock, &rs, len, MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
+ syslog(LOG_ERR, "Failed to send RS (%s)", strerror(errno));
if (++rs_attempt <= 3)
alarm(4);