diff options
| author | Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa> | 2013-09-28 14:33:23 -0400 |
|---|---|---|
| committer | Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> | 2013-09-30 15:42:58 -0400 |
| commit | 7c3ad056ef79fd10f5f111c807ccbd9fa9068c7f (patch) | |
| tree | 90b1cb0da8f3f41fbeadb0bc92dae06b96d84468 | |
| parent | 1785e8f473082aa60d62c7165856cf6484077b99 (diff) | |
netfilter: ipset: Add hash:net,port,net module to kernel.
This adds a new set that provides similar functionality to ip,port,net
but permits arbitrary size subnets for both the first and last
parameter.
Signed-off-by: Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
| -rw-r--r-- | net/netfilter/ipset/Kconfig | 9 | ||||
| -rw-r--r-- | net/netfilter/ipset/Makefile | 1 | ||||
| -rw-r--r-- | net/netfilter/ipset/ip_set_hash_netportnet.c | 588 |
3 files changed, 598 insertions, 0 deletions
diff --git a/net/netfilter/ipset/Kconfig b/net/netfilter/ipset/Kconfig index 9119f658a69b..a2d6263b6c64 100644 --- a/net/netfilter/ipset/Kconfig +++ b/net/netfilter/ipset/Kconfig | |||
| @@ -90,6 +90,15 @@ config IP_SET_HASH_IPPORTNET | |||
| 90 | 90 | ||
| 91 | To compile it as a module, choose M here. If unsure, say N. | 91 | To compile it as a module, choose M here. If unsure, say N. |
| 92 | 92 | ||
| 93 | config IP_SET_HASH_NETPORTNET | ||
| 94 | tristate "hash:net,port,net set support" | ||
| 95 | depends on IP_SET | ||
| 96 | help | ||
| 97 | This option adds the hash:net,port,net set type support, by which | ||
| 98 | one can store two IPv4/IPv6 subnets, and a protocol/port in a set. | ||
| 99 | |||
| 100 | To compile it as a module, choose M here. If unsure, say N. | ||
| 101 | |||
| 93 | config IP_SET_HASH_NET | 102 | config IP_SET_HASH_NET |
| 94 | tristate "hash:net set support" | 103 | tristate "hash:net set support" |
| 95 | depends on IP_SET | 104 | depends on IP_SET |
diff --git a/net/netfilter/ipset/Makefile b/net/netfilter/ipset/Makefile index 43eef7a19e3c..44b2d38476fa 100644 --- a/net/netfilter/ipset/Makefile +++ b/net/netfilter/ipset/Makefile | |||
| @@ -21,6 +21,7 @@ obj-$(CONFIG_IP_SET_HASH_NET) += ip_set_hash_net.o | |||
| 21 | obj-$(CONFIG_IP_SET_HASH_NETPORT) += ip_set_hash_netport.o | 21 | obj-$(CONFIG_IP_SET_HASH_NETPORT) += ip_set_hash_netport.o |
| 22 | obj-$(CONFIG_IP_SET_HASH_NETIFACE) += ip_set_hash_netiface.o | 22 | obj-$(CONFIG_IP_SET_HASH_NETIFACE) += ip_set_hash_netiface.o |
| 23 | obj-$(CONFIG_IP_SET_HASH_NETNET) += ip_set_hash_netnet.o | 23 | obj-$(CONFIG_IP_SET_HASH_NETNET) += ip_set_hash_netnet.o |
| 24 | obj-$(CONFIG_IP_SET_HASH_NETPORTNET) += ip_set_hash_netportnet.o | ||
| 24 | 25 | ||
| 25 | # list types | 26 | # list types |
| 26 | obj-$(CONFIG_IP_SET_LIST_SET) += ip_set_list_set.o | 27 | obj-$(CONFIG_IP_SET_LIST_SET) += ip_set_list_set.o |
diff --git a/net/netfilter/ipset/ip_set_hash_netportnet.c b/net/netfilter/ipset/ip_set_hash_netportnet.c new file mode 100644 index 000000000000..363fab933d48 --- /dev/null +++ b/net/netfilter/ipset/ip_set_hash_netportnet.c | |||
| @@ -0,0 +1,588 @@ | |||
| 1 | /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> | ||
| 2 | * | ||
| 3 | * This program is free software; you can redistribute it and/or modify | ||
| 4 | * it under the terms of the GNU General Public License version 2 as | ||
| 5 | * published by the Free Software Foundation. | ||
| 6 | */ | ||
| 7 | |||
| 8 | /* Kernel module implementing an IP set type: the hash:ip,port,net type */ | ||
| 9 | |||
| 10 | #include <linux/jhash.h> | ||
| 11 | #include <linux/module.h> | ||
| 12 | #include <linux/ip.h> | ||
| 13 | #include <linux/skbuff.h> | ||
| 14 | #include <linux/errno.h> | ||
| 15 | #include <linux/random.h> | ||
| 16 | #include <net/ip.h> | ||
| 17 | #include <net/ipv6.h> | ||
| 18 | #include <net/netlink.h> | ||
| 19 | #include <net/tcp.h> | ||
| 20 | |||
| 21 | #include <linux/netfilter.h> | ||
| 22 | #include <linux/netfilter/ipset/pfxlen.h> | ||
| 23 | #include <linux/netfilter/ipset/ip_set.h> | ||
| 24 | #include <linux/netfilter/ipset/ip_set_getport.h> | ||
| 25 | #include <linux/netfilter/ipset/ip_set_hash.h> | ||
| 26 | |||
| 27 | #define IPSET_TYPE_REV_MIN 0 | ||
| 28 | #define IPSET_TYPE_REV_MAX 0 /* Comments support added */ | ||
| 29 | |||
| 30 | MODULE_LICENSE("GPL"); | ||
| 31 | MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>"); | ||
| 32 | IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); | ||
| 33 | MODULE_ALIAS("ip_set_hash:net,port,net"); | ||
| 34 | |||
| 35 | /* Type specific function prefix */ | ||
| 36 | #define HTYPE hash_netportnet | ||
| 37 | #define IP_SET_HASH_WITH_PROTO | ||
| 38 | #define IP_SET_HASH_WITH_NETS | ||
| 39 | #define IPSET_NET_COUNT 2 | ||
| 40 | |||
| 41 | /* IPv4 variant */ | ||
| 42 | |||
| 43 | /* Member elements */ | ||
| 44 | struct hash_netportnet4_elem { | ||
| 45 | union { | ||
| 46 | __be32 ip[2]; | ||
| 47 | __be64 ipcmp; | ||
| 48 | }; | ||
| 49 | __be16 port; | ||
| 50 | union { | ||
| 51 | u8 cidr[2]; | ||
| 52 | u16 ccmp; | ||
| 53 | }; | ||
| 54 | u8 nomatch:1; | ||
| 55 | u8 proto; | ||
| 56 | }; | ||
| 57 | |||
| 58 | /* Common functions */ | ||
| 59 | |||
| 60 | static inline bool | ||
| 61 | hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1, | ||
| 62 | const struct hash_netportnet4_elem *ip2, | ||
| 63 | u32 *multi) | ||
| 64 | { | ||
| 65 | return ip1->ipcmp == ip2->ipcmp && | ||
| 66 | ip1->ccmp == ip2->ccmp && | ||
| 67 | ip1->port == ip2->port && | ||
| 68 | ip1->proto == ip2->proto; | ||
| 69 | } | ||
| 70 | |||
| 71 | static inline int | ||
| 72 | hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem) | ||
| 73 | { | ||
| 74 | return elem->nomatch ? -ENOTEMPTY : 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | static inline void | ||
| 78 | hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags) | ||
| 79 | { | ||
| 80 | elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH); | ||
| 81 | } | ||
| 82 | |||
| 83 | static inline void | ||
| 84 | hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags) | ||
| 85 | { | ||
| 86 | swap(*flags, elem->nomatch); | ||
| 87 | } | ||
| 88 | |||
| 89 | static inline void | ||
| 90 | hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem, | ||
| 91 | struct hash_netportnet4_elem *orig) | ||
| 92 | { | ||
| 93 | elem->ip[1] = orig->ip[1]; | ||
| 94 | } | ||
| 95 | |||
| 96 | static inline void | ||
| 97 | hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem, | ||
| 98 | u8 cidr, bool inner) | ||
| 99 | { | ||
| 100 | if (inner) { | ||
| 101 | elem->ip[1] &= ip_set_netmask(cidr); | ||
| 102 | elem->cidr[1] = cidr; | ||
| 103 | } else { | ||
| 104 | elem->ip[0] &= ip_set_netmask(cidr); | ||
| 105 | elem->cidr[0] = cidr; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | static bool | ||
| 110 | hash_netportnet4_data_list(struct sk_buff *skb, | ||
| 111 | const struct hash_netportnet4_elem *data) | ||
| 112 | { | ||
| 113 | u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0; | ||
| 114 | |||
| 115 | if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) || | ||
| 116 | nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) || | ||
| 117 | nla_put_net16(skb, IPSET_ATTR_PORT, data->port) || | ||
| 118 | nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) || | ||
| 119 | nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) || | ||
| 120 | nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) || | ||
| 121 | (flags && | ||
| 122 | nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags)))) | ||
| 123 | goto nla_put_failure; | ||
| 124 | return 0; | ||
| 125 | |||
| 126 | nla_put_failure: | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | static inline void | ||
| 131 | hash_netportnet4_data_next(struct hash_netportnet4_elem *next, | ||
| 132 | const struct hash_netportnet4_elem *d) | ||
| 133 | { | ||
| 134 | next->ipcmp = d->ipcmp; | ||
| 135 | next->port = d->port; | ||
| 136 | } | ||
| 137 | |||
| 138 | #define MTYPE hash_netportnet4 | ||
| 139 | #define PF 4 | ||
| 140 | #define HOST_MASK 32 | ||
| 141 | #include "ip_set_hash_gen.h" | ||
| 142 | |||
| 143 | static int | ||
| 144 | hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb, | ||
| 145 | const struct xt_action_param *par, | ||
| 146 | enum ipset_adt adt, struct ip_set_adt_opt *opt) | ||
| 147 | { | ||
| 148 | const struct hash_netportnet *h = set->data; | ||
| 149 | ipset_adtfn adtfn = set->variant->adt[adt]; | ||
| 150 | struct hash_netportnet4_elem e = { | ||
| 151 | .cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK), | ||
