Skip to content

Commit 5a13d87

Browse files
systemcrashNoltari
authored andcommitted
dhcpv6: set static defines for DHCPv6 option header size
so the number 4 isn't so mysterious at first glance. Signed-off-by: Paul Donald <newtwen+github@gmail.com> Link: openwrt#363 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
1 parent 3fda5f8 commit 5a13d87

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/dhcpv6-ia.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,14 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
587587
uint16_t val;
588588
} o_status = {
589589
.type = htons(DHCPV6_OPT_STATUS),
590-
.len = htons(sizeof(o_status) - 4),
590+
.len = htons(sizeof(o_status) - DHCPV6_OPT_HDR_SIZE),
591591
.val = htons(status),
592592
};
593593

594594
memcpy(buf + ia_len, &o_status, sizeof(o_status));
595595
ia_len += sizeof(o_status);
596596

597-
o_ia.len = htons(ia_len - 4);
597+
o_ia.len = htons(ia_len - DHCPV6_OPT_HDR_SIZE);
598598
memcpy(buf, &o_ia, sizeof(o_ia));
599599

600600
return ia_len;
@@ -668,7 +668,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
668668
if (a->flags & OAF_DHCPV6_PD) {
669669
struct dhcpv6_ia_prefix o_ia_p = {
670670
.type = htons(DHCPV6_OPT_IA_PREFIX),
671-
.len = htons(sizeof(o_ia_p) - 4),
671+
.len = htons(sizeof(o_ia_p) - DHCPV6_OPT_HDR_SIZE),
672672
.preferred_lt = htonl(prefix_preferred_lt),
673673
.valid_lt = htonl(prefix_valid_lt),
674674
.prefix_len = a->length,
@@ -691,7 +691,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
691691
if (a->flags & OAF_DHCPV6_NA) {
692692
struct dhcpv6_ia_addr o_ia_a = {
693693
.type = htons(DHCPV6_OPT_IA_ADDR),
694-
.len = htons(sizeof(o_ia_a) - 4),
694+
.len = htons(sizeof(o_ia_a) - DHCPV6_OPT_HDR_SIZE),
695695
.addr = in6_from_prefix_and_iid(&addrs[i], a->assigned_host_id),
696696
.preferred_lt = htonl(prefix_preferred_lt),
697697
.valid_lt = htonl(prefix_valid_lt)
@@ -736,16 +736,16 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
736736
}
737737

738738
if (!request) {
739-
uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
739+
uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + DHCPV6_OPT_HDR_SIZE;
740740
uint16_t otype, olen;
741741

742742
dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
743-
struct dhcpv6_ia_prefix *ia_p = (struct dhcpv6_ia_prefix *)&odata[-4];
744-
struct dhcpv6_ia_addr *ia_a = (struct dhcpv6_ia_addr *)&odata[-4];
743+
struct dhcpv6_ia_prefix *ia_p = (struct dhcpv6_ia_prefix *)&odata[-DHCPV6_OPT_HDR_SIZE];
744+
struct dhcpv6_ia_addr *ia_a = (struct dhcpv6_ia_addr *)&odata[-DHCPV6_OPT_HDR_SIZE];
745745
bool found = false;
746746

747-
if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*ia_p) - 4) &&
748-
(otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*ia_a) - 4))
747+
if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*ia_p) - DHCPV6_OPT_HDR_SIZE) &&
748+
(otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*ia_a) - DHCPV6_OPT_HDR_SIZE))
749749
continue;
750750

751751
if (a) {
@@ -785,7 +785,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
785785
if (otype == DHCPV6_OPT_IA_PREFIX) {
786786
struct dhcpv6_ia_prefix o_ia_p = {
787787
.type = htons(DHCPV6_OPT_IA_PREFIX),
788-
.len = htons(sizeof(o_ia_p) - 4),
788+
.len = htons(sizeof(o_ia_p) - DHCPV6_OPT_HDR_SIZE),
789789
.preferred_lt = 0,
790790
.valid_lt = 0,
791791
.prefix_len = ia_p->prefix_len,
@@ -800,7 +800,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
800800
} else {
801801
struct dhcpv6_ia_addr o_ia_a = {
802802
.type = htons(DHCPV6_OPT_IA_ADDR),
803-
.len = htons(sizeof(o_ia_a) - 4),
803+
.len = htons(sizeof(o_ia_a) - DHCPV6_OPT_HDR_SIZE),
804804
.addr = ia_a->addr,
805805
.preferred_lt = 0,
806806
.valid_lt = 0,
@@ -816,7 +816,7 @@ static size_t build_ia(uint8_t *buf, size_t buflen, uint16_t status,
816816
}
817817
}
818818

819-
o_ia.len = htons(ia_len - 4);
819+
o_ia.len = htons(ia_len - DHCPV6_OPT_HDR_SIZE);
820820
memcpy(buf, &o_ia, sizeof(o_ia));
821821
return ia_len;
822822
}
@@ -906,16 +906,16 @@ static bool dhcpv6_ia_on_link(const struct dhcpv6_ia_hdr *ia, struct dhcpv6_leas
906906
struct odhcpd_ipaddr *addrs = iface->addr6;
907907
size_t addrlen = iface->addr6_len;
908908
time_t now = odhcpd_time();
909-
uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
909+
uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + DHCPV6_OPT_HDR_SIZE;
910910
uint16_t otype, olen;
911911
bool onlink = true;
912912

913913
dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
914-
struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix *)&odata[-4];
915-
struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr *)&odata[-4];
914+
struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix *)&odata[-DHCPV6_OPT_HDR_SIZE];
915+
struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr *)&odata[-DHCPV6_OPT_HDR_SIZE];
916916

917-
if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
918-
(otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
917+
if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - DHCPV6_OPT_HDR_SIZE) &&
918+
(otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - DHCPV6_OPT_HDR_SIZE))
919919
continue;
920920

921921
onlink = false;
@@ -992,7 +992,7 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
992992
if (!is_pd && !is_na)
993993
continue;
994994

995-
struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
995+
struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-DHCPV6_OPT_HDR_SIZE];
996996
size_t ia_response_len = 0;
997997
uint8_t reqlen = (is_pd) ? 62 : 128;
998998
uint32_t reqhint = 0;
@@ -1010,10 +1010,10 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
10101010
uint8_t *sdata;
10111011
uint16_t stype, slen;
10121012
dhcpv6_for_each_sub_option(&ia[1], odata + olen, stype, slen, sdata) {
1013-
if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1013+
if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - DHCPV6_OPT_HDR_SIZE)
10141014
continue;
10151015

1016-
struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1016+
struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-DHCPV6_OPT_HDR_SIZE];
10171017
if (p->prefix_len) {
10181018
reqlen = p->prefix_len;
10191019
reqhint = ntohl(p->addr.s6_addr32[1]);
@@ -1053,7 +1053,7 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
10531053
uint8_t *sdata;
10541054
uint16_t stype, slen;
10551055
dhcpv6_for_each_sub_option(&ia[1], odata + olen, stype, slen, sdata) {
1056-
if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1056+
if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - DHCPV6_OPT_HDR_SIZE)
10571057
continue;
10581058

10591059
ia_addr_present = true;
@@ -1211,7 +1211,7 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
12111211
if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
12121212
struct dhcpv6_auth_reconfigure auth = {
12131213
htons(DHCPV6_OPT_AUTH),
1214-
htons(sizeof(auth) - 4),
1214+
htons(sizeof(auth) - DHCPV6_OPT_HDR_SIZE),
12151215
3, 1, 0,
12161216
{htonl(time(NULL)), htonl(++serial)},
12171217
1,

src/dhcpv6.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
400400
uint16_t type;
401401
uint16_t len;
402402
uint32_t value;
403-
} maxrt = {htons(DHCPV6_OPT_SOL_MAX_RT), htons(sizeof(maxrt) - 4),
403+
} maxrt = {htons(DHCPV6_OPT_SOL_MAX_RT), htons(sizeof(maxrt) - DHCPV6_OPT_HDR_SIZE),
404404
htonl(60)};
405405

406406
struct _o_packed {
@@ -412,7 +412,7 @@ static void handle_client_request(void *addr, void *data, size_t len,
412412
uint16_t type;
413413
uint16_t len;
414414
uint16_t value;
415-
} stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
415+
} stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - DHCPV6_OPT_HDR_SIZE),
416416
htons(DHCPV6_STATUS_USEMULTICAST)};
417417

418418
struct _o_packed {

src/odhcpd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ enum odhcpd_assignment_flags {
194194
OAF_DHCPV6_PD = (1 << 1),
195195
};
196196

197+
#define DHCPV6_OPT_HDR_SIZE 4
198+
197199
/* 2-byte type + 128-byte DUID, RFC8415, §11.1 */
198200
#define DUID_MAX_LEN 130
199201
/* In theory, 2 (type only), or 7 (DUID-EN + 1-byte data), but be reasonable */

0 commit comments

Comments
 (0)