netfilter.h 8.8 KB

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