From fc7fb637811089b4f26fb00ca81a0d05c09076bd Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Fri, 30 May 2014 16:59:53 +0200 Subject: [PATCH] Make filtering customizable --- src/config.c | 8 ++++++++ src/dhcpv4.c | 7 +++---- src/dhcpv6.c | 8 +++----- src/odhcpd.h | 2 ++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/config.c b/src/config.c index 6dd52bb..db5ca3b 100644 --- a/src/config.c +++ b/src/config.c @@ -32,6 +32,7 @@ enum { IFACE_ATTR_NDP, IFACE_ATTR_DNS, IFACE_ATTR_DOMAIN, + IFACE_ATTR_FILTER_CLASS, IFACE_ATTR_DHCPV6_RAW, IFACE_ATTR_RA_DEFAULT, IFACE_ATTR_RA_MANAGEMENT, @@ -62,6 +63,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = { [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING }, [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY }, [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY }, + [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING }, [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING }, [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING }, [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING }, @@ -152,6 +154,7 @@ static void clean_interface(struct interface *iface) free(iface->static_ndp); free(iface->dhcpv4_dns); free(iface->dhcpv6_raw); + free(iface->filter_class); memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra)); } @@ -457,6 +460,11 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr } } + if ((c = tb[IFACE_ATTR_FILTER_CLASS])) { + iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1); + memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1); + } + if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) { iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2; iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len); diff --git a/src/dhcpv4.c b/src/dhcpv4.c index b8e0c95..628efcd 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -37,7 +37,6 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr, const char *hostname); -static const char *excluded_class = "HOMENET"; // Create socket and register events int init_dhcpv4(void) @@ -294,11 +293,11 @@ static void handle_dhcpv4(void *addr, void *data, size_t len, } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) { if (memcmp(opt->data, &ifaddr.sin_addr, 4)) return; - } else if (opt->type == DHCPV4_OPT_USER_CLASS) { + } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) { uint8_t *c = opt->data, *cend = &opt->data[opt->len]; for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) { - size_t elen = strlen(excluded_class); - if (*c == elen && !memcmp(&c[1], excluded_class, elen)) + size_t elen = strlen(iface->filter_class); + if (*c == elen && !memcmp(&c[1], iface->filter_class, elen)) return; // Ignore from homenet } } diff --git a/src/dhcpv6.c b/src/dhcpv6.c index 37dc3f5..aaefa5e 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -22,8 +22,6 @@ #include "odhcpd.h" #include "dhcpv6.h" -static const char *excluded_class = "HOMENET"; - static void relay_client_request(struct sockaddr_in6 *source, const void *data, size_t len, struct interface *iface); @@ -293,11 +291,11 @@ static void handle_client_request(void *addr, void *data, size_t len, if (olen != ntohs(dest.serverid_length) || memcmp(odata, &dest.duid_type, olen)) return; // Not for us - } else if (otype == DHCPV6_OPT_USER_CLASS) { + } else if (iface->filter_class && otype == DHCPV6_OPT_USER_CLASS) { uint8_t *c = odata, *cend = &odata[olen]; for (; &c[2] <= cend && &c[2 + (c[0] << 8) + c[1]] <= cend; c = &c[2 + (c[0] << 8) + c[1]]) { - size_t elen = strlen(excluded_class); - if (((((size_t)c[0]) << 8) | c[1]) == elen && !memcmp(&c[2], excluded_class, elen)) + size_t elen = strlen(iface->filter_class); + if (((((size_t)c[0]) << 8) | c[1]) == elen && !memcmp(&c[2], iface->filter_class, elen)) return; // Ignore from homenet } } else if (otype == DHCPV6_OPT_IA_PD) { diff --git a/src/odhcpd.h b/src/odhcpd.h index 64ffca2..dfaec9b 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -165,6 +165,8 @@ struct interface { char *upstream; size_t upstream_len; + + char *filter_class; }; extern struct list_head interfaces; -- 2.25.1