Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu> |
| 2 | * Patrick Schaaf <bof@bof.de> |
| 3 | * Martin Josefsson <gandalf@wlug.westbo.se> |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 4 | * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
David Howells | a820141 | 2012-10-09 09:48:55 +0100 | [diff] [blame] | 10 | #ifndef _IP_SET_H |
| 11 | #define _IP_SET_H |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 12 | |
Jan Engelhardt | ae8ded1 | 2012-01-14 16:26:20 +0100 | [diff] [blame] | 13 | #include <linux/ip.h> |
| 14 | #include <linux/ipv6.h> |
| 15 | #include <linux/netlink.h> |
| 16 | #include <linux/netfilter.h> |
| 17 | #include <linux/netfilter/x_tables.h> |
Jozsef Kadlecsik | 10111a6 | 2012-09-21 21:59:32 +0200 | [diff] [blame] | 18 | #include <linux/stringify.h> |
Jan Engelhardt | ae8ded1 | 2012-01-14 16:26:20 +0100 | [diff] [blame] | 19 | #include <linux/vmalloc.h> |
| 20 | #include <net/netlink.h> |
David Howells | a820141 | 2012-10-09 09:48:55 +0100 | [diff] [blame] | 21 | #include <uapi/linux/netfilter/ipset/ip_set.h> |
Jan Engelhardt | ae8ded1 | 2012-01-14 16:26:20 +0100 | [diff] [blame] | 22 | |
Jozsef Kadlecsik | 10111a6 | 2012-09-21 21:59:32 +0200 | [diff] [blame] | 23 | #define _IP_SET_MODULE_DESC(a, b, c) \ |
| 24 | MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c) |
| 25 | #define IP_SET_MODULE_DESC(a, b, c) \ |
| 26 | _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c)) |
| 27 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 28 | /* Set features */ |
| 29 | enum ip_set_feature { |
| 30 | IPSET_TYPE_IP_FLAG = 0, |
| 31 | IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG), |
| 32 | IPSET_TYPE_PORT_FLAG = 1, |
| 33 | IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG), |
| 34 | IPSET_TYPE_MAC_FLAG = 2, |
| 35 | IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG), |
| 36 | IPSET_TYPE_IP2_FLAG = 3, |
| 37 | IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG), |
| 38 | IPSET_TYPE_NAME_FLAG = 4, |
| 39 | IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG), |
Jozsef Kadlecsik | e385357 | 2011-06-16 19:00:48 +0200 | [diff] [blame] | 40 | IPSET_TYPE_IFACE_FLAG = 5, |
| 41 | IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG), |
Jozsef Kadlecsik | 3e0304a | 2012-09-21 22:02:36 +0200 | [diff] [blame] | 42 | IPSET_TYPE_NOMATCH_FLAG = 6, |
| 43 | IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG), |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 44 | /* Strictly speaking not a feature, but a flag for dumping: |
| 45 | * this settype must be dumped last */ |
| 46 | IPSET_DUMP_LAST_FLAG = 7, |
| 47 | IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG), |
| 48 | }; |
| 49 | |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 50 | /* Set extensions */ |
| 51 | enum ip_set_extension { |
| 52 | IPSET_EXT_NONE = 0, |
| 53 | IPSET_EXT_BIT_TIMEOUT = 1, |
| 54 | IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT), |
| 55 | }; |
| 56 | |
| 57 | /* Extension offsets */ |
| 58 | enum ip_set_offset { |
| 59 | IPSET_OFFSET_TIMEOUT = 0, |
| 60 | IPSET_OFFSET_MAX, |
| 61 | }; |
| 62 | |
| 63 | #define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT) |
| 64 | |
| 65 | struct ip_set_ext { |
| 66 | unsigned long timeout; |
| 67 | }; |
| 68 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 69 | struct ip_set; |
| 70 | |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 71 | typedef int (*ipset_adtfn)(struct ip_set *set, void *value, |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 72 | const struct ip_set_ext *ext, |
| 73 | struct ip_set_ext *mext, u32 flags); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 74 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 75 | /* Kernel API function options */ |
| 76 | struct ip_set_adt_opt { |
| 77 | u8 family; /* Actual protocol family */ |
| 78 | u8 dim; /* Dimension of match/target */ |
| 79 | u8 flags; /* Direction and negation flags */ |
| 80 | u32 cmdflags; /* Command-like flags */ |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 81 | struct ip_set_ext ext; /* Extensions */ |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 84 | /* Set type, variant-specific part */ |
| 85 | struct ip_set_type_variant { |
| 86 | /* Kernelspace: test/add/del entries |
| 87 | * returns negative error code, |
| 88 | * zero for no match/success to add/delete |
| 89 | * positive for matching element */ |
Jozsef Kadlecsik | 3ace95c | 2012-09-21 22:01:45 +0200 | [diff] [blame] | 90 | int (*kadt)(struct ip_set *set, const struct sk_buff *skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 91 | const struct xt_action_param *par, |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 92 | enum ipset_adt adt, struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 93 | |
| 94 | /* Userspace: test/add/del entries |
| 95 | * returns negative error code, |
| 96 | * zero for no match/success to add/delete |
| 97 | * positive for matching element */ |
| 98 | int (*uadt)(struct ip_set *set, struct nlattr *tb[], |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 99 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 100 | |
| 101 | /* Low level add/del/test functions */ |
| 102 | ipset_adtfn adt[IPSET_ADT_MAX]; |
| 103 | |
| 104 | /* When adding entries and set is full, try to resize the set */ |
| 105 | int (*resize)(struct ip_set *set, bool retried); |
| 106 | /* Destroy the set */ |
| 107 | void (*destroy)(struct ip_set *set); |
| 108 | /* Flush the elements */ |
| 109 | void (*flush)(struct ip_set *set); |
| 110 | /* Expire entries before listing */ |
| 111 | void (*expire)(struct ip_set *set); |
| 112 | /* List set header data */ |
| 113 | int (*head)(struct ip_set *set, struct sk_buff *skb); |
| 114 | /* List elements */ |
| 115 | int (*list)(const struct ip_set *set, struct sk_buff *skb, |
| 116 | struct netlink_callback *cb); |
| 117 | |
| 118 | /* Return true if "b" set is the same as "a" |
| 119 | * according to the create set parameters */ |
| 120 | bool (*same_set)(const struct ip_set *a, const struct ip_set *b); |
| 121 | }; |
| 122 | |
| 123 | /* The core set type structure */ |
| 124 | struct ip_set_type { |
| 125 | struct list_head list; |
| 126 | |
| 127 | /* Typename */ |
| 128 | char name[IPSET_MAXNAMELEN]; |
| 129 | /* Protocol version */ |
| 130 | u8 protocol; |
| 131 | /* Set features to control swapping */ |
| 132 | u8 features; |
| 133 | /* Set type dimension */ |
| 134 | u8 dimension; |
Jan Engelhardt | c15f1c8 | 2012-02-14 00:24:10 +0100 | [diff] [blame] | 135 | /* |
| 136 | * Supported family: may be NFPROTO_UNSPEC for both |
| 137 | * NFPROTO_IPV4/NFPROTO_IPV6. |
| 138 | */ |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 139 | u8 family; |
Jozsef Kadlecsik | f1e00b3 | 2011-06-16 18:51:41 +0200 | [diff] [blame] | 140 | /* Type revisions */ |
| 141 | u8 revision_min, revision_max; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 142 | |
| 143 | /* Create set */ |
| 144 | int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags); |
| 145 | |
| 146 | /* Attribute policies */ |
| 147 | const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1]; |
| 148 | const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1]; |
| 149 | |
| 150 | /* Set this to THIS_MODULE if you are a module, otherwise NULL */ |
| 151 | struct module *me; |
| 152 | }; |
| 153 | |
| 154 | /* register and unregister set type */ |
| 155 | extern int ip_set_type_register(struct ip_set_type *set_type); |
| 156 | extern void ip_set_type_unregister(struct ip_set_type *set_type); |
| 157 | |
| 158 | /* A generic IP set */ |
| 159 | struct ip_set { |
| 160 | /* The name of the set */ |
| 161 | char name[IPSET_MAXNAMELEN]; |
| 162 | /* Lock protecting the set data */ |
| 163 | rwlock_t lock; |
| 164 | /* References to the set */ |
Jozsef Kadlecsik | 2f9f28b | 2011-04-04 15:19:25 +0200 | [diff] [blame] | 165 | u32 ref; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 166 | /* The core set type */ |
| 167 | struct ip_set_type *type; |
| 168 | /* The type variant doing the real job */ |
| 169 | const struct ip_set_type_variant *variant; |
| 170 | /* The actual INET family of the set */ |
| 171 | u8 family; |
Jozsef Kadlecsik | f1e00b3 | 2011-06-16 18:51:41 +0200 | [diff] [blame] | 172 | /* The type revision */ |
| 173 | u8 revision; |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 174 | /* Extensions */ |
| 175 | u8 extensions; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 176 | /* The type specific data */ |
| 177 | void *data; |
| 178 | }; |
| 179 | |
| 180 | /* register and unregister set references */ |
| 181 | extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set); |
| 182 | extern void ip_set_put_byindex(ip_set_id_t index); |
Jozsef Kadlecsik | 15b4d93 | 2011-06-16 19:01:26 +0200 | [diff] [blame] | 183 | extern const char *ip_set_name_byindex(ip_set_id_t index); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 184 | extern ip_set_id_t ip_set_nfnl_get(const char *name); |
| 185 | extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index); |
| 186 | extern void ip_set_nfnl_put(ip_set_id_t index); |
| 187 | |
| 188 | /* API for iptables set match, and SET target */ |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 189 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 190 | extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 191 | const struct xt_action_param *par, |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 192 | struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 193 | extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 194 | const struct xt_action_param *par, |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 195 | struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 196 | extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 197 | const struct xt_action_param *par, |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 198 | struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 199 | |
| 200 | /* Utility functions */ |
Jozsef Kadlecsik | 15b4d93 | 2011-06-16 19:01:26 +0200 | [diff] [blame] | 201 | extern void *ip_set_alloc(size_t size); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 202 | extern void ip_set_free(void *members); |
| 203 | extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr); |
| 204 | extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr); |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 205 | extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[], |
| 206 | struct ip_set_ext *ext); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 207 | |
| 208 | static inline int |
| 209 | ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr) |
| 210 | { |
| 211 | __be32 ip; |
| 212 | int ret = ip_set_get_ipaddr4(nla, &ip); |
Jozsef Kadlecsik | 15b4d93 | 2011-06-16 19:01:26 +0200 | [diff] [blame] | 213 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 214 | if (ret) |
| 215 | return ret; |
| 216 | *ipaddr = ntohl(ip); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Ignore IPSET_ERR_EXIST errors if asked to do so? */ |
| 221 | static inline bool |
| 222 | ip_set_eexist(int ret, u32 flags) |
| 223 | { |
| 224 | return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST); |
| 225 | } |
| 226 | |
Jozsef Kadlecsik | 43c56e5 | 2013-04-08 21:51:25 +0200 | [diff] [blame] | 227 | /* Match elements marked with nomatch */ |
| 228 | static inline bool |
| 229 | ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt) |
| 230 | { |
| 231 | return adt == IPSET_TEST && |
| 232 | ret == -ENOTEMPTY && ((flags >> 16) & IPSET_FLAG_NOMATCH); |
| 233 | } |
| 234 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 235 | /* Check the NLA_F_NET_BYTEORDER flag */ |
| 236 | static inline bool |
| 237 | ip_set_attr_netorder(struct nlattr *tb[], int type) |
| 238 | { |
| 239 | return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER); |
| 240 | } |
| 241 | |
| 242 | static inline bool |
| 243 | ip_set_optattr_netorder(struct nlattr *tb[], int type) |
| 244 | { |
| 245 | return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER); |
| 246 | } |
| 247 | |
| 248 | /* Useful converters */ |
| 249 | static inline u32 |
| 250 | ip_set_get_h32(const struct nlattr *attr) |
| 251 | { |
| 252 | return ntohl(nla_get_be32(attr)); |
| 253 | } |
| 254 | |
| 255 | static inline u16 |
| 256 | ip_set_get_h16(const struct nlattr *attr) |
| 257 | { |
| 258 | return ntohs(nla_get_be16(attr)); |
| 259 | } |
| 260 | |
| 261 | #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED) |
| 262 | #define ipset_nest_end(skb, start) nla_nest_end(skb, start) |
| 263 | |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 264 | static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr) |
| 265 | { |
| 266 | struct nlattr *__nested = ipset_nest_start(skb, type); |
| 267 | int ret; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 268 | |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 269 | if (!__nested) |
| 270 | return -EMSGSIZE; |
| 271 | ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr); |
| 272 | if (!ret) |
| 273 | ipset_nest_end(skb, __nested); |
| 274 | return ret; |
| 275 | } |
| 276 | |
Jozsef Kadlecsik | 3ace95c | 2012-09-21 22:01:45 +0200 | [diff] [blame] | 277 | static inline int nla_put_ipaddr6(struct sk_buff *skb, int type, |
| 278 | const struct in6_addr *ipaddrptr) |
David S. Miller | 7cf7899 | 2012-04-01 19:54:46 -0400 | [diff] [blame] | 279 | { |
| 280 | struct nlattr *__nested = ipset_nest_start(skb, type); |
| 281 | int ret; |
| 282 | |
| 283 | if (!__nested) |
| 284 | return -EMSGSIZE; |
| 285 | ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6, |
| 286 | sizeof(struct in6_addr), ipaddrptr); |
| 287 | if (!ret) |
| 288 | ipset_nest_end(skb, __nested); |
| 289 | return ret; |
| 290 | } |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 291 | |
| 292 | /* Get address from skbuff */ |
| 293 | static inline __be32 |
| 294 | ip4addr(const struct sk_buff *skb, bool src) |
| 295 | { |
| 296 | return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr; |
| 297 | } |
| 298 | |
| 299 | static inline void |
| 300 | ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr) |
| 301 | { |
| 302 | *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr; |
| 303 | } |
| 304 | |
| 305 | static inline void |
| 306 | ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr) |
| 307 | { |
| 308 | memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr, |
| 309 | sizeof(*addr)); |
| 310 | } |
| 311 | |
| 312 | /* Calculate the bytes required to store the inclusive range of a-b */ |
| 313 | static inline int |
| 314 | bitmap_bytes(u32 a, u32 b) |
| 315 | { |
| 316 | return 4 * ((((b - a + 8) / 8) + 3) / 4); |
| 317 | } |
| 318 | |
Jozsef Kadlecsik | 075e64c | 2013-04-27 14:28:55 +0200 | [diff] [blame^] | 319 | #include <linux/netfilter/ipset/ip_set_timeout.h> |
| 320 | |
| 321 | #define IP_SET_INIT_KEXT(skb, opt, map) \ |
| 322 | { .timeout = ip_set_adt_opt_timeout(opt, map) } |
| 323 | |
| 324 | #define IP_SET_INIT_UEXT(map) \ |
| 325 | { .timeout = (map)->timeout } |
| 326 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 327 | #endif /*_IP_SET_H */ |