netfilter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /* only for userspace compatibility */
  22. #ifndef __KERNEL__
  23. /* Generic cache responses from hook functions.
  24. <= 0x2000 is used for protocol-flags. */
  25. #define NFC_UNKNOWN 0x4000
  26. #define NFC_ALTERED 0x8000
  27. #endif
  28. #ifdef __KERNEL__
  29. #include <linux/config.h>
  30. #ifdef CONFIG_NETFILTER
  31. extern void netfilter_init(void);
  32. /* Largest hook number + 1 */
  33. #define NF_MAX_HOOKS 8
  34. struct sk_buff;
  35. struct net_device;
  36. typedef unsigned int nf_hookfn(unsigned int hooknum,
  37. struct sk_buff **skb,
  38. const struct net_device *in,
  39. const struct net_device *out,
  40. int (*okfn)(struct sk_buff *));
  41. struct nf_hook_ops
  42. {
  43. struct list_head list;
  44. /* User fills in from here down. */
  45. nf_hookfn *hook;
  46. struct module *owner;
  47. int pf;
  48. int hooknum;
  49. /* Hooks are ordered in ascending priority. */
  50. int priority;
  51. };
  52. struct nf_sockopt_ops
  53. {
  54. struct list_head list;
  55. int pf;
  56. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  57. int set_optmin;
  58. int set_optmax;
  59. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  60. int get_optmin;
  61. int get_optmax;
  62. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  63. /* Number of users inside set() or get(). */
  64. unsigned int use;
  65. struct task_struct *cleanup_task;
  66. };
  67. /* Each queued (to userspace) skbuff has one of these. */
  68. struct nf_info
  69. {
  70. /* The ops struct which sent us to userspace. */
  71. struct nf_hook_ops *elem;
  72. /* If we're sent to userspace, this keeps housekeeping info */
  73. int pf;
  74. unsigned int hook;
  75. struct net_device *indev, *outdev;
  76. int (*okfn)(struct sk_buff *);
  77. };
  78. /* Function to register/unregister hook points. */
  79. int nf_register_hook(struct nf_hook_ops *reg);
  80. void nf_unregister_hook(struct nf_hook_ops *reg);
  81. /* Functions to register get/setsockopt ranges (non-inclusive). You
  82. need to check permissions yourself! */
  83. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  84. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  85. extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
  86. typedef void nf_logfn(unsigned int hooknum,
  87. const struct sk_buff *skb,
  88. const struct net_device *in,
  89. const struct net_device *out,
  90. const char *prefix);
  91. /* Function to register/unregister log function. */
  92. int nf_log_register(int pf, nf_logfn *logfn);
  93. void nf_log_unregister(int pf, nf_logfn *logfn);
  94. /* Calls the registered backend logging function */
  95. void nf_log_packet(int pf,
  96. unsigned int hooknum,
  97. const struct sk_buff *skb,
  98. const struct net_device *in,
  99. const struct net_device *out,
  100. const char *fmt, ...);
  101. /* Activate hook; either okfn or kfree_skb called, unless a hook
  102. returns NF_STOLEN (in which case, it's up to the hook to deal with
  103. the consequences).
  104. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  105. accepted.
  106. */
  107. /* RR:
  108. > I don't want nf_hook to return anything because people might forget
  109. > about async and trust the return value to mean "packet was ok".
  110. AK:
  111. Just document it clearly, then you can expect some sense from kernel
  112. coders :)
  113. */
  114. /* This is gross, but inline doesn't cut it for avoiding the function
  115. call in fast path: gcc doesn't inline (needs value tracking?). --RR */
  116. #ifdef CONFIG_NETFILTER_DEBUG
  117. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  118. ({int __ret; \
  119. if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \
  120. __ret = (okfn)(skb); \
  121. __ret;})
  122. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  123. ({int __ret; \
  124. if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \
  125. __ret = (okfn)(skb); \
  126. __ret;})
  127. #else
  128. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  129. ({int __ret; \
  130. if (list_empty(&nf_hooks[pf][hook]) || \
  131. (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \
  132. __ret = (okfn)(skb); \
  133. __ret;})
  134. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  135. ({int __ret; \
  136. if (list_empty(&nf_hooks[pf][hook]) || \
  137. (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \
  138. __ret = (okfn)(skb); \
  139. __ret;})
  140. #endif
  141. int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
  142. struct net_device *indev, struct net_device *outdev,
  143. int (*okfn)(struct sk_buff *), int thresh);
  144. /* Call setsockopt() */
  145. int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  146. int len);
  147. int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  148. int *len);
  149. /* Packet queuing */
  150. typedef int (*nf_queue_outfn_t)(struct sk_buff *skb,
  151. struct nf_info *info, void *data);
  152. extern int nf_register_queue_handler(int pf,
  153. nf_queue_outfn_t outfn, void *data);
  154. extern int nf_unregister_queue_handler(int pf);
  155. extern void nf_reinject(struct sk_buff *skb,
  156. struct nf_info *info,
  157. unsigned int verdict);
  158. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  159. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  160. /* FIXME: Before cache is ever used, this must be implemented for real. */
  161. extern void nf_invalidate_cache(int pf);
  162. /* Call this before modifying an existing packet: ensures it is
  163. modifiable and linear to the point you care about (writable_len).
  164. Returns true or false. */
  165. extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
  166. struct nf_queue_rerouter {
  167. void (*save)(const struct sk_buff *skb, struct nf_info *info);
  168. int (*reroute)(struct sk_buff **skb, const struct nf_info *info);
  169. int rer_size;
  170. };
  171. #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
  172. extern int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer);
  173. extern int nf_unregister_queue_rerouter(int pf);
  174. #else /* !CONFIG_NETFILTER */
  175. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  176. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  177. #endif /*CONFIG_NETFILTER*/
  178. #endif /*__KERNEL__*/
  179. #endif /*__LINUX_NETFILTER_H*/