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