netfilter.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 (*compat_set)(struct sock *sk, int optval,
  68. void __user *user, unsigned int len);
  69. int get_optmin;
  70. int get_optmax;
  71. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  72. int (*compat_get)(struct sock *sk, int optval,
  73. void __user *user, int *len);
  74. /* Number of users inside set() or get(). */
  75. unsigned int use;
  76. struct task_struct *cleanup_task;
  77. };
  78. /* Each queued (to userspace) skbuff has one of these. */
  79. struct nf_info
  80. {
  81. /* The ops struct which sent us to userspace. */
  82. struct nf_hook_ops *elem;
  83. /* If we're sent to userspace, this keeps housekeeping info */
  84. int pf;
  85. unsigned int hook;
  86. struct net_device *indev, *outdev;
  87. int (*okfn)(struct sk_buff *);
  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. /* Functions to register get/setsockopt ranges (non-inclusive). You
  93. need to check permissions yourself! */
  94. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  95. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  96. extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
  97. /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
  98. * disappear once iptables is replaced with pkttables. Please DO NOT use them
  99. * for any new code! */
  100. #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
  101. #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
  102. #define NF_LOG_IPOPT 0x04 /* Log IP options */
  103. #define NF_LOG_UID 0x08 /* Log UID owning local socket */
  104. #define NF_LOG_MASK 0x0f
  105. #define NF_LOG_TYPE_LOG 0x01
  106. #define NF_LOG_TYPE_ULOG 0x02
  107. struct nf_loginfo {
  108. u_int8_t type;
  109. union {
  110. struct {
  111. u_int32_t copy_len;
  112. u_int16_t group;
  113. u_int16_t qthreshold;
  114. } ulog;
  115. struct {
  116. u_int8_t level;
  117. u_int8_t logflags;
  118. } log;
  119. } u;
  120. };
  121. typedef void nf_logfn(unsigned int pf,
  122. unsigned int hooknum,
  123. const struct sk_buff *skb,
  124. const struct net_device *in,
  125. const struct net_device *out,
  126. const struct nf_loginfo *li,
  127. const char *prefix);
  128. struct nf_logger {
  129. struct module *me;
  130. nf_logfn *logfn;
  131. char *name;
  132. };
  133. /* Function to register/unregister log function. */
  134. int nf_log_register(int pf, struct nf_logger *logger);
  135. int nf_log_unregister_pf(int pf);
  136. void nf_log_unregister_logger(struct nf_logger *logger);
  137. /* Calls the registered backend logging function */
  138. void nf_log_packet(int pf,
  139. unsigned int hooknum,
  140. const struct sk_buff *skb,
  141. const struct net_device *in,
  142. const struct net_device *out,
  143. struct nf_loginfo *li,
  144. const char *fmt, ...);
  145. int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
  146. struct net_device *indev, struct net_device *outdev,
  147. int (*okfn)(struct sk_buff *), int thresh);
  148. /**
  149. * nf_hook_thresh - call a netfilter hook
  150. *
  151. * Returns 1 if the hook has allowed the packet to pass. The function
  152. * okfn must be invoked by the caller in this case. Any other return
  153. * value indicates the packet has been consumed by the hook.
  154. */
  155. static inline int nf_hook_thresh(int pf, unsigned int hook,
  156. struct sk_buff **pskb,
  157. struct net_device *indev,
  158. struct net_device *outdev,
  159. int (*okfn)(struct sk_buff *), int thresh,
  160. int cond)
  161. {
  162. if (!cond)
  163. return 1;
  164. #ifndef CONFIG_NETFILTER_DEBUG
  165. if (list_empty(&nf_hooks[pf][hook]))
  166. return 1;
  167. #endif
  168. return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
  169. }
  170. static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
  171. struct net_device *indev, struct net_device *outdev,
  172. int (*okfn)(struct sk_buff *))
  173. {
  174. return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
  175. }
  176. /* Activate hook; either okfn or kfree_skb called, unless a hook
  177. returns NF_STOLEN (in which case, it's up to the hook to deal with
  178. the consequences).
  179. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  180. accepted.
  181. */
  182. /* RR:
  183. > I don't want nf_hook to return anything because people might forget
  184. > about async and trust the return value to mean "packet was ok".
  185. AK:
  186. Just document it clearly, then you can expect some sense from kernel
  187. coders :)
  188. */
  189. /* This is gross, but inline doesn't cut it for avoiding the function
  190. call in fast path: gcc doesn't inline (needs value tracking?). --RR */
  191. /* HX: It's slightly less gross now. */
  192. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  193. ({int __ret; \
  194. if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
  195. __ret = (okfn)(skb); \
  196. __ret;})
  197. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
  198. ({int __ret; \
  199. if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
  200. __ret = (okfn)(skb); \
  201. __ret;})
  202. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  203. NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
  204. /* Call setsockopt() */
  205. int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  206. int len);
  207. int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  208. int *len);
  209. int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
  210. char __user *opt, int len);
  211. int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
  212. char __user *opt, int *len);
  213. /* Packet queuing */
  214. struct nf_queue_handler {
  215. int (*outfn)(struct sk_buff *skb, struct nf_info *info,
  216. unsigned int queuenum, void *data);
  217. void *data;
  218. char *name;
  219. };
  220. extern int nf_register_queue_handler(int pf,
  221. struct nf_queue_handler *qh);
  222. extern int nf_unregister_queue_handler(int pf);
  223. extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
  224. extern void nf_reinject(struct sk_buff *skb,
  225. struct nf_info *info,
  226. unsigned int verdict);
  227. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  228. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  229. /* FIXME: Before cache is ever used, this must be implemented for real. */
  230. extern void nf_invalidate_cache(int pf);
  231. /* Call this before modifying an existing packet: ensures it is
  232. modifiable and linear to the point you care about (writable_len).
  233. Returns true or false. */
  234. extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
  235. struct nf_queue_rerouter {
  236. void (*save)(const struct sk_buff *skb, struct nf_info *info);
  237. int (*reroute)(struct sk_buff **skb, const struct nf_info *info);
  238. int rer_size;
  239. };
  240. #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
  241. extern int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer);
  242. extern int nf_unregister_queue_rerouter(int pf);
  243. #include <net/flow.h>
  244. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  245. static inline void
  246. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
  247. {
  248. #ifdef CONFIG_IP_NF_NAT_NEEDED
  249. void (*decodefn)(struct sk_buff *, struct flowi *);
  250. if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
  251. decodefn(skb, fl);
  252. #endif
  253. }
  254. #ifdef CONFIG_PROC_FS
  255. #include <linux/proc_fs.h>
  256. extern struct proc_dir_entry *proc_net_netfilter;
  257. #endif
  258. #else /* !CONFIG_NETFILTER */
  259. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  260. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  261. static inline int nf_hook_thresh(int pf, unsigned int hook,
  262. struct sk_buff **pskb,
  263. struct net_device *indev,
  264. struct net_device *outdev,
  265. int (*okfn)(struct sk_buff *), int thresh,
  266. int cond)
  267. {
  268. return okfn(*pskb);
  269. }
  270. static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
  271. struct net_device *indev, struct net_device *outdev,
  272. int (*okfn)(struct sk_buff *))
  273. {
  274. return 1;
  275. }
  276. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  277. struct flowi;
  278. static inline void
  279. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
  280. #endif /*CONFIG_NETFILTER*/
  281. #endif /*__KERNEL__*/
  282. #endif /*__LINUX_NETFILTER_H*/