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>
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
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 nf_sockopt_ops
*ops
;
29 mutex_lock(&nf_sockopt_mutex
);
30 list_for_each_entry(ops
, &nf_sockopts
, list
) {
31 if (ops
->pf
== reg
->pf
32 && (overlap(ops
->set_optmin
, ops
->set_optmax
,
33 reg
->set_optmin
, reg
->set_optmax
)
34 || overlap(ops
->get_optmin
, ops
->get_optmax
,
35 reg
->get_optmin
, reg
->get_optmax
))) {
36 NFDEBUG("nf_sock overlap: %u-%u/%u-%u v %u-%u/%u-%u\n",
37 ops
->set_optmin
, ops
->set_optmax
,
38 ops
->get_optmin
, ops
->get_optmax
,
39 reg
->set_optmin
, reg
->set_optmax
,
40 reg
->get_optmin
, reg
->get_optmax
);
46 list_add(®
->list
, &nf_sockopts
);
48 mutex_unlock(&nf_sockopt_mutex
);
51 EXPORT_SYMBOL(nf_register_sockopt
);
53 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
)
55 mutex_lock(&nf_sockopt_mutex
);
57 mutex_unlock(&nf_sockopt_mutex
);
59 EXPORT_SYMBOL(nf_unregister_sockopt
);
61 static struct nf_sockopt_ops
*nf_sockopt_find(struct sock
*sk
, u_int8_t pf
,
64 struct nf_sockopt_ops
*ops
;
66 mutex_lock(&nf_sockopt_mutex
);
67 list_for_each_entry(ops
, &nf_sockopts
, list
) {
69 if (!try_module_get(ops
->owner
))
73 if (val
>= ops
->get_optmin
&&
74 val
< ops
->get_optmax
)
77 if (val
>= ops
->set_optmin
&&
78 val
< ops
->set_optmax
)
81 module_put(ops
->owner
);
85 ops
= ERR_PTR(-ENOPROTOOPT
);
87 mutex_unlock(&nf_sockopt_mutex
);
91 /* Call get/setsockopt() */
92 static int nf_sockopt(struct sock
*sk
, u_int8_t pf
, int val
,
93 char __user
*opt
, int *len
, int get
)
95 struct nf_sockopt_ops
*ops
;
98 ops
= nf_sockopt_find(sk
, pf
, val
, get
);
103 ret
= ops
->get(sk
, val
, opt
, len
);
105 ret
= ops
->set(sk
, val
, opt
, *len
);
107 module_put(ops
->owner
);
111 int nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int val
, char __user
*opt
,
114 return nf_sockopt(sk
, pf
, val
, opt
, &len
, 0);
116 EXPORT_SYMBOL(nf_setsockopt
);
118 int nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int val
, char __user
*opt
,
121 return nf_sockopt(sk
, pf
, val
, opt
, len
, 1);
123 EXPORT_SYMBOL(nf_getsockopt
);
126 static int compat_nf_sockopt(struct sock
*sk
, u_int8_t pf
, int val
,
127 char __user
*opt
, int *len
, int get
)
129 struct nf_sockopt_ops
*ops
;
132 ops
= nf_sockopt_find(sk
, pf
, val
, get
);
138 ret
= ops
->compat_get(sk
, val
, opt
, len
);
140 ret
= ops
->get(sk
, val
, opt
, len
);
143 ret
= ops
->compat_set(sk
, val
, opt
, *len
);
145 ret
= ops
->set(sk
, val
, opt
, *len
);
148 module_put(ops
->owner
);
152 int compat_nf_setsockopt(struct sock
*sk
, u_int8_t pf
,
153 int val
, char __user
*opt
, unsigned int len
)
155 return compat_nf_sockopt(sk
, pf
, val
, opt
, &len
, 0);
157 EXPORT_SYMBOL(compat_nf_setsockopt
);
159 int compat_nf_getsockopt(struct sock
*sk
, u_int8_t pf
,
160 int val
, char __user
*opt
, int *len
)
162 return compat_nf_sockopt(sk
, pf
, val
, opt
, len
, 1);
164 EXPORT_SYMBOL(compat_nf_getsockopt
);