Skip to content

Commit

Permalink
netlib/setroute: fix dhcpd stack-overflow when calls netlib_set_dripv…
Browse files Browse the repository at this point in the history
…4addr with debug info-level

Signed-off-by: meijian <meijian@xiaomi.com>
  • Loading branch information
Meissi-jian authored and xiaoxiang781216 committed Aug 25, 2024
1 parent 05def1b commit c91a181
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
26 changes: 11 additions & 15 deletions examples/mld/mld_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,9 @@ int main(int argc, FAR char *argv[])
FAR char *iobuffer = NULL;
struct sockaddr_in6 host;
#ifdef CONFIG_NET_ROUTE
struct sockaddr_storage target;
struct sockaddr_storage router;
struct sockaddr_storage netmask;
FAR struct sockaddr_in6 *v6_addr;
struct sockaddr_in6 target;
struct sockaddr_in6 router;
struct sockaddr_in6 netmask;
#endif
struct ipv6_mreq mrec;
int nsec;
Expand Down Expand Up @@ -361,21 +360,18 @@ int main(int argc, FAR char *argv[])
/* Set up a routing table entry for the address of the multicast group */

memset(&target, 0, sizeof(target));
v6_addr = (FAR struct sockaddr_in6 *)&target;
v6_addr->sin6_family = AF_INET6;
v6_addr->sin6_port = HTONS(0x4321);
target.sin6_family = AF_INET6;
target.sin6_port = HTONS(0x4321);
memcpy(v6_addr->sin6_addr.s6_addr16, g_grp_addr, sizeof(struct in6_addr));

memset(&netmask, 0, sizeof(netmask));
v6_addr = (FAR struct sockaddr_in6 *)&netmask;
v6_addr->sin6_family = AF_INET6;
v6_addr->sin6_port = HTONS(0x4321);
netmask.sin6_family = AF_INET6;
netmask.sin6_port = HTONS(0x4321);
memset(v6_addr->sin6_addr.s6_addr16, 0xff, sizeof(struct in6_addr));

memset(&router, 0, sizeof(router));
v6_addr = (FAR struct sockaddr_in6 *)&router;
v6_addr->sin6_family = AF_INET6;
v6_addr->sin6_port = HTONS(0x4321);
router.sin6_family = AF_INET6;
router.sin6_port = HTONS(0x4321);

ret = netlib_get_ipv6addr("eth0", &v6_addr->sin6_addr);
if (ret < 0)
Expand All @@ -384,7 +380,7 @@ int main(int argc, FAR char *argv[])
}
else
{
ret = addroute(sockfd, &target, &netmask, &router);
ret = addroute(sockfd, &target, &netmask, &router, sizeof(router));
if (ret < 0)
{
fprintf(stderr, "ERROR: addroute() failed: %d\n", errno);
Expand All @@ -406,7 +402,7 @@ int main(int argc, FAR char *argv[])
}
}

ret = delroute(sockfd, &target, &netmask);
ret = delroute(sockfd, &target, &netmask, sizeof(target));
if (ret < 0)
{
fprintf(stderr, "ERROR: delroute() failed: %d\n", errno);
Expand Down
21 changes: 10 additions & 11 deletions netutils/netlib/netlib_setdripv4addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,19 @@ int netlib_set_dripv4addr(FAR const char *ifname,
int ret = ERROR;

#ifdef CONFIG_NET_ROUTE
FAR struct sockaddr_in *v4_addr;
struct sockaddr_storage target;
struct sockaddr_storage netmask;
struct sockaddr_storage router;
struct sockaddr_in target;
struct sockaddr_in netmask;
struct sockaddr_in router;

memset(&target, 0, sizeof(target));
target.ss_family = AF_INET;
target.sin_family = AF_INET;

memset(&netmask, 0, sizeof(netmask));
netmask.ss_family = AF_INET;
netmask.sin_family = AF_INET;

memset(&router, 0, sizeof(router));
v4_addr = (FAR struct sockaddr_in *)&router;
v4_addr->sin_family = AF_INET;
v4_addr->sin_addr = *addr;
router.sin_family = AF_INET;
router.sin_addr = *addr;
#endif

if (ifname && addr)
Expand Down Expand Up @@ -111,11 +109,12 @@ int netlib_set_dripv4addr(FAR const char *ifname,

/* This call fails if no default route exists, but it's OK */

delroute(sockfd, &target, &netmask);
delroute(sockfd, &target, &netmask, sizeof(target));

/* Then add the new default route */

ret = addroute(sockfd, &target, &netmask, &router);
ret = addroute(sockfd, &target, &netmask,
&router, sizeof(router));
}
#endif

Expand Down
5 changes: 3 additions & 2 deletions nshlib/nsh_routecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ int cmd_addroute(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)

/* Then add the route */

ret = addroute(sockfd, &target.ipx, &netmask.ipx, &router.ipx);
ret = addroute(sockfd, &target.ipx, &netmask.ipx,
&router.ipx, sizeof(router));
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "addroute", NSH_ERRNO);
Expand Down Expand Up @@ -676,7 +677,7 @@ int cmd_delroute(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)

/* Then delete the route */

ret = delroute(sockfd, &target.ipx, &netmask.ipx);
ret = delroute(sockfd, &target.ipx, &netmask.ipx, sizeof(target));
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "delroute", NSH_ERRNO);
Expand Down

0 comments on commit c91a181

Please sign in to comment.