blob: 3350f1d3c9aa4840e0dad93e8cb395ea09345f3b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * PACKET - implements raw packet sockets.
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
11 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090012 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090035 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070037 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090040 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070041 * and packet_mreq.
Johann Baudy69e3c752009-05-18 22:11:22 -070042 * Johann Baudy : Added TX RING.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
48 *
49 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080053#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/fcntl.h>
55#include <linux/socket.h>
56#include <linux/in.h>
57#include <linux/inet.h>
58#include <linux/netdevice.h>
59#include <linux/if_packet.h>
60#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080061#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080064#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/ip.h>
67#include <net/protocol.h>
68#include <linux/skbuff.h>
69#include <net/sock.h>
70#include <linux/errno.h>
71#include <linux/timer.h>
72#include <asm/system.h>
73#include <asm/uaccess.h>
74#include <asm/ioctls.h>
75#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040076#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <asm/io.h>
78#include <linux/proc_fs.h>
79#include <linux/seq_file.h>
80#include <linux/poll.h>
81#include <linux/module.h>
82#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080083#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070084#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080085#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000086#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070087#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#ifdef CONFIG_INET
90#include <net/inet_common.h>
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 Assumptions:
95 - if device has no dev->hard_header routine, it adds and removes ll header
96 inside itself. In this case ll header is invisible outside of device,
97 but higher levels still should reserve dev->hard_header_len.
98 Some devices are enough clever to reallocate skb, when header
99 will not fit to reserved space (tunnel), another ones are silly
100 (PPP).
101 - packet socket receives packets with pulled ll header,
102 so that SOCK_RAW should push it back.
103
104On receive:
105-----------
106
107Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700108 mac_header -> ll header
109 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700112 mac_header -> ll header
113 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700116 mac_header -> UNKNOWN position. It is very likely, that it points to ll
117 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900118 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700119 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700122 mac_header -> data. ll header is still not built!
123 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125Resume
126 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
127
128
129On transmit:
130------------
131
132dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700133 mac_header -> ll header
134 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700137 mac_header -> data
138 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 We should set nh.raw on output to correct posistion,
141 packet classifier depends on it.
142 */
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Private packet socket structures. */
145
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000146struct packet_mclist {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct packet_mclist *next;
148 int ifindex;
149 int count;
150 unsigned short type;
151 unsigned short alen;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700152 unsigned char addr[MAX_ADDR_LEN];
153};
154/* identical to struct packet_mreq except it has
155 * a longer address field.
156 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000157struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700158 int mr_ifindex;
159 unsigned short mr_type;
160 unsigned short mr_alen;
161 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
David S. Millera2efcfa2007-05-29 13:12:50 -0700163
Johann Baudy69e3c752009-05-18 22:11:22 -0700164static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
165 int closing, int tx_ring);
166
Neil Horman0e3125c2010-11-16 10:26:47 -0800167struct pgv {
168 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -0800169};
170
Johann Baudy69e3c752009-05-18 22:11:22 -0700171struct packet_ring_buffer {
Neil Horman0e3125c2010-11-16 10:26:47 -0800172 struct pgv *pg_vec;
Johann Baudy69e3c752009-05-18 22:11:22 -0700173 unsigned int head;
174 unsigned int frames_per_block;
175 unsigned int frame_size;
176 unsigned int frame_max;
177
178 unsigned int pg_vec_order;
179 unsigned int pg_vec_pages;
180 unsigned int pg_vec_len;
181
182 atomic_t pending;
183};
184
185struct packet_sock;
186static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188static void packet_flush_mclist(struct sock *sk);
189
David S. Millerdc99f602011-07-05 01:45:05 -0700190struct packet_fanout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191struct packet_sock {
192 /* struct sock has to be the first member of packet_sock */
193 struct sock sk;
David S. Millerdc99f602011-07-05 01:45:05 -0700194 struct packet_fanout *fanout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct tpacket_stats stats;
Johann Baudy69e3c752009-05-18 22:11:22 -0700196 struct packet_ring_buffer rx_ring;
197 struct packet_ring_buffer tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int copy_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spinlock_t bind_lock;
Herbert Xu905db442009-01-30 14:12:06 -0800200 struct mutex pg_vec_lock;
Herbert Xu8dc41942007-02-04 23:31:32 -0800201 unsigned int running:1, /* prot_hook is attached*/
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700202 auxdata:1,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -0800203 origdev:1,
204 has_vnet_hdr:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int ifindex; /* bound device */
Al Viro0e11c912006-11-08 00:26:29 -0800206 __be16 num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct packet_mclist *mclist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 atomic_t mapped;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700209 enum tpacket_versions tp_version;
210 unsigned int tp_hdrlen;
Patrick McHardy8913336a2008-07-18 18:05:19 -0700211 unsigned int tp_reserve;
Johann Baudy69e3c752009-05-18 22:11:22 -0700212 unsigned int tp_loss:1;
Scott McMillan614f60f2010-06-02 05:53:56 -0700213 unsigned int tp_tstamp;
Eric Dumazet94b05952009-10-16 04:02:20 +0000214 struct packet_type prot_hook ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
David S. Millerdc99f602011-07-05 01:45:05 -0700217#define PACKET_FANOUT_MAX 256
218
219struct packet_fanout {
220#ifdef CONFIG_NET_NS
221 struct net *net;
222#endif
223 unsigned int num_members;
224 u16 id;
225 u8 type;
226 u8 pad;
227 atomic_t rr_cur;
228 struct list_head list;
229 struct sock *arr[PACKET_FANOUT_MAX];
230 spinlock_t lock;
231 atomic_t sk_ref;
232 struct packet_type prot_hook ____cacheline_aligned_in_smp;
233};
234
Herbert Xuffbc6112007-02-04 23:33:10 -0800235struct packet_skb_cb {
236 unsigned int origlen;
237 union {
238 struct sockaddr_pkt pkt;
239 struct sockaddr_ll ll;
240 } sa;
241};
242
243#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800244
David S. Millerce06b032011-07-04 01:44:29 -0700245static inline struct packet_sock *pkt_sk(struct sock *sk)
246{
247 return (struct packet_sock *)sk;
248}
249
David S. Millerdc99f602011-07-05 01:45:05 -0700250static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
251static void __fanout_link(struct sock *sk, struct packet_sock *po);
252
David S. Millerce06b032011-07-04 01:44:29 -0700253/* register_prot_hook must be invoked with the po->bind_lock held,
254 * or from a context in which asynchronous accesses to the packet
255 * socket is not possible (packet_create()).
256 */
257static void register_prot_hook(struct sock *sk)
258{
259 struct packet_sock *po = pkt_sk(sk);
260 if (!po->running) {
David S. Millerdc99f602011-07-05 01:45:05 -0700261 if (po->fanout)
262 __fanout_link(sk, po);
263 else
264 dev_add_pack(&po->prot_hook);
David S. Millerce06b032011-07-04 01:44:29 -0700265 sock_hold(sk);
266 po->running = 1;
267 }
268}
269
270/* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
271 * held. If the sync parameter is true, we will temporarily drop
272 * the po->bind_lock and do a synchronize_net to make sure no
273 * asynchronous packet processing paths still refer to the elements
274 * of po->prot_hook. If the sync parameter is false, it is the
275 * callers responsibility to take care of this.
276 */
277static void __unregister_prot_hook(struct sock *sk, bool sync)
278{
279 struct packet_sock *po = pkt_sk(sk);
280
281 po->running = 0;
David S. Millerdc99f602011-07-05 01:45:05 -0700282 if (po->fanout)
283 __fanout_unlink(sk, po);
284 else
285 __dev_remove_pack(&po->prot_hook);
David S. Millerce06b032011-07-04 01:44:29 -0700286 __sock_put(sk);
287
288 if (sync) {
289 spin_unlock(&po->bind_lock);
290 synchronize_net();
291 spin_lock(&po->bind_lock);
292 }
293}
294
295static void unregister_prot_hook(struct sock *sk, bool sync)
296{
297 struct packet_sock *po = pkt_sk(sk);
298
299 if (po->running)
300 __unregister_prot_hook(sk, sync);
301}
302
Changli Gaof6dafa92010-12-07 04:26:16 +0000303static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000304{
305 if (is_vmalloc_addr(addr))
306 return vmalloc_to_page(addr);
307 return virt_to_page(addr);
308}
309
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700310static void __packet_set_status(struct packet_sock *po, void *frame, int status)
311{
312 union {
313 struct tpacket_hdr *h1;
314 struct tpacket2_hdr *h2;
315 void *raw;
316 } h;
317
318 h.raw = frame;
319 switch (po->tp_version) {
320 case TPACKET_V1:
321 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000322 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700323 break;
324 case TPACKET_V2:
325 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000326 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700327 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700328 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000329 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700330 BUG();
331 }
332
333 smp_wmb();
334}
335
336static int __packet_get_status(struct packet_sock *po, void *frame)
337{
338 union {
339 struct tpacket_hdr *h1;
340 struct tpacket2_hdr *h2;
341 void *raw;
342 } h;
343
344 smp_rmb();
345
346 h.raw = frame;
347 switch (po->tp_version) {
348 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000349 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700350 return h.h1->tp_status;
351 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000352 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700353 return h.h2->tp_status;
354 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000355 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700356 BUG();
357 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
Johann Baudy69e3c752009-05-18 22:11:22 -0700360
361static void *packet_lookup_frame(struct packet_sock *po,
362 struct packet_ring_buffer *rb,
363 unsigned int position,
364 int status)
365{
366 unsigned int pg_vec_pos, frame_offset;
367 union {
368 struct tpacket_hdr *h1;
369 struct tpacket2_hdr *h2;
370 void *raw;
371 } h;
372
373 pg_vec_pos = position / rb->frames_per_block;
374 frame_offset = position % rb->frames_per_block;
375
Neil Horman0e3125c2010-11-16 10:26:47 -0800376 h.raw = rb->pg_vec[pg_vec_pos].buffer +
377 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700378
379 if (status != __packet_get_status(po, h.raw))
380 return NULL;
381
382 return h.raw;
383}
384
385static inline void *packet_current_frame(struct packet_sock *po,
386 struct packet_ring_buffer *rb,
387 int status)
388{
389 return packet_lookup_frame(po, rb, rb->head, status);
390}
391
392static inline void *packet_previous_frame(struct packet_sock *po,
393 struct packet_ring_buffer *rb,
394 int status)
395{
396 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
397 return packet_lookup_frame(po, rb, previous, status);
398}
399
400static inline void packet_increment_head(struct packet_ring_buffer *buff)
401{
402 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static void packet_sock_destruct(struct sock *sk)
406{
Richard Cochraned85b562010-04-07 22:41:28 +0000407 skb_queue_purge(&sk->sk_error_queue);
408
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700409 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
410 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000413 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return;
415 }
416
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800417 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
David S. Millerdc99f602011-07-05 01:45:05 -0700420static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
421{
422 int x = atomic_read(&f->rr_cur) + 1;
423
424 if (x >= num)
425 x = 0;
426
427 return x;
428}
429
430static struct sock *fanout_demux_hash(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
431{
432 u32 idx, hash = skb->rxhash;
433
434 idx = ((u64)hash * num) >> 32;
435
436 return f->arr[idx];
437}
438
439static struct sock *fanout_demux_lb(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
440{
441 int cur, old;
442
443 cur = atomic_read(&f->rr_cur);
444 while ((old = atomic_cmpxchg(&f->rr_cur, cur,
445 fanout_rr_next(f, num))) != cur)
446 cur = old;
447 return f->arr[cur];
448}
449
450static int packet_rcv_fanout_hash(struct sk_buff *skb, struct net_device *dev,
451 struct packet_type *pt, struct net_device *orig_dev)
452{
453 struct packet_fanout *f = pt->af_packet_priv;
454 unsigned int num = f->num_members;
455 struct packet_sock *po;
456 struct sock *sk;
457
458 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
459 !num) {
460 kfree_skb(skb);
461 return 0;
462 }
463
464 skb_get_rxhash(skb);
465
466 sk = fanout_demux_hash(f, skb, num);
467 po = pkt_sk(sk);
468
469 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
470}
471
472static int packet_rcv_fanout_lb(struct sk_buff *skb, struct net_device *dev,
473 struct packet_type *pt, struct net_device *orig_dev)
474{
475 struct packet_fanout *f = pt->af_packet_priv;
476 unsigned int num = f->num_members;
477 struct packet_sock *po;
478 struct sock *sk;
479
480 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
481 !num) {
482 kfree_skb(skb);
483 return 0;
484 }
485
486 sk = fanout_demux_lb(f, skb, num);
487 po = pkt_sk(sk);
488
489 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
490}
491
492static DEFINE_MUTEX(fanout_mutex);
493static LIST_HEAD(fanout_list);
494
495static void __fanout_link(struct sock *sk, struct packet_sock *po)
496{
497 struct packet_fanout *f = po->fanout;
498
499 spin_lock(&f->lock);
500 f->arr[f->num_members] = sk;
501 smp_wmb();
502 f->num_members++;
503 spin_unlock(&f->lock);
504}
505
506static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
507{
508 struct packet_fanout *f = po->fanout;
509 int i;
510
511 spin_lock(&f->lock);
512 for (i = 0; i < f->num_members; i++) {
513 if (f->arr[i] == sk)
514 break;
515 }
516 BUG_ON(i >= f->num_members);
517 f->arr[i] = f->arr[f->num_members - 1];
518 f->num_members--;
519 spin_unlock(&f->lock);
520}
521
522static int fanout_add(struct sock *sk, u16 id, u8 type)
523{
524 struct packet_sock *po = pkt_sk(sk);
525 struct packet_fanout *f, *match;
526 int err;
527
528 switch (type) {
529 case PACKET_FANOUT_HASH:
530 case PACKET_FANOUT_LB:
531 break;
532 default:
533 return -EINVAL;
534 }
535
536 if (!po->running)
537 return -EINVAL;
538
539 if (po->fanout)
540 return -EALREADY;
541
542 mutex_lock(&fanout_mutex);
543 match = NULL;
544 list_for_each_entry(f, &fanout_list, list) {
545 if (f->id == id &&
546 read_pnet(&f->net) == sock_net(sk)) {
547 match = f;
548 break;
549 }
550 }
551 if (!match) {
552 match = kzalloc(sizeof(*match), GFP_KERNEL);
553 if (match) {
554 write_pnet(&match->net, sock_net(sk));
555 match->id = id;
556 match->type = type;
557 atomic_set(&match->rr_cur, 0);
558 INIT_LIST_HEAD(&match->list);
559 spin_lock_init(&match->lock);
560 atomic_set(&match->sk_ref, 0);
561 match->prot_hook.type = po->prot_hook.type;
562 match->prot_hook.dev = po->prot_hook.dev;
563 switch (type) {
564 case PACKET_FANOUT_HASH:
565 match->prot_hook.func = packet_rcv_fanout_hash;
566 break;
567 case PACKET_FANOUT_LB:
568 match->prot_hook.func = packet_rcv_fanout_lb;
569 break;
570 }
571 match->prot_hook.af_packet_priv = match;
572 dev_add_pack(&match->prot_hook);
573 list_add(&match->list, &fanout_list);
574 }
575 }
576 err = -ENOMEM;
577 if (match) {
578 err = -EINVAL;
579 if (match->type == type &&
580 match->prot_hook.type == po->prot_hook.type &&
581 match->prot_hook.dev == po->prot_hook.dev) {
582 err = -ENOSPC;
583 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
584 __dev_remove_pack(&po->prot_hook);
585 po->fanout = match;
586 atomic_inc(&match->sk_ref);
587 __fanout_link(sk, po);
588 err = 0;
589 }
590 }
591 }
592 mutex_unlock(&fanout_mutex);
593 return err;
594}
595
596static void fanout_release(struct sock *sk)
597{
598 struct packet_sock *po = pkt_sk(sk);
599 struct packet_fanout *f;
600
601 f = po->fanout;
602 if (!f)
603 return;
604
605 po->fanout = NULL;
606
607 mutex_lock(&fanout_mutex);
608 if (atomic_dec_and_test(&f->sk_ref)) {
609 list_del(&f->list);
610 dev_remove_pack(&f->prot_hook);
611 kfree(f);
612 }
613 mutex_unlock(&fanout_mutex);
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800616static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800618static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000620static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
621 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct sock *sk;
624 struct sockaddr_pkt *spkt;
625
626 /*
627 * When we registered the protocol we saved the socket in the data
628 * field for just this event.
629 */
630
631 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * Yank back the headers [hope the device set this
635 * right or kerboom...]
636 *
637 * Incoming packets have ll header pulled,
638 * push it back.
639 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700640 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * so that this procedure is noop.
642 */
643
644 if (skb->pkt_type == PACKET_LOOPBACK)
645 goto out;
646
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800647 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800648 goto out;
649
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000650 skb = skb_share_check(skb, GFP_ATOMIC);
651 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 goto oom;
653
654 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000655 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Phil Oester84531c22005-07-12 11:57:52 -0700657 /* drop conntrack reference */
658 nf_reset(skb);
659
Herbert Xuffbc6112007-02-04 23:33:10 -0800660 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700662 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 /*
665 * The SOCK_PACKET socket receives _all_ frames.
666 */
667
668 spkt->spkt_family = dev->type;
669 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
670 spkt->spkt_protocol = skb->protocol;
671
672 /*
673 * Charge the memory to the socket. This is done specifically
674 * to prevent sockets using all the memory up.
675 */
676
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000677 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679
680out:
681 kfree_skb(skb);
682oom:
683 return 0;
684}
685
686
687/*
688 * Output a raw packet to a device layer. This bypasses all the other
689 * protocol layers and you must therefore supply it with a complete frame
690 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
693 struct msghdr *msg, size_t len)
694{
695 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000696 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000697 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000699 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900703 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
705
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000706 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000708 return -EINVAL;
709 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
710 proto = saddr->spkt_protocol;
711 } else
712 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900715 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
717
718 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000719retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100720 rcu_read_lock();
721 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 err = -ENODEV;
723 if (dev == NULL)
724 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900725
David S. Millerd5e76b02007-01-25 19:30:36 -0800726 err = -ENETDOWN;
727 if (!(dev->flags & IFF_UP))
728 goto out_unlock;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000731 * You may not queue a frame bigger than the mtu. This is the lowest level
732 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +0000736 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto out_unlock;
738
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000739 if (!skb) {
740 size_t reserved = LL_RESERVED_SPACE(dev);
741 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000743 rcu_read_unlock();
744 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
745 if (skb == NULL)
746 return -ENOBUFS;
747 /* FIXME: Save some space for broken drivers that write a hard
748 * header at transmission time by themselves. PPP is the notable
749 * one here. This should really be fixed at the driver level.
750 */
751 skb_reserve(skb, reserved);
752 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900753
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000754 /* Try to align data part correctly */
755 if (hhlen) {
756 skb->data -= hhlen;
757 skb->tail -= hhlen;
758 if (len < hhlen)
759 skb_reset_network_header(skb);
760 }
761 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
762 if (err)
763 goto out_free;
764 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
Ben Greear57f89bf2011-02-11 09:35:18 +0000767 if (len > (dev->mtu + dev->hard_header_len)) {
768 /* Earlier code assumed this would be a VLAN pkt,
769 * double-check this now that we have the actual
770 * packet in hand.
771 */
772 struct ethhdr *ehdr;
773 skb_reset_mac_header(skb);
774 ehdr = eth_hdr(skb);
775 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
776 err = -EMSGSIZE;
777 goto out_unlock;
778 }
779 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 skb->protocol = proto;
782 skb->dev = dev;
783 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000784 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000785 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000786 if (err < 0)
787 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100790 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000791 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100794 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000795out_free:
796 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return err;
798}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Eric Dumazet62ab0812010-12-06 20:50:09 +0000800static inline unsigned int run_filter(const struct sk_buff *skb,
801 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800802 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct sk_filter *filter;
805
Eric Dumazet80f8f102011-01-18 07:46:52 +0000806 rcu_read_lock();
807 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800808 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +0000809 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000810 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David S. Millerdbcb5852007-01-24 15:21:02 -0800812 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000816 * This function makes lazy skb cloning in hope that most of packets
817 * are discarded by BPF.
818 *
819 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
820 * and skb->cb are mangled. It works because (and until) packets
821 * falling here are owned by current CPU. Output packets are cloned
822 * by dev_queue_xmit_nit(), input packets are processed by net_bh
823 * sequencially, so that if we return skb to original state on exit,
824 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000827static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
828 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct sock *sk;
831 struct sockaddr_ll *sll;
832 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000833 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800835 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (skb->pkt_type == PACKET_LOOPBACK)
838 goto drop;
839
840 sk = pt->af_packet_priv;
841 po = pkt_sk(sk);
842
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800843 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800844 goto drop;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 skb->dev = dev;
847
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700848 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000850 * exported to higher levels.
851 *
852 * Otherwise, the device hides details of its frame
853 * structure, so that corresponding packet head is
854 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
856 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700857 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 else if (skb->pkt_type == PACKET_OUTGOING) {
859 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300860 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 }
863
864 snaplen = skb->len;
865
David S. Millerdbcb5852007-01-24 15:21:02 -0800866 res = run_filter(skb, sk, snaplen);
867 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700868 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800869 if (snaplen > res)
870 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
873 (unsigned)sk->sk_rcvbuf)
874 goto drop_n_acct;
875
876 if (skb_shared(skb)) {
877 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
878 if (nskb == NULL)
879 goto drop_n_acct;
880
881 if (skb_head != skb->data) {
882 skb->data = skb_head;
883 skb->len = skb_len;
884 }
885 kfree_skb(skb);
886 skb = nskb;
887 }
888
Herbert Xuffbc6112007-02-04 23:33:10 -0800889 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
890 sizeof(skb->cb));
891
892 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 sll->sll_family = AF_PACKET;
894 sll->sll_hatype = dev->type;
895 sll->sll_protocol = skb->protocol;
896 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800897 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700898 sll->sll_ifindex = orig_dev->ifindex;
899 else
900 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700902 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Herbert Xuffbc6112007-02-04 23:33:10 -0800904 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (pskb_trim(skb, snaplen))
907 goto drop_n_acct;
908
909 skb_set_owner_r(skb, sk);
910 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000911 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Phil Oester84531c22005-07-12 11:57:52 -0700913 /* drop conntrack reference */
914 nf_reset(skb);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 spin_lock(&sk->sk_receive_queue.lock);
917 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700918 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 __skb_queue_tail(&sk->sk_receive_queue, skb);
920 spin_unlock(&sk->sk_receive_queue.lock);
921 sk->sk_data_ready(sk, skb->len);
922 return 0;
923
924drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700925 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927drop_n_restore:
928 if (skb_head != skb->data && skb_shared(skb)) {
929 skb->data = skb_head;
930 skb->len = skb_len;
931 }
932drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000933 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
936
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000937static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
938 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct sock *sk;
941 struct packet_sock *po;
942 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700943 union {
944 struct tpacket_hdr *h1;
945 struct tpacket2_hdr *h2;
946 void *raw;
947 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000948 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800950 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700952 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700954 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700955 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700956 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (skb->pkt_type == PACKET_LOOPBACK)
959 goto drop;
960
961 sk = pt->af_packet_priv;
962 po = pkt_sk(sk);
963
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800964 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800965 goto drop;
966
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700967 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700969 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 else if (skb->pkt_type == PACKET_OUTGOING) {
971 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300972 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 }
975
Herbert Xu8dc41942007-02-04 23:31:32 -0800976 if (skb->ip_summed == CHECKSUM_PARTIAL)
977 status |= TP_STATUS_CSUMNOTREADY;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 snaplen = skb->len;
980
David S. Millerdbcb5852007-01-24 15:21:02 -0800981 res = run_filter(skb, sk, snaplen);
982 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700983 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800984 if (snaplen > res)
985 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -0700988 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
989 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300991 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700992 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -0700993 (maclen < 16 ? 16 : maclen)) +
994 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 macoff = netoff - maclen;
996 }
997
Johann Baudy69e3c752009-05-18 22:11:22 -0700998 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (po->copy_thresh &&
1000 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
1001 (unsigned)sk->sk_rcvbuf) {
1002 if (skb_shared(skb)) {
1003 copy_skb = skb_clone(skb, GFP_ATOMIC);
1004 } else {
1005 copy_skb = skb_get(skb);
1006 skb_head = skb->data;
1007 }
1008 if (copy_skb)
1009 skb_set_owner_r(copy_skb, sk);
1010 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001011 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if ((int)snaplen < 0)
1013 snaplen = 0;
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07001017 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001018 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -07001020 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 po->stats.tp_packets++;
1022 if (copy_skb) {
1023 status |= TP_STATUS_COPY;
1024 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1025 }
1026 if (!po->stats.tp_drops)
1027 status &= ~TP_STATUS_LOSING;
1028 spin_unlock(&sk->sk_receive_queue.lock);
1029
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001030 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001032 switch (po->tp_version) {
1033 case TPACKET_V1:
1034 h.h1->tp_len = skb->len;
1035 h.h1->tp_snaplen = snaplen;
1036 h.h1->tp_mac = macoff;
1037 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -07001038 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
1039 && shhwtstamps->syststamp.tv64)
1040 tv = ktime_to_timeval(shhwtstamps->syststamp);
1041 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
1042 && shhwtstamps->hwtstamp.tv64)
1043 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
1044 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001045 tv = ktime_to_timeval(skb->tstamp);
1046 else
1047 do_gettimeofday(&tv);
1048 h.h1->tp_sec = tv.tv_sec;
1049 h.h1->tp_usec = tv.tv_usec;
1050 hdrlen = sizeof(*h.h1);
1051 break;
1052 case TPACKET_V2:
1053 h.h2->tp_len = skb->len;
1054 h.h2->tp_snaplen = snaplen;
1055 h.h2->tp_mac = macoff;
1056 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -07001057 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
1058 && shhwtstamps->syststamp.tv64)
1059 ts = ktime_to_timespec(shhwtstamps->syststamp);
1060 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
1061 && shhwtstamps->hwtstamp.tv64)
1062 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
1063 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001064 ts = ktime_to_timespec(skb->tstamp);
1065 else
1066 getnstimeofday(&ts);
1067 h.h2->tp_sec = ts.tv_sec;
1068 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +00001069 if (vlan_tx_tag_present(skb)) {
1070 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
1071 status |= TP_STATUS_VLAN_VALID;
1072 } else {
1073 h.h2->tp_vlan_tci = 0;
1074 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001075 h.h2->tp_padding = 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001076 hdrlen = sizeof(*h.h2);
1077 break;
1078 default:
1079 BUG();
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001082 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001083 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 sll->sll_family = AF_PACKET;
1085 sll->sll_hatype = dev->type;
1086 sll->sll_protocol = skb->protocol;
1087 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001088 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001089 sll->sll_ifindex = orig_dev->ifindex;
1090 else
1091 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001093 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -08001094 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +00001095#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 {
Changli Gao0af55bb2010-12-01 02:52:20 +00001097 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Changli Gao0af55bb2010-12-01 02:52:20 +00001099 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
1100 for (start = h.raw; start < end; start += PAGE_SIZE)
1101 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Changli Gaof6dafa92010-12-07 04:26:16 +00001103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 sk->sk_data_ready(sk, 0);
1106
1107drop_n_restore:
1108 if (skb_head != skb->data && skb_shared(skb)) {
1109 skb->data = skb_head;
1110 skb->len = skb_len;
1111 }
1112drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001113 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return 0;
1115
1116ring_is_full:
1117 po->stats.tp_drops++;
1118 spin_unlock(&sk->sk_receive_queue.lock);
1119
1120 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +00001121 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 goto drop_n_restore;
1123}
1124
Johann Baudy69e3c752009-05-18 22:11:22 -07001125static void tpacket_destruct_skb(struct sk_buff *skb)
1126{
1127 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001128 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -07001129
1130 BUG_ON(skb == NULL);
1131
1132 if (likely(po->tx_ring.pg_vec)) {
1133 ph = skb_shinfo(skb)->destructor_arg;
1134 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
1135 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
1136 atomic_dec(&po->tx_ring.pending);
1137 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
1138 }
1139
1140 sock_wfree(skb);
1141}
1142
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001143static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
1144 void *frame, struct net_device *dev, int size_max,
1145 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -07001146{
1147 union {
1148 struct tpacket_hdr *h1;
1149 struct tpacket2_hdr *h2;
1150 void *raw;
1151 } ph;
1152 int to_write, offset, len, tp_len, nr_frags, len_max;
1153 struct socket *sock = po->sk.sk_socket;
1154 struct page *page;
1155 void *data;
1156 int err;
1157
1158 ph.raw = frame;
1159
1160 skb->protocol = proto;
1161 skb->dev = dev;
1162 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001163 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -07001164 skb_shinfo(skb)->destructor_arg = ph.raw;
1165
1166 switch (po->tp_version) {
1167 case TPACKET_V2:
1168 tp_len = ph.h2->tp_len;
1169 break;
1170 default:
1171 tp_len = ph.h1->tp_len;
1172 break;
1173 }
1174 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001175 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -07001176 return -EMSGSIZE;
1177 }
1178
1179 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1180 skb_reset_network_header(skb);
1181
1182 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
1183 to_write = tp_len;
1184
1185 if (sock->type == SOCK_DGRAM) {
1186 err = dev_hard_header(skb, dev, ntohs(proto), addr,
1187 NULL, tp_len);
1188 if (unlikely(err < 0))
1189 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001190 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -07001191 /* net device doesn't like empty head */
1192 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001193 pr_err("packet size is too short (%d < %d)\n",
1194 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001195 return -EINVAL;
1196 }
1197
1198 skb_push(skb, dev->hard_header_len);
1199 err = skb_store_bits(skb, 0, data,
1200 dev->hard_header_len);
1201 if (unlikely(err))
1202 return err;
1203
1204 data += dev->hard_header_len;
1205 to_write -= dev->hard_header_len;
1206 }
1207
1208 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001209 offset = offset_in_page(data);
1210 len_max = PAGE_SIZE - offset;
1211 len = ((to_write > len_max) ? len_max : to_write);
1212
1213 skb->data_len = to_write;
1214 skb->len += to_write;
1215 skb->truesize += to_write;
1216 atomic_add(to_write, &po->sk.sk_wmem_alloc);
1217
1218 while (likely(to_write)) {
1219 nr_frags = skb_shinfo(skb)->nr_frags;
1220
1221 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001222 pr_err("Packet exceed the number of skb frags(%lu)\n",
1223 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07001224 return -EFAULT;
1225 }
1226
Changli Gao0af55bb2010-12-01 02:52:20 +00001227 page = pgv_to_page(data);
1228 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07001229 flush_dcache_page(page);
1230 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00001231 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001232 to_write -= len;
1233 offset = 0;
1234 len_max = PAGE_SIZE;
1235 len = ((to_write > len_max) ? len_max : to_write);
1236 }
1237
1238 return tp_len;
1239}
1240
1241static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
1242{
Johann Baudy69e3c752009-05-18 22:11:22 -07001243 struct sk_buff *skb;
1244 struct net_device *dev;
1245 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001246 bool need_rls_dev = false;
1247 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001248 void *ph;
1249 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -07001250 int tp_len, size_max;
1251 unsigned char *addr;
1252 int len_sum = 0;
1253 int status = 0;
1254
Johann Baudy69e3c752009-05-18 22:11:22 -07001255 mutex_lock(&po->pg_vec_lock);
1256
1257 err = -EBUSY;
1258 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001259 dev = po->prot_hook.dev;
Johann Baudy69e3c752009-05-18 22:11:22 -07001260 proto = po->num;
1261 addr = NULL;
1262 } else {
1263 err = -EINVAL;
1264 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1265 goto out;
1266 if (msg->msg_namelen < (saddr->sll_halen
1267 + offsetof(struct sockaddr_ll,
1268 sll_addr)))
1269 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07001270 proto = saddr->sll_protocol;
1271 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001272 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
1273 need_rls_dev = true;
Johann Baudy69e3c752009-05-18 22:11:22 -07001274 }
1275
Johann Baudy69e3c752009-05-18 22:11:22 -07001276 err = -ENXIO;
1277 if (unlikely(dev == NULL))
1278 goto out;
1279
1280 reserve = dev->hard_header_len;
1281
1282 err = -ENETDOWN;
1283 if (unlikely(!(dev->flags & IFF_UP)))
1284 goto out_put;
1285
1286 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001287 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001288
1289 if (size_max > dev->mtu + reserve)
1290 size_max = dev->mtu + reserve;
1291
1292 do {
1293 ph = packet_current_frame(po, &po->tx_ring,
1294 TP_STATUS_SEND_REQUEST);
1295
1296 if (unlikely(ph == NULL)) {
1297 schedule();
1298 continue;
1299 }
1300
1301 status = TP_STATUS_SEND_REQUEST;
1302 skb = sock_alloc_send_skb(&po->sk,
1303 LL_ALLOCATED_SPACE(dev)
1304 + sizeof(struct sockaddr_ll),
1305 0, &err);
1306
1307 if (unlikely(skb == NULL))
1308 goto out_status;
1309
1310 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1311 addr);
1312
1313 if (unlikely(tp_len < 0)) {
1314 if (po->tp_loss) {
1315 __packet_set_status(po, ph,
1316 TP_STATUS_AVAILABLE);
1317 packet_increment_head(&po->tx_ring);
1318 kfree_skb(skb);
1319 continue;
1320 } else {
1321 status = TP_STATUS_WRONG_FORMAT;
1322 err = tp_len;
1323 goto out_status;
1324 }
1325 }
1326
1327 skb->destructor = tpacket_destruct_skb;
1328 __packet_set_status(po, ph, TP_STATUS_SENDING);
1329 atomic_inc(&po->tx_ring.pending);
1330
1331 status = TP_STATUS_SEND_REQUEST;
1332 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001333 if (unlikely(err > 0)) {
1334 err = net_xmit_errno(err);
1335 if (err && __packet_get_status(po, ph) ==
1336 TP_STATUS_AVAILABLE) {
1337 /* skb was destructed already */
1338 skb = NULL;
1339 goto out_status;
1340 }
1341 /*
1342 * skb was dropped but not destructed yet;
1343 * let's treat it like congestion or err < 0
1344 */
1345 err = 0;
1346 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001347 packet_increment_head(&po->tx_ring);
1348 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001349 } while (likely((ph != NULL) ||
1350 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1351 (atomic_read(&po->tx_ring.pending))))
1352 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001353
1354 err = len_sum;
1355 goto out_put;
1356
Johann Baudy69e3c752009-05-18 22:11:22 -07001357out_status:
1358 __packet_set_status(po, ph, status);
1359 kfree_skb(skb);
1360out_put:
Ben Greear827d9782011-06-01 07:18:53 +00001361 if (need_rls_dev)
1362 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07001363out:
1364 mutex_unlock(&po->pg_vec_lock);
1365 return err;
1366}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001368static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1369 size_t reserve, size_t len,
1370 size_t linear, int noblock,
1371 int *err)
1372{
1373 struct sk_buff *skb;
1374
1375 /* Under a page? Don't bother with paged skb. */
1376 if (prepad + len < PAGE_SIZE || !linear)
1377 linear = len;
1378
1379 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1380 err);
1381 if (!skb)
1382 return NULL;
1383
1384 skb_reserve(skb, reserve);
1385 skb_put(skb, linear);
1386 skb->data_len = len - linear;
1387 skb->len += len - linear;
1388
1389 return skb;
1390}
1391
Johann Baudy69e3c752009-05-18 22:11:22 -07001392static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct msghdr *msg, size_t len)
1394{
1395 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001396 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct sk_buff *skb;
1398 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001399 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001400 bool need_rls_dev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00001402 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001403 struct virtio_net_hdr vnet_hdr = { 0 };
1404 int offset = 0;
1405 int vnet_hdr_len;
1406 struct packet_sock *po = pkt_sk(sk);
1407 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001410 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001414 dev = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 proto = po->num;
1416 addr = NULL;
1417 } else {
1418 err = -EINVAL;
1419 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1420 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001421 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 proto = saddr->sll_protocol;
1424 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001425 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
1426 need_rls_dev = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 err = -ENXIO;
1430 if (dev == NULL)
1431 goto out_unlock;
1432 if (sock->type == SOCK_RAW)
1433 reserve = dev->hard_header_len;
1434
David S. Millerd5e76b02007-01-25 19:30:36 -08001435 err = -ENETDOWN;
1436 if (!(dev->flags & IFF_UP))
1437 goto out_unlock;
1438
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001439 if (po->has_vnet_hdr) {
1440 vnet_hdr_len = sizeof(vnet_hdr);
1441
1442 err = -EINVAL;
1443 if (len < vnet_hdr_len)
1444 goto out_unlock;
1445
1446 len -= vnet_hdr_len;
1447
1448 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1449 vnet_hdr_len);
1450 if (err < 0)
1451 goto out_unlock;
1452
1453 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1454 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1455 vnet_hdr.hdr_len))
1456 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1457 vnet_hdr.csum_offset + 2;
1458
1459 err = -EINVAL;
1460 if (vnet_hdr.hdr_len > len)
1461 goto out_unlock;
1462
1463 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1464 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1465 case VIRTIO_NET_HDR_GSO_TCPV4:
1466 gso_type = SKB_GSO_TCPV4;
1467 break;
1468 case VIRTIO_NET_HDR_GSO_TCPV6:
1469 gso_type = SKB_GSO_TCPV6;
1470 break;
1471 case VIRTIO_NET_HDR_GSO_UDP:
1472 gso_type = SKB_GSO_UDP;
1473 break;
1474 default:
1475 goto out_unlock;
1476 }
1477
1478 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1479 gso_type |= SKB_GSO_TCP_ECN;
1480
1481 if (vnet_hdr.gso_size == 0)
1482 goto out_unlock;
1483
1484 }
1485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +00001488 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto out_unlock;
1490
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001491 err = -ENOBUFS;
1492 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1493 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1494 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001495 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 goto out_unlock;
1497
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001498 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001500 err = -EINVAL;
1501 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001502 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001503 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001506 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (err)
1508 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001509 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001510 if (err < 0)
1511 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Ben Greear57f89bf2011-02-11 09:35:18 +00001513 if (!gso_type && (len > dev->mtu + reserve)) {
1514 /* Earlier code assumed this would be a VLAN pkt,
1515 * double-check this now that we have the actual
1516 * packet in hand.
1517 */
1518 struct ethhdr *ehdr;
1519 skb_reset_mac_header(skb);
1520 ehdr = eth_hdr(skb);
1521 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1522 err = -EMSGSIZE;
1523 goto out_free;
1524 }
1525 }
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 skb->protocol = proto;
1528 skb->dev = dev;
1529 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001530 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001532 if (po->has_vnet_hdr) {
1533 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1534 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1535 vnet_hdr.csum_offset)) {
1536 err = -EINVAL;
1537 goto out_free;
1538 }
1539 }
1540
1541 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1542 skb_shinfo(skb)->gso_type = gso_type;
1543
1544 /* Header must be checked, and gso_segs computed. */
1545 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1546 skb_shinfo(skb)->gso_segs = 0;
1547
1548 len += vnet_hdr_len;
1549 }
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Now send it
1553 */
1554
1555 err = dev_queue_xmit(skb);
1556 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1557 goto out_unlock;
1558
Ben Greear827d9782011-06-01 07:18:53 +00001559 if (need_rls_dev)
1560 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001562 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564out_free:
1565 kfree_skb(skb);
1566out_unlock:
Ben Greear827d9782011-06-01 07:18:53 +00001567 if (dev && need_rls_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 dev_put(dev);
1569out:
1570 return err;
1571}
1572
Johann Baudy69e3c752009-05-18 22:11:22 -07001573static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1574 struct msghdr *msg, size_t len)
1575{
Johann Baudy69e3c752009-05-18 22:11:22 -07001576 struct sock *sk = sock->sk;
1577 struct packet_sock *po = pkt_sk(sk);
1578 if (po->tx_ring.pg_vec)
1579 return tpacket_snd(po, msg);
1580 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001581 return packet_snd(sock, msg, len);
1582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/*
1585 * Close a PACKET socket. This is fairly simple. We immediately go
1586 * to 'closed' state and remove our protocol entry in the device list.
1587 */
1588
1589static int packet_release(struct socket *sock)
1590{
1591 struct sock *sk = sock->sk;
1592 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001593 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001594 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (!sk)
1597 return 0;
1598
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001599 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 po = pkt_sk(sk);
1601
stephen hemminger808f5112010-02-22 07:57:18 +00001602 spin_lock_bh(&net->packet.sklist_lock);
1603 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001604 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001605 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
stephen hemminger808f5112010-02-22 07:57:18 +00001607 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001608 unregister_prot_hook(sk, false);
Ben Greear160ff182011-06-01 07:18:52 +00001609 if (po->prot_hook.dev) {
1610 dev_put(po->prot_hook.dev);
1611 po->prot_hook.dev = NULL;
1612 }
stephen hemminger808f5112010-02-22 07:57:18 +00001613 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Johann Baudy69e3c752009-05-18 22:11:22 -07001617 memset(&req, 0, sizeof(req));
1618
1619 if (po->rx_ring.pg_vec)
1620 packet_set_ring(sk, &req, 1, 0);
1621
1622 if (po->tx_ring.pg_vec)
1623 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
David S. Millerdc99f602011-07-05 01:45:05 -07001625 fanout_release(sk);
1626
stephen hemminger808f5112010-02-22 07:57:18 +00001627 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * Now the socket is dead. No more input will appear.
1630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sock_orphan(sk);
1632 sock->sk = NULL;
1633
1634 /* Purge queues */
1635
1636 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001637 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 sock_put(sk);
1640 return 0;
1641}
1642
1643/*
1644 * Attach a packet hook.
1645 */
1646
Al Viro0e11c912006-11-08 00:26:29 -08001647static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct packet_sock *po = pkt_sk(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07001650
1651 if (po->fanout)
1652 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 lock_sock(sk);
1655
1656 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001657 unregister_prot_hook(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 po->num = protocol;
1659 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00001660 if (po->prot_hook.dev)
1661 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 po->prot_hook.dev = dev;
1663
1664 po->ifindex = dev ? dev->ifindex : 0;
1665
1666 if (protocol == 0)
1667 goto out_unlock;
1668
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001669 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07001670 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001671 } else {
1672 sk->sk_err = ENETDOWN;
1673 if (!sock_flag(sk, SOCK_DEAD))
1674 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677out_unlock:
1678 spin_unlock(&po->bind_lock);
1679 release_sock(sk);
1680 return 0;
1681}
1682
1683/*
1684 * Bind a packet socket to a device
1685 */
1686
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001687static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1688 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001690 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 char name[15];
1692 struct net_device *dev;
1693 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Check legality
1697 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001698
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001699 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001701 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001703 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00001704 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return err;
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1710{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001711 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1712 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct net_device *dev = NULL;
1714 int err;
1715
1716
1717 /*
1718 * Check legality
1719 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (addr_len < sizeof(struct sockaddr_ll))
1722 return -EINVAL;
1723 if (sll->sll_family != AF_PACKET)
1724 return -EINVAL;
1725
1726 if (sll->sll_ifindex) {
1727 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001728 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (dev == NULL)
1730 goto out;
1731 }
1732 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734out:
1735 return err;
1736}
1737
1738static struct proto packet_proto = {
1739 .name = "PACKET",
1740 .owner = THIS_MODULE,
1741 .obj_size = sizeof(struct packet_sock),
1742};
1743
1744/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001745 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
1747
Eric Paris3f378b62009-11-05 22:18:14 -08001748static int packet_create(struct net *net, struct socket *sock, int protocol,
1749 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 struct sock *sk;
1752 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001753 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int err;
1755
1756 if (!capable(CAP_NET_RAW))
1757 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001758 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1759 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return -ESOCKTNOSUPPORT;
1761
1762 sock->state = SS_UNCONNECTED;
1763
1764 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001765 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (sk == NULL)
1767 goto out;
1768
1769 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (sock->type == SOCK_PACKET)
1771 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 sock_init_data(sock, sk);
1774
1775 po = pkt_sk(sk);
1776 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001777 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001780 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /*
1783 * Attach a protocol block
1784 */
1785
1786 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001787 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (sock->type == SOCK_PACKET)
1791 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 po->prot_hook.af_packet_priv = sk;
1794
Al Viro0e11c912006-11-08 00:26:29 -08001795 if (proto) {
1796 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07001797 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799
stephen hemminger808f5112010-02-22 07:57:18 +00001800 spin_lock_bh(&net->packet.sklist_lock);
1801 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001802 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001803 spin_unlock_bh(&net->packet.sklist_lock);
1804
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806out:
1807 return err;
1808}
1809
Richard Cochraned85b562010-04-07 22:41:28 +00001810static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1811{
1812 struct sock_exterr_skb *serr;
1813 struct sk_buff *skb, *skb2;
1814 int copied, err;
1815
1816 err = -EAGAIN;
1817 skb = skb_dequeue(&sk->sk_error_queue);
1818 if (skb == NULL)
1819 goto out;
1820
1821 copied = skb->len;
1822 if (copied > len) {
1823 msg->msg_flags |= MSG_TRUNC;
1824 copied = len;
1825 }
1826 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1827 if (err)
1828 goto out_free_skb;
1829
1830 sock_recv_timestamp(msg, sk, skb);
1831
1832 serr = SKB_EXT_ERR(skb);
1833 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1834 sizeof(serr->ee), &serr->ee);
1835
1836 msg->msg_flags |= MSG_ERRQUEUE;
1837 err = copied;
1838
1839 /* Reset and regenerate socket error */
1840 spin_lock_bh(&sk->sk_error_queue.lock);
1841 sk->sk_err = 0;
1842 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1843 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1844 spin_unlock_bh(&sk->sk_error_queue.lock);
1845 sk->sk_error_report(sk);
1846 } else
1847 spin_unlock_bh(&sk->sk_error_queue.lock);
1848
1849out_free_skb:
1850 kfree_skb(skb);
1851out:
1852 return err;
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
1856 * Pull a packet from our receive queue and hand it to the user.
1857 * If necessary we block.
1858 */
1859
1860static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1861 struct msghdr *msg, size_t len, int flags)
1862{
1863 struct sock *sk = sock->sk;
1864 struct sk_buff *skb;
1865 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001866 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001867 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001870 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto out;
1872
1873#if 0
1874 /* What error should we return now? EUNATTACH? */
1875 if (pkt_sk(sk)->ifindex < 0)
1876 return -ENODEV;
1877#endif
1878
Richard Cochraned85b562010-04-07 22:41:28 +00001879 if (flags & MSG_ERRQUEUE) {
1880 err = packet_recv_error(sk, msg, len);
1881 goto out;
1882 }
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * Call the generic datagram receiver. This handles all sorts
1886 * of horrible races and re-entrancy so we can forget about it
1887 * in the protocol layers.
1888 *
1889 * Now it will return ENETDOWN, if device have just gone down,
1890 * but then it will block.
1891 */
1892
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001893 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001896 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 * handles the blocking we don't see and worry about blocking
1898 * retries.
1899 */
1900
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001901 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto out;
1903
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001904 if (pkt_sk(sk)->has_vnet_hdr) {
1905 struct virtio_net_hdr vnet_hdr = { 0 };
1906
1907 err = -EINVAL;
1908 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001909 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001910 goto out_free;
1911
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001912 len -= vnet_hdr_len;
1913
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001914 if (skb_is_gso(skb)) {
1915 struct skb_shared_info *sinfo = skb_shinfo(skb);
1916
1917 /* This is a hint as to how much should be linear. */
1918 vnet_hdr.hdr_len = skb_headlen(skb);
1919 vnet_hdr.gso_size = sinfo->gso_size;
1920 if (sinfo->gso_type & SKB_GSO_TCPV4)
1921 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1922 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1923 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1924 else if (sinfo->gso_type & SKB_GSO_UDP)
1925 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1926 else if (sinfo->gso_type & SKB_GSO_FCOE)
1927 goto out_free;
1928 else
1929 BUG();
1930 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1931 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1932 } else
1933 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1934
1935 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1936 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001937 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001938 vnet_hdr.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001939 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1940 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001941 } /* else everything is zero */
1942
1943 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1944 vnet_hdr_len);
1945 if (err < 0)
1946 goto out_free;
1947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001950 * If the address length field is there to be filled in, we fill
1951 * it in now.
1952 */
1953
Herbert Xuffbc6112007-02-04 23:33:10 -08001954 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001955 if (sock->type == SOCK_PACKET)
1956 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1957 else
1958 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1959
1960 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * You lose any data beyond the buffer you gave. If it worries a
1962 * user program they can ask the device for its MTU anyway.
1963 */
1964
1965 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001966 if (copied > len) {
1967 copied = len;
1968 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
1971 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1972 if (err)
1973 goto out_free;
1974
Neil Horman3b885782009-10-12 13:26:31 -07001975 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001978 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1979 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Herbert Xu8dc41942007-02-04 23:31:32 -08001981 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001982 struct tpacket_auxdata aux;
1983
1984 aux.tp_status = TP_STATUS_USER;
1985 if (skb->ip_summed == CHECKSUM_PARTIAL)
1986 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1987 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1988 aux.tp_snaplen = skb->len;
1989 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001990 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00001991 if (vlan_tx_tag_present(skb)) {
1992 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
1993 aux.tp_status |= TP_STATUS_VLAN_VALID;
1994 } else {
1995 aux.tp_vlan_tci = 0;
1996 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001997 aux.tp_padding = 0;
Herbert Xuffbc6112007-02-04 23:33:10 -08001998 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001999 }
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
2002 * Free or return the buffer as appropriate. Again this
2003 * hides all the races and re-entrancy issues from us.
2004 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002005 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007out_free:
2008 skb_free_datagram(sk, skb);
2009out:
2010 return err;
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
2014 int *uaddr_len, int peer)
2015{
2016 struct net_device *dev;
2017 struct sock *sk = sock->sk;
2018
2019 if (peer)
2020 return -EOPNOTSUPP;
2021
2022 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002023 rcu_read_lock();
2024 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
2025 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002026 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002027 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002029 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 *uaddr_len = sizeof(*uaddr);
2031
2032 return 0;
2033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
2036 int *uaddr_len, int peer)
2037{
2038 struct net_device *dev;
2039 struct sock *sk = sock->sk;
2040 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00002041 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 if (peer)
2044 return -EOPNOTSUPP;
2045
2046 sll->sll_family = AF_PACKET;
2047 sll->sll_ifindex = po->ifindex;
2048 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002049 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002050 rcu_read_lock();
2051 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (dev) {
2053 sll->sll_hatype = dev->type;
2054 sll->sll_halen = dev->addr_len;
2055 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 } else {
2057 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
2058 sll->sll_halen = 0;
2059 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01002060 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002061 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063 return 0;
2064}
2065
Wang Chen2aeb0b82008-07-14 20:49:46 -07002066static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
2067 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 switch (i->type) {
2070 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002071 if (i->alen != dev->addr_len)
2072 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (what > 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00002074 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 else
Jiri Pirko22bedad2010-04-01 21:22:57 +00002076 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 break;
2078 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002079 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 break;
2081 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002082 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002084 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002085 if (i->alen != dev->addr_len)
2086 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002087 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002088 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002089 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002090 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002091 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002092 default:
2093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07002095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
2099{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002100 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (i->ifindex == dev->ifindex)
2102 packet_dev_mc(dev, i, what);
2103 }
2104}
2105
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002106static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 struct packet_sock *po = pkt_sk(sk);
2109 struct packet_mclist *ml, *i;
2110 struct net_device *dev;
2111 int err;
2112
2113 rtnl_lock();
2114
2115 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002116 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (!dev)
2118 goto done;
2119
2120 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00002121 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto done;
2123
2124 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002125 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (i == NULL)
2127 goto done;
2128
2129 err = 0;
2130 for (ml = po->mclist; ml; ml = ml->next) {
2131 if (ml->ifindex == mreq->mr_ifindex &&
2132 ml->type == mreq->mr_type &&
2133 ml->alen == mreq->mr_alen &&
2134 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2135 ml->count++;
2136 /* Free the new element ... */
2137 kfree(i);
2138 goto done;
2139 }
2140 }
2141
2142 i->type = mreq->mr_type;
2143 i->ifindex = mreq->mr_ifindex;
2144 i->alen = mreq->mr_alen;
2145 memcpy(i->addr, mreq->mr_address, i->alen);
2146 i->count = 1;
2147 i->next = po->mclist;
2148 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07002149 err = packet_dev_mc(dev, i, 1);
2150 if (err) {
2151 po->mclist = i->next;
2152 kfree(i);
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155done:
2156 rtnl_unlock();
2157 return err;
2158}
2159
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002160static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 struct packet_mclist *ml, **mlp;
2163
2164 rtnl_lock();
2165
2166 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
2167 if (ml->ifindex == mreq->mr_ifindex &&
2168 ml->type == mreq->mr_type &&
2169 ml->alen == mreq->mr_alen &&
2170 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2171 if (--ml->count == 0) {
2172 struct net_device *dev;
2173 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002174 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2175 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 kfree(ml);
2178 }
2179 rtnl_unlock();
2180 return 0;
2181 }
2182 }
2183 rtnl_unlock();
2184 return -EADDRNOTAVAIL;
2185}
2186
2187static void packet_flush_mclist(struct sock *sk)
2188{
2189 struct packet_sock *po = pkt_sk(sk);
2190 struct packet_mclist *ml;
2191
2192 if (!po->mclist)
2193 return;
2194
2195 rtnl_lock();
2196 while ((ml = po->mclist) != NULL) {
2197 struct net_device *dev;
2198
2199 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002200 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2201 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 kfree(ml);
2204 }
2205 rtnl_unlock();
2206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208static int
David S. Millerb7058842009-09-30 16:12:20 -07002209packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
2211 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08002212 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int ret;
2214
2215 if (level != SOL_PACKET)
2216 return -ENOPROTOOPT;
2217
Johann Baudy69e3c752009-05-18 22:11:22 -07002218 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002219 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 case PACKET_DROP_MEMBERSHIP:
2221 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002222 struct packet_mreq_max mreq;
2223 int len = optlen;
2224 memset(&mreq, 0, sizeof(mreq));
2225 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002227 if (len > sizeof(mreq))
2228 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002229 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002231 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
2232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (optname == PACKET_ADD_MEMBERSHIP)
2234 ret = packet_mc_add(sk, &mreq);
2235 else
2236 ret = packet_mc_drop(sk, &mreq);
2237 return ret;
2238 }
David S. Millera2efcfa2007-05-29 13:12:50 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07002241 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 {
2243 struct tpacket_req req;
2244
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002245 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002247 if (pkt_sk(sk)->has_vnet_hdr)
2248 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002249 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07002251 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253 case PACKET_COPY_THRESH:
2254 {
2255 int val;
2256
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002257 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002259 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return -EFAULT;
2261
2262 pkt_sk(sk)->copy_thresh = val;
2263 return 0;
2264 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002265 case PACKET_VERSION:
2266 {
2267 int val;
2268
2269 if (optlen != sizeof(val))
2270 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002271 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002272 return -EBUSY;
2273 if (copy_from_user(&val, optval, sizeof(val)))
2274 return -EFAULT;
2275 switch (val) {
2276 case TPACKET_V1:
2277 case TPACKET_V2:
2278 po->tp_version = val;
2279 return 0;
2280 default:
2281 return -EINVAL;
2282 }
2283 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07002284 case PACKET_RESERVE:
2285 {
2286 unsigned int val;
2287
2288 if (optlen != sizeof(val))
2289 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002290 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07002291 return -EBUSY;
2292 if (copy_from_user(&val, optval, sizeof(val)))
2293 return -EFAULT;
2294 po->tp_reserve = val;
2295 return 0;
2296 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002297 case PACKET_LOSS:
2298 {
2299 unsigned int val;
2300
2301 if (optlen != sizeof(val))
2302 return -EINVAL;
2303 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2304 return -EBUSY;
2305 if (copy_from_user(&val, optval, sizeof(val)))
2306 return -EFAULT;
2307 po->tp_loss = !!val;
2308 return 0;
2309 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002310 case PACKET_AUXDATA:
2311 {
2312 int val;
2313
2314 if (optlen < sizeof(val))
2315 return -EINVAL;
2316 if (copy_from_user(&val, optval, sizeof(val)))
2317 return -EFAULT;
2318
2319 po->auxdata = !!val;
2320 return 0;
2321 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002322 case PACKET_ORIGDEV:
2323 {
2324 int val;
2325
2326 if (optlen < sizeof(val))
2327 return -EINVAL;
2328 if (copy_from_user(&val, optval, sizeof(val)))
2329 return -EFAULT;
2330
2331 po->origdev = !!val;
2332 return 0;
2333 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002334 case PACKET_VNET_HDR:
2335 {
2336 int val;
2337
2338 if (sock->type != SOCK_RAW)
2339 return -EINVAL;
2340 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2341 return -EBUSY;
2342 if (optlen < sizeof(val))
2343 return -EINVAL;
2344 if (copy_from_user(&val, optval, sizeof(val)))
2345 return -EFAULT;
2346
2347 po->has_vnet_hdr = !!val;
2348 return 0;
2349 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002350 case PACKET_TIMESTAMP:
2351 {
2352 int val;
2353
2354 if (optlen != sizeof(val))
2355 return -EINVAL;
2356 if (copy_from_user(&val, optval, sizeof(val)))
2357 return -EFAULT;
2358
2359 po->tp_tstamp = val;
2360 return 0;
2361 }
David S. Millerdc99f602011-07-05 01:45:05 -07002362 case PACKET_FANOUT:
2363 {
2364 int val;
2365
2366 if (optlen != sizeof(val))
2367 return -EINVAL;
2368 if (copy_from_user(&val, optval, sizeof(val)))
2369 return -EFAULT;
2370
2371 return fanout_add(sk, val & 0xffff, val >> 16);
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 default:
2374 return -ENOPROTOOPT;
2375 }
2376}
2377
2378static int packet_getsockopt(struct socket *sock, int level, int optname,
2379 char __user *optval, int __user *optlen)
2380{
2381 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002382 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct sock *sk = sock->sk;
2384 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002385 void *data;
2386 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (level != SOL_PACKET)
2389 return -ENOPROTOOPT;
2390
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002391 if (get_user(len, optlen))
2392 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 if (len < 0)
2395 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002396
Johann Baudy69e3c752009-05-18 22:11:22 -07002397 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (len > sizeof(struct tpacket_stats))
2400 len = sizeof(struct tpacket_stats);
2401 spin_lock_bh(&sk->sk_receive_queue.lock);
2402 st = po->stats;
2403 memset(&po->stats, 0, sizeof(st));
2404 spin_unlock_bh(&sk->sk_receive_queue.lock);
2405 st.tp_packets += st.tp_drops;
2406
Herbert Xu8dc41942007-02-04 23:31:32 -08002407 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002409 case PACKET_AUXDATA:
2410 if (len > sizeof(int))
2411 len = sizeof(int);
2412 val = po->auxdata;
2413
2414 data = &val;
2415 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002416 case PACKET_ORIGDEV:
2417 if (len > sizeof(int))
2418 len = sizeof(int);
2419 val = po->origdev;
2420
2421 data = &val;
2422 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002423 case PACKET_VNET_HDR:
2424 if (len > sizeof(int))
2425 len = sizeof(int);
2426 val = po->has_vnet_hdr;
2427
2428 data = &val;
2429 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002430 case PACKET_VERSION:
2431 if (len > sizeof(int))
2432 len = sizeof(int);
2433 val = po->tp_version;
2434 data = &val;
2435 break;
2436 case PACKET_HDRLEN:
2437 if (len > sizeof(int))
2438 len = sizeof(int);
2439 if (copy_from_user(&val, optval, len))
2440 return -EFAULT;
2441 switch (val) {
2442 case TPACKET_V1:
2443 val = sizeof(struct tpacket_hdr);
2444 break;
2445 case TPACKET_V2:
2446 val = sizeof(struct tpacket2_hdr);
2447 break;
2448 default:
2449 return -EINVAL;
2450 }
2451 data = &val;
2452 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002453 case PACKET_RESERVE:
2454 if (len > sizeof(unsigned int))
2455 len = sizeof(unsigned int);
2456 val = po->tp_reserve;
2457 data = &val;
2458 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002459 case PACKET_LOSS:
2460 if (len > sizeof(unsigned int))
2461 len = sizeof(unsigned int);
2462 val = po->tp_loss;
2463 data = &val;
2464 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002465 case PACKET_TIMESTAMP:
2466 if (len > sizeof(int))
2467 len = sizeof(int);
2468 val = po->tp_tstamp;
2469 data = &val;
2470 break;
David S. Millerdc99f602011-07-05 01:45:05 -07002471 case PACKET_FANOUT:
2472 if (len > sizeof(int))
2473 len = sizeof(int);
2474 val = (po->fanout ?
2475 ((u32)po->fanout->id |
2476 ((u32)po->fanout->type << 16)) :
2477 0);
2478 data = &val;
2479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 default:
2481 return -ENOPROTOOPT;
2482 }
2483
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002484 if (put_user(len, optlen))
2485 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002486 if (copy_to_user(optval, data, len))
2487 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
2491
2492static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2493{
2494 struct sock *sk;
2495 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002496 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002497 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
stephen hemminger808f5112010-02-22 07:57:18 +00002499 rcu_read_lock();
2500 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct packet_sock *po = pkt_sk(sk);
2502
2503 switch (msg) {
2504 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (po->mclist)
2506 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002507 /* fallthrough */
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 case NETDEV_DOWN:
2510 if (dev->ifindex == po->ifindex) {
2511 spin_lock(&po->bind_lock);
2512 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07002513 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 sk->sk_err = ENETDOWN;
2515 if (!sock_flag(sk, SOCK_DEAD))
2516 sk->sk_error_report(sk);
2517 }
2518 if (msg == NETDEV_UNREGISTER) {
2519 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00002520 if (po->prot_hook.dev)
2521 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 po->prot_hook.dev = NULL;
2523 }
2524 spin_unlock(&po->bind_lock);
2525 }
2526 break;
2527 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002528 if (dev->ifindex == po->ifindex) {
2529 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002530 if (po->num)
2531 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00002532 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 break;
2535 }
2536 }
stephen hemminger808f5112010-02-22 07:57:18 +00002537 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return NOTIFY_DONE;
2539}
2540
2541
2542static int packet_ioctl(struct socket *sock, unsigned int cmd,
2543 unsigned long arg)
2544{
2545 struct sock *sk = sock->sk;
2546
Johann Baudy69e3c752009-05-18 22:11:22 -07002547 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002548 case SIOCOUTQ:
2549 {
2550 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002551
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002552 return put_user(amount, (int __user *)arg);
2553 }
2554 case SIOCINQ:
2555 {
2556 struct sk_buff *skb;
2557 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002559 spin_lock_bh(&sk->sk_receive_queue.lock);
2560 skb = skb_peek(&sk->sk_receive_queue);
2561 if (skb)
2562 amount = skb->len;
2563 spin_unlock_bh(&sk->sk_receive_queue.lock);
2564 return put_user(amount, (int __user *)arg);
2565 }
2566 case SIOCGSTAMP:
2567 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2568 case SIOCGSTAMPNS:
2569 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002572 case SIOCADDRT:
2573 case SIOCDELRT:
2574 case SIOCDARP:
2575 case SIOCGARP:
2576 case SIOCSARP:
2577 case SIOCGIFADDR:
2578 case SIOCSIFADDR:
2579 case SIOCGIFBRDADDR:
2580 case SIOCSIFBRDADDR:
2581 case SIOCGIFNETMASK:
2582 case SIOCSIFNETMASK:
2583 case SIOCGIFDSTADDR:
2584 case SIOCSIFDSTADDR:
2585 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002586 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587#endif
2588
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002589 default:
2590 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592 return 0;
2593}
2594
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002595static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 poll_table *wait)
2597{
2598 struct sock *sk = sock->sk;
2599 struct packet_sock *po = pkt_sk(sk);
2600 unsigned int mask = datagram_poll(file, sock, wait);
2601
2602 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002603 if (po->rx_ring.pg_vec) {
2604 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 mask |= POLLIN | POLLRDNORM;
2606 }
2607 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002608 spin_lock_bh(&sk->sk_write_queue.lock);
2609 if (po->tx_ring.pg_vec) {
2610 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2611 mask |= POLLOUT | POLLWRNORM;
2612 }
2613 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return mask;
2615}
2616
2617
2618/* Dirty? Well, I still did not learn better way to account
2619 * for user mmaps.
2620 */
2621
2622static void packet_mm_open(struct vm_area_struct *vma)
2623{
2624 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002625 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (sk)
2629 atomic_inc(&pkt_sk(sk)->mapped);
2630}
2631
2632static void packet_mm_close(struct vm_area_struct *vma)
2633{
2634 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002635 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (sk)
2639 atomic_dec(&pkt_sk(sk)->mapped);
2640}
2641
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002642static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002643 .open = packet_mm_open,
2644 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645};
2646
Neil Horman0e3125c2010-11-16 10:26:47 -08002647static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2648 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
2650 int i;
2651
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002652 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002653 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002654 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002655 vfree(pg_vec[i].buffer);
2656 else
2657 free_pages((unsigned long)pg_vec[i].buffer,
2658 order);
2659 pg_vec[i].buffer = NULL;
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 kfree(pg_vec);
2663}
2664
Changli Gaoc56b4d92010-12-01 02:52:57 +00002665static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002666{
Neil Horman0e3125c2010-11-16 10:26:47 -08002667 char *buffer = NULL;
2668 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2669 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002670
Neil Horman0e3125c2010-11-16 10:26:47 -08002671 buffer = (char *) __get_free_pages(gfp_flags, order);
2672
2673 if (buffer)
2674 return buffer;
2675
2676 /*
2677 * __get_free_pages failed, fall back to vmalloc
2678 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002679 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002680
2681 if (buffer)
2682 return buffer;
2683
2684 /*
2685 * vmalloc failed, lets dig into swap here
2686 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002687 gfp_flags &= ~__GFP_NORETRY;
2688 buffer = (char *)__get_free_pages(gfp_flags, order);
2689 if (buffer)
2690 return buffer;
2691
2692 /*
2693 * complete and utter failure
2694 */
2695 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002696}
2697
Neil Horman0e3125c2010-11-16 10:26:47 -08002698static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002699{
2700 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002701 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002702 int i;
2703
Neil Horman0e3125c2010-11-16 10:26:47 -08002704 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002705 if (unlikely(!pg_vec))
2706 goto out;
2707
2708 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002709 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002710 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002711 goto out_free_pgvec;
2712 }
2713
2714out:
2715 return pg_vec;
2716
2717out_free_pgvec:
2718 free_pg_vec(pg_vec, order, block_nr);
2719 pg_vec = NULL;
2720 goto out;
2721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Johann Baudy69e3c752009-05-18 22:11:22 -07002723static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2724 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Neil Horman0e3125c2010-11-16 10:26:47 -08002726 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002728 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002729 struct packet_ring_buffer *rb;
2730 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002731 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002732 int err;
2733
2734 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2735 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2736
2737 err = -EBUSY;
2738 if (!closing) {
2739 if (atomic_read(&po->mapped))
2740 goto out;
2741 if (atomic_read(&rb->pending))
2742 goto out;
2743 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002747 err = -EBUSY;
2748 if (unlikely(rb->pg_vec))
2749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002751 switch (po->tp_version) {
2752 case TPACKET_V1:
2753 po->tp_hdrlen = TPACKET_HDRLEN;
2754 break;
2755 case TPACKET_V2:
2756 po->tp_hdrlen = TPACKET2_HDRLEN;
2757 break;
2758 }
2759
Johann Baudy69e3c752009-05-18 22:11:22 -07002760 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002761 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002762 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002763 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002764 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002765 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002766 po->tp_reserve))
2767 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002768 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002769 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Johann Baudy69e3c752009-05-18 22:11:22 -07002771 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2772 if (unlikely(rb->frames_per_block <= 0))
2773 goto out;
2774 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2775 req->tp_frame_nr))
2776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002779 order = get_order(req->tp_block_size);
2780 pg_vec = alloc_pg_vec(req, order);
2781 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002783 }
2784 /* Done */
2785 else {
2786 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002787 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790
2791 lock_sock(sk);
2792
2793 /* Detach socket from network */
2794 spin_lock(&po->bind_lock);
2795 was_running = po->running;
2796 num = po->num;
2797 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07002799 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 synchronize_net();
2804
2805 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002806 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (closing || atomic_read(&po->mapped) == 0) {
2808 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002809 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002810 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002811 rb->frame_max = (req->tp_frame_nr - 1);
2812 rb->head = 0;
2813 rb->frame_size = req->tp_frame_size;
2814 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Changli Gaoc053fd92010-12-10 16:02:20 -08002816 swap(rb->pg_vec_order, order);
2817 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Johann Baudy69e3c752009-05-18 22:11:22 -07002819 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2820 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2821 tpacket_rcv : packet_rcv;
2822 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002824 pr_err("packet_mmap: vma is busy: %d\n",
2825 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Herbert Xu905db442009-01-30 14:12:06 -08002827 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002830 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07002832 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
2834 spin_unlock(&po->bind_lock);
2835
2836 release_sock(sk);
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (pg_vec)
2839 free_pg_vec(pg_vec, order, req->tp_block_nr);
2840out:
2841 return err;
2842}
2843
Johann Baudy69e3c752009-05-18 22:11:22 -07002844static int packet_mmap(struct file *file, struct socket *sock,
2845 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 struct sock *sk = sock->sk;
2848 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002849 unsigned long size, expected_size;
2850 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 unsigned long start;
2852 int err = -EINVAL;
2853 int i;
2854
2855 if (vma->vm_pgoff)
2856 return -EINVAL;
2857
Herbert Xu905db442009-01-30 14:12:06 -08002858 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002859
2860 expected_size = 0;
2861 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2862 if (rb->pg_vec) {
2863 expected_size += rb->pg_vec_len
2864 * rb->pg_vec_pages
2865 * PAGE_SIZE;
2866 }
2867 }
2868
2869 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002871
2872 size = vma->vm_end - vma->vm_start;
2873 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto out;
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002877 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2878 if (rb->pg_vec == NULL)
2879 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002880
Johann Baudy69e3c752009-05-18 22:11:22 -07002881 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002882 struct page *page;
2883 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002884 int pg_num;
2885
Changli Gaoc56b4d92010-12-01 02:52:57 +00002886 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2887 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002888 err = vm_insert_page(vma, start, page);
2889 if (unlikely(err))
2890 goto out;
2891 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002892 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002893 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002896
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002897 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 vma->vm_ops = &packet_mmap_ops;
2899 err = 0;
2900
2901out:
Herbert Xu905db442009-01-30 14:12:06 -08002902 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return err;
2904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002906static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 .family = PF_PACKET,
2908 .owner = THIS_MODULE,
2909 .release = packet_release,
2910 .bind = packet_bind_spkt,
2911 .connect = sock_no_connect,
2912 .socketpair = sock_no_socketpair,
2913 .accept = sock_no_accept,
2914 .getname = packet_getname_spkt,
2915 .poll = datagram_poll,
2916 .ioctl = packet_ioctl,
2917 .listen = sock_no_listen,
2918 .shutdown = sock_no_shutdown,
2919 .setsockopt = sock_no_setsockopt,
2920 .getsockopt = sock_no_getsockopt,
2921 .sendmsg = packet_sendmsg_spkt,
2922 .recvmsg = packet_recvmsg,
2923 .mmap = sock_no_mmap,
2924 .sendpage = sock_no_sendpage,
2925};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002927static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 .family = PF_PACKET,
2929 .owner = THIS_MODULE,
2930 .release = packet_release,
2931 .bind = packet_bind,
2932 .connect = sock_no_connect,
2933 .socketpair = sock_no_socketpair,
2934 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002935 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .poll = packet_poll,
2937 .ioctl = packet_ioctl,
2938 .listen = sock_no_listen,
2939 .shutdown = sock_no_shutdown,
2940 .setsockopt = packet_setsockopt,
2941 .getsockopt = packet_getsockopt,
2942 .sendmsg = packet_sendmsg,
2943 .recvmsg = packet_recvmsg,
2944 .mmap = packet_mmap,
2945 .sendpage = sock_no_sendpage,
2946};
2947
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002948static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 .family = PF_PACKET,
2950 .create = packet_create,
2951 .owner = THIS_MODULE,
2952};
2953
2954static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002955 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956};
2957
2958#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002961 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Denis V. Luneve372c412007-11-19 22:31:54 -08002963 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002964
2965 rcu_read_lock();
2966 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
2969static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2970{
Herbert Xu1bf40952007-12-16 14:04:02 -08002971 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002972 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00002976 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
stephen hemminger808f5112010-02-22 07:57:18 +00002978 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002981static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 if (v == SEQ_START_TOKEN)
2984 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2985 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00002986 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 const struct packet_sock *po = pkt_sk(s);
2988
2989 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002990 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 s,
2992 atomic_read(&s->sk_refcnt),
2993 s->sk_type,
2994 ntohs(po->num),
2995 po->ifindex,
2996 po->running,
2997 atomic_read(&s->sk_rmem_alloc),
2998 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002999 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001
3002 return 0;
3003}
3004
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003005static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 .start = packet_seq_start,
3007 .next = packet_seq_next,
3008 .stop = packet_seq_stop,
3009 .show = packet_seq_show,
3010};
3011
3012static int packet_seq_open(struct inode *inode, struct file *file)
3013{
Denis V. Luneve372c412007-11-19 22:31:54 -08003014 return seq_open_net(inode, file, &packet_seq_ops,
3015 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Arjan van de Venda7071d2007-02-12 00:55:36 -08003018static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 .owner = THIS_MODULE,
3020 .open = packet_seq_open,
3021 .read = seq_read,
3022 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003023 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024};
3025
3026#endif
3027
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003028static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003029{
stephen hemminger808f5112010-02-22 07:57:18 +00003030 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08003031 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003032
3033 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
3034 return -ENOMEM;
3035
3036 return 0;
3037}
3038
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003039static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003040{
3041 proc_net_remove(net, "packet");
3042}
3043
3044static struct pernet_operations packet_net_ops = {
3045 .init = packet_net_init,
3046 .exit = packet_net_exit,
3047};
3048
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050static void __exit packet_exit(void)
3051{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003053 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 sock_unregister(PF_PACKET);
3055 proto_unregister(&packet_proto);
3056}
3057
3058static int __init packet_init(void)
3059{
3060 int rc = proto_register(&packet_proto, 0);
3061
3062 if (rc != 0)
3063 goto out;
3064
3065 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003066 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068out:
3069 return rc;
3070}
3071
3072module_init(packet_init);
3073module_exit(packet_exit);
3074MODULE_LICENSE("GPL");
3075MODULE_ALIAS_NETPROTO(PF_PACKET);