blob: f9b46de6a3db90e428610c9174105b4554bec249 [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/module.h>
4#include <linux/skbuff.h>
5#include <linux/netfilter.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08006#include <linux/mutex.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07007#include <net/sock.h>
8
9#include "nf_internals.h"
10
11/* Sockopts only registered and called from user context, so
12 net locking would be overkill. Also, [gs]etsockopt calls may
13 sleep. */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080014static DEFINE_MUTEX(nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070015static LIST_HEAD(nf_sockopts);
16
17/* Do exclusive ranges overlap? */
18static inline int overlap(int min1, int max1, int min2, int max2)
19{
20 return max1 > min2 && min1 < max2;
21}
22
23/* Functions to register sockopt ranges (exclusive). */
24int nf_register_sockopt(struct nf_sockopt_ops *reg)
25{
Alexey Dobriyan55d84ac2007-11-05 20:44:06 -080026 struct nf_sockopt_ops *ops;
Harald Weltef6ebe772005-08-09 20:21:49 -070027 int ret = 0;
28
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080029 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
Harald Weltef6ebe772005-08-09 20:21:49 -070030 return -EINTR;
31
Alexey Dobriyan55d84ac2007-11-05 20:44:06 -080032 list_for_each_entry(ops, &nf_sockopts, list) {
Harald Weltef6ebe772005-08-09 20:21:49 -070033 if (ops->pf == reg->pf
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080034 && (overlap(ops->set_optmin, ops->set_optmax,
Harald Weltef6ebe772005-08-09 20:21:49 -070035 reg->set_optmin, reg->set_optmax)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080036 || overlap(ops->get_optmin, ops->get_optmax,
Harald Weltef6ebe772005-08-09 20:21:49 -070037 reg->get_optmin, reg->get_optmax))) {
38 NFDEBUG("nf_sock overlap: %u-%u/%u-%u v %u-%u/%u-%u\n",
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080039 ops->set_optmin, ops->set_optmax,
40 ops->get_optmin, ops->get_optmax,
Harald Weltef6ebe772005-08-09 20:21:49 -070041 reg->set_optmin, reg->set_optmax,
42 reg->get_optmin, reg->get_optmax);
43 ret = -EBUSY;
44 goto out;
45 }
46 }
47
48 list_add(&reg->list, &nf_sockopts);
49out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080050 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070051 return ret;
52}
53EXPORT_SYMBOL(nf_register_sockopt);
54
55void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
56{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080057 mutex_lock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070058 list_del(&reg->list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080059 mutex_unlock(&nf_sockopt_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070060}
61EXPORT_SYMBOL(nf_unregister_sockopt);
62
Jan Engelhardt76108ce2008-10-08 11:35:00 +020063static struct nf_sockopt_ops *nf_sockopt_find(struct sock *sk, u_int8_t pf,
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -080064 int val, int get)
65{
66 struct nf_sockopt_ops *ops;
67
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -070068 if (!net_eq(sock_net(sk), &init_net))
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -080069 return ERR_PTR(-ENOPROTOOPT);
70
71 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
72 return ERR_PTR(-EINTR);
73
74 list_for_each_entry(ops, &nf_sockopts, list) {
75 if (ops->pf == pf) {
76 if (!try_module_get(ops->owner))
77 goto out_nosup;
78
79 if (get) {
80 if (val >= ops->get_optmin &&
81 val < ops->get_optmax)
82 goto out;
83 } else {
84 if (val >= ops->set_optmin &&
85 val < ops->set_optmax)
86 goto out;
87 }
88 module_put(ops->owner);
89 }
90 }
91out_nosup:
92 ops = ERR_PTR(-ENOPROTOOPT);
93out:
94 mutex_unlock(&nf_sockopt_mutex);
95 return ops;
96}
97
Harald Weltef6ebe772005-08-09 20:21:49 -070098/* Call get/setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +020099static int nf_sockopt(struct sock *sk, u_int8_t pf, int val,
Harald Weltef6ebe772005-08-09 20:21:49 -0700100 char __user *opt, int *len, int get)
101{
Harald Weltef6ebe772005-08-09 20:21:49 -0700102 struct nf_sockopt_ops *ops;
103 int ret;
104
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800105 ops = nf_sockopt_find(sk, pf, val, get);
106 if (IS_ERR(ops))
107 return PTR_ERR(ops);
Eric W. Biedermanc48dad72007-09-12 13:58:02 +0200108
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800109 if (get)
110 ret = ops->get(sk, val, opt, len);
111 else
112 ret = ops->set(sk, val, opt, *len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700113
Neil Horman16fcec32007-09-11 11:28:26 +0200114 module_put(ops->owner);
Harald Weltef6ebe772005-08-09 20:21:49 -0700115 return ret;
116}
117
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200118int nf_setsockopt(struct sock *sk, u_int8_t pf, int val, char __user *opt,
Harald Weltef6ebe772005-08-09 20:21:49 -0700119 int len)
120{
121 return nf_sockopt(sk, pf, val, opt, &len, 0);
122}
123EXPORT_SYMBOL(nf_setsockopt);
124
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200125int nf_getsockopt(struct sock *sk, u_int8_t pf, int val, char __user *opt,
126 int *len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700127{
128 return nf_sockopt(sk, pf, val, opt, len, 1);
129}
130EXPORT_SYMBOL(nf_getsockopt);
131
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800132#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200133static int compat_nf_sockopt(struct sock *sk, u_int8_t pf, int val,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800134 char __user *opt, int *len, int get)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800135{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800136 struct nf_sockopt_ops *ops;
137 int ret;
138
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800139 ops = nf_sockopt_find(sk, pf, val, get);
140 if (IS_ERR(ops))
141 return PTR_ERR(ops);
Eric W. Biedermanc48dad72007-09-12 13:58:02 +0200142
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800143 if (get) {
144 if (ops->compat_get)
145 ret = ops->compat_get(sk, val, opt, len);
146 else
Patrick McHardy6452a5f2007-11-15 14:29:21 -0800147 ret = ops->get(sk, val, opt, len);
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800148 } else {
149 if (ops->compat_set)
Patrick McHardy6452a5f2007-11-15 14:29:21 -0800150 ret = ops->compat_set(sk, val, opt, *len);
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800151 else
Patrick McHardy6452a5f2007-11-15 14:29:21 -0800152 ret = ops->set(sk, val, opt, *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800153 }
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800154
Neil Horman16fcec32007-09-11 11:28:26 +0200155 module_put(ops->owner);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800156 return ret;
157}
158
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200159int compat_nf_setsockopt(struct sock *sk, u_int8_t pf,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800160 int val, char __user *opt, int len)
161{
162 return compat_nf_sockopt(sk, pf, val, opt, &len, 0);
163}
164EXPORT_SYMBOL(compat_nf_setsockopt);
165
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200166int compat_nf_getsockopt(struct sock *sk, u_int8_t pf,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800167 int val, char __user *opt, int *len)
168{
169 return compat_nf_sockopt(sk, pf, val, opt, len, 1);
170}
171EXPORT_SYMBOL(compat_nf_getsockopt);
172#endif