netfilter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #ifndef __LINUX_NETFILTER_H
  2. #define __LINUX_NETFILTER_H
  3. #ifdef __KERNEL__
  4. #include <linux/init.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/net.h>
  7. #include <linux/if.h>
  8. #include <linux/in.h>
  9. #include <linux/in6.h>
  10. #include <linux/wait.h>
  11. #include <linux/list.h>
  12. #endif
  13. #include <linux/types.h>
  14. #include <linux/compiler.h>
  15. /* Responses from hook functions. */
  16. #define NF_DROP 0
  17. #define NF_ACCEPT 1
  18. #define NF_STOLEN 2
  19. #define NF_QUEUE 3
  20. #define NF_REPEAT 4
  21. #define NF_STOP 5
  22. #define NF_MAX_VERDICT NF_STOP
  23. /* we overload the higher bits for encoding auxiliary data such as the queue
  24. * number. Not nice, but better than additional function arguments. */
  25. #define NF_VERDICT_MASK 0x0000ffff
  26. #define NF_VERDICT_BITS 16
  27. #define NF_VERDICT_QMASK 0xffff0000
  28. #define NF_VERDICT_QBITS 16
  29. #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
  30. #define NF_DROP_ERR(x) (((-x) << NF_VERDICT_BITS) | NF_DROP)
  31. /* only for userspace compatibility */
  32. #ifndef __KERNEL__
  33. /* Generic cache responses from hook functions.
  34. <= 0x2000 is used for protocol-flags. */
  35. #define NFC_UNKNOWN 0x4000
  36. #define NFC_ALTERED 0x8000
  37. #endif
  38. enum nf_inet_hooks {
  39. NF_INET_PRE_ROUTING,
  40. NF_INET_LOCAL_IN,
  41. NF_INET_FORWARD,
  42. NF_INET_LOCAL_OUT,
  43. NF_INET_POST_ROUTING,
  44. NF_INET_NUMHOOKS
  45. };
  46. enum {
  47. NFPROTO_UNSPEC = 0,
  48. NFPROTO_IPV4 = 2,
  49. NFPROTO_ARP = 3,
  50. NFPROTO_BRIDGE = 7,
  51. NFPROTO_IPV6 = 10,
  52. NFPROTO_DECNET = 12,
  53. NFPROTO_NUMPROTO,
  54. };
  55. union nf_inet_addr {
  56. __u32 all[4];
  57. __be32 ip;
  58. __be32 ip6[4];
  59. struct in_addr in;
  60. struct in6_addr in6;
  61. };
  62. #ifdef __KERNEL__
  63. #ifdef CONFIG_NETFILTER
  64. static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
  65. const union nf_inet_addr *a2)
  66. {
  67. return a1->all[0] == a2->all[0] &&
  68. a1->all[1] == a2->all[1] &&
  69. a1->all[2] == a2->all[2] &&
  70. a1->all[3] == a2->all[3];
  71. }
  72. extern void netfilter_init(void);
  73. /* Largest hook number + 1 */
  74. #define NF_MAX_HOOKS 8
  75. struct sk_buff;
  76. typedef unsigned int nf_hookfn(unsigned int hooknum,
  77. struct sk_buff *skb,
  78. const struct net_device *in,
  79. const struct net_device *out,
  80. int (*okfn)(struct sk_buff *));
  81. struct nf_hook_ops {
  82. struct list_head list;
  83. /* User fills in from here down. */
  84. nf_hookfn *hook;
  85. struct module *owner;
  86. u_int8_t pf;
  87. unsigned int hooknum;
  88. /* Hooks are ordered in ascending priority. */
  89. int priority;
  90. };
  91. struct nf_sockopt_ops {
  92. struct list_head list;
  93. u_int8_t pf;
  94. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  95. int set_optmin;
  96. int set_optmax;
  97. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  98. #ifdef CONFIG_COMPAT
  99. int (*compat_set)(struct sock *sk, int optval,
  100. void __user *user, unsigned int len);
  101. #endif
  102. int get_optmin;
  103. int get_optmax;
  104. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  105. #ifdef CONFIG_COMPAT
  106. int (*compat_get)(struct sock *sk, int optval,
  107. void __user *user, int *len);
  108. #endif
  109. /* Use the module struct to lock set/get code in place */
  110. struct module *owner;
  111. };
  112. /* Function to register/unregister hook points. */
  113. int nf_register_hook(struct nf_hook_ops *reg);
  114. void nf_unregister_hook(struct nf_hook_ops *reg);
  115. int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  116. void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  117. /* Functions to register get/setsockopt ranges (non-inclusive). You
  118. need to check permissions yourself! */
  119. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  120. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  121. #ifdef CONFIG_SYSCTL
  122. /* Sysctl registration */
  123. extern struct ctl_path nf_net_netfilter_sysctl_path[];
  124. extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
  125. #endif /* CONFIG_SYSCTL */
  126. extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  127. int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  128. struct net_device *indev, struct net_device *outdev,
  129. int (*okfn)(struct sk_buff *), int thresh);
  130. /**
  131. * nf_hook_thresh - call a netfilter hook
  132. *
  133. * Returns 1 if the hook has allowed the packet to pass. The function
  134. * okfn must be invoked by the caller in this case. Any other return
  135. * value indicates the packet has been consumed by the hook.
  136. */
  137. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  138. struct sk_buff *skb,
  139. struct net_device *indev,
  140. struct net_device *outdev,
  141. int (*okfn)(struct sk_buff *), int thresh)
  142. {
  143. #ifndef CONFIG_NETFILTER_DEBUG
  144. if (list_empty(&nf_hooks[pf][hook]))
  145. return 1;
  146. #endif
  147. return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
  148. }
  149. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  150. struct net_device *indev, struct net_device *outdev,
  151. int (*okfn)(struct sk_buff *))
  152. {
  153. return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
  154. }
  155. /* Activate hook; either okfn or kfree_skb called, unless a hook
  156. returns NF_STOLEN (in which case, it's up to the hook to deal with
  157. the consequences).
  158. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  159. accepted.
  160. */
  161. /* RR:
  162. > I don't want nf_hook to return anything because people might forget
  163. > about async and trust the return value to mean "packet was ok".
  164. AK:
  165. Just document it clearly, then you can expect some sense from kernel
  166. coders :)
  167. */
  168. static inline int
  169. NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  170. struct net_device *in, struct net_device *out,
  171. int (*okfn)(struct sk_buff *), int thresh)
  172. {
  173. int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
  174. if (ret == 1)
  175. ret = okfn(skb);
  176. return ret;
  177. }
  178. static inline int
  179. NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  180. struct net_device *in, struct net_device *out,
  181. int (*okfn)(struct sk_buff *), bool cond)
  182. {
  183. int ret;
  184. if (!cond ||
  185. ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
  186. ret = okfn(skb);
  187. return ret;
  188. }
  189. static inline int
  190. NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  191. struct net_device *in, struct net_device *out,
  192. int (*okfn)(struct sk_buff *))
  193. {
  194. return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
  195. }
  196. /* Call setsockopt() */
  197. int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  198. unsigned int len);
  199. int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  200. int *len);
  201. #ifdef CONFIG_COMPAT
  202. int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
  203. char __user *opt, unsigned int len);
  204. int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
  205. char __user *opt, int *len);
  206. #endif
  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 *skb, unsigned int writable_len);
  211. struct flowi;
  212. struct nf_queue_entry;
  213. struct nf_afinfo {
  214. unsigned short family;
  215. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  216. unsigned int dataoff, u_int8_t protocol);
  217. __sum16 (*checksum_partial)(struct sk_buff *skb,
  218. unsigned int hook,
  219. unsigned int dataoff,
  220. unsigned int len,
  221. u_int8_t protocol);
  222. int (*route)(struct dst_entry **dst, struct flowi *fl);
  223. void (*saveroute)(const struct sk_buff *skb,
  224. struct nf_queue_entry *entry);
  225. int (*reroute)(struct sk_buff *skb,
  226. const struct nf_queue_entry *entry);
  227. int route_key_size;
  228. };
  229. extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
  230. static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
  231. {
  232. return rcu_dereference(nf_afinfo[family]);
  233. }
  234. static inline __sum16
  235. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  236. u_int8_t protocol, unsigned short family)
  237. {
  238. const struct nf_afinfo *afinfo;
  239. __sum16 csum = 0;
  240. rcu_read_lock();
  241. afinfo = nf_get_afinfo(family);
  242. if (afinfo)
  243. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  244. rcu_read_unlock();
  245. return csum;
  246. }
  247. static inline __sum16
  248. nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
  249. unsigned int dataoff, unsigned int len,
  250. u_int8_t protocol, unsigned short family)
  251. {
  252. const struct nf_afinfo *afinfo;
  253. __sum16 csum = 0;
  254. rcu_read_lock();
  255. afinfo = nf_get_afinfo(family);
  256. if (afinfo)
  257. csum = afinfo->checksum_partial(skb, hook, dataoff, len,
  258. protocol);
  259. rcu_read_unlock();
  260. return csum;
  261. }
  262. extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
  263. extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
  264. #include <net/flow.h>
  265. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  266. static inline void
  267. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  268. {
  269. #ifdef CONFIG_NF_NAT_NEEDED
  270. void (*decodefn)(struct sk_buff *, struct flowi *);
  271. if (family == AF_INET) {
  272. rcu_read_lock();
  273. decodefn = rcu_dereference(ip_nat_decode_session);
  274. if (decodefn)
  275. decodefn(skb, fl);
  276. rcu_read_unlock();
  277. }
  278. #endif
  279. }
  280. #ifdef CONFIG_PROC_FS
  281. #include <linux/proc_fs.h>
  282. extern struct proc_dir_entry *proc_net_netfilter;
  283. #endif
  284. #else /* !CONFIG_NETFILTER */
  285. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  286. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  287. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  288. struct sk_buff *skb,
  289. struct net_device *indev,
  290. struct net_device *outdev,
  291. int (*okfn)(struct sk_buff *), int thresh)
  292. {
  293. return okfn(skb);
  294. }
  295. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  296. struct net_device *indev, struct net_device *outdev,
  297. int (*okfn)(struct sk_buff *))
  298. {
  299. return 1;
  300. }
  301. struct flowi;
  302. static inline void
  303. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  304. {
  305. }
  306. #endif /*CONFIG_NETFILTER*/
  307. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  308. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  309. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  310. extern void (*nf_ct_destroy)(struct nf_conntrack *);
  311. #else
  312. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  313. #endif
  314. #endif /*__KERNEL__*/
  315. #endif /*__LINUX_NETFILTER_H*/