netfilter.h 12 KB

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