Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 24 | /* only for userspace compatibility */ |
| 25 | #ifndef __KERNEL__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* Generic cache responses from hook functions. |
| 27 | <= 0x2000 is used for protocol-flags. */ |
| 28 | #define NFC_UNKNOWN 0x4000 |
| 29 | #define NFC_ALTERED 0x8000 |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 30 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #ifdef __KERNEL__ |
| 33 | #include <linux/config.h> |
| 34 | #ifdef CONFIG_NETFILTER |
| 35 | |
| 36 | extern void netfilter_init(void); |
| 37 | |
| 38 | /* Largest hook number + 1 */ |
| 39 | #define NF_MAX_HOOKS 8 |
| 40 | |
| 41 | struct sk_buff; |
| 42 | struct net_device; |
| 43 | |
| 44 | typedef unsigned int nf_hookfn(unsigned int hooknum, |
| 45 | struct sk_buff **skb, |
| 46 | const struct net_device *in, |
| 47 | const struct net_device *out, |
| 48 | int (*okfn)(struct sk_buff *)); |
| 49 | |
| 50 | struct nf_hook_ops |
| 51 | { |
| 52 | struct list_head list; |
| 53 | |
| 54 | /* User fills in from here down. */ |
| 55 | nf_hookfn *hook; |
| 56 | struct module *owner; |
| 57 | int pf; |
| 58 | int hooknum; |
| 59 | /* Hooks are ordered in ascending priority. */ |
| 60 | int priority; |
| 61 | }; |
| 62 | |
| 63 | struct nf_sockopt_ops |
| 64 | { |
| 65 | struct list_head list; |
| 66 | |
| 67 | int pf; |
| 68 | |
| 69 | /* Non-inclusive ranges: use 0/0/NULL to never get called. */ |
| 70 | int set_optmin; |
| 71 | int set_optmax; |
| 72 | int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); |
| 73 | |
| 74 | int get_optmin; |
| 75 | int get_optmax; |
| 76 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); |
| 77 | |
| 78 | /* Number of users inside set() or get(). */ |
| 79 | unsigned int use; |
| 80 | struct task_struct *cleanup_task; |
| 81 | }; |
| 82 | |
| 83 | /* Each queued (to userspace) skbuff has one of these. */ |
| 84 | struct nf_info |
| 85 | { |
| 86 | /* The ops struct which sent us to userspace. */ |
| 87 | struct nf_hook_ops *elem; |
| 88 | |
| 89 | /* If we're sent to userspace, this keeps housekeeping info */ |
| 90 | int pf; |
| 91 | unsigned int hook; |
| 92 | struct net_device *indev, *outdev; |
| 93 | int (*okfn)(struct sk_buff *); |
| 94 | }; |
| 95 | |
| 96 | /* Function to register/unregister hook points. */ |
| 97 | int nf_register_hook(struct nf_hook_ops *reg); |
| 98 | void nf_unregister_hook(struct nf_hook_ops *reg); |
| 99 | |
| 100 | /* Functions to register get/setsockopt ranges (non-inclusive). You |
| 101 | need to check permissions yourself! */ |
| 102 | int nf_register_sockopt(struct nf_sockopt_ops *reg); |
| 103 | void nf_unregister_sockopt(struct nf_sockopt_ops *reg); |
| 104 | |
| 105 | extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS]; |
| 106 | |
| 107 | typedef void nf_logfn(unsigned int hooknum, |
| 108 | const struct sk_buff *skb, |
| 109 | const struct net_device *in, |
| 110 | const struct net_device *out, |
| 111 | const char *prefix); |
| 112 | |
| 113 | /* Function to register/unregister log function. */ |
| 114 | int nf_log_register(int pf, nf_logfn *logfn); |
| 115 | void nf_log_unregister(int pf, nf_logfn *logfn); |
| 116 | |
| 117 | /* Calls the registered backend logging function */ |
| 118 | void nf_log_packet(int pf, |
| 119 | unsigned int hooknum, |
| 120 | const struct sk_buff *skb, |
| 121 | const struct net_device *in, |
| 122 | const struct net_device *out, |
| 123 | const char *fmt, ...); |
| 124 | |
| 125 | /* Activate hook; either okfn or kfree_skb called, unless a hook |
| 126 | returns NF_STOLEN (in which case, it's up to the hook to deal with |
| 127 | the consequences). |
| 128 | |
| 129 | Returns -ERRNO if packet dropped. Zero means queued, stolen or |
| 130 | accepted. |
| 131 | */ |
| 132 | |
| 133 | /* RR: |
| 134 | > I don't want nf_hook to return anything because people might forget |
| 135 | > about async and trust the return value to mean "packet was ok". |
| 136 | |
| 137 | AK: |
| 138 | Just document it clearly, then you can expect some sense from kernel |
| 139 | coders :) |
| 140 | */ |
| 141 | |
| 142 | /* This is gross, but inline doesn't cut it for avoiding the function |
| 143 | call in fast path: gcc doesn't inline (needs value tracking?). --RR */ |
| 144 | #ifdef CONFIG_NETFILTER_DEBUG |
| 145 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \ |
| 146 | ({int __ret; \ |
| 147 | if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \ |
| 148 | __ret = (okfn)(skb); \ |
| 149 | __ret;}) |
| 150 | #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \ |
| 151 | ({int __ret; \ |
| 152 | if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \ |
| 153 | __ret = (okfn)(skb); \ |
| 154 | __ret;}) |
| 155 | #else |
| 156 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \ |
| 157 | ({int __ret; \ |
| 158 | if (list_empty(&nf_hooks[pf][hook]) || \ |
| 159 | (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \ |
| 160 | __ret = (okfn)(skb); \ |
| 161 | __ret;}) |
| 162 | #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \ |
| 163 | ({int __ret; \ |
| 164 | if (list_empty(&nf_hooks[pf][hook]) || \ |
| 165 | (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \ |
| 166 | __ret = (okfn)(skb); \ |
| 167 | __ret;}) |
| 168 | #endif |
| 169 | |
| 170 | int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb, |
| 171 | struct net_device *indev, struct net_device *outdev, |
| 172 | int (*okfn)(struct sk_buff *), int thresh); |
| 173 | |
| 174 | /* Call setsockopt() */ |
| 175 | int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, |
| 176 | int len); |
| 177 | int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt, |
| 178 | int *len); |
| 179 | |
| 180 | /* Packet queuing */ |
| 181 | typedef int (*nf_queue_outfn_t)(struct sk_buff *skb, |
| 182 | struct nf_info *info, void *data); |
| 183 | extern int nf_register_queue_handler(int pf, |
| 184 | nf_queue_outfn_t outfn, void *data); |
| 185 | extern int nf_unregister_queue_handler(int pf); |
| 186 | extern void nf_reinject(struct sk_buff *skb, |
| 187 | struct nf_info *info, |
| 188 | unsigned int verdict); |
| 189 | |
| 190 | extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); |
| 191 | extern void nf_ct_attach(struct sk_buff *, struct sk_buff *); |
| 192 | |
| 193 | /* FIXME: Before cache is ever used, this must be implemented for real. */ |
| 194 | extern void nf_invalidate_cache(int pf); |
| 195 | |
Harald Welte | 089af26 | 2005-08-09 19:37:23 -0700 | [diff] [blame] | 196 | /* Call this before modifying an existing packet: ensures it is |
| 197 | modifiable and linear to the point you care about (writable_len). |
| 198 | Returns true or false. */ |
| 199 | extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len); |
| 200 | |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame^] | 201 | struct nf_queue_rerouter { |
| 202 | void (*save)(const struct sk_buff *skb, struct nf_info *info); |
| 203 | int (*reroute)(struct sk_buff **skb, const struct nf_info *info); |
| 204 | int rer_size; |
| 205 | }; |
| 206 | |
| 207 | #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info)) |
| 208 | |
| 209 | extern int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer); |
| 210 | extern int nf_unregister_queue_rerouter(int pf); |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | #else /* !CONFIG_NETFILTER */ |
| 213 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) |
| 214 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} |
| 215 | #endif /*CONFIG_NETFILTER*/ |
| 216 | |
| 217 | #endif /*__KERNEL__*/ |
| 218 | #endif /*__LINUX_NETFILTER_H*/ |