From b6ae9ffaeb0e18e9fa3d5be62faa8d3c9f340a58 Mon Sep 17 00:00:00 2001 From: Vladislav Grishenko Date: Fri, 26 Jul 2024 00:51:09 +0500 Subject: [PATCH] odhcp6c: allow to request specific ipv6 prefix Expand -P option with optional exact ipv6 prefix format. This allows to keep the IPv6 prefix in some cases, for example if the prefix is issued dynamically on upstream. Examples: -P -P Based on https://github.com/openwrt/odhcp6c/pull/86 --- src/dhcpv6.c | 3 ++- src/odhcp6c.c | 20 ++++++++++++++++---- src/odhcp6c.h | 3 ++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/dhcpv6.c b/src/dhcpv6.c index ca5957e..c1603f5 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -384,7 +384,8 @@ static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs) struct dhcpv6_ia_prefix pref = { .type = htons(DHCPV6_OPT_IA_PREFIX), .len = htons(sizeof(pref) - 4), - .prefix = request_prefixes[i].length + .prefix = request_prefixes[i].length, + .addr = request_prefixes[i].addr }; memcpy(ia_pd + ia_pd_len, &hdr_ia_pd, sizeof(hdr_ia_pd)); ia_pd_len += sizeof(hdr_ia_pd); diff --git a/src/odhcp6c.c b/src/odhcp6c.c index 4a6d7bd..20b4040 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -241,17 +241,29 @@ int main(_unused int argc, char* const argv[]) if (allow_slaac_only >= 0 && allow_slaac_only < 10) allow_slaac_only = 10; + struct odhcp6c_request_prefix prefix = { 0 }; + + optpos = strchr(optarg, '/'); + if (optpos) { + strncpy((char *)buf, optarg, optpos - optarg); + buf[optpos - optarg] = '\0'; + if (inet_pton(AF_INET6, (char *)buf, &prefix.addr) <= 0) { + syslog(LOG_ERR, "invalid argument: '%s'", optarg); + return 1; + } + optpos++; + } else + optpos = optarg; + char *iaid_begin; int iaid_len = 0; - int prefix_length = strtoul(optarg, &iaid_begin, 10); + prefix.length = strtoul(optpos, &iaid_begin, 10); if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') { syslog(LOG_ERR, "invalid argument: '%s'", optarg); return 1; } - struct odhcp6c_request_prefix prefix = { 0, prefix_length }; - if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1) memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len); else if (*iaid_begin == ':') @@ -614,7 +626,7 @@ static int usage(void) " -S