netfilter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <linux/rcupdate.h>
  14. /* Responses from hook functions. */
  15. #define NF_DROP 0
  16. #define NF_ACCEPT 1
  17. #define NF_STOLEN 2
  18. #define NF_QUEUE 3
  19. #define NF_REPEAT 4
  20. #define NF_STOP 5
  21. #define NF_MAX_VERDICT NF_STOP
  22. /* we overload the higher bits for encoding auxiliary data such as the queue
  23. * number. Not nice, but better than additional function arguments. */
  24. #define NF_VERDICT_MASK 0x0000ffff
  25. #define NF_VERDICT_BITS 16
  26. #define NF_VERDICT_QMASK 0xffff0000
  27. #define NF_VERDICT_QBITS 16
  28. #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
  29. /* only for userspace compatibility */
  30. #ifndef __KERNEL__
  31. /* Generic cache responses from hook functions.
  32. <= 0x2000 is used for protocol-flags. */
  33. #define NFC_UNKNOWN 0x4000
  34. #define NFC_ALTERED 0x8000
  35. #endif
  36. #ifdef __KERNEL__
  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. #ifdef CONFIG_SYSCTL
  99. /* Sysctl registration */
  100. struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
  101. struct ctl_table *table);
  102. void nf_unregister_sysctl_table(struct ctl_table_header *header,
  103. struct ctl_table *table);
  104. extern struct ctl_table nf_net_netfilter_sysctl_path[];
  105. extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
  106. #endif /* CONFIG_SYSCTL */
  107. extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
  108. /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
  109. * disappear once iptables is replaced with pkttables. Please DO NOT use them
  110. * for any new code! */
  111. #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
  112. #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
  113. #define NF_LOG_IPOPT 0x04 /* Log IP options */
  114. #define NF_LOG_UID 0x08 /* Log UID owning local socket */
  115. #define NF_LOG_MASK 0x0f
  116. #define NF_LOG_TYPE_LOG 0x01
  117. #define NF_LOG_TYPE_ULOG 0x02
  118. struct nf_loginfo {
  119. u_int8_t type;
  120. union {
  121. struct {
  122. u_int32_t copy_len;
  123. u_int16_t group;
  124. u_int16_t qthreshold;
  125. } ulog;
  126. struct {
  127. u_int8_t level;
  128. u_int8_t logflags;
  129. } log;
  130. } u;
  131. };
  132. typedef void nf_logfn(unsigned int pf,
  133. unsigned int hooknum,
  134. const struct sk_buff *skb,
  135. const struct net_device *in,
  136. const struct net_device *out,
  137. const struct nf_loginfo *li,
  138. const char *prefix);
  139. struct nf_logger {
  140. struct module *me;
  141. nf_logfn *logfn;
  142. char *name;
  143. };
  144. /* Function to register/unregister log function. */
  145. int nf_log_register(int pf, struct nf_logger *logger);
  146. int nf_log_unregister_pf(int pf);
  147. void nf_log_unregister_logger(struct nf_logger *logger);
  148. /* Calls the registered backend logging function */
  149. void nf_log_packet(int pf,
  150. unsigned int hooknum,
  151. const struct sk_buff *skb,
  152. const struct net_device *in,
  153. const struct net_device *out,
  154. struct nf_loginfo *li,
  155. const char *fmt, ...);
  156. int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
  157. struct net_device *indev, struct net_device *outdev,
  158. int (*okfn)(struct sk_buff *), int thresh);
  159. /**
  160. * nf_hook_thresh - call a netfilter hook
  161. *
  162. * Returns 1 if the hook has allowed the packet to pass. The function
  163. * okfn must be invoked by the caller in this case. Any other return
  164. * value indicates the packet has been consumed by the hook.
  165. */
  166. static inline int nf_hook_thresh(int pf, unsigned int hook,
  167. struct sk_buff **pskb,
  168. struct net_device *indev,
  169. struct net_device *outdev,
  170. int (*okfn)(struct sk_buff *), int thresh,
  171. int cond)
  172. {
  173. if (!cond)
  174. return 1;
  175. #ifndef CONFIG_NETFILTER_DEBUG
  176. if (list_empty(&nf_hooks[pf][hook]))
  177. return 1;
  178. #endif
  179. return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
  180. }
  181. static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
  182. struct net_device *indev, struct net_device *outdev,
  183. int (*okfn)(struct sk_buff *))
  184. {
  185. return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
  186. }
  187. /* Activate hook; either okfn or kfree_skb called, unless a hook
  188. returns NF_STOLEN (in which case, it's up to the hook to deal with
  189. the consequences).
  190. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  191. accepted.
  192. */
  193. /* RR:
  194. > I don't want nf_hook to return anything because people might forget
  195. > about async and trust the return value to mean "packet was ok".
  196. AK:
  197. Just document it clearly, then you can expect some sense from kernel
  198. coders :)
  199. */
  200. /* This is gross, but inline doesn't cut it for avoiding the function
  201. call in fast path: gcc doesn't inline (needs value tracking?). --RR */
  202. /* HX: It's slightly less gross now. */
  203. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  204. ({int __ret; \
  205. if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
  206. __ret = (okfn)(skb); \
  207. __ret;})
  208. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
  209. ({int __ret; \
  210. if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
  211. __ret = (okfn)(skb); \
  212. __ret;})
  213. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  214. NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
  215. /* Call setsockopt() */
  216. int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  217. int len);
  218. int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  219. int *len);
  220. int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
  221. char __user *opt, int len);
  222. int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
  223. char __user *opt, int *len);
  224. /* Packet queuing */
  225. struct nf_queue_handler {
  226. int (*outfn)(struct sk_buff *skb, struct nf_info *info,
  227. unsigned int queuenum, void *data);
  228. void *data;
  229. char *name;
  230. };
  231. extern int nf_register_queue_handler(int pf,
  232. struct nf_queue_handler *qh);
  233. extern int nf_unregister_queue_handler(int pf);
  234. extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
  235. extern void nf_reinject(struct sk_buff *skb,
  236. struct nf_info *info,
  237. unsigned int verdict);
  238. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  239. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  240. /* FIXME: Before cache is ever used, this must be implemented for real. */
  241. extern void nf_invalidate_cache(int pf);
  242. /* Call this before modifying an existing packet: ensures it is
  243. modifiable and linear to the point you care about (writable_len).
  244. Returns true or false. */
  245. extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
  246. static inline void nf_csum_replace4(__sum16 *sum, __be32 from, __be32 to)
  247. {
  248. __be32 diff[] = { ~from, to };
  249. *sum = csum_fold(csum_partial((char *)diff, sizeof(diff), ~csum_unfold(*sum)));
  250. }
  251. static inline void nf_csum_replace2(__sum16 *sum, __be16 from, __be16 to)
  252. {
  253. nf_csum_replace4(sum, (__force __be32)from, (__force __be32)to);
  254. }
  255. extern void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  256. __be32 from, __be32 to, int pseudohdr);
  257. static inline void nf_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
  258. __be16 from, __be16 to, int pseudohdr)
  259. {
  260. nf_proto_csum_replace4(sum, skb, (__force __be32)from,
  261. (__force __be32)to, pseudohdr);
  262. }
  263. struct nf_afinfo {
  264. unsigned short family;
  265. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  266. unsigned int dataoff, u_int8_t protocol);
  267. void (*saveroute)(const struct sk_buff *skb,
  268. struct nf_info *info);
  269. int (*reroute)(struct sk_buff **skb,
  270. const struct nf_info *info);
  271. int route_key_size;
  272. };
  273. extern struct nf_afinfo *nf_afinfo[];
  274. static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
  275. {
  276. return rcu_dereference(nf_afinfo[family]);
  277. }
  278. static inline __sum16
  279. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  280. u_int8_t protocol, unsigned short family)
  281. {
  282. struct nf_afinfo *afinfo;
  283. __sum16 csum = 0;
  284. rcu_read_lock();
  285. afinfo = nf_get_afinfo(family);
  286. if (afinfo)
  287. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  288. rcu_read_unlock();
  289. return csum;
  290. }
  291. extern int nf_register_afinfo(struct nf_afinfo *afinfo);
  292. extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
  293. #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
  294. #include <net/flow.h>
  295. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  296. static inline void
  297. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
  298. {
  299. #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
  300. void (*decodefn)(struct sk_buff *, struct flowi *);
  301. if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
  302. decodefn(skb, fl);
  303. #endif
  304. }
  305. #ifdef CONFIG_PROC_FS
  306. #include <linux/proc_fs.h>
  307. extern struct proc_dir_entry *proc_net_netfilter;
  308. #endif
  309. #else /* !CONFIG_NETFILTER */
  310. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  311. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  312. static inline int nf_hook_thresh(int pf, unsigned int hook,
  313. struct sk_buff **pskb,
  314. struct net_device *indev,
  315. struct net_device *outdev,
  316. int (*okfn)(struct sk_buff *), int thresh,
  317. int cond)
  318. {
  319. return okfn(*pskb);
  320. }
  321. static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
  322. struct net_device *indev, struct net_device *outdev,
  323. int (*okfn)(struct sk_buff *))
  324. {
  325. return 1;
  326. }
  327. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  328. struct flowi;
  329. static inline void
  330. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
  331. #endif /*CONFIG_NETFILTER*/
  332. #endif /*__KERNEL__*/
  333. #endif /*__LINUX_NETFILTER_H*/