]>
Commit | Line | Data |
---|---|---|
20d29d7a AB |
1 | #include <linux/etherdevice.h> |
2 | #include <linux/if_macvlan.h> | |
3 | #include <linux/interrupt.h> | |
4 | #include <linux/nsproxy.h> | |
5 | #include <linux/compat.h> | |
6 | #include <linux/if_tun.h> | |
7 | #include <linux/module.h> | |
8 | #include <linux/skbuff.h> | |
9 | #include <linux/cache.h> | |
10 | #include <linux/sched.h> | |
11 | #include <linux/types.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
20d29d7a AB |
13 | #include <linux/init.h> |
14 | #include <linux/wait.h> | |
15 | #include <linux/cdev.h> | |
16 | #include <linux/fs.h> | |
17 | ||
18 | #include <net/net_namespace.h> | |
19 | #include <net/rtnetlink.h> | |
20 | #include <net/sock.h> | |
b9fb9ee0 | 21 | #include <linux/virtio_net.h> |
20d29d7a AB |
22 | |
23 | /* | |
24 | * A macvtap queue is the central object of this driver, it connects | |
25 | * an open character device to a macvlan interface. There can be | |
26 | * multiple queues on one interface, which map back to queues | |
27 | * implemented in hardware on the underlying device. | |
28 | * | |
29 | * macvtap_proto is used to allocate queues through the sock allocation | |
30 | * mechanism. | |
31 | * | |
32 | * TODO: multiqueue support is currently not implemented, even though | |
33 | * macvtap is basically prepared for that. We will need to add this | |
34 | * here as well as in virtio-net and qemu to get line rate on 10gbit | |
35 | * adapters from a guest. | |
36 | */ | |
37 | struct macvtap_queue { | |
38 | struct sock sk; | |
39 | struct socket sock; | |
43815482 | 40 | struct socket_wq wq; |
55afbd08 | 41 | int vnet_hdr_sz; |
20d29d7a AB |
42 | struct macvlan_dev *vlan; |
43 | struct file *file; | |
b9fb9ee0 | 44 | unsigned int flags; |
20d29d7a AB |
45 | }; |
46 | ||
47 | static struct proto macvtap_proto = { | |
48 | .name = "macvtap", | |
49 | .owner = THIS_MODULE, | |
50 | .obj_size = sizeof (struct macvtap_queue), | |
51 | }; | |
52 | ||
53 | /* | |
54 | * Minor number matches netdev->ifindex, so need a potentially | |
55 | * large value. This also makes it possible to split the | |
56 | * tap functionality out again in the future by offering it | |
57 | * from other drivers besides macvtap. As long as every device | |
58 | * only has one tap, the interface numbers assure that the | |
59 | * device nodes are unique. | |
60 | */ | |
61 | static unsigned int macvtap_major; | |
62 | #define MACVTAP_NUM_DEVS 65536 | |
63 | static struct class *macvtap_class; | |
64 | static struct cdev macvtap_cdev; | |
65 | ||
501c774c AB |
66 | static const struct proto_ops macvtap_socket_ops; |
67 | ||
20d29d7a AB |
68 | /* |
69 | * RCU usage: | |
02df55d2 AB |
70 | * The macvtap_queue and the macvlan_dev are loosely coupled, the |
71 | * pointers from one to the other can only be read while rcu_read_lock | |
72 | * or macvtap_lock is held. | |
20d29d7a | 73 | * |
02df55d2 AB |
74 | * Both the file and the macvlan_dev hold a reference on the macvtap_queue |
75 | * through sock_hold(&q->sk). When the macvlan_dev goes away first, | |
76 | * q->vlan becomes inaccessible. When the files gets closed, | |
77 | * macvtap_get_queue() fails. | |
20d29d7a | 78 | * |
02df55d2 AB |
79 | * There may still be references to the struct sock inside of the |
80 | * queue from outbound SKBs, but these never reference back to the | |
81 | * file or the dev. The data structure is freed through __sk_free | |
82 | * when both our references and any pending SKBs are gone. | |
20d29d7a AB |
83 | */ |
84 | static DEFINE_SPINLOCK(macvtap_lock); | |
85 | ||
86 | /* | |
87 | * Choose the next free queue, for now there is only one | |
88 | */ | |
89 | static int macvtap_set_queue(struct net_device *dev, struct file *file, | |
90 | struct macvtap_queue *q) | |
91 | { | |
92 | struct macvlan_dev *vlan = netdev_priv(dev); | |
93 | int err = -EBUSY; | |
94 | ||
95 | spin_lock(&macvtap_lock); | |
96 | if (rcu_dereference(vlan->tap)) | |
97 | goto out; | |
98 | ||
99 | err = 0; | |
02df55d2 | 100 | rcu_assign_pointer(q->vlan, vlan); |
20d29d7a | 101 | rcu_assign_pointer(vlan->tap, q); |
02df55d2 | 102 | sock_hold(&q->sk); |
20d29d7a AB |
103 | |
104 | q->file = file; | |
02df55d2 | 105 | file->private_data = q; |
20d29d7a AB |
106 | |
107 | out: | |
108 | spin_unlock(&macvtap_lock); | |
109 | return err; | |
110 | } | |
111 | ||
112 | /* | |
02df55d2 AB |
113 | * The file owning the queue got closed, give up both |
114 | * the reference that the files holds as well as the | |
115 | * one from the macvlan_dev if that still exists. | |
20d29d7a AB |
116 | * |
117 | * Using the spinlock makes sure that we don't get | |
118 | * to the queue again after destroying it. | |
20d29d7a | 119 | */ |
02df55d2 | 120 | static void macvtap_put_queue(struct macvtap_queue *q) |
20d29d7a | 121 | { |
02df55d2 | 122 | struct macvlan_dev *vlan; |
20d29d7a AB |
123 | |
124 | spin_lock(&macvtap_lock); | |
02df55d2 AB |
125 | vlan = rcu_dereference(q->vlan); |
126 | if (vlan) { | |
127 | rcu_assign_pointer(vlan->tap, NULL); | |
128 | rcu_assign_pointer(q->vlan, NULL); | |
129 | sock_put(&q->sk); | |
20d29d7a AB |
130 | } |
131 | ||
20d29d7a AB |
132 | spin_unlock(&macvtap_lock); |
133 | ||
134 | synchronize_rcu(); | |
135 | sock_put(&q->sk); | |
136 | } | |
137 | ||
138 | /* | |
139 | * Since we only support one queue, just dereference the pointer. | |
140 | */ | |
141 | static struct macvtap_queue *macvtap_get_queue(struct net_device *dev, | |
142 | struct sk_buff *skb) | |
143 | { | |
144 | struct macvlan_dev *vlan = netdev_priv(dev); | |
145 | ||
146 | return rcu_dereference(vlan->tap); | |
147 | } | |
148 | ||
02df55d2 AB |
149 | /* |
150 | * The net_device is going away, give up the reference | |
151 | * that it holds on the queue (all the queues one day) | |
152 | * and safely set the pointer from the queues to NULL. | |
153 | */ | |
20d29d7a AB |
154 | static void macvtap_del_queues(struct net_device *dev) |
155 | { | |
156 | struct macvlan_dev *vlan = netdev_priv(dev); | |
564517e8 | 157 | struct macvtap_queue *q; |
02df55d2 AB |
158 | |
159 | spin_lock(&macvtap_lock); | |
160 | q = rcu_dereference(vlan->tap); | |
161 | if (!q) { | |
162 | spin_unlock(&macvtap_lock); | |
163 | return; | |
564517e8 | 164 | } |
20d29d7a | 165 | |
02df55d2 AB |
166 | rcu_assign_pointer(vlan->tap, NULL); |
167 | rcu_assign_pointer(q->vlan, NULL); | |
168 | spin_unlock(&macvtap_lock); | |
169 | ||
170 | synchronize_rcu(); | |
564517e8 | 171 | sock_put(&q->sk); |
20d29d7a AB |
172 | } |
173 | ||
174 | /* | |
175 | * Forward happens for data that gets sent from one macvlan | |
176 | * endpoint to another one in bridge mode. We just take | |
177 | * the skb and put it into the receive queue. | |
178 | */ | |
179 | static int macvtap_forward(struct net_device *dev, struct sk_buff *skb) | |
180 | { | |
181 | struct macvtap_queue *q = macvtap_get_queue(dev, skb); | |
182 | if (!q) | |
183 | return -ENOLINK; | |
184 | ||
185 | skb_queue_tail(&q->sk.sk_receive_queue, skb); | |
4a4771a5 | 186 | wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND); |
20d29d7a AB |
187 | return 0; |
188 | } | |
189 | ||
190 | /* | |
191 | * Receive is for data from the external interface (lowerdev), | |
192 | * in case of macvtap, we can treat that the same way as | |
193 | * forward, which macvlan cannot. | |
194 | */ | |
195 | static int macvtap_receive(struct sk_buff *skb) | |
196 | { | |
197 | skb_push(skb, ETH_HLEN); | |
198 | return macvtap_forward(skb->dev, skb); | |
199 | } | |
200 | ||
201 | static int macvtap_newlink(struct net *src_net, | |
202 | struct net_device *dev, | |
203 | struct nlattr *tb[], | |
204 | struct nlattr *data[]) | |
205 | { | |
206 | struct device *classdev; | |
207 | dev_t devt; | |
208 | int err; | |
209 | ||
210 | err = macvlan_common_newlink(src_net, dev, tb, data, | |
211 | macvtap_receive, macvtap_forward); | |
212 | if (err) | |
213 | goto out; | |
214 | ||
215 | devt = MKDEV(MAJOR(macvtap_major), dev->ifindex); | |
216 | ||
217 | classdev = device_create(macvtap_class, &dev->dev, devt, | |
218 | dev, "tap%d", dev->ifindex); | |
219 | if (IS_ERR(classdev)) { | |
220 | err = PTR_ERR(classdev); | |
221 | macvtap_del_queues(dev); | |
222 | } | |
223 | ||
224 | out: | |
225 | return err; | |
226 | } | |
227 | ||
228 | static void macvtap_dellink(struct net_device *dev, | |
229 | struct list_head *head) | |
230 | { | |
231 | device_destroy(macvtap_class, | |
232 | MKDEV(MAJOR(macvtap_major), dev->ifindex)); | |
233 | ||
234 | macvtap_del_queues(dev); | |
235 | macvlan_dellink(dev, head); | |
236 | } | |
237 | ||
238 | static struct rtnl_link_ops macvtap_link_ops __read_mostly = { | |
239 | .kind = "macvtap", | |
240 | .newlink = macvtap_newlink, | |
241 | .dellink = macvtap_dellink, | |
242 | }; | |
243 | ||
244 | ||
245 | static void macvtap_sock_write_space(struct sock *sk) | |
246 | { | |
43815482 ED |
247 | wait_queue_head_t *wqueue; |
248 | ||
20d29d7a AB |
249 | if (!sock_writeable(sk) || |
250 | !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) | |
251 | return; | |
252 | ||
43815482 ED |
253 | wqueue = sk_sleep(sk); |
254 | if (wqueue && waitqueue_active(wqueue)) | |
255 | wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND); | |
20d29d7a AB |
256 | } |
257 | ||
258 | static int macvtap_open(struct inode *inode, struct file *file) | |
259 | { | |
260 | struct net *net = current->nsproxy->net_ns; | |
261 | struct net_device *dev = dev_get_by_index(net, iminor(inode)); | |
262 | struct macvtap_queue *q; | |
263 | int err; | |
264 | ||
265 | err = -ENODEV; | |
266 | if (!dev) | |
267 | goto out; | |
268 | ||
269 | /* check if this is a macvtap device */ | |
270 | err = -EINVAL; | |
271 | if (dev->rtnl_link_ops != &macvtap_link_ops) | |
272 | goto out; | |
273 | ||
274 | err = -ENOMEM; | |
275 | q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, | |
276 | &macvtap_proto); | |
277 | if (!q) | |
278 | goto out; | |
279 | ||
43815482 ED |
280 | q->sock.wq = &q->wq; |
281 | init_waitqueue_head(&q->wq.wait); | |
20d29d7a AB |
282 | q->sock.type = SOCK_RAW; |
283 | q->sock.state = SS_CONNECTED; | |
501c774c AB |
284 | q->sock.file = file; |
285 | q->sock.ops = &macvtap_socket_ops; | |
20d29d7a | 286 | sock_init_data(&q->sock, &q->sk); |
20d29d7a | 287 | q->sk.sk_write_space = macvtap_sock_write_space; |
b9fb9ee0 | 288 | q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP; |
55afbd08 | 289 | q->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
20d29d7a AB |
290 | |
291 | err = macvtap_set_queue(dev, file, q); | |
292 | if (err) | |
293 | sock_put(&q->sk); | |
294 | ||
295 | out: | |
296 | if (dev) | |
297 | dev_put(dev); | |
298 | ||
299 | return err; | |
300 | } | |
301 | ||
302 | static int macvtap_release(struct inode *inode, struct file *file) | |
303 | { | |
02df55d2 AB |
304 | struct macvtap_queue *q = file->private_data; |
305 | macvtap_put_queue(q); | |
20d29d7a AB |
306 | return 0; |
307 | } | |
308 | ||
309 | static unsigned int macvtap_poll(struct file *file, poll_table * wait) | |
310 | { | |
02df55d2 | 311 | struct macvtap_queue *q = file->private_data; |
20d29d7a AB |
312 | unsigned int mask = POLLERR; |
313 | ||
314 | if (!q) | |
315 | goto out; | |
316 | ||
317 | mask = 0; | |
43815482 | 318 | poll_wait(file, &q->wq.wait, wait); |
20d29d7a AB |
319 | |
320 | if (!skb_queue_empty(&q->sk.sk_receive_queue)) | |
321 | mask |= POLLIN | POLLRDNORM; | |
322 | ||
323 | if (sock_writeable(&q->sk) || | |
324 | (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) && | |
325 | sock_writeable(&q->sk))) | |
326 | mask |= POLLOUT | POLLWRNORM; | |
327 | ||
328 | out: | |
20d29d7a AB |
329 | return mask; |
330 | } | |
331 | ||
b9fb9ee0 AB |
332 | static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad, |
333 | size_t len, size_t linear, | |
334 | int noblock, int *err) | |
335 | { | |
336 | struct sk_buff *skb; | |
337 | ||
338 | /* Under a page? Don't bother with paged skb. */ | |
339 | if (prepad + len < PAGE_SIZE || !linear) | |
340 | linear = len; | |
341 | ||
342 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, | |
343 | err); | |
344 | if (!skb) | |
345 | return NULL; | |
346 | ||
347 | skb_reserve(skb, prepad); | |
348 | skb_put(skb, linear); | |
349 | skb->data_len = len - linear; | |
350 | skb->len += len - linear; | |
351 | ||
352 | return skb; | |
353 | } | |
354 | ||
355 | /* | |
356 | * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should | |
357 | * be shared with the tun/tap driver. | |
358 | */ | |
359 | static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb, | |
360 | struct virtio_net_hdr *vnet_hdr) | |
361 | { | |
362 | unsigned short gso_type = 0; | |
363 | if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { | |
364 | switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { | |
365 | case VIRTIO_NET_HDR_GSO_TCPV4: | |
366 | gso_type = SKB_GSO_TCPV4; | |
367 | break; | |
368 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
369 | gso_type = SKB_GSO_TCPV6; | |
370 | break; | |
371 | case VIRTIO_NET_HDR_GSO_UDP: | |
372 | gso_type = SKB_GSO_UDP; | |
373 | break; | |
374 | default: | |
375 | return -EINVAL; | |
376 | } | |
377 | ||
378 | if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) | |
379 | gso_type |= SKB_GSO_TCP_ECN; | |
380 | ||
381 | if (vnet_hdr->gso_size == 0) | |
382 | return -EINVAL; | |
383 | } | |
384 | ||
385 | if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { | |
386 | if (!skb_partial_csum_set(skb, vnet_hdr->csum_start, | |
387 | vnet_hdr->csum_offset)) | |
388 | return -EINVAL; | |
389 | } | |
390 | ||
391 | if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { | |
392 | skb_shinfo(skb)->gso_size = vnet_hdr->gso_size; | |
393 | skb_shinfo(skb)->gso_type = gso_type; | |
394 | ||
395 | /* Header must be checked, and gso_segs computed. */ | |
396 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
397 | skb_shinfo(skb)->gso_segs = 0; | |
398 | } | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb, | |
403 | struct virtio_net_hdr *vnet_hdr) | |
404 | { | |
405 | memset(vnet_hdr, 0, sizeof(*vnet_hdr)); | |
406 | ||
407 | if (skb_is_gso(skb)) { | |
408 | struct skb_shared_info *sinfo = skb_shinfo(skb); | |
409 | ||
410 | /* This is a hint as to how much should be linear. */ | |
411 | vnet_hdr->hdr_len = skb_headlen(skb); | |
412 | vnet_hdr->gso_size = sinfo->gso_size; | |
413 | if (sinfo->gso_type & SKB_GSO_TCPV4) | |
414 | vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; | |
415 | else if (sinfo->gso_type & SKB_GSO_TCPV6) | |
416 | vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | |
417 | else if (sinfo->gso_type & SKB_GSO_UDP) | |
418 | vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; | |
419 | else | |
420 | BUG(); | |
421 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) | |
422 | vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
423 | } else | |
424 | vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
425 | ||
426 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
427 | vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
428 | vnet_hdr->csum_start = skb->csum_start - | |
429 | skb_headroom(skb); | |
430 | vnet_hdr->csum_offset = skb->csum_offset; | |
431 | } /* else everything is zero */ | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
436 | ||
20d29d7a AB |
437 | /* Get packet from user space buffer */ |
438 | static ssize_t macvtap_get_user(struct macvtap_queue *q, | |
439 | const struct iovec *iv, size_t count, | |
440 | int noblock) | |
441 | { | |
442 | struct sk_buff *skb; | |
02df55d2 | 443 | struct macvlan_dev *vlan; |
20d29d7a AB |
444 | size_t len = count; |
445 | int err; | |
b9fb9ee0 AB |
446 | struct virtio_net_hdr vnet_hdr = { 0 }; |
447 | int vnet_hdr_len = 0; | |
448 | ||
449 | if (q->flags & IFF_VNET_HDR) { | |
55afbd08 | 450 | vnet_hdr_len = q->vnet_hdr_sz; |
b9fb9ee0 AB |
451 | |
452 | err = -EINVAL; | |
453 | if ((len -= vnet_hdr_len) < 0) | |
454 | goto err; | |
455 | ||
456 | err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0, | |
55afbd08 | 457 | sizeof(vnet_hdr)); |
b9fb9ee0 AB |
458 | if (err < 0) |
459 | goto err; | |
460 | if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && | |
461 | vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 > | |
462 | vnet_hdr.hdr_len) | |
463 | vnet_hdr.hdr_len = vnet_hdr.csum_start + | |
464 | vnet_hdr.csum_offset + 2; | |
465 | err = -EINVAL; | |
466 | if (vnet_hdr.hdr_len > len) | |
467 | goto err; | |
468 | } | |
20d29d7a | 469 | |
b9fb9ee0 | 470 | err = -EINVAL; |
20d29d7a | 471 | if (unlikely(len < ETH_HLEN)) |
b9fb9ee0 | 472 | goto err; |
20d29d7a | 473 | |
b9fb9ee0 AB |
474 | skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len, vnet_hdr.hdr_len, |
475 | noblock, &err); | |
02df55d2 AB |
476 | if (!skb) |
477 | goto err; | |
20d29d7a | 478 | |
b9fb9ee0 | 479 | err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, len); |
02df55d2 | 480 | if (err) |
b9fb9ee0 | 481 | goto err_kfree; |
20d29d7a AB |
482 | |
483 | skb_set_network_header(skb, ETH_HLEN); | |
b9fb9ee0 AB |
484 | skb_reset_mac_header(skb); |
485 | skb->protocol = eth_hdr(skb)->h_proto; | |
486 | ||
487 | if (vnet_hdr_len) { | |
488 | err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr); | |
489 | if (err) | |
490 | goto err_kfree; | |
491 | } | |
492 | ||
02df55d2 AB |
493 | rcu_read_lock_bh(); |
494 | vlan = rcu_dereference(q->vlan); | |
495 | if (vlan) | |
496 | macvlan_start_xmit(skb, vlan->dev); | |
497 | else | |
498 | kfree_skb(skb); | |
499 | rcu_read_unlock_bh(); | |
20d29d7a AB |
500 | |
501 | return count; | |
02df55d2 | 502 | |
b9fb9ee0 AB |
503 | err_kfree: |
504 | kfree_skb(skb); | |
505 | ||
02df55d2 AB |
506 | err: |
507 | rcu_read_lock_bh(); | |
508 | vlan = rcu_dereference(q->vlan); | |
509 | if (vlan) | |
b9fb9ee0 | 510 | netdev_get_tx_queue(vlan->dev, 0)->tx_dropped++; |
02df55d2 AB |
511 | rcu_read_unlock_bh(); |
512 | ||
02df55d2 | 513 | return err; |
20d29d7a AB |
514 | } |
515 | ||
516 | static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv, | |
517 | unsigned long count, loff_t pos) | |
518 | { | |
519 | struct file *file = iocb->ki_filp; | |
520 | ssize_t result = -ENOLINK; | |
02df55d2 | 521 | struct macvtap_queue *q = file->private_data; |
20d29d7a AB |
522 | |
523 | result = macvtap_get_user(q, iv, iov_length(iv, count), | |
524 | file->f_flags & O_NONBLOCK); | |
20d29d7a AB |
525 | return result; |
526 | } | |
527 | ||
528 | /* Put packet to the user space buffer */ | |
529 | static ssize_t macvtap_put_user(struct macvtap_queue *q, | |
530 | const struct sk_buff *skb, | |
531 | const struct iovec *iv, int len) | |
532 | { | |
02df55d2 | 533 | struct macvlan_dev *vlan; |
20d29d7a | 534 | int ret; |
b9fb9ee0 AB |
535 | int vnet_hdr_len = 0; |
536 | ||
537 | if (q->flags & IFF_VNET_HDR) { | |
538 | struct virtio_net_hdr vnet_hdr; | |
55afbd08 | 539 | vnet_hdr_len = q->vnet_hdr_sz; |
b9fb9ee0 AB |
540 | if ((len -= vnet_hdr_len) < 0) |
541 | return -EINVAL; | |
542 | ||
543 | ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr); | |
544 | if (ret) | |
545 | return ret; | |
546 | ||
55afbd08 | 547 | if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr))) |
b9fb9ee0 AB |
548 | return -EFAULT; |
549 | } | |
20d29d7a AB |
550 | |
551 | len = min_t(int, skb->len, len); | |
552 | ||
b9fb9ee0 | 553 | ret = skb_copy_datagram_const_iovec(skb, 0, iv, vnet_hdr_len, len); |
20d29d7a | 554 | |
02df55d2 AB |
555 | rcu_read_lock_bh(); |
556 | vlan = rcu_dereference(q->vlan); | |
501c774c AB |
557 | if (vlan) |
558 | macvlan_count_rx(vlan, len, ret == 0, 0); | |
02df55d2 | 559 | rcu_read_unlock_bh(); |
20d29d7a | 560 | |
b9fb9ee0 | 561 | return ret ? ret : (len + vnet_hdr_len); |
20d29d7a AB |
562 | } |
563 | ||
501c774c AB |
564 | static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb, |
565 | const struct iovec *iv, unsigned long len, | |
566 | int noblock) | |
20d29d7a | 567 | { |
20d29d7a AB |
568 | DECLARE_WAITQUEUE(wait, current); |
569 | struct sk_buff *skb; | |
501c774c | 570 | ssize_t ret = 0; |
20d29d7a | 571 | |
4a4771a5 | 572 | add_wait_queue(sk_sleep(&q->sk), &wait); |
20d29d7a AB |
573 | while (len) { |
574 | current->state = TASK_INTERRUPTIBLE; | |
575 | ||
576 | /* Read frames from the queue */ | |
577 | skb = skb_dequeue(&q->sk.sk_receive_queue); | |
578 | if (!skb) { | |
501c774c | 579 | if (noblock) { |
20d29d7a AB |
580 | ret = -EAGAIN; |
581 | break; | |
582 | } | |
583 | if (signal_pending(current)) { | |
584 | ret = -ERESTARTSYS; | |
585 | break; | |
586 | } | |
587 | /* Nothing to read, let's sleep */ | |
588 | schedule(); | |
589 | continue; | |
590 | } | |
591 | ret = macvtap_put_user(q, skb, iv, len); | |
592 | kfree_skb(skb); | |
593 | break; | |
594 | } | |
595 | ||
596 | current->state = TASK_RUNNING; | |
4a4771a5 | 597 | remove_wait_queue(sk_sleep(&q->sk), &wait); |
501c774c AB |
598 | return ret; |
599 | } | |
600 | ||
601 | static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv, | |
602 | unsigned long count, loff_t pos) | |
603 | { | |
604 | struct file *file = iocb->ki_filp; | |
605 | struct macvtap_queue *q = file->private_data; | |
606 | ssize_t len, ret = 0; | |
20d29d7a | 607 | |
501c774c AB |
608 | len = iov_length(iv, count); |
609 | if (len < 0) { | |
610 | ret = -EINVAL; | |
611 | goto out; | |
612 | } | |
613 | ||
614 | ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK); | |
615 | ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */ | |
20d29d7a | 616 | out: |
20d29d7a AB |
617 | return ret; |
618 | } | |
619 | ||
620 | /* | |
621 | * provide compatibility with generic tun/tap interface | |
622 | */ | |
623 | static long macvtap_ioctl(struct file *file, unsigned int cmd, | |
624 | unsigned long arg) | |
625 | { | |
02df55d2 AB |
626 | struct macvtap_queue *q = file->private_data; |
627 | struct macvlan_dev *vlan; | |
20d29d7a AB |
628 | void __user *argp = (void __user *)arg; |
629 | struct ifreq __user *ifr = argp; | |
630 | unsigned int __user *up = argp; | |
631 | unsigned int u; | |
55afbd08 MT |
632 | int __user *sp = argp; |
633 | int s; | |
02df55d2 | 634 | int ret; |
20d29d7a AB |
635 | |
636 | switch (cmd) { | |
637 | case TUNSETIFF: | |
638 | /* ignore the name, just look at flags */ | |
639 | if (get_user(u, &ifr->ifr_flags)) | |
640 | return -EFAULT; | |
b9fb9ee0 AB |
641 | |
642 | ret = 0; | |
643 | if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP)) | |
644 | ret = -EINVAL; | |
645 | else | |
646 | q->flags = u; | |
647 | ||
648 | return ret; | |
20d29d7a AB |
649 | |
650 | case TUNGETIFF: | |
02df55d2 AB |
651 | rcu_read_lock_bh(); |
652 | vlan = rcu_dereference(q->vlan); | |
653 | if (vlan) | |
654 | dev_hold(vlan->dev); | |
655 | rcu_read_unlock_bh(); | |
656 | ||
657 | if (!vlan) | |
20d29d7a | 658 | return -ENOLINK; |
20d29d7a | 659 | |
02df55d2 | 660 | ret = 0; |
20d29d7a | 661 | if (copy_to_user(&ifr->ifr_name, q->vlan->dev->name, IFNAMSIZ) || |
b9fb9ee0 | 662 | put_user(q->flags, &ifr->ifr_flags)) |
02df55d2 AB |
663 | ret = -EFAULT; |
664 | dev_put(vlan->dev); | |
665 | return ret; | |
20d29d7a AB |
666 | |
667 | case TUNGETFEATURES: | |
b9fb9ee0 | 668 | if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR, up)) |
20d29d7a AB |
669 | return -EFAULT; |
670 | return 0; | |
671 | ||
672 | case TUNSETSNDBUF: | |
673 | if (get_user(u, up)) | |
674 | return -EFAULT; | |
675 | ||
20d29d7a | 676 | q->sk.sk_sndbuf = u; |
20d29d7a AB |
677 | return 0; |
678 | ||
55afbd08 MT |
679 | case TUNGETVNETHDRSZ: |
680 | s = q->vnet_hdr_sz; | |
681 | if (put_user(s, sp)) | |
682 | return -EFAULT; | |
683 | return 0; | |
684 | ||
685 | case TUNSETVNETHDRSZ: | |
686 | if (get_user(s, sp)) | |
687 | return -EFAULT; | |
688 | if (s < (int)sizeof(struct virtio_net_hdr)) | |
689 | return -EINVAL; | |
690 | ||
691 | q->vnet_hdr_sz = s; | |
692 | return 0; | |
693 | ||
20d29d7a AB |
694 | case TUNSETOFFLOAD: |
695 | /* let the user check for future flags */ | |
696 | if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 | | |
b9fb9ee0 | 697 | TUN_F_TSO_ECN | TUN_F_UFO)) |
20d29d7a AB |
698 | return -EINVAL; |
699 | ||
b9fb9ee0 AB |
700 | /* TODO: only accept frames with the features that |
701 | got enabled for forwarded frames */ | |
702 | if (!(q->flags & IFF_VNET_HDR)) | |
703 | return -EINVAL; | |
20d29d7a AB |
704 | return 0; |
705 | ||
706 | default: | |
707 | return -EINVAL; | |
708 | } | |
709 | } | |
710 | ||
711 | #ifdef CONFIG_COMPAT | |
712 | static long macvtap_compat_ioctl(struct file *file, unsigned int cmd, | |
713 | unsigned long arg) | |
714 | { | |
715 | return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); | |
716 | } | |
717 | #endif | |
718 | ||
719 | static const struct file_operations macvtap_fops = { | |
720 | .owner = THIS_MODULE, | |
721 | .open = macvtap_open, | |
722 | .release = macvtap_release, | |
723 | .aio_read = macvtap_aio_read, | |
724 | .aio_write = macvtap_aio_write, | |
725 | .poll = macvtap_poll, | |
726 | .llseek = no_llseek, | |
727 | .unlocked_ioctl = macvtap_ioctl, | |
728 | #ifdef CONFIG_COMPAT | |
729 | .compat_ioctl = macvtap_compat_ioctl, | |
730 | #endif | |
731 | }; | |
732 | ||
501c774c AB |
733 | static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock, |
734 | struct msghdr *m, size_t total_len) | |
735 | { | |
736 | struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock); | |
737 | return macvtap_get_user(q, m->msg_iov, total_len, | |
738 | m->msg_flags & MSG_DONTWAIT); | |
739 | } | |
740 | ||
741 | static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock, | |
742 | struct msghdr *m, size_t total_len, | |
743 | int flags) | |
744 | { | |
745 | struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock); | |
746 | int ret; | |
747 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) | |
748 | return -EINVAL; | |
749 | ret = macvtap_do_read(q, iocb, m->msg_iov, total_len, | |
750 | flags & MSG_DONTWAIT); | |
751 | if (ret > total_len) { | |
752 | m->msg_flags |= MSG_TRUNC; | |
753 | ret = flags & MSG_TRUNC ? ret : total_len; | |
754 | } | |
755 | return ret; | |
756 | } | |
757 | ||
758 | /* Ops structure to mimic raw sockets with tun */ | |
759 | static const struct proto_ops macvtap_socket_ops = { | |
760 | .sendmsg = macvtap_sendmsg, | |
761 | .recvmsg = macvtap_recvmsg, | |
762 | }; | |
763 | ||
764 | /* Get an underlying socket object from tun file. Returns error unless file is | |
765 | * attached to a device. The returned object works like a packet socket, it | |
766 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for | |
767 | * holding a reference to the file for as long as the socket is in use. */ | |
768 | struct socket *macvtap_get_socket(struct file *file) | |
769 | { | |
770 | struct macvtap_queue *q; | |
771 | if (file->f_op != &macvtap_fops) | |
772 | return ERR_PTR(-EINVAL); | |
773 | q = file->private_data; | |
774 | if (!q) | |
775 | return ERR_PTR(-EBADFD); | |
776 | return &q->sock; | |
777 | } | |
778 | EXPORT_SYMBOL_GPL(macvtap_get_socket); | |
779 | ||
20d29d7a AB |
780 | static int macvtap_init(void) |
781 | { | |
782 | int err; | |
783 | ||
784 | err = alloc_chrdev_region(&macvtap_major, 0, | |
785 | MACVTAP_NUM_DEVS, "macvtap"); | |
786 | if (err) | |
787 | goto out1; | |
788 | ||
789 | cdev_init(&macvtap_cdev, &macvtap_fops); | |
790 | err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS); | |
791 | if (err) | |
792 | goto out2; | |
793 | ||
794 | macvtap_class = class_create(THIS_MODULE, "macvtap"); | |
795 | if (IS_ERR(macvtap_class)) { | |
796 | err = PTR_ERR(macvtap_class); | |
797 | goto out3; | |
798 | } | |
799 | ||
800 | err = macvlan_link_register(&macvtap_link_ops); | |
801 | if (err) | |
802 | goto out4; | |
803 | ||
804 | return 0; | |
805 | ||
806 | out4: | |
807 | class_unregister(macvtap_class); | |
808 | out3: | |
809 | cdev_del(&macvtap_cdev); | |
810 | out2: | |
811 | unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS); | |
812 | out1: | |
813 | return err; | |
814 | } | |
815 | module_init(macvtap_init); | |
816 | ||
817 | static void macvtap_exit(void) | |
818 | { | |
819 | rtnl_link_unregister(&macvtap_link_ops); | |
820 | class_unregister(macvtap_class); | |
821 | cdev_del(&macvtap_cdev); | |
822 | unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS); | |
823 | } | |
824 | module_exit(macvtap_exit); | |
825 | ||
826 | MODULE_ALIAS_RTNL_LINK("macvtap"); | |
827 | MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>"); | |
828 | MODULE_LICENSE("GPL"); |