Skip to content

Commit 68b63f0

Browse files
OliproJozsef Kadlecsik
authored andcommitted
netfilter: ipset: Support comments for ipset entries in the core.
This adds the core support for having comments on ipset entries. The comments are stored as standard null-terminated strings in dynamically allocated memory after being passed to the kernel. As a result of this, code has been added to the generic destroy function to iterate all extensions and call that extension's destroy task if the set has that extension activated, and if such a task is defined. 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>
1 parent ea53ac5 commit 68b63f0

4 files changed

Lines changed: 121 additions & 9 deletions

File tree

‎include/linux/netfilter/ipset/ip_set.h‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ enum ip_set_extension {
5353
IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
5454
IPSET_EXT_BIT_COUNTER = 1,
5555
IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
56+
IPSET_EXT_BIT_COMMENT = 2,
57+
IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
5658
/* Mark set with an extension which needs to call destroy */
5759
IPSET_EXT_BIT_DESTROY = 7,
5860
IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
5961
};
6062

6163
#define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
6264
#define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
65+
#define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
6366

6467
/* Extension id, in size order */
6568
enum ip_set_ext_id {
6669
IPSET_EXT_ID_COUNTER = 0,
6770
IPSET_EXT_ID_TIMEOUT,
71+
IPSET_EXT_ID_COMMENT,
6872
IPSET_EXT_ID_MAX,
6973
};
7074

@@ -85,27 +89,27 @@ struct ip_set_ext {
8589
u64 packets;
8690
u64 bytes;
8791
u32 timeout;
92+
char *comment;
8893
};
8994

9095
struct ip_set_counter {
9196
atomic64_t bytes;
9297
atomic64_t packets;
9398
};
9499

95-
struct ip_set;
100+
struct ip_set_comment {
101+
char *str;
102+
};
96103

97-
static inline void
98-
ip_set_ext_destroy(struct ip_set *set, void *data)
99-
{
100-
/* Check that the extension is enabled for the set and
101-
* call it's destroy function for its extension part in data.
102-
*/
103-
}
104+
struct ip_set;
104105

105106
#define ext_timeout(e, s) \
106107
(unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT])
107108
#define ext_counter(e, s) \
108109
(struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER])
110+
#define ext_comment(e, s) \
111+
(struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT])
112+
109113

110114
typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
111115
const struct ip_set_ext *ext,
@@ -222,6 +226,36 @@ struct ip_set {
222226
void *data;
223227
};
224228

229+
static inline void
230+
ip_set_ext_destroy(struct ip_set *set, void *data)
231+
{
232+
/* Check that the extension is enabled for the set and
233+
* call it's destroy function for its extension part in data.
234+
*/
235+
if (SET_WITH_COMMENT(set))
236+
ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
237+
ext_comment(data, set));
238+
}
239+
240+
static inline int
241+
ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
242+
{
243+
u32 cadt_flags = 0;
244+
245+
if (SET_WITH_TIMEOUT(set))
246+
if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
247+
htonl(set->timeout))))
248+
return -EMSGSIZE;
249+
if (SET_WITH_COUNTER(set))
250+
cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
251+
if (SET_WITH_COMMENT(set))
252+
cadt_flags |= IPSET_FLAG_WITH_COMMENT;
253+
254+
if (!cadt_flags)
255+
return 0;
256+
return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
257+
}
258+
225259
static inline void
226260
ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
227261
{
@@ -425,6 +459,7 @@ bitmap_bytes(u32 a, u32 b)
425459
}
426460

427461
#include <linux/netfilter/ipset/ip_set_timeout.h>
462+
#include <linux/netfilter/ipset/ip_set_comment.h>
428463

429464
#define IP_SET_INIT_KEXT(skb, opt, set) \
430465
{ .bytes = (skb)->len, .packets = 1, \
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef _IP_SET_COMMENT_H
2+
#define _IP_SET_COMMENT_H
3+
4+
/* Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
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+
*/
10+
11+
#ifdef __KERNEL__
12+
13+
static inline char*
14+
ip_set_comment_uget(struct nlattr *tb)
15+
{
16+
return nla_data(tb);
17+
}
18+
19+
static inline void
20+
ip_set_init_comment(struct ip_set_comment *comment,
21+
const struct ip_set_ext *ext)
22+
{
23+
size_t len = ext->comment ? strlen(ext->comment) : 0;
24+
25+
if (unlikely(comment->str)) {
26+
kfree(comment->str);
27+
comment->str = NULL;
28+
}
29+
if (!len)
30+
return;
31+
if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
32+
len = IPSET_MAX_COMMENT_SIZE;
33+
comment->str = kzalloc(len + 1, GFP_ATOMIC);
34+
if (unlikely(!comment->str))
35+
return;
36+
strlcpy(comment->str, ext->comment, len + 1);
37+
}
38+
39+
static inline int
40+
ip_set_put_comment(struct sk_buff *skb, struct ip_set_comment *comment)
41+
{
42+
if (!comment->str)
43+
return 0;
44+
return nla_put_string(skb, IPSET_ATTR_COMMENT, comment->str);
45+
}
46+
47+
static inline void
48+
ip_set_comment_free(struct ip_set_comment *comment)
49+
{
50+
if (unlikely(!comment->str))
51+
return;
52+
kfree(comment->str);
53+
comment->str = NULL;
54+
}
55+
56+
#endif
57+
#endif

‎include/uapi/linux/netfilter/ipset/ip_set.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
#ifndef _UAPI_IP_SET_H
1111
#define _UAPI_IP_SET_H
1212

13-
1413
#include <linux/types.h>
1514

1615
/* The protocol version */
1716
#define IPSET_PROTOCOL 6
1817

18+
/* The maximum permissible comment length we will accept over netlink */
19+
#define IPSET_MAX_COMMENT_SIZE 255
20+
1921
/* The max length of strings including NUL: set and type identifiers */
2022
#define IPSET_MAXNAMELEN 32
2123

@@ -110,6 +112,7 @@ enum {
110112
IPSET_ATTR_IFACE,
111113
IPSET_ATTR_BYTES,
112114
IPSET_ATTR_PACKETS,
115+
IPSET_ATTR_COMMENT,
113116
__IPSET_ATTR_ADT_MAX,
114117
};
115118
#define IPSET_ATTR_ADT_MAX (__IPSET_ATTR_ADT_MAX - 1)
@@ -140,6 +143,7 @@ enum ipset_errno {
140143
IPSET_ERR_IPADDR_IPV4,
141144
IPSET_ERR_IPADDR_IPV6,
142145
IPSET_ERR_COUNTER,
146+
IPSET_ERR_COMMENT,
143147

144148
/* Type specific error codes */
145149
IPSET_ERR_TYPE_SPECIFIC = 4352,
@@ -176,6 +180,8 @@ enum ipset_cadt_flags {
176180
IPSET_FLAG_NOMATCH = (1 << IPSET_FLAG_BIT_NOMATCH),
177181
IPSET_FLAG_BIT_WITH_COUNTERS = 3,
178182
IPSET_FLAG_WITH_COUNTERS = (1 << IPSET_FLAG_BIT_WITH_COUNTERS),
183+
IPSET_FLAG_BIT_WITH_COMMENT = 4,
184+
IPSET_FLAG_WITH_COMMENT = (1 << IPSET_FLAG_BIT_WITH_COMMENT),
179185
IPSET_FLAG_CADT_MAX = 15,
180186
};
181187

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
315315
}
316316
EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
317317

318+
typedef void (*destroyer)(void *);
318319
/* ipset data extension types, in size order */
319320

320321
const struct ip_set_ext_type ip_set_extensions[] = {
@@ -329,6 +330,13 @@ const struct ip_set_ext_type ip_set_extensions[] = {
329330
.len = sizeof(unsigned long),
330331
.align = __alignof__(unsigned long),
331332
},
333+
[IPSET_EXT_ID_COMMENT] = {
334+
.type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
335+
.flag = IPSET_FLAG_WITH_COMMENT,
336+
.len = sizeof(struct ip_set_comment),
337+
.align = __alignof__(struct ip_set_comment),
338+
.destroy = (destroyer) ip_set_comment_free,
339+
},
332340
};
333341
EXPORT_SYMBOL_GPL(ip_set_extensions);
334342

@@ -380,6 +388,12 @@ ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
380388
ext->packets = be64_to_cpu(nla_get_be64(
381389
tb[IPSET_ATTR_PACKETS]));
382390
}
391+
if (tb[IPSET_ATTR_COMMENT]) {
392+
if (!(set->extensions & IPSET_EXT_COMMENT))
393+
return -IPSET_ERR_COMMENT;
394+
ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
395+
}
396+
383397
return 0;
384398
}
385399
EXPORT_SYMBOL_GPL(ip_set_get_extensions);

0 commit comments

Comments
 (0)