netfilter.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #ifndef __LINUX_NETFILTER_H
  2. #define __LINUX_NETFILTER_H
  3. #include <linux/init.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/net.h>
  6. #include <linux/if.h>
  7. #include <linux/in.h>
  8. #include <linux/in6.h>
  9. #include <linux/wait.h>
  10. #include <linux/list.h>
  11. #include <uapi/linux/netfilter.h>
  12. #ifdef CONFIG_NETFILTER
  13. static inline int NF_DROP_GETERR(int verdict)
  14. {
  15. return -(verdict >> NF_VERDICT_QBITS);
  16. }
  17. static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
  18. const union nf_inet_addr *a2)
  19. {
  20. return a1->all[0] == a2->all[0] &&
  21. a1->all[1] == a2->all[1] &&
  22. a1->all[2] == a2->all[2] &&
  23. a1->all[3] == a2->all[3];
  24. }
  25. static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
  26. union nf_inet_addr *result,
  27. const union nf_inet_addr *mask)
  28. {
  29. result->all[0] = a1->all[0] & mask->all[0];
  30. result->all[1] = a1->all[1] & mask->all[1];
  31. result->all[2] = a1->all[2] & mask->all[2];
  32. result->all[3] = a1->all[3] & mask->all[3];
  33. }
  34. extern int netfilter_init(void);
  35. /* Largest hook number + 1 */
  36. #define NF_MAX_HOOKS 8
  37. struct sk_buff;
  38. typedef unsigned int nf_hookfn(unsigned int hooknum,
  39. struct sk_buff *skb,
  40. const struct net_device *in,
  41. const struct net_device *out,
  42. int (*okfn)(struct sk_buff *));
  43. struct nf_hook_ops {
  44. struct list_head list;
  45. /* User fills in from here down. */
  46. nf_hookfn *hook;
  47. struct module *owner;
  48. u_int8_t pf;
  49. unsigned int hooknum;
  50. /* Hooks are ordered in ascending priority. */
  51. int priority;
  52. };
  53. struct nf_sockopt_ops {
  54. struct list_head list;
  55. u_int8_t pf;
  56. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  57. int set_optmin;
  58. int set_optmax;
  59. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  60. #ifdef CONFIG_COMPAT
  61. int (*compat_set)(struct sock *sk, int optval,
  62. void __user *user, unsigned int len);
  63. #endif
  64. int get_optmin;
  65. int get_optmax;
  66. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  67. #ifdef CONFIG_COMPAT
  68. int (*compat_get)(struct sock *sk, int optval,
  69. void __user *user, int *len);
  70. #endif
  71. /* Use the module struct to lock set/get code in place */
  72. struct module *owner;
  73. };
  74. /* Function to register/unregister hook points. */
  75. int nf_register_hook(struct nf_hook_ops *reg);
  76. void nf_unregister_hook(struct nf_hook_ops *reg);
  77. int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  78. void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  79. /* Functions to register get/setsockopt ranges (non-inclusive). You
  80. need to check permissions yourself! */
  81. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  82. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  83. extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  84. #if defined(CONFIG_JUMP_LABEL)
  85. #include <linux/static_key.h>
  86. extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  87. static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  88. {
  89. if (__builtin_constant_p(pf) &&
  90. __builtin_constant_p(hook))
  91. return static_key_false(&nf_hooks_needed[pf][hook]);
  92. return !list_empty(&nf_hooks[pf][hook]);
  93. }
  94. #else
  95. static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  96. {
  97. return !list_empty(&nf_hooks[pf][hook]);
  98. }
  99. #endif
  100. int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  101. struct net_device *indev, struct net_device *outdev,
  102. int (*okfn)(struct sk_buff *), int thresh);
  103. /**
  104. * nf_hook_thresh - call a netfilter hook
  105. *
  106. * Returns 1 if the hook has allowed the packet to pass. The function
  107. * okfn must be invoked by the caller in this case. Any other return
  108. * value indicates the packet has been consumed by the hook.
  109. */
  110. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  111. struct sk_buff *skb,
  112. struct net_device *indev,
  113. struct net_device *outdev,
  114. int (*okfn)(struct sk_buff *), int thresh)
  115. {
  116. if (nf_hooks_active(pf, hook))
  117. return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
  118. return 1;
  119. }
  120. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  121. struct net_device *indev, struct net_device *outdev,
  122. int (*okfn)(struct sk_buff *))
  123. {
  124. return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
  125. }
  126. /* Activate hook; either okfn or kfree_skb called, unless a hook
  127. returns NF_STOLEN (in which case, it's up to the hook to deal with
  128. the consequences).
  129. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  130. accepted.
  131. */
  132. /* RR:
  133. > I don't want nf_hook to return anything because people might forget
  134. > about async and trust the return value to mean "packet was ok".
  135. AK:
  136. Just document it clearly, then you can expect some sense from kernel
  137. coders :)
  138. */
  139. static inline int
  140. NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  141. struct net_device *in, struct net_device *out,
  142. int (*okfn)(struct sk_buff *), int thresh)
  143. {
  144. int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
  145. if (ret == 1)
  146. ret = okfn(skb);
  147. return ret;
  148. }
  149. static inline int
  150. NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  151. struct net_device *in, struct net_device *out,
  152. int (*okfn)(struct sk_buff *), bool cond)
  153. {
  154. int ret;
  155. if (!cond ||
  156. ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
  157. ret = okfn(skb);
  158. return ret;
  159. }
  160. static inline int
  161. NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  162. struct net_device *in, struct net_device *out,
  163. int (*okfn)(struct sk_buff *))
  164. {
  165. return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
  166. }
  167. /* Call setsockopt() */
  168. int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  169. unsigned int len);
  170. int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  171. int *len);
  172. #ifdef CONFIG_COMPAT
  173. int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
  174. char __user *opt, unsigned int len);
  175. int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
  176. char __user *opt, int *len);
  177. #endif
  178. /* Call this before modifying an existing packet: ensures it is
  179. modifiable and linear to the point you care about (writable_len).
  180. Returns true or false. */
  181. extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
  182. struct flowi;
  183. struct nf_queue_entry;
  184. struct nf_afinfo {
  185. unsigned short family;
  186. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  187. unsigned int dataoff, u_int8_t protocol);
  188. __sum16 (*checksum_partial)(struct sk_buff *skb,
  189. unsigned int hook,
  190. unsigned int dataoff,
  191. unsigned int len,
  192. u_int8_t protocol);
  193. int (*route)(struct net *net, struct dst_entry **dst,
  194. struct flowi *fl, bool strict);
  195. void (*saveroute)(const struct sk_buff *skb,
  196. struct nf_queue_entry *entry);
  197. int (*reroute)(struct sk_buff *skb,
  198. const struct nf_queue_entry *entry);
  199. int route_key_size;
  200. };
  201. extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
  202. static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
  203. {
  204. return rcu_dereference(nf_afinfo[family]);
  205. }
  206. static inline __sum16
  207. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  208. u_int8_t protocol, unsigned short family)
  209. {
  210. const struct nf_afinfo *afinfo;
  211. __sum16 csum = 0;
  212. rcu_read_lock();
  213. afinfo = nf_get_afinfo(family);
  214. if (afinfo)
  215. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  216. rcu_read_unlock();
  217. return csum;
  218. }
  219. static inline __sum16
  220. nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
  221. unsigned int dataoff, unsigned int len,
  222. u_int8_t protocol, unsigned short family)
  223. {
  224. const struct nf_afinfo *afinfo;
  225. __sum16 csum = 0;
  226. rcu_read_lock();
  227. afinfo = nf_get_afinfo(family);
  228. if (afinfo)
  229. csum = afinfo->checksum_partial(skb, hook, dataoff, len,
  230. protocol);
  231. rcu_read_unlock();
  232. return csum;
  233. }
  234. extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
  235. extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
  236. #include <net/flow.h>
  237. extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
  238. static inline void
  239. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  240. {
  241. #ifdef CONFIG_NF_NAT_NEEDED
  242. void (*decodefn)(struct sk_buff *, struct flowi *);
  243. rcu_read_lock();
  244. decodefn = rcu_dereference(nf_nat_decode_session_hook);
  245. if (decodefn)
  246. decodefn(skb, fl);
  247. rcu_read_unlock();
  248. #endif
  249. }
  250. #else /* !CONFIG_NETFILTER */
  251. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  252. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  253. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  254. struct sk_buff *skb,
  255. struct net_device *indev,
  256. struct net_device *outdev,
  257. int (*okfn)(struct sk_buff *), int thresh)
  258. {
  259. return okfn(skb);
  260. }
  261. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  262. struct net_device *indev, struct net_device *outdev,
  263. int (*okfn)(struct sk_buff *))
  264. {
  265. return 1;
  266. }
  267. struct flowi;
  268. static inline void
  269. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  270. {
  271. }
  272. #endif /*CONFIG_NETFILTER*/
  273. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  274. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
  275. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  276. extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
  277. struct nf_conn;
  278. struct nlattr;
  279. struct nfq_ct_hook {
  280. size_t (*build_size)(const struct nf_conn *ct);
  281. int (*build)(struct sk_buff *skb, struct nf_conn *ct);
  282. int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
  283. };
  284. extern struct nfq_ct_hook __rcu *nfq_ct_hook;
  285. struct nfq_ct_nat_hook {
  286. void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
  287. u32 ctinfo, int off);
  288. };
  289. extern struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook;
  290. #else
  291. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  292. #endif
  293. #endif /*__LINUX_NETFILTER_H*/