Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6/mini2440.git] / net / netfilter / nf_sockopt.c
blob8b8ece750313b5a401741564c57c4eb3b775b552
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 /* No point being interruptible: we're probably in cleanup_module() */
59 restart:
60 mutex_lock(&nf_sockopt_mutex);
61 if (reg->use != 0) {
62 /* To be woken by nf_sockopt call... */
63 /* FIXME: Stuart Young's name appears gratuitously. */
64 set_current_state(TASK_UNINTERRUPTIBLE);
65 reg->cleanup_task = current;
66 mutex_unlock(&nf_sockopt_mutex);
67 schedule();
68 goto restart;
70 list_del(&reg->list);
71 mutex_unlock(&nf_sockopt_mutex);
73 EXPORT_SYMBOL(nf_unregister_sockopt);
75 /* Call get/setsockopt() */
76 static int nf_sockopt(struct sock *sk, int pf, int val,
77 char __user *opt, int *len, int get)
79 struct list_head *i;
80 struct nf_sockopt_ops *ops;
81 int ret;
83 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
84 return -EINTR;
86 list_for_each(i, &nf_sockopts) {
87 ops = (struct nf_sockopt_ops *)i;
88 if (ops->pf == pf) {
89 if (get) {
90 if (val >= ops->get_optmin
91 && val < ops->get_optmax) {
92 ops->use++;
93 mutex_unlock(&nf_sockopt_mutex);
94 ret = ops->get(sk, val, opt, len);
95 goto out;
97 } else {
98 if (val >= ops->set_optmin
99 && val < ops->set_optmax) {
100 ops->use++;
101 mutex_unlock(&nf_sockopt_mutex);
102 ret = ops->set(sk, val, opt, *len);
103 goto out;
108 mutex_unlock(&nf_sockopt_mutex);
109 return -ENOPROTOOPT;
111 out:
112 mutex_lock(&nf_sockopt_mutex);
113 ops->use--;
114 if (ops->cleanup_task)
115 wake_up_process(ops->cleanup_task);
116 mutex_unlock(&nf_sockopt_mutex);
117 return ret;
120 int nf_setsockopt(struct sock *sk, int pf, int val, char __user *opt,
121 int len)
123 return nf_sockopt(sk, pf, val, opt, &len, 0);
125 EXPORT_SYMBOL(nf_setsockopt);
127 int nf_getsockopt(struct sock *sk, int pf, int val, char __user *opt, int *len)
129 return nf_sockopt(sk, pf, val, opt, len, 1);
131 EXPORT_SYMBOL(nf_getsockopt);
133 #ifdef CONFIG_COMPAT
134 static int compat_nf_sockopt(struct sock *sk, int pf, int val,
135 char __user *opt, int *len, int get)
137 struct list_head *i;
138 struct nf_sockopt_ops *ops;
139 int ret;
141 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
142 return -EINTR;
144 list_for_each(i, &nf_sockopts) {
145 ops = (struct nf_sockopt_ops *)i;
146 if (ops->pf == pf) {
147 if (get) {
148 if (val >= ops->get_optmin
149 && val < ops->get_optmax) {
150 ops->use++;
151 mutex_unlock(&nf_sockopt_mutex);
152 if (ops->compat_get)
153 ret = ops->compat_get(sk,
154 val, opt, len);
155 else
156 ret = ops->get(sk,
157 val, opt, len);
158 goto out;
160 } else {
161 if (val >= ops->set_optmin
162 && val < ops->set_optmax) {
163 ops->use++;
164 mutex_unlock(&nf_sockopt_mutex);
165 if (ops->compat_set)
166 ret = ops->compat_set(sk,
167 val, opt, *len);
168 else
169 ret = ops->set(sk,
170 val, opt, *len);
171 goto out;
176 mutex_unlock(&nf_sockopt_mutex);
177 return -ENOPROTOOPT;
179 out:
180 mutex_lock(&nf_sockopt_mutex);
181 ops->use--;
182 if (ops->cleanup_task)
183 wake_up_process(ops->cleanup_task);
184 mutex_unlock(&nf_sockopt_mutex);
185 return ret;
188 int compat_nf_setsockopt(struct sock *sk, int pf,
189 int val, char __user *opt, int len)
191 return compat_nf_sockopt(sk, pf, val, opt, &len, 0);
193 EXPORT_SYMBOL(compat_nf_setsockopt);
195 int compat_nf_getsockopt(struct sock *sk, int pf,
196 int val, char __user *opt, int *len)
198 return compat_nf_sockopt(sk, pf, val, opt, len, 1);
200 EXPORT_SYMBOL(compat_nf_getsockopt);
201 #endif