netfilter.h 11 KB

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