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*
ip_set_comment_uget(struct nlattr * tb)14 ip_set_comment_uget(struct nlattr *tb)
15 {
16 return nla_data(tb);
17 }
18
19 /* Called from uadd only, protected by the set spinlock.
20 * The kadt functions don't use the comment extensions in any way.
21 */
22 static inline void
ip_set_init_comment(struct ip_set_comment * comment,const struct ip_set_ext * ext)23 ip_set_init_comment(struct ip_set_comment *comment,
24 const struct ip_set_ext *ext)
25 {
26 struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
27 size_t len = ext->comment ? strlen(ext->comment) : 0;
28
29 if (unlikely(c)) {
30 kfree_rcu(c, rcu);
31 rcu_assign_pointer(comment->c, NULL);
32 }
33 if (!len)
34 return;
35 if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
36 len = IPSET_MAX_COMMENT_SIZE;
37 c = kzalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
38 if (unlikely(!c))
39 return;
40 strlcpy(c->str, ext->comment, len + 1);
41 rcu_assign_pointer(comment->c, c);
42 }
43
44 /* Used only when dumping a set, protected by rcu_read_lock_bh() */
45 static inline int
ip_set_put_comment(struct sk_buff * skb,struct ip_set_comment * comment)46 ip_set_put_comment(struct sk_buff *skb, struct ip_set_comment *comment)
47 {
48 struct ip_set_comment_rcu *c = rcu_dereference_bh(comment->c);
49
50 if (!c)
51 return 0;
52 return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
53 }
54
55 /* Called from uadd/udel, flush or the garbage collectors protected
56 * by the set spinlock.
57 * Called when the set is destroyed and when there can't be any user
58 * of the set data anymore.
59 */
60 static inline void
ip_set_comment_free(struct ip_set_comment * comment)61 ip_set_comment_free(struct ip_set_comment *comment)
62 {
63 struct ip_set_comment_rcu *c;
64
65 c = rcu_dereference_protected(comment->c, 1);
66 if (unlikely(!c))
67 return;
68 kfree_rcu(c, rcu);
69 rcu_assign_pointer(comment->c, NULL);
70 }
71
72 #endif
73 #endif
74