netfilter.h 9.8 KB

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