netfilter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. u_int8_t pf;
  44. struct net_device *in;
  45. struct net_device *out;
  46. struct sock *sk;
  47. struct net *net;
  48. int (*okfn)(struct net *, struct sock *, struct sk_buff *);
  49. };
  50. typedef unsigned int nf_hookfn(void *priv,
  51. struct sk_buff *skb,
  52. const struct nf_hook_state *state);
  53. struct nf_hook_ops {
  54. struct list_head list;
  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. unsigned int hooknum;
  61. /* Hooks are ordered in ascending priority. */
  62. int priority;
  63. };
  64. struct nf_hook_entry {
  65. struct nf_hook_entry __rcu *next;
  66. nf_hookfn *hook;
  67. void *priv;
  68. const struct nf_hook_ops *orig_ops;
  69. };
  70. static inline void
  71. nf_hook_entry_init(struct nf_hook_entry *entry, const struct nf_hook_ops *ops)
  72. {
  73. entry->next = NULL;
  74. entry->hook = ops->hook;
  75. entry->priv = ops->priv;
  76. entry->orig_ops = ops;
  77. }
  78. static inline int
  79. nf_hook_entry_priority(const struct nf_hook_entry *entry)
  80. {
  81. return entry->orig_ops->priority;
  82. }
  83. static inline int
  84. nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
  85. struct nf_hook_state *state)
  86. {
  87. return entry->hook(entry->priv, skb, state);
  88. }
  89. static inline const struct nf_hook_ops *
  90. nf_hook_entry_ops(const struct nf_hook_entry *entry)
  91. {
  92. return entry->orig_ops;
  93. }
  94. static inline void nf_hook_state_init(struct nf_hook_state *p,
  95. unsigned int hook,
  96. u_int8_t pf,
  97. struct net_device *indev,
  98. struct net_device *outdev,
  99. struct sock *sk,
  100. struct net *net,
  101. int (*okfn)(struct net *, struct sock *, struct sk_buff *))
  102. {
  103. p->hook = hook;
  104. p->pf = pf;
  105. p->in = indev;
  106. p->out = outdev;
  107. p->sk = sk;
  108. p->net = net;
  109. p->okfn = okfn;
  110. }
  111. struct nf_sockopt_ops {
  112. struct list_head list;
  113. u_int8_t pf;
  114. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  115. int set_optmin;
  116. int set_optmax;
  117. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  118. #ifdef CONFIG_COMPAT
  119. int (*compat_set)(struct sock *sk, int optval,
  120. void __user *user, unsigned int len);
  121. #endif
  122. int get_optmin;
  123. int get_optmax;
  124. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  125. #ifdef CONFIG_COMPAT
  126. int (*compat_get)(struct sock *sk, int optval,
  127. void __user *user, int *len);
  128. #endif
  129. /* Use the module struct to lock set/get code in place */
  130. struct module *owner;
  131. };
  132. /* Function to register/unregister hook points. */
  133. int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
  134. void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
  135. int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
  136. unsigned int n);
  137. void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
  138. unsigned int n);
  139. int nf_register_hook(struct nf_hook_ops *reg);
  140. void nf_unregister_hook(struct nf_hook_ops *reg);
  141. int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  142. void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  143. int _nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  144. void _nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  145. /* Functions to register get/setsockopt ranges (non-inclusive). You
  146. need to check permissions yourself! */
  147. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  148. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  149. #ifdef HAVE_JUMP_LABEL
  150. extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  151. #endif
  152. int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
  153. struct nf_hook_entry *entry);
  154. /**
  155. * nf_hook - call a netfilter hook
  156. *
  157. * Returns 1 if the hook has allowed the packet to pass. The function
  158. * okfn must be invoked by the caller in this case. Any other return
  159. * value indicates the packet has been consumed by the hook.
  160. */
  161. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
  162. struct sock *sk, struct sk_buff *skb,
  163. struct net_device *indev, struct net_device *outdev,
  164. int (*okfn)(struct net *, struct sock *, struct sk_buff *))
  165. {
  166. struct nf_hook_entry *hook_head;
  167. int ret = 1;
  168. #ifdef HAVE_JUMP_LABEL
  169. if (__builtin_constant_p(pf) &&
  170. __builtin_constant_p(hook) &&
  171. !static_key_false(&nf_hooks_needed[pf][hook]))
  172. return 1;
  173. #endif
  174. rcu_read_lock();
  175. hook_head = rcu_dereference(net->nf.hooks[pf][hook]);
  176. if (hook_head) {
  177. struct nf_hook_state state;
  178. nf_hook_state_init(&state, hook, pf, indev, outdev,
  179. sk, net, okfn);
  180. ret = nf_hook_slow(skb, &state, hook_head);
  181. }
  182. rcu_read_unlock();
  183. return ret;
  184. }
  185. /* Activate hook; either okfn or kfree_skb called, unless a hook
  186. returns NF_STOLEN (in which case, it's up to the hook to deal with
  187. the consequences).
  188. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  189. accepted.
  190. */
  191. /* RR:
  192. > I don't want nf_hook to return anything because people might forget
  193. > about async and trust the return value to mean "packet was ok".
  194. AK:
  195. Just document it clearly, then you can expect some sense from kernel
  196. coders :)
  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(pf, hook, net, sk, skb, in, out, okfn)) == 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. int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
  216. if (ret == 1)
  217. ret = okfn(net, sk, skb);
  218. return ret;
  219. }
  220. /* Call setsockopt() */
  221. int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  222. unsigned int len);
  223. int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  224. int *len);
  225. #ifdef CONFIG_COMPAT
  226. int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
  227. char __user *opt, unsigned int len);
  228. int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
  229. char __user *opt, int *len);
  230. #endif
  231. /* Call this before modifying an existing packet: ensures it is
  232. modifiable and linear to the point you care about (writable_len).
  233. Returns true or false. */
  234. int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
  235. struct flowi;
  236. struct nf_queue_entry;
  237. struct nf_afinfo {
  238. unsigned short family;
  239. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  240. unsigned int dataoff, u_int8_t protocol);
  241. __sum16 (*checksum_partial)(struct sk_buff *skb,
  242. unsigned int hook,
  243. unsigned int dataoff,
  244. unsigned int len,
  245. u_int8_t protocol);
  246. int (*route)(struct net *net, struct dst_entry **dst,
  247. struct flowi *fl, bool strict);
  248. void (*saveroute)(const struct sk_buff *skb,
  249. struct nf_queue_entry *entry);
  250. int (*reroute)(struct net *net, struct sk_buff *skb,
  251. const struct nf_queue_entry *entry);
  252. int route_key_size;
  253. };
  254. extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
  255. static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
  256. {
  257. return rcu_dereference(nf_afinfo[family]);
  258. }
  259. static inline __sum16
  260. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  261. u_int8_t protocol, unsigned short family)
  262. {
  263. const struct nf_afinfo *afinfo;
  264. __sum16 csum = 0;
  265. rcu_read_lock();
  266. afinfo = nf_get_afinfo(family);
  267. if (afinfo)
  268. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  269. rcu_read_unlock();
  270. return csum;
  271. }
  272. static inline __sum16
  273. nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
  274. unsigned int dataoff, unsigned int len,
  275. u_int8_t protocol, unsigned short family)
  276. {
  277. const struct nf_afinfo *afinfo;
  278. __sum16 csum = 0;
  279. rcu_read_lock();
  280. afinfo = nf_get_afinfo(family);
  281. if (afinfo)
  282. csum = afinfo->checksum_partial(skb, hook, dataoff, len,
  283. protocol);
  284. rcu_read_unlock();
  285. return csum;
  286. }
  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*/