netfilter.h 10 KB

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