netfilter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  132. #if defined(CONFIG_JUMP_LABEL)
  133. #include <linux/static_key.h>
  134. extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  135. static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  136. {
  137. if (__builtin_constant_p(pf) &&
  138. __builtin_constant_p(hook))
  139. return static_key_false(&nf_hooks_needed[pf][hook]);
  140. return !list_empty(&nf_hooks[pf][hook]);
  141. }
  142. #else
  143. static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  144. {
  145. return !list_empty(&nf_hooks[pf][hook]);
  146. }
  147. #endif
  148. int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  149. struct net_device *indev, struct net_device *outdev,
  150. int (*okfn)(struct sk_buff *), int thresh);
  151. /**
  152. * nf_hook_thresh - call a netfilter hook
  153. *
  154. * Returns 1 if the hook has allowed the packet to pass. The function
  155. * okfn must be invoked by the caller in this case. Any other return
  156. * value indicates the packet has been consumed by the hook.
  157. */
  158. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  159. struct sk_buff *skb,
  160. struct net_device *indev,
  161. struct net_device *outdev,
  162. int (*okfn)(struct sk_buff *), int thresh)
  163. {
  164. if (nf_hooks_active(pf, hook))
  165. return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
  166. return 1;
  167. }
  168. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  169. struct net_device *indev, struct net_device *outdev,
  170. int (*okfn)(struct sk_buff *))
  171. {
  172. return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
  173. }
  174. /* Activate hook; either okfn or kfree_skb called, unless a hook
  175. returns NF_STOLEN (in which case, it's up to the hook to deal with
  176. the consequences).
  177. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  178. accepted.
  179. */
  180. /* RR:
  181. > I don't want nf_hook to return anything because people might forget
  182. > about async and trust the return value to mean "packet was ok".
  183. AK:
  184. Just document it clearly, then you can expect some sense from kernel
  185. coders :)
  186. */
  187. static inline int
  188. NF_HOOK_THRESH(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 *), int thresh)
  191. {
  192. int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
  193. if (ret == 1)
  194. ret = okfn(skb);
  195. return ret;
  196. }
  197. static inline int
  198. NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  199. struct net_device *in, struct net_device *out,
  200. int (*okfn)(struct sk_buff *), bool cond)
  201. {
  202. int ret;
  203. if (!cond ||
  204. ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
  205. ret = okfn(skb);
  206. return ret;
  207. }
  208. static inline int
  209. NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  210. struct net_device *in, struct net_device *out,
  211. int (*okfn)(struct sk_buff *))
  212. {
  213. return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
  214. }
  215. /* Call setsockopt() */
  216. int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  217. unsigned int len);
  218. int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  219. int *len);
  220. #ifdef CONFIG_COMPAT
  221. int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
  222. char __user *opt, unsigned int len);
  223. int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
  224. char __user *opt, int *len);
  225. #endif
  226. /* Call this before modifying an existing packet: ensures it is
  227. modifiable and linear to the point you care about (writable_len).
  228. Returns true or false. */
  229. extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
  230. struct flowi;
  231. struct nf_queue_entry;
  232. struct nf_afinfo {
  233. unsigned short family;
  234. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  235. unsigned int dataoff, u_int8_t protocol);
  236. __sum16 (*checksum_partial)(struct sk_buff *skb,
  237. unsigned int hook,
  238. unsigned int dataoff,
  239. unsigned int len,
  240. u_int8_t protocol);
  241. int (*route)(struct net *net, struct dst_entry **dst,
  242. struct flowi *fl, bool strict);
  243. void (*saveroute)(const struct sk_buff *skb,
  244. struct nf_queue_entry *entry);
  245. int (*reroute)(struct sk_buff *skb,
  246. const struct nf_queue_entry *entry);
  247. int route_key_size;
  248. };
  249. extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
  250. static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
  251. {
  252. return rcu_dereference(nf_afinfo[family]);
  253. }
  254. static inline __sum16
  255. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  256. u_int8_t protocol, unsigned short family)
  257. {
  258. const struct nf_afinfo *afinfo;
  259. __sum16 csum = 0;
  260. rcu_read_lock();
  261. afinfo = nf_get_afinfo(family);
  262. if (afinfo)
  263. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  264. rcu_read_unlock();
  265. return csum;
  266. }
  267. static inline __sum16
  268. nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
  269. unsigned int dataoff, unsigned int len,
  270. u_int8_t protocol, unsigned short family)
  271. {
  272. const struct nf_afinfo *afinfo;
  273. __sum16 csum = 0;
  274. rcu_read_lock();
  275. afinfo = nf_get_afinfo(family);
  276. if (afinfo)
  277. csum = afinfo->checksum_partial(skb, hook, dataoff, len,
  278. protocol);
  279. rcu_read_unlock();
  280. return csum;
  281. }
  282. extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
  283. extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
  284. #include <net/flow.h>
  285. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  286. static inline void
  287. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  288. {
  289. #ifdef CONFIG_NF_NAT_NEEDED
  290. void (*decodefn)(struct sk_buff *, struct flowi *);
  291. if (family == AF_INET) {
  292. rcu_read_lock();
  293. decodefn = rcu_dereference(ip_nat_decode_session);
  294. if (decodefn)
  295. decodefn(skb, fl);
  296. rcu_read_unlock();
  297. }
  298. #endif
  299. }
  300. #ifdef CONFIG_PROC_FS
  301. #include <linux/proc_fs.h>
  302. extern struct proc_dir_entry *proc_net_netfilter;
  303. #endif
  304. #else /* !CONFIG_NETFILTER */
  305. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  306. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  307. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  308. struct sk_buff *skb,
  309. struct net_device *indev,
  310. struct net_device *outdev,
  311. int (*okfn)(struct sk_buff *), int thresh)
  312. {
  313. return okfn(skb);
  314. }
  315. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  316. struct net_device *indev, struct net_device *outdev,
  317. int (*okfn)(struct sk_buff *))
  318. {
  319. return 1;
  320. }
  321. struct flowi;
  322. static inline void
  323. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  324. {
  325. }
  326. #endif /*CONFIG_NETFILTER*/
  327. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  328. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
  329. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  330. extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
  331. #else
  332. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  333. #endif
  334. #endif /*__KERNEL__*/
  335. #endif /*__LINUX_NETFILTER_H*/