netfilter.h 10 KB

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