blob: 7ba6871a1942d1e622058d918f5d47b87ca0f44d [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. Miller7736d332011-07-05 01:43:20 -0700450static struct sk_buff *fanout_check_defrag(struct sk_buff *skb)
451{
452 const struct iphdr *iph;
453 u32 len;
454
455 if (skb->protocol != htons(ETH_P_IP))
456 return skb;
457
458 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
459 return skb;
460
461 iph = ip_hdr(skb);
462 if (iph->ihl < 5 || iph->version != 4)
463 return skb;
464 if (!pskb_may_pull(skb, iph->ihl*4))
465 return skb;
466 iph = ip_hdr(skb);
467 len = ntohs(iph->tot_len);
468 if (skb->len < len || len < (iph->ihl * 4))
469 return skb;
470
471 if (ip_is_fragment(ip_hdr(skb))) {
472 skb = skb_clone(skb, GFP_ATOMIC);
473 if (skb) {
474 if (pskb_trim_rcsum(skb, len))
475 return skb;
476 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
477 if (ip_defrag(skb, IP_DEFRAG_AF_PACKET))
478 return NULL;
479 skb->rxhash = 0;
480 }
481 }
482 return skb;
483}
484
David S. Millerdc99f602011-07-05 01:45:05 -0700485static int packet_rcv_fanout_hash(struct sk_buff *skb, struct net_device *dev,
486 struct packet_type *pt, struct net_device *orig_dev)
487{
488 struct packet_fanout *f = pt->af_packet_priv;
489 unsigned int num = f->num_members;
490 struct packet_sock *po;
491 struct sock *sk;
492
493 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
494 !num) {
495 kfree_skb(skb);
496 return 0;
497 }
498
David S. Miller7736d332011-07-05 01:43:20 -0700499 if (f->defrag) {
500 skb = fanout_check_defrag(skb);
501 if (!skb)
502 return 0;
503 }
504
David S. Millerdc99f602011-07-05 01:45:05 -0700505 skb_get_rxhash(skb);
506
507 sk = fanout_demux_hash(f, skb, num);
508 po = pkt_sk(sk);
509
510 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
511}
512
513static int packet_rcv_fanout_lb(struct sk_buff *skb, struct net_device *dev,
514 struct packet_type *pt, struct net_device *orig_dev)
515{
516 struct packet_fanout *f = pt->af_packet_priv;
517 unsigned int num = f->num_members;
518 struct packet_sock *po;
519 struct sock *sk;
520
521 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
522 !num) {
523 kfree_skb(skb);
524 return 0;
525 }
526
527 sk = fanout_demux_lb(f, skb, num);
528 po = pkt_sk(sk);
529
530 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
531}
532
533static DEFINE_MUTEX(fanout_mutex);
534static LIST_HEAD(fanout_list);
535
536static void __fanout_link(struct sock *sk, struct packet_sock *po)
537{
538 struct packet_fanout *f = po->fanout;
539
540 spin_lock(&f->lock);
541 f->arr[f->num_members] = sk;
542 smp_wmb();
543 f->num_members++;
544 spin_unlock(&f->lock);
545}
546
547static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
548{
549 struct packet_fanout *f = po->fanout;
550 int i;
551
552 spin_lock(&f->lock);
553 for (i = 0; i < f->num_members; i++) {
554 if (f->arr[i] == sk)
555 break;
556 }
557 BUG_ON(i >= f->num_members);
558 f->arr[i] = f->arr[f->num_members - 1];
559 f->num_members--;
560 spin_unlock(&f->lock);
561}
562
David S. Miller7736d332011-07-05 01:43:20 -0700563static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -0700564{
565 struct packet_sock *po = pkt_sk(sk);
566 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -0700567 u8 type = type_flags & 0xff;
568 u8 defrag = (type_flags & PACKET_FANOUT_FLAG_DEFRAG) ? 1 : 0;
David S. Millerdc99f602011-07-05 01:45:05 -0700569 int err;
570
571 switch (type) {
572 case PACKET_FANOUT_HASH:
573 case PACKET_FANOUT_LB:
574 break;
575 default:
576 return -EINVAL;
577 }
578
579 if (!po->running)
580 return -EINVAL;
581
582 if (po->fanout)
583 return -EALREADY;
584
585 mutex_lock(&fanout_mutex);
586 match = NULL;
587 list_for_each_entry(f, &fanout_list, list) {
588 if (f->id == id &&
589 read_pnet(&f->net) == sock_net(sk)) {
590 match = f;
591 break;
592 }
593 }
David S. Miller7736d332011-07-05 01:43:20 -0700594 if (match && match->defrag != defrag)
595 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -0700596 if (!match) {
597 match = kzalloc(sizeof(*match), GFP_KERNEL);
598 if (match) {
599 write_pnet(&match->net, sock_net(sk));
600 match->id = id;
601 match->type = type;
David S. Miller7736d332011-07-05 01:43:20 -0700602 match->defrag = defrag;
David S. Millerdc99f602011-07-05 01:45:05 -0700603 atomic_set(&match->rr_cur, 0);
604 INIT_LIST_HEAD(&match->list);
605 spin_lock_init(&match->lock);
606 atomic_set(&match->sk_ref, 0);
607 match->prot_hook.type = po->prot_hook.type;
608 match->prot_hook.dev = po->prot_hook.dev;
609 switch (type) {
610 case PACKET_FANOUT_HASH:
611 match->prot_hook.func = packet_rcv_fanout_hash;
612 break;
613 case PACKET_FANOUT_LB:
614 match->prot_hook.func = packet_rcv_fanout_lb;
615 break;
616 }
617 match->prot_hook.af_packet_priv = match;
618 dev_add_pack(&match->prot_hook);
619 list_add(&match->list, &fanout_list);
620 }
621 }
622 err = -ENOMEM;
623 if (match) {
624 err = -EINVAL;
625 if (match->type == type &&
626 match->prot_hook.type == po->prot_hook.type &&
627 match->prot_hook.dev == po->prot_hook.dev) {
628 err = -ENOSPC;
629 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
630 __dev_remove_pack(&po->prot_hook);
631 po->fanout = match;
632 atomic_inc(&match->sk_ref);
633 __fanout_link(sk, po);
634 err = 0;
635 }
636 }
637 }
638 mutex_unlock(&fanout_mutex);
639 return err;
640}
641
642static void fanout_release(struct sock *sk)
643{
644 struct packet_sock *po = pkt_sk(sk);
645 struct packet_fanout *f;
646
647 f = po->fanout;
648 if (!f)
649 return;
650
651 po->fanout = NULL;
652
653 mutex_lock(&fanout_mutex);
654 if (atomic_dec_and_test(&f->sk_ref)) {
655 list_del(&f->list);
656 dev_remove_pack(&f->prot_hook);
657 kfree(f);
658 }
659 mutex_unlock(&fanout_mutex);
660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800662static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800664static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000666static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
667 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct sock *sk;
670 struct sockaddr_pkt *spkt;
671
672 /*
673 * When we registered the protocol we saved the socket in the data
674 * field for just this event.
675 */
676
677 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * Yank back the headers [hope the device set this
681 * right or kerboom...]
682 *
683 * Incoming packets have ll header pulled,
684 * push it back.
685 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700686 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * so that this procedure is noop.
688 */
689
690 if (skb->pkt_type == PACKET_LOOPBACK)
691 goto out;
692
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800693 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800694 goto out;
695
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000696 skb = skb_share_check(skb, GFP_ATOMIC);
697 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto oom;
699
700 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000701 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Phil Oester84531c22005-07-12 11:57:52 -0700703 /* drop conntrack reference */
704 nf_reset(skb);
705
Herbert Xuffbc6112007-02-04 23:33:10 -0800706 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700708 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /*
711 * The SOCK_PACKET socket receives _all_ frames.
712 */
713
714 spkt->spkt_family = dev->type;
715 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
716 spkt->spkt_protocol = skb->protocol;
717
718 /*
719 * Charge the memory to the socket. This is done specifically
720 * to prevent sockets using all the memory up.
721 */
722
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000723 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725
726out:
727 kfree_skb(skb);
728oom:
729 return 0;
730}
731
732
733/*
734 * Output a raw packet to a device layer. This bypasses all the other
735 * protocol layers and you must therefore supply it with a complete frame
736 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
739 struct msghdr *msg, size_t len)
740{
741 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000742 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000743 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000745 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900749 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
751
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000752 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000754 return -EINVAL;
755 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
756 proto = saddr->spkt_protocol;
757 } else
758 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900761 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
763
764 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000765retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100766 rcu_read_lock();
767 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 err = -ENODEV;
769 if (dev == NULL)
770 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900771
David S. Millerd5e76b02007-01-25 19:30:36 -0800772 err = -ENETDOWN;
773 if (!(dev->flags & IFF_UP))
774 goto out_unlock;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000777 * You may not queue a frame bigger than the mtu. This is the lowest level
778 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +0000782 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto out_unlock;
784
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000785 if (!skb) {
786 size_t reserved = LL_RESERVED_SPACE(dev);
787 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000789 rcu_read_unlock();
790 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
791 if (skb == NULL)
792 return -ENOBUFS;
793 /* FIXME: Save some space for broken drivers that write a hard
794 * header at transmission time by themselves. PPP is the notable
795 * one here. This should really be fixed at the driver level.
796 */
797 skb_reserve(skb, reserved);
798 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900799
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000800 /* Try to align data part correctly */
801 if (hhlen) {
802 skb->data -= hhlen;
803 skb->tail -= hhlen;
804 if (len < hhlen)
805 skb_reset_network_header(skb);
806 }
807 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
808 if (err)
809 goto out_free;
810 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Ben Greear57f89bf2011-02-11 09:35:18 +0000813 if (len > (dev->mtu + dev->hard_header_len)) {
814 /* Earlier code assumed this would be a VLAN pkt,
815 * double-check this now that we have the actual
816 * packet in hand.
817 */
818 struct ethhdr *ehdr;
819 skb_reset_mac_header(skb);
820 ehdr = eth_hdr(skb);
821 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
822 err = -EMSGSIZE;
823 goto out_unlock;
824 }
825 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 skb->protocol = proto;
828 skb->dev = dev;
829 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000830 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000831 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000832 if (err < 0)
833 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100836 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000837 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100840 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000841out_free:
842 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return err;
844}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Eric Dumazet62ab0812010-12-06 20:50:09 +0000846static inline unsigned int run_filter(const struct sk_buff *skb,
847 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800848 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct sk_filter *filter;
851
Eric Dumazet80f8f102011-01-18 07:46:52 +0000852 rcu_read_lock();
853 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800854 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +0000855 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000856 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David S. Millerdbcb5852007-01-24 15:21:02 -0800858 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000862 * This function makes lazy skb cloning in hope that most of packets
863 * are discarded by BPF.
864 *
865 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
866 * and skb->cb are mangled. It works because (and until) packets
867 * falling here are owned by current CPU. Output packets are cloned
868 * by dev_queue_xmit_nit(), input packets are processed by net_bh
869 * sequencially, so that if we return skb to original state on exit,
870 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000873static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
874 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct sock *sk;
877 struct sockaddr_ll *sll;
878 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000879 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800881 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (skb->pkt_type == PACKET_LOOPBACK)
884 goto drop;
885
886 sk = pt->af_packet_priv;
887 po = pkt_sk(sk);
888
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800889 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800890 goto drop;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 skb->dev = dev;
893
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700894 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000896 * exported to higher levels.
897 *
898 * Otherwise, the device hides details of its frame
899 * structure, so that corresponding packet head is
900 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
902 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700903 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 else if (skb->pkt_type == PACKET_OUTGOING) {
905 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300906 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 }
909
910 snaplen = skb->len;
911
David S. Millerdbcb5852007-01-24 15:21:02 -0800912 res = run_filter(skb, sk, snaplen);
913 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700914 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800915 if (snaplen > res)
916 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
919 (unsigned)sk->sk_rcvbuf)
920 goto drop_n_acct;
921
922 if (skb_shared(skb)) {
923 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
924 if (nskb == NULL)
925 goto drop_n_acct;
926
927 if (skb_head != skb->data) {
928 skb->data = skb_head;
929 skb->len = skb_len;
930 }
931 kfree_skb(skb);
932 skb = nskb;
933 }
934
Herbert Xuffbc6112007-02-04 23:33:10 -0800935 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
936 sizeof(skb->cb));
937
938 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 sll->sll_family = AF_PACKET;
940 sll->sll_hatype = dev->type;
941 sll->sll_protocol = skb->protocol;
942 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800943 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700944 sll->sll_ifindex = orig_dev->ifindex;
945 else
946 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700948 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Herbert Xuffbc6112007-02-04 23:33:10 -0800950 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (pskb_trim(skb, snaplen))
953 goto drop_n_acct;
954
955 skb_set_owner_r(skb, sk);
956 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000957 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Phil Oester84531c22005-07-12 11:57:52 -0700959 /* drop conntrack reference */
960 nf_reset(skb);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 spin_lock(&sk->sk_receive_queue.lock);
963 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700964 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 __skb_queue_tail(&sk->sk_receive_queue, skb);
966 spin_unlock(&sk->sk_receive_queue.lock);
967 sk->sk_data_ready(sk, skb->len);
968 return 0;
969
970drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700971 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973drop_n_restore:
974 if (skb_head != skb->data && skb_shared(skb)) {
975 skb->data = skb_head;
976 skb->len = skb_len;
977 }
978drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000979 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
981}
982
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000983static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
984 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct sock *sk;
987 struct packet_sock *po;
988 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700989 union {
990 struct tpacket_hdr *h1;
991 struct tpacket2_hdr *h2;
992 void *raw;
993 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000994 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800996 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700998 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -07001000 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001001 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -07001002 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (skb->pkt_type == PACKET_LOOPBACK)
1005 goto drop;
1006
1007 sk = pt->af_packet_priv;
1008 po = pkt_sk(sk);
1009
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001010 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001011 goto drop;
1012
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001013 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001015 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 else if (skb->pkt_type == PACKET_OUTGOING) {
1017 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001018 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 }
1021
Herbert Xu8dc41942007-02-04 23:31:32 -08001022 if (skb->ip_summed == CHECKSUM_PARTIAL)
1023 status |= TP_STATUS_CSUMNOTREADY;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 snaplen = skb->len;
1026
David S. Millerdbcb5852007-01-24 15:21:02 -08001027 res = run_filter(skb, sk, snaplen);
1028 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001029 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001030 if (snaplen > res)
1031 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07001034 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
1035 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001037 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001038 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07001039 (maclen < 16 ? 16 : maclen)) +
1040 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 macoff = netoff - maclen;
1042 }
1043
Johann Baudy69e3c752009-05-18 22:11:22 -07001044 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (po->copy_thresh &&
1046 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
1047 (unsigned)sk->sk_rcvbuf) {
1048 if (skb_shared(skb)) {
1049 copy_skb = skb_clone(skb, GFP_ATOMIC);
1050 } else {
1051 copy_skb = skb_get(skb);
1052 skb_head = skb->data;
1053 }
1054 if (copy_skb)
1055 skb_set_owner_r(copy_skb, sk);
1056 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001057 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if ((int)snaplen < 0)
1059 snaplen = 0;
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07001063 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001064 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -07001066 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 po->stats.tp_packets++;
1068 if (copy_skb) {
1069 status |= TP_STATUS_COPY;
1070 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1071 }
1072 if (!po->stats.tp_drops)
1073 status &= ~TP_STATUS_LOSING;
1074 spin_unlock(&sk->sk_receive_queue.lock);
1075
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001076 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001078 switch (po->tp_version) {
1079 case TPACKET_V1:
1080 h.h1->tp_len = skb->len;
1081 h.h1->tp_snaplen = snaplen;
1082 h.h1->tp_mac = macoff;
1083 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -07001084 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
1085 && shhwtstamps->syststamp.tv64)
1086 tv = ktime_to_timeval(shhwtstamps->syststamp);
1087 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
1088 && shhwtstamps->hwtstamp.tv64)
1089 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
1090 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001091 tv = ktime_to_timeval(skb->tstamp);
1092 else
1093 do_gettimeofday(&tv);
1094 h.h1->tp_sec = tv.tv_sec;
1095 h.h1->tp_usec = tv.tv_usec;
1096 hdrlen = sizeof(*h.h1);
1097 break;
1098 case TPACKET_V2:
1099 h.h2->tp_len = skb->len;
1100 h.h2->tp_snaplen = snaplen;
1101 h.h2->tp_mac = macoff;
1102 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -07001103 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
1104 && shhwtstamps->syststamp.tv64)
1105 ts = ktime_to_timespec(shhwtstamps->syststamp);
1106 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
1107 && shhwtstamps->hwtstamp.tv64)
1108 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
1109 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001110 ts = ktime_to_timespec(skb->tstamp);
1111 else
1112 getnstimeofday(&ts);
1113 h.h2->tp_sec = ts.tv_sec;
1114 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +00001115 if (vlan_tx_tag_present(skb)) {
1116 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
1117 status |= TP_STATUS_VLAN_VALID;
1118 } else {
1119 h.h2->tp_vlan_tci = 0;
1120 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001121 h.h2->tp_padding = 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001122 hdrlen = sizeof(*h.h2);
1123 break;
1124 default:
1125 BUG();
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001128 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001129 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sll->sll_family = AF_PACKET;
1131 sll->sll_hatype = dev->type;
1132 sll->sll_protocol = skb->protocol;
1133 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001134 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001135 sll->sll_ifindex = orig_dev->ifindex;
1136 else
1137 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001139 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -08001140 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +00001141#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 {
Changli Gao0af55bb2010-12-01 02:52:20 +00001143 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Changli Gao0af55bb2010-12-01 02:52:20 +00001145 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
1146 for (start = h.raw; start < end; start += PAGE_SIZE)
1147 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Changli Gaof6dafa92010-12-07 04:26:16 +00001149#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 sk->sk_data_ready(sk, 0);
1152
1153drop_n_restore:
1154 if (skb_head != skb->data && skb_shared(skb)) {
1155 skb->data = skb_head;
1156 skb->len = skb_len;
1157 }
1158drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001159 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
1161
1162ring_is_full:
1163 po->stats.tp_drops++;
1164 spin_unlock(&sk->sk_receive_queue.lock);
1165
1166 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +00001167 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto drop_n_restore;
1169}
1170
Johann Baudy69e3c752009-05-18 22:11:22 -07001171static void tpacket_destruct_skb(struct sk_buff *skb)
1172{
1173 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001174 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -07001175
1176 BUG_ON(skb == NULL);
1177
1178 if (likely(po->tx_ring.pg_vec)) {
1179 ph = skb_shinfo(skb)->destructor_arg;
1180 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
1181 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
1182 atomic_dec(&po->tx_ring.pending);
1183 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
1184 }
1185
1186 sock_wfree(skb);
1187}
1188
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001189static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
1190 void *frame, struct net_device *dev, int size_max,
1191 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -07001192{
1193 union {
1194 struct tpacket_hdr *h1;
1195 struct tpacket2_hdr *h2;
1196 void *raw;
1197 } ph;
1198 int to_write, offset, len, tp_len, nr_frags, len_max;
1199 struct socket *sock = po->sk.sk_socket;
1200 struct page *page;
1201 void *data;
1202 int err;
1203
1204 ph.raw = frame;
1205
1206 skb->protocol = proto;
1207 skb->dev = dev;
1208 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001209 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -07001210 skb_shinfo(skb)->destructor_arg = ph.raw;
1211
1212 switch (po->tp_version) {
1213 case TPACKET_V2:
1214 tp_len = ph.h2->tp_len;
1215 break;
1216 default:
1217 tp_len = ph.h1->tp_len;
1218 break;
1219 }
1220 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001221 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -07001222 return -EMSGSIZE;
1223 }
1224
1225 skb_reserve(skb, LL_RESERVED_SPACE(dev));
1226 skb_reset_network_header(skb);
1227
1228 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
1229 to_write = tp_len;
1230
1231 if (sock->type == SOCK_DGRAM) {
1232 err = dev_hard_header(skb, dev, ntohs(proto), addr,
1233 NULL, tp_len);
1234 if (unlikely(err < 0))
1235 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001236 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -07001237 /* net device doesn't like empty head */
1238 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001239 pr_err("packet size is too short (%d < %d)\n",
1240 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001241 return -EINVAL;
1242 }
1243
1244 skb_push(skb, dev->hard_header_len);
1245 err = skb_store_bits(skb, 0, data,
1246 dev->hard_header_len);
1247 if (unlikely(err))
1248 return err;
1249
1250 data += dev->hard_header_len;
1251 to_write -= dev->hard_header_len;
1252 }
1253
1254 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001255 offset = offset_in_page(data);
1256 len_max = PAGE_SIZE - offset;
1257 len = ((to_write > len_max) ? len_max : to_write);
1258
1259 skb->data_len = to_write;
1260 skb->len += to_write;
1261 skb->truesize += to_write;
1262 atomic_add(to_write, &po->sk.sk_wmem_alloc);
1263
1264 while (likely(to_write)) {
1265 nr_frags = skb_shinfo(skb)->nr_frags;
1266
1267 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001268 pr_err("Packet exceed the number of skb frags(%lu)\n",
1269 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07001270 return -EFAULT;
1271 }
1272
Changli Gao0af55bb2010-12-01 02:52:20 +00001273 page = pgv_to_page(data);
1274 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07001275 flush_dcache_page(page);
1276 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00001277 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001278 to_write -= len;
1279 offset = 0;
1280 len_max = PAGE_SIZE;
1281 len = ((to_write > len_max) ? len_max : to_write);
1282 }
1283
1284 return tp_len;
1285}
1286
1287static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
1288{
Johann Baudy69e3c752009-05-18 22:11:22 -07001289 struct sk_buff *skb;
1290 struct net_device *dev;
1291 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001292 bool need_rls_dev = false;
1293 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001294 void *ph;
1295 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -07001296 int tp_len, size_max;
1297 unsigned char *addr;
1298 int len_sum = 0;
1299 int status = 0;
1300
Johann Baudy69e3c752009-05-18 22:11:22 -07001301 mutex_lock(&po->pg_vec_lock);
1302
1303 err = -EBUSY;
1304 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001305 dev = po->prot_hook.dev;
Johann Baudy69e3c752009-05-18 22:11:22 -07001306 proto = po->num;
1307 addr = NULL;
1308 } else {
1309 err = -EINVAL;
1310 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1311 goto out;
1312 if (msg->msg_namelen < (saddr->sll_halen
1313 + offsetof(struct sockaddr_ll,
1314 sll_addr)))
1315 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07001316 proto = saddr->sll_protocol;
1317 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001318 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
1319 need_rls_dev = true;
Johann Baudy69e3c752009-05-18 22:11:22 -07001320 }
1321
Johann Baudy69e3c752009-05-18 22:11:22 -07001322 err = -ENXIO;
1323 if (unlikely(dev == NULL))
1324 goto out;
1325
1326 reserve = dev->hard_header_len;
1327
1328 err = -ENETDOWN;
1329 if (unlikely(!(dev->flags & IFF_UP)))
1330 goto out_put;
1331
1332 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001333 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001334
1335 if (size_max > dev->mtu + reserve)
1336 size_max = dev->mtu + reserve;
1337
1338 do {
1339 ph = packet_current_frame(po, &po->tx_ring,
1340 TP_STATUS_SEND_REQUEST);
1341
1342 if (unlikely(ph == NULL)) {
1343 schedule();
1344 continue;
1345 }
1346
1347 status = TP_STATUS_SEND_REQUEST;
1348 skb = sock_alloc_send_skb(&po->sk,
1349 LL_ALLOCATED_SPACE(dev)
1350 + sizeof(struct sockaddr_ll),
1351 0, &err);
1352
1353 if (unlikely(skb == NULL))
1354 goto out_status;
1355
1356 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1357 addr);
1358
1359 if (unlikely(tp_len < 0)) {
1360 if (po->tp_loss) {
1361 __packet_set_status(po, ph,
1362 TP_STATUS_AVAILABLE);
1363 packet_increment_head(&po->tx_ring);
1364 kfree_skb(skb);
1365 continue;
1366 } else {
1367 status = TP_STATUS_WRONG_FORMAT;
1368 err = tp_len;
1369 goto out_status;
1370 }
1371 }
1372
1373 skb->destructor = tpacket_destruct_skb;
1374 __packet_set_status(po, ph, TP_STATUS_SENDING);
1375 atomic_inc(&po->tx_ring.pending);
1376
1377 status = TP_STATUS_SEND_REQUEST;
1378 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001379 if (unlikely(err > 0)) {
1380 err = net_xmit_errno(err);
1381 if (err && __packet_get_status(po, ph) ==
1382 TP_STATUS_AVAILABLE) {
1383 /* skb was destructed already */
1384 skb = NULL;
1385 goto out_status;
1386 }
1387 /*
1388 * skb was dropped but not destructed yet;
1389 * let's treat it like congestion or err < 0
1390 */
1391 err = 0;
1392 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001393 packet_increment_head(&po->tx_ring);
1394 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001395 } while (likely((ph != NULL) ||
1396 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1397 (atomic_read(&po->tx_ring.pending))))
1398 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001399
1400 err = len_sum;
1401 goto out_put;
1402
Johann Baudy69e3c752009-05-18 22:11:22 -07001403out_status:
1404 __packet_set_status(po, ph, status);
1405 kfree_skb(skb);
1406out_put:
Ben Greear827d9782011-06-01 07:18:53 +00001407 if (need_rls_dev)
1408 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07001409out:
1410 mutex_unlock(&po->pg_vec_lock);
1411 return err;
1412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001414static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1415 size_t reserve, size_t len,
1416 size_t linear, int noblock,
1417 int *err)
1418{
1419 struct sk_buff *skb;
1420
1421 /* Under a page? Don't bother with paged skb. */
1422 if (prepad + len < PAGE_SIZE || !linear)
1423 linear = len;
1424
1425 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1426 err);
1427 if (!skb)
1428 return NULL;
1429
1430 skb_reserve(skb, reserve);
1431 skb_put(skb, linear);
1432 skb->data_len = len - linear;
1433 skb->len += len - linear;
1434
1435 return skb;
1436}
1437
Johann Baudy69e3c752009-05-18 22:11:22 -07001438static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct msghdr *msg, size_t len)
1440{
1441 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001442 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct sk_buff *skb;
1444 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001445 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001446 bool need_rls_dev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00001448 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001449 struct virtio_net_hdr vnet_hdr = { 0 };
1450 int offset = 0;
1451 int vnet_hdr_len;
1452 struct packet_sock *po = pkt_sk(sk);
1453 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001456 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001460 dev = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 proto = po->num;
1462 addr = NULL;
1463 } else {
1464 err = -EINVAL;
1465 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1466 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001467 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 proto = saddr->sll_protocol;
1470 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001471 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
1472 need_rls_dev = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 err = -ENXIO;
1476 if (dev == NULL)
1477 goto out_unlock;
1478 if (sock->type == SOCK_RAW)
1479 reserve = dev->hard_header_len;
1480
David S. Millerd5e76b02007-01-25 19:30:36 -08001481 err = -ENETDOWN;
1482 if (!(dev->flags & IFF_UP))
1483 goto out_unlock;
1484
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001485 if (po->has_vnet_hdr) {
1486 vnet_hdr_len = sizeof(vnet_hdr);
1487
1488 err = -EINVAL;
1489 if (len < vnet_hdr_len)
1490 goto out_unlock;
1491
1492 len -= vnet_hdr_len;
1493
1494 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1495 vnet_hdr_len);
1496 if (err < 0)
1497 goto out_unlock;
1498
1499 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1500 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1501 vnet_hdr.hdr_len))
1502 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1503 vnet_hdr.csum_offset + 2;
1504
1505 err = -EINVAL;
1506 if (vnet_hdr.hdr_len > len)
1507 goto out_unlock;
1508
1509 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1510 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1511 case VIRTIO_NET_HDR_GSO_TCPV4:
1512 gso_type = SKB_GSO_TCPV4;
1513 break;
1514 case VIRTIO_NET_HDR_GSO_TCPV6:
1515 gso_type = SKB_GSO_TCPV6;
1516 break;
1517 case VIRTIO_NET_HDR_GSO_UDP:
1518 gso_type = SKB_GSO_UDP;
1519 break;
1520 default:
1521 goto out_unlock;
1522 }
1523
1524 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1525 gso_type |= SKB_GSO_TCP_ECN;
1526
1527 if (vnet_hdr.gso_size == 0)
1528 goto out_unlock;
1529
1530 }
1531 }
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +00001534 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 goto out_unlock;
1536
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001537 err = -ENOBUFS;
1538 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1539 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1540 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001541 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto out_unlock;
1543
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001544 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001546 err = -EINVAL;
1547 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001548 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001549 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001552 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (err)
1554 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001555 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001556 if (err < 0)
1557 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Ben Greear57f89bf2011-02-11 09:35:18 +00001559 if (!gso_type && (len > dev->mtu + reserve)) {
1560 /* Earlier code assumed this would be a VLAN pkt,
1561 * double-check this now that we have the actual
1562 * packet in hand.
1563 */
1564 struct ethhdr *ehdr;
1565 skb_reset_mac_header(skb);
1566 ehdr = eth_hdr(skb);
1567 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1568 err = -EMSGSIZE;
1569 goto out_free;
1570 }
1571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 skb->protocol = proto;
1574 skb->dev = dev;
1575 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001576 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001578 if (po->has_vnet_hdr) {
1579 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1580 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1581 vnet_hdr.csum_offset)) {
1582 err = -EINVAL;
1583 goto out_free;
1584 }
1585 }
1586
1587 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1588 skb_shinfo(skb)->gso_type = gso_type;
1589
1590 /* Header must be checked, and gso_segs computed. */
1591 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1592 skb_shinfo(skb)->gso_segs = 0;
1593
1594 len += vnet_hdr_len;
1595 }
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /*
1598 * Now send it
1599 */
1600
1601 err = dev_queue_xmit(skb);
1602 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1603 goto out_unlock;
1604
Ben Greear827d9782011-06-01 07:18:53 +00001605 if (need_rls_dev)
1606 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001608 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610out_free:
1611 kfree_skb(skb);
1612out_unlock:
Ben Greear827d9782011-06-01 07:18:53 +00001613 if (dev && need_rls_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 dev_put(dev);
1615out:
1616 return err;
1617}
1618
Johann Baudy69e3c752009-05-18 22:11:22 -07001619static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1620 struct msghdr *msg, size_t len)
1621{
Johann Baudy69e3c752009-05-18 22:11:22 -07001622 struct sock *sk = sock->sk;
1623 struct packet_sock *po = pkt_sk(sk);
1624 if (po->tx_ring.pg_vec)
1625 return tpacket_snd(po, msg);
1626 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001627 return packet_snd(sock, msg, len);
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/*
1631 * Close a PACKET socket. This is fairly simple. We immediately go
1632 * to 'closed' state and remove our protocol entry in the device list.
1633 */
1634
1635static int packet_release(struct socket *sock)
1636{
1637 struct sock *sk = sock->sk;
1638 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001639 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001640 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 if (!sk)
1643 return 0;
1644
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001645 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 po = pkt_sk(sk);
1647
stephen hemminger808f5112010-02-22 07:57:18 +00001648 spin_lock_bh(&net->packet.sklist_lock);
1649 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001650 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001651 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
stephen hemminger808f5112010-02-22 07:57:18 +00001653 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001654 unregister_prot_hook(sk, false);
Ben Greear160ff182011-06-01 07:18:52 +00001655 if (po->prot_hook.dev) {
1656 dev_put(po->prot_hook.dev);
1657 po->prot_hook.dev = NULL;
1658 }
stephen hemminger808f5112010-02-22 07:57:18 +00001659 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Johann Baudy69e3c752009-05-18 22:11:22 -07001663 memset(&req, 0, sizeof(req));
1664
1665 if (po->rx_ring.pg_vec)
1666 packet_set_ring(sk, &req, 1, 0);
1667
1668 if (po->tx_ring.pg_vec)
1669 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
David S. Millerdc99f602011-07-05 01:45:05 -07001671 fanout_release(sk);
1672
stephen hemminger808f5112010-02-22 07:57:18 +00001673 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Now the socket is dead. No more input will appear.
1676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 sock_orphan(sk);
1678 sock->sk = NULL;
1679
1680 /* Purge queues */
1681
1682 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001683 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 sock_put(sk);
1686 return 0;
1687}
1688
1689/*
1690 * Attach a packet hook.
1691 */
1692
Al Viro0e11c912006-11-08 00:26:29 -08001693static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct packet_sock *po = pkt_sk(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07001696
1697 if (po->fanout)
1698 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 lock_sock(sk);
1701
1702 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001703 unregister_prot_hook(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 po->num = protocol;
1705 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00001706 if (po->prot_hook.dev)
1707 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 po->prot_hook.dev = dev;
1709
1710 po->ifindex = dev ? dev->ifindex : 0;
1711
1712 if (protocol == 0)
1713 goto out_unlock;
1714
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001715 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07001716 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001717 } else {
1718 sk->sk_err = ENETDOWN;
1719 if (!sock_flag(sk, SOCK_DEAD))
1720 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
1723out_unlock:
1724 spin_unlock(&po->bind_lock);
1725 release_sock(sk);
1726 return 0;
1727}
1728
1729/*
1730 * Bind a packet socket to a device
1731 */
1732
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001733static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1734 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001736 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 char name[15];
1738 struct net_device *dev;
1739 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
1742 * Check legality
1743 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001744
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001745 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001747 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001749 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00001750 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return err;
1753}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1756{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001757 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1758 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 struct net_device *dev = NULL;
1760 int err;
1761
1762
1763 /*
1764 * Check legality
1765 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (addr_len < sizeof(struct sockaddr_ll))
1768 return -EINVAL;
1769 if (sll->sll_family != AF_PACKET)
1770 return -EINVAL;
1771
1772 if (sll->sll_ifindex) {
1773 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001774 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (dev == NULL)
1776 goto out;
1777 }
1778 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780out:
1781 return err;
1782}
1783
1784static struct proto packet_proto = {
1785 .name = "PACKET",
1786 .owner = THIS_MODULE,
1787 .obj_size = sizeof(struct packet_sock),
1788};
1789
1790/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001791 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 */
1793
Eric Paris3f378b62009-11-05 22:18:14 -08001794static int packet_create(struct net *net, struct socket *sock, int protocol,
1795 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct sock *sk;
1798 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001799 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 int err;
1801
1802 if (!capable(CAP_NET_RAW))
1803 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001804 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1805 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return -ESOCKTNOSUPPORT;
1807
1808 sock->state = SS_UNCONNECTED;
1809
1810 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001811 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (sk == NULL)
1813 goto out;
1814
1815 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (sock->type == SOCK_PACKET)
1817 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 sock_init_data(sock, sk);
1820
1821 po = pkt_sk(sk);
1822 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001823 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001826 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 /*
1829 * Attach a protocol block
1830 */
1831
1832 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001833 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (sock->type == SOCK_PACKET)
1837 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 po->prot_hook.af_packet_priv = sk;
1840
Al Viro0e11c912006-11-08 00:26:29 -08001841 if (proto) {
1842 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07001843 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845
stephen hemminger808f5112010-02-22 07:57:18 +00001846 spin_lock_bh(&net->packet.sklist_lock);
1847 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001848 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001849 spin_unlock_bh(&net->packet.sklist_lock);
1850
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852out:
1853 return err;
1854}
1855
Richard Cochraned85b562010-04-07 22:41:28 +00001856static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1857{
1858 struct sock_exterr_skb *serr;
1859 struct sk_buff *skb, *skb2;
1860 int copied, err;
1861
1862 err = -EAGAIN;
1863 skb = skb_dequeue(&sk->sk_error_queue);
1864 if (skb == NULL)
1865 goto out;
1866
1867 copied = skb->len;
1868 if (copied > len) {
1869 msg->msg_flags |= MSG_TRUNC;
1870 copied = len;
1871 }
1872 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1873 if (err)
1874 goto out_free_skb;
1875
1876 sock_recv_timestamp(msg, sk, skb);
1877
1878 serr = SKB_EXT_ERR(skb);
1879 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1880 sizeof(serr->ee), &serr->ee);
1881
1882 msg->msg_flags |= MSG_ERRQUEUE;
1883 err = copied;
1884
1885 /* Reset and regenerate socket error */
1886 spin_lock_bh(&sk->sk_error_queue.lock);
1887 sk->sk_err = 0;
1888 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1889 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1890 spin_unlock_bh(&sk->sk_error_queue.lock);
1891 sk->sk_error_report(sk);
1892 } else
1893 spin_unlock_bh(&sk->sk_error_queue.lock);
1894
1895out_free_skb:
1896 kfree_skb(skb);
1897out:
1898 return err;
1899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/*
1902 * Pull a packet from our receive queue and hand it to the user.
1903 * If necessary we block.
1904 */
1905
1906static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1907 struct msghdr *msg, size_t len, int flags)
1908{
1909 struct sock *sk = sock->sk;
1910 struct sk_buff *skb;
1911 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001912 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001913 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001916 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto out;
1918
1919#if 0
1920 /* What error should we return now? EUNATTACH? */
1921 if (pkt_sk(sk)->ifindex < 0)
1922 return -ENODEV;
1923#endif
1924
Richard Cochraned85b562010-04-07 22:41:28 +00001925 if (flags & MSG_ERRQUEUE) {
1926 err = packet_recv_error(sk, msg, len);
1927 goto out;
1928 }
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * Call the generic datagram receiver. This handles all sorts
1932 * of horrible races and re-entrancy so we can forget about it
1933 * in the protocol layers.
1934 *
1935 * Now it will return ENETDOWN, if device have just gone down,
1936 * but then it will block.
1937 */
1938
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001939 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001942 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * handles the blocking we don't see and worry about blocking
1944 * retries.
1945 */
1946
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001947 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto out;
1949
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001950 if (pkt_sk(sk)->has_vnet_hdr) {
1951 struct virtio_net_hdr vnet_hdr = { 0 };
1952
1953 err = -EINVAL;
1954 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001955 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001956 goto out_free;
1957
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001958 len -= vnet_hdr_len;
1959
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001960 if (skb_is_gso(skb)) {
1961 struct skb_shared_info *sinfo = skb_shinfo(skb);
1962
1963 /* This is a hint as to how much should be linear. */
1964 vnet_hdr.hdr_len = skb_headlen(skb);
1965 vnet_hdr.gso_size = sinfo->gso_size;
1966 if (sinfo->gso_type & SKB_GSO_TCPV4)
1967 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1968 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1969 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1970 else if (sinfo->gso_type & SKB_GSO_UDP)
1971 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1972 else if (sinfo->gso_type & SKB_GSO_FCOE)
1973 goto out_free;
1974 else
1975 BUG();
1976 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1977 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1978 } else
1979 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1980
1981 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1982 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001983 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001984 vnet_hdr.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001985 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1986 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001987 } /* else everything is zero */
1988
1989 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1990 vnet_hdr_len);
1991 if (err < 0)
1992 goto out_free;
1993 }
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001996 * If the address length field is there to be filled in, we fill
1997 * it in now.
1998 */
1999
Herbert Xuffbc6112007-02-04 23:33:10 -08002000 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002001 if (sock->type == SOCK_PACKET)
2002 msg->msg_namelen = sizeof(struct sockaddr_pkt);
2003 else
2004 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
2005
2006 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * You lose any data beyond the buffer you gave. If it worries a
2008 * user program they can ask the device for its MTU anyway.
2009 */
2010
2011 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002012 if (copied > len) {
2013 copied = len;
2014 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
2017 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2018 if (err)
2019 goto out_free;
2020
Neil Horman3b885782009-10-12 13:26:31 -07002021 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08002024 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
2025 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Herbert Xu8dc41942007-02-04 23:31:32 -08002027 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08002028 struct tpacket_auxdata aux;
2029
2030 aux.tp_status = TP_STATUS_USER;
2031 if (skb->ip_summed == CHECKSUM_PARTIAL)
2032 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
2033 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
2034 aux.tp_snaplen = skb->len;
2035 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002036 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00002037 if (vlan_tx_tag_present(skb)) {
2038 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
2039 aux.tp_status |= TP_STATUS_VLAN_VALID;
2040 } else {
2041 aux.tp_vlan_tci = 0;
2042 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07002043 aux.tp_padding = 0;
Herbert Xuffbc6112007-02-04 23:33:10 -08002044 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08002045 }
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /*
2048 * Free or return the buffer as appropriate. Again this
2049 * hides all the races and re-entrancy issues from us.
2050 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002051 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053out_free:
2054 skb_free_datagram(sk, skb);
2055out:
2056 return err;
2057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
2060 int *uaddr_len, int peer)
2061{
2062 struct net_device *dev;
2063 struct sock *sk = sock->sk;
2064
2065 if (peer)
2066 return -EOPNOTSUPP;
2067
2068 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002069 rcu_read_lock();
2070 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
2071 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002072 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002073 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002075 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 *uaddr_len = sizeof(*uaddr);
2077
2078 return 0;
2079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
2082 int *uaddr_len, int peer)
2083{
2084 struct net_device *dev;
2085 struct sock *sk = sock->sk;
2086 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00002087 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (peer)
2090 return -EOPNOTSUPP;
2091
2092 sll->sll_family = AF_PACKET;
2093 sll->sll_ifindex = po->ifindex;
2094 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002095 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002096 rcu_read_lock();
2097 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (dev) {
2099 sll->sll_hatype = dev->type;
2100 sll->sll_halen = dev->addr_len;
2101 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } else {
2103 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
2104 sll->sll_halen = 0;
2105 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01002106 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002107 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 return 0;
2110}
2111
Wang Chen2aeb0b82008-07-14 20:49:46 -07002112static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
2113 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 switch (i->type) {
2116 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002117 if (i->alen != dev->addr_len)
2118 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00002120 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00002122 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 break;
2124 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002125 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 break;
2127 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002128 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002130 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002131 if (i->alen != dev->addr_len)
2132 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002133 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002134 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002135 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002136 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002137 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002138 default:
2139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07002141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
2144static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
2145{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002146 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (i->ifindex == dev->ifindex)
2148 packet_dev_mc(dev, i, what);
2149 }
2150}
2151
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002152static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct packet_sock *po = pkt_sk(sk);
2155 struct packet_mclist *ml, *i;
2156 struct net_device *dev;
2157 int err;
2158
2159 rtnl_lock();
2160
2161 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002162 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (!dev)
2164 goto done;
2165
2166 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00002167 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto done;
2169
2170 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002171 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (i == NULL)
2173 goto done;
2174
2175 err = 0;
2176 for (ml = po->mclist; ml; ml = ml->next) {
2177 if (ml->ifindex == mreq->mr_ifindex &&
2178 ml->type == mreq->mr_type &&
2179 ml->alen == mreq->mr_alen &&
2180 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2181 ml->count++;
2182 /* Free the new element ... */
2183 kfree(i);
2184 goto done;
2185 }
2186 }
2187
2188 i->type = mreq->mr_type;
2189 i->ifindex = mreq->mr_ifindex;
2190 i->alen = mreq->mr_alen;
2191 memcpy(i->addr, mreq->mr_address, i->alen);
2192 i->count = 1;
2193 i->next = po->mclist;
2194 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07002195 err = packet_dev_mc(dev, i, 1);
2196 if (err) {
2197 po->mclist = i->next;
2198 kfree(i);
2199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201done:
2202 rtnl_unlock();
2203 return err;
2204}
2205
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002206static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 struct packet_mclist *ml, **mlp;
2209
2210 rtnl_lock();
2211
2212 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
2213 if (ml->ifindex == mreq->mr_ifindex &&
2214 ml->type == mreq->mr_type &&
2215 ml->alen == mreq->mr_alen &&
2216 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2217 if (--ml->count == 0) {
2218 struct net_device *dev;
2219 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002220 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2221 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 kfree(ml);
2224 }
2225 rtnl_unlock();
2226 return 0;
2227 }
2228 }
2229 rtnl_unlock();
2230 return -EADDRNOTAVAIL;
2231}
2232
2233static void packet_flush_mclist(struct sock *sk)
2234{
2235 struct packet_sock *po = pkt_sk(sk);
2236 struct packet_mclist *ml;
2237
2238 if (!po->mclist)
2239 return;
2240
2241 rtnl_lock();
2242 while ((ml = po->mclist) != NULL) {
2243 struct net_device *dev;
2244
2245 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002246 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2247 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 kfree(ml);
2250 }
2251 rtnl_unlock();
2252}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254static int
David S. Millerb7058842009-09-30 16:12:20 -07002255packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08002258 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int ret;
2260
2261 if (level != SOL_PACKET)
2262 return -ENOPROTOOPT;
2263
Johann Baudy69e3c752009-05-18 22:11:22 -07002264 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002265 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 case PACKET_DROP_MEMBERSHIP:
2267 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002268 struct packet_mreq_max mreq;
2269 int len = optlen;
2270 memset(&mreq, 0, sizeof(mreq));
2271 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002273 if (len > sizeof(mreq))
2274 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002275 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002277 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
2278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (optname == PACKET_ADD_MEMBERSHIP)
2280 ret = packet_mc_add(sk, &mreq);
2281 else
2282 ret = packet_mc_drop(sk, &mreq);
2283 return ret;
2284 }
David S. Millera2efcfa2007-05-29 13:12:50 -07002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07002287 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 {
2289 struct tpacket_req req;
2290
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002291 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002293 if (pkt_sk(sk)->has_vnet_hdr)
2294 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002295 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07002297 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 case PACKET_COPY_THRESH:
2300 {
2301 int val;
2302
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002303 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002305 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return -EFAULT;
2307
2308 pkt_sk(sk)->copy_thresh = val;
2309 return 0;
2310 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002311 case PACKET_VERSION:
2312 {
2313 int val;
2314
2315 if (optlen != sizeof(val))
2316 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002317 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002318 return -EBUSY;
2319 if (copy_from_user(&val, optval, sizeof(val)))
2320 return -EFAULT;
2321 switch (val) {
2322 case TPACKET_V1:
2323 case TPACKET_V2:
2324 po->tp_version = val;
2325 return 0;
2326 default:
2327 return -EINVAL;
2328 }
2329 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07002330 case PACKET_RESERVE:
2331 {
2332 unsigned int val;
2333
2334 if (optlen != sizeof(val))
2335 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002336 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07002337 return -EBUSY;
2338 if (copy_from_user(&val, optval, sizeof(val)))
2339 return -EFAULT;
2340 po->tp_reserve = val;
2341 return 0;
2342 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002343 case PACKET_LOSS:
2344 {
2345 unsigned int val;
2346
2347 if (optlen != sizeof(val))
2348 return -EINVAL;
2349 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2350 return -EBUSY;
2351 if (copy_from_user(&val, optval, sizeof(val)))
2352 return -EFAULT;
2353 po->tp_loss = !!val;
2354 return 0;
2355 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002356 case PACKET_AUXDATA:
2357 {
2358 int val;
2359
2360 if (optlen < sizeof(val))
2361 return -EINVAL;
2362 if (copy_from_user(&val, optval, sizeof(val)))
2363 return -EFAULT;
2364
2365 po->auxdata = !!val;
2366 return 0;
2367 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002368 case PACKET_ORIGDEV:
2369 {
2370 int val;
2371
2372 if (optlen < sizeof(val))
2373 return -EINVAL;
2374 if (copy_from_user(&val, optval, sizeof(val)))
2375 return -EFAULT;
2376
2377 po->origdev = !!val;
2378 return 0;
2379 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002380 case PACKET_VNET_HDR:
2381 {
2382 int val;
2383
2384 if (sock->type != SOCK_RAW)
2385 return -EINVAL;
2386 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2387 return -EBUSY;
2388 if (optlen < sizeof(val))
2389 return -EINVAL;
2390 if (copy_from_user(&val, optval, sizeof(val)))
2391 return -EFAULT;
2392
2393 po->has_vnet_hdr = !!val;
2394 return 0;
2395 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002396 case PACKET_TIMESTAMP:
2397 {
2398 int val;
2399
2400 if (optlen != sizeof(val))
2401 return -EINVAL;
2402 if (copy_from_user(&val, optval, sizeof(val)))
2403 return -EFAULT;
2404
2405 po->tp_tstamp = val;
2406 return 0;
2407 }
David S. Millerdc99f602011-07-05 01:45:05 -07002408 case PACKET_FANOUT:
2409 {
2410 int val;
2411
2412 if (optlen != sizeof(val))
2413 return -EINVAL;
2414 if (copy_from_user(&val, optval, sizeof(val)))
2415 return -EFAULT;
2416
2417 return fanout_add(sk, val & 0xffff, val >> 16);
2418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 default:
2420 return -ENOPROTOOPT;
2421 }
2422}
2423
2424static int packet_getsockopt(struct socket *sock, int level, int optname,
2425 char __user *optval, int __user *optlen)
2426{
2427 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002428 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct sock *sk = sock->sk;
2430 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002431 void *data;
2432 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 if (level != SOL_PACKET)
2435 return -ENOPROTOOPT;
2436
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002437 if (get_user(len, optlen))
2438 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (len < 0)
2441 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002442
Johann Baudy69e3c752009-05-18 22:11:22 -07002443 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (len > sizeof(struct tpacket_stats))
2446 len = sizeof(struct tpacket_stats);
2447 spin_lock_bh(&sk->sk_receive_queue.lock);
2448 st = po->stats;
2449 memset(&po->stats, 0, sizeof(st));
2450 spin_unlock_bh(&sk->sk_receive_queue.lock);
2451 st.tp_packets += st.tp_drops;
2452
Herbert Xu8dc41942007-02-04 23:31:32 -08002453 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002455 case PACKET_AUXDATA:
2456 if (len > sizeof(int))
2457 len = sizeof(int);
2458 val = po->auxdata;
2459
2460 data = &val;
2461 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002462 case PACKET_ORIGDEV:
2463 if (len > sizeof(int))
2464 len = sizeof(int);
2465 val = po->origdev;
2466
2467 data = &val;
2468 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002469 case PACKET_VNET_HDR:
2470 if (len > sizeof(int))
2471 len = sizeof(int);
2472 val = po->has_vnet_hdr;
2473
2474 data = &val;
2475 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002476 case PACKET_VERSION:
2477 if (len > sizeof(int))
2478 len = sizeof(int);
2479 val = po->tp_version;
2480 data = &val;
2481 break;
2482 case PACKET_HDRLEN:
2483 if (len > sizeof(int))
2484 len = sizeof(int);
2485 if (copy_from_user(&val, optval, len))
2486 return -EFAULT;
2487 switch (val) {
2488 case TPACKET_V1:
2489 val = sizeof(struct tpacket_hdr);
2490 break;
2491 case TPACKET_V2:
2492 val = sizeof(struct tpacket2_hdr);
2493 break;
2494 default:
2495 return -EINVAL;
2496 }
2497 data = &val;
2498 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002499 case PACKET_RESERVE:
2500 if (len > sizeof(unsigned int))
2501 len = sizeof(unsigned int);
2502 val = po->tp_reserve;
2503 data = &val;
2504 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002505 case PACKET_LOSS:
2506 if (len > sizeof(unsigned int))
2507 len = sizeof(unsigned int);
2508 val = po->tp_loss;
2509 data = &val;
2510 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002511 case PACKET_TIMESTAMP:
2512 if (len > sizeof(int))
2513 len = sizeof(int);
2514 val = po->tp_tstamp;
2515 data = &val;
2516 break;
David S. Millerdc99f602011-07-05 01:45:05 -07002517 case PACKET_FANOUT:
2518 if (len > sizeof(int))
2519 len = sizeof(int);
2520 val = (po->fanout ?
2521 ((u32)po->fanout->id |
2522 ((u32)po->fanout->type << 16)) :
2523 0);
2524 data = &val;
2525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 default:
2527 return -ENOPROTOOPT;
2528 }
2529
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002530 if (put_user(len, optlen))
2531 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002532 if (copy_to_user(optval, data, len))
2533 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
2537
2538static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2539{
2540 struct sock *sk;
2541 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002542 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002543 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
stephen hemminger808f5112010-02-22 07:57:18 +00002545 rcu_read_lock();
2546 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 struct packet_sock *po = pkt_sk(sk);
2548
2549 switch (msg) {
2550 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (po->mclist)
2552 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002553 /* fallthrough */
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 case NETDEV_DOWN:
2556 if (dev->ifindex == po->ifindex) {
2557 spin_lock(&po->bind_lock);
2558 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07002559 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 sk->sk_err = ENETDOWN;
2561 if (!sock_flag(sk, SOCK_DEAD))
2562 sk->sk_error_report(sk);
2563 }
2564 if (msg == NETDEV_UNREGISTER) {
2565 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00002566 if (po->prot_hook.dev)
2567 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 po->prot_hook.dev = NULL;
2569 }
2570 spin_unlock(&po->bind_lock);
2571 }
2572 break;
2573 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002574 if (dev->ifindex == po->ifindex) {
2575 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002576 if (po->num)
2577 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00002578 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 break;
2581 }
2582 }
stephen hemminger808f5112010-02-22 07:57:18 +00002583 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return NOTIFY_DONE;
2585}
2586
2587
2588static int packet_ioctl(struct socket *sock, unsigned int cmd,
2589 unsigned long arg)
2590{
2591 struct sock *sk = sock->sk;
2592
Johann Baudy69e3c752009-05-18 22:11:22 -07002593 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002594 case SIOCOUTQ:
2595 {
2596 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002597
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002598 return put_user(amount, (int __user *)arg);
2599 }
2600 case SIOCINQ:
2601 {
2602 struct sk_buff *skb;
2603 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002605 spin_lock_bh(&sk->sk_receive_queue.lock);
2606 skb = skb_peek(&sk->sk_receive_queue);
2607 if (skb)
2608 amount = skb->len;
2609 spin_unlock_bh(&sk->sk_receive_queue.lock);
2610 return put_user(amount, (int __user *)arg);
2611 }
2612 case SIOCGSTAMP:
2613 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2614 case SIOCGSTAMPNS:
2615 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002618 case SIOCADDRT:
2619 case SIOCDELRT:
2620 case SIOCDARP:
2621 case SIOCGARP:
2622 case SIOCSARP:
2623 case SIOCGIFADDR:
2624 case SIOCSIFADDR:
2625 case SIOCGIFBRDADDR:
2626 case SIOCSIFBRDADDR:
2627 case SIOCGIFNETMASK:
2628 case SIOCSIFNETMASK:
2629 case SIOCGIFDSTADDR:
2630 case SIOCSIFDSTADDR:
2631 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002632 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633#endif
2634
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002635 default:
2636 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638 return 0;
2639}
2640
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002641static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 poll_table *wait)
2643{
2644 struct sock *sk = sock->sk;
2645 struct packet_sock *po = pkt_sk(sk);
2646 unsigned int mask = datagram_poll(file, sock, wait);
2647
2648 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002649 if (po->rx_ring.pg_vec) {
2650 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 mask |= POLLIN | POLLRDNORM;
2652 }
2653 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002654 spin_lock_bh(&sk->sk_write_queue.lock);
2655 if (po->tx_ring.pg_vec) {
2656 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2657 mask |= POLLOUT | POLLWRNORM;
2658 }
2659 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return mask;
2661}
2662
2663
2664/* Dirty? Well, I still did not learn better way to account
2665 * for user mmaps.
2666 */
2667
2668static void packet_mm_open(struct vm_area_struct *vma)
2669{
2670 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002671 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (sk)
2675 atomic_inc(&pkt_sk(sk)->mapped);
2676}
2677
2678static void packet_mm_close(struct vm_area_struct *vma)
2679{
2680 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002681 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (sk)
2685 atomic_dec(&pkt_sk(sk)->mapped);
2686}
2687
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002688static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002689 .open = packet_mm_open,
2690 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691};
2692
Neil Horman0e3125c2010-11-16 10:26:47 -08002693static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2694 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 int i;
2697
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002698 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002699 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002700 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002701 vfree(pg_vec[i].buffer);
2702 else
2703 free_pages((unsigned long)pg_vec[i].buffer,
2704 order);
2705 pg_vec[i].buffer = NULL;
2706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708 kfree(pg_vec);
2709}
2710
Changli Gaoc56b4d92010-12-01 02:52:57 +00002711static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002712{
Neil Horman0e3125c2010-11-16 10:26:47 -08002713 char *buffer = NULL;
2714 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2715 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002716
Neil Horman0e3125c2010-11-16 10:26:47 -08002717 buffer = (char *) __get_free_pages(gfp_flags, order);
2718
2719 if (buffer)
2720 return buffer;
2721
2722 /*
2723 * __get_free_pages failed, fall back to vmalloc
2724 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002725 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002726
2727 if (buffer)
2728 return buffer;
2729
2730 /*
2731 * vmalloc failed, lets dig into swap here
2732 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002733 gfp_flags &= ~__GFP_NORETRY;
2734 buffer = (char *)__get_free_pages(gfp_flags, order);
2735 if (buffer)
2736 return buffer;
2737
2738 /*
2739 * complete and utter failure
2740 */
2741 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002742}
2743
Neil Horman0e3125c2010-11-16 10:26:47 -08002744static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002745{
2746 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002747 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002748 int i;
2749
Neil Horman0e3125c2010-11-16 10:26:47 -08002750 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002751 if (unlikely(!pg_vec))
2752 goto out;
2753
2754 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002755 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002756 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002757 goto out_free_pgvec;
2758 }
2759
2760out:
2761 return pg_vec;
2762
2763out_free_pgvec:
2764 free_pg_vec(pg_vec, order, block_nr);
2765 pg_vec = NULL;
2766 goto out;
2767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Johann Baudy69e3c752009-05-18 22:11:22 -07002769static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2770 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Neil Horman0e3125c2010-11-16 10:26:47 -08002772 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002774 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002775 struct packet_ring_buffer *rb;
2776 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002777 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002778 int err;
2779
2780 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2781 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2782
2783 err = -EBUSY;
2784 if (!closing) {
2785 if (atomic_read(&po->mapped))
2786 goto out;
2787 if (atomic_read(&rb->pending))
2788 goto out;
2789 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002793 err = -EBUSY;
2794 if (unlikely(rb->pg_vec))
2795 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002797 switch (po->tp_version) {
2798 case TPACKET_V1:
2799 po->tp_hdrlen = TPACKET_HDRLEN;
2800 break;
2801 case TPACKET_V2:
2802 po->tp_hdrlen = TPACKET2_HDRLEN;
2803 break;
2804 }
2805
Johann Baudy69e3c752009-05-18 22:11:22 -07002806 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002807 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002808 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002809 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002810 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002811 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002812 po->tp_reserve))
2813 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002814 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Johann Baudy69e3c752009-05-18 22:11:22 -07002817 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2818 if (unlikely(rb->frames_per_block <= 0))
2819 goto out;
2820 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2821 req->tp_frame_nr))
2822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002825 order = get_order(req->tp_block_size);
2826 pg_vec = alloc_pg_vec(req, order);
2827 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002829 }
2830 /* Done */
2831 else {
2832 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002833 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002834 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
2837 lock_sock(sk);
2838
2839 /* Detach socket from network */
2840 spin_lock(&po->bind_lock);
2841 was_running = po->running;
2842 num = po->num;
2843 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07002845 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 synchronize_net();
2850
2851 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002852 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (closing || atomic_read(&po->mapped) == 0) {
2854 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002855 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002856 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002857 rb->frame_max = (req->tp_frame_nr - 1);
2858 rb->head = 0;
2859 rb->frame_size = req->tp_frame_size;
2860 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Changli Gaoc053fd92010-12-10 16:02:20 -08002862 swap(rb->pg_vec_order, order);
2863 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Johann Baudy69e3c752009-05-18 22:11:22 -07002865 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2866 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2867 tpacket_rcv : packet_rcv;
2868 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002870 pr_err("packet_mmap: vma is busy: %d\n",
2871 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Herbert Xu905db442009-01-30 14:12:06 -08002873 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002876 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07002878 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880 spin_unlock(&po->bind_lock);
2881
2882 release_sock(sk);
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (pg_vec)
2885 free_pg_vec(pg_vec, order, req->tp_block_nr);
2886out:
2887 return err;
2888}
2889
Johann Baudy69e3c752009-05-18 22:11:22 -07002890static int packet_mmap(struct file *file, struct socket *sock,
2891 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 struct sock *sk = sock->sk;
2894 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002895 unsigned long size, expected_size;
2896 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 unsigned long start;
2898 int err = -EINVAL;
2899 int i;
2900
2901 if (vma->vm_pgoff)
2902 return -EINVAL;
2903
Herbert Xu905db442009-01-30 14:12:06 -08002904 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002905
2906 expected_size = 0;
2907 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2908 if (rb->pg_vec) {
2909 expected_size += rb->pg_vec_len
2910 * rb->pg_vec_pages
2911 * PAGE_SIZE;
2912 }
2913 }
2914
2915 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002917
2918 size = vma->vm_end - vma->vm_start;
2919 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 goto out;
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002923 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2924 if (rb->pg_vec == NULL)
2925 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002926
Johann Baudy69e3c752009-05-18 22:11:22 -07002927 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002928 struct page *page;
2929 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002930 int pg_num;
2931
Changli Gaoc56b4d92010-12-01 02:52:57 +00002932 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2933 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002934 err = vm_insert_page(vma, start, page);
2935 if (unlikely(err))
2936 goto out;
2937 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002938 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002939 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002942
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002943 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 vma->vm_ops = &packet_mmap_ops;
2945 err = 0;
2946
2947out:
Herbert Xu905db442009-01-30 14:12:06 -08002948 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return err;
2950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002952static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .family = PF_PACKET,
2954 .owner = THIS_MODULE,
2955 .release = packet_release,
2956 .bind = packet_bind_spkt,
2957 .connect = sock_no_connect,
2958 .socketpair = sock_no_socketpair,
2959 .accept = sock_no_accept,
2960 .getname = packet_getname_spkt,
2961 .poll = datagram_poll,
2962 .ioctl = packet_ioctl,
2963 .listen = sock_no_listen,
2964 .shutdown = sock_no_shutdown,
2965 .setsockopt = sock_no_setsockopt,
2966 .getsockopt = sock_no_getsockopt,
2967 .sendmsg = packet_sendmsg_spkt,
2968 .recvmsg = packet_recvmsg,
2969 .mmap = sock_no_mmap,
2970 .sendpage = sock_no_sendpage,
2971};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002973static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 .family = PF_PACKET,
2975 .owner = THIS_MODULE,
2976 .release = packet_release,
2977 .bind = packet_bind,
2978 .connect = sock_no_connect,
2979 .socketpair = sock_no_socketpair,
2980 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002981 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .poll = packet_poll,
2983 .ioctl = packet_ioctl,
2984 .listen = sock_no_listen,
2985 .shutdown = sock_no_shutdown,
2986 .setsockopt = packet_setsockopt,
2987 .getsockopt = packet_getsockopt,
2988 .sendmsg = packet_sendmsg,
2989 .recvmsg = packet_recvmsg,
2990 .mmap = packet_mmap,
2991 .sendpage = sock_no_sendpage,
2992};
2993
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002994static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 .family = PF_PACKET,
2996 .create = packet_create,
2997 .owner = THIS_MODULE,
2998};
2999
3000static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003001 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002};
3003
3004#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00003007 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Denis V. Luneve372c412007-11-19 22:31:54 -08003009 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003010
3011 rcu_read_lock();
3012 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
3015static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3016{
Herbert Xu1bf40952007-12-16 14:04:02 -08003017 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003018 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
3021static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00003022 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
stephen hemminger808f5112010-02-22 07:57:18 +00003024 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003027static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 if (v == SEQ_START_TOKEN)
3030 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
3031 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00003032 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 const struct packet_sock *po = pkt_sk(s);
3034
3035 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003036 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 s,
3038 atomic_read(&s->sk_refcnt),
3039 s->sk_type,
3040 ntohs(po->num),
3041 po->ifindex,
3042 po->running,
3043 atomic_read(&s->sk_rmem_alloc),
3044 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003045 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047
3048 return 0;
3049}
3050
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003051static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 .start = packet_seq_start,
3053 .next = packet_seq_next,
3054 .stop = packet_seq_stop,
3055 .show = packet_seq_show,
3056};
3057
3058static int packet_seq_open(struct inode *inode, struct file *file)
3059{
Denis V. Luneve372c412007-11-19 22:31:54 -08003060 return seq_open_net(inode, file, &packet_seq_ops,
3061 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Arjan van de Venda7071d2007-02-12 00:55:36 -08003064static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .owner = THIS_MODULE,
3066 .open = packet_seq_open,
3067 .read = seq_read,
3068 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003069 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070};
3071
3072#endif
3073
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003074static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003075{
stephen hemminger808f5112010-02-22 07:57:18 +00003076 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08003077 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003078
3079 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
3080 return -ENOMEM;
3081
3082 return 0;
3083}
3084
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003085static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003086{
3087 proc_net_remove(net, "packet");
3088}
3089
3090static struct pernet_operations packet_net_ops = {
3091 .init = packet_net_init,
3092 .exit = packet_net_exit,
3093};
3094
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096static void __exit packet_exit(void)
3097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003099 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 sock_unregister(PF_PACKET);
3101 proto_unregister(&packet_proto);
3102}
3103
3104static int __init packet_init(void)
3105{
3106 int rc = proto_register(&packet_proto, 0);
3107
3108 if (rc != 0)
3109 goto out;
3110
3111 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003112 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114out:
3115 return rc;
3116}
3117
3118module_init(packet_init);
3119module_exit(packet_exit);
3120MODULE_LICENSE("GPL");
3121MODULE_ALIAS_NETPROTO(PF_PACKET);