• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Rusty Russell (C)2000 -- This code is GPL.
3  * Patrick McHardy (c) 2006-2012
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
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>
13 #include <linux/seq_file.h>
14 #include <linux/rcupdate.h>
15 #include <net/protocol.h>
16 #include <net/netfilter/nf_queue.h>
17 #include <net/dst.h>
18 
19 #include "nf_internals.h"
20 
21 /*
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.
27  */
28 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
29 
30 /* return EBUSY when somebody else is registered, return EEXIST if the
31  * same handler is registered, return 0 in case of success. */
nf_register_queue_handler(const struct nf_queue_handler * qh)32 void nf_register_queue_handler(const struct nf_queue_handler *qh)
33 {
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);
37 }
38 EXPORT_SYMBOL(nf_register_queue_handler);
39 
40 /* The caller must flush their queue before this */
nf_unregister_queue_handler(void)41 void nf_unregister_queue_handler(void)
42 {
43 	RCU_INIT_POINTER(queue_handler, NULL);
44 	synchronize_rcu();
45 }
46 EXPORT_SYMBOL(nf_unregister_queue_handler);
47 
nf_queue_entry_release_refs(struct nf_queue_entry * entry)48 void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
49 {
50 	/* Release those devices we held, or Alexey will kill me. */
51 	if (entry->indev)
52 		dev_put(entry->indev);
53 	if (entry->outdev)
54 		dev_put(entry->outdev);
55 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
56 	if (entry->skb->nf_bridge) {
57 		struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
58 
59 		if (nf_bridge->physindev)
60 			dev_put(nf_bridge->physindev);
61 		if (nf_bridge->physoutdev)
62 			dev_put(nf_bridge->physoutdev);
63 	}
64 #endif
65 	/* Drop reference to owner of hook which queued us. */
66 	module_put(entry->elem->owner);
67 }
68 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
69 
70 /* Bump dev refs so they don't vanish while packet is out */
nf_queue_entry_get_refs(struct nf_queue_entry * entry)71 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
72 {
73 	if (!try_module_get(entry->elem->owner))
74 		return false;
75 
76 	if (entry->indev)
77 		dev_hold(entry->indev);
78 	if (entry->outdev)
79 		dev_hold(entry->outdev);
80 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
81 	if (entry->skb->nf_bridge) {
82 		struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
83 		struct net_device *physdev;
84 
85 		physdev = nf_bridge->physindev;
86 		if (physdev)
87 			dev_hold(physdev);
88 		physdev = nf_bridge->physoutdev;
89 		if (physdev)
90 			dev_hold(physdev);
91 	}
92 #endif
93 
94 	return true;
95 }
96 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
97 
nf_queue_nf_hook_drop(struct nf_hook_ops * ops)98 void nf_queue_nf_hook_drop(struct nf_hook_ops *ops)
99 {
100 	const struct nf_queue_handler *qh;
101 	struct net *net;
102 
103 	rtnl_lock();
104 	rcu_read_lock();
105 	qh = rcu_dereference(queue_handler);
106 	if (qh) {
107 		for_each_net(net) {
108 			qh->nf_hook_drop(net, ops);
109 		}
110 	}
111 	rcu_read_unlock();
112 	rtnl_unlock();
113 }
114 
115 /*
116  * Any packet that leaves via this function must come back
117  * through nf_reinject().
118  */
nf_queue(struct sk_buff * skb,struct nf_hook_ops * elem,u_int8_t pf,unsigned int hook,struct net_device * indev,struct net_device * outdev,int (* okfn)(struct sk_buff *),unsigned int queuenum)119 int nf_queue(struct sk_buff *skb,
120 		      struct nf_hook_ops *elem,
121 		      u_int8_t pf, unsigned int hook,
122 		      struct net_device *indev,
123 		      struct net_device *outdev,
124 		      int (*okfn)(struct sk_buff *),
125 		      unsigned int queuenum)
126 {
127 	int status = -ENOENT;
128 	struct nf_queue_entry *entry = NULL;
129 	const struct nf_afinfo *afinfo;
130 	const struct nf_queue_handler *qh;
131 
132 	/* QUEUE == DROP if no one is waiting, to be safe. */
133 	rcu_read_lock();
134 
135 	qh = rcu_dereference(queue_handler);
136 	if (!qh) {
137 		status = -ESRCH;
138 		goto err_unlock;
139 	}
140 
141 	afinfo = nf_get_afinfo(pf);
142 	if (!afinfo)
143 		goto err_unlock;
144 
145 	entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
146 	if (!entry) {
147 		status = -ENOMEM;
148 		goto err_unlock;
149 	}
150 
151 	*entry = (struct nf_queue_entry) {
152 		.skb	= skb,
153 		.elem	= elem,
154 		.pf	= pf,
155 		.hook	= hook,
156 		.indev	= indev,
157 		.outdev	= outdev,
158 		.okfn	= okfn,
159 		.size	= sizeof(*entry) + afinfo->route_key_size,
160 	};
161 
162 	if (!nf_queue_entry_get_refs(entry)) {
163 		status = -ECANCELED;
164 		goto err_unlock;
165 	}
166 	skb_dst_force(skb);
167 	afinfo->saveroute(skb, entry);
168 	status = qh->outfn(entry, queuenum);
169 
170 	rcu_read_unlock();
171 
172 	if (status < 0) {
173 		nf_queue_entry_release_refs(entry);
174 		goto err;
175 	}
176 
177 	return 0;
178 
179 err_unlock:
180 	rcu_read_unlock();
181 err:
182 	kfree(entry);
183 	return status;
184 }
185 
nf_reinject(struct nf_queue_entry * entry,unsigned int verdict)186 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
187 {
188 	struct sk_buff *skb = entry->skb;
189 	struct nf_hook_ops *elem = entry->elem;
190 	const struct nf_afinfo *afinfo;
191 	int err;
192 
193 	rcu_read_lock();
194 
195 	nf_queue_entry_release_refs(entry);
196 
197 	/* Continue traversal iff userspace said ok... */
198 	if (verdict == NF_REPEAT) {
199 		elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
200 		verdict = NF_ACCEPT;
201 	}
202 
203 	if (verdict == NF_ACCEPT) {
204 		afinfo = nf_get_afinfo(entry->pf);
205 		if (!afinfo || afinfo->reroute(skb, entry) < 0)
206 			verdict = NF_DROP;
207 	}
208 
209 	if (verdict == NF_ACCEPT) {
210 	next_hook:
211 		verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
212 				     skb, entry->hook,
213 				     entry->indev, entry->outdev, &elem,
214 				     entry->okfn, INT_MIN);
215 	}
216 
217 	switch (verdict & NF_VERDICT_MASK) {
218 	case NF_ACCEPT:
219 	case NF_STOP:
220 		local_bh_disable();
221 		entry->okfn(skb);
222 		local_bh_enable();
223 		break;
224 	case NF_QUEUE:
225 		err = nf_queue(skb, elem, entry->pf, entry->hook,
226 				entry->indev, entry->outdev, entry->okfn,
227 				verdict >> NF_VERDICT_QBITS);
228 		if (err < 0) {
229 			if (err == -ECANCELED)
230 				goto next_hook;
231 			if (err == -ESRCH &&
232 			   (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
233 				goto next_hook;
234 			kfree_skb(skb);
235 		}
236 		break;
237 	case NF_STOLEN:
238 		break;
239 	default:
240 		kfree_skb(skb);
241 	}
242 	rcu_read_unlock();
243 	kfree(entry);
244 }
245 EXPORT_SYMBOL(nf_reinject);
246