From 3ee43ee22302fb38f74454c38710a5937afb74e3 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Sun, 14 Jun 2015 21:43:20 +0200 Subject: dhcpv4: read ipv4-address from UBUS to ADDRESS4 Signed-off-by: Patrick Grimm --- src/ubus.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ubus.c b/src/ubus.c index 0b51183..19bd3af 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -163,6 +163,7 @@ enum { IFACE_ATTR_DATA, IFACE_ATTR_PREFIX, IFACE_ATTR_ADDRESS, + IFACE_ATTR_ADDRESS4, IFACE_ATTR_MAX, }; @@ -173,6 +174,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = { [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE }, [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY }, [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY }, + [IFACE_ATTR_ADDRESS4] = { .name = "ipv4-address", .type = BLOBMSG_TYPE_ARRAY }, }; static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg) -- cgit v1.2.3 From e99ca79b04f18831ada51b913a3869804ddf98ee Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Sun, 14 Jun 2015 22:55:50 +0200 Subject: dhcpv4: add ubus_get_address4 ubus_get_mask4 function Signed-off-by: Patrick Grimm --- src/ubus.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/ubus.c b/src/ubus.c index 19bd3af..7e956fa 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -361,6 +361,57 @@ bool ubus_has_prefix(const char *name, const char *ifname) return false; } +struct in_addr ubus_get_address4(const char *name) +{ + struct blob_attr *c; + unsigned rem; + + if (!dump) + return NULL; + + blobmsg_for_each_attr(c, dump, rem) { + struct blob_attr *tb[IFACE_ATTR_MAX]; + blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c)); + + if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name, + blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]))) + continue; + + if (tb[IFACE_ATTR_IFNAME]) { + struct in_addr addr4; + if (inet_pton(AF_INET, blobmsg_get_string(tb[IFACE_ATTR_ADDRESS4]), &addr4) == 1) + return addr4; + } + } + + return NULL; +} + +struct in_addr ubus_get_mask4(const char *name) +{ + struct blob_attr *c; + unsigned rem; + + if (!dump) + return NULL; + + blobmsg_for_each_attr(c, dump, rem) { + struct blob_attr *tb[IFACE_ATTR_MAX]; + blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c)); + + if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name, + blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]))) + continue; + + if (tb[IFACE_ATTR_IFNAME]) { + struct in_addr mask4; + if (inet_pton(AF_INET, blobmsg_get_string(tb[IFACE_ATTR_MASK4]), &mask4) == 1) + return mask4; + } + } + + return NULL; +} int init_ubus(void) { -- cgit v1.2.3 From cc0763d6edff3ec43a025067f1c8f13a42821a8d Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 15 Jun 2015 00:35:50 +0200 Subject: dhcpv4: add for_each ipv4-address array in ubus_get_address4 ubus_get_mask4 function Signed-off-by: Patrick Grimm --- src/ubus.c | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/ubus.c b/src/ubus.c index 7e956fa..63bfd3c 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -177,6 +177,17 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = { [IFACE_ATTR_ADDRESS4] = { .name = "ipv4-address", .type = BLOBMSG_TYPE_ARRAY }, }; +enum { + ADDR_ATTR_ADDRESS, + ADDR_ATTR_MASK, + ADDR_ATTR_MAX, +}; + +static const struct blobmsg_policy addr_attrs[ADDR_ATTR_MAX] = { + [ADDR_ATTR_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_ARRAY }, + [ADDR_ATTR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 }, +}; + static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg) { struct blob_attr *tb[DUMP_ATTR_MAX]; @@ -363,7 +374,7 @@ bool ubus_has_prefix(const char *name, const char *ifname) struct in_addr ubus_get_address4(const char *name) { - struct blob_attr *c; + struct blob_attr *c, *cur; unsigned rem; if (!dump) @@ -377,10 +388,19 @@ struct in_addr ubus_get_address4(const char *name) blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]))) continue; - if (tb[IFACE_ATTR_IFNAME]) { - struct in_addr addr4; - if (inet_pton(AF_INET, blobmsg_get_string(tb[IFACE_ATTR_ADDRESS4]), &addr4) == 1) - return addr4; + if ((cur = tb[IFACE_ATTR_ADDRESS4])) { + if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, NULL)) + continue; + + struct blob_attr *d; + unsigned drem; + blobmsg_for_each_attr(d, cur, drem) { + struct blob_attr *addr[ADDR_ATTR_MAX]; + blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d)); + struct in_addr addr4; + if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_ADDRESS]), &addr4) == 1) + return addr4; + } } } @@ -389,7 +409,7 @@ struct in_addr ubus_get_address4(const char *name) struct in_addr ubus_get_mask4(const char *name) { - struct blob_attr *c; + struct blob_attr *c, *cur; unsigned rem; if (!dump) @@ -403,10 +423,19 @@ struct in_addr ubus_get_mask4(const char *name) blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]))) continue; - if (tb[IFACE_ATTR_IFNAME]) { - struct in_addr mask4; - if (inet_pton(AF_INET, blobmsg_get_string(tb[IFACE_ATTR_MASK4]), &mask4) == 1) - return mask4; + if ((cur = tb[IFACE_ATTR_ADDRESS4])) { + if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, NULL)) + continue; + + struct blob_attr *d; + unsigned drem; + blobmsg_for_each_attr(d, cur, drem) { + struct blob_attr *addr[ADDR_ATTR_MAX]; + blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d)); + struct in_addr addr4; + if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_MASK]), &addr4) == 1) + return addr4; + } } } -- cgit v1.2.3 From 6e6d58c2389c7610e75b5a766af873deb77377b0 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 15 Jun 2015 00:39:32 +0200 Subject: dhcpv4: use ubus for ipv4 dhcp range calculation Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index a657e13..bc3ee31 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -119,18 +119,22 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) } // Create a range if not specified - struct ifreq ifreq; - strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name)); - - struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr; - struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask; + //struct ifreq ifreq; + //strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name)); + + //struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr; + //struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask; + //if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && + // !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) && + // !ioctl(sock, SIOCGIFADDR, &ifreq)) { if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && - !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) && - !ioctl(sock, SIOCGIFADDR, &ifreq)) { - struct in_addr addr = saddr->sin_addr; + !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) { + //struct in_addr addr = saddr->sin_addr; + struct in_addr addr = ubus_get_address4(iface->ifname); - ioctl(sock, SIOCGIFNETMASK, &ifreq); - struct in_addr mask = smask->sin_addr; + //ioctl(sock, SIOCGIFNETMASK, &ifreq); + //struct in_addr mask = smask->sin_addr; + struct in_addr mask = ubus_get_mask4(iface->ifname); uint32_t start = ntohl(iface->dhcpv4_start.s_addr); uint32_t end = ntohl(iface->dhcpv4_end.s_addr); -- cgit v1.2.3 From a278f7fbafe043164204502d1c04632af8cb5dea Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 15 Jun 2015 00:43:28 +0200 Subject: dhcpv4: remove comment code lines Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index bc3ee31..0f613b2 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -119,21 +119,10 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) } // Create a range if not specified - //struct ifreq ifreq; - //strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name)); - - //struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr; - //struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask; - //if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && - // !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) && - // !ioctl(sock, SIOCGIFADDR, &ifreq)) { if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) { - //struct in_addr addr = saddr->sin_addr; struct in_addr addr = ubus_get_address4(iface->ifname); - //ioctl(sock, SIOCGIFNETMASK, &ifreq); - //struct in_addr mask = smask->sin_addr; struct in_addr mask = ubus_get_mask4(iface->ifname); uint32_t start = ntohl(iface->dhcpv4_start.s_addr); -- cgit v1.2.3 From 513951e56d71fded79b2e299e253f9b2154b9483 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 15 Jun 2015 03:36:15 +0200 Subject: dhcpv4: use iface-name Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 0f613b2..99ad658 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -121,9 +121,11 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) // Create a range if not specified if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) { - struct in_addr addr = ubus_get_address4(iface->ifname); - struct in_addr mask = ubus_get_mask4(iface->ifname); + struct in_addr *saddr = ubus_get_address4(iface->name); + struct in_addr addr = { .s_addr = saddr->s_addr } ; + struct in_addr *smask = ubus_get_mask4(iface->name); + struct in_addr mask = { .s_addr = smask->s_addr } ; uint32_t start = ntohl(iface->dhcpv4_start.s_addr); uint32_t end = ntohl(iface->dhcpv4_end.s_addr); @@ -195,9 +197,11 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) // Clean invalid assignments struct dhcpv4_assignment *a, *n; + struct in_addr *smask = ubus_get_mask4(iface->name); + struct in_addr mask = { .s_addr = smask->s_addr } ; list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) { - if ((htonl(a->addr) & smask->sin_addr.s_addr) != - (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) { + if ((htonl(a->addr) & mask.s_addr) != + (iface->dhcpv4_start.s_addr & mask.s_addr)) { list_del(&a->head); free(a); } -- cgit v1.2.3 From 9117bc46f629c515bc686c22a652c9074562b891 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 15 Jun 2015 03:38:09 +0200 Subject: dhcpv4: return pointer Signed-off-by: Patrick Grimm --- src/ubus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ubus.c b/src/ubus.c index 63bfd3c..2b6b1c7 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -372,7 +372,7 @@ bool ubus_has_prefix(const char *name, const char *ifname) return false; } -struct in_addr ubus_get_address4(const char *name) +struct in_addr* ubus_get_address4(const char *name) { struct blob_attr *c, *cur; unsigned rem; @@ -397,7 +397,7 @@ struct in_addr ubus_get_address4(const char *name) blobmsg_for_each_attr(d, cur, drem) { struct blob_attr *addr[ADDR_ATTR_MAX]; blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d)); - struct in_addr addr4; + struct in_addr *addr4; if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_ADDRESS]), &addr4) == 1) return addr4; } @@ -407,7 +407,7 @@ struct in_addr ubus_get_address4(const char *name) return NULL; } -struct in_addr ubus_get_mask4(const char *name) +struct in_addr* ubus_get_mask4(const char *name) { struct blob_attr *c, *cur; unsigned rem; @@ -432,7 +432,7 @@ struct in_addr ubus_get_mask4(const char *name) blobmsg_for_each_attr(d, cur, drem) { struct blob_attr *addr[ADDR_ATTR_MAX]; blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d)); - struct in_addr addr4; + struct in_addr *addr4; if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_MASK]), &addr4) == 1) return addr4; } -- cgit v1.2.3 From f35dce59337a8b0a0acdf362e3f8f092647c9374 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 15 Jun 2015 03:39:59 +0200 Subject: dhcpv4: add ubus_get_address4 ubus_get_mask4 function Signed-off-by: Patrick Grimm --- src/odhcpd.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/odhcpd.h b/src/odhcpd.h index 5944661..82ba46b 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -209,6 +209,8 @@ int init_ubus(void); const char* ubus_get_ifname(const char *name); void ubus_apply_network(void); bool ubus_has_prefix(const char *name, const char *ifname); +struct in_addr* ubus_get_address4(const char *name); +struct in_addr* ubus_get_mask4(const char *name); #endif -- cgit v1.2.3 From d5b2b5353e99552ac68fef21a19fdb9157dcf96b Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Thu, 18 Jun 2015 04:27:24 +0200 Subject: dhcpv4: fix char ubus_get_address4 int ubus_get_mask4 function Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 18 ++++++++++++------ src/odhcpd.h | 4 ++-- src/ubus.c | 33 ++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 99ad658..07ad3ff 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -118,14 +118,22 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) return -1; } + const char* saddr = ubus_get_address4(iface->name); + struct in_addr addr; + inet_pton(AF_INET,saddr, &addr); + int bits = ubus_get_mask4(iface->name); + struct in_addr mask; + if (!(bits < -32 || bits > 32)) { + mask.s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0; + if (bits < 0) + mask.s_addr = ~mask.s_addr; + } + + // Create a range if not specified if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) { - struct in_addr *saddr = ubus_get_address4(iface->name); - struct in_addr addr = { .s_addr = saddr->s_addr } ; - struct in_addr *smask = ubus_get_mask4(iface->name); - struct in_addr mask = { .s_addr = smask->s_addr } ; uint32_t start = ntohl(iface->dhcpv4_start.s_addr); uint32_t end = ntohl(iface->dhcpv4_end.s_addr); @@ -197,8 +205,6 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) // Clean invalid assignments struct dhcpv4_assignment *a, *n; - struct in_addr *smask = ubus_get_mask4(iface->name); - struct in_addr mask = { .s_addr = smask->s_addr } ; list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) { if ((htonl(a->addr) & mask.s_addr) != (iface->dhcpv4_start.s_addr & mask.s_addr)) { diff --git a/src/odhcpd.h b/src/odhcpd.h index 82ba46b..b8cbeb2 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -209,8 +209,8 @@ int init_ubus(void); const char* ubus_get_ifname(const char *name); void ubus_apply_network(void); bool ubus_has_prefix(const char *name, const char *ifname); -struct in_addr* ubus_get_address4(const char *name); -struct in_addr* ubus_get_mask4(const char *name); +const char* ubus_get_address4(const char *name); +int ubus_get_mask4(const char *name); #endif diff --git a/src/ubus.c b/src/ubus.c index 2b6b1c7..e8563bd 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -372,7 +372,7 @@ bool ubus_has_prefix(const char *name, const char *ifname) return false; } -struct in_addr* ubus_get_address4(const char *name) +const char* ubus_get_address4(const char *name) { struct blob_attr *c, *cur; unsigned rem; @@ -395,11 +395,13 @@ struct in_addr* ubus_get_address4(const char *name) struct blob_attr *d; unsigned drem; blobmsg_for_each_attr(d, cur, drem) { - struct blob_attr *addr[ADDR_ATTR_MAX]; - blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d)); - struct in_addr *addr4; - if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_ADDRESS]), &addr4) == 1) - return addr4; + struct blob_attr *ccur; + unsigned ddrem; + struct blob_attr *dict = blobmsg_data(cur); + blobmsg_for_each_attr(ccur, dict, ddrem) { + if (!strcmp(blobmsg_name(ccur), "address")) + return blobmsg_get_string(ccur); + } } } } @@ -407,13 +409,12 @@ struct in_addr* ubus_get_address4(const char *name) return NULL; } -struct in_addr* ubus_get_mask4(const char *name) +int ubus_get_mask4(const char *name) { struct blob_attr *c, *cur; unsigned rem; - if (!dump) - return NULL; + return 0; blobmsg_for_each_attr(c, dump, rem) { struct blob_attr *tb[IFACE_ATTR_MAX]; @@ -430,16 +431,18 @@ struct in_addr* ubus_get_mask4(const char *name) struct blob_attr *d; unsigned drem; blobmsg_for_each_attr(d, cur, drem) { - struct blob_attr *addr[ADDR_ATTR_MAX]; - blobmsg_parse(addr_attrs, ADDR_ATTR_MAX, addr, blobmsg_data(d), blobmsg_data_len(d)); - struct in_addr *addr4; - if (inet_pton(AF_INET, blobmsg_get_string(addr[ADDR_ATTR_MASK]), &addr4) == 1) - return addr4; + struct blob_attr *ccur; + unsigned ddrem; + struct blob_attr *dict = blobmsg_data(cur); + blobmsg_for_each_attr(ccur, dict, ddrem) { + if (!strcmp(blobmsg_name(ccur), "mask")) + return blobmsg_get_u32(ccur); + } } } } - return NULL; + return 0; } int init_ubus(void) -- cgit v1.2.3 From b55ea3757c37a3daf19547fc3c6f437221c79a0c Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Thu, 18 Jun 2015 04:45:45 +0200 Subject: dhcpv4: fix unitialization of mask Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 07ad3ff..f6f4b0a 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -123,11 +123,12 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) inet_pton(AF_INET,saddr, &addr); int bits = ubus_get_mask4(iface->name); struct in_addr mask; - if (!(bits < -32 || bits > 32)) { - mask.s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0; - if (bits < 0) - mask.s_addr = ~mask.s_addr; - } + if (bits < -32 || bits > 32) + bits = 0; + + mask.s_addr = bits ? htonl(~((1 << (32 - abs(bits))) - 1)) : 0; + if (bits < 0) + mask.s_addr = ~mask.s_addr; // Create a range if not specified -- cgit v1.2.3 From ebbff3872a1f242ef5b0a7c029895848876a54b2 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Fri, 19 Jun 2015 01:30:29 +0200 Subject: dhcpv4: add in_addr dhcpv4_mask and in_addr dhcpv4_addr to struct interface Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 34 +++++++++++++++++----------------- src/odhcpd.h | 2 ++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index f6f4b0a..09cca91 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -121,6 +121,7 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) const char* saddr = ubus_get_address4(iface->name); struct in_addr addr; inet_pton(AF_INET,saddr, &addr); + iface->dhcpv4_addr.s_addr = addr.s_addr; int bits = ubus_get_mask4(iface->name); struct in_addr mask; if (bits < -32 || bits > 32) @@ -130,6 +131,7 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) if (bits < 0) mask.s_addr = ~mask.s_addr; + iface->dhcpv4_mask.s_addr = mask.s_addr; // Create a range if not specified if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && @@ -168,6 +170,11 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) } + syslog(LOG_WARNING, "DHCPv4 dhcp addr %s", inet_ntoa(iface->dhcpv4_addr)); + syslog(LOG_WARNING, "DHCPv4 dhcp mask %s", inet_ntoa(iface->dhcpv4_mask)); + syslog(LOG_WARNING, "DHCPv4 dhcp start %s", inet_ntoa(iface->dhcpv4_start)); + syslog(LOG_WARNING, "DHCPv4 dhcp end %s", inet_ntoa(iface->dhcpv4_end)); + // Parse static entries struct lease *lease; @@ -262,10 +269,8 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, return; int sock = iface->dhcpv4_event.uloop.fd; - struct sockaddr_in ifaddr; - struct sockaddr_in ifnetmask; - syslog(LOG_NOTICE, "Got DHCPv4 request"); + syslog(LOG_WARNING, "Got DHCPv4 request"); struct ifreq ifreq; memcpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name)); @@ -274,15 +279,10 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, return; } - memcpy(&ifaddr, &ifreq.ifr_addr, sizeof(ifaddr)); - if (ioctl(sock, SIOCGIFNETMASK, &ifreq)) - return; - - memcpy(&ifnetmask, &ifreq.ifr_netmask, sizeof(ifnetmask)); - uint32_t network = ifaddr.sin_addr.s_addr & ifnetmask.sin_addr.s_addr; + uint32_t network = iface->dhcpv4_addr.s_addr & iface->dhcpv4_mask.s_addr; - if ((iface->dhcpv4_start.s_addr & ifnetmask.sin_addr.s_addr) != network || - (iface->dhcpv4_end.s_addr & ifnetmask.sin_addr.s_addr) != network) { + if ((iface->dhcpv4_start.s_addr & iface->dhcpv4_mask.s_addr) != network || + (iface->dhcpv4_end.s_addr & iface->dhcpv4_mask.s_addr) != network) { syslog(LOG_WARNING, "DHCPv4 range out of assigned network"); return; } @@ -300,7 +300,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, .flags = req->flags, .ciaddr = {INADDR_ANY}, .giaddr = req->giaddr, - .siaddr = ifaddr.sin_addr, + .siaddr = iface->dhcpv4_addr, }; memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr)); @@ -329,7 +329,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) { memcpy(&reqaddr, opt->data, 4); } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) { - if (memcmp(opt->data, &ifaddr.sin_addr, 4)) + if (memcmp(opt->data, &iface->dhcpv4_addr, 4)) return; } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) { uint8_t *c = opt->data, *cend = &opt->data[opt->len]; @@ -381,7 +381,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, return; dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg); - dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &ifaddr.sin_addr); + dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &iface->dhcpv4_addr); if (lease) { reply.yiaddr.s_addr = htonl(lease->addr); @@ -395,7 +395,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, val = htonl(875 * iface->dhcpv4_leasetime / 1000); dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val); - dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr); + dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &iface->dhcpv4_mask); if (lease->hostname[0]) dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME, @@ -425,14 +425,14 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, } if (iface->dhcpv4_router_cnt == 0) - dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &ifaddr.sin_addr); + dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &iface->dhcpv4_addr); else dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router); if (iface->dhcpv4_dns_cnt == 0) - dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &ifaddr.sin_addr); + dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &iface->dhcpv4_addr); else dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns); diff --git a/src/odhcpd.h b/src/odhcpd.h index b8cbeb2..d3a5ad6 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -145,6 +145,8 @@ struct interface { int route_preference; // DHCPv4 + struct in_addr dhcpv4_addr; + struct in_addr dhcpv4_mask; struct in_addr dhcpv4_start; struct in_addr dhcpv4_end; struct in_addr *dhcpv4_router; -- cgit v1.2.3 From 2302172bf89116dbc4b644c08289c0aaa08c8aa8 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 22 Jun 2015 16:16:04 +0200 Subject: dhcpv4: add ifdef ubus for dhcp4 ip Signed-off-by: Patrick Grimm --- src/dhcpv4.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 09cca91..26cd5e1 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -118,6 +118,21 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) return -1; } +#ifndef WITH_UBUS + struct ifreq ifreq; + strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name)); + + struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr; + struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask; + + if (!ioctl(sock, SIOCGIFADDR, &ifreq)) { + struct in_addr addr = saddr->sin_addr; + iface->dhcpv4_addr.s_addr = addr.s_addr; + ioctl(sock, SIOCGIFNETMASK, &ifreq); + struct in_addr mask = smask->sin_addr; + iface->dhcpv4_mask.s_addr = mask.s_addr; + } +#else const char* saddr = ubus_get_address4(iface->name); struct in_addr addr; inet_pton(AF_INET,saddr, &addr); @@ -132,11 +147,12 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable) mask.s_addr = ~mask.s_addr; iface->dhcpv4_mask.s_addr = mask.s_addr; +#endif // Create a range if not specified if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) && - !(iface->dhcpv4_end.s_addr & htonl(0xffff0000))) { - + !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) && + iface->dhcpv4_addr.s_addr) { uint32_t start = ntohl(iface->dhcpv4_start.s_addr); uint32_t end = ntohl(iface->dhcpv4_end.s_addr); @@ -270,7 +286,7 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, int sock = iface->dhcpv4_event.uloop.fd; - syslog(LOG_WARNING, "Got DHCPv4 request"); + syslog(LOG_NOTICE, "Got DHCPv4 request"); struct ifreq ifreq; memcpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name)); -- cgit v1.2.3 From 8f328a9e191908d5c54929ab3172dce61be555e2 Mon Sep 17 00:00:00 2001 From: Patrick Grimm Date: Mon, 22 Jun 2015 16:22:36 +0200 Subject: dhcpv4: remove unused addr_attrs struct Signed-off-by: Patrick Grimm --- src/ubus.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/ubus.c b/src/ubus.c index e8563bd..0ef3153 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -177,17 +177,6 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = { [IFACE_ATTR_ADDRESS4] = { .name = "ipv4-address", .type = BLOBMSG_TYPE_ARRAY }, }; -enum { - ADDR_ATTR_ADDRESS, - ADDR_ATTR_MASK, - ADDR_ATTR_MAX, -}; - -static const struct blobmsg_policy addr_attrs[ADDR_ATTR_MAX] = { - [ADDR_ATTR_ADDRESS] = { .name = "address", .type = BLOBMSG_TYPE_ARRAY }, - [ADDR_ATTR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 }, -}; - static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg) { struct blob_attr *tb[DUMP_ATTR_MAX]; -- cgit v1.2.3