Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 1 | #ifndef _IP_SET_H |
| 2 | #define _IP_SET_H |
| 3 | |
| 4 | /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu> |
| 5 | * Patrick Schaaf <bof@bof.de> |
| 6 | * Martin Josefsson <gandalf@wlug.westbo.se> |
| 7 | * Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | /* The protocol version */ |
| 15 | #define IPSET_PROTOCOL 6 |
| 16 | |
| 17 | /* The max length of strings including NUL: set and type identifiers */ |
| 18 | #define IPSET_MAXNAMELEN 32 |
| 19 | |
| 20 | /* Message types and commands */ |
| 21 | enum ipset_cmd { |
| 22 | IPSET_CMD_NONE, |
| 23 | IPSET_CMD_PROTOCOL, /* 1: Return protocol version */ |
| 24 | IPSET_CMD_CREATE, /* 2: Create a new (empty) set */ |
| 25 | IPSET_CMD_DESTROY, /* 3: Destroy a (empty) set */ |
| 26 | IPSET_CMD_FLUSH, /* 4: Remove all elements from a set */ |
| 27 | IPSET_CMD_RENAME, /* 5: Rename a set */ |
| 28 | IPSET_CMD_SWAP, /* 6: Swap two sets */ |
| 29 | IPSET_CMD_LIST, /* 7: List sets */ |
| 30 | IPSET_CMD_SAVE, /* 8: Save sets */ |
| 31 | IPSET_CMD_ADD, /* 9: Add an element to a set */ |
| 32 | IPSET_CMD_DEL, /* 10: Delete an element from a set */ |
| 33 | IPSET_CMD_TEST, /* 11: Test an element in a set */ |
| 34 | IPSET_CMD_HEADER, /* 12: Get set header data only */ |
| 35 | IPSET_CMD_TYPE, /* 13: Get set type */ |
| 36 | IPSET_MSG_MAX, /* Netlink message commands */ |
| 37 | |
| 38 | /* Commands in userspace: */ |
| 39 | IPSET_CMD_RESTORE = IPSET_MSG_MAX, /* 14: Enter restore mode */ |
| 40 | IPSET_CMD_HELP, /* 15: Get help */ |
| 41 | IPSET_CMD_VERSION, /* 16: Get program version */ |
| 42 | IPSET_CMD_QUIT, /* 17: Quit from interactive mode */ |
| 43 | |
| 44 | IPSET_CMD_MAX, |
| 45 | |
| 46 | IPSET_CMD_COMMIT = IPSET_CMD_MAX, /* 18: Commit buffered commands */ |
| 47 | }; |
| 48 | |
| 49 | /* Attributes at command level */ |
| 50 | enum { |
| 51 | IPSET_ATTR_UNSPEC, |
| 52 | IPSET_ATTR_PROTOCOL, /* 1: Protocol version */ |
| 53 | IPSET_ATTR_SETNAME, /* 2: Name of the set */ |
| 54 | IPSET_ATTR_TYPENAME, /* 3: Typename */ |
| 55 | IPSET_ATTR_SETNAME2 = IPSET_ATTR_TYPENAME, /* Setname at rename/swap */ |
| 56 | IPSET_ATTR_REVISION, /* 4: Settype revision */ |
| 57 | IPSET_ATTR_FAMILY, /* 5: Settype family */ |
| 58 | IPSET_ATTR_FLAGS, /* 6: Flags at command level */ |
| 59 | IPSET_ATTR_DATA, /* 7: Nested attributes */ |
| 60 | IPSET_ATTR_ADT, /* 8: Multiple data containers */ |
| 61 | IPSET_ATTR_LINENO, /* 9: Restore lineno */ |
| 62 | IPSET_ATTR_PROTOCOL_MIN, /* 10: Minimal supported version number */ |
| 63 | IPSET_ATTR_REVISION_MIN = IPSET_ATTR_PROTOCOL_MIN, /* type rev min */ |
| 64 | __IPSET_ATTR_CMD_MAX, |
| 65 | }; |
| 66 | #define IPSET_ATTR_CMD_MAX (__IPSET_ATTR_CMD_MAX - 1) |
| 67 | |
| 68 | /* CADT specific attributes */ |
| 69 | enum { |
| 70 | IPSET_ATTR_IP = IPSET_ATTR_UNSPEC + 1, |
| 71 | IPSET_ATTR_IP_FROM = IPSET_ATTR_IP, |
| 72 | IPSET_ATTR_IP_TO, /* 2 */ |
| 73 | IPSET_ATTR_CIDR, /* 3 */ |
| 74 | IPSET_ATTR_PORT, /* 4 */ |
| 75 | IPSET_ATTR_PORT_FROM = IPSET_ATTR_PORT, |
| 76 | IPSET_ATTR_PORT_TO, /* 5 */ |
| 77 | IPSET_ATTR_TIMEOUT, /* 6 */ |
| 78 | IPSET_ATTR_PROTO, /* 7 */ |
| 79 | IPSET_ATTR_CADT_FLAGS, /* 8 */ |
| 80 | IPSET_ATTR_CADT_LINENO = IPSET_ATTR_LINENO, /* 9 */ |
| 81 | /* Reserve empty slots */ |
| 82 | IPSET_ATTR_CADT_MAX = 16, |
| 83 | /* Create-only specific attributes */ |
| 84 | IPSET_ATTR_GC, |
| 85 | IPSET_ATTR_HASHSIZE, |
| 86 | IPSET_ATTR_MAXELEM, |
| 87 | IPSET_ATTR_NETMASK, |
| 88 | IPSET_ATTR_PROBES, |
| 89 | IPSET_ATTR_RESIZE, |
| 90 | IPSET_ATTR_SIZE, |
| 91 | /* Kernel-only */ |
| 92 | IPSET_ATTR_ELEMENTS, |
| 93 | IPSET_ATTR_REFERENCES, |
| 94 | IPSET_ATTR_MEMSIZE, |
| 95 | |
| 96 | __IPSET_ATTR_CREATE_MAX, |
| 97 | }; |
| 98 | #define IPSET_ATTR_CREATE_MAX (__IPSET_ATTR_CREATE_MAX - 1) |
| 99 | |
| 100 | /* ADT specific attributes */ |
| 101 | enum { |
| 102 | IPSET_ATTR_ETHER = IPSET_ATTR_CADT_MAX + 1, |
| 103 | IPSET_ATTR_NAME, |
| 104 | IPSET_ATTR_NAMEREF, |
| 105 | IPSET_ATTR_IP2, |
| 106 | IPSET_ATTR_CIDR2, |
Jozsef Kadlecsik | d0d9e0a | 2011-06-16 18:52:41 +0200 | [diff] [blame] | 107 | IPSET_ATTR_IP2_TO, |
Jozsef Kadlecsik | e385357 | 2011-06-16 19:00:48 +0200 | [diff] [blame] | 108 | IPSET_ATTR_IFACE, |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 109 | __IPSET_ATTR_ADT_MAX, |
| 110 | }; |
| 111 | #define IPSET_ATTR_ADT_MAX (__IPSET_ATTR_ADT_MAX - 1) |
| 112 | |
| 113 | /* IP specific attributes */ |
| 114 | enum { |
| 115 | IPSET_ATTR_IPADDR_IPV4 = IPSET_ATTR_UNSPEC + 1, |
| 116 | IPSET_ATTR_IPADDR_IPV6, |
| 117 | __IPSET_ATTR_IPADDR_MAX, |
| 118 | }; |
| 119 | #define IPSET_ATTR_IPADDR_MAX (__IPSET_ATTR_IPADDR_MAX - 1) |
| 120 | |
| 121 | /* Error codes */ |
| 122 | enum ipset_errno { |
| 123 | IPSET_ERR_PRIVATE = 4096, |
| 124 | IPSET_ERR_PROTOCOL, |
| 125 | IPSET_ERR_FIND_TYPE, |
| 126 | IPSET_ERR_MAX_SETS, |
| 127 | IPSET_ERR_BUSY, |
| 128 | IPSET_ERR_EXIST_SETNAME2, |
| 129 | IPSET_ERR_TYPE_MISMATCH, |
| 130 | IPSET_ERR_EXIST, |
| 131 | IPSET_ERR_INVALID_CIDR, |
| 132 | IPSET_ERR_INVALID_NETMASK, |
| 133 | IPSET_ERR_INVALID_FAMILY, |
| 134 | IPSET_ERR_TIMEOUT, |
| 135 | IPSET_ERR_REFERENCED, |
| 136 | IPSET_ERR_IPADDR_IPV4, |
| 137 | IPSET_ERR_IPADDR_IPV6, |
| 138 | |
| 139 | /* Type specific error codes */ |
| 140 | IPSET_ERR_TYPE_SPECIFIC = 4352, |
| 141 | }; |
| 142 | |
| 143 | /* Flags at command level */ |
| 144 | enum ipset_cmd_flags { |
| 145 | IPSET_FLAG_BIT_EXIST = 0, |
| 146 | IPSET_FLAG_EXIST = (1 << IPSET_FLAG_BIT_EXIST), |
Jozsef Kadlecsik | c1e2e04 | 2011-06-16 18:47:07 +0200 | [diff] [blame] | 147 | IPSET_FLAG_BIT_LIST_SETNAME = 1, |
| 148 | IPSET_FLAG_LIST_SETNAME = (1 << IPSET_FLAG_BIT_LIST_SETNAME), |
| 149 | IPSET_FLAG_BIT_LIST_HEADER = 2, |
| 150 | IPSET_FLAG_LIST_HEADER = (1 << IPSET_FLAG_BIT_LIST_HEADER), |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /* Flags at CADT attribute level */ |
| 154 | enum ipset_cadt_flags { |
| 155 | IPSET_FLAG_BIT_BEFORE = 0, |
| 156 | IPSET_FLAG_BEFORE = (1 << IPSET_FLAG_BIT_BEFORE), |
Jozsef Kadlecsik | e385357 | 2011-06-16 19:00:48 +0200 | [diff] [blame] | 157 | IPSET_FLAG_BIT_PHYSDEV = 1, |
| 158 | IPSET_FLAG_PHYSDEV = (1 << IPSET_FLAG_BIT_PHYSDEV), |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /* Commands with settype-specific attributes */ |
| 162 | enum ipset_adt { |
| 163 | IPSET_ADD, |
| 164 | IPSET_DEL, |
| 165 | IPSET_TEST, |
| 166 | IPSET_ADT_MAX, |
| 167 | IPSET_CREATE = IPSET_ADT_MAX, |
| 168 | IPSET_CADT_MAX, |
| 169 | }; |
| 170 | |
| 171 | #ifdef __KERNEL__ |
| 172 | #include <linux/ip.h> |
| 173 | #include <linux/ipv6.h> |
| 174 | #include <linux/netlink.h> |
| 175 | #include <linux/netfilter.h> |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 176 | #include <linux/netfilter/x_tables.h> |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 177 | #include <linux/vmalloc.h> |
| 178 | #include <net/netlink.h> |
| 179 | |
| 180 | /* Sets are identified by an index in kernel space. Tweak with ip_set_id_t |
| 181 | * and IPSET_INVALID_ID if you want to increase the max number of sets. |
| 182 | */ |
| 183 | typedef u16 ip_set_id_t; |
| 184 | |
| 185 | #define IPSET_INVALID_ID 65535 |
| 186 | |
| 187 | enum ip_set_dim { |
| 188 | IPSET_DIM_ZERO = 0, |
| 189 | IPSET_DIM_ONE, |
| 190 | IPSET_DIM_TWO, |
| 191 | IPSET_DIM_THREE, |
| 192 | /* Max dimension in elements. |
| 193 | * If changed, new revision of iptables match/target is required. |
| 194 | */ |
| 195 | IPSET_DIM_MAX = 6, |
| 196 | }; |
| 197 | |
| 198 | /* Option flags for kernel operations */ |
| 199 | enum ip_set_kopt { |
| 200 | IPSET_INV_MATCH = (1 << IPSET_DIM_ZERO), |
| 201 | IPSET_DIM_ONE_SRC = (1 << IPSET_DIM_ONE), |
| 202 | IPSET_DIM_TWO_SRC = (1 << IPSET_DIM_TWO), |
| 203 | IPSET_DIM_THREE_SRC = (1 << IPSET_DIM_THREE), |
| 204 | }; |
| 205 | |
| 206 | /* Set features */ |
| 207 | enum ip_set_feature { |
| 208 | IPSET_TYPE_IP_FLAG = 0, |
| 209 | IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG), |
| 210 | IPSET_TYPE_PORT_FLAG = 1, |
| 211 | IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG), |
| 212 | IPSET_TYPE_MAC_FLAG = 2, |
| 213 | IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG), |
| 214 | IPSET_TYPE_IP2_FLAG = 3, |
| 215 | IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG), |
| 216 | IPSET_TYPE_NAME_FLAG = 4, |
| 217 | IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG), |
Jozsef Kadlecsik | e385357 | 2011-06-16 19:00:48 +0200 | [diff] [blame] | 218 | IPSET_TYPE_IFACE_FLAG = 5, |
| 219 | IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG), |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 220 | /* Strictly speaking not a feature, but a flag for dumping: |
| 221 | * this settype must be dumped last */ |
| 222 | IPSET_DUMP_LAST_FLAG = 7, |
| 223 | IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG), |
| 224 | }; |
| 225 | |
| 226 | struct ip_set; |
| 227 | |
Jozsef Kadlecsik | 5416219 | 2011-06-16 18:40:55 +0200 | [diff] [blame] | 228 | typedef int (*ipset_adtfn)(struct ip_set *set, void *value, |
| 229 | u32 timeout, u32 flags); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 230 | |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 231 | /* Kernel API function options */ |
| 232 | struct ip_set_adt_opt { |
| 233 | u8 family; /* Actual protocol family */ |
| 234 | u8 dim; /* Dimension of match/target */ |
| 235 | u8 flags; /* Direction and negation flags */ |
| 236 | u32 cmdflags; /* Command-like flags */ |
| 237 | u32 timeout; /* Timeout value */ |
| 238 | }; |
| 239 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 240 | /* Set type, variant-specific part */ |
| 241 | struct ip_set_type_variant { |
| 242 | /* Kernelspace: test/add/del entries |
| 243 | * returns negative error code, |
| 244 | * zero for no match/success to add/delete |
| 245 | * positive for matching element */ |
| 246 | int (*kadt)(struct ip_set *set, const struct sk_buff * skb, |
Jozsef Kadlecsik | b66554c | 2011-06-16 18:56:47 +0200 | [diff] [blame] | 247 | const struct xt_action_param *par, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 248 | enum ipset_adt adt, const struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 249 | |
| 250 | /* Userspace: test/add/del entries |
| 251 | * returns negative error code, |
| 252 | * zero for no match/success to add/delete |
| 253 | * positive for matching element */ |
| 254 | int (*uadt)(struct ip_set *set, struct nlattr *tb[], |
Jozsef Kadlecsik | 3d14b17 | 2011-06-16 18:49:17 +0200 | [diff] [blame] | 255 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 256 | |
| 257 | /* Low level add/del/test functions */ |
| 258 | ipset_adtfn adt[IPSET_ADT_MAX]; |
| 259 | |
| 260 | /* When adding entries and set is full, try to resize the set */ |
| 261 | int (*resize)(struct ip_set *set, bool retried); |
| 262 | /* Destroy the set */ |
| 263 | void (*destroy)(struct ip_set *set); |
| 264 | /* Flush the elements */ |
| 265 | void (*flush)(struct ip_set *set); |
| 266 | /* Expire entries before listing */ |
| 267 | void (*expire)(struct ip_set *set); |
| 268 | /* List set header data */ |
| 269 | int (*head)(struct ip_set *set, struct sk_buff *skb); |
| 270 | /* List elements */ |
| 271 | int (*list)(const struct ip_set *set, struct sk_buff *skb, |
| 272 | struct netlink_callback *cb); |
| 273 | |
| 274 | /* Return true if "b" set is the same as "a" |
| 275 | * according to the create set parameters */ |
| 276 | bool (*same_set)(const struct ip_set *a, const struct ip_set *b); |
| 277 | }; |
| 278 | |
| 279 | /* The core set type structure */ |
| 280 | struct ip_set_type { |
| 281 | struct list_head list; |
| 282 | |
| 283 | /* Typename */ |
| 284 | char name[IPSET_MAXNAMELEN]; |
| 285 | /* Protocol version */ |
| 286 | u8 protocol; |
| 287 | /* Set features to control swapping */ |
| 288 | u8 features; |
| 289 | /* Set type dimension */ |
| 290 | u8 dimension; |
| 291 | /* Supported family: may be AF_UNSPEC for both AF_INET/AF_INET6 */ |
| 292 | u8 family; |
Jozsef Kadlecsik | f1e00b3 | 2011-06-16 18:51:41 +0200 | [diff] [blame] | 293 | /* Type revisions */ |
| 294 | u8 revision_min, revision_max; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 295 | |
| 296 | /* Create set */ |
| 297 | int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags); |
| 298 | |
| 299 | /* Attribute policies */ |
| 300 | const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1]; |
| 301 | const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1]; |
| 302 | |
| 303 | /* Set this to THIS_MODULE if you are a module, otherwise NULL */ |
| 304 | struct module *me; |
| 305 | }; |
| 306 | |
| 307 | /* register and unregister set type */ |
| 308 | extern int ip_set_type_register(struct ip_set_type *set_type); |
| 309 | extern void ip_set_type_unregister(struct ip_set_type *set_type); |
| 310 | |
| 311 | /* A generic IP set */ |
| 312 | struct ip_set { |
| 313 | /* The name of the set */ |
| 314 | char name[IPSET_MAXNAMELEN]; |
| 315 | /* Lock protecting the set data */ |
| 316 | rwlock_t lock; |
| 317 | /* References to the set */ |
Jozsef Kadlecsik | 2f9f28b | 2011-04-04 15:19:25 +0200 | [diff] [blame] | 318 | u32 ref; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 319 | /* The core set type */ |
| 320 | struct ip_set_type *type; |
| 321 | /* The type variant doing the real job */ |
| 322 | const struct ip_set_type_variant *variant; |
| 323 | /* The actual INET family of the set */ |
| 324 | u8 family; |
Jozsef Kadlecsik | f1e00b3 | 2011-06-16 18:51:41 +0200 | [diff] [blame] | 325 | /* The type revision */ |
| 326 | u8 revision; |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 327 | /* The type specific data */ |
| 328 | void *data; |
| 329 | }; |
| 330 | |
| 331 | /* register and unregister set references */ |
| 332 | extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set); |
| 333 | extern void ip_set_put_byindex(ip_set_id_t index); |
Jozsef Kadlecsik | 15b4d93 | 2011-06-16 19:01:26 +0200 | [diff] [blame] | 334 | extern const char *ip_set_name_byindex(ip_set_id_t index); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 335 | extern ip_set_id_t ip_set_nfnl_get(const char *name); |
| 336 | extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index); |
| 337 | extern void ip_set_nfnl_put(ip_set_id_t index); |
| 338 | |
| 339 | /* API for iptables set match, and SET target */ |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 340 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 341 | 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] | 342 | const struct xt_action_param *par, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 343 | const struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 344 | 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] | 345 | const struct xt_action_param *par, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 346 | const struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 347 | 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] | 348 | const struct xt_action_param *par, |
Jozsef Kadlecsik | ac8cc92 | 2011-06-16 18:42:40 +0200 | [diff] [blame] | 349 | const struct ip_set_adt_opt *opt); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 350 | |
| 351 | /* Utility functions */ |
Jozsef Kadlecsik | 15b4d93 | 2011-06-16 19:01:26 +0200 | [diff] [blame] | 352 | extern void *ip_set_alloc(size_t size); |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 353 | extern void ip_set_free(void *members); |
| 354 | extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr); |
| 355 | extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr); |
| 356 | |
| 357 | static inline int |
| 358 | ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr) |
| 359 | { |
| 360 | __be32 ip; |
| 361 | int ret = ip_set_get_ipaddr4(nla, &ip); |
Jozsef Kadlecsik | 15b4d93 | 2011-06-16 19:01:26 +0200 | [diff] [blame] | 362 | |
Jozsef Kadlecsik | a7b4f98 | 2011-02-01 15:28:35 +0100 | [diff] [blame] | 363 | if (ret) |
| 364 | return ret; |
| 365 | *ipaddr = ntohl(ip); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* Ignore IPSET_ERR_EXIST errors if asked to do so? */ |
| 370 | static inline bool |
| 371 | ip_set_eexist(int ret, u32 flags) |
| 372 | { |
| 373 | return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST); |
| 374 | } |
| 375 | |
| 376 | /* Check the NLA_F_NET_BYTEORDER flag */ |
| 377 | static inline bool |
| 378 | ip_set_attr_netorder(struct nlattr *tb[], int type) |
| 379 | { |
| 380 | return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER); |
| 381 | } |
| 382 | |
| 383 | static inline bool |
| 384 | ip_set_optattr_netorder(struct nlattr *tb[], int type) |
| 385 | { |
| 386 | return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER); |
| 387 | } |
| 388 | |
| 389 | /* Useful converters */ |
| 390 | static inline u32 |
| 391 | ip_set_get_h32(const struct nlattr *attr) |
| 392 | { |
| 393 | return ntohl(nla_get_be32(attr)); |
| 394 | } |
| 395 | |
| 396 | static inline u16 |
| 397 | ip_set_get_h16(const struct nlattr *attr) |
| 398 | { |
| 399 | return ntohs(nla_get_be16(attr)); |
| 400 | } |
| 401 | |
| 402 | #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED) |
| 403 | #define ipset_nest_end(skb, start) nla_nest_end(skb, start) |
| 404 | |
| 405 | #define NLA_PUT_IPADDR4(skb, type, ipaddr) \ |
| 406 | do { \ |
| 407 | struct nlattr *__nested = ipset_nest_start(skb, type); \ |
| 408 | \ |
| 409 | if (!__nested) \ |
| 410 | goto nla_put_failure; \ |
| 411 | NLA_PUT_NET32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr); \ |
| 412 | ipset_nest_end(skb, __nested); \ |
| 413 | } while (0) |
| 414 | |
| 415 | #define NLA_PUT_IPADDR6(skb, type, ipaddrptr) \ |
| 416 | do { \ |
| 417 | struct nlattr *__nested = ipset_nest_start(skb, type); \ |
| 418 | \ |
| 419 | if (!__nested) \ |
| 420 | goto nla_put_failure; \ |
| 421 | NLA_PUT(skb, IPSET_ATTR_IPADDR_IPV6, \ |
| 422 | sizeof(struct in6_addr), ipaddrptr); \ |
| 423 | ipset_nest_end(skb, __nested); \ |
| 424 | } while (0) |
| 425 | |
| 426 | /* Get address from skbuff */ |
| 427 | static inline __be32 |
| 428 | ip4addr(const struct sk_buff *skb, bool src) |
| 429 | { |
| 430 | return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr; |
| 431 | } |
| 432 | |
| 433 | static inline void |
| 434 | ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr) |
| 435 | { |
| 436 | *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr; |
| 437 | } |
| 438 | |
| 439 | static inline void |
| 440 | ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr) |
| 441 | { |
| 442 | memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr, |
| 443 | sizeof(*addr)); |
| 444 | } |
| 445 | |
| 446 | /* Calculate the bytes required to store the inclusive range of a-b */ |
| 447 | static inline int |
| 448 | bitmap_bytes(u32 a, u32 b) |
| 449 | { |
| 450 | return 4 * ((((b - a + 8) / 8) + 3) / 4); |
| 451 | } |
| 452 | |
| 453 | /* Interface to iptables/ip6tables */ |
| 454 | |
| 455 | #define SO_IP_SET 83 |
| 456 | |
| 457 | union ip_set_name_index { |
| 458 | char name[IPSET_MAXNAMELEN]; |
| 459 | ip_set_id_t index; |
| 460 | }; |
| 461 | |
| 462 | #define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */ |
| 463 | struct ip_set_req_get_set { |
| 464 | unsigned op; |
| 465 | unsigned version; |
| 466 | union ip_set_name_index set; |
| 467 | }; |
| 468 | |
| 469 | #define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */ |
| 470 | /* Uses ip_set_req_get_set */ |
| 471 | |
| 472 | #define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */ |
| 473 | struct ip_set_req_version { |
| 474 | unsigned op; |
| 475 | unsigned version; |
| 476 | }; |
| 477 | |
| 478 | #endif /* __KERNEL__ */ |
| 479 | |
| 480 | #endif /*_IP_SET_H */ |