Commit | Line | Data |
---|---|---|
f229f6ce PM |
1 | /* |
2 | * Rusty Russell (C)2000 -- This code is GPL. | |
3 | * Patrick McHardy (c) 2006-2012 | |
4 | */ | |
5 | ||
f6ebe77f | 6 | #include <linux/kernel.h> |
5a0e3ad6 | 7 | #include <linux/slab.h> |
f6ebe77f HW |
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> | |
c737b7c4 | 13 | #include <linux/netfilter_bridge.h> |
bbd86b9f | 14 | #include <linux/seq_file.h> |
7a11b984 | 15 | #include <linux/rcupdate.h> |
f6ebe77f | 16 | #include <net/protocol.h> |
c01cd429 | 17 | #include <net/netfilter/nf_queue.h> |
7fee226a | 18 | #include <net/dst.h> |
f6ebe77f HW |
19 | |
20 | #include "nf_internals.h" | |
21 | ||
601e68e1 | 22 | /* |
0360ae41 FW |
23 | * Hook for nfnetlink_queue to register its queue handler. |
24 | * We do this so that most of the NFQUEUE code can be modular. | |
25 | * | |
26 | * Once the queue is registered it must reinject all packets it | |
27 | * receives, no matter what. | |
f6ebe77f | 28 | */ |
0360ae41 | 29 | static const struct nf_queue_handler __rcu *queue_handler __read_mostly; |
f6ebe77f | 30 | |
d72367b6 HW |
31 | /* return EBUSY when somebody else is registered, return EEXIST if the |
32 | * same handler is registered, return 0 in case of success. */ | |
0360ae41 | 33 | void nf_register_queue_handler(const struct nf_queue_handler *qh) |
601e68e1 | 34 | { |
0360ae41 FW |
35 | /* should never happen, we only have one queueing backend in kernel */ |
36 | WARN_ON(rcu_access_pointer(queue_handler)); | |
37 | rcu_assign_pointer(queue_handler, qh); | |
f6ebe77f HW |
38 | } |
39 | EXPORT_SYMBOL(nf_register_queue_handler); | |
40 | ||
41 | /* The caller must flush their queue before this */ | |
0360ae41 | 42 | void nf_unregister_queue_handler(void) |
f6ebe77f | 43 | { |
0360ae41 | 44 | RCU_INIT_POINTER(queue_handler, NULL); |
585426fd | 45 | synchronize_rcu(); |
f6ebe77f HW |
46 | } |
47 | EXPORT_SYMBOL(nf_unregister_queue_handler); | |
48 | ||
a5fedd43 | 49 | void nf_queue_entry_release_refs(struct nf_queue_entry *entry) |
daaa8be2 | 50 | { |
1d1de89b DM |
51 | struct nf_hook_state *state = &entry->state; |
52 | ||
daaa8be2 | 53 | /* Release those devices we held, or Alexey will kill me. */ |
1d1de89b DM |
54 | if (state->in) |
55 | dev_put(state->in); | |
56 | if (state->out) | |
57 | dev_put(state->out); | |
1c984f8a DM |
58 | if (state->sk) |
59 | sock_put(state->sk); | |
1109a90c | 60 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
daaa8be2 | 61 | if (entry->skb->nf_bridge) { |
c737b7c4 | 62 | struct net_device *physdev; |
daaa8be2 | 63 | |
c737b7c4 FW |
64 | physdev = nf_bridge_get_physindev(entry->skb); |
65 | if (physdev) | |
66 | dev_put(physdev); | |
67 | physdev = nf_bridge_get_physoutdev(entry->skb); | |
68 | if (physdev) | |
69 | dev_put(physdev); | |
daaa8be2 PM |
70 | } |
71 | #endif | |
daaa8be2 | 72 | } |
a5fedd43 | 73 | EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs); |
daaa8be2 | 74 | |
4bd60443 | 75 | /* Bump dev refs so they don't vanish while packet is out */ |
ed78d09d | 76 | void nf_queue_entry_get_refs(struct nf_queue_entry *entry) |
4bd60443 | 77 | { |
1d1de89b DM |
78 | struct nf_hook_state *state = &entry->state; |
79 | ||
1d1de89b DM |
80 | if (state->in) |
81 | dev_hold(state->in); | |
82 | if (state->out) | |
83 | dev_hold(state->out); | |
1c984f8a DM |
84 | if (state->sk) |
85 | sock_hold(state->sk); | |
1109a90c | 86 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
4bd60443 | 87 | if (entry->skb->nf_bridge) { |
4bd60443 FW |
88 | struct net_device *physdev; |
89 | ||
c737b7c4 | 90 | physdev = nf_bridge_get_physindev(entry->skb); |
4bd60443 FW |
91 | if (physdev) |
92 | dev_hold(physdev); | |
c737b7c4 | 93 | physdev = nf_bridge_get_physoutdev(entry->skb); |
4bd60443 FW |
94 | if (physdev) |
95 | dev_hold(physdev); | |
96 | } | |
97 | #endif | |
4bd60443 | 98 | } |
a5fedd43 | 99 | EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs); |
4bd60443 | 100 | |
2385eb0c | 101 | void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops) |
8405a8ff EB |
102 | { |
103 | const struct nf_queue_handler *qh; | |
8405a8ff | 104 | |
8405a8ff EB |
105 | rcu_read_lock(); |
106 | qh = rcu_dereference(queue_handler); | |
2385eb0c PNA |
107 | if (qh) |
108 | qh->nf_hook_drop(net, ops); | |
8405a8ff | 109 | rcu_read_unlock(); |
8405a8ff EB |
110 | } |
111 | ||
601e68e1 YH |
112 | /* |
113 | * Any packet that leaves via this function must come back | |
f6ebe77f HW |
114 | * through nf_reinject(). |
115 | */ | |
a5fedd43 | 116 | int nf_queue(struct sk_buff *skb, |
cfdfab31 DM |
117 | struct nf_hook_ops *elem, |
118 | struct nf_hook_state *state, | |
119 | unsigned int queuenum) | |
f6ebe77f | 120 | { |
f1585086 | 121 | int status = -ENOENT; |
daaa8be2 | 122 | struct nf_queue_entry *entry = NULL; |
1e796fda | 123 | const struct nf_afinfo *afinfo; |
e3ac5298 | 124 | const struct nf_queue_handler *qh; |
f6ebe77f | 125 | |
25985edc | 126 | /* QUEUE == DROP if no one is waiting, to be safe. */ |
585426fd YK |
127 | rcu_read_lock(); |
128 | ||
0360ae41 | 129 | qh = rcu_dereference(queue_handler); |
94b27cc3 FW |
130 | if (!qh) { |
131 | status = -ESRCH; | |
daaa8be2 | 132 | goto err_unlock; |
94b27cc3 | 133 | } |
f6ebe77f | 134 | |
cfdfab31 | 135 | afinfo = nf_get_afinfo(state->pf); |
daaa8be2 PM |
136 | if (!afinfo) |
137 | goto err_unlock; | |
bce8032e | 138 | |
02f014d8 | 139 | entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC); |
f1585086 FW |
140 | if (!entry) { |
141 | status = -ENOMEM; | |
daaa8be2 | 142 | goto err_unlock; |
f1585086 | 143 | } |
f6ebe77f | 144 | |
02f014d8 PM |
145 | *entry = (struct nf_queue_entry) { |
146 | .skb = skb, | |
1c15b677 | 147 | .elem = elem, |
1d1de89b | 148 | .state = *state, |
a5fedd43 | 149 | .size = sizeof(*entry) + afinfo->route_key_size, |
02f014d8 | 150 | }; |
f6ebe77f | 151 | |
ed78d09d | 152 | nf_queue_entry_get_refs(entry); |
7fee226a | 153 | skb_dst_force(skb); |
02f014d8 PM |
154 | afinfo->saveroute(skb, entry); |
155 | status = qh->outfn(entry, queuenum); | |
f6ebe77f | 156 | |
585426fd | 157 | rcu_read_unlock(); |
f6ebe77f HW |
158 | |
159 | if (status < 0) { | |
daaa8be2 PM |
160 | nf_queue_entry_release_refs(entry); |
161 | goto err; | |
f6ebe77f HW |
162 | } |
163 | ||
f1585086 | 164 | return 0; |
daaa8be2 PM |
165 | |
166 | err_unlock: | |
167 | rcu_read_unlock(); | |
168 | err: | |
daaa8be2 | 169 | kfree(entry); |
f1585086 | 170 | return status; |
f6ebe77f HW |
171 | } |
172 | ||
02f014d8 | 173 | void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) |
f6ebe77f | 174 | { |
02f014d8 | 175 | struct sk_buff *skb = entry->skb; |
2a6decfd | 176 | struct nf_hook_ops *elem = entry->elem; |
1e796fda | 177 | const struct nf_afinfo *afinfo; |
f1585086 | 178 | int err; |
f6ebe77f HW |
179 | |
180 | rcu_read_lock(); | |
181 | ||
daaa8be2 | 182 | nf_queue_entry_release_refs(entry); |
f6ebe77f | 183 | |
f6ebe77f | 184 | /* Continue traversal iff userspace said ok... */ |
7ceebfe4 FW |
185 | if (verdict == NF_REPEAT) |
186 | verdict = elem->hook(elem->priv, skb, &entry->state); | |
f6ebe77f | 187 | |
7a11b984 | 188 | if (verdict == NF_ACCEPT) { |
1d1de89b | 189 | afinfo = nf_get_afinfo(entry->state.pf); |
d815d90b | 190 | if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0) |
7a11b984 PM |
191 | verdict = NF_DROP; |
192 | } | |
193 | ||
1d1de89b | 194 | entry->state.thresh = INT_MIN; |
cfdfab31 | 195 | |
f6ebe77f HW |
196 | if (verdict == NF_ACCEPT) { |
197 | next_hook: | |
f307170d | 198 | verdict = nf_iterate(entry->state.hook_list, |
1d1de89b | 199 | skb, &entry->state, &elem); |
f6ebe77f HW |
200 | } |
201 | ||
202 | switch (verdict & NF_VERDICT_MASK) { | |
203 | case NF_ACCEPT: | |
3bc38712 | 204 | case NF_STOP: |
4b3d15ef | 205 | local_bh_disable(); |
0c4b51f0 | 206 | entry->state.okfn(entry->state.net, entry->state.sk, skb); |
4b3d15ef | 207 | local_bh_enable(); |
f6ebe77f | 208 | break; |
f6ebe77f | 209 | case NF_QUEUE: |
1d1de89b | 210 | err = nf_queue(skb, elem, &entry->state, |
cfdfab31 | 211 | verdict >> NF_VERDICT_QBITS); |
06cdb634 | 212 | if (err < 0) { |
94b27cc3 FW |
213 | if (err == -ESRCH && |
214 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) | |
215 | goto next_hook; | |
06cdb634 FW |
216 | kfree_skb(skb); |
217 | } | |
f6ebe77f | 218 | break; |
64507fdb | 219 | case NF_STOLEN: |
fad54440 | 220 | break; |
3bc38712 PM |
221 | default: |
222 | kfree_skb(skb); | |
f6ebe77f HW |
223 | } |
224 | rcu_read_unlock(); | |
02f014d8 | 225 | kfree(entry); |
f6ebe77f HW |
226 | } |
227 | EXPORT_SYMBOL(nf_reinject); |