netfilter.h 12 KB

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