Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/netutils/netlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ FAR struct ipt_entry *netlib_ipt_masquerade_entry(FAR const char *ifname);
FAR struct ipt_entry *netlib_ipt_filter_entry(FAR const char *target,
int verdict,
uint8_t match_proto);
FAR struct ipt_entry *netlib_ipt_filter_entry_limit(FAR const char *target,
int verdict,
uint8_t match_proto,
uint32_t avg,
uint32_t burst);
# endif
# endif /* CONFIG_NET_IPv4 */
# ifdef CONFIG_NET_IPv6
Expand All @@ -501,6 +506,11 @@ int netlib_ip6t_fillifname(FAR struct ip6t_entry *entry,
FAR struct ip6t_entry *netlib_ip6t_filter_entry(FAR const char *target,
int verdict,
uint8_t match_proto);
FAR struct ip6t_entry *netlib_ip6t_filter_entry_limit(FAR const char *target,
int verdict,
uint8_t match_proto,
uint32_t avg,
uint32_t burst);
# endif
# endif /* CONFIG_NET_IPv6 */
#endif /* CONFIG_NET_IPTABLES */
Expand Down
192 changes: 181 additions & 11 deletions netutils/netlib/netlib_ip6tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <sys/socket.h>

#include <nuttx/compiler.h>
#include <nuttx/net/netfilter/ip6_tables.h>

#include "netutils/netlib.h"
Expand All @@ -37,13 +38,40 @@
* Pre-processor Definitions
****************************************************************************/

/* Each match and the target start at an offset that is a multiple of
* XT_ALIGN(), so match_size and target_size are multiples of XT_ALIGN(),
* as Linux iptables makes them. The kernel checks the match sizes.
*/

#define NETLIB_XT_ALIGNED aligned_data(__alignof__(struct _xt_align))

/* Fill the name of match m, and its size up to the next member. An entry
* can have more than one match, so the target does not always follow.
*/

#define IP6T_FILL_MATCH_TO(e, m, next, match_name) \
do \
{ \
strlcpy((e)->m.u.user.name, (match_name), \
sizeof((e)->m.u.user.name)); \
(e)->m.u.match_size = offsetof(typeof(*(e)), next) - \
offsetof(typeof(*(e)), m); \
} \
while(0)

#define IP6T_FILL_MATCH(e, match_name) \
IP6T_FILL_MATCH_TO(e, match, target, match_name)

/* Fill the entry, the limit match and the target of a rate limit entry */

#define IP6T_FILL_LIMIT(e, target_name, v, a, b) \
do \
{ \
strlcpy((e)->match.u.user.name, (match_name), \
sizeof((e)->match.u.user.name)); \
(e)->match.u.match_size = offsetof(typeof(*(e)), target) - \
offsetof(typeof(*(e)), match); \
IP6T_FILL_ENTRY(e, target_name); \
IP6T_FILL_MATCH_TO(e, lmatch, target, XT_MATCH_NAME_LIMIT); \
(e)->rate.avg = (a); \
(e)->rate.burst = (b); \
(e)->target.verdict = (v); \
} \
while(0)

Expand All @@ -57,31 +85,71 @@ struct ip6t_filter_entry_s

/* Compatible with ACCEPT/DROP/REJECT target. */

struct xt_standard_target target;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

struct ip6t_filter_tcp_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match match;
struct xt_entry_match match NETLIB_XT_ALIGNED;
struct xt_tcp tcp;
struct xt_standard_target target;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

struct ip6t_filter_udp_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match match;
struct xt_entry_match match NETLIB_XT_ALIGNED;
struct xt_udp udp;
struct xt_standard_target target;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

/* Entries with a rate limit match: the limit alone, or after a match */

struct ip6t_filter_limit_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match lmatch NETLIB_XT_ALIGNED;
struct xt_rateinfo rate;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

struct ip6t_filter_tcp_limit_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match match NETLIB_XT_ALIGNED;
struct xt_tcp tcp;
struct xt_entry_match lmatch NETLIB_XT_ALIGNED;
struct xt_rateinfo rate;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

struct ip6t_filter_udp_limit_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match match NETLIB_XT_ALIGNED;
struct xt_udp udp;
struct xt_entry_match lmatch NETLIB_XT_ALIGNED;
struct xt_rateinfo rate;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

struct ip6t_filter_icmp_limit_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match match NETLIB_XT_ALIGNED;
struct ip6t_icmp icmp;
struct xt_entry_match lmatch NETLIB_XT_ALIGNED;
struct xt_rateinfo rate;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

struct ip6t_filter_icmp_entry_s
{
struct ip6t_entry entry;
struct xt_entry_match match;
struct xt_entry_match match NETLIB_XT_ALIGNED;
struct ip6t_icmp icmp;
struct xt_standard_target target;
struct xt_standard_target target NETLIB_XT_ALIGNED;
};

/****************************************************************************
Expand Down Expand Up @@ -663,6 +731,108 @@ int netlib_ip6t_fillifname(FAR struct ip6t_entry *entry,
return OK;
}

/****************************************************************************
* Name: netlib_ip6t_filter_entry_limit
*
* Description:
* Alloc an entry with filter target and a rate limit match (-m limit),
* after a TCP, UDP or ICMP match if match_proto is one of them.
*
* Input Parameters:
* target - The target name to apply.
* verdict - The verdict to set.
* match_proto - The protocol of the match before the limit, or 0.
* avg - Time between packets in seconds * XT_LIMIT_SCALE.
* burst - Number of packets that can come at once.
*
* Returned Value:
* The pointer to the entry, or NULL if failed.
* Caller must free it after use.
*
****************************************************************************/

#ifdef CONFIG_NET_IPFILTER
FAR struct ip6t_entry *netlib_ip6t_filter_entry_limit(FAR const char *target,
int verdict,
uint8_t match_proto,
uint32_t avg,
uint32_t burst)
{
if (target == NULL)
{
fprintf(stderr, "Empty target!\n");
return NULL;
}

switch (match_proto)
{
case 0:
{
FAR struct ip6t_filter_limit_entry_s *entry =
zalloc(sizeof(*entry));

if (entry == NULL)
{
return NULL;
}

IP6T_FILL_LIMIT(entry, target, verdict, avg, burst);
return &entry->entry;
}

case IPPROTO_TCP:
{
FAR struct ip6t_filter_tcp_limit_entry_s *entry =
zalloc(sizeof(*entry));

if (entry == NULL)
{
return NULL;
}

IP6T_FILL_LIMIT(entry, target, verdict, avg, burst);
IP6T_FILL_MATCH_TO(entry, match, lmatch, XT_MATCH_NAME_TCP);
return &entry->entry;
}

case IPPROTO_UDP:
{
FAR struct ip6t_filter_udp_limit_entry_s *entry =
zalloc(sizeof(*entry));

if (entry == NULL)
{
return NULL;
}

IP6T_FILL_LIMIT(entry, target, verdict, avg, burst);
IP6T_FILL_MATCH_TO(entry, match, lmatch, XT_MATCH_NAME_UDP);
return &entry->entry;
}

case IPPROTO_ICMP6:
{
FAR struct ip6t_filter_icmp_limit_entry_s *entry =
zalloc(sizeof(*entry));

if (entry == NULL)
{
return NULL;
}

IP6T_FILL_LIMIT(entry, target, verdict, avg, burst);
IP6T_FILL_MATCH_TO(entry, match, lmatch, XT_MATCH_NAME_ICMP6);
return &entry->entry;
}

default:
fprintf(stderr, "Rate limit is not supported for protocol %u!\n",
match_proto);
return NULL;
}
}
#endif

/****************************************************************************
* Name: netlib_ip6t_filter_entry
*
Expand Down
Loading
Loading