netfilter.h 5.9 KB

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