netfilter: add segment routing header 'srh' match
Subsystems (3)
It allows matching packets based on Segment Routing Header (SRH) information.
The implementation considers revision 7 of the SRH draft.
https://tools.ietf.org/html/draft-ietf-6man-segment-routing-header-07
Currently supported match options include:
(1) Next Header
(2) Hdr Ext Len
(3) Segments Left
(4) Last Entry
(5) Tag value of SRH
--- include/uapi/linux/netfilter_ipv6/ip6t_srh.h | 63 ++++++++++ net/ipv6/netfilter/Kconfig | 9 ++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/ip6t_srh.c | 165 +++++++++++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 include/uapi/linux/netfilter_ipv6/ip6t_srh.h create mode 100644 net/ipv6/netfilter/ip6t_srh.c
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +63 -0
@@ -0,0 +1,63 @@ +/** +* Definitions for Segment Routing Header 'srh' match +* +* Author: +* Ahmed Abdelsalam <[email protected]> +*/ + +#ifndef _IP6T_SRH_H +#define _IP6T_SRH_H + +#include <linux/types.h> +#include <linux/netfilter.h> + +/* Values for "mt_flags" field in struct ip6t_srh */ +#define IP6T_SRH_NEXTHDR 0x0001 +#define IP6T_SRH_LEN_EQ 0x0002 +#define IP6T_SRH_LEN_GT 0x0004 +#define IP6T_SRH_LEN_LT 0x0008 +#define IP6T_SRH_SEGS_EQ 0x0010 +#define IP6T_SRH_SEGS_GT 0x0020 +#define IP6T_SRH_SEGS_LT 0x0040 +#define IP6T_SRH_LAST_EQ 0x0080 +#define IP6T_SRH_LAST_GT 0x0100 +#define IP6T_SRH_LAST_LT 0x0200 +#define IP6T_SRH_TAG 0x0400 +#define IP6T_SRH_MASK 0x07FF + +/* Values for "mt_invflags" field in struct ip6t_srh */ +#define IP6T_SRH_INV_NEXTHDR 0x0001 +#define IP6T_SRH_INV_LEN_EQ 0x0002 +#define IP6T_SRH_INV_LEN_GT 0x0004 +#define IP6T_SRH_INV_LEN_LT 0x0008 +#define IP6T_SRH_INV_SEGS_EQ 0x0010 +#define IP6T_SRH_INV_SEGS_GT 0x0020 +#define IP6T_SRH_INV_SEGS_LT 0x0040 +#define IP6T_SRH_INV_LAST_EQ 0x0080 +#define IP6T_SRH_INV_LAST_GT 0x0100 +#define IP6T_SRH_INV_LAST_LT 0x0200 +#define IP6T_SRH_INV_TAG 0x0400 +#define IP6T_SRH_INV_MASK 0x07FF + +/** +* struct ip6t_srh - SRH match options +* @ next_hdr: Next header field of SRH +* @ hdr_len: Extension header length field of SRH +* @ segs_left: Segments left field of SRH +* @ last_entry: Last entry field of SRH +* @ tag: Tag field of SRH +* @ mt_flags: match options +* @ mt_invflags: Invert the sense of match options +*/ + +struct ip6t_srh { +__u8 next_hdr; +__u8 hdr_len; +__u8 segs_left; +__u8 last_entry; +__u16 tag; +__u16 mt_flags; +__u16 mt_invflags; +}; + +#endif /*_IP6T_SRH_H*/
net/ipv6/netfilter/Kconfig +9 -0
@@ -232,6 +232,15 @@ config IP6_NF_MATCH_RT To compile it as a module, choose M here. If unsure, say N. +config IP6_NF_MATCH_SRH + tristate '"srh" Segment Routing header match support' + depends on NETFILTER_ADVANCED + help + srh matching allows you to match packets based on the segment + routing header of the packet. + + To compile it as a module, choose M here. If unsure, say N. + # The targets config IP6_NF_TARGET_HL tristate '"HL" hoplimit target support'
net/ipv6/netfilter/Makefile +1 -0
@@ -54,6 +54,7 @@ obj-$(CONFIG_IP6_NF_MATCH_MH) += ip6t_mh.o obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_hbh.o obj-$(CONFIG_IP6_NF_MATCH_RPFILTER) += ip6t_rpfilter.o obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o +obj-$(CONFIG_IP6_NF_MATCH_SRH) += ip6t_srh.o # targets obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.o
net/ipv6/netfilter/ip6t_srh.c new file +165 -0
165 lines not on this page · raw message
Signature · 1 line
2.1.4
Quoted 297 lines · On Fri, 29 Dec 2017 12:07:52 +0100 Ahmed Abdelsalam <[email protected]> wrote:
It allows matching packets based on Segment Routing Header (SRH) information.
The implementation considers revision 7 of the SRH draft.
https://tools.ietf.org/html/draft-ietf-6man-segment-routing-header-07Currently supported match options include:
(1) Next Header
(2) Hdr Ext Len
(3) Segments Left
(4) Last Entry
(5) Tag value of SRHSigned-off-by Ahmed Abdelsalam <[email protected]>--- include/uapi/linux/netfilter_ipv6/ip6t_srh.h | 63 ++++++++++ net/ipv6/netfilter/Kconfig | 9 ++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/ip6t_srh.c | 165 +++++++++++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 include/uapi/linux/netfilter_ipv6/ip6t_srh.h create mode 100644 net/ipv6/netfilter/ip6t_srh.cinclude/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +63
-0@@ -0,0 +1,63 @@ +/** +* Definitions for Segment Routing Header 'srh' match +* +* Author: +* Ahmed Abdelsalam <[email protected]> +*/ + +#ifndef _IP6T_SRH_H +#define _IP6T_SRH_H + +#include <linux/types.h> +#include <linux/netfilter.h> + +/* Values for "mt_flags" field in struct ip6t_srh */ +#define IP6T_SRH_NEXTHDR 0x0001 +#define IP6T_SRH_LEN_EQ 0x0002 +#define IP6T_SRH_LEN_GT 0x0004 +#define IP6T_SRH_LEN_LT 0x0008 +#define IP6T_SRH_SEGS_EQ 0x0010 +#define IP6T_SRH_SEGS_GT 0x0020 +#define IP6T_SRH_SEGS_LT 0x0040 +#define IP6T_SRH_LAST_EQ 0x0080 +#define IP6T_SRH_LAST_GT 0x0100 +#define IP6T_SRH_LAST_LT 0x0200 +#define IP6T_SRH_TAG 0x0400 +#define IP6T_SRH_MASK 0x07FF + +/* Values for "mt_invflags" field in struct ip6t_srh */ +#define IP6T_SRH_INV_NEXTHDR 0x0001 +#define IP6T_SRH_INV_LEN_EQ 0x0002 +#define IP6T_SRH_INV_LEN_GT 0x0004 +#define IP6T_SRH_INV_LEN_LT 0x0008 +#define IP6T_SRH_INV_SEGS_EQ 0x0010 +#define IP6T_SRH_INV_SEGS_GT 0x0020 +#define IP6T_SRH_INV_SEGS_LT 0x0040 +#define IP6T_SRH_INV_LAST_EQ 0x0080 +#define IP6T_SRH_INV_LAST_GT 0x0100 +#define IP6T_SRH_INV_LAST_LT 0x0200 +#define IP6T_SRH_INV_TAG 0x0400 +#define IP6T_SRH_INV_MASK 0x07FF + +/** +* struct ip6t_srh - SRH match options +* @ next_hdr: Next header field of SRH +* @ hdr_len: Extension header length field of SRH +* @ segs_left: Segments left field of SRH +* @ last_entry: Last entry field of SRH +* @ tag: Tag field of SRH +* @ mt_flags: match options +* @ mt_invflags: Invert the sense of match options +*/ + +struct ip6t_srh { +__u8 next_hdr; +__u8 hdr_len; +__u8 segs_left; +__u8 last_entry; +__u16 tag; +__u16 mt_flags; +__u16 mt_invflags; +}; + +#endif /*_IP6T_SRH_H*/ net/ipv6/netfilter/Kconfig +9
-0@@ -232,6 +232,15 @@ config IP6_NF_MATCH_RT To compile it as a module, choose M here. If unsure, say N. +config IP6_NF_MATCH_SRH + tristate '"srh" Segment Routing header match support' + depends on NETFILTER_ADVANCED + help + srh matching allows you to match packets based on the segment + routing header of the packet. + + To compile it as a module, choose M here. If unsure, say N. + # The targets config IP6_NF_TARGET_HL tristate '"HL" hoplimit target support'net/ipv6/netfilter/Makefile +1
-0@@ -54,6 +54,7 @@ obj-$(CONFIG_IP6_NF_MATCH_MH) += ip6t_mh.o obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_hbh.o obj-$(CONFIG_IP6_NF_MATCH_RPFILTER) += ip6t_rpfilter.o obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o +obj-$(CONFIG_IP6_NF_MATCH_SRH) += ip6t_srh.o # targets obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.onet/ipv6/netfilter/ip6t_srh.c new file +165
-0@@ -0,0 +1,165 @@ +/* +* Kernel module to match Segment Routing Header (SRH) parameters. +* +* Author: +* Ahmed Abdelsalam <[email protected]> +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> +#include <linux/skbuff.h> +#include <linux/ipv6.h> +#include <linux/types.h> +#include <net/ipv6.h> +#include <net/seg6.h> + +#include <linux/netfilter/x_tables.h> +#include <linux/netfilter_ipv6/ip6t_srh.h> +#include <linux/netfilter_ipv6/ip6_tables.h> + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Xtables: IPv6 Segment Routing Header match" ); +MODULE_AUTHOR ("Ahmed Abdelsalam <[email protected]>" ); + +/* Test a struct->mt_invflags and a boolean for inequality */ +#define NF_SRH_INVF (ptr, flag, boolean) \ + ((boolean) ^ !!((ptr)->mt_invflags & (flag))) + +static bool srh_mt6 (const struct sk_buff *skb,struct xt_action_param *par) +{ +const struct ip6t_srh *srhinfo = par->matchinfo; +struct ipv6_sr_hdr *srh; +struct ipv6_sr_hdr _srh; +int hdrlen, srhoff =0 ; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return false; + + srh =skb_header_pointer (skb, srhoff,sizeof (_srh), &_srh); + +if (!srh) +return false; + + hdrlen =ipv6_optlen (srh); +if (skb->len - srhoff < hdrlen) +return false; + +if (srh->type !=IPV6_SRCRT_TYPE_4 ) +return false; + +if (srh->segments_left > srh->first_segment) +return false; + +/* Next Header matching */ +if (srhinfo->mt_flags &IP6T_SRH_NEXTHDR ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_NEXTHDR , + !(srh->nexthdr == srhinfo->next_hdr))) +return false; + +/* Header Extension Length matching */ +if (srhinfo->mt_flags &IP6T_SRH_LEN_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_EQ , + !(srh->hdrlen == srhinfo->hdr_len))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LEN_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_GT , + !(srh->hdrlen > srhinfo->hdr_len))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LEN_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_LT , + !(srh->hdrlen < srhinfo->hdr_len))) +return false; + +/* Segments Left matching */ +if (srhinfo->mt_flags &IP6T_SRH_SEGS_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_EQ , + !(srh->segments_left == srhinfo->segs_left))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_SEGS_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_GT , + !(srh->segments_left > srhinfo->segs_left))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_SEGS_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_LT , + !(srh->segments_left < srhinfo->segs_left))) +return false; + +/** +* Last Entry matching +* Last_Entry field was introduced in revision 6 of the SRH draft. +* It was called First_Segment in the previous revision +*/ +if (srhinfo->mt_flags &IP6T_SRH_LAST_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_EQ , + !(srh->first_segment == srhinfo->last_entry))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LAST_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_GT , + !(srh->first_segment > srhinfo->last_entry))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LAST_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_LT , + !(srh->first_segment < srhinfo->last_entry))) +return false; + +/** +* Tag matchig +* Tag field was introduced in revision 6 of the SRH draft. +*/ +if (srhinfo->mt_flags &IP6T_SRH_TAG ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_TAG , + !(srh->tag == srhinfo->tag))) +return false; +return true; +} + +static int srh_mt6_check (const struct xt_mtchk_param *par) +{ +const struct ip6t_srh *srhinfo = par->matchinfo; + +if (srhinfo->mt_flags & ~IP6T_SRH_MASK ) { +pr_debug ("unknown srh match flags %X\n" , srhinfo->mt_flags); +return -EINVAL ; + } + +if (srhinfo->mt_invflags & ~IP6T_SRH_INV_MASK ) { +pr_debug ("unknown srh invflags %X\n" , srhinfo->mt_invflags); +return -EINVAL ; + } + +return 0 ; +} + +static struct xt_match srh_mt6_reg__read_mostly = { + .name ="srh" , + .family =NFPROTO_IPV6 , + .match = srh_mt6, + .matchsize =sizeof (struct ip6t_srh ), + .checkentry = srh_mt6_check, + .me =THIS_MODULE , +}; + +static int __init srh_mt6_init (void ) +{ +return xt_register_match (&srh_mt6_reg); +} + +static void __exit srh_mt6_exit(void) +{ +xt_unregister_match (&srh_mt6_reg); +} + +module_init (srh_mt6_init); +module_exit (srh_mt6_exit);Signature · 1 line
2.1.4
David,
Any comment on this patch ?
Signature · 1 line
Ahmed
Pablo Neira Ayuso
maintainerHi Ahmed,
Quoted 28 lines · Ahmed Abdelsalam
It allows matching packets based on Segment Routing Header (SRH) information.
The implementation considers revision 7 of the SRH draft.
https://tools.ietf.org/html/draft-ietf-6man-segment-routing-header-07Currently supported match options include:
(1) Next Header
(2) Hdr Ext Len
(3) Segments Left
(4) Last Entry
(5) Tag value of SRHSigned-off-by Ahmed Abdelsalam <[email protected]>--- include/uapi/linux/netfilter_ipv6/ip6t_srh.h | 63 ++++++++++ net/ipv6/netfilter/Kconfig | 9 ++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/ip6t_srh.c | 165 +++++++++++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 include/uapi/linux/netfilter_ipv6/ip6t_srh.h create mode 100644 net/ipv6/netfilter/ip6t_srh.cinclude/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +0
-0
include/uapi/linux/netfilter_ipv6/ip6t_srh.h line 6
Quoted 6 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +6
-0@@ -0,0 +1,63 @@ +/** +* Definitions for Segment Routing Header 'srh' match +* +* Author: +* Ahmed Abdelsalam <[email protected]> +*/
Please, add this in SPDX format instead.
See include/uapi/linux/netfilter/xt_owner.h for instance.
Quoted 23 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +23
-0+#ifndef _IP6T_SRH_H +#define _IP6T_SRH_H + +#include <linux/types.h> +#include <linux/netfilter.h> + +/* Values for "mt_flags" field in struct ip6t_srh */ +#define IP6T_SRH_NEXTHDR 0x0001 +#define IP6T_SRH_LEN_EQ 0x0002 +#define IP6T_SRH_LEN_GT 0x0004 +#define IP6T_SRH_LEN_LT 0x0008 +#define IP6T_SRH_SEGS_EQ 0x0010 +#define IP6T_SRH_SEGS_GT 0x0020 +#define IP6T_SRH_SEGS_LT 0x0040 +#define IP6T_SRH_LAST_EQ 0x0080 +#define IP6T_SRH_LAST_GT 0x0100 +#define IP6T_SRH_LAST_LT 0x0200 +#define IP6T_SRH_TAG 0x0400 +#define IP6T_SRH_MASK 0x07FF + +/* Values for "mt_invflags" field in struct ip6t_srh */ +#define IP6T_SRH_INV_NEXTHDR 0x0001 +#define IP6T_SRH_INV_LEN_EQ 0x0002
include/uapi/linux/netfilter_ipv6/ip6t_srh.h line 40
Quoted 10 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +10
-0+#define IP6T_SRH_INV_LEN_GT 0x0004 +#define IP6T_SRH_INV_LEN_LT 0x0008 +#define IP6T_SRH_INV_SEGS_EQ 0x0010 +#define IP6T_SRH_INV_SEGS_GT 0x0020 +#define IP6T_SRH_INV_SEGS_LT 0x0040 +#define IP6T_SRH_INV_LAST_EQ 0x0080 +#define IP6T_SRH_INV_LAST_GT 0x0100 +#define IP6T_SRH_INV_LAST_LT 0x0200 +#define IP6T_SRH_INV_TAG 0x0400 +#define IP6T_SRH_INV_MASK 0x07FF
Looking at all these EQ, GT, LT... I think this should be very easy to implement in nf_tables with no kernel changes.
You only need to add the protocol definition to:
nftables/src/exthdr.c
Would you have a look into this? This would be very much appreciated to we keep nftables in sync with what we have in iptables.
Quoted 79 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +23
-0+ +/** +* struct ip6t_srh - SRH match options +* @ next_hdr: Next header field of SRH +* @ hdr_len: Extension header length field of SRH +* @ segs_left: Segments left field of SRH +* @ last_entry: Last entry field of SRH +* @ tag: Tag field of SRH +* @ mt_flags: match options +* @ mt_invflags: Invert the sense of match options +*/ + +struct ip6t_srh { +__u8 next_hdr; +__u8 hdr_len; +__u8 segs_left; +__u8 last_entry; +__u16 tag; +__u16 mt_flags; +__u16 mt_invflags; +}; + +#endif /*_IP6T_SRH_H*/ net/ipv6/netfilter/Kconfig +9
-0@@ -232,6 +232,15 @@ config IP6_NF_MATCH_RT To compile it as a module, choose M here. If unsure, say N. +config IP6_NF_MATCH_SRH + tristate '"srh" Segment Routing header match support' + depends on NETFILTER_ADVANCED + help + srh matching allows you to match packets based on the segment + routing header of the packet. + + To compile it as a module, choose M here. If unsure, say N. + # The targets config IP6_NF_TARGET_HL tristate '"HL" hoplimit target support'net/ipv6/netfilter/Makefile +1
-0@@ -54,6 +54,7 @@ obj-$(CONFIG_IP6_NF_MATCH_MH) += ip6t_mh.o obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_hbh.o obj-$(CONFIG_IP6_NF_MATCH_RPFILTER) += ip6t_rpfilter.o obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o +obj-$(CONFIG_IP6_NF_MATCH_SRH) += ip6t_srh.o # targets obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.onet/ipv6/netfilter/ip6t_srh.c new file +18
-0@@ -0,0 +1,165 @@ +/* +* Kernel module to match Segment Routing Header (SRH) parameters. +* +* Author: +* Ahmed Abdelsalam <[email protected]> +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> +#include <linux/skbuff.h> +#include <linux/ipv6.h> +#include <linux/types.h>
net/ipv6/netfilter/ip6t_srh.c line 28
Quoted 10 lines
net/ipv6/netfilter/ip6t_srh.c new file +10
-0+#include <net/ipv6.h> +#include <net/seg6.h> + +#include <linux/netfilter/x_tables.h> +#include <linux/netfilter_ipv6/ip6t_srh.h> +#include <linux/netfilter_ipv6/ip6_tables.h> + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Xtables: IPv6 Segment Routing Header match" ); +MODULE_AUTHOR ("Ahmed Abdelsalam <[email protected]>" );
Not a deal breaker, but modern code usually places these MODULE_* lines at the end of the file.
Quoted 7 lines
net/ipv6/netfilter/ip6t_srh.c new file +7
-0+ +/* Test a struct->mt_invflags and a boolean for inequality */ +#define NF_SRH_INVF (ptr, flag, boolean) \ + ((boolean) ^ !!((ptr)->mt_invflags & (flag))) + +static bool srh_mt6 (const struct sk_buff *skb,struct xt_action_param *par) +{
net/ipv6/netfilter/ip6t_srh.c line 45
Quoted 10 lines
net/ipv6/netfilter/ip6t_srh.c new file +10
-0+const struct ip6t_srh *srhinfo = par->matchinfo; +struct ipv6_sr_hdr *srh; +struct ipv6_sr_hdr _srh; +int hdrlen, srhoff =0 ; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return false; + + srh =skb_header_pointer (skb, srhoff,sizeof (_srh), &_srh); +
Remove unnecessary line break.
Quoted 78 lines
net/ipv6/netfilter/ip6t_srh.c new file +78
-0+if (!srh) +return false; + + hdrlen =ipv6_optlen (srh); +if (skb->len - srhoff < hdrlen) +return false; + +if (srh->type !=IPV6_SRCRT_TYPE_4 ) +return false; + +if (srh->segments_left > srh->first_segment) +return false; + +/* Next Header matching */ +if (srhinfo->mt_flags &IP6T_SRH_NEXTHDR ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_NEXTHDR , + !(srh->nexthdr == srhinfo->next_hdr))) +return false; + +/* Header Extension Length matching */ +if (srhinfo->mt_flags &IP6T_SRH_LEN_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_EQ , + !(srh->hdrlen == srhinfo->hdr_len))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LEN_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_GT , + !(srh->hdrlen > srhinfo->hdr_len))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LEN_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_LT , + !(srh->hdrlen < srhinfo->hdr_len))) +return false; + +/* Segments Left matching */ +if (srhinfo->mt_flags &IP6T_SRH_SEGS_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_EQ , + !(srh->segments_left == srhinfo->segs_left))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_SEGS_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_GT , + !(srh->segments_left > srhinfo->segs_left))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_SEGS_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_LT , + !(srh->segments_left < srhinfo->segs_left))) +return false; + +/** +* Last Entry matching +* Last_Entry field was introduced in revision 6 of the SRH draft. +* It was called First_Segment in the previous revision +*/ +if (srhinfo->mt_flags &IP6T_SRH_LAST_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_EQ , + !(srh->first_segment == srhinfo->last_entry))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LAST_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_GT , + !(srh->first_segment > srhinfo->last_entry))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LAST_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_LT , + !(srh->first_segment < srhinfo->last_entry))) +return false; + +/** +* Tag matchig +* Tag field was introduced in revision 6 of the SRH draft. +*/ +if (srhinfo->mt_flags &IP6T_SRH_TAG ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_TAG , + !(srh->tag == srhinfo->tag)))
net/ipv6/netfilter/ip6t_srh.c line 133
Quoted 10 lines
net/ipv6/netfilter/ip6t_srh.c new file +10
-0+return false; +return true; +} + +static int srh_mt6_check (const struct xt_mtchk_param *par) +{ +const struct ip6t_srh *srhinfo = par->matchinfo; + +if (srhinfo->mt_flags & ~IP6T_SRH_MASK ) { +pr_debug ("unknown srh match flags %X\n" , srhinfo->mt_flags);
Better call pr_err() here. I know we don't do this in other extensions, but we should not use pr_debug() for this. ip6tables explicit refers to 'dmesg' when -EINVAL is returned.
Thanks.
Quoted 40 lines · On Sun, 7 Jan 2018 00:40:03 +0100 Pablo Neira Ayuso <[email protected]> wrote:
Hi Ahmed,
Quoted 28 lines · Ahmed Abdelsalam
It allows matching packets based on Segment Routing Header (SRH) information.
The implementation considers revision 7 of the SRH draft.
https://tools.ietf.org/html/draft-ietf-6man-segment-routing-header-07Currently supported match options include:
(1) Next Header
(2) Hdr Ext Len
(3) Segments Left
(4) Last Entry
(5) Tag value of SRHSigned-off-by Ahmed Abdelsalam <[email protected]>--- include/uapi/linux/netfilter_ipv6/ip6t_srh.h | 63 ++++++++++ net/ipv6/netfilter/Kconfig | 9 ++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/ip6t_srh.c | 165 +++++++++++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 include/uapi/linux/netfilter_ipv6/ip6t_srh.h create mode 100644 net/ipv6/netfilter/ip6t_srh.cinclude/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +0
-0include/uapi/linux/netfilter_ipv6/ip6t_srh.h line 6
Quoted 6 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +6
-0@@ -0,0 +1,63 @@ +/** +* Definitions for Segment Routing Header 'srh' match +* +* Author: +* Ahmed Abdelsalam <[email protected]> +*/ Please, add this in SPDX format instead.
Quoted 2 lines
See
include/uapi/linux/netfilter/xt_owner.hfor instance.
Ok
Quoted 37 lines
Quoted 23 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +23
-0+#ifndef _IP6T_SRH_H +#define _IP6T_SRH_H + +#include <linux/types.h> +#include <linux/netfilter.h> + +/* Values for "mt_flags" field in struct ip6t_srh */ +#define IP6T_SRH_NEXTHDR 0x0001 +#define IP6T_SRH_LEN_EQ 0x0002 +#define IP6T_SRH_LEN_GT 0x0004 +#define IP6T_SRH_LEN_LT 0x0008 +#define IP6T_SRH_SEGS_EQ 0x0010 +#define IP6T_SRH_SEGS_GT 0x0020 +#define IP6T_SRH_SEGS_LT 0x0040 +#define IP6T_SRH_LAST_EQ 0x0080 +#define IP6T_SRH_LAST_GT 0x0100 +#define IP6T_SRH_LAST_LT 0x0200 +#define IP6T_SRH_TAG 0x0400 +#define IP6T_SRH_MASK 0x07FF + +/* Values for "mt_invflags" field in struct ip6t_srh */ +#define IP6T_SRH_INV_NEXTHDR 0x0001 +#define IP6T_SRH_INV_LEN_EQ 0x0002include/uapi/linux/netfilter_ipv6/ip6t_srh.h line 40
Quoted 10 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +10
-0+#define IP6T_SRH_INV_LEN_GT 0x0004 +#define IP6T_SRH_INV_LEN_LT 0x0008 +#define IP6T_SRH_INV_SEGS_EQ 0x0010 +#define IP6T_SRH_INV_SEGS_GT 0x0020 +#define IP6T_SRH_INV_SEGS_LT 0x0040 +#define IP6T_SRH_INV_LAST_EQ 0x0080 +#define IP6T_SRH_INV_LAST_GT 0x0100 +#define IP6T_SRH_INV_LAST_LT 0x0200 +#define IP6T_SRH_INV_TAG 0x0400 +#define IP6T_SRH_INV_MASK 0x07FFLooking at all these EQ, GT, LT... I think this should be very easy to implement in
nf_tableswith no kernel changes.
Quoted 6 lines
You only need to add the protocol definition to:
nftables/src/exthdr.cWould you have a look into this? This would be very much appreciated to we keep nftables in sync with what we have in iptables.
Yes, I look into it. I will send you a patch for nf_tables as well.
Quoted 85 lines
Quoted 85 lines
include/uapi/linux/netfilter_ipv6/ip6t_srh.h new file +23
-0+ +/** +* struct ip6t_srh - SRH match options +* @ next_hdr: Next header field of SRH +* @ hdr_len: Extension header length field of SRH +* @ segs_left: Segments left field of SRH +* @ last_entry: Last entry field of SRH +* @ tag: Tag field of SRH +* @ mt_flags: match options +* @ mt_invflags: Invert the sense of match options +*/ + +struct ip6t_srh { +__u8 next_hdr; +__u8 hdr_len; +__u8 segs_left; +__u8 last_entry; +__u16 tag; +__u16 mt_flags; +__u16 mt_invflags; +}; + +#endif /*_IP6T_SRH_H*/ net/ipv6/netfilter/Kconfig +9
-0@@ -232,6 +232,15 @@ config IP6_NF_MATCH_RT To compile it as a module, choose M here. If unsure, say N. +config IP6_NF_MATCH_SRH + tristate '"srh" Segment Routing header match support' + depends on NETFILTER_ADVANCED + help + srh matching allows you to match packets based on the segment + routing header of the packet. + + To compile it as a module, choose M here. If unsure, say N. + # The targets config IP6_NF_TARGET_HL tristate '"HL" hoplimit target support'net/ipv6/netfilter/Makefile +1
-0@@ -54,6 +54,7 @@ obj-$(CONFIG_IP6_NF_MATCH_MH) += ip6t_mh.o obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_hbh.o obj-$(CONFIG_IP6_NF_MATCH_RPFILTER) += ip6t_rpfilter.o obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o +obj-$(CONFIG_IP6_NF_MATCH_SRH) += ip6t_srh.o # targets obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.onet/ipv6/netfilter/ip6t_srh.c new file +23
-0@@ -0,0 +1,165 @@ +/* +* Kernel module to match Segment Routing Header (SRH) parameters. +* +* Author: +* Ahmed Abdelsalam <[email protected]> +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> +#include <linux/skbuff.h> +#include <linux/ipv6.h> +#include <linux/types.h> +#include <net/ipv6.h> +#include <net/seg6.h> + +#include <linux/netfilter/x_tables.h> +#include <linux/netfilter_ipv6/ip6t_srh.h>
Quoted 9 lines
net/ipv6/netfilter/ip6t_srh.c line 28
Quoted 5 lines
net/ipv6/netfilter/ip6t_srh.c new file +5
-0+#include <linux/netfilter_ipv6/ip6_tables.h> + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Xtables: IPv6 Segment Routing Header match" ); +MODULE_AUTHOR ("Ahmed Abdelsalam <[email protected]>" );Not a deal breaker, but modern code usually places these MODULE_* lines at the end of the file.
I will move them to the end.
Quoted 11 lines
Quoted 11 lines
net/ipv6/netfilter/ip6t_srh.c new file +11
-0+ +/* Test a struct->mt_invflags and a boolean for inequality */ +#define NF_SRH_INVF (ptr, flag, boolean) \ + ((boolean) ^ !!((ptr)->mt_invflags & (flag))) + +static bool srh_mt6 (const struct sk_buff *skb,struct xt_action_param *par) +{ +const struct ip6t_srh *srhinfo = par->matchinfo; +struct ipv6_sr_hdr *srh; +struct ipv6_sr_hdr _srh; +int hdrlen, srhoff =0 ;
Quoted 9 lines
net/ipv6/netfilter/ip6t_srh.c line 45
Quoted 6 lines
net/ipv6/netfilter/ip6t_srh.c new file +6
-0+ +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return false; + + srh =skb_header_pointer (skb, srhoff,sizeof (_srh), &_srh); +Remove unnecessary line break.
Ok
Quoted 84 lines
Quoted 84 lines
net/ipv6/netfilter/ip6t_srh.c new file +84
-0+if (!srh) +return false; + + hdrlen =ipv6_optlen (srh); +if (skb->len - srhoff < hdrlen) +return false; + +if (srh->type !=IPV6_SRCRT_TYPE_4 ) +return false; + +if (srh->segments_left > srh->first_segment) +return false; + +/* Next Header matching */ +if (srhinfo->mt_flags &IP6T_SRH_NEXTHDR ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_NEXTHDR , + !(srh->nexthdr == srhinfo->next_hdr))) +return false; + +/* Header Extension Length matching */ +if (srhinfo->mt_flags &IP6T_SRH_LEN_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_EQ , + !(srh->hdrlen == srhinfo->hdr_len))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LEN_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_GT , + !(srh->hdrlen > srhinfo->hdr_len))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LEN_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LEN_LT , + !(srh->hdrlen < srhinfo->hdr_len))) +return false; + +/* Segments Left matching */ +if (srhinfo->mt_flags &IP6T_SRH_SEGS_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_EQ , + !(srh->segments_left == srhinfo->segs_left))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_SEGS_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_GT , + !(srh->segments_left > srhinfo->segs_left))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_SEGS_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_SEGS_LT , + !(srh->segments_left < srhinfo->segs_left))) +return false; + +/** +* Last Entry matching +* Last_Entry field was introduced in revision 6 of the SRH draft. +* It was called First_Segment in the previous revision +*/ +if (srhinfo->mt_flags &IP6T_SRH_LAST_EQ ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_EQ , + !(srh->first_segment == srhinfo->last_entry))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LAST_GT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_GT , + !(srh->first_segment > srhinfo->last_entry))) +return false; + +if (srhinfo->mt_flags &IP6T_SRH_LAST_LT ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_LAST_LT , + !(srh->first_segment < srhinfo->last_entry))) +return false; + +/** +* Tag matchig +* Tag field was introduced in revision 6 of the SRH draft. +*/ +if (srhinfo->mt_flags &IP6T_SRH_TAG ) +if (NF_SRH_INVF (srhinfo,IP6T_SRH_INV_TAG , + !(srh->tag == srhinfo->tag))) +return false; +return true; +} + +static int srh_mt6_check (const struct xt_mtchk_param *par) +{
Quoted 9 lines
net/ipv6/netfilter/ip6t_srh.c line 133
Quoted 4 lines
net/ipv6/netfilter/ip6t_srh.c new file +4
-0+const struct ip6t_srh *srhinfo = par->matchinfo; + +if (srhinfo->mt_flags & ~IP6T_SRH_MASK ) { +pr_debug ("unknown srh match flags %X\n" , srhinfo->mt_flags);Better call
pr_err()here. I know we don't do this in other extensions, but we should not usepr_debug()for this. ip6tables explicit refers to 'dmesg' when-EINVALis returned.
I will change this one also .
Quoted 1 line
Thanks.
I will send a new pacth addressing these comments. Thanks
Signature · 1 line
Ahmed Abdelsalam <[email protected]>