netfilter: introduce l2tp match extension
Subsystems (2)
PATCH v4 1/1 netfilter: introduce l2tp match extension
Introduce an xtables add-on for matching L2TP packets. Supports L2TPv2 and L2TPv3 over IPv4 and IPv6. As well as filtering on L2TP tunnel-id and session-id, the filtering decision can also include the L2TP packet type (control or data), protocol version (2 or 3) and encapsulation type (UDP or IP).
The most common use for this will likely be to filter L2TP data packets of individual L2TP tunnels or sessions. While a u32 match can be used, the L2TP protocol headers are such that field offsets differ depending on bits set in the header, making rules for matching generic L2TP connections cumbersome. This match extension takes care of all that.
An iptables patch will be submitted separately.
---
Changes in v2:
Address comments from Patrick McHardy:-
- Added checkentry function to check args passed into kernel.
Changes in v3:
Address comments from Pablo Neira Ayuso:-
- Remove debug code.
- Avoid multiple nested if statements when they are unnecessary.
- Fix data access to use skb_header_pointer() properly.
- Use #defines for L2TP packet header bit definitions.
- Improve comments to clarify how variations in L2TP header field locations are handled when parsing header fields.
Changes in v4:
Address comments from Pablo Neira Ayuso:-
- Remove packet layout diagrams which are c&p'd from the RFCs.
- Use ip6_find_hdr() to get the IP protocol inside IPv6 packets. After this change, the common match code path thru l2tp_mt_common() was not useful so has been removed and l2tp_mt_udp() or l2tp_mt_ip() is called directly instead.
- Require encap to be specified
--- include/uapi/linux/netfilter/xt_l2tp.h | 36 ++++ net/netfilter/Kconfig | 10 + net/netfilter/Makefile | 1 + net/netfilter/xt_l2tp.c | 305 ++++++++++++++++++++++++++++++++ 4 files changed, 352 insertions(+), 0 deletions(-)
include/uapi/linux/netfilter/xt_l2tp.h new file +36 -0
@@ -0,0 +1,36 @@ +#ifndef _LINUX_NETFILTER_XT_L2TP_H +#define _LINUX_NETFILTER_XT_L2TP_H + +#include <linux/types.h> + +enum xt_l2tp_encap { +XT_L2TP_ENCAP_UDP , +XT_L2TP_ENCAP_IP , +}; + +enum xt_l2tp_type { +XT_L2TP_TYPE_CONTROL , +XT_L2TP_TYPE_DATA , +}; + +/* L2TP matching stuff */ +struct xt_l2tp_info { +__u32 tid;/* tunnel id */ +__u32 sid;/* session id */ +__u8 version;/* L2TP protocol version */ +__u8 encap;/* L2TP encapsulation type */ +__u8 type;/* L2TP packet type */ +__u8 flags;/* which fields to match */ +}; + +enum { +XT_L2TP_TID = (1 <<0 ),/* match L2TP tunnel id */ +XT_L2TP_SID = (1 <<1 ),/* match L2TP session id */ +XT_L2TP_VERSION = (1 <<2 ),/* match L2TP protocol version */ +XT_L2TP_ENCAP = (1 <<3 ),/* match L2TP encapsulation type */ +XT_L2TP_TYPE = (1 <<4 ),/* match L2TP packet type */ +}; + + +#endif /* _LINUX_NETFILTER_XT_L2TP_H */ +
net/netfilter/Kconfig +10 -0
@@ -1055,6 +1055,16 @@ config NETFILTER_XT_MATCH_IPVS If unsure, say N. +config NETFILTER_XT_MATCH_L2TP + tristate '"l2tp" match support' + depends on NETFILTER_ADVANCED + default L2TP + ---help--- + This option adds an "L2TP" match, which allows you to match against + L2TP protocol header fields. + + To compile it as a module, choose M here. If unsure, say N. + config NETFILTER_XT_MATCH_LENGTH tristate '"length" match support' depends on NETFILTER_ADVANCED
net/netfilter/Makefile +1 -0
@@ -135,6 +135,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_HELPER) += xt_helper.o obj-$(CONFIG_NETFILTER_XT_MATCH_HL) += xt_hl.o obj-$(CONFIG_NETFILTER_XT_MATCH_IPRANGE) += xt_iprange.o obj-$(CONFIG_NETFILTER_XT_MATCH_IPVS) += xt_ipvs.o +obj-$(CONFIG_NETFILTER_XT_MATCH_L2TP) += xt_l2tp.o obj-$(CONFIG_NETFILTER_XT_MATCH_LENGTH) += xt_length.o obj-$(CONFIG_NETFILTER_XT_MATCH_LIMIT) += xt_limit.o obj-$(CONFIG_NETFILTER_XT_MATCH_MAC) += xt_mac.o
net/netfilter/xt_l2tp.c new file +305 -0
305 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Pablo Neira Ayuso
maintainerHi,
Quoted 6 lines · James Chapman
Introduce an xtables add-on for matching L2TP packets. Supports L2TPv2 and L2TPv3 over IPv4 and IPv6. As well as filtering on L2TP tunnel-id and session-id, the filtering decision can also include the L2TP packet type (control or data), protocol version (2 or 3) and encapsulation type (UDP or IP).
Quoted 8 lines
The most common use for this will likely be to filter L2TP data packets of individual L2TP tunnels or sessions. While a u32 match can be used, the L2TP protocol headers are such that field offsets differ depending on bits set in the header, making rules for matching generic L2TP connections cumbersome. This match extension takes care of all that.
An iptables patch will be submitted separately.
I'm testing this with the last userspace iptables patch that you posted [1]. I'm using the example in the manpage:
# iptables -A INPUT -s 1.2.3.4 -m l2tp --tid 42 iptables: Invalid argument. Run `dmesg' for more information. # dmesg ... [ 490.827569] xt_l2tp: missing encapsulation
The error message is added by the patch I made on top of your last kernel patch (find it attached, feel free to merge it to your next v5).
Quoted 5 lines
net/netfilter/xt_l2tp.c new file +0
-0
[...]
Quoted 27 lines
net/netfilter/xt_l2tp.c new file +27
-0+static int l2tp_mt_check (const struct xt_mtchk_param *par) +{ +struct xt_l2tp_info *info = par->matchinfo; + +/* Check for invalid flags */ +if (info->flags & ~(XT_L2TP_TID |XT_L2TP_SID |XT_L2TP_VERSION | +XT_L2TP_ENCAP |XT_L2TP_TYPE )) +return -EINVAL ; + +/* At least one of tid, sid or type=control must be specified */ +if ((!(info->flags &XT_L2TP_TID )) && + (!(info->flags &XT_L2TP_SID )) && + ((!(info->flags &XT_L2TP_TYPE )) || + (info->type !=XT_L2TP_TYPE_CONTROL ))) +return -EINVAL ; + +/* If version 2 is specified, check that incompatible params +* are not supplied +*/ +if (info->flags &XT_L2TP_VERSION ) { +if ((info->version <2 ) || (info->version >3 )) +return -EINVAL ; + +if (info->version ==2 ) { +if ((info->flags &XT_L2TP_TID ) && + (info->tid >0xffff )) +return -EINVAL ;
net/netfilter/xt_l2tp.c line 254
Quoted 10 lines
net/netfilter/xt_l2tp.c new file +10
-0+if ((info->flags &XT_L2TP_SID ) && + (info->sid >0xffff )) +return -EINVAL ; +if ((info->flags &XT_L2TP_ENCAP ) && + (info->encap ==XT_L2TP_ENCAP_IP )) +return -EINVAL ; + +/* Force UDP encap */ + info->encap =XT_L2TP_ENCAP_UDP ; + info->flags |=XT_L2TP_ENCAP ;
Note that changing the info data area from the kernel side is problematic in iptables, eg.
# iptables -A INPUT -s 1.2.3.4/32 -m l2tp --tid 42 --pversion 2 # iptables -D INPUT -s 1.2.3.4/32 -m l2tp --tid 42 --pversion 2 iptables: Bad rule (does a matching rule exist in that chain?).
Userspace needs to get exactly what it passes, no changes are allowed. I think you need explicit option passing to indicate the behaviour you want.
net/netfilter/xt_l2tp.c line 263
Quoted 9 lines
net/netfilter/xt_l2tp.c new file +9
-0+ } + } + +/* Encap must be specified */ +if (!(info->flags &XT_L2TP_ENCAP )) +return -EINVAL ; + +return 0 ; +}
Please, review this logic in the _check() function and send me a v5, including a refreshed iptables version.
It would be great if you can include a list of iptables commands using your l2tp match that you have used to validate this in the next round. Thanks.
Pablo Neira Ayuso
maintainerOn Thu, Jan 02, 2014 at 09:57:13PM +0100, Pablo Neira Ayuso wrote:
[...]
Quoted 11 lines
I'm testing this with the last userspace iptables patch that you posted [1]. I'm using the example in the manpage:
# iptables -A INPUT -s 1.2.3.4 -m l2tp --tid 42 iptables: Invalid argument. Run `dmesg' for more information. # dmesg ... [ 490.827569] xt_l2tp: missing encapsulationThe error message is added by the patch I made on top of your last kernel patch (find it attached, feel free to merge it to your next v5).
Forgot attachment, here it comes.
net/netfilter/xt_l2tp.c +21 -7
@@ -221,33 +221,45 @@ static int l2tp_mt_check(const struct xt_mtchk_param *par)/* Check for invalid flags */ if (info->flags & ~(XT_L2TP_TID |XT_L2TP_SID |XT_L2TP_VERSION | -XT_L2TP_ENCAP |XT_L2TP_TYPE )) +XT_L2TP_ENCAP |XT_L2TP_TYPE )) { +pr_info ("unknown flags: %x\n" , info->flags);return -EINVAL ; + }/* At least one of tid, sid or type=control must be specified */ if ((!(info->flags &XT_L2TP_TID )) && (!(info->flags &XT_L2TP_SID )) && ((!(info->flags &XT_L2TP_TYPE )) || - (info->type !=XT_L2TP_TYPE_CONTROL ))) + (info->type !=XT_L2TP_TYPE_CONTROL ))) { +pr_info ("invalid flags combination: %x\n" , info->flags);return -EINVAL ; + }/* If version 2 is specified, check that incompatible params * are not supplied */ if (info->flags &XT_L2TP_VERSION ) { -if ((info->version <2 ) || (info->version >3 )) +if ((info->version <2 ) || (info->version >3 )) { +pr_info ("wrong L2TP version: %u\n" , info->version);return -EINVAL ; + }if (info->version ==2 ) {if ((info->flags &XT_L2TP_TID ) && - (info->tid >0xffff )) + (info->tid >0xffff )) { +pr_info ("tid > 0xffff: %u\n" , info->tid);return -EINVAL ; + }if ((info->flags &XT_L2TP_SID ) && - (info->sid >0xffff )) + (info->sid >0xffff )) { +pr_info ("sid > 0xffff: %u\n" , info->sid);return -EINVAL ; + }if ((info->flags &XT_L2TP_ENCAP ) && - (info->encap ==XT_L2TP_ENCAP_IP )) + (info->encap ==XT_L2TP_ENCAP_IP )) { +pr_info ("v2 doesn't support IP mode\n" );return -EINVAL ; + }/* Force UDP encap */ info->encap =XT_L2TP_ENCAP_UDP ;
@@ -256,8 +268,10 @@ static int l2tp_mt_check(const struct xt_mtchk_param *par) }/* Encap must be specified */ -if (!(info->flags &XT_L2TP_ENCAP )) +if (!(info->flags &XT_L2TP_ENCAP )) { +pr_info ("missing encapsulation\n" );return -EINVAL ; + }return 0 ; }
Quoted 4 lines · Pablo Neira Ayuso
Hi,
On Thu, Dec 12, 2013 at 02:13:42PM +0000, James Chapman wrote:
Quoted 8 lines
I'm testing this with the last userspace iptables patch that you posted [1]. I'm using the example in the manpage:
# iptables -A INPUT -s 1.2.3.4 -m l2tp --tid 42 iptables: Invalid argument. Run `dmesg' for more information. # dmesg ... [ 490.827569] xt_l2tp: missing encapsulation
By bad. I made the encap type a required param to the kernel as a result of previous comments and updated the iptables patch to enforce this, but I didn't submit the updated iptables patch.
Quoted 11 lines
Note that changing the info data area from the kernel side is problematic in iptables, eg.
# iptables -A INPUT -s 1.2.3.4/32 -m l2tp --tid 42 --pversion 2 # iptables -D INPUT -s 1.2.3.4/32 -m l2tp --tid 42 --pversion 2 iptables: Bad rule (does a matching rule exist in that chain?).Userspace needs to get exactly what it passes, no changes are allowed. I think you need explicit option passing to indicate the behaviour you want.
Quoted 12 lines
net/netfilter/xt_l2tp.c line 263
Quoted 9 lines
net/netfilter/xt_l2tp.c new file +9
-0+ } + } + +/* Encap must be specified */ +if (!(info->flags &XT_L2TP_ENCAP )) +return -EINVAL ; + +return 0 ; +}Please, review this logic in the
_check()function and send me a v5, including a refreshed iptables version.
On reflection, having the kernel derive an encap_type if the version is set 2 is unnecessary - it is better to have the iptables command parser do this and just have the kernel reject the request if encap_type is not specified, rather than trying to fake it as being set to udp itself when version is 2.
Quoted 4 lines
It would be great if you can include a list of iptables commands using your l2tp match that you have used to validate this in the next round. Thanks.
Will do. Thanks.