Use 1 socket per interface for DHCPv6
authorSteven Barth <steven@midlink.org>
Sat, 4 Jan 2014 09:14:59 +0000 (10:14 +0100)
committerSteven Barth <steven@midlink.org>
Sat, 4 Jan 2014 09:14:59 +0000 (10:14 +0100)
src/dhcpv6-ia.c
src/dhcpv6.c
src/dhcpv6.h
src/odhcpd.h

index e5a7c0bfff4708b6a7acc3136c315ae76f229dc9..f933da695aecd4d17de7e4e05b52bb88a0b0c93e 100644 (file)
 static void update(struct interface *iface);
 static void reconf_timer(struct uloop_timeout *event);
 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
-static int socket_fd = -1;
 static uint32_t serial = 0;
 
 
-int dhcpv6_ia_init(int dhcpv6_socket)
+int dhcpv6_ia_init(void)
 {
-       socket_fd = dhcpv6_socket;
        uloop_timeout_set(&reconf_event, 2000);
        return 0;
 }
@@ -183,7 +181,7 @@ static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign
        md5_hash(reconf_msg.auth.key, 16, &md5);
        md5_end(reconf_msg.auth.key, &md5);
 
-       return odhcpd_send(socket_fd, &assign->peer, &iov, 1, iface);
+       return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
 }
 
 
index 7560a75bc961f7a44941a38f3ab92915c99786d6..8f8f4356774029e367c9e3caf765a446f187dffb 100644 (file)
@@ -32,68 +32,68 @@ static void handle_dhcpv6(void *addr, void *data, size_t len,
 static void handle_client_request(void *addr, void *data, size_t len,
                struct interface *iface);
 
-static struct odhcpd_event dhcpv6_event = {{.fd = -1}, handle_dhcpv6};
-
 
 
 // Create socket and register events
 int init_dhcpv6(void)
 {
-       int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
-       if (sock < 0) {
-               syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s",
-                               strerror(errno));
-               return -1;
-       }
-
-       // Basic IPv6 configuration
-       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));
-
-       val = DHCPV6_HOP_COUNT_LIMIT;
-       setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
-
-       val = 0;
-       setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val));
+       dhcpv6_ia_init();
+       return 0;
+}
 
-       struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
-                               0, IN6ADDR_ANY_INIT, 0};
 
-       if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
-               syslog(LOG_ERR, "Failed to open DHCPv6 server socket: %s",
-                               strerror(errno));
-               return -1;
+int setup_dhcpv6_interface(struct interface *iface, bool enable)
+{
+       if (iface->dhcpv6_event.uloop.fd > 0) {
+               uloop_fd_delete(&iface->dhcpv6_event.uloop);
+               close(iface->dhcpv6_event.uloop.fd);
+               iface->dhcpv6_event.uloop.fd = -1;
        }
 
-       dhcpv6_event.uloop.fd = sock;
-       odhcpd_register(&dhcpv6_event);
+       // Configure multicast settings
+       if (enable && iface->dhcpv6 && !iface->master) {
+               int sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
+               if (sock < 0) {
+                       syslog(LOG_ERR, "Failed to create DHCPv6 server socket: %s",
+                                       strerror(errno));
+                       return -1;
+               }
 
-       dhcpv6_ia_init(dhcpv6_event.uloop.fd);
+               // Basic IPv6 configuration
+               setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, iface->ifname, strlen(iface->ifname));
 
-       return 0;
-}
+               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));
 
+               val = DHCPV6_HOP_COUNT_LIMIT;
+               setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
 
-int setup_dhcpv6_interface(struct interface *iface, bool enable)
-{
-       // Configure multicast settings
-       struct ipv6_mreq relay = {ALL_DHCPV6_RELAYS, iface->ifindex};
-       struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex};
+               val = 0;
+               setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val));
 
-       setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
-                       IPV6_DROP_MEMBERSHIP, &relay, sizeof(relay));
-       setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
-                       IPV6_DROP_MEMBERSHIP, &server, sizeof(server));
+               struct sockaddr_in6 bind_addr = {AF_INET6, htons(DHCPV6_SERVER_PORT),
+                                       0, IN6ADDR_ANY_INIT, 0};
 
-       if (enable && iface->dhcpv6 && !iface->master) {
-               setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
+               if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
+                       syslog(LOG_ERR, "Failed to open DHCPv6 server socket: %s",
+                                       strerror(errno));
+                       return -1;
+               }
+
+               struct ipv6_mreq relay = {ALL_DHCPV6_RELAYS, iface->ifindex};
+               struct ipv6_mreq server = {ALL_DHCPV6_SERVERS, iface->ifindex};
+               setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6,
                                IPV6_ADD_MEMBERSHIP, &relay, sizeof(relay));
 
                if (iface->dhcpv6 == RELAYD_SERVER)
-                       setsockopt(dhcpv6_event.uloop.fd, IPPROTO_IPV6,
+                       setsockopt(iface->dhcpv6_event.uloop.fd, IPPROTO_IPV6,
                                        IPV6_ADD_MEMBERSHIP, &server, sizeof(server));
+
+               iface->dhcpv6_event.uloop.fd = sock;
+               iface->dhcpv6_event.handle_dgram = handle_dhcpv6;
+               odhcpd_register(&iface->dhcpv6_event);
        }
 
        return setup_dhcpv6_ia_interface(iface, enable);
@@ -291,7 +291,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
                                iov[3].iov_len + iov[4].iov_len + iov[5].iov_len +
                                iov[6].iov_len - (4 + opts_end - opts));
 
-       odhcpd_send(dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
+       odhcpd_send(iface->dhcpv6_event.uloop.fd, addr, iov, ARRAY_SIZE(iov), iface);
 }
 
 
@@ -395,7 +395,7 @@ static void relay_server_response(uint8_t *data, size_t len)
        }
 
        struct iovec iov = {payload_data, payload_len};
-       odhcpd_send(dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
+       odhcpd_send(iface->dhcpv6_event.uloop.fd, &target, &iov, 1, iface);
 }
 
 
@@ -452,5 +452,5 @@ static void relay_client_request(struct sockaddr_in6 *source,
        struct sockaddr_in6 dhcpv6_servers = {AF_INET6,
                        htons(DHCPV6_SERVER_PORT), 0, ALL_DHCPV6_SERVERS, 0};
        struct iovec iov[2] = {{&hdr, sizeof(hdr)}, {(void*)data, len}};
-       odhcpd_send(dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);
+       odhcpd_send(iface->dhcpv6_event.uloop.fd, &dhcpv6_servers, iov, 2, master);
 }
index 693bc576bf19b5d0a016f6040520b39aa9171db8..7b3b484dbb5854c5634cad67786a961ae7ca65b1 100644 (file)
@@ -166,6 +166,6 @@ struct dhcpv6_assignment {
 int dhcpv6_init_ia(struct interface *iface, int socket);
 size_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
-int dhcpv6_ia_init(int dhcpv6_socket);
+int dhcpv6_ia_init(void);
 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable);
 void dhcpv6_write_statefile(void);
index 77376cbc5f6ef70a91069d3b423b306b8b3557a7..789f696f139bb197f121c7b1cdff80480e2f5833 100644 (file)
@@ -115,6 +115,7 @@ struct interface {
        bool ia_reconf;
 
        // DHCPv4
+       struct odhcpd_event dhcpv6_event;
        struct odhcpd_event dhcpv4_event;
        struct list_head dhcpv4_assignments;