Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_NETFILTER_H |
| 2 | #define __LINUX_NETFILTER_H |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/skbuff.h> |
| 6 | #include <linux/net.h> |
| 7 | #include <linux/if.h> |
Jan Engelhardt | 2e3075a | 2008-01-14 23:40:34 -0800 | [diff] [blame] | 8 | #include <linux/in.h> |
| 9 | #include <linux/in6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/wait.h> |
| 11 | #include <linux/list.h> |
Zhouyi Zhou | d1c85c2 | 2014-08-22 10:40:15 +0800 | [diff] [blame] | 12 | #include <linux/static_key.h> |
Pablo Neira Ayuso | a263653 | 2015-06-17 10:28:27 -0500 | [diff] [blame] | 13 | #include <linux/netfilter_defs.h> |
Eric W. Biederman | 085db2c | 2015-07-10 18:15:06 -0500 | [diff] [blame] | 14 | #include <linux/netdevice.h> |
| 15 | #include <net/net_namespace.h> |
Pablo Neira Ayuso | a263653 | 2015-06-17 10:28:27 -0500 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #ifdef CONFIG_NETFILTER |
Florian Westphal | f615df7 | 2011-01-18 15:52:14 +0100 | [diff] [blame] | 18 | static inline int NF_DROP_GETERR(int verdict) |
| 19 | { |
| 20 | return -(verdict >> NF_VERDICT_QBITS); |
| 21 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Patrick McHardy | b8beedd | 2008-03-25 20:09:33 -0700 | [diff] [blame] | 23 | static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1, |
| 24 | const union nf_inet_addr *a2) |
| 25 | { |
| 26 | return a1->all[0] == a2->all[0] && |
| 27 | a1->all[1] == a2->all[1] && |
| 28 | a1->all[2] == a2->all[2] && |
| 29 | a1->all[3] == a2->all[3]; |
| 30 | } |
| 31 | |
Denys Fedoryshchenko | efdedd5 | 2012-05-17 23:08:57 +0300 | [diff] [blame] | 32 | static inline void nf_inet_addr_mask(const union nf_inet_addr *a1, |
| 33 | union nf_inet_addr *result, |
| 34 | const union nf_inet_addr *mask) |
| 35 | { |
| 36 | result->all[0] = a1->all[0] & mask->all[0]; |
| 37 | result->all[1] = a1->all[1] & mask->all[1]; |
| 38 | result->all[2] = a1->all[2] & mask->all[2]; |
| 39 | result->all[3] = a1->all[3] & mask->all[3]; |
| 40 | } |
| 41 | |
Joe Perches | a0f4ecf | 2013-09-26 14:48:15 -0700 | [diff] [blame] | 42 | int netfilter_init(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct sk_buff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Patrick McHardy | 795aa6e | 2013-10-10 09:21:55 +0200 | [diff] [blame] | 46 | struct nf_hook_ops; |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 47 | |
David Miller | 1c984f8 | 2015-04-05 22:19:00 -0400 | [diff] [blame] | 48 | struct sock; |
| 49 | |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 50 | struct nf_hook_state { |
| 51 | unsigned int hook; |
| 52 | int thresh; |
| 53 | u_int8_t pf; |
| 54 | struct net_device *in; |
| 55 | struct net_device *out; |
David Miller | 1c984f8 | 2015-04-05 22:19:00 -0400 | [diff] [blame] | 56 | struct sock *sk; |
Eric W. Biederman | b11b1f6 | 2015-09-15 20:03:50 -0500 | [diff] [blame] | 57 | struct net *net; |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 58 | struct nf_hook_entry __rcu *hook_entries; |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 59 | int (*okfn)(struct net *, struct sock *, struct sk_buff *); |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 60 | }; |
| 61 | |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 62 | typedef unsigned int nf_hookfn(void *priv, |
| 63 | struct sk_buff *skb, |
| 64 | const struct nf_hook_state *state); |
| 65 | struct nf_hook_ops { |
| 66 | struct list_head list; |
| 67 | |
| 68 | /* User fills in from here down. */ |
| 69 | nf_hookfn *hook; |
| 70 | struct net_device *dev; |
| 71 | void *priv; |
| 72 | u_int8_t pf; |
| 73 | unsigned int hooknum; |
| 74 | /* Hooks are ordered in ascending priority. */ |
| 75 | int priority; |
| 76 | }; |
| 77 | |
| 78 | struct nf_hook_entry { |
| 79 | struct nf_hook_entry __rcu *next; |
| 80 | struct nf_hook_ops ops; |
| 81 | const struct nf_hook_ops *orig_ops; |
| 82 | }; |
| 83 | |
David Miller | 107a9f4 | 2015-04-05 22:18:54 -0400 | [diff] [blame] | 84 | static inline void nf_hook_state_init(struct nf_hook_state *p, |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 85 | struct nf_hook_entry *hook_entry, |
David Miller | 107a9f4 | 2015-04-05 22:18:54 -0400 | [diff] [blame] | 86 | unsigned int hook, |
| 87 | int thresh, u_int8_t pf, |
| 88 | struct net_device *indev, |
| 89 | struct net_device *outdev, |
David Miller | 1c984f8 | 2015-04-05 22:19:00 -0400 | [diff] [blame] | 90 | struct sock *sk, |
Eric W. Biederman | b11b1f6 | 2015-09-15 20:03:50 -0500 | [diff] [blame] | 91 | struct net *net, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 92 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
David Miller | 107a9f4 | 2015-04-05 22:18:54 -0400 | [diff] [blame] | 93 | { |
| 94 | p->hook = hook; |
| 95 | p->thresh = thresh; |
| 96 | p->pf = pf; |
| 97 | p->in = indev; |
| 98 | p->out = outdev; |
David Miller | 1c984f8 | 2015-04-05 22:19:00 -0400 | [diff] [blame] | 99 | p->sk = sk; |
Eric W. Biederman | b11b1f6 | 2015-09-15 20:03:50 -0500 | [diff] [blame] | 100 | p->net = net; |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 101 | RCU_INIT_POINTER(p->hook_entries, hook_entry); |
David Miller | 107a9f4 | 2015-04-05 22:18:54 -0400 | [diff] [blame] | 102 | p->okfn = okfn; |
| 103 | } |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Eric Dumazet | d94d9fe | 2009-11-04 09:50:58 -0800 | [diff] [blame] | 107 | struct nf_sockopt_ops { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | struct list_head list; |
| 109 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 110 | u_int8_t pf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 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); |
Alexey Dobriyan | c30f540 | 2010-02-02 15:03:24 +0100 | [diff] [blame] | 116 | #ifdef CONFIG_COMPAT |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 117 | int (*compat_set)(struct sock *sk, int optval, |
| 118 | void __user *user, unsigned int len); |
Alexey Dobriyan | c30f540 | 2010-02-02 15:03:24 +0100 | [diff] [blame] | 119 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | int get_optmin; |
| 121 | int get_optmax; |
| 122 | int (*get)(struct sock *sk, int optval, void __user *user, int *len); |
Alexey Dobriyan | c30f540 | 2010-02-02 15:03:24 +0100 | [diff] [blame] | 123 | #ifdef CONFIG_COMPAT |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 124 | int (*compat_get)(struct sock *sk, int optval, |
| 125 | void __user *user, int *len); |
Alexey Dobriyan | c30f540 | 2010-02-02 15:03:24 +0100 | [diff] [blame] | 126 | #endif |
Neil Horman | 16fcec3 | 2007-09-11 11:28:26 +0200 | [diff] [blame] | 127 | /* Use the module struct to lock set/get code in place */ |
| 128 | struct module *owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* Function to register/unregister hook points. */ |
Eric W. Biederman | 085db2c | 2015-07-10 18:15:06 -0500 | [diff] [blame] | 132 | int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops); |
| 133 | void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops); |
| 134 | int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg, |
| 135 | unsigned int n); |
| 136 | void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg, |
| 137 | unsigned int n); |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int nf_register_hook(struct nf_hook_ops *reg); |
| 140 | void nf_unregister_hook(struct nf_hook_ops *reg); |
Patrick McHardy | 972d1cb | 2006-04-06 14:09:12 -0700 | [diff] [blame] | 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); |
Mahesh Bandewar | e8bffe0 | 2016-09-16 12:59:13 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | /* Functions to register get/setsockopt ranges (non-inclusive). You |
| 147 | need to check permissions yourself! */ |
| 148 | int nf_register_sockopt(struct nf_sockopt_ops *reg); |
| 149 | void nf_unregister_sockopt(struct nf_sockopt_ops *reg); |
| 150 | |
Zhouyi Zhou | d1c85c2 | 2014-08-22 10:40:15 +0800 | [diff] [blame] | 151 | #ifdef HAVE_JUMP_LABEL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 152 | extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS]; |
Eric Dumazet | a2d7ec5 | 2011-11-18 17:32:46 +0000 | [diff] [blame] | 153 | #endif |
| 154 | |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 155 | int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * nf_hook_thresh - call a netfilter hook |
Pablo Neira | b8d0aad | 2015-05-13 18:19:36 +0200 | [diff] [blame] | 159 | * |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 160 | * Returns 1 if the hook has allowed the packet to pass. The function |
| 161 | * okfn must be invoked by the caller in this case. Any other return |
| 162 | * value indicates the packet has been consumed by the hook. |
| 163 | */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 164 | static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, |
Eric W. Biederman | 7a77350 | 2015-09-15 20:03:51 -0500 | [diff] [blame] | 165 | struct net *net, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 166 | struct sock *sk, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 167 | struct sk_buff *skb, |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 168 | struct net_device *indev, |
| 169 | struct net_device *outdev, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 170 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 171 | int thresh) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 172 | { |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 173 | struct nf_hook_entry *hook_head; |
| 174 | int ret = 1; |
Eric W. Biederman | 70aa996 | 2015-07-10 18:13:20 -0500 | [diff] [blame] | 175 | |
Florian Westphal | af4610c | 2016-02-25 10:08:38 +0100 | [diff] [blame] | 176 | #ifdef HAVE_JUMP_LABEL |
| 177 | if (__builtin_constant_p(pf) && |
| 178 | __builtin_constant_p(hook) && |
| 179 | !static_key_false(&nf_hooks_needed[pf][hook])) |
| 180 | return 1; |
| 181 | #endif |
| 182 | |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 183 | rcu_read_lock(); |
| 184 | hook_head = rcu_dereference(net->nf.hooks[pf][hook]); |
| 185 | if (hook_head) { |
David Miller | 107a9f4 | 2015-04-05 22:18:54 -0400 | [diff] [blame] | 186 | struct nf_hook_state state; |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 187 | |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 188 | nf_hook_state_init(&state, hook_head, hook, thresh, |
Eric W. Biederman | b11b1f6 | 2015-09-15 20:03:50 -0500 | [diff] [blame] | 189 | pf, indev, outdev, sk, net, okfn); |
Florian Westphal | fe72926 | 2016-09-21 11:35:02 -0400 | [diff] [blame] | 190 | |
| 191 | ret = nf_hook_slow(skb, &state); |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 192 | } |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 193 | rcu_read_unlock(); |
| 194 | |
| 195 | return ret; |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 198 | static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, |
| 199 | struct sock *sk, struct sk_buff *skb, |
| 200 | struct net_device *indev, struct net_device *outdev, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 201 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 202 | { |
Eric W. Biederman | 7a77350 | 2015-09-15 20:03:51 -0500 | [diff] [blame] | 203 | return nf_hook_thresh(pf, hook, net, sk, skb, indev, outdev, okfn, INT_MIN); |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* Activate hook; either okfn or kfree_skb called, unless a hook |
| 207 | returns NF_STOLEN (in which case, it's up to the hook to deal with |
| 208 | the consequences). |
| 209 | |
| 210 | Returns -ERRNO if packet dropped. Zero means queued, stolen or |
| 211 | accepted. |
| 212 | */ |
| 213 | |
| 214 | /* RR: |
| 215 | > I don't want nf_hook to return anything because people might forget |
| 216 | > about async and trust the return value to mean "packet was ok". |
| 217 | |
| 218 | AK: |
| 219 | Just document it clearly, then you can expect some sense from kernel |
| 220 | coders :) |
| 221 | */ |
| 222 | |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 223 | static inline int |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 224 | NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 225 | struct sk_buff *skb, struct net_device *in, |
| 226 | struct net_device *out, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 227 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
| 228 | int thresh) |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 229 | { |
Eric W. Biederman | 7a77350 | 2015-09-15 20:03:51 -0500 | [diff] [blame] | 230 | int ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, thresh); |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 231 | if (ret == 1) |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 232 | ret = okfn(net, sk, skb); |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 233 | return ret; |
| 234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 236 | static inline int |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 237 | NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 238 | struct sk_buff *skb, struct net_device *in, struct net_device *out, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 239 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
| 240 | bool cond) |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 241 | { |
Patrick McHardy | 4bac6b1 | 2010-02-19 08:03:28 +0100 | [diff] [blame] | 242 | int ret; |
| 243 | |
| 244 | if (!cond || |
Eric W. Biederman | 7a77350 | 2015-09-15 20:03:51 -0500 | [diff] [blame] | 245 | ((ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, INT_MIN)) == 1)) |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 246 | ret = okfn(net, sk, skb); |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 247 | return ret; |
| 248 | } |
Patrick McHardy | 16a6677 | 2006-01-06 23:01:48 -0800 | [diff] [blame] | 249 | |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 250 | static inline int |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 251 | NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb, |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 252 | struct net_device *in, struct net_device *out, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 253 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 254 | { |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 255 | return NF_HOOK_THRESH(pf, hook, net, sk, skb, in, out, okfn, INT_MIN); |
Jan Engelhardt | 2249065 | 2009-06-13 04:13:26 +0200 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | /* Call setsockopt() */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 259 | int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 260 | unsigned int len); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 261 | int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | int *len); |
Alexey Dobriyan | c30f540 | 2010-02-02 15:03:24 +0100 | [diff] [blame] | 263 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 264 | int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 265 | char __user *opt, unsigned int len); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 266 | int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 267 | char __user *opt, int *len); |
Alexey Dobriyan | c30f540 | 2010-02-02 15:03:24 +0100 | [diff] [blame] | 268 | #endif |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 269 | |
Harald Welte | 089af26 | 2005-08-09 19:37:23 -0700 | [diff] [blame] | 270 | /* Call this before modifying an existing packet: ensures it is |
| 271 | modifiable and linear to the point you care about (writable_len). |
| 272 | Returns true or false. */ |
Joe Perches | a0f4ecf | 2013-09-26 14:48:15 -0700 | [diff] [blame] | 273 | int skb_make_writable(struct sk_buff *skb, unsigned int writable_len); |
Harald Welte | 089af26 | 2005-08-09 19:37:23 -0700 | [diff] [blame] | 274 | |
Patrick McHardy | 1841a4c | 2007-12-05 01:22:05 -0800 | [diff] [blame] | 275 | struct flowi; |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 276 | struct nf_queue_entry; |
Patrick McHardy | c01cd42 | 2007-12-05 01:24:48 -0800 | [diff] [blame] | 277 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 278 | struct nf_afinfo { |
| 279 | unsigned short family; |
Al Viro | b51655b | 2006-11-14 21:40:42 -0800 | [diff] [blame] | 280 | __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook, |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 281 | unsigned int dataoff, u_int8_t protocol); |
Patrick McHardy | d63a650 | 2008-03-20 15:15:53 +0100 | [diff] [blame] | 282 | __sum16 (*checksum_partial)(struct sk_buff *skb, |
| 283 | unsigned int hook, |
| 284 | unsigned int dataoff, |
| 285 | unsigned int len, |
| 286 | u_int8_t protocol); |
Florian Westphal | 31ad3dd | 2011-04-04 16:56:29 +0200 | [diff] [blame] | 287 | int (*route)(struct net *net, struct dst_entry **dst, |
Florian Westphal | 0fae2e7 | 2011-04-04 17:00:54 +0200 | [diff] [blame] | 288 | struct flowi *fl, bool strict); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 289 | void (*saveroute)(const struct sk_buff *skb, |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 290 | struct nf_queue_entry *entry); |
Eric W. Biederman | d815d90 | 2015-09-25 15:07:28 -0500 | [diff] [blame] | 291 | int (*reroute)(struct net *net, struct sk_buff *skb, |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 292 | const struct nf_queue_entry *entry); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 293 | int route_key_size; |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 294 | }; |
| 295 | |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 296 | extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO]; |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 297 | static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family) |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 298 | { |
| 299 | return rcu_dereference(nf_afinfo[family]); |
| 300 | } |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 301 | |
Al Viro | b51655b | 2006-11-14 21:40:42 -0800 | [diff] [blame] | 302 | static inline __sum16 |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 303 | nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff, |
| 304 | u_int8_t protocol, unsigned short family) |
| 305 | { |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 306 | const struct nf_afinfo *afinfo; |
Al Viro | b51655b | 2006-11-14 21:40:42 -0800 | [diff] [blame] | 307 | __sum16 csum = 0; |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 308 | |
| 309 | rcu_read_lock(); |
| 310 | afinfo = nf_get_afinfo(family); |
| 311 | if (afinfo) |
| 312 | csum = afinfo->checksum(skb, hook, dataoff, protocol); |
| 313 | rcu_read_unlock(); |
| 314 | return csum; |
| 315 | } |
| 316 | |
Patrick McHardy | d63a650 | 2008-03-20 15:15:53 +0100 | [diff] [blame] | 317 | static inline __sum16 |
| 318 | nf_checksum_partial(struct sk_buff *skb, unsigned int hook, |
| 319 | unsigned int dataoff, unsigned int len, |
| 320 | u_int8_t protocol, unsigned short family) |
| 321 | { |
| 322 | const struct nf_afinfo *afinfo; |
| 323 | __sum16 csum = 0; |
| 324 | |
| 325 | rcu_read_lock(); |
| 326 | afinfo = nf_get_afinfo(family); |
| 327 | if (afinfo) |
| 328 | csum = afinfo->checksum_partial(skb, hook, dataoff, len, |
| 329 | protocol); |
| 330 | rcu_read_unlock(); |
| 331 | return csum; |
| 332 | } |
| 333 | |
Joe Perches | a0f4ecf | 2013-09-26 14:48:15 -0700 | [diff] [blame] | 334 | int nf_register_afinfo(const struct nf_afinfo *afinfo); |
| 335 | void nf_unregister_afinfo(const struct nf_afinfo *afinfo); |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 336 | |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 337 | #include <net/flow.h> |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 338 | extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *); |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 339 | |
| 340 | static inline void |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 341 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 342 | { |
Patrick McHardy | 051578c | 2007-12-17 22:42:51 -0800 | [diff] [blame] | 343 | #ifdef CONFIG_NF_NAT_NEEDED |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 344 | void (*decodefn)(struct sk_buff *, struct flowi *); |
| 345 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 346 | rcu_read_lock(); |
| 347 | decodefn = rcu_dereference(nf_nat_decode_session_hook); |
| 348 | if (decodefn) |
| 349 | decodefn(skb, fl); |
| 350 | rcu_read_unlock(); |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 351 | #endif |
| 352 | } |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | #else /* !CONFIG_NETFILTER */ |
Arnd Bergmann | 008027c | 2015-10-09 20:45:42 +0200 | [diff] [blame] | 355 | static inline int |
| 356 | NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
| 357 | struct sk_buff *skb, struct net_device *in, struct net_device *out, |
| 358 | int (*okfn)(struct net *, struct sock *, struct sk_buff *), |
| 359 | bool cond) |
| 360 | { |
| 361 | return okfn(net, sk, skb); |
| 362 | } |
| 363 | |
| 364 | static inline int |
| 365 | NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, |
| 366 | struct sk_buff *skb, struct net_device *in, struct net_device *out, |
| 367 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
| 368 | { |
| 369 | return okfn(net, sk, skb); |
| 370 | } |
| 371 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 372 | static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net, |
| 373 | struct sock *sk, struct sk_buff *skb, |
| 374 | struct net_device *indev, struct net_device *outdev, |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 375 | int (*okfn)(struct net *, struct sock *, struct sk_buff *)) |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 376 | { |
Patrick McHardy | 9c92d34 | 2006-02-15 15:18:19 -0800 | [diff] [blame] | 377 | return 1; |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 378 | } |
David S. Miller | f53b61d | 2006-01-07 12:50:27 -0800 | [diff] [blame] | 379 | struct flowi; |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 380 | static inline void |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 381 | nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family) |
| 382 | { |
| 383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | #endif /*CONFIG_NETFILTER*/ |
| 385 | |
Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 386 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
Daniel Borkmann | 62da986 | 2015-09-03 01:26:07 +0200 | [diff] [blame] | 387 | #include <linux/netfilter/nf_conntrack_zones_common.h> |
| 388 | |
Patrick McHardy | 312a0c16 | 2013-07-28 22:54:08 +0200 | [diff] [blame] | 389 | extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu; |
Joe Perches | a0f4ecf | 2013-09-26 14:48:15 -0700 | [diff] [blame] | 390 | void nf_ct_attach(struct sk_buff *, const struct sk_buff *); |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 391 | extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu; |
Pablo Neira Ayuso | b7bd180 | 2015-09-30 22:53:44 +0100 | [diff] [blame] | 392 | #else |
| 393 | static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {} |
| 394 | #endif |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 395 | |
| 396 | struct nf_conn; |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 397 | enum ip_conntrack_info; |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 398 | struct nlattr; |
| 399 | |
Ken-ichirou MATSUZAWA | a4b4766 | 2015-10-05 11:47:13 +0900 | [diff] [blame] | 400 | struct nfnl_ct_hook { |
Ken-ichirou MATSUZAWA | 224a059 | 2015-10-05 11:49:56 +0900 | [diff] [blame] | 401 | struct nf_conn *(*get_ct)(const struct sk_buff *skb, |
Pablo Neira Ayuso | b7bd180 | 2015-09-30 22:53:44 +0100 | [diff] [blame] | 402 | enum ip_conntrack_info *ctinfo); |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 403 | size_t (*build_size)(const struct nf_conn *ct); |
Pablo Neira Ayuso | b7bd180 | 2015-09-30 22:53:44 +0100 | [diff] [blame] | 404 | int (*build)(struct sk_buff *skb, struct nf_conn *ct, |
| 405 | enum ip_conntrack_info ctinfo, |
| 406 | u_int16_t ct_attr, u_int16_t ct_info_attr); |
Pablo Neira Ayuso | 9cb0176 | 2012-06-07 12:13:39 +0200 | [diff] [blame] | 407 | int (*parse)(const struct nlattr *attr, struct nf_conn *ct); |
Pablo Neira Ayuso | bd07793 | 2013-08-07 18:13:20 +0200 | [diff] [blame] | 408 | int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct, |
| 409 | u32 portid, u32 report); |
Patrick McHardy | 41d73ec | 2013-08-27 08:50:12 +0200 | [diff] [blame] | 410 | void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct, |
| 411 | enum ip_conntrack_info ctinfo, s32 off); |
Pablo Neira Ayuso | d584a61 | 2012-06-20 20:52:31 +0200 | [diff] [blame] | 412 | }; |
Ken-ichirou MATSUZAWA | a4b4766 | 2015-10-05 11:47:13 +0900 | [diff] [blame] | 413 | extern struct nfnl_ct_hook __rcu *nfnl_ct_hook; |
Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 414 | |
Florian Westphal | e7c8899 | 2015-07-14 17:51:07 +0200 | [diff] [blame] | 415 | /** |
| 416 | * nf_skb_duplicated - TEE target has sent a packet |
| 417 | * |
| 418 | * When a xtables target sends a packet, the OUTPUT and POSTROUTING |
| 419 | * hooks are traversed again, i.e. nft and xtables are invoked recursively. |
| 420 | * |
| 421 | * This is used by xtables TEE target to prevent the duplicated skb from |
| 422 | * being duplicated again. |
| 423 | */ |
| 424 | DECLARE_PER_CPU(bool, nf_skb_duplicated); |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | #endif /*__LINUX_NETFILTER_H*/ |