Use helpers to obtain task pid in printks (arch code)
[wrt350n-kernel.git] / net / netfilter / nf_sockopt.c
blobaa2831587b821d1c3afb65d59f96d0eb739a9377
1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/skbuff.h>
5 #include <linux/netfilter.h>
6 #include <linux/mutex.h>
7 #include <net/sock.h>
9 #include "nf_internals.h"
11 /* Sockopts only registered and called from user context, so
12 net locking would be overkill. Also, [gs]etsockopt calls may
13 sleep. */
14 static DEFINE_MUTEX(nf_sockopt_mutex);
15 static LIST_HEAD(nf_sockopts);
17 /* Do exclusive ranges overlap? */
18 static inline int overlap(int min1, int max1, int min2, int max2)
20 return max1 > min2 && min1 < max2;
23 /* Functions to register sockopt ranges (exclusive). */
24 int nf_register_sockopt(struct nf_sockopt_ops *reg)
26 struct list_head *i;
27 int ret = 0;
29 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
30 return -EINTR;
32 list_for_each(i, &nf_sockopts) {
33 struct nf_sockopt_ops *ops = (struct nf_sockopt_ops *)i;
34 if (ops->pf == reg->pf
35 && (overlap(ops->set_optmin, ops->set_optmax,
36 reg->set_optmin, reg->set_optmax)
37 || overlap(ops->get_optmin, ops->get_optmax,
38 reg->get_optmin, reg->get_optmax))) {
39 NFDEBUG("nf_sock overlap: %u-%u/%u-%u v %u-%u/%u-%u\n",
40 ops->set_optmin, ops->set_optmax,
41 ops->get_optmin, ops->get_optmax,
42 reg->set_optmin, reg->set_optmax,
43 reg->get_optmin, reg->get_optmax);
44 ret = -EBUSY;
45 goto out;
49 list_add(&reg->list, &nf_sockopts);
50 out:
51 mutex_unlock(&nf_sockopt_mutex);
52 return ret;
54 EXPORT_SYMBOL(nf_register_sockopt);
56 void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
58 mutex_lock(&nf_sockopt_mutex);
59 list_del(&reg->list);
60 mutex_unlock(&nf_sockopt_mutex);
62 EXPORT_SYMBOL(nf_unregister_sockopt);
64 /* Call get/setsockopt() */
65 static int nf_sockopt(struct sock *sk, int pf, int val,
66 char __user *opt, int *len, int get)
68 struct list_head *i;
69 struct nf_sockopt_ops *ops;
70 int ret;
72 if (sk->sk_net != &init_net)
73 return -ENOPROTOOPT;
75 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
76 return -EINTR;
78 list_for_each(i, &nf_sockopts) {
79 ops = (struct nf_sockopt_ops *)i;
80 if (ops->pf == pf) {
81 if (!try_module_get(ops->owner))
82 goto out_nosup;
83 if (get) {
84 if (val >= ops->get_optmin
85 && val < ops->get_optmax) {
86 mutex_unlock(&nf_sockopt_mutex);
87 ret = ops->get(sk, val, opt, len);
88 goto out;
90 } else {
91 if (val >= ops->set_optmin
92 && val < ops->set_optmax) {
93 mutex_unlock(&nf_sockopt_mutex);
94 ret = ops->set(sk, val, opt, *len);
95 goto out;
98 module_put(ops->owner);
101 out_nosup:
102 mutex_unlock(&nf_sockopt_mutex);
103 return -ENOPROTOOPT;
105 out:
106 module_put(ops->owner);
107 return ret;
110 int nf_setsockopt(struct sock *sk, int pf, int val, char __user *opt,
111 int len)
113 return nf_sockopt(sk, pf, val, opt, &len, 0);
115 EXPORT_SYMBOL(nf_setsockopt);
117 int nf_getsockopt(struct sock *sk, int pf, int val, char __user *opt, int *len)
119 return nf_sockopt(sk, pf, val, opt, len, 1);
121 EXPORT_SYMBOL(nf_getsockopt);
123 #ifdef CONFIG_COMPAT
124 static int compat_nf_sockopt(struct sock *sk, int pf, int val,
125 char __user *opt, int *len, int get)
127 struct list_head *i;
128 struct nf_sockopt_ops *ops;
129 int ret;
131 if (sk->sk_net != &init_net)
132 return -ENOPROTOOPT;
135 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
136 return -EINTR;
138 list_for_each(i, &nf_sockopts) {
139 ops = (struct nf_sockopt_ops *)i;
140 if (ops->pf == pf) {
141 if (!try_module_get(ops->owner))
142 goto out_nosup;
144 if (get) {
145 if (val >= ops->get_optmin
146 && val < ops->get_optmax) {
147 mutex_unlock(&nf_sockopt_mutex);
148 if (ops->compat_get)
149 ret = ops->compat_get(sk,
150 val, opt, len);
151 else
152 ret = ops->get(sk,
153 val, opt, len);
154 goto out;
156 } else {
157 if (val >= ops->set_optmin
158 && val < ops->set_optmax) {
159 mutex_unlock(&nf_sockopt_mutex);
160 if (ops->compat_set)
161 ret = ops->compat_set(sk,
162 val, opt, *len);
163 else
164 ret = ops->set(sk,
165 val, opt, *len);
166 goto out;
169 module_put(ops->owner);
172 out_nosup:
173 mutex_unlock(&nf_sockopt_mutex);
174 return -ENOPROTOOPT;
176 out:
177 module_put(ops->owner);
178 return ret;
181 int compat_nf_setsockopt(struct sock *sk, int pf,
182 int val, char __user *opt, int len)
184 return compat_nf_sockopt(sk, pf, val, opt, &len, 0);
186 EXPORT_SYMBOL(compat_nf_setsockopt);
188 int compat_nf_getsockopt(struct sock *sk, int pf,
189 int val, char __user *opt, int *len)
191 return compat_nf_sockopt(sk, pf, val, opt, len, 1);
193 EXPORT_SYMBOL(compat_nf_getsockopt);
194 #endif