secpath match
PATCH 1/1 secpath match
Hi Harald,
this is a second version of the sectype match I sent to netfilter-devel yesterday. It is used to match stuff from the secpath of a decapsulated packet. It can be used for matching if a packet came from a specific tunnel, tunnel endpoints, spis, protos, ... and can also match complex descriptions like "ah transport + esp tunnel from 192.168.0.0/24". If you like it, please add to patch-o-matic.
Best regards,
Patrick
patch-o-matic_2.5/extra/secpath.patch +193 -0
@@ -0,0 +1,193 @@ +diff -Nru a/include/linux/netfilter_ipv4/ipt_secpath.h b/include/linux/netfilter_ipv4/ipt_secpath.h +--- /dev/null Wed Dec 31 16:00:00 1969 ++++ b/include/linux/netfilter_ipv4/ipt_secpath.h Tue Aug 26 16:09:24 2003 +@@ -0,0 +1,43 @@ ++#ifndef _IPT_SECPATH_H ++#define _IPT_SECPATH_H ++ ++#define SECPATH_MAX_DEPTH 4 /* must match XFRM_MAX_DEPTH */ ++ ++#define MATCH_PATH 0x1 ++ ++#define SECPATH_MODE_TRANSPORT 0 ++#define SECPATH_MODE_TUNNEL 1 ++ ++struct ipt_secpath_flags ++{ ++ u_int8_t saddr:1, ++ daddr:1, ++ spi:1, ++ reqid:1, ++ proto:1, ++ mode:1; ++}; ++ ++struct ipt_secpath_elem ++{ ++ u_int32_t saddr; ++ u_int32_t smask; ++ u_int32_t daddr; ++ u_int32_t dmask; ++ u_int32_t spi; ++ u_int32_t reqid; ++ u_int8_t proto; ++ u_int8_t mode; ++ ++ struct ipt_secpath_flags match; ++ struct ipt_secpath_flags invert; ++}; ++ ++struct ipt_secpath_info ++{ ++ struct ipt_secpath_elem path[SECPATH_MAX_DEPTH]; ++ u_int8_t len; ++ u_int8_t flags; ++}; ++ ++#endif +diff -Nru a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig +--- a/net/ipv4/netfilter/Kconfig Tue Aug 26 16:09:24 2003 ++++ b/net/ipv4/netfilter/Kconfig Tue Aug 26 16:09:24 2003 +@@ -301,6 +301,17 @@ + If you want to compile it as a module, say M here and read + <file:Documentation/modules.txt>. If unsure, say `N'. + ++config IP_NF_MATCH_SECPATH ++ tristate "Secpath match support" ++ depends on EXPERIMENTAL && IP_NF_IPTABLES ++ help ++ Secpath packet matching allows you to match various attributes ++ of a packets secpath which describes the path a packet took through ++ the (ipsec) transformers during decapsulation. ++ ++ If you want to compile it as a module, say M here and read ++ <file:Documentation/modules.txt>. If unsure, say `N'. ++ + # The targets + config IP_NF_FILTER + tristate "Packet filtering" +diff -Nru a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile +--- a/net/ipv4/netfilter/Makefile Tue Aug 26 16:09:24 2003 ++++ b/net/ipv4/netfilter/Makefile Tue Aug 26 16:09:24 2003 +@@ -65,6 +65,7 @@ + obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o + + obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o ++obj-$(CONFIG_IP_NF_MATCH_SECPATH) += ipt_secpath.o + + # targets + obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o +diff -Nru a/net/ipv4/netfilter/ipt_secpath.c b/net/ipv4/netfilter/ipt_secpath.c +--- /dev/null Wed Dec 31 16:00:00 1969 ++++ b/net/ipv4/netfilter/ipt_secpath.c Tue Aug 26 16:09:24 2003 +@@ -0,0 +1,110 @@ ++/* IP tables module for matching various secpath attributes ++ * ++ * (C) 2003 by Patrick McHardy <[email protected]> ++ * ++ * This software is distributed under the terms of the GNU GPL version 2. ++ */ ++ ++#include <linux/config.h> ++#include <linux/module.h> ++#include <linux/skbuff.h> ++#include <net/xfrm.h> ++ ++#include <linux/netfilter_ipv4/ipt_secpath.h> ++#include <linux/netfilter_ipv4/ip_tables.h> ++ ++MODULE_AUTHOR("Patrick McHardy <[email protected]>"); ++MODULE_DESCRIPTION("IPtables secpath attribute matching module"); ++MODULE_LICENSE("GPL"); ++ ++static int match_secpath(struct xfrm_state *x, const struct ipt_secpath_elem *e) ++{ ++ if (e->match.saddr) { ++ if ((e->saddr != ((u32) x->props.saddr.a4 & e->smask)) ^ ++ e->invert.saddr) ++ return 0; ++ } ++ if (e->match.daddr) { ++ if ((e->daddr != ((u32) x->id.daddr.a4 & e->dmask)) ^ ++ e->invert.daddr) ++ return 0; ++ } ++ if (e->match.proto) { ++ if ((e->proto != x->id.proto) ^ e->invert.proto) ++ return 0; ++ } ++ if (e->match.mode) { ++ if ((e->mode != x->props.mode) ^ e->invert.mode) ++ return 0; ++ } ++ if (e->match.spi) { ++ if ((e->spi != x->id.spi) ^ e->invert.spi) ++ return 0; ++ } ++ if (e->match.reqid) { ++ if ((e->reqid != x->props.reqid) ^ e->invert.reqid) ++ return 0; ++ } ++ return 1; ++} ++ ++static int match(const struct sk_buff *skb, ++ const struct net_device *in, ++ const struct net_device *out, ++ const void *matchinfo, int offset, int *hotdrop) ++{ ++ const struct ipt_secpath_info *info = matchinfo; ++ struct sec_path *sp = skb->sp; ++ unsigned int i; ++ int strict = info->flags & MATCH_PATH; ++ ++ if (sp == NULL) ++ return 0; ++ ++ if (strict && info->len != sp->len) ++ return 0; ++ ++ for (i = 0; i < sp->len; i++) { ++ if (match_secpath(sp->x[i].xvec, &info->path[strict ? i : 0])) { ++ if (!strict) ++ return 1; ++ } else if (strict) ++ return 0; ++ } ++ ++ return strict ? 1 : 0; ++} ++ ++static int checkentry(const char *tablename, const struct ipt_ip *ip, ++ void *matchinfo, unsigned int matchsize, ++ unsigned int hook_mask) ++{ ++ if (matchsize != IPT_ALIGN(sizeof(struct ipt_secpath_info))) { ++ printk(KERN_ERR "ipt_secpath: matchsize %u != %u.\n", ++ matchsize, IPT_ALIGN(sizeof(struct ipt_secpath_info))); ++ return 0; ++ } ++ ++ return 1; ++} ++ ++static struct ipt_match secpath_match = { ++ .name = "secpath", ++ .match = &match, ++ .checkentry = &checkentry, ++ .me = THIS_MODULE, ++}; ++ ++static int __init init(void) ++{ ++ return ipt_register_match(&secpath_match); ++} ++ ++static void __exit fini(void) ++{ ++ ipt_unregister_match(&secpath_match); ++ ++} ++ ++module_init(init); ++module_exit(fini);
patch-o-matic_2.5/extra/secpath.patch.help +57 -0
57 lines not on this page · raw message
userspace/extensions/libipt_secpath.c +352 -0
352 lines not on this page · raw message
userspace/extensions/.secpath-test +3 -0
@@ -0,0 +1,3 @@ +#!/bin/sh +# True if secpath match patch is applied. +[ -f $KERNEL_DIR/include/linux/netfilter_ipv4/ipt_secpath.h ] && echo secpath