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 | 0ab43f8 | 2005-08-09 19:43:44 -0700 | [diff] [blame] | 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 | |
Harald Welte | b766b30 | 2005-08-12 11:36:44 -0700 | [diff] [blame] | 32 | #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE) |
Harald Welte | 0ab43f8 | 2005-08-09 19:43:44 -0700 | [diff] [blame] | 33 | |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 34 | /* only for userspace compatibility */ |
| 35 | #ifndef __KERNEL__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* Generic cache responses from hook functions. |
| 37 | <= 0x2000 is used for protocol-flags. */ |
| 38 | #define NFC_UNKNOWN 0x4000 |
| 39 | #define NFC_ALTERED 0x8000 |
Harald Welte | 6869c4d | 2005-08-09 19:24:19 -0700 | [diff] [blame] | 40 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #ifdef __KERNEL__ |
| 43 | #include <linux/config.h> |
| 44 | #ifdef CONFIG_NETFILTER |
| 45 | |
| 46 | extern void netfilter_init(void); |
| 47 | |
| 48 | /* Largest hook number + 1 */ |
| 49 | #define NF_MAX_HOOKS 8 |
| 50 | |
| 51 | struct sk_buff; |
| 52 | struct net_device; |
| 53 | |
| 54 | typedef unsigned int nf_hookfn(unsigned int hooknum, |
| 55 | struct sk_buff **skb, |
| 56 | const struct net_device *in, |
| 57 | const struct net_device *out, |
| 58 | int (*okfn)(struct sk_buff *)); |
| 59 | |
| 60 | struct nf_hook_ops |
| 61 | { |
| 62 | struct list_head list; |
| 63 | |
| 64 | /* User fills in from here down. */ |
| 65 | nf_hookfn *hook; |
| 66 | struct module *owner; |
| 67 | int pf; |
| 68 | int hooknum; |
| 69 | /* Hooks are ordered in ascending priority. */ |
| 70 | int priority; |
| 71 | }; |
| 72 | |
| 73 | struct nf_sockopt_ops |
| 74 | { |
| 75 | struct list_head list; |
| 76 | |
| 77 | int pf; |
| 78 | |
| 79 | /* Non-inclusive ranges: use 0/0/NULL to never get called. */ |
| 80 | int set_optmin; |
| 81 | int set_optmax; |
| 82 | int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 83 | int (*compat_set)(struct sock *sk, int optval, |
| 84 | void __user *user, unsigned int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | int get_optmin; |
| 87 | int get_optmax; |
| 88 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 89 | int (*compat_get)(struct sock *sk, int optval, |
| 90 | void __user *user, int *len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* Number of users inside set() or get(). */ |
| 93 | unsigned int use; |
| 94 | struct task_struct *cleanup_task; |
| 95 | }; |
| 96 | |
| 97 | /* Each queued (to userspace) skbuff has one of these. */ |
| 98 | struct nf_info |
| 99 | { |
| 100 | /* The ops struct which sent us to userspace. */ |
| 101 | struct nf_hook_ops *elem; |
| 102 | |
| 103 | /* If we're sent to userspace, this keeps housekeeping info */ |
| 104 | int pf; |
| 105 | unsigned int hook; |
| 106 | struct net_device *indev, *outdev; |
| 107 | int (*okfn)(struct sk_buff *); |
| 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); |
Patrick McHardy | 972d1cb | 2006-04-06 14:09:12 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS]; |
| 122 | |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 123 | /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will |
| 124 | * disappear once iptables is replaced with pkttables. Please DO NOT use them |
| 125 | * for any new code! */ |
| 126 | #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */ |
| 127 | #define NF_LOG_TCPOPT 0x02 /* Log TCP options */ |
| 128 | #define NF_LOG_IPOPT 0x04 /* Log IP options */ |
| 129 | #define NF_LOG_UID 0x08 /* Log UID owning local socket */ |
| 130 | #define NF_LOG_MASK 0x0f |
| 131 | |
| 132 | #define NF_LOG_TYPE_LOG 0x01 |
| 133 | #define NF_LOG_TYPE_ULOG 0x02 |
| 134 | |
| 135 | struct nf_loginfo { |
| 136 | u_int8_t type; |
| 137 | union { |
| 138 | struct { |
| 139 | u_int32_t copy_len; |
| 140 | u_int16_t group; |
| 141 | u_int16_t qthreshold; |
| 142 | } ulog; |
| 143 | struct { |
| 144 | u_int8_t level; |
| 145 | u_int8_t logflags; |
| 146 | } log; |
| 147 | } u; |
| 148 | }; |
| 149 | |
| 150 | typedef void nf_logfn(unsigned int pf, |
| 151 | unsigned int hooknum, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | const struct sk_buff *skb, |
| 153 | const struct net_device *in, |
| 154 | const struct net_device *out, |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 155 | const struct nf_loginfo *li, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | const char *prefix); |
| 157 | |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 158 | struct nf_logger { |
| 159 | struct module *me; |
| 160 | nf_logfn *logfn; |
| 161 | char *name; |
| 162 | }; |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | /* Function to register/unregister log function. */ |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 165 | int nf_log_register(int pf, struct nf_logger *logger); |
Harald Welte | 8a61fad | 2005-08-09 20:23:53 -0700 | [diff] [blame] | 166 | int nf_log_unregister_pf(int pf); |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 167 | void nf_log_unregister_logger(struct nf_logger *logger); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /* Calls the registered backend logging function */ |
| 170 | void nf_log_packet(int pf, |
| 171 | unsigned int hooknum, |
| 172 | const struct sk_buff *skb, |
| 173 | const struct net_device *in, |
| 174 | const struct net_device *out, |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 175 | struct nf_loginfo *li, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | const char *fmt, ...); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 177 | |
| 178 | int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb, |
| 179 | struct net_device *indev, struct net_device *outdev, |
| 180 | int (*okfn)(struct sk_buff *), int thresh); |
| 181 | |
| 182 | /** |
| 183 | * nf_hook_thresh - call a netfilter hook |
| 184 | * |
| 185 | * Returns 1 if the hook has allowed the packet to pass. The function |
| 186 | * okfn must be invoked by the caller in this case. Any other return |
| 187 | * value indicates the packet has been consumed by the hook. |
| 188 | */ |
| 189 | static inline int nf_hook_thresh(int pf, unsigned int hook, |
| 190 | struct sk_buff **pskb, |
| 191 | struct net_device *indev, |
| 192 | struct net_device *outdev, |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 193 | int (*okfn)(struct sk_buff *), int thresh, |
| 194 | int cond) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 195 | { |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 196 | if (!cond) |
| 197 | return 1; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 198 | #ifndef CONFIG_NETFILTER_DEBUG |
| 199 | if (list_empty(&nf_hooks[pf][hook])) |
| 200 | return 1; |
| 201 | #endif |
| 202 | return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh); |
| 203 | } |
| 204 | |
| 205 | static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb, |
| 206 | struct net_device *indev, struct net_device *outdev, |
| 207 | int (*okfn)(struct sk_buff *)) |
| 208 | { |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 209 | return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | /* Activate hook; either okfn or kfree_skb called, unless a hook |
| 213 | returns NF_STOLEN (in which case, it's up to the hook to deal with |
| 214 | the consequences). |
| 215 | |
| 216 | Returns -ERRNO if packet dropped. Zero means queued, stolen or |
| 217 | accepted. |
| 218 | */ |
| 219 | |
| 220 | /* RR: |
| 221 | > I don't want nf_hook to return anything because people might forget |
| 222 | > about async and trust the return value to mean "packet was ok". |
| 223 | |
| 224 | AK: |
| 225 | Just document it clearly, then you can expect some sense from kernel |
| 226 | coders :) |
| 227 | */ |
| 228 | |
| 229 | /* This is gross, but inline doesn't cut it for avoiding the function |
| 230 | call in fast path: gcc doesn't inline (needs value tracking?). --RR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 232 | /* HX: It's slightly less gross now. */ |
| 233 | |
| 234 | #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \ |
| 235 | ({int __ret; \ |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 236 | if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\ |
| 237 | __ret = (okfn)(skb); \ |
| 238 | __ret;}) |
| 239 | |
| 240 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \ |
| 241 | ({int __ret; \ |
| 242 | if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\ |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 243 | __ret = (okfn)(skb); \ |
| 244 | __ret;}) |
| 245 | |
| 246 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \ |
| 247 | NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /* Call setsockopt() */ |
| 250 | int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, |
| 251 | int len); |
| 252 | int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt, |
| 253 | int *len); |
| 254 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 255 | int compat_nf_setsockopt(struct sock *sk, int pf, int optval, |
| 256 | char __user *opt, int len); |
| 257 | int compat_nf_getsockopt(struct sock *sk, int pf, int optval, |
| 258 | char __user *opt, int *len); |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* Packet queuing */ |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 261 | struct nf_queue_handler { |
| 262 | int (*outfn)(struct sk_buff *skb, struct nf_info *info, |
| 263 | unsigned int queuenum, void *data); |
| 264 | void *data; |
| 265 | char *name; |
| 266 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | extern int nf_register_queue_handler(int pf, |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 268 | struct nf_queue_handler *qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | extern int nf_unregister_queue_handler(int pf); |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 270 | extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | extern void nf_reinject(struct sk_buff *skb, |
| 272 | struct nf_info *info, |
| 273 | unsigned int verdict); |
| 274 | |
| 275 | extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); |
| 276 | extern void nf_ct_attach(struct sk_buff *, struct sk_buff *); |
| 277 | |
| 278 | /* FIXME: Before cache is ever used, this must be implemented for real. */ |
| 279 | extern void nf_invalidate_cache(int pf); |
| 280 | |
Harald Welte | 089af26 | 2005-08-09 19:37:23 -0700 | [diff] [blame] | 281 | /* Call this before modifying an existing packet: ensures it is |
| 282 | modifiable and linear to the point you care about (writable_len). |
| 283 | Returns true or false. */ |
| 284 | extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len); |
| 285 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 286 | struct nf_afinfo { |
| 287 | unsigned short family; |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 288 | unsigned int (*checksum)(struct sk_buff *skb, unsigned int hook, |
| 289 | unsigned int dataoff, u_int8_t protocol); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 290 | void (*saveroute)(const struct sk_buff *skb, |
| 291 | struct nf_info *info); |
| 292 | int (*reroute)(struct sk_buff **skb, |
| 293 | const struct nf_info *info); |
| 294 | int route_key_size; |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 295 | }; |
| 296 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 297 | extern struct nf_afinfo *nf_afinfo[]; |
| 298 | static inline struct nf_afinfo *nf_get_afinfo(unsigned short family) |
| 299 | { |
| 300 | return rcu_dereference(nf_afinfo[family]); |
| 301 | } |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 302 | |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 303 | static inline unsigned int |
| 304 | nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, |
| 305 | u_int8_t protocol, unsigned short family) |
| 306 | { |
| 307 | struct nf_afinfo *afinfo; |
| 308 | unsigned int csum = 0; |
| 309 | |
| 310 | rcu_read_lock(); |
| 311 | afinfo = nf_get_afinfo(family); |
| 312 | if (afinfo) |
| 313 | csum = afinfo->checksum(skb, hook, dataoff, protocol); |
| 314 | rcu_read_unlock(); |
| 315 | return csum; |
| 316 | } |
| 317 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 318 | extern int nf_register_afinfo(struct nf_afinfo *afinfo); |
| 319 | extern void nf_unregister_afinfo(struct nf_afinfo *afinfo); |
| 320 | |
| 321 | #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info)) |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 322 | |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 323 | #include <net/flow.h> |
| 324 | extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *); |
| 325 | |
| 326 | static inline void |
| 327 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) |
| 328 | { |
| 329 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 330 | void (*decodefn)(struct sk_buff *, struct flowi *); |
| 331 | |
| 332 | if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL) |
| 333 | decodefn(skb, fl); |
| 334 | #endif |
| 335 | } |
| 336 | |
Harald Welte | 608c8e4 | 2005-08-09 19:58:27 -0700 | [diff] [blame] | 337 | #ifdef CONFIG_PROC_FS |
| 338 | #include <linux/proc_fs.h> |
| 339 | extern struct proc_dir_entry *proc_net_netfilter; |
| 340 | #endif |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | #else /* !CONFIG_NETFILTER */ |
| 343 | #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 344 | #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb) |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 345 | static inline int nf_hook_thresh(int pf, unsigned int hook, |
| 346 | struct sk_buff **pskb, |
| 347 | struct net_device *indev, |
| 348 | struct net_device *outdev, |
Patrick McHardy | 48d5cad | 2006-02-15 15:10:22 -0800 | [diff] [blame] | 349 | int (*okfn)(struct sk_buff *), int thresh, |
| 350 | int cond) |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 351 | { |
| 352 | return okfn(*pskb); |
| 353 | } |
| 354 | static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb, |
| 355 | struct net_device *indev, struct net_device *outdev, |
| 356 | int (*okfn)(struct sk_buff *)) |
| 357 | { |
Patrick McHardy | 9c92d34 | 2006-02-15 15:18:19 -0800 | [diff] [blame] | 358 | return 1; |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 361 | struct flowi; |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 362 | static inline void |
| 363 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | #endif /*CONFIG_NETFILTER*/ |
| 365 | |
| 366 | #endif /*__KERNEL__*/ |
| 367 | #endif /*__LINUX_NETFILTER_H*/ |