Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Rusty Russell (C)2000 -- This code is GPL. |
| 3 | * Patrick McHardy (c) 2006-2012 |
| 4 | */ |
| 5 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/proc_fs.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/netfilter.h> |
Harald Welte | bbd86b9f | 2005-08-09 20:23:11 -0700 | [diff] [blame] | 13 | #include <linux/seq_file.h> |
Patrick McHardy | 7a11b98 | 2006-02-27 13:03:24 -0800 | [diff] [blame] | 14 | #include <linux/rcupdate.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 15 | #include <net/protocol.h> |
Patrick McHardy | c01cd42 | 2007-12-05 01:24:48 -0800 | [diff] [blame] | 16 | #include <net/netfilter/nf_queue.h> |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 17 | #include <net/dst.h> |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 18 | |
| 19 | #include "nf_internals.h" |
| 20 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 21 | /* |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 22 | * Hook for nfnetlink_queue to register its queue handler. |
| 23 | * We do this so that most of the NFQUEUE code can be modular. |
| 24 | * |
| 25 | * Once the queue is registered it must reinject all packets it |
| 26 | * receives, no matter what. |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 27 | */ |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 28 | static const struct nf_queue_handler __rcu *queue_handler __read_mostly; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 29 | |
Harald Welte | d72367b | 2005-08-09 20:23:36 -0700 | [diff] [blame] | 30 | /* return EBUSY when somebody else is registered, return EEXIST if the |
| 31 | * same handler is registered, return 0 in case of success. */ |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 32 | void nf_register_queue_handler(const struct nf_queue_handler *qh) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 33 | { |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 34 | /* should never happen, we only have one queueing backend in kernel */ |
| 35 | WARN_ON(rcu_access_pointer(queue_handler)); |
| 36 | rcu_assign_pointer(queue_handler, qh); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 37 | } |
| 38 | EXPORT_SYMBOL(nf_register_queue_handler); |
| 39 | |
| 40 | /* The caller must flush their queue before this */ |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 41 | void nf_unregister_queue_handler(void) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 42 | { |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 43 | RCU_INIT_POINTER(queue_handler, NULL); |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 44 | synchronize_rcu(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 45 | } |
| 46 | EXPORT_SYMBOL(nf_unregister_queue_handler); |
| 47 | |
Florian Westphal | a5fedd43 | 2013-04-19 04:58:25 +0000 | [diff] [blame] | 48 | void nf_queue_entry_release_refs(struct nf_queue_entry *entry) |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 49 | { |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 50 | struct nf_hook_state *state = &entry->state; |
| 51 | |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 52 | /* Release those devices we held, or Alexey will kill me. */ |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 53 | if (state->in) |
| 54 | dev_put(state->in); |
| 55 | if (state->out) |
| 56 | dev_put(state->out); |
David Miller | 1c984f8 | 2015-04-05 22:19:00 -0400 | [diff] [blame^] | 57 | if (state->sk) |
| 58 | sock_put(state->sk); |
Pablo Neira Ayuso | 1109a90 | 2014-10-01 11:19:17 +0200 | [diff] [blame] | 59 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 60 | if (entry->skb->nf_bridge) { |
| 61 | struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge; |
| 62 | |
| 63 | if (nf_bridge->physindev) |
| 64 | dev_put(nf_bridge->physindev); |
| 65 | if (nf_bridge->physoutdev) |
| 66 | dev_put(nf_bridge->physoutdev); |
| 67 | } |
| 68 | #endif |
| 69 | /* Drop reference to owner of hook which queued us. */ |
| 70 | module_put(entry->elem->owner); |
| 71 | } |
Florian Westphal | a5fedd43 | 2013-04-19 04:58:25 +0000 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs); |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 73 | |
Florian Westphal | 4bd6044 | 2013-04-19 04:58:23 +0000 | [diff] [blame] | 74 | /* Bump dev refs so they don't vanish while packet is out */ |
Florian Westphal | a5fedd43 | 2013-04-19 04:58:25 +0000 | [diff] [blame] | 75 | bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) |
Florian Westphal | 4bd6044 | 2013-04-19 04:58:23 +0000 | [diff] [blame] | 76 | { |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 77 | struct nf_hook_state *state = &entry->state; |
| 78 | |
Florian Westphal | 4bd6044 | 2013-04-19 04:58:23 +0000 | [diff] [blame] | 79 | if (!try_module_get(entry->elem->owner)) |
| 80 | return false; |
| 81 | |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 82 | if (state->in) |
| 83 | dev_hold(state->in); |
| 84 | if (state->out) |
| 85 | dev_hold(state->out); |
David Miller | 1c984f8 | 2015-04-05 22:19:00 -0400 | [diff] [blame^] | 86 | if (state->sk) |
| 87 | sock_hold(state->sk); |
Pablo Neira Ayuso | 1109a90 | 2014-10-01 11:19:17 +0200 | [diff] [blame] | 88 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
Florian Westphal | 4bd6044 | 2013-04-19 04:58:23 +0000 | [diff] [blame] | 89 | if (entry->skb->nf_bridge) { |
| 90 | struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge; |
| 91 | struct net_device *physdev; |
| 92 | |
| 93 | physdev = nf_bridge->physindev; |
| 94 | if (physdev) |
| 95 | dev_hold(physdev); |
| 96 | physdev = nf_bridge->physoutdev; |
| 97 | if (physdev) |
| 98 | dev_hold(physdev); |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | return true; |
| 103 | } |
Florian Westphal | a5fedd43 | 2013-04-19 04:58:25 +0000 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs); |
Florian Westphal | 4bd6044 | 2013-04-19 04:58:23 +0000 | [diff] [blame] | 105 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 106 | /* |
| 107 | * Any packet that leaves via this function must come back |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 108 | * through nf_reinject(). |
| 109 | */ |
Florian Westphal | a5fedd43 | 2013-04-19 04:58:25 +0000 | [diff] [blame] | 110 | int nf_queue(struct sk_buff *skb, |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 111 | struct nf_hook_ops *elem, |
| 112 | struct nf_hook_state *state, |
| 113 | unsigned int queuenum) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 114 | { |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 115 | int status = -ENOENT; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 116 | struct nf_queue_entry *entry = NULL; |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 117 | const struct nf_afinfo *afinfo; |
Patrick McHardy | e3ac529 | 2007-12-05 01:23:57 -0800 | [diff] [blame] | 118 | const struct nf_queue_handler *qh; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 119 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 120 | /* QUEUE == DROP if no one is waiting, to be safe. */ |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 121 | rcu_read_lock(); |
| 122 | |
Florian Westphal | 0360ae4 | 2012-11-23 06:22:21 +0000 | [diff] [blame] | 123 | qh = rcu_dereference(queue_handler); |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 124 | if (!qh) { |
| 125 | status = -ESRCH; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 126 | goto err_unlock; |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 127 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 128 | |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 129 | afinfo = nf_get_afinfo(state->pf); |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 130 | if (!afinfo) |
| 131 | goto err_unlock; |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 132 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 133 | entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 134 | if (!entry) { |
| 135 | status = -ENOMEM; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 136 | goto err_unlock; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 137 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 138 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 139 | *entry = (struct nf_queue_entry) { |
| 140 | .skb = skb, |
Michael Wang | 1c15b67 | 2012-08-22 20:00:06 +0000 | [diff] [blame] | 141 | .elem = elem, |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 142 | .state = *state, |
Florian Westphal | a5fedd43 | 2013-04-19 04:58:25 +0000 | [diff] [blame] | 143 | .size = sizeof(*entry) + afinfo->route_key_size, |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 144 | }; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 145 | |
Florian Westphal | 4bd6044 | 2013-04-19 04:58:23 +0000 | [diff] [blame] | 146 | if (!nf_queue_entry_get_refs(entry)) { |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 147 | status = -ECANCELED; |
| 148 | goto err_unlock; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 149 | } |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 150 | skb_dst_force(skb); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 151 | afinfo->saveroute(skb, entry); |
| 152 | status = qh->outfn(entry, queuenum); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 153 | |
Yasuyuki Kozakai | 585426f | 2007-07-07 22:40:26 -0700 | [diff] [blame] | 154 | rcu_read_unlock(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 155 | |
| 156 | if (status < 0) { |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 157 | nf_queue_entry_release_refs(entry); |
| 158 | goto err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 161 | return 0; |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 162 | |
| 163 | err_unlock: |
| 164 | rcu_read_unlock(); |
| 165 | err: |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 166 | kfree(entry); |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 167 | return status; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 170 | void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 171 | { |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 172 | struct sk_buff *skb = entry->skb; |
Michael Wang | 2a6decf | 2012-08-22 19:59:57 +0000 | [diff] [blame] | 173 | struct nf_hook_ops *elem = entry->elem; |
Patrick McHardy | 1e796fd | 2007-12-17 22:42:27 -0800 | [diff] [blame] | 174 | const struct nf_afinfo *afinfo; |
Florian Westphal | f158508 | 2011-01-18 15:27:28 +0100 | [diff] [blame] | 175 | int err; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 176 | |
| 177 | rcu_read_lock(); |
| 178 | |
Patrick McHardy | daaa8be | 2007-12-05 01:27:19 -0800 | [diff] [blame] | 179 | nf_queue_entry_release_refs(entry); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 180 | |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 181 | /* Continue traversal iff userspace said ok... */ |
| 182 | if (verdict == NF_REPEAT) { |
Michael Wang | 2a6decf | 2012-08-22 19:59:57 +0000 | [diff] [blame] | 183 | elem = list_entry(elem->list.prev, struct nf_hook_ops, list); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 184 | verdict = NF_ACCEPT; |
| 185 | } |
| 186 | |
| 187 | if (verdict == NF_ACCEPT) { |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 188 | afinfo = nf_get_afinfo(entry->state.pf); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 189 | if (!afinfo || afinfo->reroute(skb, entry) < 0) |
Patrick McHardy | 7a11b98 | 2006-02-27 13:03:24 -0800 | [diff] [blame] | 190 | verdict = NF_DROP; |
| 191 | } |
| 192 | |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 193 | entry->state.thresh = INT_MIN; |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 194 | |
Patrick McHardy | 7a11b98 | 2006-02-27 13:03:24 -0800 | [diff] [blame] | 195 | if (verdict == NF_ACCEPT) { |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 196 | next_hook: |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 197 | verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook], |
| 198 | skb, &entry->state, &elem); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | switch (verdict & NF_VERDICT_MASK) { |
| 202 | case NF_ACCEPT: |
Patrick McHardy | 3bc3871 | 2006-07-24 22:52:47 -0700 | [diff] [blame] | 203 | case NF_STOP: |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 204 | local_bh_disable(); |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 205 | entry->state.okfn(skb); |
Patrick McHardy | 4b3d15e | 2007-12-05 01:27:02 -0800 | [diff] [blame] | 206 | local_bh_enable(); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 207 | break; |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 208 | case NF_QUEUE: |
David S. Miller | 1d1de89 | 2015-04-03 16:31:01 -0400 | [diff] [blame] | 209 | err = nf_queue(skb, elem, &entry->state, |
David S. Miller | cfdfab3 | 2015-04-03 16:23:58 -0400 | [diff] [blame] | 210 | verdict >> NF_VERDICT_QBITS); |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 211 | if (err < 0) { |
| 212 | if (err == -ECANCELED) |
| 213 | goto next_hook; |
Florian Westphal | 94b27cc | 2011-01-18 16:08:30 +0100 | [diff] [blame] | 214 | if (err == -ESRCH && |
| 215 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) |
| 216 | goto next_hook; |
Florian Westphal | 06cdb63 | 2011-01-18 15:28:38 +0100 | [diff] [blame] | 217 | kfree_skb(skb); |
| 218 | } |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 219 | break; |
Eric Dumazet | 64507fd | 2010-02-19 15:28:38 +0100 | [diff] [blame] | 220 | case NF_STOLEN: |
Julian Anastasov | fad5444 | 2011-08-05 00:36:28 +0000 | [diff] [blame] | 221 | break; |
Patrick McHardy | 3bc3871 | 2006-07-24 22:52:47 -0700 | [diff] [blame] | 222 | default: |
| 223 | kfree_skb(skb); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 224 | } |
| 225 | rcu_read_unlock(); |
Patrick McHardy | 02f014d | 2007-12-05 01:26:33 -0800 | [diff] [blame] | 226 | kfree(entry); |
Harald Welte | f6ebe77 | 2005-08-09 20:21:49 -0700 | [diff] [blame] | 227 | } |
| 228 | EXPORT_SYMBOL(nf_reinject); |