1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/skbuff.h>
6 #include <linux/netfilter.h>
7 #include <linux/mutex.h>
10 #include "nf_internals.h"
12 /* Sockopts only registered and called from user context, so
13 net locking would be overkill. Also, [gs]etsockopt calls may
15 static DEFINE_MUTEX(nf_sockopt_mutex
);
16 static LIST_HEAD(nf_sockopts
);
18 /* Do exclusive ranges overlap? */
19 static inline int overlap(int min1
, int max1
, int min2
, int max2
)
21 return max1
> min2
&& min1
< max2
;
24 /* Functions to register sockopt ranges (exclusive). */
25 int nf_register_sockopt(struct nf_sockopt_ops
*reg
)
30 if (mutex_lock_interruptible(&nf_sockopt_mutex
) != 0)
33 list_for_each(i
, &nf_sockopts
) {
34 struct nf_sockopt_ops
*ops
= (struct nf_sockopt_ops
*)i
;
35 if (ops
->pf
== reg
->pf
36 && (overlap(ops
->set_optmin
, ops
->set_optmax
,
37 reg
->set_optmin
, reg
->set_optmax
)
38 || overlap(ops
->get_optmin
, ops
->get_optmax
,
39 reg
->get_optmin
, reg
->get_optmax
))) {
40 NFDEBUG("nf_sock overlap: %u-%u/%u-%u v %u-%u/%u-%u\n",
41 ops
->set_optmin
, ops
->set_optmax
,
42 ops
->get_optmin
, ops
->get_optmax
,
43 reg
->set_optmin
, reg
->set_optmax
,
44 reg
->get_optmin
, reg
->get_optmax
);
50 list_add(®
->list
, &nf_sockopts
);
52 mutex_unlock(&nf_sockopt_mutex
);
55 EXPORT_SYMBOL(nf_register_sockopt
);
57 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
)
59 /* No point being interruptible: we're probably in cleanup_module() */
61 mutex_lock(&nf_sockopt_mutex
);
63 /* To be woken by nf_sockopt call... */
64 /* FIXME: Stuart Young's name appears gratuitously. */
65 set_current_state(TASK_UNINTERRUPTIBLE
);
66 reg
->cleanup_task
= current
;
67 mutex_unlock(&nf_sockopt_mutex
);
72 mutex_unlock(&nf_sockopt_mutex
);
74 EXPORT_SYMBOL(nf_unregister_sockopt
);
76 /* Call get/setsockopt() */
77 static int nf_sockopt(struct sock
*sk
, int pf
, int val
,
78 char __user
*opt
, int *len
, int get
)
81 struct nf_sockopt_ops
*ops
;
84 if (mutex_lock_interruptible(&nf_sockopt_mutex
) != 0)
87 list_for_each(i
, &nf_sockopts
) {
88 ops
= (struct nf_sockopt_ops
*)i
;
91 if (val
>= ops
->get_optmin
92 && val
< ops
->get_optmax
) {
94 mutex_unlock(&nf_sockopt_mutex
);
95 ret
= ops
->get(sk
, val
, opt
, len
);
99 if (val
>= ops
->set_optmin
100 && val
< ops
->set_optmax
) {
102 mutex_unlock(&nf_sockopt_mutex
);
103 ret
= ops
->set(sk
, val
, opt
, *len
);
109 mutex_unlock(&nf_sockopt_mutex
);
113 mutex_lock(&nf_sockopt_mutex
);
115 if (ops
->cleanup_task
)
116 wake_up_process(ops
->cleanup_task
);
117 mutex_unlock(&nf_sockopt_mutex
);
121 int nf_setsockopt(struct sock
*sk
, int pf
, int val
, char __user
*opt
,
124 return nf_sockopt(sk
, pf
, val
, opt
, &len
, 0);
126 EXPORT_SYMBOL(nf_setsockopt
);
128 int nf_getsockopt(struct sock
*sk
, int pf
, int val
, char __user
*opt
, int *len
)
130 return nf_sockopt(sk
, pf
, val
, opt
, len
, 1);
132 EXPORT_SYMBOL(nf_getsockopt
);
135 static int compat_nf_sockopt(struct sock
*sk
, int pf
, int val
,
136 char __user
*opt
, int *len
, int get
)
139 struct nf_sockopt_ops
*ops
;
142 if (mutex_lock_interruptible(&nf_sockopt_mutex
) != 0)
145 list_for_each(i
, &nf_sockopts
) {
146 ops
= (struct nf_sockopt_ops
*)i
;
149 if (val
>= ops
->get_optmin
150 && val
< ops
->get_optmax
) {
152 mutex_unlock(&nf_sockopt_mutex
);
154 ret
= ops
->compat_get(sk
,
162 if (val
>= ops
->set_optmin
163 && val
< ops
->set_optmax
) {
165 mutex_unlock(&nf_sockopt_mutex
);
167 ret
= ops
->compat_set(sk
,
177 mutex_unlock(&nf_sockopt_mutex
);
181 mutex_lock(&nf_sockopt_mutex
);
183 if (ops
->cleanup_task
)
184 wake_up_process(ops
->cleanup_task
);
185 mutex_unlock(&nf_sockopt_mutex
);
189 int compat_nf_setsockopt(struct sock
*sk
, int pf
,
190 int val
, char __user
*opt
, int len
)
192 return compat_nf_sockopt(sk
, pf
, val
, opt
, &len
, 0);
194 EXPORT_SYMBOL(compat_nf_setsockopt
);
196 int compat_nf_getsockopt(struct sock
*sk
, int pf
,
197 int val
, char __user
*opt
, int *len
)
199 return compat_nf_sockopt(sk
, pf
, val
, opt
, len
, 1);
201 EXPORT_SYMBOL(compat_nf_getsockopt
);