netfilter.h 8.7 KB

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