netfilter.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #ifdef __KERNEL__
  36. #include <linux/config.h>
  37. #ifdef CONFIG_NETFILTER
  38. extern void netfilter_init(void);
  39. /* Largest hook number + 1 */
  40. #define NF_MAX_HOOKS 8
  41. struct sk_buff;
  42. struct net_device;
  43. typedef unsigned int nf_hookfn(unsigned int hooknum,
  44. struct sk_buff **skb,
  45. const struct net_device *in,
  46. const struct net_device *out,
  47. int (*okfn)(struct sk_buff *));
  48. struct nf_hook_ops
  49. {
  50. struct list_head list;
  51. /* User fills in from here down. */
  52. nf_hookfn *hook;
  53. struct module *owner;
  54. int pf;
  55. int hooknum;
  56. /* Hooks are ordered in ascending priority. */
  57. int priority;
  58. };
  59. struct nf_sockopt_ops
  60. {
  61. struct list_head list;
  62. int pf;
  63. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  64. int set_optmin;
  65. int set_optmax;
  66. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  67. int get_optmin;
  68. int get_optmax;
  69. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  70. /* Number of users inside set() or get(). */
  71. unsigned int use;
  72. struct task_struct *cleanup_task;
  73. };
  74. /* Each queued (to userspace) skbuff has one of these. */
  75. struct nf_info
  76. {
  77. /* The ops struct which sent us to userspace. */
  78. struct nf_hook_ops *elem;
  79. /* If we're sent to userspace, this keeps housekeeping info */
  80. int pf;
  81. unsigned int hook;
  82. struct net_device *indev, *outdev;
  83. int (*okfn)(struct sk_buff *);
  84. };
  85. /* Function to register/unregister hook points. */
  86. int nf_register_hook(struct nf_hook_ops *reg);
  87. void nf_unregister_hook(struct nf_hook_ops *reg);
  88. /* Functions to register get/setsockopt ranges (non-inclusive). You
  89. need to check permissions yourself! */
  90. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  91. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  92. extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
  93. /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
  94. * disappear once iptables is replaced with pkttables. Please DO NOT use them
  95. * for any new code! */
  96. #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
  97. #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
  98. #define NF_LOG_IPOPT 0x04 /* Log IP options */
  99. #define NF_LOG_UID 0x08 /* Log UID owning local socket */
  100. #define NF_LOG_MASK 0x0f
  101. #define NF_LOG_TYPE_LOG 0x01
  102. #define NF_LOG_TYPE_ULOG 0x02
  103. struct nf_loginfo {
  104. u_int8_t type;
  105. union {
  106. struct {
  107. u_int32_t copy_len;
  108. u_int16_t group;
  109. u_int16_t qthreshold;
  110. } ulog;
  111. struct {
  112. u_int8_t level;
  113. u_int8_t logflags;
  114. } log;
  115. } u;
  116. };
  117. typedef void nf_logfn(unsigned int pf,
  118. unsigned int hooknum,
  119. const struct sk_buff *skb,
  120. const struct net_device *in,
  121. const struct net_device *out,
  122. const struct nf_loginfo *li,
  123. const char *prefix);
  124. struct nf_logger {
  125. struct module *me;
  126. nf_logfn *logfn;
  127. char *name;
  128. };
  129. /* Function to register/unregister log function. */
  130. int nf_log_register(int pf, struct nf_logger *logger);
  131. int nf_log_unregister_pf(int pf);
  132. void nf_log_unregister_logger(struct nf_logger *logger);
  133. /* Calls the registered backend logging function */
  134. void nf_log_packet(int pf,
  135. unsigned int hooknum,
  136. const struct sk_buff *skb,
  137. const struct net_device *in,
  138. const struct net_device *out,
  139. struct nf_loginfo *li,
  140. const char *fmt, ...);
  141. /* Activate hook; either okfn or kfree_skb called, unless a hook
  142. returns NF_STOLEN (in which case, it's up to the hook to deal with
  143. the consequences).
  144. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  145. accepted.
  146. */
  147. /* RR:
  148. > I don't want nf_hook to return anything because people might forget
  149. > about async and trust the return value to mean "packet was ok".
  150. AK:
  151. Just document it clearly, then you can expect some sense from kernel
  152. coders :)
  153. */
  154. /* This is gross, but inline doesn't cut it for avoiding the function
  155. call in fast path: gcc doesn't inline (needs value tracking?). --RR */
  156. #ifdef CONFIG_NETFILTER_DEBUG
  157. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  158. ({int __ret; \
  159. if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \
  160. __ret = (okfn)(skb); \
  161. __ret;})
  162. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  163. ({int __ret; \
  164. if ((__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \
  165. __ret = (okfn)(skb); \
  166. __ret;})
  167. #else
  168. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  169. ({int __ret; \
  170. if (list_empty(&nf_hooks[pf][hook]) || \
  171. (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, INT_MIN)) == 1) \
  172. __ret = (okfn)(skb); \
  173. __ret;})
  174. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  175. ({int __ret; \
  176. if (list_empty(&nf_hooks[pf][hook]) || \
  177. (__ret=nf_hook_slow(pf, hook, &(skb), indev, outdev, okfn, thresh)) == 1) \
  178. __ret = (okfn)(skb); \
  179. __ret;})
  180. #endif
  181. int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
  182. struct net_device *indev, struct net_device *outdev,
  183. int (*okfn)(struct sk_buff *), int thresh);
  184. /* Call setsockopt() */
  185. int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  186. int len);
  187. int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  188. int *len);
  189. /* Packet queuing */
  190. struct nf_queue_handler {
  191. int (*outfn)(struct sk_buff *skb, struct nf_info *info,
  192. unsigned int queuenum, void *data);
  193. void *data;
  194. char *name;
  195. };
  196. extern int nf_register_queue_handler(int pf,
  197. struct nf_queue_handler *qh);
  198. extern int nf_unregister_queue_handler(int pf);
  199. extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
  200. extern void nf_reinject(struct sk_buff *skb,
  201. struct nf_info *info,
  202. unsigned int verdict);
  203. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  204. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  205. /* FIXME: Before cache is ever used, this must be implemented for real. */
  206. extern void nf_invalidate_cache(int pf);
  207. /* Call this before modifying an existing packet: ensures it is
  208. modifiable and linear to the point you care about (writable_len).
  209. Returns true or false. */
  210. extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
  211. struct nf_queue_rerouter {
  212. void (*save)(const struct sk_buff *skb, struct nf_info *info);
  213. int (*reroute)(struct sk_buff **skb, const struct nf_info *info);
  214. int rer_size;
  215. };
  216. #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
  217. extern int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer);
  218. extern int nf_unregister_queue_rerouter(int pf);
  219. #ifdef CONFIG_PROC_FS
  220. #include <linux/proc_fs.h>
  221. extern struct proc_dir_entry *proc_net_netfilter;
  222. #endif
  223. #else /* !CONFIG_NETFILTER */
  224. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  225. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  226. #endif /*CONFIG_NETFILTER*/
  227. #endif /*__KERNEL__*/
  228. #endif /*__LINUX_NETFILTER_H*/