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> | |
bbd86b9f | 13 | #include <linux/seq_file.h> |
7a11b984 | 14 | #include <linux/rcupdate.h> |
f6ebe77f | 15 | #include <net/protocol.h> |
c01cd429 | 16 | #include <net/netfilter/nf_queue.h> |
7fee226a | 17 | #include <net/dst.h> |
f6ebe77f HW |
18 | |
19 | #include "nf_internals.h" | |
20 | ||
601e68e1 | 21 | /* |
0360ae41 FW |
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. | |
f6ebe77f | 27 | */ |
0360ae41 | 28 | static const struct nf_queue_handler __rcu *queue_handler __read_mostly; |
f6ebe77f | 29 | |
d72367b6 HW |
30 | /* return EBUSY when somebody else is registered, return EEXIST if the |
31 | * same handler is registered, return 0 in case of success. */ | |
0360ae41 | 32 | void nf_register_queue_handler(const struct nf_queue_handler *qh) |
601e68e1 | 33 | { |
0360ae41 FW |
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); | |
f6ebe77f HW |
37 | } |
38 | EXPORT_SYMBOL(nf_register_queue_handler); | |
39 | ||
40 | /* The caller must flush their queue before this */ | |
0360ae41 | 41 | void nf_unregister_queue_handler(void) |
f6ebe77f | 42 | { |
0360ae41 | 43 | RCU_INIT_POINTER(queue_handler, NULL); |
585426fd | 44 | synchronize_rcu(); |
f6ebe77f HW |
45 | } |
46 | EXPORT_SYMBOL(nf_unregister_queue_handler); | |
47 | ||
a5fedd43 | 48 | void nf_queue_entry_release_refs(struct nf_queue_entry *entry) |
daaa8be2 | 49 | { |
1d1de89b DM |
50 | struct nf_hook_state *state = &entry->state; |
51 | ||
daaa8be2 | 52 | /* Release those devices we held, or Alexey will kill me. */ |
1d1de89b DM |
53 | if (state->in) |
54 | dev_put(state->in); | |
55 | if (state->out) | |
56 | dev_put(state->out); | |
1c984f8a DM |
57 | if (state->sk) |
58 | sock_put(state->sk); | |
1109a90c | 59 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
daaa8be2 PM |
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 | } | |
a5fedd43 | 72 | EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs); |
daaa8be2 | 73 | |
4bd60443 | 74 | /* Bump dev refs so they don't vanish while packet is out */ |
a5fedd43 | 75 | bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) |
4bd60443 | 76 | { |
1d1de89b DM |
77 | struct nf_hook_state *state = &entry->state; |
78 | ||
4bd60443 FW |
79 | if (!try_module_get(entry->elem->owner)) |
80 | return false; | |
81 | ||
1d1de89b DM |
82 | if (state->in) |
83 | dev_hold(state->in); | |
84 | if (state->out) | |
85 | dev_hold(state->out); | |
1c984f8a DM |
86 | if (state->sk) |
87 | sock_hold(state->sk); | |
1109a90c | 88 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
4bd60443 FW |
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 | } | |
a5fedd43 | 104 | EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs); |
4bd60443 | 105 | |
601e68e1 YH |
106 | /* |
107 | * Any packet that leaves via this function must come back | |
f6ebe77f HW |
108 | * through nf_reinject(). |
109 | */ | |
a5fedd43 | 110 | int nf_queue(struct sk_buff *skb, |
cfdfab31 DM |
111 | struct nf_hook_ops *elem, |
112 | struct nf_hook_state *state, | |
113 | unsigned int queuenum) | |
f6ebe77f | 114 | { |
f1585086 | 115 | int status = -ENOENT; |
daaa8be2 | 116 | struct nf_queue_entry *entry = NULL; |
1e796fda | 117 | const struct nf_afinfo *afinfo; |
e3ac5298 | 118 | const struct nf_queue_handler *qh; |
f6ebe77f | 119 | |
25985edc | 120 | /* QUEUE == DROP if no one is waiting, to be safe. */ |
585426fd YK |
121 | rcu_read_lock(); |
122 | ||
0360ae41 | 123 | qh = rcu_dereference(queue_handler); |
94b27cc3 FW |
124 | if (!qh) { |
125 | status = -ESRCH; | |
daaa8be2 | 126 | goto err_unlock; |
94b27cc3 | 127 | } |
f6ebe77f | 128 | |
cfdfab31 | 129 | afinfo = nf_get_afinfo(state->pf); |
daaa8be2 PM |
130 | if (!afinfo) |
131 | goto err_unlock; | |
bce8032e | 132 | |
02f014d8 | 133 | entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC); |
f1585086 FW |
134 | if (!entry) { |
135 | status = -ENOMEM; | |
daaa8be2 | 136 | goto err_unlock; |
f1585086 | 137 | } |
f6ebe77f | 138 | |
02f014d8 PM |
139 | *entry = (struct nf_queue_entry) { |
140 | .skb = skb, | |
1c15b677 | 141 | .elem = elem, |
1d1de89b | 142 | .state = *state, |
a5fedd43 | 143 | .size = sizeof(*entry) + afinfo->route_key_size, |
02f014d8 | 144 | }; |
f6ebe77f | 145 | |
4bd60443 | 146 | if (!nf_queue_entry_get_refs(entry)) { |
06cdb634 FW |
147 | status = -ECANCELED; |
148 | goto err_unlock; | |
f6ebe77f | 149 | } |
7fee226a | 150 | skb_dst_force(skb); |
02f014d8 PM |
151 | afinfo->saveroute(skb, entry); |
152 | status = qh->outfn(entry, queuenum); | |
f6ebe77f | 153 | |
585426fd | 154 | rcu_read_unlock(); |
f6ebe77f HW |
155 | |
156 | if (status < 0) { | |
daaa8be2 PM |
157 | nf_queue_entry_release_refs(entry); |
158 | goto err; | |
f6ebe77f HW |
159 | } |
160 | ||
f1585086 | 161 | return 0; |
daaa8be2 PM |
162 | |
163 | err_unlock: | |
164 | rcu_read_unlock(); | |
165 | err: | |
daaa8be2 | 166 | kfree(entry); |
f1585086 | 167 | return status; |
f6ebe77f HW |
168 | } |
169 | ||
02f014d8 | 170 | void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) |
f6ebe77f | 171 | { |
02f014d8 | 172 | struct sk_buff *skb = entry->skb; |
2a6decfd | 173 | struct nf_hook_ops *elem = entry->elem; |
1e796fda | 174 | const struct nf_afinfo *afinfo; |
f1585086 | 175 | int err; |
f6ebe77f HW |
176 | |
177 | rcu_read_lock(); | |
178 | ||
daaa8be2 | 179 | nf_queue_entry_release_refs(entry); |
f6ebe77f | 180 | |
f6ebe77f HW |
181 | /* Continue traversal iff userspace said ok... */ |
182 | if (verdict == NF_REPEAT) { | |
2a6decfd | 183 | elem = list_entry(elem->list.prev, struct nf_hook_ops, list); |
f6ebe77f HW |
184 | verdict = NF_ACCEPT; |
185 | } | |
186 | ||
7a11b984 | 187 | if (verdict == NF_ACCEPT) { |
1d1de89b | 188 | afinfo = nf_get_afinfo(entry->state.pf); |
02f014d8 | 189 | if (!afinfo || afinfo->reroute(skb, entry) < 0) |
7a11b984 PM |
190 | verdict = NF_DROP; |
191 | } | |
192 | ||
1d1de89b | 193 | entry->state.thresh = INT_MIN; |
cfdfab31 | 194 | |
f6ebe77f HW |
195 | if (verdict == NF_ACCEPT) { |
196 | next_hook: | |
1d1de89b DM |
197 | verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook], |
198 | skb, &entry->state, &elem); | |
f6ebe77f HW |
199 | } |
200 | ||
201 | switch (verdict & NF_VERDICT_MASK) { | |
202 | case NF_ACCEPT: | |
3bc38712 | 203 | case NF_STOP: |
4b3d15ef | 204 | local_bh_disable(); |
7026b1dd | 205 | entry->state.okfn(entry->state.sk, skb); |
4b3d15ef | 206 | local_bh_enable(); |
f6ebe77f | 207 | break; |
f6ebe77f | 208 | case NF_QUEUE: |
1d1de89b | 209 | err = nf_queue(skb, elem, &entry->state, |
cfdfab31 | 210 | verdict >> NF_VERDICT_QBITS); |
06cdb634 FW |
211 | if (err < 0) { |
212 | if (err == -ECANCELED) | |
213 | goto next_hook; | |
94b27cc3 FW |
214 | if (err == -ESRCH && |
215 | (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) | |
216 | goto next_hook; | |
06cdb634 FW |
217 | kfree_skb(skb); |
218 | } | |
f6ebe77f | 219 | break; |
64507fdb | 220 | case NF_STOLEN: |
fad54440 | 221 | break; |
3bc38712 PM |
222 | default: |
223 | kfree_skb(skb); | |
f6ebe77f HW |
224 | } |
225 | rcu_read_unlock(); | |
02f014d8 | 226 | kfree(entry); |
f6ebe77f HW |
227 | } |
228 | EXPORT_SYMBOL(nf_reinject); |