blob: aec50a1e9849ce74d1ff1bc520e998e5fc0e4674 [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;
David S. Miller7736d332011-07-05 01:43:20 -0700226 u8 defrag;
David S. Millerdc99f602011-07-05 01:45:05 -0700227 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
David S. Miller95ec3eb2011-07-06 01:56:38 -0700450static struct sock *fanout_demux_cpu(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
451{
452 unsigned int cpu = smp_processor_id();
453
454 return f->arr[cpu % num];
455}
456
David S. Miller7736d332011-07-05 01:43:20 -0700457static struct sk_buff *fanout_check_defrag(struct sk_buff *skb)
458{
459 const struct iphdr *iph;
460 u32 len;
461
462 if (skb->protocol != htons(ETH_P_IP))
463 return skb;
464
465 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
466 return skb;
467
468 iph = ip_hdr(skb);
469 if (iph->ihl < 5 || iph->version != 4)
470 return skb;
471 if (!pskb_may_pull(skb, iph->ihl*4))
472 return skb;
473 iph = ip_hdr(skb);
474 len = ntohs(iph->tot_len);
475 if (skb->len < len || len < (iph->ihl * 4))
476 return skb;
477
478 if (ip_is_fragment(ip_hdr(skb))) {
David S. Milleraec27312011-07-06 07:30:59 -0700479 skb = skb_share_check(skb, GFP_ATOMIC);
David S. Miller7736d332011-07-05 01:43:20 -0700480 if (skb) {
481 if (pskb_trim_rcsum(skb, len))
482 return skb;
483 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
484 if (ip_defrag(skb, IP_DEFRAG_AF_PACKET))
485 return NULL;
486 skb->rxhash = 0;
487 }
488 }
489 return skb;
490}
491
David S. Miller95ec3eb2011-07-06 01:56:38 -0700492static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
493 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -0700494{
495 struct packet_fanout *f = pt->af_packet_priv;
496 unsigned int num = f->num_members;
497 struct packet_sock *po;
498 struct sock *sk;
499
500 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
501 !num) {
502 kfree_skb(skb);
503 return 0;
504 }
505
David S. Miller95ec3eb2011-07-06 01:56:38 -0700506 switch (f->type) {
507 case PACKET_FANOUT_HASH:
508 default:
509 if (f->defrag) {
510 skb = fanout_check_defrag(skb);
511 if (!skb)
512 return 0;
513 }
514 skb_get_rxhash(skb);
515 sk = fanout_demux_hash(f, skb, num);
516 break;
517 case PACKET_FANOUT_LB:
518 sk = fanout_demux_lb(f, skb, num);
519 break;
520 case PACKET_FANOUT_CPU:
521 sk = fanout_demux_cpu(f, skb, num);
522 break;
David S. Miller7736d332011-07-05 01:43:20 -0700523 }
524
David S. Millerdc99f602011-07-05 01:45:05 -0700525 po = pkt_sk(sk);
526
527 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
528}
529
530static DEFINE_MUTEX(fanout_mutex);
531static LIST_HEAD(fanout_list);
532
533static void __fanout_link(struct sock *sk, struct packet_sock *po)
534{
535 struct packet_fanout *f = po->fanout;
536
537 spin_lock(&f->lock);
538 f->arr[f->num_members] = sk;
539 smp_wmb();
540 f->num_members++;
541 spin_unlock(&f->lock);
542}
543
544static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
545{
546 struct packet_fanout *f = po->fanout;
547 int i;
548
549 spin_lock(&f->lock);
550 for (i = 0; i < f->num_members; i++) {
551 if (f->arr[i] == sk)
552 break;
553 }
554 BUG_ON(i >= f->num_members);
555 f->arr[i] = f->arr[f->num_members - 1];
556 f->num_members--;
557 spin_unlock(&f->lock);
558}
559
David S. Miller7736d332011-07-05 01:43:20 -0700560static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -0700561{
562 struct packet_sock *po = pkt_sk(sk);
563 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -0700564 u8 type = type_flags & 0xff;
565 u8 defrag = (type_flags & PACKET_FANOUT_FLAG_DEFRAG) ? 1 : 0;
David S. Millerdc99f602011-07-05 01:45:05 -0700566 int err;
567
568 switch (type) {
569 case PACKET_FANOUT_HASH:
570 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -0700571 case PACKET_FANOUT_CPU:
David S. Millerdc99f602011-07-05 01:45:05 -0700572 break;
573 default:
574 return -EINVAL;
575 }
576
577 if (!po->running)
578 return -EINVAL;
579
580 if (po->fanout)
581 return -EALREADY;
582
583 mutex_lock(&fanout_mutex);
584 match = NULL;
585 list_for_each_entry(f, &fanout_list, list) {
586 if (f->id == id &&
587 read_pnet(&f->net) == sock_net(sk)) {
588 match = f;
589 break;
590 }
591 }
David S. Miller7736d332011-07-05 01:43:20 -0700592 if (match && match->defrag != defrag)
593 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -0700594 if (!match) {
595 match = kzalloc(sizeof(*match), GFP_KERNEL);
596 if (match) {
597 write_pnet(&match->net, sock_net(sk));
598 match->id = id;
599 match->type = type;
David S. Miller7736d332011-07-05 01:43:20 -0700600 match->defrag = defrag;
David S. Millerdc99f602011-07-05 01:45:05 -0700601 atomic_set(&match->rr_cur, 0);
602 INIT_LIST_HEAD(&match->list);
603 spin_lock_init(&match->lock);
604 atomic_set(&match->sk_ref, 0);
605 match->prot_hook.type = po->prot_hook.type;
606 match->prot_hook.dev = po->prot_hook.dev;
David S. Miller95ec3eb2011-07-06 01:56:38 -0700607 match->prot_hook.func = packet_rcv_fanout;
David S. Millerdc99f602011-07-05 01:45:05 -0700608 match->prot_hook.af_packet_priv = match;
609 dev_add_pack(&match->prot_hook);
610 list_add(&match->list, &fanout_list);
611 }
612 }
613 err = -ENOMEM;
614 if (match) {
615 err = -EINVAL;
616 if (match->type == type &&
617 match->prot_hook.type == po->prot_hook.type &&
618 match->prot_hook.dev == po->prot_hook.dev) {
619 err = -ENOSPC;
620 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
621 __dev_remove_pack(&po->prot_hook);
622 po->fanout = match;
623 atomic_inc(&match->sk_ref);
624 __fanout_link(sk, po);
625 err = 0;
626 }
627 }
628 }
629 mutex_unlock(&fanout_mutex);
630 return err;
631}
632
633static void fanout_release(struct sock *sk)
634{
635 struct packet_sock *po = pkt_sk(sk);
636 struct packet_fanout *f;
637
638 f = po->fanout;
639 if (!f)
640 return;
641
642 po->fanout = NULL;
643
644 mutex_lock(&fanout_mutex);
645 if (atomic_dec_and_test(&f->sk_ref)) {
646 list_del(&f->list);
647 dev_remove_pack(&f->prot_hook);
648 kfree(f);
649 }
650 mutex_unlock(&fanout_mutex);
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800653static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800655static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000657static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
658 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct sock *sk;
661 struct sockaddr_pkt *spkt;
662
663 /*
664 * When we registered the protocol we saved the socket in the data
665 * field for just this event.
666 */
667
668 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * Yank back the headers [hope the device set this
672 * right or kerboom...]
673 *
674 * Incoming packets have ll header pulled,
675 * push it back.
676 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700677 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * so that this procedure is noop.
679 */
680
681 if (skb->pkt_type == PACKET_LOOPBACK)
682 goto out;
683
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800684 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800685 goto out;
686
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000687 skb = skb_share_check(skb, GFP_ATOMIC);
688 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto oom;
690
691 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000692 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Phil Oester84531c22005-07-12 11:57:52 -0700694 /* drop conntrack reference */
695 nf_reset(skb);
696
Herbert Xuffbc6112007-02-04 23:33:10 -0800697 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700699 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /*
702 * The SOCK_PACKET socket receives _all_ frames.
703 */
704
705 spkt->spkt_family = dev->type;
706 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
707 spkt->spkt_protocol = skb->protocol;
708
709 /*
710 * Charge the memory to the socket. This is done specifically
711 * to prevent sockets using all the memory up.
712 */
713
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000714 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return 0;
716
717out:
718 kfree_skb(skb);
719oom:
720 return 0;
721}
722
723
724/*
725 * Output a raw packet to a device layer. This bypasses all the other
726 * protocol layers and you must therefore supply it with a complete frame
727 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
730 struct msghdr *msg, size_t len)
731{
732 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000733 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000734 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000736 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900740 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000743 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000745 return -EINVAL;
746 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
747 proto = saddr->spkt_protocol;
748 } else
749 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900752 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754
755 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000756retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100757 rcu_read_lock();
758 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 err = -ENODEV;
760 if (dev == NULL)
761 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900762
David S. Millerd5e76b02007-01-25 19:30:36 -0800763 err = -ENETDOWN;
764 if (!(dev->flags & IFF_UP))
765 goto out_unlock;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000768 * You may not queue a frame bigger than the mtu. This is the lowest level
769 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +0000773 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto out_unlock;
775
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000776 if (!skb) {
777 size_t reserved = LL_RESERVED_SPACE(dev);
778 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000780 rcu_read_unlock();
781 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
782 if (skb == NULL)
783 return -ENOBUFS;
784 /* FIXME: Save some space for broken drivers that write a hard
785 * header at transmission time by themselves. PPP is the notable
786 * one here. This should really be fixed at the driver level.
787 */
788 skb_reserve(skb, reserved);
789 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900790
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000791 /* Try to align data part correctly */
792 if (hhlen) {
793 skb->data -= hhlen;
794 skb->tail -= hhlen;
795 if (len < hhlen)
796 skb_reset_network_header(skb);
797 }
798 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
799 if (err)
800 goto out_free;
801 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Ben Greear57f89bf2011-02-11 09:35:18 +0000804 if (len > (dev->mtu + dev->hard_header_len)) {
805 /* Earlier code assumed this would be a VLAN pkt,
806 * double-check this now that we have the actual
807 * packet in hand.
808 */
809 struct ethhdr *ehdr;
810 skb_reset_mac_header(skb);
811 ehdr = eth_hdr(skb);
812 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
813 err = -EMSGSIZE;
814 goto out_unlock;
815 }
816 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 skb->protocol = proto;
819 skb->dev = dev;
820 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000821 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000822 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000823 if (err < 0)
824 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100827 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000828 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100831 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000832out_free:
833 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return err;
835}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Eric Dumazet62ab0812010-12-06 20:50:09 +0000837static inline unsigned int run_filter(const struct sk_buff *skb,
838 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800839 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct sk_filter *filter;
842
Eric Dumazet80f8f102011-01-18 07:46:52 +0000843 rcu_read_lock();
844 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800845 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +0000846 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000847 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
David S. Millerdbcb5852007-01-24 15:21:02 -0800849 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000853 * This function makes lazy skb cloning in hope that most of packets
854 * are discarded by BPF.
855 *
856 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
857 * and skb->cb are mangled. It works because (and until) packets
858 * falling here are owned by current CPU. Output packets are cloned
859 * by dev_queue_xmit_nit(), input packets are processed by net_bh
860 * sequencially, so that if we return skb to original state on exit,
861 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
863
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000864static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
865 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct sock *sk;
868 struct sockaddr_ll *sll;
869 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000870 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800872 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (skb->pkt_type == PACKET_LOOPBACK)
875 goto drop;
876
877 sk = pt->af_packet_priv;
878 po = pkt_sk(sk);
879
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800880 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800881 goto drop;
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 skb->dev = dev;
884
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700885 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000887 * exported to higher levels.
888 *
889 * Otherwise, the device hides details of its frame
890 * structure, so that corresponding packet head is
891 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
893 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700894 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 else if (skb->pkt_type == PACKET_OUTGOING) {
896 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300897 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 }
900
901 snaplen = skb->len;
902
David S. Millerdbcb5852007-01-24 15:21:02 -0800903 res = run_filter(skb, sk, snaplen);
904 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700905 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800906 if (snaplen > res)
907 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
910 (unsigned)sk->sk_rcvbuf)
911 goto drop_n_acct;
912
913 if (skb_shared(skb)) {
914 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
915 if (nskb == NULL)
916 goto drop_n_acct;
917
918 if (skb_head != skb->data) {
919 skb->data = skb_head;
920 skb->len = skb_len;
921 }
922 kfree_skb(skb);
923 skb = nskb;
924 }
925
Herbert Xuffbc6112007-02-04 23:33:10 -0800926 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
927 sizeof(skb->cb));
928
929 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 sll->sll_family = AF_PACKET;
931 sll->sll_hatype = dev->type;
932 sll->sll_protocol = skb->protocol;
933 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800934 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700935 sll->sll_ifindex = orig_dev->ifindex;
936 else
937 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700939 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Herbert Xuffbc6112007-02-04 23:33:10 -0800941 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (pskb_trim(skb, snaplen))
944 goto drop_n_acct;
945
946 skb_set_owner_r(skb, sk);
947 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000948 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Phil Oester84531c22005-07-12 11:57:52 -0700950 /* drop conntrack reference */
951 nf_reset(skb);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 spin_lock(&sk->sk_receive_queue.lock);
954 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700955 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 __skb_queue_tail(&sk->sk_receive_queue, skb);
957 spin_unlock(&sk->sk_receive_queue.lock);
958 sk->sk_data_ready(sk, skb->len);
959 return 0;
960
961drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700962 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964drop_n_restore:
965 if (skb_head != skb->data && skb_shared(skb)) {
966 skb->data = skb_head;
967 skb->len = skb_len;
968 }
969drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000970 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return 0;
972}
973
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000974static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
975 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct sock *sk;
978 struct packet_sock *po;
979 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700980 union {
981 struct tpacket_hdr *h1;
982 struct tpacket2_hdr *h2;
983 void *raw;
984 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000985 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800987 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700989 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700991 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700992 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700993 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (skb->pkt_type == PACKET_LOOPBACK)
996 goto drop;
997
998 sk = pt->af_packet_priv;
999 po = pkt_sk(sk);
1000
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001001 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001002 goto drop;
1003
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001004 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001006 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 else if (skb->pkt_type == PACKET_OUTGOING) {
1008 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001009 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 }
1012
Herbert Xu8dc41942007-02-04 23:31:32 -08001013 if (skb->ip_summed == CHECKSUM_PARTIAL)
1014 status |= TP_STATUS_CSUMNOTREADY;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 snaplen = skb->len;
1017
David S. Millerdbcb5852007-01-24 15:21:02 -08001018 res = run_filter(skb, sk, snaplen);
1019 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001020 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001021 if (snaplen > res)
1022 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07001025 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
1026 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001028 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001029 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07001030 (maclen < 16 ? 16 : maclen)) +
1031 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 macoff = netoff - maclen;
1033 }
1034
Johann Baudy69e3c752009-05-18 22:11:22 -07001035 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (po->copy_thresh &&
1037 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
1038 (unsigned)sk->sk_rcvbuf) {
1039 if (skb_shared(skb)) {
1040 copy_skb = skb_clone(skb, GFP_ATOMIC);
1041 } else {
1042 copy_skb = skb_get(skb);
1043 skb_head = skb->data;
1044 }
1045 if (copy_skb)
1046 skb_set_owner_r(copy_skb, sk);
1047 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001048 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if ((int)snaplen < 0)
1050 snaplen = 0;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07001054 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001055 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -07001057 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 po->stats.tp_packets++;
1059 if (copy_skb) {
1060 status |= TP_STATUS_COPY;
1061 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1062 }
1063 if (!po->stats.tp_drops)
1064 status &= ~TP_STATUS_LOSING;
1065 spin_unlock(&sk->sk_receive_queue.lock);
1066
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001067 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001069 switch (po->tp_version) {
1070 case TPACKET_V1:
1071 h.h1->tp_len = skb->len;
1072 h.h1->tp_snaplen = snaplen;
1073 h.h1->tp_mac = macoff;
1074 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -07001075 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
1076 && shhwtstamps->syststamp.tv64)
1077 tv = ktime_to_timeval(shhwtstamps->syststamp);
1078 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
1079 && shhwtstamps->hwtstamp.tv64)
1080 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
1081 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001082 tv = ktime_to_timeval(skb->tstamp);
1083 else
1084 do_gettimeofday(&tv);
1085 h.h1->tp_sec = tv.tv_sec;
1086 h.h1->tp_usec = tv.tv_usec;
1087 hdrlen = sizeof(*h.h1);
1088 break;
1089 case TPACKET_V2:
1090 h.h2->tp_len = skb->len;
1091 h.h2->tp_snaplen = snaplen;
1092 h.h2->tp_mac = macoff;
1093 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -07001094 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
1095 && shhwtstamps->syststamp.tv64)
1096 ts = ktime_to_timespec(shhwtstamps->syststamp);
1097 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
1098 && shhwtstamps->hwtstamp.tv64)
1099 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
1100 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001101 ts = ktime_to_timespec(skb->tstamp);
1102 else
1103 getnstimeofday(&ts);
1104 h.h2->tp_sec = ts.tv_sec;
1105 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +00001106 if (vlan_tx_tag_present(skb)) {
1107 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
1108 status |= TP_STATUS_VLAN_VALID;
1109 } else {
1110 h.h2->tp_vlan_tci = 0;
1111 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001112 h.h2->tp_padding = 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001113 hdrlen = sizeof(*h.h2);
1114 break;
1115 default:
1116 BUG();
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001119 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001120 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 sll->sll_family = AF_PACKET;
1122 sll->sll_hatype = dev->type;
1123 sll->sll_protocol = skb->protocol;
1124 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001125 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001126 sll->sll_ifindex = orig_dev->ifindex;
1127 else
1128 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001130 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -08001131 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +00001132#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 {
Changli Gao0af55bb2010-12-01 02:52:20 +00001134 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Changli Gao0af55bb2010-12-01 02:52:20 +00001136 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
1137 for (start = h.raw; start < end; start += PAGE_SIZE)
1138 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Changli Gaof6dafa92010-12-07 04:26:16 +00001140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 sk->sk_data_ready(sk, 0);
1143
1144drop_n_restore:
1145 if (skb_head != skb->data && skb_shared(skb)) {
1146 skb->data = skb_head;
1147 skb->len = skb_len;
1148 }
1149drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001150 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152
1153ring_is_full:
1154 po->stats.tp_drops++;
1155 spin_unlock(&sk->sk_receive_queue.lock);
1156
1157 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +00001158 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto drop_n_restore;
1160}
1161
Johann Baudy69e3c752009-05-18 22:11:22 -07001162static void tpacket_destruct_skb(struct sk_buff *skb)
1163{
1164 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001165 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -07001166
1167 BUG_ON(skb == NULL);
1168
1169 if (likely(po->tx_ring.pg_vec)) {
1170 ph = skb_shinfo(skb)->destructor_arg;
1171 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
1172 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
1173 atomic_dec(&po->tx_ring.pending);
1174 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
1175 }
1176
1177 sock_wfree(skb);
1178}
1179
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001180static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
1181 void *frame, struct net_device *dev, int size_max,
1182 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -07001183{
1184 union {
1185 struct tpacket_hdr *h1;
1186 struct tpacket2_hdr *h2;
1187 void *raw;
1188 } ph;
1189 int to_write, offset, len, tp_len, nr_frags, len_max;
1190 struct socket *sock = po->sk.sk_socket;
1191 struct page *page;
1192 void *data;
1193 int err;
1194
1195 ph.raw = frame;
1196
1197 skb->protocol = proto;
1198 skb->dev = dev;
1199 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001200 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -07001201 skb_shinfo(skb)->destructor_arg = ph.raw;
1202
1203 switch (po->tp_version) {
1204 case TPACKET_V2:
1205 tp_len = ph.h2->tp_len;
1206 break;
1207 default:
1208 tp_len = ph.h1->tp_len;
1209 break;
1210 }
1211 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001212 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -07001213 return -EMSGSIZE;
1214 }
1215
1216 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1217 skb_reset_network_header(skb);
1218
1219 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
1220 to_write = tp_len;
1221
1222 if (sock->type == SOCK_DGRAM) {
1223 err = dev_hard_header(skb, dev, ntohs(proto), addr,
1224 NULL, tp_len);
1225 if (unlikely(err < 0))
1226 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001227 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -07001228 /* net device doesn't like empty head */
1229 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001230 pr_err("packet size is too short (%d < %d)\n",
1231 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001232 return -EINVAL;
1233 }
1234
1235 skb_push(skb, dev->hard_header_len);
1236 err = skb_store_bits(skb, 0, data,
1237 dev->hard_header_len);
1238 if (unlikely(err))
1239 return err;
1240
1241 data += dev->hard_header_len;
1242 to_write -= dev->hard_header_len;
1243 }
1244
1245 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001246 offset = offset_in_page(data);
1247 len_max = PAGE_SIZE - offset;
1248 len = ((to_write > len_max) ? len_max : to_write);
1249
1250 skb->data_len = to_write;
1251 skb->len += to_write;
1252 skb->truesize += to_write;
1253 atomic_add(to_write, &po->sk.sk_wmem_alloc);
1254
1255 while (likely(to_write)) {
1256 nr_frags = skb_shinfo(skb)->nr_frags;
1257
1258 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001259 pr_err("Packet exceed the number of skb frags(%lu)\n",
1260 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07001261 return -EFAULT;
1262 }
1263
Changli Gao0af55bb2010-12-01 02:52:20 +00001264 page = pgv_to_page(data);
1265 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07001266 flush_dcache_page(page);
1267 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00001268 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001269 to_write -= len;
1270 offset = 0;
1271 len_max = PAGE_SIZE;
1272 len = ((to_write > len_max) ? len_max : to_write);
1273 }
1274
1275 return tp_len;
1276}
1277
1278static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
1279{
Johann Baudy69e3c752009-05-18 22:11:22 -07001280 struct sk_buff *skb;
1281 struct net_device *dev;
1282 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001283 bool need_rls_dev = false;
1284 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001285 void *ph;
1286 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -07001287 int tp_len, size_max;
1288 unsigned char *addr;
1289 int len_sum = 0;
1290 int status = 0;
1291
Johann Baudy69e3c752009-05-18 22:11:22 -07001292 mutex_lock(&po->pg_vec_lock);
1293
1294 err = -EBUSY;
1295 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001296 dev = po->prot_hook.dev;
Johann Baudy69e3c752009-05-18 22:11:22 -07001297 proto = po->num;
1298 addr = NULL;
1299 } else {
1300 err = -EINVAL;
1301 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1302 goto out;
1303 if (msg->msg_namelen < (saddr->sll_halen
1304 + offsetof(struct sockaddr_ll,
1305 sll_addr)))
1306 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07001307 proto = saddr->sll_protocol;
1308 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001309 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
1310 need_rls_dev = true;
Johann Baudy69e3c752009-05-18 22:11:22 -07001311 }
1312
Johann Baudy69e3c752009-05-18 22:11:22 -07001313 err = -ENXIO;
1314 if (unlikely(dev == NULL))
1315 goto out;
1316
1317 reserve = dev->hard_header_len;
1318
1319 err = -ENETDOWN;
1320 if (unlikely(!(dev->flags & IFF_UP)))
1321 goto out_put;
1322
1323 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001324 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001325
1326 if (size_max > dev->mtu + reserve)
1327 size_max = dev->mtu + reserve;
1328
1329 do {
1330 ph = packet_current_frame(po, &po->tx_ring,
1331 TP_STATUS_SEND_REQUEST);
1332
1333 if (unlikely(ph == NULL)) {
1334 schedule();
1335 continue;
1336 }
1337
1338 status = TP_STATUS_SEND_REQUEST;
1339 skb = sock_alloc_send_skb(&po->sk,
1340 LL_ALLOCATED_SPACE(dev)
1341 + sizeof(struct sockaddr_ll),
1342 0, &err);
1343
1344 if (unlikely(skb == NULL))
1345 goto out_status;
1346
1347 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1348 addr);
1349
1350 if (unlikely(tp_len < 0)) {
1351 if (po->tp_loss) {
1352 __packet_set_status(po, ph,
1353 TP_STATUS_AVAILABLE);
1354 packet_increment_head(&po->tx_ring);
1355 kfree_skb(skb);
1356 continue;
1357 } else {
1358 status = TP_STATUS_WRONG_FORMAT;
1359 err = tp_len;
1360 goto out_status;
1361 }
1362 }
1363
1364 skb->destructor = tpacket_destruct_skb;
1365 __packet_set_status(po, ph, TP_STATUS_SENDING);
1366 atomic_inc(&po->tx_ring.pending);
1367
1368 status = TP_STATUS_SEND_REQUEST;
1369 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001370 if (unlikely(err > 0)) {
1371 err = net_xmit_errno(err);
1372 if (err && __packet_get_status(po, ph) ==
1373 TP_STATUS_AVAILABLE) {
1374 /* skb was destructed already */
1375 skb = NULL;
1376 goto out_status;
1377 }
1378 /*
1379 * skb was dropped but not destructed yet;
1380 * let's treat it like congestion or err < 0
1381 */
1382 err = 0;
1383 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001384 packet_increment_head(&po->tx_ring);
1385 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001386 } while (likely((ph != NULL) ||
1387 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1388 (atomic_read(&po->tx_ring.pending))))
1389 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001390
1391 err = len_sum;
1392 goto out_put;
1393
Johann Baudy69e3c752009-05-18 22:11:22 -07001394out_status:
1395 __packet_set_status(po, ph, status);
1396 kfree_skb(skb);
1397out_put:
Ben Greear827d9782011-06-01 07:18:53 +00001398 if (need_rls_dev)
1399 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07001400out:
1401 mutex_unlock(&po->pg_vec_lock);
1402 return err;
1403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001405static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1406 size_t reserve, size_t len,
1407 size_t linear, int noblock,
1408 int *err)
1409{
1410 struct sk_buff *skb;
1411
1412 /* Under a page? Don't bother with paged skb. */
1413 if (prepad + len < PAGE_SIZE || !linear)
1414 linear = len;
1415
1416 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1417 err);
1418 if (!skb)
1419 return NULL;
1420
1421 skb_reserve(skb, reserve);
1422 skb_put(skb, linear);
1423 skb->data_len = len - linear;
1424 skb->len += len - linear;
1425
1426 return skb;
1427}
1428
Johann Baudy69e3c752009-05-18 22:11:22 -07001429static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct msghdr *msg, size_t len)
1431{
1432 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001433 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct sk_buff *skb;
1435 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001436 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001437 bool need_rls_dev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00001439 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001440 struct virtio_net_hdr vnet_hdr = { 0 };
1441 int offset = 0;
1442 int vnet_hdr_len;
1443 struct packet_sock *po = pkt_sk(sk);
1444 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001447 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001451 dev = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 proto = po->num;
1453 addr = NULL;
1454 } else {
1455 err = -EINVAL;
1456 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1457 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001458 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 proto = saddr->sll_protocol;
1461 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001462 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
1463 need_rls_dev = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 err = -ENXIO;
1467 if (dev == NULL)
1468 goto out_unlock;
1469 if (sock->type == SOCK_RAW)
1470 reserve = dev->hard_header_len;
1471
David S. Millerd5e76b02007-01-25 19:30:36 -08001472 err = -ENETDOWN;
1473 if (!(dev->flags & IFF_UP))
1474 goto out_unlock;
1475
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001476 if (po->has_vnet_hdr) {
1477 vnet_hdr_len = sizeof(vnet_hdr);
1478
1479 err = -EINVAL;
1480 if (len < vnet_hdr_len)
1481 goto out_unlock;
1482
1483 len -= vnet_hdr_len;
1484
1485 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1486 vnet_hdr_len);
1487 if (err < 0)
1488 goto out_unlock;
1489
1490 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1491 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1492 vnet_hdr.hdr_len))
1493 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1494 vnet_hdr.csum_offset + 2;
1495
1496 err = -EINVAL;
1497 if (vnet_hdr.hdr_len > len)
1498 goto out_unlock;
1499
1500 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1501 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1502 case VIRTIO_NET_HDR_GSO_TCPV4:
1503 gso_type = SKB_GSO_TCPV4;
1504 break;
1505 case VIRTIO_NET_HDR_GSO_TCPV6:
1506 gso_type = SKB_GSO_TCPV6;
1507 break;
1508 case VIRTIO_NET_HDR_GSO_UDP:
1509 gso_type = SKB_GSO_UDP;
1510 break;
1511 default:
1512 goto out_unlock;
1513 }
1514
1515 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1516 gso_type |= SKB_GSO_TCP_ECN;
1517
1518 if (vnet_hdr.gso_size == 0)
1519 goto out_unlock;
1520
1521 }
1522 }
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +00001525 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto out_unlock;
1527
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001528 err = -ENOBUFS;
1529 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1530 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1531 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001532 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 goto out_unlock;
1534
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001535 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001537 err = -EINVAL;
1538 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001539 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001540 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001543 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (err)
1545 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001546 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001547 if (err < 0)
1548 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Ben Greear57f89bf2011-02-11 09:35:18 +00001550 if (!gso_type && (len > dev->mtu + reserve)) {
1551 /* Earlier code assumed this would be a VLAN pkt,
1552 * double-check this now that we have the actual
1553 * packet in hand.
1554 */
1555 struct ethhdr *ehdr;
1556 skb_reset_mac_header(skb);
1557 ehdr = eth_hdr(skb);
1558 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1559 err = -EMSGSIZE;
1560 goto out_free;
1561 }
1562 }
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 skb->protocol = proto;
1565 skb->dev = dev;
1566 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001567 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001569 if (po->has_vnet_hdr) {
1570 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1571 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1572 vnet_hdr.csum_offset)) {
1573 err = -EINVAL;
1574 goto out_free;
1575 }
1576 }
1577
1578 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1579 skb_shinfo(skb)->gso_type = gso_type;
1580
1581 /* Header must be checked, and gso_segs computed. */
1582 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1583 skb_shinfo(skb)->gso_segs = 0;
1584
1585 len += vnet_hdr_len;
1586 }
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Now send it
1590 */
1591
1592 err = dev_queue_xmit(skb);
1593 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1594 goto out_unlock;
1595
Ben Greear827d9782011-06-01 07:18:53 +00001596 if (need_rls_dev)
1597 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001599 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601out_free:
1602 kfree_skb(skb);
1603out_unlock:
Ben Greear827d9782011-06-01 07:18:53 +00001604 if (dev && need_rls_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 dev_put(dev);
1606out:
1607 return err;
1608}
1609
Johann Baudy69e3c752009-05-18 22:11:22 -07001610static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1611 struct msghdr *msg, size_t len)
1612{
Johann Baudy69e3c752009-05-18 22:11:22 -07001613 struct sock *sk = sock->sk;
1614 struct packet_sock *po = pkt_sk(sk);
1615 if (po->tx_ring.pg_vec)
1616 return tpacket_snd(po, msg);
1617 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001618 return packet_snd(sock, msg, len);
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/*
1622 * Close a PACKET socket. This is fairly simple. We immediately go
1623 * to 'closed' state and remove our protocol entry in the device list.
1624 */
1625
1626static int packet_release(struct socket *sock)
1627{
1628 struct sock *sk = sock->sk;
1629 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001630 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001631 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (!sk)
1634 return 0;
1635
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001636 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 po = pkt_sk(sk);
1638
stephen hemminger808f5112010-02-22 07:57:18 +00001639 spin_lock_bh(&net->packet.sklist_lock);
1640 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001641 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001642 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
stephen hemminger808f5112010-02-22 07:57:18 +00001644 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001645 unregister_prot_hook(sk, false);
Ben Greear160ff182011-06-01 07:18:52 +00001646 if (po->prot_hook.dev) {
1647 dev_put(po->prot_hook.dev);
1648 po->prot_hook.dev = NULL;
1649 }
stephen hemminger808f5112010-02-22 07:57:18 +00001650 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Johann Baudy69e3c752009-05-18 22:11:22 -07001654 memset(&req, 0, sizeof(req));
1655
1656 if (po->rx_ring.pg_vec)
1657 packet_set_ring(sk, &req, 1, 0);
1658
1659 if (po->tx_ring.pg_vec)
1660 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
David S. Millerdc99f602011-07-05 01:45:05 -07001662 fanout_release(sk);
1663
stephen hemminger808f5112010-02-22 07:57:18 +00001664 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * Now the socket is dead. No more input will appear.
1667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 sock_orphan(sk);
1669 sock->sk = NULL;
1670
1671 /* Purge queues */
1672
1673 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001674 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 sock_put(sk);
1677 return 0;
1678}
1679
1680/*
1681 * Attach a packet hook.
1682 */
1683
Al Viro0e11c912006-11-08 00:26:29 -08001684static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct packet_sock *po = pkt_sk(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07001687
1688 if (po->fanout)
1689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 lock_sock(sk);
1692
1693 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001694 unregister_prot_hook(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 po->num = protocol;
1696 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00001697 if (po->prot_hook.dev)
1698 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 po->prot_hook.dev = dev;
1700
1701 po->ifindex = dev ? dev->ifindex : 0;
1702
1703 if (protocol == 0)
1704 goto out_unlock;
1705
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001706 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07001707 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001708 } else {
1709 sk->sk_err = ENETDOWN;
1710 if (!sock_flag(sk, SOCK_DEAD))
1711 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
1714out_unlock:
1715 spin_unlock(&po->bind_lock);
1716 release_sock(sk);
1717 return 0;
1718}
1719
1720/*
1721 * Bind a packet socket to a device
1722 */
1723
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001724static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1725 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001727 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 char name[15];
1729 struct net_device *dev;
1730 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
1733 * Check legality
1734 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001735
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001736 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001738 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001740 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00001741 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return err;
1744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1747{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001748 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1749 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct net_device *dev = NULL;
1751 int err;
1752
1753
1754 /*
1755 * Check legality
1756 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (addr_len < sizeof(struct sockaddr_ll))
1759 return -EINVAL;
1760 if (sll->sll_family != AF_PACKET)
1761 return -EINVAL;
1762
1763 if (sll->sll_ifindex) {
1764 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001765 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (dev == NULL)
1767 goto out;
1768 }
1769 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771out:
1772 return err;
1773}
1774
1775static struct proto packet_proto = {
1776 .name = "PACKET",
1777 .owner = THIS_MODULE,
1778 .obj_size = sizeof(struct packet_sock),
1779};
1780
1781/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001782 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
1784
Eric Paris3f378b62009-11-05 22:18:14 -08001785static int packet_create(struct net *net, struct socket *sock, int protocol,
1786 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct sock *sk;
1789 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001790 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int err;
1792
1793 if (!capable(CAP_NET_RAW))
1794 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001795 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1796 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return -ESOCKTNOSUPPORT;
1798
1799 sock->state = SS_UNCONNECTED;
1800
1801 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001802 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (sk == NULL)
1804 goto out;
1805
1806 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (sock->type == SOCK_PACKET)
1808 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 sock_init_data(sock, sk);
1811
1812 po = pkt_sk(sk);
1813 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001814 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001817 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /*
1820 * Attach a protocol block
1821 */
1822
1823 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001824 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (sock->type == SOCK_PACKET)
1828 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 po->prot_hook.af_packet_priv = sk;
1831
Al Viro0e11c912006-11-08 00:26:29 -08001832 if (proto) {
1833 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07001834 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
stephen hemminger808f5112010-02-22 07:57:18 +00001837 spin_lock_bh(&net->packet.sklist_lock);
1838 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001839 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001840 spin_unlock_bh(&net->packet.sklist_lock);
1841
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843out:
1844 return err;
1845}
1846
Richard Cochraned85b562010-04-07 22:41:28 +00001847static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1848{
1849 struct sock_exterr_skb *serr;
1850 struct sk_buff *skb, *skb2;
1851 int copied, err;
1852
1853 err = -EAGAIN;
1854 skb = skb_dequeue(&sk->sk_error_queue);
1855 if (skb == NULL)
1856 goto out;
1857
1858 copied = skb->len;
1859 if (copied > len) {
1860 msg->msg_flags |= MSG_TRUNC;
1861 copied = len;
1862 }
1863 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1864 if (err)
1865 goto out_free_skb;
1866
1867 sock_recv_timestamp(msg, sk, skb);
1868
1869 serr = SKB_EXT_ERR(skb);
1870 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1871 sizeof(serr->ee), &serr->ee);
1872
1873 msg->msg_flags |= MSG_ERRQUEUE;
1874 err = copied;
1875
1876 /* Reset and regenerate socket error */
1877 spin_lock_bh(&sk->sk_error_queue.lock);
1878 sk->sk_err = 0;
1879 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1880 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1881 spin_unlock_bh(&sk->sk_error_queue.lock);
1882 sk->sk_error_report(sk);
1883 } else
1884 spin_unlock_bh(&sk->sk_error_queue.lock);
1885
1886out_free_skb:
1887 kfree_skb(skb);
1888out:
1889 return err;
1890}
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892/*
1893 * Pull a packet from our receive queue and hand it to the user.
1894 * If necessary we block.
1895 */
1896
1897static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1898 struct msghdr *msg, size_t len, int flags)
1899{
1900 struct sock *sk = sock->sk;
1901 struct sk_buff *skb;
1902 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001903 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001904 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001907 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto out;
1909
1910#if 0
1911 /* What error should we return now? EUNATTACH? */
1912 if (pkt_sk(sk)->ifindex < 0)
1913 return -ENODEV;
1914#endif
1915
Richard Cochraned85b562010-04-07 22:41:28 +00001916 if (flags & MSG_ERRQUEUE) {
1917 err = packet_recv_error(sk, msg, len);
1918 goto out;
1919 }
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * Call the generic datagram receiver. This handles all sorts
1923 * of horrible races and re-entrancy so we can forget about it
1924 * in the protocol layers.
1925 *
1926 * Now it will return ENETDOWN, if device have just gone down,
1927 * but then it will block.
1928 */
1929
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001930 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001933 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * handles the blocking we don't see and worry about blocking
1935 * retries.
1936 */
1937
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001938 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto out;
1940
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001941 if (pkt_sk(sk)->has_vnet_hdr) {
1942 struct virtio_net_hdr vnet_hdr = { 0 };
1943
1944 err = -EINVAL;
1945 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001946 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001947 goto out_free;
1948
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001949 len -= vnet_hdr_len;
1950
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001951 if (skb_is_gso(skb)) {
1952 struct skb_shared_info *sinfo = skb_shinfo(skb);
1953
1954 /* This is a hint as to how much should be linear. */
1955 vnet_hdr.hdr_len = skb_headlen(skb);
1956 vnet_hdr.gso_size = sinfo->gso_size;
1957 if (sinfo->gso_type & SKB_GSO_TCPV4)
1958 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1959 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1960 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1961 else if (sinfo->gso_type & SKB_GSO_UDP)
1962 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1963 else if (sinfo->gso_type & SKB_GSO_FCOE)
1964 goto out_free;
1965 else
1966 BUG();
1967 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1968 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1969 } else
1970 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1971
1972 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1973 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001974 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001975 vnet_hdr.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001976 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1977 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001978 } /* else everything is zero */
1979
1980 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1981 vnet_hdr_len);
1982 if (err < 0)
1983 goto out_free;
1984 }
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001987 * If the address length field is there to be filled in, we fill
1988 * it in now.
1989 */
1990
Herbert Xuffbc6112007-02-04 23:33:10 -08001991 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001992 if (sock->type == SOCK_PACKET)
1993 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1994 else
1995 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1996
1997 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * You lose any data beyond the buffer you gave. If it worries a
1999 * user program they can ask the device for its MTU anyway.
2000 */
2001
2002 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002003 if (copied > len) {
2004 copied = len;
2005 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007
2008 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2009 if (err)
2010 goto out_free;
2011
Neil Horman3b885782009-10-12 13:26:31 -07002012 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08002015 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
2016 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Herbert Xu8dc41942007-02-04 23:31:32 -08002018 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08002019 struct tpacket_auxdata aux;
2020
2021 aux.tp_status = TP_STATUS_USER;
2022 if (skb->ip_summed == CHECKSUM_PARTIAL)
2023 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
2024 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
2025 aux.tp_snaplen = skb->len;
2026 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002027 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00002028 if (vlan_tx_tag_present(skb)) {
2029 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
2030 aux.tp_status |= TP_STATUS_VLAN_VALID;
2031 } else {
2032 aux.tp_vlan_tci = 0;
2033 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07002034 aux.tp_padding = 0;
Herbert Xuffbc6112007-02-04 23:33:10 -08002035 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08002036 }
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * Free or return the buffer as appropriate. Again this
2040 * hides all the races and re-entrancy issues from us.
2041 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002042 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044out_free:
2045 skb_free_datagram(sk, skb);
2046out:
2047 return err;
2048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
2051 int *uaddr_len, int peer)
2052{
2053 struct net_device *dev;
2054 struct sock *sk = sock->sk;
2055
2056 if (peer)
2057 return -EOPNOTSUPP;
2058
2059 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002060 rcu_read_lock();
2061 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
2062 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002063 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002064 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002066 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 *uaddr_len = sizeof(*uaddr);
2068
2069 return 0;
2070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
2073 int *uaddr_len, int peer)
2074{
2075 struct net_device *dev;
2076 struct sock *sk = sock->sk;
2077 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00002078 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (peer)
2081 return -EOPNOTSUPP;
2082
2083 sll->sll_family = AF_PACKET;
2084 sll->sll_ifindex = po->ifindex;
2085 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002086 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002087 rcu_read_lock();
2088 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (dev) {
2090 sll->sll_hatype = dev->type;
2091 sll->sll_halen = dev->addr_len;
2092 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 } else {
2094 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
2095 sll->sll_halen = 0;
2096 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01002097 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002098 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 return 0;
2101}
2102
Wang Chen2aeb0b82008-07-14 20:49:46 -07002103static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
2104 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 switch (i->type) {
2107 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002108 if (i->alen != dev->addr_len)
2109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00002111 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00002113 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 break;
2115 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002116 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 break;
2118 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002119 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002121 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002122 if (i->alen != dev->addr_len)
2123 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002124 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002125 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002126 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002127 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002128 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002129 default:
2130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07002132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
2136{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002137 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (i->ifindex == dev->ifindex)
2139 packet_dev_mc(dev, i, what);
2140 }
2141}
2142
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002143static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct packet_sock *po = pkt_sk(sk);
2146 struct packet_mclist *ml, *i;
2147 struct net_device *dev;
2148 int err;
2149
2150 rtnl_lock();
2151
2152 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002153 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (!dev)
2155 goto done;
2156
2157 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00002158 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 goto done;
2160
2161 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002162 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (i == NULL)
2164 goto done;
2165
2166 err = 0;
2167 for (ml = po->mclist; ml; ml = ml->next) {
2168 if (ml->ifindex == mreq->mr_ifindex &&
2169 ml->type == mreq->mr_type &&
2170 ml->alen == mreq->mr_alen &&
2171 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2172 ml->count++;
2173 /* Free the new element ... */
2174 kfree(i);
2175 goto done;
2176 }
2177 }
2178
2179 i->type = mreq->mr_type;
2180 i->ifindex = mreq->mr_ifindex;
2181 i->alen = mreq->mr_alen;
2182 memcpy(i->addr, mreq->mr_address, i->alen);
2183 i->count = 1;
2184 i->next = po->mclist;
2185 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07002186 err = packet_dev_mc(dev, i, 1);
2187 if (err) {
2188 po->mclist = i->next;
2189 kfree(i);
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192done:
2193 rtnl_unlock();
2194 return err;
2195}
2196
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002197static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 struct packet_mclist *ml, **mlp;
2200
2201 rtnl_lock();
2202
2203 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
2204 if (ml->ifindex == mreq->mr_ifindex &&
2205 ml->type == mreq->mr_type &&
2206 ml->alen == mreq->mr_alen &&
2207 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2208 if (--ml->count == 0) {
2209 struct net_device *dev;
2210 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002211 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2212 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 kfree(ml);
2215 }
2216 rtnl_unlock();
2217 return 0;
2218 }
2219 }
2220 rtnl_unlock();
2221 return -EADDRNOTAVAIL;
2222}
2223
2224static void packet_flush_mclist(struct sock *sk)
2225{
2226 struct packet_sock *po = pkt_sk(sk);
2227 struct packet_mclist *ml;
2228
2229 if (!po->mclist)
2230 return;
2231
2232 rtnl_lock();
2233 while ((ml = po->mclist) != NULL) {
2234 struct net_device *dev;
2235
2236 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002237 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2238 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 kfree(ml);
2241 }
2242 rtnl_unlock();
2243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245static int
David S. Millerb7058842009-09-30 16:12:20 -07002246packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08002249 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int ret;
2251
2252 if (level != SOL_PACKET)
2253 return -ENOPROTOOPT;
2254
Johann Baudy69e3c752009-05-18 22:11:22 -07002255 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002256 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 case PACKET_DROP_MEMBERSHIP:
2258 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002259 struct packet_mreq_max mreq;
2260 int len = optlen;
2261 memset(&mreq, 0, sizeof(mreq));
2262 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002264 if (len > sizeof(mreq))
2265 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002266 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002268 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
2269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (optname == PACKET_ADD_MEMBERSHIP)
2271 ret = packet_mc_add(sk, &mreq);
2272 else
2273 ret = packet_mc_drop(sk, &mreq);
2274 return ret;
2275 }
David S. Millera2efcfa2007-05-29 13:12:50 -07002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07002278 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 {
2280 struct tpacket_req req;
2281
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002282 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002284 if (pkt_sk(sk)->has_vnet_hdr)
2285 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002286 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07002288 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290 case PACKET_COPY_THRESH:
2291 {
2292 int val;
2293
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002294 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002296 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return -EFAULT;
2298
2299 pkt_sk(sk)->copy_thresh = val;
2300 return 0;
2301 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002302 case PACKET_VERSION:
2303 {
2304 int val;
2305
2306 if (optlen != sizeof(val))
2307 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002308 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002309 return -EBUSY;
2310 if (copy_from_user(&val, optval, sizeof(val)))
2311 return -EFAULT;
2312 switch (val) {
2313 case TPACKET_V1:
2314 case TPACKET_V2:
2315 po->tp_version = val;
2316 return 0;
2317 default:
2318 return -EINVAL;
2319 }
2320 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07002321 case PACKET_RESERVE:
2322 {
2323 unsigned int val;
2324
2325 if (optlen != sizeof(val))
2326 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002327 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07002328 return -EBUSY;
2329 if (copy_from_user(&val, optval, sizeof(val)))
2330 return -EFAULT;
2331 po->tp_reserve = val;
2332 return 0;
2333 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002334 case PACKET_LOSS:
2335 {
2336 unsigned int val;
2337
2338 if (optlen != sizeof(val))
2339 return -EINVAL;
2340 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2341 return -EBUSY;
2342 if (copy_from_user(&val, optval, sizeof(val)))
2343 return -EFAULT;
2344 po->tp_loss = !!val;
2345 return 0;
2346 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002347 case PACKET_AUXDATA:
2348 {
2349 int val;
2350
2351 if (optlen < sizeof(val))
2352 return -EINVAL;
2353 if (copy_from_user(&val, optval, sizeof(val)))
2354 return -EFAULT;
2355
2356 po->auxdata = !!val;
2357 return 0;
2358 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002359 case PACKET_ORIGDEV:
2360 {
2361 int val;
2362
2363 if (optlen < sizeof(val))
2364 return -EINVAL;
2365 if (copy_from_user(&val, optval, sizeof(val)))
2366 return -EFAULT;
2367
2368 po->origdev = !!val;
2369 return 0;
2370 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002371 case PACKET_VNET_HDR:
2372 {
2373 int val;
2374
2375 if (sock->type != SOCK_RAW)
2376 return -EINVAL;
2377 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2378 return -EBUSY;
2379 if (optlen < sizeof(val))
2380 return -EINVAL;
2381 if (copy_from_user(&val, optval, sizeof(val)))
2382 return -EFAULT;
2383
2384 po->has_vnet_hdr = !!val;
2385 return 0;
2386 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002387 case PACKET_TIMESTAMP:
2388 {
2389 int val;
2390
2391 if (optlen != sizeof(val))
2392 return -EINVAL;
2393 if (copy_from_user(&val, optval, sizeof(val)))
2394 return -EFAULT;
2395
2396 po->tp_tstamp = val;
2397 return 0;
2398 }
David S. Millerdc99f602011-07-05 01:45:05 -07002399 case PACKET_FANOUT:
2400 {
2401 int val;
2402
2403 if (optlen != sizeof(val))
2404 return -EINVAL;
2405 if (copy_from_user(&val, optval, sizeof(val)))
2406 return -EFAULT;
2407
2408 return fanout_add(sk, val & 0xffff, val >> 16);
2409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 default:
2411 return -ENOPROTOOPT;
2412 }
2413}
2414
2415static int packet_getsockopt(struct socket *sock, int level, int optname,
2416 char __user *optval, int __user *optlen)
2417{
2418 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002419 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct sock *sk = sock->sk;
2421 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002422 void *data;
2423 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 if (level != SOL_PACKET)
2426 return -ENOPROTOOPT;
2427
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002428 if (get_user(len, optlen))
2429 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 if (len < 0)
2432 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002433
Johann Baudy69e3c752009-05-18 22:11:22 -07002434 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (len > sizeof(struct tpacket_stats))
2437 len = sizeof(struct tpacket_stats);
2438 spin_lock_bh(&sk->sk_receive_queue.lock);
2439 st = po->stats;
2440 memset(&po->stats, 0, sizeof(st));
2441 spin_unlock_bh(&sk->sk_receive_queue.lock);
2442 st.tp_packets += st.tp_drops;
2443
Herbert Xu8dc41942007-02-04 23:31:32 -08002444 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002446 case PACKET_AUXDATA:
2447 if (len > sizeof(int))
2448 len = sizeof(int);
2449 val = po->auxdata;
2450
2451 data = &val;
2452 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002453 case PACKET_ORIGDEV:
2454 if (len > sizeof(int))
2455 len = sizeof(int);
2456 val = po->origdev;
2457
2458 data = &val;
2459 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002460 case PACKET_VNET_HDR:
2461 if (len > sizeof(int))
2462 len = sizeof(int);
2463 val = po->has_vnet_hdr;
2464
2465 data = &val;
2466 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002467 case PACKET_VERSION:
2468 if (len > sizeof(int))
2469 len = sizeof(int);
2470 val = po->tp_version;
2471 data = &val;
2472 break;
2473 case PACKET_HDRLEN:
2474 if (len > sizeof(int))
2475 len = sizeof(int);
2476 if (copy_from_user(&val, optval, len))
2477 return -EFAULT;
2478 switch (val) {
2479 case TPACKET_V1:
2480 val = sizeof(struct tpacket_hdr);
2481 break;
2482 case TPACKET_V2:
2483 val = sizeof(struct tpacket2_hdr);
2484 break;
2485 default:
2486 return -EINVAL;
2487 }
2488 data = &val;
2489 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002490 case PACKET_RESERVE:
2491 if (len > sizeof(unsigned int))
2492 len = sizeof(unsigned int);
2493 val = po->tp_reserve;
2494 data = &val;
2495 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002496 case PACKET_LOSS:
2497 if (len > sizeof(unsigned int))
2498 len = sizeof(unsigned int);
2499 val = po->tp_loss;
2500 data = &val;
2501 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002502 case PACKET_TIMESTAMP:
2503 if (len > sizeof(int))
2504 len = sizeof(int);
2505 val = po->tp_tstamp;
2506 data = &val;
2507 break;
David S. Millerdc99f602011-07-05 01:45:05 -07002508 case PACKET_FANOUT:
2509 if (len > sizeof(int))
2510 len = sizeof(int);
2511 val = (po->fanout ?
2512 ((u32)po->fanout->id |
2513 ((u32)po->fanout->type << 16)) :
2514 0);
2515 data = &val;
2516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 default:
2518 return -ENOPROTOOPT;
2519 }
2520
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002521 if (put_user(len, optlen))
2522 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002523 if (copy_to_user(optval, data, len))
2524 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
2528
2529static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2530{
2531 struct sock *sk;
2532 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002533 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002534 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
stephen hemminger808f5112010-02-22 07:57:18 +00002536 rcu_read_lock();
2537 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct packet_sock *po = pkt_sk(sk);
2539
2540 switch (msg) {
2541 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (po->mclist)
2543 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002544 /* fallthrough */
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 case NETDEV_DOWN:
2547 if (dev->ifindex == po->ifindex) {
2548 spin_lock(&po->bind_lock);
2549 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07002550 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 sk->sk_err = ENETDOWN;
2552 if (!sock_flag(sk, SOCK_DEAD))
2553 sk->sk_error_report(sk);
2554 }
2555 if (msg == NETDEV_UNREGISTER) {
2556 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00002557 if (po->prot_hook.dev)
2558 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 po->prot_hook.dev = NULL;
2560 }
2561 spin_unlock(&po->bind_lock);
2562 }
2563 break;
2564 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002565 if (dev->ifindex == po->ifindex) {
2566 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002567 if (po->num)
2568 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00002569 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 break;
2572 }
2573 }
stephen hemminger808f5112010-02-22 07:57:18 +00002574 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return NOTIFY_DONE;
2576}
2577
2578
2579static int packet_ioctl(struct socket *sock, unsigned int cmd,
2580 unsigned long arg)
2581{
2582 struct sock *sk = sock->sk;
2583
Johann Baudy69e3c752009-05-18 22:11:22 -07002584 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002585 case SIOCOUTQ:
2586 {
2587 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002588
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002589 return put_user(amount, (int __user *)arg);
2590 }
2591 case SIOCINQ:
2592 {
2593 struct sk_buff *skb;
2594 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002596 spin_lock_bh(&sk->sk_receive_queue.lock);
2597 skb = skb_peek(&sk->sk_receive_queue);
2598 if (skb)
2599 amount = skb->len;
2600 spin_unlock_bh(&sk->sk_receive_queue.lock);
2601 return put_user(amount, (int __user *)arg);
2602 }
2603 case SIOCGSTAMP:
2604 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2605 case SIOCGSTAMPNS:
2606 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002609 case SIOCADDRT:
2610 case SIOCDELRT:
2611 case SIOCDARP:
2612 case SIOCGARP:
2613 case SIOCSARP:
2614 case SIOCGIFADDR:
2615 case SIOCSIFADDR:
2616 case SIOCGIFBRDADDR:
2617 case SIOCSIFBRDADDR:
2618 case SIOCGIFNETMASK:
2619 case SIOCSIFNETMASK:
2620 case SIOCGIFDSTADDR:
2621 case SIOCSIFDSTADDR:
2622 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002623 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624#endif
2625
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002626 default:
2627 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629 return 0;
2630}
2631
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002632static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 poll_table *wait)
2634{
2635 struct sock *sk = sock->sk;
2636 struct packet_sock *po = pkt_sk(sk);
2637 unsigned int mask = datagram_poll(file, sock, wait);
2638
2639 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002640 if (po->rx_ring.pg_vec) {
2641 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 mask |= POLLIN | POLLRDNORM;
2643 }
2644 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002645 spin_lock_bh(&sk->sk_write_queue.lock);
2646 if (po->tx_ring.pg_vec) {
2647 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2648 mask |= POLLOUT | POLLWRNORM;
2649 }
2650 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return mask;
2652}
2653
2654
2655/* Dirty? Well, I still did not learn better way to account
2656 * for user mmaps.
2657 */
2658
2659static void packet_mm_open(struct vm_area_struct *vma)
2660{
2661 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002662 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (sk)
2666 atomic_inc(&pkt_sk(sk)->mapped);
2667}
2668
2669static void packet_mm_close(struct vm_area_struct *vma)
2670{
2671 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002672 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (sk)
2676 atomic_dec(&pkt_sk(sk)->mapped);
2677}
2678
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002679static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002680 .open = packet_mm_open,
2681 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682};
2683
Neil Horman0e3125c2010-11-16 10:26:47 -08002684static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2685 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 int i;
2688
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002689 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002690 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002691 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002692 vfree(pg_vec[i].buffer);
2693 else
2694 free_pages((unsigned long)pg_vec[i].buffer,
2695 order);
2696 pg_vec[i].buffer = NULL;
2697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699 kfree(pg_vec);
2700}
2701
Changli Gaoc56b4d92010-12-01 02:52:57 +00002702static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002703{
Neil Horman0e3125c2010-11-16 10:26:47 -08002704 char *buffer = NULL;
2705 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2706 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002707
Neil Horman0e3125c2010-11-16 10:26:47 -08002708 buffer = (char *) __get_free_pages(gfp_flags, order);
2709
2710 if (buffer)
2711 return buffer;
2712
2713 /*
2714 * __get_free_pages failed, fall back to vmalloc
2715 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002716 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002717
2718 if (buffer)
2719 return buffer;
2720
2721 /*
2722 * vmalloc failed, lets dig into swap here
2723 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002724 gfp_flags &= ~__GFP_NORETRY;
2725 buffer = (char *)__get_free_pages(gfp_flags, order);
2726 if (buffer)
2727 return buffer;
2728
2729 /*
2730 * complete and utter failure
2731 */
2732 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002733}
2734
Neil Horman0e3125c2010-11-16 10:26:47 -08002735static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002736{
2737 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002738 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002739 int i;
2740
Neil Horman0e3125c2010-11-16 10:26:47 -08002741 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002742 if (unlikely(!pg_vec))
2743 goto out;
2744
2745 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002746 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002747 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002748 goto out_free_pgvec;
2749 }
2750
2751out:
2752 return pg_vec;
2753
2754out_free_pgvec:
2755 free_pg_vec(pg_vec, order, block_nr);
2756 pg_vec = NULL;
2757 goto out;
2758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Johann Baudy69e3c752009-05-18 22:11:22 -07002760static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2761 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Neil Horman0e3125c2010-11-16 10:26:47 -08002763 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002765 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002766 struct packet_ring_buffer *rb;
2767 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002768 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002769 int err;
2770
2771 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2772 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2773
2774 err = -EBUSY;
2775 if (!closing) {
2776 if (atomic_read(&po->mapped))
2777 goto out;
2778 if (atomic_read(&rb->pending))
2779 goto out;
2780 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002784 err = -EBUSY;
2785 if (unlikely(rb->pg_vec))
2786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002788 switch (po->tp_version) {
2789 case TPACKET_V1:
2790 po->tp_hdrlen = TPACKET_HDRLEN;
2791 break;
2792 case TPACKET_V2:
2793 po->tp_hdrlen = TPACKET2_HDRLEN;
2794 break;
2795 }
2796
Johann Baudy69e3c752009-05-18 22:11:22 -07002797 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002798 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002799 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002800 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002801 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002802 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002803 po->tp_reserve))
2804 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002805 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Johann Baudy69e3c752009-05-18 22:11:22 -07002808 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2809 if (unlikely(rb->frames_per_block <= 0))
2810 goto out;
2811 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2812 req->tp_frame_nr))
2813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002816 order = get_order(req->tp_block_size);
2817 pg_vec = alloc_pg_vec(req, order);
2818 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002820 }
2821 /* Done */
2822 else {
2823 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002824 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827
2828 lock_sock(sk);
2829
2830 /* Detach socket from network */
2831 spin_lock(&po->bind_lock);
2832 was_running = po->running;
2833 num = po->num;
2834 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07002836 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 synchronize_net();
2841
2842 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002843 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (closing || atomic_read(&po->mapped) == 0) {
2845 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002846 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002847 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002848 rb->frame_max = (req->tp_frame_nr - 1);
2849 rb->head = 0;
2850 rb->frame_size = req->tp_frame_size;
2851 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Changli Gaoc053fd92010-12-10 16:02:20 -08002853 swap(rb->pg_vec_order, order);
2854 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Johann Baudy69e3c752009-05-18 22:11:22 -07002856 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2857 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2858 tpacket_rcv : packet_rcv;
2859 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002861 pr_err("packet_mmap: vma is busy: %d\n",
2862 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Herbert Xu905db442009-01-30 14:12:06 -08002864 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002867 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07002869 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
2871 spin_unlock(&po->bind_lock);
2872
2873 release_sock(sk);
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (pg_vec)
2876 free_pg_vec(pg_vec, order, req->tp_block_nr);
2877out:
2878 return err;
2879}
2880
Johann Baudy69e3c752009-05-18 22:11:22 -07002881static int packet_mmap(struct file *file, struct socket *sock,
2882 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 struct sock *sk = sock->sk;
2885 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002886 unsigned long size, expected_size;
2887 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 unsigned long start;
2889 int err = -EINVAL;
2890 int i;
2891
2892 if (vma->vm_pgoff)
2893 return -EINVAL;
2894
Herbert Xu905db442009-01-30 14:12:06 -08002895 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002896
2897 expected_size = 0;
2898 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2899 if (rb->pg_vec) {
2900 expected_size += rb->pg_vec_len
2901 * rb->pg_vec_pages
2902 * PAGE_SIZE;
2903 }
2904 }
2905
2906 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002908
2909 size = vma->vm_end - vma->vm_start;
2910 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 goto out;
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002914 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2915 if (rb->pg_vec == NULL)
2916 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002917
Johann Baudy69e3c752009-05-18 22:11:22 -07002918 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002919 struct page *page;
2920 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002921 int pg_num;
2922
Changli Gaoc56b4d92010-12-01 02:52:57 +00002923 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2924 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002925 err = vm_insert_page(vma, start, page);
2926 if (unlikely(err))
2927 goto out;
2928 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002929 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002930 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002933
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002934 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 vma->vm_ops = &packet_mmap_ops;
2936 err = 0;
2937
2938out:
Herbert Xu905db442009-01-30 14:12:06 -08002939 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return err;
2941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002943static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 .family = PF_PACKET,
2945 .owner = THIS_MODULE,
2946 .release = packet_release,
2947 .bind = packet_bind_spkt,
2948 .connect = sock_no_connect,
2949 .socketpair = sock_no_socketpair,
2950 .accept = sock_no_accept,
2951 .getname = packet_getname_spkt,
2952 .poll = datagram_poll,
2953 .ioctl = packet_ioctl,
2954 .listen = sock_no_listen,
2955 .shutdown = sock_no_shutdown,
2956 .setsockopt = sock_no_setsockopt,
2957 .getsockopt = sock_no_getsockopt,
2958 .sendmsg = packet_sendmsg_spkt,
2959 .recvmsg = packet_recvmsg,
2960 .mmap = sock_no_mmap,
2961 .sendpage = sock_no_sendpage,
2962};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002964static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 .family = PF_PACKET,
2966 .owner = THIS_MODULE,
2967 .release = packet_release,
2968 .bind = packet_bind,
2969 .connect = sock_no_connect,
2970 .socketpair = sock_no_socketpair,
2971 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002972 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 .poll = packet_poll,
2974 .ioctl = packet_ioctl,
2975 .listen = sock_no_listen,
2976 .shutdown = sock_no_shutdown,
2977 .setsockopt = packet_setsockopt,
2978 .getsockopt = packet_getsockopt,
2979 .sendmsg = packet_sendmsg,
2980 .recvmsg = packet_recvmsg,
2981 .mmap = packet_mmap,
2982 .sendpage = sock_no_sendpage,
2983};
2984
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002985static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .family = PF_PACKET,
2987 .create = packet_create,
2988 .owner = THIS_MODULE,
2989};
2990
2991static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002992 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993};
2994
2995#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002998 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Denis V. Luneve372c412007-11-19 22:31:54 -08003000 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003001
3002 rcu_read_lock();
3003 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
3006static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3007{
Herbert Xu1bf40952007-12-16 14:04:02 -08003008 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003009 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
3012static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00003013 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
stephen hemminger808f5112010-02-22 07:57:18 +00003015 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003018static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 if (v == SEQ_START_TOKEN)
3021 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
3022 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00003023 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 const struct packet_sock *po = pkt_sk(s);
3025
3026 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003027 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 s,
3029 atomic_read(&s->sk_refcnt),
3030 s->sk_type,
3031 ntohs(po->num),
3032 po->ifindex,
3033 po->running,
3034 atomic_read(&s->sk_rmem_alloc),
3035 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003036 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038
3039 return 0;
3040}
3041
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003042static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 .start = packet_seq_start,
3044 .next = packet_seq_next,
3045 .stop = packet_seq_stop,
3046 .show = packet_seq_show,
3047};
3048
3049static int packet_seq_open(struct inode *inode, struct file *file)
3050{
Denis V. Luneve372c412007-11-19 22:31:54 -08003051 return seq_open_net(inode, file, &packet_seq_ops,
3052 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
3054
Arjan van de Venda7071d2007-02-12 00:55:36 -08003055static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .owner = THIS_MODULE,
3057 .open = packet_seq_open,
3058 .read = seq_read,
3059 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003060 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061};
3062
3063#endif
3064
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003065static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003066{
stephen hemminger808f5112010-02-22 07:57:18 +00003067 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08003068 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003069
3070 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
3071 return -ENOMEM;
3072
3073 return 0;
3074}
3075
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003076static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003077{
3078 proc_net_remove(net, "packet");
3079}
3080
3081static struct pernet_operations packet_net_ops = {
3082 .init = packet_net_init,
3083 .exit = packet_net_exit,
3084};
3085
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087static void __exit packet_exit(void)
3088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003090 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 sock_unregister(PF_PACKET);
3092 proto_unregister(&packet_proto);
3093}
3094
3095static int __init packet_init(void)
3096{
3097 int rc = proto_register(&packet_proto, 0);
3098
3099 if (rc != 0)
3100 goto out;
3101
3102 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003103 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105out:
3106 return rc;
3107}
3108
3109module_init(packet_init);
3110module_exit(packet_exit);
3111MODULE_LICENSE("GPL");
3112MODULE_ALIAS_NETPROTO(PF_PACKET);