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>
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 DECLARE_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
)
29 if (down_interruptible(&nf_sockopt_mutex
) != 0)
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
);
49 list_add(®
->list
, &nf_sockopts
);
51 up(&nf_sockopt_mutex
);
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() */
60 down(&nf_sockopt_mutex
);
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 up(&nf_sockopt_mutex
);
71 up(&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
)
80 struct nf_sockopt_ops
*ops
;
83 if (down_interruptible(&nf_sockopt_mutex
) != 0)
86 list_for_each(i
, &nf_sockopts
) {
87 ops
= (struct nf_sockopt_ops
*)i
;
90 if (val
>= ops
->get_optmin
91 && val
< ops
->get_optmax
) {
93 up(&nf_sockopt_mutex
);
94 ret
= ops
->get(sk
, val
, opt
, len
);
98 if (val
>= ops
->set_optmin
99 && val
< ops
->set_optmax
) {
101 up(&nf_sockopt_mutex
);
102 ret
= ops
->set(sk
, val
, opt
, *len
);
108 up(&nf_sockopt_mutex
);
112 down(&nf_sockopt_mutex
);
114 if (ops
->cleanup_task
)
115 wake_up_process(ops
->cleanup_task
);
116 up(&nf_sockopt_mutex
);
120 int nf_setsockopt(struct sock
*sk
, int pf
, int val
, char __user
*opt
,
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
);