netfilter.h 12 KB

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