blob: f042ae521557b340be5252aa41ccdc67f5202a29 [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
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -080068 if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
69 return ERR_PTR(-EINTR);
70
71 list_for_each_entry(ops, &nf_sockopts, list) {
72 if (ops->pf == pf) {
73 if (!try_module_get(ops->owner))
74 goto out_nosup;
75
76 if (get) {
77 if (val >= ops->get_optmin &&
78 val < ops->get_optmax)
79 goto out;
80 } else {
81 if (val >= ops->set_optmin &&
82 val < ops->set_optmax)
83 goto out;
84 }
85 module_put(ops->owner);
86 }
87 }
88out_nosup:
89 ops = ERR_PTR(-ENOPROTOOPT);
90out:
91 mutex_unlock(&nf_sockopt_mutex);
92 return ops;
93}
94
Harald Weltef6ebe772005-08-09 20:21:49 -070095/* Call get/setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +020096static int nf_sockopt(struct sock *sk, u_int8_t pf, int val,
Harald Weltef6ebe772005-08-09 20:21:49 -070097 char __user *opt, int *len, int get)
98{
Harald Weltef6ebe772005-08-09 20:21:49 -070099 struct nf_sockopt_ops *ops;
100 int ret;
101
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800102 ops = nf_sockopt_find(sk, pf, val, get);
103 if (IS_ERR(ops))
104 return PTR_ERR(ops);
Eric W. Biedermanc48dad72007-09-12 13:58:02 +0200105
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800106 if (get)
107 ret = ops->get(sk, val, opt, len);
108 else
109 ret = ops->set(sk, val, opt, *len);
Harald Weltef6ebe772005-08-09 20:21:49 -0700110
Neil Horman16fcec32007-09-11 11:28:26 +0200111 module_put(ops->owner);
Harald Weltef6ebe772005-08-09 20:21:49 -0700112 return ret;
113}
114
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200115int nf_setsockopt(struct sock *sk, u_int8_t pf, int val, char __user *opt,
David S. Millerb7058842009-09-30 16:12:20 -0700116 unsigned int len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700117{
118 return nf_sockopt(sk, pf, val, opt, &len, 0);
119}
120EXPORT_SYMBOL(nf_setsockopt);
121
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200122int nf_getsockopt(struct sock *sk, u_int8_t pf, int val, char __user *opt,
123 int *len)
Harald Weltef6ebe772005-08-09 20:21:49 -0700124{
125 return nf_sockopt(sk, pf, val, opt, len, 1);
126}
127EXPORT_SYMBOL(nf_getsockopt);
128
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800129#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200130static int compat_nf_sockopt(struct sock *sk, u_int8_t pf, int val,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800131 char __user *opt, int *len, int get)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800132{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800133 struct nf_sockopt_ops *ops;
134 int ret;
135
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800136 ops = nf_sockopt_find(sk, pf, val, get);
137 if (IS_ERR(ops))
138 return PTR_ERR(ops);
Eric W. Biedermanc48dad72007-09-12 13:58:02 +0200139
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800140 if (get) {
141 if (ops->compat_get)
142 ret = ops->compat_get(sk, val, opt, len);
143 else
Patrick McHardy6452a5f2007-11-15 14:29:21 -0800144 ret = ops->get(sk, val, opt, len);
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800145 } else {
146 if (ops->compat_set)
Patrick McHardy6452a5f2007-11-15 14:29:21 -0800147 ret = ops->compat_set(sk, val, opt, *len);
Pavel Emelyanov4ce5ba62007-11-13 02:58:09 -0800148 else
Patrick McHardy6452a5f2007-11-15 14:29:21 -0800149 ret = ops->set(sk, val, opt, *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800150 }
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800151
Neil Horman16fcec32007-09-11 11:28:26 +0200152 module_put(ops->owner);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800153 return ret;
154}
155
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200156int compat_nf_setsockopt(struct sock *sk, u_int8_t pf,
David S. Millerb7058842009-09-30 16:12:20 -0700157 int val, char __user *opt, unsigned int len)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800158{
159 return compat_nf_sockopt(sk, pf, val, opt, &len, 0);
160}
161EXPORT_SYMBOL(compat_nf_setsockopt);
162
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200163int compat_nf_getsockopt(struct sock *sk, u_int8_t pf,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800164 int val, char __user *opt, int *len)
165{
166 return compat_nf_sockopt(sk, pf, val, opt, len, 1);
167}
168EXPORT_SYMBOL(compat_nf_getsockopt);
169#endif