[NETNS][IPV6]: Make mld_max_msf readonly in other namespaces.
[linux-2.6] / include / linux / netfilter.h
1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
3
4 #ifdef __KERNEL__
5 #include <linux/init.h>
6 #include <linux/types.h>
7 #include <linux/skbuff.h>
8 #include <linux/net.h>
9 #include <linux/if.h>
10 #include <linux/wait.h>
11 #include <linux/list.h>
12 #endif
13 #include <linux/compiler.h>
14
15 /* Responses from hook functions. */
16 #define NF_DROP 0
17 #define NF_ACCEPT 1
18 #define NF_STOLEN 2
19 #define NF_QUEUE 3
20 #define NF_REPEAT 4
21 #define NF_STOP 5
22 #define NF_MAX_VERDICT NF_STOP
23
24 /* we overload the higher bits for encoding auxiliary data such as the queue
25  * number. Not nice, but better than additional function arguments. */
26 #define NF_VERDICT_MASK 0x0000ffff
27 #define NF_VERDICT_BITS 16
28
29 #define NF_VERDICT_QMASK 0xffff0000
30 #define NF_VERDICT_QBITS 16
31
32 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
33
34 /* only for userspace compatibility */
35 #ifndef __KERNEL__
36 /* Generic cache responses from hook functions.
37    <= 0x2000 is used for protocol-flags. */
38 #define NFC_UNKNOWN 0x4000
39 #define NFC_ALTERED 0x8000
40 #endif
41
42 enum nf_inet_hooks {
43         NF_INET_PRE_ROUTING,
44         NF_INET_LOCAL_IN,
45         NF_INET_FORWARD,
46         NF_INET_LOCAL_OUT,
47         NF_INET_POST_ROUTING,
48         NF_INET_NUMHOOKS
49 };
50
51 union nf_inet_addr {
52         u_int32_t       all[4];
53         __be32          ip;
54         __be32          ip6[4];
55 };
56
57 #ifdef __KERNEL__
58 #ifdef CONFIG_NETFILTER
59
60 extern void netfilter_init(void);
61
62 /* Largest hook number + 1 */
63 #define NF_MAX_HOOKS 8
64
65 struct sk_buff;
66 struct net_device;
67
68 typedef unsigned int nf_hookfn(unsigned int hooknum,
69                                struct sk_buff *skb,
70                                const struct net_device *in,
71                                const struct net_device *out,
72                                int (*okfn)(struct sk_buff *));
73
74 struct nf_hook_ops
75 {
76         struct list_head list;
77
78         /* User fills in from here down. */
79         nf_hookfn *hook;
80         struct module *owner;
81         int pf;
82         int hooknum;
83         /* Hooks are ordered in ascending priority. */
84         int priority;
85 };
86
87 struct nf_sockopt_ops
88 {
89         struct list_head list;
90
91         int pf;
92
93         /* Non-inclusive ranges: use 0/0/NULL to never get called. */
94         int set_optmin;
95         int set_optmax;
96         int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
97         int (*compat_set)(struct sock *sk, int optval,
98                         void __user *user, unsigned int len);
99
100         int get_optmin;
101         int get_optmax;
102         int (*get)(struct sock *sk, int optval, void __user *user, int *len);
103         int (*compat_get)(struct sock *sk, int optval,
104                         void __user *user, int *len);
105
106         /* Use the module struct to lock set/get code in place */
107         struct module *owner;
108 };
109
110 /* Function to register/unregister hook points. */
111 int nf_register_hook(struct nf_hook_ops *reg);
112 void nf_unregister_hook(struct nf_hook_ops *reg);
113 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
114 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
115
116 /* Functions to register get/setsockopt ranges (non-inclusive).  You
117    need to check permissions yourself! */
118 int nf_register_sockopt(struct nf_sockopt_ops *reg);
119 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
120
121 #ifdef CONFIG_SYSCTL
122 /* Sysctl registration */
123 extern struct ctl_path nf_net_netfilter_sysctl_path[];
124 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
125 #endif /* CONFIG_SYSCTL */
126
127 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
128
129 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
130                  struct net_device *indev, struct net_device *outdev,
131                  int (*okfn)(struct sk_buff *), int thresh);
132
133 /**
134  *      nf_hook_thresh - call a netfilter hook
135  *      
136  *      Returns 1 if the hook has allowed the packet to pass.  The function
137  *      okfn must be invoked by the caller in this case.  Any other return
138  *      value indicates the packet has been consumed by the hook.
139  */
140 static inline int nf_hook_thresh(int pf, unsigned int hook,
141                                  struct sk_buff *skb,
142                                  struct net_device *indev,
143                                  struct net_device *outdev,
144                                  int (*okfn)(struct sk_buff *), int thresh,
145                                  int cond)
146 {
147         if (!cond)
148                 return 1;
149 #ifndef CONFIG_NETFILTER_DEBUG
150         if (list_empty(&nf_hooks[pf][hook]))
151                 return 1;
152 #endif
153         return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
154 }
155
156 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
157                           struct net_device *indev, struct net_device *outdev,
158                           int (*okfn)(struct sk_buff *))
159 {
160         return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
161 }
162                    
163 /* Activate hook; either okfn or kfree_skb called, unless a hook
164    returns NF_STOLEN (in which case, it's up to the hook to deal with
165    the consequences).
166
167    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
168    accepted.
169 */
170
171 /* RR:
172    > I don't want nf_hook to return anything because people might forget
173    > about async and trust the return value to mean "packet was ok".
174
175    AK:
176    Just document it clearly, then you can expect some sense from kernel
177    coders :)
178 */
179
180 /* This is gross, but inline doesn't cut it for avoiding the function
181    call in fast path: gcc doesn't inline (needs value tracking?). --RR */
182
183 /* HX: It's slightly less gross now. */
184
185 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh)             \
186 ({int __ret;                                                                   \
187 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
188         __ret = (okfn)(skb);                                                   \
189 __ret;})
190
191 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond)                 \
192 ({int __ret;                                                                   \
193 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
194         __ret = (okfn)(skb);                                                   \
195 __ret;})
196
197 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
198         NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
199
200 /* Call setsockopt() */
201 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, 
202                   int len);
203 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
204                   int *len);
205
206 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
207                 char __user *opt, int len);
208 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
209                 char __user *opt, int *len);
210
211 /* Call this before modifying an existing packet: ensures it is
212    modifiable and linear to the point you care about (writable_len).
213    Returns true or false. */
214 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
215
216 struct flowi;
217 struct nf_queue_entry;
218
219 struct nf_afinfo {
220         unsigned short  family;
221         __sum16         (*checksum)(struct sk_buff *skb, unsigned int hook,
222                                     unsigned int dataoff, u_int8_t protocol);
223         int             (*route)(struct dst_entry **dst, struct flowi *fl);
224         void            (*saveroute)(const struct sk_buff *skb,
225                                      struct nf_queue_entry *entry);
226         int             (*reroute)(struct sk_buff *skb,
227                                    const struct nf_queue_entry *entry);
228         int             route_key_size;
229 };
230
231 extern const struct nf_afinfo *nf_afinfo[NPROTO];
232 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
233 {
234         return rcu_dereference(nf_afinfo[family]);
235 }
236
237 static inline __sum16
238 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
239             u_int8_t protocol, unsigned short family)
240 {
241         const struct nf_afinfo *afinfo;
242         __sum16 csum = 0;
243
244         rcu_read_lock();
245         afinfo = nf_get_afinfo(family);
246         if (afinfo)
247                 csum = afinfo->checksum(skb, hook, dataoff, protocol);
248         rcu_read_unlock();
249         return csum;
250 }
251
252 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
253 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
254
255 #include <net/flow.h>
256 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
257
258 static inline void
259 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
260 {
261 #ifdef CONFIG_NF_NAT_NEEDED
262         void (*decodefn)(struct sk_buff *, struct flowi *);
263
264         if (family == AF_INET) {
265                 rcu_read_lock();
266                 decodefn = rcu_dereference(ip_nat_decode_session);
267                 if (decodefn)
268                         decodefn(skb, fl);
269                 rcu_read_unlock();
270         }
271 #endif
272 }
273
274 #ifdef CONFIG_PROC_FS
275 #include <linux/proc_fs.h>
276 extern struct proc_dir_entry *proc_net_netfilter;
277 #endif
278
279 #else /* !CONFIG_NETFILTER */
280 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
281 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
282 static inline int nf_hook_thresh(int pf, unsigned int hook,
283                                  struct sk_buff *skb,
284                                  struct net_device *indev,
285                                  struct net_device *outdev,
286                                  int (*okfn)(struct sk_buff *), int thresh,
287                                  int cond)
288 {
289         return okfn(skb);
290 }
291 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
292                           struct net_device *indev, struct net_device *outdev,
293                           int (*okfn)(struct sk_buff *))
294 {
295         return 1;
296 }
297 struct flowi;
298 static inline void
299 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
300 #endif /*CONFIG_NETFILTER*/
301
302 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
303 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
304 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
305 extern void (*nf_ct_destroy)(struct nf_conntrack *);
306 #else
307 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
308 #endif
309
310 #endif /*__KERNEL__*/
311 #endif /*__LINUX_NETFILTER_H*/