at v2.6.16 9.7 kB view raw
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#ifdef __KERNEL__ 43#include <linux/config.h> 44#ifdef CONFIG_NETFILTER 45 46extern void netfilter_init(void); 47 48/* Largest hook number + 1 */ 49#define NF_MAX_HOOKS 8 50 51struct sk_buff; 52struct net_device; 53 54typedef 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 60struct 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 73struct 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); 83 84 int get_optmin; 85 int get_optmax; 86 int (*get)(struct sock *sk, int optval, void __user *user, int *len); 87 88 /* Number of users inside set() or get(). */ 89 unsigned int use; 90 struct task_struct *cleanup_task; 91}; 92 93/* Each queued (to userspace) skbuff has one of these. */ 94struct nf_info 95{ 96 /* The ops struct which sent us to userspace. */ 97 struct nf_hook_ops *elem; 98 99 /* If we're sent to userspace, this keeps housekeeping info */ 100 int pf; 101 unsigned int hook; 102 struct net_device *indev, *outdev; 103 int (*okfn)(struct sk_buff *); 104}; 105 106/* Function to register/unregister hook points. */ 107int nf_register_hook(struct nf_hook_ops *reg); 108void nf_unregister_hook(struct nf_hook_ops *reg); 109 110/* Functions to register get/setsockopt ranges (non-inclusive). You 111 need to check permissions yourself! */ 112int nf_register_sockopt(struct nf_sockopt_ops *reg); 113void nf_unregister_sockopt(struct nf_sockopt_ops *reg); 114 115extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS]; 116 117/* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will 118 * disappear once iptables is replaced with pkttables. Please DO NOT use them 119 * for any new code! */ 120#define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */ 121#define NF_LOG_TCPOPT 0x02 /* Log TCP options */ 122#define NF_LOG_IPOPT 0x04 /* Log IP options */ 123#define NF_LOG_UID 0x08 /* Log UID owning local socket */ 124#define NF_LOG_MASK 0x0f 125 126#define NF_LOG_TYPE_LOG 0x01 127#define NF_LOG_TYPE_ULOG 0x02 128 129struct nf_loginfo { 130 u_int8_t type; 131 union { 132 struct { 133 u_int32_t copy_len; 134 u_int16_t group; 135 u_int16_t qthreshold; 136 } ulog; 137 struct { 138 u_int8_t level; 139 u_int8_t logflags; 140 } log; 141 } u; 142}; 143 144typedef void nf_logfn(unsigned int pf, 145 unsigned int hooknum, 146 const struct sk_buff *skb, 147 const struct net_device *in, 148 const struct net_device *out, 149 const struct nf_loginfo *li, 150 const char *prefix); 151 152struct nf_logger { 153 struct module *me; 154 nf_logfn *logfn; 155 char *name; 156}; 157 158/* Function to register/unregister log function. */ 159int nf_log_register(int pf, struct nf_logger *logger); 160int nf_log_unregister_pf(int pf); 161void nf_log_unregister_logger(struct nf_logger *logger); 162 163/* Calls the registered backend logging function */ 164void nf_log_packet(int pf, 165 unsigned int hooknum, 166 const struct sk_buff *skb, 167 const struct net_device *in, 168 const struct net_device *out, 169 struct nf_loginfo *li, 170 const char *fmt, ...); 171 172int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb, 173 struct net_device *indev, struct net_device *outdev, 174 int (*okfn)(struct sk_buff *), int thresh); 175 176/** 177 * nf_hook_thresh - call a netfilter hook 178 * 179 * Returns 1 if the hook has allowed the packet to pass. The function 180 * okfn must be invoked by the caller in this case. Any other return 181 * value indicates the packet has been consumed by the hook. 182 */ 183static inline int nf_hook_thresh(int pf, unsigned int hook, 184 struct sk_buff **pskb, 185 struct net_device *indev, 186 struct net_device *outdev, 187 int (*okfn)(struct sk_buff *), int thresh, 188 int cond) 189{ 190 if (!cond) 191 return 1; 192#ifndef CONFIG_NETFILTER_DEBUG 193 if (list_empty(&nf_hooks[pf][hook])) 194 return 1; 195#endif 196 return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh); 197} 198 199static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb, 200 struct net_device *indev, struct net_device *outdev, 201 int (*okfn)(struct sk_buff *)) 202{ 203 return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1); 204} 205 206/* Activate hook; either okfn or kfree_skb called, unless a hook 207 returns NF_STOLEN (in which case, it's up to the hook to deal with 208 the consequences). 209 210 Returns -ERRNO if packet dropped. Zero means queued, stolen or 211 accepted. 212*/ 213 214/* RR: 215 > I don't want nf_hook to return anything because people might forget 216 > about async and trust the return value to mean "packet was ok". 217 218 AK: 219 Just document it clearly, then you can expect some sense from kernel 220 coders :) 221*/ 222 223/* This is gross, but inline doesn't cut it for avoiding the function 224 call in fast path: gcc doesn't inline (needs value tracking?). --RR */ 225 226/* HX: It's slightly less gross now. */ 227 228#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \ 229({int __ret; \ 230if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\ 231 __ret = (okfn)(skb); \ 232__ret;}) 233 234#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \ 235({int __ret; \ 236if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\ 237 __ret = (okfn)(skb); \ 238__ret;}) 239 240#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \ 241 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN) 242 243/* Call setsockopt() */ 244int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt, 245 int len); 246int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt, 247 int *len); 248 249/* Packet queuing */ 250struct nf_queue_handler { 251 int (*outfn)(struct sk_buff *skb, struct nf_info *info, 252 unsigned int queuenum, void *data); 253 void *data; 254 char *name; 255}; 256extern int nf_register_queue_handler(int pf, 257 struct nf_queue_handler *qh); 258extern int nf_unregister_queue_handler(int pf); 259extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh); 260extern void nf_reinject(struct sk_buff *skb, 261 struct nf_info *info, 262 unsigned int verdict); 263 264extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *); 265extern void nf_ct_attach(struct sk_buff *, struct sk_buff *); 266 267/* FIXME: Before cache is ever used, this must be implemented for real. */ 268extern void nf_invalidate_cache(int pf); 269 270/* Call this before modifying an existing packet: ensures it is 271 modifiable and linear to the point you care about (writable_len). 272 Returns true or false. */ 273extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len); 274 275struct nf_queue_rerouter { 276 void (*save)(const struct sk_buff *skb, struct nf_info *info); 277 int (*reroute)(struct sk_buff **skb, const struct nf_info *info); 278 int rer_size; 279}; 280 281#define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info)) 282 283extern int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer); 284extern int nf_unregister_queue_rerouter(int pf); 285 286#include <net/flow.h> 287extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *); 288 289static inline void 290nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) 291{ 292#ifdef CONFIG_IP_NF_NAT_NEEDED 293 void (*decodefn)(struct sk_buff *, struct flowi *); 294 295 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL) 296 decodefn(skb, fl); 297#endif 298} 299 300#ifdef CONFIG_PROC_FS 301#include <linux/proc_fs.h> 302extern struct proc_dir_entry *proc_net_netfilter; 303#endif 304 305#else /* !CONFIG_NETFILTER */ 306#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb) 307#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb) 308static inline int nf_hook_thresh(int pf, unsigned int hook, 309 struct sk_buff **pskb, 310 struct net_device *indev, 311 struct net_device *outdev, 312 int (*okfn)(struct sk_buff *), int thresh, 313 int cond) 314{ 315 return okfn(*pskb); 316} 317static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb, 318 struct net_device *indev, struct net_device *outdev, 319 int (*okfn)(struct sk_buff *)) 320{ 321 return 1; 322} 323static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} 324struct flowi; 325static inline void 326nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {} 327#endif /*CONFIG_NETFILTER*/ 328 329#endif /*__KERNEL__*/ 330#endif /*__LINUX_NETFILTER_H*/