From 4dceea4901b772cd9a209beb30a72be24481c50c Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 10 Aug 2017 16:18:06 -0600 Subject: [PATCH] Network routing table: Trivial name changes to make IPv4 and IPv6 more symmetric. Add debug logic to dump the content of the router table. --- net/route/Make.defs | 4 +++ net/route/net_addroute.c | 6 ++-- net/route/net_allocroute.c | 10 +++---- net/route/net_delroute.c | 30 +++++++++++++++---- net/route/net_foreachroute.c | 16 +++++----- net/route/net_router.c | 2 +- net/route/netdev_router.c | 15 ++++++---- net/route/route.h | 57 ++++++++++++++++++++++++++++++------ 8 files changed, 105 insertions(+), 35 deletions(-) diff --git a/net/route/Make.defs b/net/route/Make.defs index 5cbdc8bf75..cdc61e9e90 100644 --- a/net/route/Make.defs +++ b/net/route/Make.defs @@ -40,6 +40,10 @@ ifeq ($(CONFIG_NET_ROUTE),y) SOCK_CSRCS += net_addroute.c net_allocroute.c net_delroute.c SOCK_CSRCS += net_foreachroute.c net_router.c netdev_router.c +ifeq ($(CONFIG_DEBUG_NET_INFO),y) +SOCK_CSRCS += net_dumproute.c +endif + # Include routing table build support DEPPATH += --dep-path route diff --git a/net/route/net_addroute.c b/net/route/net_addroute.c index 99e171886e..a5e595de90 100644 --- a/net/route/net_addroute.c +++ b/net/route/net_addroute.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/route/net_addroute.c * - * Copyright (C) 2013, 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2013, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -74,7 +74,7 @@ #ifdef CONFIG_NET_IPv4 int net_addroute_ipv4(in_addr_t target, in_addr_t netmask, in_addr_t router) { - FAR struct net_route_s *route; + FAR struct net_route_ipv4_s *route; /* Allocate a route entry */ @@ -90,6 +90,7 @@ int net_addroute_ipv4(in_addr_t target, in_addr_t netmask, in_addr_t router) net_ipv4addr_copy(route->target, target); net_ipv4addr_copy(route->netmask, netmask); net_ipv4addr_copy(route->router, router); + net_ipv4_dumproute("New route", route); /* Get exclusive address to the networking data structures */ @@ -123,6 +124,7 @@ int net_addroute_ipv6(net_ipv6addr_t target, net_ipv6addr_t netmask, net_ipv6addr_copy(route->target, target); net_ipv6addr_copy(route->netmask, netmask); net_ipv6addr_copy(route->router, router); + net_ipv6_dumproute("New route", route); /* Get exclusive address to the networking data structures */ diff --git a/net/route/net_allocroute.c b/net/route/net_allocroute.c index 4d5c37cc5a..ebb21c08e7 100644 --- a/net/route/net_allocroute.c +++ b/net/route/net_allocroute.c @@ -81,7 +81,7 @@ static sq_queue_t g_freeroutes_ipv6; /* This is an array of pre-allocated network routes */ #ifdef CONFIG_NET_IPv4 -static struct net_route_s g_preallocroutes[CONFIG_NET_MAXROUTES]; +static struct net_route_ipv4_s g_preallocroutes[CONFIG_NET_MAXROUTES]; #endif #ifdef CONFIG_NET_IPv6 @@ -158,9 +158,9 @@ void net_initroute(void) ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -FAR struct net_route_s *net_allocroute_ipv4(void) +FAR struct net_route_ipv4_s *net_allocroute_ipv4(void) { - FAR struct net_route_s *route; + FAR struct net_route_ipv4_s *route; /* Get exclusive address to the networking data structures */ @@ -168,7 +168,7 @@ FAR struct net_route_s *net_allocroute_ipv4(void) /* Then add the new entry to the table */ - route = (FAR struct net_route_s *) + route = (FAR struct net_route_ipv4_s *) sq_remfirst((FAR sq_queue_t *)&g_freeroutes); net_unlock(); @@ -210,7 +210,7 @@ FAR struct net_route_ipv6_s *net_allocroute_ipv6(void) ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -void net_freeroute_ipv4(FAR struct net_route_s *route) +void net_freeroute_ipv4(FAR struct net_route_ipv4_s *route) { DEBUGASSERT(route); diff --git a/net/route/net_delroute.c b/net/route/net_delroute.c index fde8302883..b6b724c067 100644 --- a/net/route/net_delroute.c +++ b/net/route/net_delroute.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/route/net_delroute.c * - * Copyright (C) 2013, 2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2013, 2015, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -42,7 +42,9 @@ #include #include #include +#include +#include #include #include "route/route.h" @@ -56,7 +58,7 @@ #ifdef CONFIG_NET_IPv4 struct route_match_s { - FAR struct net_route_s *prev; /* Predecessor in the list */ + FAR struct net_route_ipv4_s *prev; /* Predecessor in the list */ in_addr_t target; /* The target IP address to match */ in_addr_t netmask; /* The network mask to match */ }; @@ -76,7 +78,7 @@ struct route_match_ipv6_s ****************************************************************************/ /**************************************************************************** - * Name: net_match + * Name: net_match_ipv4 * * Description: * Return 1 if the route is available @@ -91,7 +93,7 @@ struct route_match_ipv6_s ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -static int net_match(FAR struct net_route_s *route, FAR void *arg) +static int net_match_ipv4(FAR struct net_route_ipv4_s *route, FAR void *arg) { FAR struct route_match_s *match = (FAR struct route_match_s *)arg; @@ -99,6 +101,11 @@ static int net_match(FAR struct net_route_s *route, FAR void *arg) * must be the same. */ + net_ipv4_dumproute("Comparing", route); + ninfo("With:\n"); + ninfo(" target=%08lx netmask=%08lx\n", + htonl(match->target), htonl(match->netmask)); + if (net_ipv4addr_maskcmp(route->target, match->target, match->netmask) && net_ipv4addr_cmp(route->netmask, match->netmask)) { @@ -139,6 +146,19 @@ static int net_match_ipv6(FAR struct net_route_ipv6_s *route, FAR void *arg) * must be the same. */ + net_ipv6_dumproute("Comparing", route); + ninfo("With:\n"); + ninfo(" target: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", + htons(match->target[0]), htons(match->target[1]), + htons(match->target[2]), htons(match->target[3]), + htons(match->target[4]), htons(match->target[5]), + htons(match->target[6]), htons(match->target[7])); + ninfo(" netmask: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", + htons(match->netmask[0]), htons(match->netmask[1]), + htons(match->netmask[2]), htons(match->netmask[3]), + htons(match->netmask[4]), htons(match->netmask[5]), + htons(match->netmask[6]), htons(match->netmask[7])); + if (net_ipv6addr_maskcmp(route->target, match->target, match->netmask) && net_ipv6addr_cmp(route->netmask, match->netmask)) { @@ -200,7 +220,7 @@ int net_delroute_ipv4(in_addr_t target, in_addr_t netmask) /* Then remove the entry from the routing table */ - return net_foreachroute_ipv4(net_match, &match) ? OK : -ENOENT; + return net_foreachroute_ipv4(net_match_ipv4, &match) ? OK : -ENOENT; } #endif diff --git a/net/route/net_foreachroute.c b/net/route/net_foreachroute.c index c7e49e15ce..bce57bec98 100644 --- a/net/route/net_foreachroute.c +++ b/net/route/net_foreachroute.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/route/net_foreachroute.c * - * Copyright (C) 2013 Gregory Nutt. All rights reserved. + * Copyright (C) 2013, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -72,8 +72,8 @@ #ifdef CONFIG_NET_IPv4 int net_foreachroute_ipv4(route_handler_t handler, FAR void *arg) { - FAR struct net_route_s *route; - FAR struct net_route_s *next; + FAR struct net_route_ipv4_s *route; + FAR struct net_route_ipv4_s *next; int ret = 0; /* Prevent concurrent access to the routing table */ @@ -82,8 +82,8 @@ int net_foreachroute_ipv4(route_handler_t handler, FAR void *arg) /* Visit each entry in the routing table */ - for (route = (FAR struct net_route_s *)g_ipv4_routes.head; - route; + for (route = (FAR struct net_route_ipv4_s *)g_ipv4_routes.head; + ret == 0 && route != NULL; route = next) { /* Get the next entry in the to visit. We do this BEFORE calling the @@ -91,7 +91,7 @@ int net_foreachroute_ipv4(route_handler_t handler, FAR void *arg) */ next = route->flink; - ret = handler(route, arg); + ret = handler(route, arg); } /* Unlock the network */ @@ -115,7 +115,7 @@ int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg) /* Visit each entry in the routing table */ for (route = (FAR struct net_route_ipv6_s *)g_ipv6_routes.head; - route; + ret == 0 && route != NULL; route = next) { /* Get the next entry in the to visit. We do this BEFORE calling the @@ -123,7 +123,7 @@ int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg) */ next = route->flink; - ret = handler(route, arg); + ret = handler(route, arg); } /* Unlock the network */ diff --git a/net/route/net_router.c b/net/route/net_router.c index b25af7d36b..ef48bcffd1 100644 --- a/net/route/net_router.c +++ b/net/route/net_router.c @@ -92,7 +92,7 @@ struct route_ipv6_match_s ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -static int net_ipv4_match(FAR struct net_route_s *route, FAR void *arg) +static int net_ipv4_match(FAR struct net_route_ipv4_s *route, FAR void *arg) { FAR struct route_ipv4_match_s *match = (FAR struct route_ipv4_match_s *)arg; diff --git a/net/route/netdev_router.c b/net/route/netdev_router.c index fe35abe952..6ff1ab6bd8 100644 --- a/net/route/netdev_router.c +++ b/net/route/netdev_router.c @@ -93,9 +93,11 @@ struct route_ipv6_devmatch_s ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -static int net_ipv4_devmatch(FAR struct net_route_s *route, FAR void *arg) +static int net_ipv4_devmatch(FAR struct net_route_ipv4_s *route, + FAR void *arg) { - FAR struct route_ipv4_devmatch_s *match = (FAR struct route_ipv4_devmatch_s *)arg; + FAR struct route_ipv4_devmatch_s *match = + (FAR struct route_ipv4_devmatch_s *)arg; FAR struct net_driver_s *dev = match->dev; /* To match, (1) the masked target addresses must be the same, and (2) the @@ -134,9 +136,11 @@ static int net_ipv4_devmatch(FAR struct net_route_s *route, FAR void *arg) ****************************************************************************/ #ifdef CONFIG_NET_IPv6 -static int net_ipv6_devmatch(FAR struct net_route_ipv6_s *route, FAR void *arg) +static int net_ipv6_devmatch(FAR struct net_route_ipv6_s *route, + FAR void *arg) { - FAR struct route_ipv6_devmatch_s *match = (FAR struct route_ipv6_devmatch_s *)arg; + FAR struct route_ipv6_devmatch_s *match = + (FAR struct route_ipv6_devmatch_s *)arg; FAR struct net_driver_s *dev = match->dev; /* To match, (1) the masked target addresses must be the same, and (2) the @@ -147,7 +151,8 @@ static int net_ipv6_devmatch(FAR struct net_route_ipv6_s *route, FAR void *arg) */ if (net_ipv6addr_maskcmp(route->target, match->target, route->netmask) && - net_ipv6addr_maskcmp(route->router, dev->d_ipv6addr, dev->d_ipv6netmask)) + net_ipv6addr_maskcmp(route->router, dev->d_ipv6addr, + dev->d_ipv6netmask)) { /* They match.. Copy the router address */ diff --git a/net/route/route.h b/net/route/route.h index d9fdc3cfaa..7d11a21042 100644 --- a/net/route/route.h +++ b/net/route/route.h @@ -1,7 +1,7 @@ /**************************************************************************** * net/route/route.h * - * Copyright (C) 2013-2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2013-2014, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -65,17 +65,18 @@ /* This structure describes one entry in the routing table */ #ifdef CONFIG_NET_IPv4 -struct net_route_s +struct net_route_ipv4_s { - FAR struct net_route_s *flink; /* Supports a singly linked list */ - in_addr_t target; /* The destination network */ - in_addr_t netmask; /* The network address mask */ - in_addr_t router; /* Route packets via this router */ + FAR struct net_route_ipv4_s *flink; /* Supports a singly linked list */ + in_addr_t target; /* The destination network */ + in_addr_t netmask; /* The network address mask */ + in_addr_t router; /* Route packets via this router */ }; /* Type of the call out function pointer provided to net_foreachroute_ipv4() */ -typedef int (*route_handler_t)(FAR struct net_route_s *route, FAR void *arg); +typedef int (*route_handler_t)(FAR struct net_route_ipv4_s *route, + FAR void *arg); #endif #ifdef CONFIG_NET_IPv6 @@ -151,7 +152,7 @@ void net_initroute(void); ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -FAR struct net_route_s *net_allocroute_ipv4(void); +FAR struct net_route_ipv4_s *net_allocroute_ipv4(void); #endif #ifdef CONFIG_NET_IPv6 @@ -173,7 +174,7 @@ FAR struct net_route_ipv6_s *net_allocroute_ipv6(void); ****************************************************************************/ #ifdef CONFIG_NET_IPv4 -void net_freeroute_ipv4(FAR struct net_route_s *route); +void net_freeroute_ipv4(FAR struct net_route_ipv4_s *route); #endif #ifdef CONFIG_NET_IPv6 @@ -346,6 +347,44 @@ int net_foreachroute_ipv4(route_handler_t handler, FAR void *arg); int net_foreachroute_ipv6(route_handler_ipv6_t handler, FAR void *arg); #endif +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: net_ipv4_dumproute and net_ipv6_dumproute + * + * Description: + * Dump a routing table enter + * + * Parameters: + * route - The entry to be dumped + * + * Returned Value: + * None + * + ****************************************************************************/ + +#ifdef CONFIG_DEBUG_NET_INFO +#ifdef CONFIG_NET_IPv4 +void net_ipv4_dumproute(FAR const char *msg, + FAR struct net_route_ipv4_s *route); +#else +# define net_ipv4_dumproute(m,r) +#endif + +#ifdef CONFIG_NET_IPv6 +void net_ipv6_dumproute(FAR const char *msg, + FAR struct net_route_ipv6_s *route); +#else +# define net_ipv6_dumproute(m,r) +#endif + +#else +# define net_ipv4_dumproute(m,r) +# define net_ipv6_dumproute(m,r) +#endif + #undef EXTERN #ifdef __cplusplus }