blob: 0e5c5e2047995bf03c422ef68f4233b38154220d [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001#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>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08007#include <linux/mutex.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07008#include <net/sock.h>
9
10#include "nf_internals.h"
11
12/* Sockopts only registered and called from user context, so
13 net locking would be overkill. Also, [gs]etsockopt calls may
14 sleep. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080015static DEFINE_MUTEX(nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070016static LIST_HEAD(nf_sockopts);
17
18/* Do exclusive ranges overlap? */
19static inline int overlap(int min1, int max1, int min2, int max2)
20{
21 return max1 > min2 && min1 < max2;
22}
23
24/* Functions to register sockopt ranges (exclusive). */
25int nf_register_sockopt(struct nf_sockopt_ops *reg)
26{
27 struct list_head *i;
28 int ret = 0;
29
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080030 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
Harald Weltef6ebe772005-08-09 20:21:49 -070031 return -EINTR;
32
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);
45 ret = -EBUSY;
46 goto out;
47 }
48 }
49
50 list_add(&reg->list, &nf_sockopts);
51out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080052 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070053 return ret;
54}
55EXPORT_SYMBOL(nf_register_sockopt);
56
57void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
58{
59 /* No point being interruptible: we're probably in cleanup_module() */
60 restart:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080061 mutex_lock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070062 if (reg->use != 0) {
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;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080067 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070068 schedule();
69 goto restart;
70 }
71 list_del(&reg->list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080072 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070073}
74EXPORT_SYMBOL(nf_unregister_sockopt);
75
76/* Call get/setsockopt() */
77static int nf_sockopt(struct sock *sk, int pf, int val,
78 char __user *opt, int *len, int get)
79{
80 struct list_head *i;
81 struct nf_sockopt_ops *ops;
82 int ret;
83
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080084 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
Harald Weltef6ebe772005-08-09 20:21:49 -070085 return -EINTR;
86
87 list_for_each(i, &nf_sockopts) {
88 ops = (struct nf_sockopt_ops *)i;
89 if (ops->pf == pf) {
90 if (get) {
91 if (val >= ops->get_optmin
92 && val < ops->get_optmax) {
93 ops->use++;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080094 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070095 ret = ops->get(sk, val, opt, len);
96 goto out;
97 }
98 } else {
99 if (val >= ops->set_optmin
100 && val < ops->set_optmax) {
101 ops->use++;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800102 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700103 ret = ops->set(sk, val, opt, *len);
104 goto out;
105 }
106 }
107 }
108 }
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800109 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700110 return -ENOPROTOOPT;
111
112 out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800113 mutex_lock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700114 ops->use--;
115 if (ops->cleanup_task)
116 wake_up_process(ops->cleanup_task);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800117 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -0700118 return ret;
119}
120
121int nf_setsockopt(struct sock *sk, int pf, int val, char __user *opt,
122 int len)
123{
124 return nf_sockopt(sk, pf, val, opt, &len, 0);
125}
126EXPORT_SYMBOL(nf_setsockopt);
127
128int nf_getsockopt(struct sock *sk, int pf, int val, char __user *opt, int *len)
129{
130 return nf_sockopt(sk, pf, val, opt, len, 1);
131}
132EXPORT_SYMBOL(nf_getsockopt);
133