netfilter.h 12 KB

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