netfilter.h 10 KB

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