diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h index a4dace4de6d..a010e86ffc2 100644 --- a/include/netutils/netlib.h +++ b/include/netutils/netlib.h @@ -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 @@ -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 */ diff --git a/netutils/netlib/netlib_ip6tables.c b/netutils/netlib/netlib_ip6tables.c index cfc88335d2f..27e5a56f227 100644 --- a/netutils/netlib/netlib_ip6tables.c +++ b/netutils/netlib/netlib_ip6tables.c @@ -29,6 +29,7 @@ #include +#include #include #include "netutils/netlib.h" @@ -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) @@ -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; }; /**************************************************************************** @@ -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 * diff --git a/netutils/netlib/netlib_iptables.c b/netutils/netlib/netlib_iptables.c index 5bf2aab4197..b6bdfafb8df 100644 --- a/netutils/netlib/netlib_iptables.c +++ b/netutils/netlib/netlib_iptables.c @@ -29,6 +29,7 @@ #include +#include #include #include @@ -38,13 +39,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 IPT_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 IPT_FILL_MATCH(e, match_name) \ + IPT_FILL_MATCH_TO(e, match, target, match_name) + +/* Fill the entry, the limit match and the target of a rate limit entry */ + +#define IPT_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); \ + IPT_FILL_ENTRY(e, target_name); \ + IPT_FILL_MATCH_TO(e, lmatch, target, XT_MATCH_NAME_LIMIT); \ + (e)->rate.avg = (a); \ + (e)->rate.burst = (b); \ + (e)->target.verdict = (v); \ } \ while(0) @@ -72,31 +100,71 @@ struct ipt_filter_entry_s /* Compatible with ACCEPT/DROP/REJECT target. */ - struct xt_standard_target target; + struct xt_standard_target target NETLIB_XT_ALIGNED; }; struct ipt_filter_tcp_entry_s { struct ipt_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 ipt_filter_udp_entry_s { struct ipt_entry entry; - struct xt_entry_match match; + struct xt_entry_match match NETLIB_XT_ALIGNED; + struct xt_udp udp; + struct xt_standard_target target NETLIB_XT_ALIGNED; +}; + +/* Entries with a rate limit match: the limit alone, or after a match */ + +struct ipt_filter_limit_entry_s +{ + struct ipt_entry entry; + struct xt_entry_match lmatch NETLIB_XT_ALIGNED; + struct xt_rateinfo rate; + struct xt_standard_target target NETLIB_XT_ALIGNED; +}; + +struct ipt_filter_tcp_limit_entry_s +{ + struct ipt_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 ipt_filter_udp_limit_entry_s +{ + struct ipt_entry entry; + struct xt_entry_match match NETLIB_XT_ALIGNED; struct xt_udp udp; - struct xt_standard_target target; + struct xt_entry_match lmatch NETLIB_XT_ALIGNED; + struct xt_rateinfo rate; + struct xt_standard_target target NETLIB_XT_ALIGNED; +}; + +struct ipt_filter_icmp_limit_entry_s +{ + struct ipt_entry entry; + struct xt_entry_match match NETLIB_XT_ALIGNED; + struct ipt_icmp icmp; + struct xt_entry_match lmatch NETLIB_XT_ALIGNED; + struct xt_rateinfo rate; + struct xt_standard_target target NETLIB_XT_ALIGNED; }; struct ipt_filter_icmp_entry_s { struct ipt_entry entry; - struct xt_entry_match match; + struct xt_entry_match match NETLIB_XT_ALIGNED; struct ipt_icmp icmp; - struct xt_standard_target target; + struct xt_standard_target target NETLIB_XT_ALIGNED; }; /**************************************************************************** @@ -715,6 +783,108 @@ FAR struct ipt_entry *netlib_ipt_masquerade_entry(FAR const char *ifname) } #endif +/**************************************************************************** + * Name: netlib_ipt_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 ipt_entry *netlib_ipt_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 ipt_filter_limit_entry_s *entry = + zalloc(sizeof(*entry)); + + if (entry == NULL) + { + return NULL; + } + + IPT_FILL_LIMIT(entry, target, verdict, avg, burst); + return &entry->entry; + } + + case IPPROTO_TCP: + { + FAR struct ipt_filter_tcp_limit_entry_s *entry = + zalloc(sizeof(*entry)); + + if (entry == NULL) + { + return NULL; + } + + IPT_FILL_LIMIT(entry, target, verdict, avg, burst); + IPT_FILL_MATCH_TO(entry, match, lmatch, XT_MATCH_NAME_TCP); + return &entry->entry; + } + + case IPPROTO_UDP: + { + FAR struct ipt_filter_udp_limit_entry_s *entry = + zalloc(sizeof(*entry)); + + if (entry == NULL) + { + return NULL; + } + + IPT_FILL_LIMIT(entry, target, verdict, avg, burst); + IPT_FILL_MATCH_TO(entry, match, lmatch, XT_MATCH_NAME_UDP); + return &entry->entry; + } + + case IPPROTO_ICMP: + { + FAR struct ipt_filter_icmp_limit_entry_s *entry = + zalloc(sizeof(*entry)); + + if (entry == NULL) + { + return NULL; + } + + IPT_FILL_LIMIT(entry, target, verdict, avg, burst); + IPT_FILL_MATCH_TO(entry, match, lmatch, XT_MATCH_NAME_ICMP); + return &entry->entry; + } + + default: + fprintf(stderr, "Rate limit is not supported for protocol %u!\n", + match_proto); + return NULL; + } +} +#endif + /**************************************************************************** * Name: netlib_ipt_filter_entry * diff --git a/system/iptables/ip6tables.c b/system/iptables/ip6tables.c index 7702985b266..38160e77c96 100644 --- a/system/iptables/ip6tables.c +++ b/system/iptables/ip6tables.c @@ -92,8 +92,8 @@ static void ip6tables_print_chain(FAR const struct ip6t_replace *repl, char dst[INET6_ADDRSTRLEN + 4]; FAR struct ip6t_entry *entry; - FAR struct xt_entry_match *match; FAR struct xt_entry_target *target; + size_t mlen; FAR uint8_t *head = (FAR uint8_t *)repl->entries + repl->hook_entry[hook]; int size = repl->underflow[hook] - repl->hook_entry[hook]; @@ -110,8 +110,8 @@ static void ip6tables_print_chain(FAR const struct ip6t_replace *repl, ip6t_entry_for_every(entry, head, size) { target = IP6T_TARGET(entry); - match = entry->target_offset >= sizeof(struct xt_entry_match) ? - IP6T_MATCH(entry) : NULL; + mlen = entry->target_offset > sizeof(struct ip6t_entry) ? + entry->target_offset - sizeof(struct ip6t_entry) : 0; printf(fmt, iptables_target2str(target), INV_FLAG_STR(entry->ipv6.invflags & IP6T_INV_PROTO), iptables_proto2str(entry->ipv6.proto), @@ -125,7 +125,7 @@ static void ip6tables_print_chain(FAR const struct ip6t_replace *repl, INV_FLAG_STR(entry->ipv6.invflags & IP6T_INV_DSTIP), ip6tables_addr2str(entry->ipv6.dst.s6_addr16, entry->ipv6.dmsk.s6_addr16, dst, sizeof(dst)), - iptables_match2str(match)); + iptables_match2str(IP6T_MATCH(entry), mlen)); } printf("\n"); @@ -254,10 +254,18 @@ static int ip6tables_filter_command(FAR const struct iptables_args_s *args) /* Get entry and fill in proto-specific details. */ - if (args->sport != NULL || args->dport != NULL) + if (args->sport != NULL || args->dport != NULL || + IPTABLES_HAS_TCPFLAGS(args)) { FAR struct xt_udp *tcpudp; + if (IPTABLES_HAS_TCPFLAGS(args) && args->protocol != IPPROTO_TCP) + { + printf("TCP flags are only supported for TCP!\n"); + ret = -EINVAL; + goto errout; + } + if (args->protocol != IPPROTO_TCP && args->protocol != IPPROTO_UDP) { printf("Source/destination port is only supported for TCP/UDP!\n"); @@ -265,7 +273,12 @@ static int ip6tables_filter_command(FAR const struct iptables_args_s *args) goto errout; } - entry = netlib_ip6t_filter_entry(args->target, args->verdict, + entry = args->limit_avg != 0 ? + netlib_ip6t_filter_entry_limit(args->target, args->verdict, + args->protocol, + args->limit_avg, + args->limit_burst) : + netlib_ip6t_filter_entry(args->target, args->verdict, args->protocol); if (entry == NULL) { @@ -279,6 +292,8 @@ static int ip6tables_filter_command(FAR const struct iptables_args_s *args) { case IPPROTO_TCP: ((FAR struct xt_tcp *)tcpudp)->invflags = args->tcpudpinv; + ((FAR struct xt_tcp *)tcpudp)->flg_mask = args->tcp_flg_mask; + ((FAR struct xt_tcp *)tcpudp)->flg_cmp = args->tcp_flg_cmp; break; case IPPROTO_UDP: @@ -311,7 +326,12 @@ static int ip6tables_filter_command(FAR const struct iptables_args_s *args) goto errout; } - entry = netlib_ip6t_filter_entry(args->target, args->verdict, + entry = args->limit_avg != 0 ? + netlib_ip6t_filter_entry_limit(args->target, args->verdict, + args->protocol, + args->limit_avg, + args->limit_burst) : + netlib_ip6t_filter_entry(args->target, args->verdict, args->protocol); if (entry == NULL) { @@ -333,7 +353,11 @@ static int ip6tables_filter_command(FAR const struct iptables_args_s *args) } else { - entry = netlib_ip6t_filter_entry(args->target, args->verdict, 0); + entry = args->limit_avg != 0 ? + netlib_ip6t_filter_entry_limit(args->target, args->verdict, 0, + args->limit_avg, + args->limit_burst) : + netlib_ip6t_filter_entry(args->target, args->verdict, 0); if (entry == NULL) { goto errout_prepare_entry; diff --git a/system/iptables/iptables.c b/system/iptables/iptables.c index 8ec7feb865d..fdf1e42c749 100644 --- a/system/iptables/iptables.c +++ b/system/iptables/iptables.c @@ -134,8 +134,8 @@ static void iptables_print_chain(FAR const struct ipt_replace *repl, char dst[INET_ADDRSTRLEN + 3]; FAR struct ipt_entry *entry; - FAR struct xt_entry_match *match; FAR struct xt_entry_target *target; + size_t mlen; FAR uint8_t *head = (FAR uint8_t *)repl->entries + repl->hook_entry[hook]; int size = repl->underflow[hook] - repl->hook_entry[hook]; @@ -152,8 +152,8 @@ static void iptables_print_chain(FAR const struct ipt_replace *repl, ipt_entry_for_every(entry, head, size) { target = IPT_TARGET(entry); - match = entry->target_offset >= sizeof(struct xt_entry_match) ? - IPT_MATCH(entry) : NULL; + mlen = entry->target_offset > sizeof(struct ipt_entry) ? + entry->target_offset - sizeof(struct ipt_entry) : 0; printf(fmt, iptables_target2str(target), INV_FLAG_STR(entry->ip.invflags & IPT_INV_PROTO), iptables_proto2str(entry->ip.proto), @@ -165,7 +165,7 @@ static void iptables_print_chain(FAR const struct ipt_replace *repl, iptables_addr2str(entry->ip.src, entry->ip.smsk, src, sizeof(src)), INV_FLAG_STR(entry->ip.invflags & IPT_INV_DSTIP), iptables_addr2str(entry->ip.dst, entry->ip.dmsk, dst, sizeof(dst)), - iptables_match2str(match)); + iptables_match2str(IPT_MATCH(entry), mlen)); } printf("\n"); @@ -347,10 +347,18 @@ static int iptables_filter_command(FAR const struct iptables_args_s *args) /* Get entry and fill in proto-specific details. */ - if (args->sport != NULL || args->dport != NULL) + if (args->sport != NULL || args->dport != NULL || + IPTABLES_HAS_TCPFLAGS(args)) { FAR struct xt_udp *tcpudp; + if (IPTABLES_HAS_TCPFLAGS(args) && args->protocol != IPPROTO_TCP) + { + printf("TCP flags are only supported for TCP!\n"); + ret = -EINVAL; + goto errout; + } + if (args->protocol != IPPROTO_TCP && args->protocol != IPPROTO_UDP) { printf("Source/destination port is only supported for TCP/UDP!\n"); @@ -358,7 +366,11 @@ static int iptables_filter_command(FAR const struct iptables_args_s *args) goto errout; } - entry = netlib_ipt_filter_entry(args->target, args->verdict, + entry = args->limit_avg != 0 ? + netlib_ipt_filter_entry_limit(args->target, args->verdict, + args->protocol, args->limit_avg, + args->limit_burst) : + netlib_ipt_filter_entry(args->target, args->verdict, args->protocol); if (entry == NULL) { @@ -372,6 +384,8 @@ static int iptables_filter_command(FAR const struct iptables_args_s *args) { case IPPROTO_TCP: ((FAR struct xt_tcp *)tcpudp)->invflags = args->tcpudpinv; + ((FAR struct xt_tcp *)tcpudp)->flg_mask = args->tcp_flg_mask; + ((FAR struct xt_tcp *)tcpudp)->flg_cmp = args->tcp_flg_cmp; break; case IPPROTO_UDP: @@ -404,7 +418,11 @@ static int iptables_filter_command(FAR const struct iptables_args_s *args) goto errout; } - entry = netlib_ipt_filter_entry(args->target, args->verdict, + entry = args->limit_avg != 0 ? + netlib_ipt_filter_entry_limit(args->target, args->verdict, + args->protocol, args->limit_avg, + args->limit_burst) : + netlib_ipt_filter_entry(args->target, args->verdict, args->protocol); if (entry == NULL) { @@ -426,7 +444,11 @@ static int iptables_filter_command(FAR const struct iptables_args_s *args) } else { - entry = netlib_ipt_filter_entry(args->target, args->verdict, 0); + entry = args->limit_avg != 0 ? + netlib_ipt_filter_entry_limit(args->target, args->verdict, 0, + args->limit_avg, + args->limit_burst) : + netlib_ipt_filter_entry(args->target, args->verdict, 0); if (entry == NULL) { goto errout_prepare_entry; diff --git a/system/iptables/iptables.h b/system/iptables/iptables.h index a5dedbb9f54..487fb3836de 100644 --- a/system/iptables/iptables.h +++ b/system/iptables/iptables.h @@ -42,6 +42,15 @@ #define INV_FLAG_STR(flag) ((flag) ? "!" : "") +/* The rule has a TCP flags match (--tcp-flags, --syn). "--tcp-flags NONE + * NONE" matches all packets and needs no match, but its inversion matches + * no packet. + */ + +#define IPTABLES_HAS_TCPFLAGS(args) \ + ((args)->tcp_flg_mask != 0 || (args)->tcp_flg_cmp != 0 || \ + ((args)->tcpudpinv & XT_TCP_INV_FLAGS) != 0) + /**************************************************************************** * Public Type Definitions ****************************************************************************/ @@ -74,9 +83,14 @@ struct iptables_args_s FAR const char *dport; FAR const char *icmp_type; + uint32_t limit_avg; /* -m limit: seconds * XT_LIMIT_SCALE, 0 = none */ + uint32_t limit_burst; /* --limit-burst */ + int8_t verdict; int8_t rulenum; uint8_t protocol; + uint8_t tcp_flg_mask; /* --tcp-flags, --syn: flags to examine, 0 = none */ + uint8_t tcp_flg_cmp; /* --tcp-flags, --syn: value of those flags */ /* invert flags */ @@ -197,10 +211,11 @@ FAR const char *iptables_proto2str(uint8_t proto); * Name: iptables_match2str * * Description: - * Get match details from match structure + * Get match details from the matches of an entry (len bytes) * ****************************************************************************/ -FAR const char *iptables_match2str(FAR const struct xt_entry_match *match); +FAR const char *iptables_match2str(FAR const struct xt_entry_match *match, + size_t len); #endif /* __APPS_SYSTEM_IPTABLES_IPTABLES_H */ diff --git a/system/iptables/iptables_utils.c b/system/iptables/iptables_utils.c index 86c1492de27..b82e8243f64 100644 --- a/system/iptables/iptables_utils.c +++ b/system/iptables/iptables_utils.c @@ -28,11 +28,13 @@ #include #include +#include #include #include #include #include #include +#include #include #include @@ -44,7 +46,33 @@ * Pre-processor Definitions ****************************************************************************/ -#define MATCH_BUFSIZE sizeof("tcp spts:!65535:65535 dpts:!65535:65535") +#define MATCH_BUFSIZE 128 + +/* The TCP header flags, as the Linux iptables tool defines them */ + +#define IPTABLES_TCP_FIN 0x01 +#define IPTABLES_TCP_SYN 0x02 +#define IPTABLES_TCP_RST 0x04 +#define IPTABLES_TCP_PSH 0x08 +#define IPTABLES_TCP_ACK 0x10 +#define IPTABLES_TCP_URG 0x20 +#define IPTABLES_TCP_ALL 0x3f + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct iptables_tcpflag_s +{ + FAR const char *name; + uint8_t flag; +}; + +struct iptables_rateunit_s +{ + FAR const char *name; + uint32_t secs; +}; /**************************************************************************** * Private Data @@ -55,6 +83,26 @@ static FAR const char *g_hooknames[] = "PREROUTING", "INPUT", "FORWARD", "OUTPUT", "POSTROUTING" }; +static const struct iptables_tcpflag_s g_tcpflags[] = +{ + {"FIN", IPTABLES_TCP_FIN}, + {"SYN", IPTABLES_TCP_SYN}, + {"RST", IPTABLES_TCP_RST}, + {"PSH", IPTABLES_TCP_PSH}, + {"ACK", IPTABLES_TCP_ACK}, + {"URG", IPTABLES_TCP_URG}, + {"ALL", IPTABLES_TCP_ALL}, + {"NONE", 0} +}; + +static const struct iptables_rateunit_s g_rateunits[] = +{ + {"second", 1}, + {"minute", 60}, + {"hour", 60 * 60}, + {"day", 24 * 60 * 60} +}; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -203,6 +251,204 @@ static FAR const char *iptables_parse_target(FAR const char *str, } } +/**************************************************************************** + * Name: iptables_parse_tcpflags + * + * Description: + * Parse a comma separated list of TCP flag names (an argument of + * --tcp-flags) to a flag mask. The names can be in upper or lower case. + * "ALL" is all flags, "NONE" is no flag. + * + * Returned Value: + * The flag mask on success, or a negative error code on failure + * + ****************************************************************************/ + +static int iptables_parse_tcpflags(FAR const char *str) +{ + FAR const char *p = str; + size_t len; + size_t i; + int mask = 0; + + while (*p != '\0') + { + len = strcspn(p, ","); + for (i = 0; i < nitems(g_tcpflags); i++) + { + if (strlen(g_tcpflags[i].name) == len && + strncasecmp(p, g_tcpflags[i].name, len) == 0) + { + mask |= g_tcpflags[i].flag; + break; + } + } + + if (i == nitems(g_tcpflags)) + { + printf("Unknown TCP flag %.*s!\n", (int)len, p); + return -EINVAL; + } + + p += len; + if (*p == ',') + { + p++; + } + } + + return mask; +} + +/**************************************************************************** + * Name: iptables_parse_rate + * + * Description: + * Parse a rate (an argument of --limit) like "5/second", "3/m" or "100" + * (per second) to the average time between two packets, in seconds * + * XT_LIMIT_SCALE, as struct xt_rateinfo has it. + * + * Returned Value: + * 0 on success, or a negative error code on failure + * + ****************************************************************************/ + +static int iptables_parse_rate(FAR const char *str, FAR uint32_t *avg) +{ + unsigned long rate; + uint32_t per = 1; + FAR char *end; + size_t len; + size_t i; + + rate = strtoul(str, &end, 10); + if (end == str || rate == 0) + { + return -EINVAL; + } + + if (*end == '/') + { + end++; + len = strlen(end); + for (i = 0; i < nitems(g_rateunits); i++) + { + if (len > 0 && strncmp(end, g_rateunits[i].name, len) == 0) + { + per = g_rateunits[i].secs; + break; + } + } + + if (i == nitems(g_rateunits)) + { + return -EINVAL; + } + } + else if (*end != '\0') + { + return -EINVAL; + } + + if (rate > XT_LIMIT_SCALE * per) + { + return -ERANGE; + } + + *avg = XT_LIMIT_SCALE * per / rate; + return OK; +} + +/**************************************************************************** + * Name: iptables_onematch2str + * + * Description: + * Format the details of one match, after the separator sep + * + * Returned Value: + * The return value of snprintf() + * + ****************************************************************************/ + +static int iptables_onematch2str(FAR const struct xt_entry_match *match, + FAR const char *sep, FAR char *buf, + size_t size) +{ + FAR const void *data = match + 1; + + if (strcmp(match->u.user.name, XT_MATCH_NAME_TCP) == 0) + { + FAR const struct xt_tcp *tcp = data; + char flags[24] = ""; + + if (tcp->flg_mask != 0 || tcp->flg_cmp != 0 || + (tcp->invflags & XT_TCP_INV_FLAGS) != 0) + { + snprintf(flags, sizeof(flags), " flags:%s0x%02x/0x%02x", + INV_FLAG_STR(tcp->invflags & XT_TCP_INV_FLAGS), + tcp->flg_mask, tcp->flg_cmp); + } + + return snprintf(buf, size, "%stcp spts:%s%u:%u dpts:%s%u:%u%s", sep, + INV_FLAG_STR(tcp->invflags & XT_TCP_INV_SRCPT), + tcp->spts[0], tcp->spts[1], + INV_FLAG_STR(tcp->invflags & XT_TCP_INV_DSTPT), + tcp->dpts[0], tcp->dpts[1], flags); + } + else if (strcmp(match->u.user.name, XT_MATCH_NAME_UDP) == 0) + { + FAR const struct xt_udp *udp = data; + + return snprintf(buf, size, "%sudp spts:%s%u:%u dpts:%s%u:%u", sep, + INV_FLAG_STR(udp->invflags & XT_UDP_INV_SRCPT), + udp->spts[0], udp->spts[1], + INV_FLAG_STR(udp->invflags & XT_UDP_INV_DSTPT), + udp->dpts[0], udp->dpts[1]); + } + else if (strcmp(match->u.user.name, XT_MATCH_NAME_ICMP) == 0) + { + FAR const struct ipt_icmp *icmp = data; + + return snprintf(buf, size, "%sicmp %stype %u", sep, + INV_FLAG_STR(icmp->invflags & IPT_ICMP_INV), + icmp->type); + } + else if (strcmp(match->u.user.name, XT_MATCH_NAME_ICMP6) == 0) + { + FAR const struct ip6t_icmp *icmp6 = data; + + return snprintf(buf, size, "%sipv6-icmp %stype %u", sep, + INV_FLAG_STR(icmp6->invflags & IP6T_ICMP_INV), + icmp6->type); + } + else if (strcmp(match->u.user.name, XT_MATCH_NAME_LIMIT) == 0) + { + FAR const struct xt_rateinfo *rate = data; + uint32_t mult; + size_t i; + + /* Show the rate in the smallest unit that keeps it exact enough, + * as Linux iptables does. + */ + + for (i = nitems(g_rateunits) - 1; rate->avg != 0 && i > 0; i--) + { + mult = XT_LIMIT_SCALE * g_rateunits[i - 1].secs; + if (rate->avg > mult || mult / rate->avg < mult % rate->avg) + { + break; + } + } + + mult = XT_LIMIT_SCALE * g_rateunits[i].secs; + return snprintf(buf, size, "%slimit: avg %" PRIu32 "/%s burst %" + PRIu32, sep, rate->avg != 0 ? mult / rate->avg : 0, + g_rateunits[i].name, rate->burst); + } + + return snprintf(buf, size, "%s%s", sep, match->u.user.name); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -433,6 +679,122 @@ int iptables_parse(FAR struct iptables_args_s *args, continue; } + /* TCP flags. --syn is "--tcp-flags SYN,RST,ACK,FIN SYN". */ + + if (strcmp(argv[i], "--tcp-flags") == 0) + { + int mask; + int cmp; + + if (i + 2 >= argc) + { + printf("Missing TCP flags!\n"); + return -EINVAL; + } + + mask = iptables_parse_tcpflags(argv[++i]); + cmp = iptables_parse_tcpflags(argv[++i]); + if (mask < 0 || cmp < 0) + { + return -EINVAL; + } + + args->tcp_flg_mask = mask; + args->tcp_flg_cmp = cmp; + if (inv) + { + args->tcpudpinv |= XT_TCP_INV_FLAGS; + inv = false; + } + + continue; + } + + if (strcmp(argv[i], "--syn") == 0) + { + args->tcp_flg_mask = IPTABLES_TCP_SYN | IPTABLES_TCP_RST | + IPTABLES_TCP_ACK | IPTABLES_TCP_FIN; + args->tcp_flg_cmp = IPTABLES_TCP_SYN; + if (inv) + { + args->tcpudpinv |= XT_TCP_INV_FLAGS; + inv = false; + } + + continue; + } + + /* Match extension, only "limit". As in Linux, "!" cannot invert + * the match or its options. + */ + + if ((strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--match") == 0 || + strcmp(argv[i], "--limit") == 0 || + strcmp(argv[i], "--limit-burst") == 0) && inv) + { + printf("! is not supported before %s!\n", argv[i]); + return -EINVAL; + } + + if (strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--match") == 0) + { + if (++i >= argc) + { + printf("Missing match name!\n"); + return -EINVAL; + } + + if (strcmp(argv[i], XT_MATCH_NAME_LIMIT) != 0) + { + printf("Unsupported match %s!\n", argv[i]); + return -EINVAL; + } + + /* The defaults of Linux: 3 per hour, burst 5 */ + + args->limit_avg = XT_LIMIT_SCALE * 60 * 60 / 3; + args->limit_burst = 5; + continue; + } + + if (strcmp(argv[i], "--limit") == 0) + { + if (args->limit_avg == 0 || ++i >= argc) + { + printf("--limit needs -m limit and a rate!\n"); + return -EINVAL; + } + + if (iptables_parse_rate(argv[i], &args->limit_avg) < 0) + { + printf("Invalid rate %s!\n", argv[i]); + return -EINVAL; + } + + continue; + } + + if (strcmp(argv[i], "--limit-burst") == 0) + { + FAR char *end; + + if (args->limit_avg == 0 || ++i >= argc) + { + printf("--limit-burst needs -m limit and a number!\n"); + return -EINVAL; + } + + args->limit_burst = strtoul(argv[i], &end, 10); + if (*end != '\0' || args->limit_burst == 0 || + args->limit_burst > 10000) + { + printf("Invalid burst %s (1 to 10000)!\n", argv[i]); + return -EINVAL; + } + + continue; + } + /* In interface */ if (strcmp(argv[i], "-i") == 0 || @@ -697,6 +1059,15 @@ void iptables_showusage(FAR const char *progname) printf(fmt, " ", "", "", "port[:port]", "Destination port"); printf(fmt, "[!]", "--icmp-type", "", "type", "ICMP type"); printf(fmt, "[!]", "--icmpv6-type", "", "type", "ICMPv6 type"); + printf(fmt, "[!]", "--tcp-flags", "", "mask comp", + "TCP flags (SYN,ACK,FIN,RST,URG,PSH,ALL,NONE)"); + printf(fmt, "[!]", "--syn", "", "", + "TCP SYN (--tcp-flags SYN,RST,ACK,FIN SYN)"); + printf(fmt, " ", "--match", "-m", "limit", "Match a packet rate"); + printf(fmt, " ", "--limit", "", "rate[/unit]", + "Packets per s, m, h or d (default: 3/hour)"); + printf(fmt, " ", "--limit-burst", "", "number", + "Packets that can come at once (default: 5)"); } /**************************************************************************** @@ -730,6 +1101,7 @@ FAR const char *iptables_target2str(FAR const struct xt_entry_target *tgt) if (strcmp(tgt->u.user.name, XT_STANDARD_TARGET) == 0) { int verdict = ((FAR const struct xt_standard_target *)tgt)->verdict; + verdict = -verdict - 1; if (verdict == NF_ACCEPT) @@ -788,62 +1160,32 @@ FAR const char *iptables_proto2str(uint8_t proto) * Name: iptables_match2str * * Description: - * Get match details from match structure + * Get match details from the matches of an entry * ****************************************************************************/ -FAR const char *iptables_match2str(FAR const struct xt_entry_match *match) +FAR const char *iptables_match2str(FAR const struct xt_entry_match *match, + size_t len) { static char s_buf[MATCH_BUFSIZE]; + FAR const uint8_t *base = (FAR const uint8_t *)match; + FAR const char *sep; + size_t off = 0; + size_t n = 0; - if (match == NULL) - { - return ""; - } - - if (strcmp(match->u.user.name, XT_MATCH_NAME_TCP) == 0) - { - FAR struct xt_tcp *tcp = (FAR struct xt_tcp *)(match + 1); - - snprintf(s_buf, MATCH_BUFSIZE, - "tcp spts:%s%u:%u dpts:%s%u:%u", - INV_FLAG_STR(tcp->invflags & XT_TCP_INV_SRCPT), - tcp->spts[0], tcp->spts[1], - INV_FLAG_STR(tcp->invflags & XT_TCP_INV_DSTPT), - tcp->dpts[0], tcp->dpts[1]); - } - else if (strcmp(match->u.user.name, XT_MATCH_NAME_UDP) == 0) - { - FAR struct xt_udp *udp = (FAR struct xt_udp *)(match + 1); - - snprintf(s_buf, MATCH_BUFSIZE, - "udp spts:%s%u:%u dpts:%s%u:%u", - INV_FLAG_STR(udp->invflags & XT_UDP_INV_SRCPT), - udp->spts[0], udp->spts[1], - INV_FLAG_STR(udp->invflags & XT_UDP_INV_DSTPT), - udp->dpts[0], udp->dpts[1]); - } - else if (strcmp(match->u.user.name, XT_MATCH_NAME_ICMP) == 0) + s_buf[0] = '\0'; + while (off + sizeof(*match) <= len && n < sizeof(s_buf)) { - FAR struct ipt_icmp *icmp = (FAR struct ipt_icmp *)(match + 1); - - snprintf(s_buf, MATCH_BUFSIZE, - "icmp %stype %u", - INV_FLAG_STR(icmp->invflags & IPT_ICMP_INV), - icmp->type); - } - else if (strcmp(match->u.user.name, XT_MATCH_NAME_ICMP6) == 0) - { - FAR struct ip6t_icmp *icmp6 = (FAR struct ip6t_icmp *)(match + 1); + match = (FAR const struct xt_entry_match *)(base + off); + if (match->u.match_size < sizeof(*match) || + off + match->u.match_size > len) + { + break; + } - snprintf(s_buf, MATCH_BUFSIZE, - "ipv6-icmp %stype %u", - INV_FLAG_STR(icmp6->invflags & IP6T_ICMP_INV), - icmp6->type); - } - else - { - return match->u.user.name; + sep = n > 0 ? " " : ""; + n += iptables_onematch2str(match, sep, s_buf + n, sizeof(s_buf) - n); + off += match->u.match_size; } return s_buf;