dhcpv4: improve error when a prefix is too long
[project/odhcpd.git] / src / dhcpv4.c
index 24a4fea08bb0df787142dae6574671a7258a44cd..3191ff2cc473d53b2e296daf8afbb76e029c97cb 100644 (file)
@@ -37,6 +37,7 @@
 
 #define PACKET_SIZE(start, end) (((uint8_t *)end - (uint8_t *)start) < DHCPV4_MIN_PACKET_SIZE ? \
                                 DHCPV4_MIN_PACKET_SIZE : (uint8_t *)end - (uint8_t *)start)
+#define MAX_PREFIX_LEN 28
 
 static void dhcpv4_netevent_cb(unsigned long event, struct netevent_handler_info *info);
 static int setup_dhcpv4_addresses(struct interface *iface);
@@ -248,9 +249,9 @@ static int setup_dhcpv4_addresses(struct interface *iface)
                }
        }
 
-       /* Don't allocate IP range for subnets bigger than 28 */
-       if (iface->addr4[0].prefix > 28) {
-               syslog(LOG_WARNING, "Auto allocation of DHCP range fails on %s", iface->name);
+       /* Don't allocate IP range for subnets smaller than /28 */
+       if (iface->addr4[0].prefix > MAX_PREFIX_LEN) {
+               syslog(LOG_WARNING, "Auto allocation of DHCP range fails on %s (prefix length must be < %d).", iface->name, MAX_PREFIX_LEN + 1);
                return -1;
        }
 
@@ -588,9 +589,27 @@ static void dhcpv4_fr_stop(struct dhcp_assignment *a)
        a->fr_timer.cb = NULL;
 }
 
+static int dhcpv4_send_reply(const void *buf, size_t len,
+                            const struct sockaddr *dest, socklen_t dest_len,
+                            void *opaque)
+{
+       int *sock = opaque;
+
+       return sendto(*sock, buf, len, MSG_DONTWAIT, dest, dest_len);
+}
+
 /* Handler for DHCPv4 messages */
 static void handle_dhcpv4(void *addr, void *data, size_t len,
                struct interface *iface, _unused void *dest_addr)
+{
+       int sock = iface->dhcpv4_event.uloop.fd;
+
+       dhcpv4_handle_msg(addr, data, len, iface, dest_addr, dhcpv4_send_reply, &sock);
+}
+
+void dhcpv4_handle_msg(void *addr, void *data, size_t len,
+               struct interface *iface, _unused void *dest_addr,
+               send_reply_cb_t send_reply, void *opaque)
 {
        struct dhcpv4_message *req = data;
 
@@ -740,7 +759,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
 #ifdef WITH_UBUS
        if (reqmsg == DHCPV4_MSG_RELEASE)
                ubus_bcast_dhcp_event("dhcp.release", req->chaddr, req->hlen,
-                                       &req->ciaddr, hostname, iface->ifname);
+                                       &req->ciaddr, a ? a->hostname : NULL, iface->ifname);
 #endif
        if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
                return;
@@ -824,12 +843,21 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                                4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
 
 
-       if (iface->dhcpv4_dns_cnt == 0)
-               dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &iface->dhcpv4_local);
-       else
+       if (iface->dhcpv4_dns_cnt == 0) {
+               if (iface->dns_service)
+                       dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &iface->dhcpv4_local);
+       } else
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
                                4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
 
+       if (a && a->reqopts && iface->dhcpv4_ntp_cnt != 0) {
+               for(size_t opts = 0; a->reqopts[opts]; opts++) {
+                       if (a->reqopts[opts] == DHCPV4_OPT_NTPSERVER) {
+                               dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NTPSERVER,
+                                               4 * iface->dhcpv4_ntp_cnt, iface->dhcpv4_ntp);
+                       }
+               }
+       }
 
        dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
 
@@ -870,16 +898,18 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
                dest.sin_addr = reply.yiaddr;
                dest.sin_port = htons(DHCPV4_CLIENT_PORT);
 
-               memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
-               memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
-               memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
+               if (!(iface->ifflags & IFF_NOARP)) {
+                       memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
+                       memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
+                       memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
 
-               if (ioctl(sock, SIOCSARP, &arp) < 0)
-                       syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
+                       if (ioctl(sock, SIOCSARP, &arp) < 0)
+                               syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
+               }
        }
 
-       if (sendto(sock, &reply, PACKET_SIZE(&reply, cookie), MSG_DONTWAIT,
-                       (struct sockaddr*)&dest, sizeof(dest)) < 0)
+       if (send_reply(&reply, PACKET_SIZE(&reply, cookie),
+                      (struct sockaddr*)&dest, sizeof(dest), opaque) < 0)
                syslog(LOG_ERR, "Failed to send %s to %s - %s: %m",
                        dhcpv4_msg_to_string(msg),
                        dest.sin_addr.s_addr == INADDR_BROADCAST ?
@@ -896,7 +926,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
 #ifdef WITH_UBUS
        if (msg == DHCPV4_MSG_ACK)
                ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, req->hlen, &reply.yiaddr,
-                                       hostname, iface->ifname);
+                                       a ? a->hostname : NULL, iface->ifname);
 #endif
 }
 
@@ -1081,8 +1111,7 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
                                a->flags &= ~OAF_BOUND;
 
                                *incl_fr_opt = accept_fr_nonce;
-                               if (!(a->flags & OAF_STATIC))
-                                       a->valid_until = now;
+                               a->valid_until = now;
                        } else {
                                if ((!(a->flags & OAF_STATIC) || !a->hostname) && hostname_len > 0) {
                                        a->hostname = realloc(a->hostname, hostname_len + 1);
@@ -1113,8 +1142,7 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
                                } else
                                        *incl_fr_opt = false;
 
-                               if (!(a->flags & OAF_STATIC))
-                                       a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
+                               a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
                        }
                } else if (!assigned && a) {
                        /* Cleanup failed assignment */
@@ -1124,17 +1152,16 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
 
        } else if (msg == DHCPV4_MSG_RELEASE && a) {
                a->flags &= ~OAF_BOUND;
-
-               if (!(a->flags & OAF_STATIC))
-                       a->valid_until = now - 1;
+               a->valid_until = now - 1;
 
        } else if (msg == DHCPV4_MSG_DECLINE && a) {
                a->flags &= ~OAF_BOUND;
 
-               if (!(a->flags & OAF_STATIC)) {
+               if (!(a->flags & OAF_STATIC) || a->lease->ipaddr != a->addr) {
                        memset(a->hwaddr, 0, sizeof(a->hwaddr));
                        a->valid_until = now + 3600; /* Block address for 1h */
-               }
+               } else
+                       a->valid_until = now - 1;
        }
 
        dhcpv6_ia_write_statefile();