blob: 9d46a07a15c0667925eafd727362a392cf1088a7 [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.
chetan lokef6fb8f12011-08-19 10:18:16 +000043 * Chetan Loke : Implemented TPACKET_V3 block abstraction
44 * layer.
45 * Copyright (C) 2011, <lokec@ccs.neu.edu>
46 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * This program is free software; you can redistribute it and/or
49 * modify it under the terms of the GNU General Public License
50 * as published by the Free Software Foundation; either version
51 * 2 of the License, or (at your option) any later version.
52 *
53 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/fcntl.h>
59#include <linux/socket.h>
60#include <linux/in.h>
61#include <linux/inet.h>
62#include <linux/netdevice.h>
63#include <linux/if_packet.h>
64#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080065#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080068#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/ip.h>
71#include <net/protocol.h>
72#include <linux/skbuff.h>
73#include <net/sock.h>
74#include <linux/errno.h>
75#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/uaccess.h>
77#include <asm/ioctls.h>
78#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <asm/io.h>
81#include <linux/proc_fs.h>
82#include <linux/seq_file.h>
83#include <linux/poll.h>
84#include <linux/module.h>
85#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080086#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070087#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080088#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000089#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070090#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#ifdef CONFIG_INET
93#include <net/inet_common.h>
94#endif
95
Pavel Emelyanov2787b042012-08-13 05:49:39 +000096#include "internal.h"
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 Assumptions:
100 - if device has no dev->hard_header routine, it adds and removes ll header
101 inside itself. In this case ll header is invisible outside of device,
102 but higher levels still should reserve dev->hard_header_len.
103 Some devices are enough clever to reallocate skb, when header
104 will not fit to reserved space (tunnel), another ones are silly
105 (PPP).
106 - packet socket receives packets with pulled ll header,
107 so that SOCK_RAW should push it back.
108
109On receive:
110-----------
111
112Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700113 mac_header -> ll header
114 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700117 mac_header -> ll header
118 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700121 mac_header -> UNKNOWN position. It is very likely, that it points to ll
122 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900123 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700124 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700127 mac_header -> data. ll header is still not built!
128 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130Resume
131 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
132
133
134On transmit:
135------------
136
137dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700138 mac_header -> ll header
139 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700142 mac_header -> data
143 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 We should set nh.raw on output to correct posistion,
146 packet classifier depends on it.
147 */
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* Private packet socket structures. */
150
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700151/* identical to struct packet_mreq except it has
152 * a longer address field.
153 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000154struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700155 int mr_ifindex;
156 unsigned short mr_type;
157 unsigned short mr_alen;
158 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
David S. Millera2efcfa2007-05-29 13:12:50 -0700160
Daniel Borkmann184f4892013-04-16 01:57:46 +0000161union tpacket_uhdr {
162 struct tpacket_hdr *h1;
163 struct tpacket2_hdr *h2;
164 struct tpacket3_hdr *h3;
165 void *raw;
166};
167
chetan lokef6fb8f12011-08-19 10:18:16 +0000168static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700169 int closing, int tx_ring);
170
chetan lokef6fb8f12011-08-19 10:18:16 +0000171#define V3_ALIGNMENT (8)
172
chetan lokebc59ba32011-08-25 10:43:30 +0000173#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f12011-08-19 10:18:16 +0000174
175#define BLK_PLUS_PRIV(sz_of_priv) \
176 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
177
chetan lokef6fb8f12011-08-19 10:18:16 +0000178#define PGV_FROM_VMALLOC 1
Johann Baudy69e3c752009-05-18 22:11:22 -0700179
chetan lokef6fb8f12011-08-19 10:18:16 +0000180#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
181#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
182#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
183#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
184#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
185#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
186#define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
187
Johann Baudy69e3c752009-05-18 22:11:22 -0700188struct packet_sock;
189static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000190static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
191 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
chetan lokef6fb8f12011-08-19 10:18:16 +0000193static void *packet_previous_frame(struct packet_sock *po,
194 struct packet_ring_buffer *rb,
195 int status);
196static void packet_increment_head(struct packet_ring_buffer *buff);
chetan lokebc59ba32011-08-25 10:43:30 +0000197static int prb_curr_blk_in_use(struct tpacket_kbdq_core *,
198 struct tpacket_block_desc *);
199static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000200 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000201static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000202 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000203static int prb_queue_frozen(struct tpacket_kbdq_core *);
204static void prb_open_block(struct tpacket_kbdq_core *,
205 struct tpacket_block_desc *);
chetan lokef6fb8f12011-08-19 10:18:16 +0000206static void prb_retire_rx_blk_timer_expired(unsigned long);
chetan lokebc59ba32011-08-25 10:43:30 +0000207static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
208static void prb_init_blk_timer(struct packet_sock *,
209 struct tpacket_kbdq_core *,
210 void (*func) (unsigned long));
211static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
212static void prb_clear_rxhash(struct tpacket_kbdq_core *,
213 struct tpacket3_hdr *);
214static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
215 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void packet_flush_mclist(struct sock *sk);
217
Herbert Xuffbc6112007-02-04 23:33:10 -0800218struct packet_skb_cb {
219 unsigned int origlen;
220 union {
221 struct sockaddr_pkt pkt;
222 struct sockaddr_ll ll;
223 } sa;
224};
225
226#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800227
chetan lokebc59ba32011-08-25 10:43:30 +0000228#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f12011-08-19 10:18:16 +0000229#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000230 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000231#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000232 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000233#define GET_NEXT_PRB_BLK_NUM(x) \
234 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
235 ((x)->kactive_blk_num+1) : 0)
236
David S. Millerdc99f602011-07-05 01:45:05 -0700237static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
238static void __fanout_link(struct sock *sk, struct packet_sock *po);
239
David S. Millerce06b032011-07-04 01:44:29 -0700240/* register_prot_hook must be invoked with the po->bind_lock held,
241 * or from a context in which asynchronous accesses to the packet
242 * socket is not possible (packet_create()).
243 */
244static void register_prot_hook(struct sock *sk)
245{
246 struct packet_sock *po = pkt_sk(sk);
247 if (!po->running) {
David S. Millerdc99f602011-07-05 01:45:05 -0700248 if (po->fanout)
249 __fanout_link(sk, po);
250 else
251 dev_add_pack(&po->prot_hook);
David S. Millerce06b032011-07-04 01:44:29 -0700252 sock_hold(sk);
253 po->running = 1;
254 }
255}
256
257/* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
258 * held. If the sync parameter is true, we will temporarily drop
259 * the po->bind_lock and do a synchronize_net to make sure no
260 * asynchronous packet processing paths still refer to the elements
261 * of po->prot_hook. If the sync parameter is false, it is the
262 * callers responsibility to take care of this.
263 */
264static void __unregister_prot_hook(struct sock *sk, bool sync)
265{
266 struct packet_sock *po = pkt_sk(sk);
267
268 po->running = 0;
David S. Millerdc99f602011-07-05 01:45:05 -0700269 if (po->fanout)
270 __fanout_unlink(sk, po);
271 else
272 __dev_remove_pack(&po->prot_hook);
David S. Millerce06b032011-07-04 01:44:29 -0700273 __sock_put(sk);
274
275 if (sync) {
276 spin_unlock(&po->bind_lock);
277 synchronize_net();
278 spin_lock(&po->bind_lock);
279 }
280}
281
282static void unregister_prot_hook(struct sock *sk, bool sync)
283{
284 struct packet_sock *po = pkt_sk(sk);
285
286 if (po->running)
287 __unregister_prot_hook(sk, sync);
288}
289
Changli Gaof6dafa92010-12-07 04:26:16 +0000290static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000291{
292 if (is_vmalloc_addr(addr))
293 return vmalloc_to_page(addr);
294 return virt_to_page(addr);
295}
296
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700297static void __packet_set_status(struct packet_sock *po, void *frame, int status)
298{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000299 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700300
301 h.raw = frame;
302 switch (po->tp_version) {
303 case TPACKET_V1:
304 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000305 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700306 break;
307 case TPACKET_V2:
308 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000309 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700310 break;
chetan lokef6fb8f12011-08-19 10:18:16 +0000311 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700312 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000313 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700314 BUG();
315 }
316
317 smp_wmb();
318}
319
320static int __packet_get_status(struct packet_sock *po, void *frame)
321{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000322 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700323
324 smp_rmb();
325
326 h.raw = frame;
327 switch (po->tp_version) {
328 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000329 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700330 return h.h1->tp_status;
331 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000332 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700333 return h.h2->tp_status;
chetan lokef6fb8f12011-08-19 10:18:16 +0000334 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700335 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000336 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700337 BUG();
338 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
Johann Baudy69e3c752009-05-18 22:11:22 -0700341
Daniel Borkmann7a513842013-04-23 00:39:29 +0000342static bool tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
343 unsigned int flags)
344{
345 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
346
347 if (shhwtstamps) {
348 if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
349 ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
350 return true;
351 if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
352 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
353 return true;
354 }
355
356 if (ktime_to_timespec_cond(skb->tstamp, ts))
357 return true;
358
359 return false;
360}
361
Willem de Bruijn2e313962013-04-23 00:39:28 +0000362static void __packet_set_timestamp(struct packet_sock *po, void *frame,
Daniel Borkmann7a513842013-04-23 00:39:29 +0000363 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000364{
365 union tpacket_uhdr h;
366 struct timespec ts;
367
Daniel Borkmann7a513842013-04-23 00:39:29 +0000368 if (!tpacket_get_timestamp(skb, &ts, po->tp_tstamp))
Willem de Bruijn2e313962013-04-23 00:39:28 +0000369 return;
370
371 h.raw = frame;
372 switch (po->tp_version) {
373 case TPACKET_V1:
374 h.h1->tp_sec = ts.tv_sec;
375 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
376 break;
377 case TPACKET_V2:
378 h.h2->tp_sec = ts.tv_sec;
379 h.h2->tp_nsec = ts.tv_nsec;
380 break;
381 case TPACKET_V3:
382 default:
383 WARN(1, "TPACKET version not supported.\n");
384 BUG();
385 }
386
387 /* one flush is safe, as both fields always lie on the same cacheline */
388 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
389 smp_wmb();
390}
391
Johann Baudy69e3c752009-05-18 22:11:22 -0700392static void *packet_lookup_frame(struct packet_sock *po,
393 struct packet_ring_buffer *rb,
394 unsigned int position,
395 int status)
396{
397 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000398 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700399
400 pg_vec_pos = position / rb->frames_per_block;
401 frame_offset = position % rb->frames_per_block;
402
Neil Horman0e3125c2010-11-16 10:26:47 -0800403 h.raw = rb->pg_vec[pg_vec_pos].buffer +
404 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700405
406 if (status != __packet_get_status(po, h.raw))
407 return NULL;
408
409 return h.raw;
410}
411
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000412static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700413 struct packet_ring_buffer *rb,
414 int status)
415{
416 return packet_lookup_frame(po, rb, rb->head, status);
417}
418
chetan lokebc59ba32011-08-25 10:43:30 +0000419static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000420{
421 del_timer_sync(&pkc->retire_blk_timer);
422}
423
424static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
425 int tx_ring,
426 struct sk_buff_head *rb_queue)
427{
chetan lokebc59ba32011-08-25 10:43:30 +0000428 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000429
430 pkc = tx_ring ? &po->tx_ring.prb_bdqc : &po->rx_ring.prb_bdqc;
431
432 spin_lock(&rb_queue->lock);
433 pkc->delete_blk_timer = 1;
434 spin_unlock(&rb_queue->lock);
435
436 prb_del_retire_blk_timer(pkc);
437}
438
439static void prb_init_blk_timer(struct packet_sock *po,
chetan lokebc59ba32011-08-25 10:43:30 +0000440 struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000441 void (*func) (unsigned long))
442{
443 init_timer(&pkc->retire_blk_timer);
444 pkc->retire_blk_timer.data = (long)po;
445 pkc->retire_blk_timer.function = func;
446 pkc->retire_blk_timer.expires = jiffies;
447}
448
449static void prb_setup_retire_blk_timer(struct packet_sock *po, int tx_ring)
450{
chetan lokebc59ba32011-08-25 10:43:30 +0000451 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000452
453 if (tx_ring)
454 BUG();
455
456 pkc = tx_ring ? &po->tx_ring.prb_bdqc : &po->rx_ring.prb_bdqc;
457 prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
458}
459
460static int prb_calc_retire_blk_tmo(struct packet_sock *po,
461 int blk_size_in_bytes)
462{
463 struct net_device *dev;
464 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000465 struct ethtool_cmd ecmd;
466 int err;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000467 u32 speed;
chetan lokef6fb8f12011-08-19 10:18:16 +0000468
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000469 rtnl_lock();
470 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
471 if (unlikely(!dev)) {
472 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000473 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000474 }
475 err = __ethtool_get_settings(dev, &ecmd);
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000476 speed = ethtool_cmd_speed(&ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000477 rtnl_unlock();
478 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000479 /*
480 * If the link speed is so slow you don't really
481 * need to worry about perf anyways
482 */
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000483 if (speed < SPEED_1000 || speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000484 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000485 } else {
486 msec = 1;
487 div = speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000488 }
489 }
490
491 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
492
493 if (div)
494 mbits /= div;
495
496 tmo = mbits * msec;
497
498 if (div)
499 return tmo+1;
500 return tmo;
501}
502
chetan lokebc59ba32011-08-25 10:43:30 +0000503static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000504 union tpacket_req_u *req_u)
505{
506 p1->feature_req_word = req_u->req3.tp_feature_req_word;
507}
508
509static void init_prb_bdqc(struct packet_sock *po,
510 struct packet_ring_buffer *rb,
511 struct pgv *pg_vec,
512 union tpacket_req_u *req_u, int tx_ring)
513{
chetan lokebc59ba32011-08-25 10:43:30 +0000514 struct tpacket_kbdq_core *p1 = &rb->prb_bdqc;
515 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000516
517 memset(p1, 0x0, sizeof(*p1));
518
519 p1->knxt_seq_num = 1;
520 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000521 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000522 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000523 p1->kblk_size = req_u->req3.tp_block_size;
524 p1->knum_blocks = req_u->req3.tp_block_nr;
525 p1->hdrlen = po->tp_hdrlen;
526 p1->version = po->tp_version;
527 p1->last_kactive_blk_num = 0;
528 po->stats_u.stats3.tp_freeze_q_cnt = 0;
529 if (req_u->req3.tp_retire_blk_tov)
530 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
531 else
532 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
533 req_u->req3.tp_block_size);
534 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
535 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
536
537 prb_init_ft_ops(p1, req_u);
538 prb_setup_retire_blk_timer(po, tx_ring);
539 prb_open_block(p1, pbd);
540}
541
542/* Do NOT update the last_blk_num first.
543 * Assumes sk_buff_head lock is held.
544 */
chetan lokebc59ba32011-08-25 10:43:30 +0000545static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000546{
547 mod_timer(&pkc->retire_blk_timer,
548 jiffies + pkc->tov_in_jiffies);
549 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
550}
551
552/*
553 * Timer logic:
554 * 1) We refresh the timer only when we open a block.
555 * By doing this we don't waste cycles refreshing the timer
556 * on packet-by-packet basis.
557 *
558 * With a 1MB block-size, on a 1Gbps line, it will take
559 * i) ~8 ms to fill a block + ii) memcpy etc.
560 * In this cut we are not accounting for the memcpy time.
561 *
562 * So, if the user sets the 'tmo' to 10ms then the timer
563 * will never fire while the block is still getting filled
564 * (which is what we want). However, the user could choose
565 * to close a block early and that's fine.
566 *
567 * But when the timer does fire, we check whether or not to refresh it.
568 * Since the tmo granularity is in msecs, it is not too expensive
569 * to refresh the timer, lets say every '8' msecs.
570 * Either the user can set the 'tmo' or we can derive it based on
571 * a) line-speed and b) block-size.
572 * prb_calc_retire_blk_tmo() calculates the tmo.
573 *
574 */
575static void prb_retire_rx_blk_timer_expired(unsigned long data)
576{
577 struct packet_sock *po = (struct packet_sock *)data;
chetan lokebc59ba32011-08-25 10:43:30 +0000578 struct tpacket_kbdq_core *pkc = &po->rx_ring.prb_bdqc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000579 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000580 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000581
582 spin_lock(&po->sk.sk_receive_queue.lock);
583
584 frozen = prb_queue_frozen(pkc);
585 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
586
587 if (unlikely(pkc->delete_blk_timer))
588 goto out;
589
590 /* We only need to plug the race when the block is partially filled.
591 * tpacket_rcv:
592 * lock(); increment BLOCK_NUM_PKTS; unlock()
593 * copy_bits() is in progress ...
594 * timer fires on other cpu:
595 * we can't retire the current block because copy_bits
596 * is in progress.
597 *
598 */
599 if (BLOCK_NUM_PKTS(pbd)) {
600 while (atomic_read(&pkc->blk_fill_in_prog)) {
601 /* Waiting for skb_copy_bits to finish... */
602 cpu_relax();
603 }
604 }
605
606 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
607 if (!frozen) {
608 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
609 if (!prb_dispatch_next_block(pkc, po))
610 goto refresh_timer;
611 else
612 goto out;
613 } else {
614 /* Case 1. Queue was frozen because user-space was
615 * lagging behind.
616 */
617 if (prb_curr_blk_in_use(pkc, pbd)) {
618 /*
619 * Ok, user-space is still behind.
620 * So just refresh the timer.
621 */
622 goto refresh_timer;
623 } else {
624 /* Case 2. queue was frozen,user-space caught up,
625 * now the link went idle && the timer fired.
626 * We don't have a block to close.So we open this
627 * block and restart the timer.
628 * opening a block thaws the queue,restarts timer
629 * Thawing/timer-refresh is a side effect.
630 */
631 prb_open_block(pkc, pbd);
632 goto out;
633 }
634 }
635 }
636
637refresh_timer:
638 _prb_refresh_rx_retire_blk_timer(pkc);
639
640out:
641 spin_unlock(&po->sk.sk_receive_queue.lock);
642}
643
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000644static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000645 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000646{
647 /* Flush everything minus the block header */
648
649#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
650 u8 *start, *end;
651
652 start = (u8 *)pbd1;
653
654 /* Skip the block header(we know header WILL fit in 4K) */
655 start += PAGE_SIZE;
656
657 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
658 for (; start < end; start += PAGE_SIZE)
659 flush_dcache_page(pgv_to_page(start));
660
661 smp_wmb();
662#endif
663
664 /* Now update the block status. */
665
666 BLOCK_STATUS(pbd1) = status;
667
668 /* Flush the block header */
669
670#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
671 start = (u8 *)pbd1;
672 flush_dcache_page(pgv_to_page(start));
673
674 smp_wmb();
675#endif
676}
677
678/*
679 * Side effect:
680 *
681 * 1) flush the block
682 * 2) Increment active_blk_num
683 *
684 * Note:We DONT refresh the timer on purpose.
685 * Because almost always the next block will be opened.
686 */
chetan lokebc59ba32011-08-25 10:43:30 +0000687static void prb_close_block(struct tpacket_kbdq_core *pkc1,
688 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000689 struct packet_sock *po, unsigned int stat)
690{
691 __u32 status = TP_STATUS_USER | stat;
692
693 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000694 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000695
696 if (po->stats.tp_drops)
697 status |= TP_STATUS_LOSING;
698
699 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
700 last_pkt->tp_next_offset = 0;
701
702 /* Get the ts of the last pkt */
703 if (BLOCK_NUM_PKTS(pbd1)) {
704 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
705 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
706 } else {
707 /* Ok, we tmo'd - so get the current time */
708 struct timespec ts;
709 getnstimeofday(&ts);
710 h1->ts_last_pkt.ts_sec = ts.tv_sec;
711 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
712 }
713
714 smp_wmb();
715
716 /* Flush the block */
717 prb_flush_block(pkc1, pbd1, status);
718
719 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
720}
721
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000722static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000723{
724 pkc->reset_pending_on_curr_blk = 0;
725}
726
727/*
728 * Side effect of opening a block:
729 *
730 * 1) prb_queue is thawed.
731 * 2) retire_blk_timer is refreshed.
732 *
733 */
chetan lokebc59ba32011-08-25 10:43:30 +0000734static void prb_open_block(struct tpacket_kbdq_core *pkc1,
735 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000736{
737 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000738 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000739
740 smp_rmb();
741
742 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd1))) {
743
744 /* We could have just memset this but we will lose the
745 * flexibility of making the priv area sticky
746 */
747 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
748 BLOCK_NUM_PKTS(pbd1) = 0;
749 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
750 getnstimeofday(&ts);
751 h1->ts_first_pkt.ts_sec = ts.tv_sec;
752 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
753 pkc1->pkblk_start = (char *)pbd1;
Joe Perchese3192692012-06-03 17:41:40 +0000754 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000755 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
756 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
757 pbd1->version = pkc1->version;
758 pkc1->prev = pkc1->nxt_offset;
759 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
760 prb_thaw_queue(pkc1);
761 _prb_refresh_rx_retire_blk_timer(pkc1);
762
763 smp_wmb();
764
765 return;
766 }
767
768 WARN(1, "ERROR block:%p is NOT FREE status:%d kactive_blk_num:%d\n",
769 pbd1, BLOCK_STATUS(pbd1), pkc1->kactive_blk_num);
770 dump_stack();
771 BUG();
772}
773
774/*
775 * Queue freeze logic:
776 * 1) Assume tp_block_nr = 8 blocks.
777 * 2) At time 't0', user opens Rx ring.
778 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
779 * 4) user-space is either sleeping or processing block '0'.
780 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
781 * it will close block-7,loop around and try to fill block '0'.
782 * call-flow:
783 * __packet_lookup_frame_in_block
784 * prb_retire_current_block()
785 * prb_dispatch_next_block()
786 * |->(BLOCK_STATUS == USER) evaluates to true
787 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
788 * 6) Now there are two cases:
789 * 6.1) Link goes idle right after the queue is frozen.
790 * But remember, the last open_block() refreshed the timer.
791 * When this timer expires,it will refresh itself so that we can
792 * re-open block-0 in near future.
793 * 6.2) Link is busy and keeps on receiving packets. This is a simple
794 * case and __packet_lookup_frame_in_block will check if block-0
795 * is free and can now be re-used.
796 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000797static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000798 struct packet_sock *po)
799{
800 pkc->reset_pending_on_curr_blk = 1;
801 po->stats_u.stats3.tp_freeze_q_cnt++;
802}
803
804#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
805
806/*
807 * If the next block is free then we will dispatch it
808 * and return a good offset.
809 * Else, we will freeze the queue.
810 * So, caller must check the return value.
811 */
chetan lokebc59ba32011-08-25 10:43:30 +0000812static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000813 struct packet_sock *po)
814{
chetan lokebc59ba32011-08-25 10:43:30 +0000815 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000816
817 smp_rmb();
818
819 /* 1. Get current block num */
820 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
821
822 /* 2. If this block is currently in_use then freeze the queue */
823 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
824 prb_freeze_queue(pkc, po);
825 return NULL;
826 }
827
828 /*
829 * 3.
830 * open this block and return the offset where the first packet
831 * needs to get stored.
832 */
833 prb_open_block(pkc, pbd);
834 return (void *)pkc->nxt_offset;
835}
836
chetan lokebc59ba32011-08-25 10:43:30 +0000837static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000838 struct packet_sock *po, unsigned int status)
839{
chetan lokebc59ba32011-08-25 10:43:30 +0000840 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000841
842 /* retire/close the current block */
843 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
844 /*
845 * Plug the case where copy_bits() is in progress on
846 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
847 * have space to copy the pkt in the current block and
848 * called prb_retire_current_block()
849 *
850 * We don't need to worry about the TMO case because
851 * the timer-handler already handled this case.
852 */
853 if (!(status & TP_STATUS_BLK_TMO)) {
854 while (atomic_read(&pkc->blk_fill_in_prog)) {
855 /* Waiting for skb_copy_bits to finish... */
856 cpu_relax();
857 }
858 }
859 prb_close_block(pkc, pbd, po, status);
860 return;
861 }
862
863 WARN(1, "ERROR-pbd[%d]:%p\n", pkc->kactive_blk_num, pbd);
864 dump_stack();
865 BUG();
866}
867
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000868static int prb_curr_blk_in_use(struct tpacket_kbdq_core *pkc,
chetan lokebc59ba32011-08-25 10:43:30 +0000869 struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000870{
871 return TP_STATUS_USER & BLOCK_STATUS(pbd);
872}
873
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000874static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000875{
876 return pkc->reset_pending_on_curr_blk;
877}
878
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000879static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +0000880{
chetan lokebc59ba32011-08-25 10:43:30 +0000881 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000882 atomic_dec(&pkc->blk_fill_in_prog);
883}
884
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000885static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000886 struct tpacket3_hdr *ppd)
887{
888 ppd->hv1.tp_rxhash = skb_get_rxhash(pkc->skb);
889}
890
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000891static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000892 struct tpacket3_hdr *ppd)
893{
894 ppd->hv1.tp_rxhash = 0;
895}
896
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000897static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000898 struct tpacket3_hdr *ppd)
899{
900 if (vlan_tx_tag_present(pkc->skb)) {
901 ppd->hv1.tp_vlan_tci = vlan_tx_tag_get(pkc->skb);
902 ppd->tp_status = TP_STATUS_VLAN_VALID;
903 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000904 ppd->hv1.tp_vlan_tci = 0;
905 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +0000906 }
907}
908
chetan lokebc59ba32011-08-25 10:43:30 +0000909static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000910 struct tpacket3_hdr *ppd)
911{
912 prb_fill_vlan_info(pkc, ppd);
913
914 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
915 prb_fill_rxhash(pkc, ppd);
916 else
917 prb_clear_rxhash(pkc, ppd);
918}
919
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000920static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000921 struct tpacket_kbdq_core *pkc,
922 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +0000923 unsigned int len)
924{
925 struct tpacket3_hdr *ppd;
926
927 ppd = (struct tpacket3_hdr *)curr;
928 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
929 pkc->prev = curr;
930 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
931 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
932 BLOCK_NUM_PKTS(pbd) += 1;
933 atomic_inc(&pkc->blk_fill_in_prog);
934 prb_run_all_ft_ops(pkc, ppd);
935}
936
937/* Assumes caller has the sk->rx_queue.lock */
938static void *__packet_lookup_frame_in_block(struct packet_sock *po,
939 struct sk_buff *skb,
940 int status,
941 unsigned int len
942 )
943{
chetan lokebc59ba32011-08-25 10:43:30 +0000944 struct tpacket_kbdq_core *pkc;
945 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000946 char *curr, *end;
947
Joe Perchese3192692012-06-03 17:41:40 +0000948 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000949 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
950
951 /* Queue is frozen when user space is lagging behind */
952 if (prb_queue_frozen(pkc)) {
953 /*
954 * Check if that last block which caused the queue to freeze,
955 * is still in_use by user-space.
956 */
957 if (prb_curr_blk_in_use(pkc, pbd)) {
958 /* Can't record this packet */
959 return NULL;
960 } else {
961 /*
962 * Ok, the block was released by user-space.
963 * Now let's open that block.
964 * opening a block also thaws the queue.
965 * Thawing is a side effect.
966 */
967 prb_open_block(pkc, pbd);
968 }
969 }
970
971 smp_mb();
972 curr = pkc->nxt_offset;
973 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +0000974 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +0000975
976 /* first try the current block */
977 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
978 prb_fill_curr_block(curr, pkc, pbd, len);
979 return (void *)curr;
980 }
981
982 /* Ok, close the current block */
983 prb_retire_current_block(pkc, po, 0);
984
985 /* Now, try to dispatch the next block */
986 curr = (char *)prb_dispatch_next_block(pkc, po);
987 if (curr) {
988 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
989 prb_fill_curr_block(curr, pkc, pbd, len);
990 return (void *)curr;
991 }
992
993 /*
994 * No free blocks are available.user_space hasn't caught up yet.
995 * Queue was just frozen and now this packet will get dropped.
996 */
997 return NULL;
998}
999
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001000static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001001 struct sk_buff *skb,
1002 int status, unsigned int len)
1003{
1004 char *curr = NULL;
1005 switch (po->tp_version) {
1006 case TPACKET_V1:
1007 case TPACKET_V2:
1008 curr = packet_lookup_frame(po, &po->rx_ring,
1009 po->rx_ring.head, status);
1010 return curr;
1011 case TPACKET_V3:
1012 return __packet_lookup_frame_in_block(po, skb, status, len);
1013 default:
1014 WARN(1, "TPACKET version not supported\n");
1015 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001016 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +00001017 }
1018}
1019
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001020static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001021 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001022 unsigned int idx,
chetan lokef6fb8f12011-08-19 10:18:16 +00001023 int status)
1024{
chetan lokebc59ba32011-08-25 10:43:30 +00001025 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001026 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001027
1028 if (status != BLOCK_STATUS(pbd))
1029 return NULL;
1030 return pbd;
1031}
1032
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001033static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001034{
1035 unsigned int prev;
1036 if (rb->prb_bdqc.kactive_blk_num)
1037 prev = rb->prb_bdqc.kactive_blk_num-1;
1038 else
1039 prev = rb->prb_bdqc.knum_blocks-1;
1040 return prev;
1041}
1042
1043/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001044static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001045 struct packet_ring_buffer *rb,
1046 int status)
1047{
1048 unsigned int previous = prb_previous_blk_num(rb);
1049 return prb_lookup_block(po, rb, previous, status);
1050}
1051
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001052static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001053 struct packet_ring_buffer *rb,
1054 int status)
1055{
1056 if (po->tp_version <= TPACKET_V2)
1057 return packet_previous_frame(po, rb, status);
1058
1059 return __prb_previous_block(po, rb, status);
1060}
1061
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001062static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001063 struct packet_ring_buffer *rb)
1064{
1065 switch (po->tp_version) {
1066 case TPACKET_V1:
1067 case TPACKET_V2:
1068 return packet_increment_head(rb);
1069 case TPACKET_V3:
1070 default:
1071 WARN(1, "TPACKET version not supported.\n");
1072 BUG();
1073 return;
1074 }
1075}
1076
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001077static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001078 struct packet_ring_buffer *rb,
1079 int status)
1080{
1081 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1082 return packet_lookup_frame(po, rb, previous, status);
1083}
1084
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001085static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001086{
1087 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1088}
1089
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001090static bool packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1091{
1092 struct sock *sk = &po->sk;
1093 bool has_room;
1094
1095 if (po->prot_hook.func != tpacket_rcv)
1096 return (atomic_read(&sk->sk_rmem_alloc) + skb->truesize)
1097 <= sk->sk_rcvbuf;
1098
1099 spin_lock(&sk->sk_receive_queue.lock);
1100 if (po->tp_version == TPACKET_V3)
1101 has_room = prb_lookup_block(po, &po->rx_ring,
1102 po->rx_ring.prb_bdqc.kactive_blk_num,
1103 TP_STATUS_KERNEL);
1104 else
1105 has_room = packet_lookup_frame(po, &po->rx_ring,
1106 po->rx_ring.head,
1107 TP_STATUS_KERNEL);
1108 spin_unlock(&sk->sk_receive_queue.lock);
1109
1110 return has_room;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113static void packet_sock_destruct(struct sock *sk)
1114{
Richard Cochraned85b562010-04-07 22:41:28 +00001115 skb_queue_purge(&sk->sk_error_queue);
1116
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001117 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
1118 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001121 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return;
1123 }
1124
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001125 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
David S. Millerdc99f602011-07-05 01:45:05 -07001128static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
1129{
1130 int x = atomic_read(&f->rr_cur) + 1;
1131
1132 if (x >= num)
1133 x = 0;
1134
1135 return x;
1136}
1137
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001138static unsigned int fanout_demux_hash(struct packet_fanout *f,
1139 struct sk_buff *skb,
1140 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001141{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001142 return (((u64)skb->rxhash) * num) >> 32;
David S. Millerdc99f602011-07-05 01:45:05 -07001143}
1144
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001145static unsigned int fanout_demux_lb(struct packet_fanout *f,
1146 struct sk_buff *skb,
1147 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001148{
1149 int cur, old;
1150
1151 cur = atomic_read(&f->rr_cur);
1152 while ((old = atomic_cmpxchg(&f->rr_cur, cur,
1153 fanout_rr_next(f, num))) != cur)
1154 cur = old;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001155 return cur;
David S. Millerdc99f602011-07-05 01:45:05 -07001156}
1157
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001158static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1159 struct sk_buff *skb,
1160 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001161{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001162 return smp_processor_id() % num;
1163}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001164
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001165static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1166 struct sk_buff *skb,
1167 unsigned int idx, unsigned int skip,
1168 unsigned int num)
1169{
1170 unsigned int i, j;
1171
1172 i = j = min_t(int, f->next[idx], num - 1);
1173 do {
1174 if (i != skip && packet_rcv_has_room(pkt_sk(f->arr[i]), skb)) {
1175 if (i != j)
1176 f->next[idx] = i;
1177 return i;
1178 }
1179 if (++i == num)
1180 i = 0;
1181 } while (i != j);
1182
1183 return idx;
1184}
1185
1186static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1187{
1188 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001189}
1190
David S. Miller95ec3eb2011-07-06 01:56:38 -07001191static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1192 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001193{
1194 struct packet_fanout *f = pt->af_packet_priv;
1195 unsigned int num = f->num_members;
1196 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001197 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001198
1199 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
1200 !num) {
1201 kfree_skb(skb);
1202 return 0;
1203 }
1204
David S. Miller95ec3eb2011-07-06 01:56:38 -07001205 switch (f->type) {
1206 case PACKET_FANOUT_HASH:
1207 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001208 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +00001209 skb = ip_check_defrag(skb, IP_DEFRAG_AF_PACKET);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001210 if (!skb)
1211 return 0;
1212 }
1213 skb_get_rxhash(skb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001214 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001215 break;
1216 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001217 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001218 break;
1219 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001220 idx = fanout_demux_cpu(f, skb, num);
1221 break;
1222 case PACKET_FANOUT_ROLLOVER:
1223 idx = fanout_demux_rollover(f, skb, 0, (unsigned int) -1, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001224 break;
David S. Miller7736d332011-07-05 01:43:20 -07001225 }
1226
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001227 po = pkt_sk(f->arr[idx]);
1228 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER) &&
1229 unlikely(!packet_rcv_has_room(po, skb))) {
1230 idx = fanout_demux_rollover(f, skb, idx, idx, num);
1231 po = pkt_sk(f->arr[idx]);
1232 }
David S. Millerdc99f602011-07-05 01:45:05 -07001233
1234 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1235}
1236
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001237DEFINE_MUTEX(fanout_mutex);
1238EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001239static LIST_HEAD(fanout_list);
1240
1241static void __fanout_link(struct sock *sk, struct packet_sock *po)
1242{
1243 struct packet_fanout *f = po->fanout;
1244
1245 spin_lock(&f->lock);
1246 f->arr[f->num_members] = sk;
1247 smp_wmb();
1248 f->num_members++;
1249 spin_unlock(&f->lock);
1250}
1251
1252static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1253{
1254 struct packet_fanout *f = po->fanout;
1255 int i;
1256
1257 spin_lock(&f->lock);
1258 for (i = 0; i < f->num_members; i++) {
1259 if (f->arr[i] == sk)
1260 break;
1261 }
1262 BUG_ON(i >= f->num_members);
1263 f->arr[i] = f->arr[f->num_members - 1];
1264 f->num_members--;
1265 spin_unlock(&f->lock);
1266}
1267
Fengguang Wua0dfb262012-08-23 19:51:21 +08001268static bool match_fanout_group(struct packet_type *ptype, struct sock * sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001269{
1270 if (ptype->af_packet_priv == (void*)((struct packet_sock *)sk)->fanout)
1271 return true;
1272
1273 return false;
1274}
1275
David S. Miller7736d332011-07-05 01:43:20 -07001276static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001277{
1278 struct packet_sock *po = pkt_sk(sk);
1279 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001280 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001281 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001282 int err;
1283
1284 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001285 case PACKET_FANOUT_ROLLOVER:
1286 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1287 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001288 case PACKET_FANOUT_HASH:
1289 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001290 case PACKET_FANOUT_CPU:
David S. Millerdc99f602011-07-05 01:45:05 -07001291 break;
1292 default:
1293 return -EINVAL;
1294 }
1295
1296 if (!po->running)
1297 return -EINVAL;
1298
1299 if (po->fanout)
1300 return -EALREADY;
1301
1302 mutex_lock(&fanout_mutex);
1303 match = NULL;
1304 list_for_each_entry(f, &fanout_list, list) {
1305 if (f->id == id &&
1306 read_pnet(&f->net) == sock_net(sk)) {
1307 match = f;
1308 break;
1309 }
1310 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001311 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001312 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001313 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001314 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001315 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001316 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001317 if (!match)
1318 goto out;
1319 write_pnet(&match->net, sock_net(sk));
1320 match->id = id;
1321 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001322 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001323 atomic_set(&match->rr_cur, 0);
1324 INIT_LIST_HEAD(&match->list);
1325 spin_lock_init(&match->lock);
1326 atomic_set(&match->sk_ref, 0);
1327 match->prot_hook.type = po->prot_hook.type;
1328 match->prot_hook.dev = po->prot_hook.dev;
1329 match->prot_hook.func = packet_rcv_fanout;
1330 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001331 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001332 dev_add_pack(&match->prot_hook);
1333 list_add(&match->list, &fanout_list);
1334 }
1335 err = -EINVAL;
1336 if (match->type == type &&
1337 match->prot_hook.type == po->prot_hook.type &&
1338 match->prot_hook.dev == po->prot_hook.dev) {
1339 err = -ENOSPC;
1340 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1341 __dev_remove_pack(&po->prot_hook);
1342 po->fanout = match;
1343 atomic_inc(&match->sk_ref);
1344 __fanout_link(sk, po);
1345 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001346 }
1347 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001348out:
David S. Millerdc99f602011-07-05 01:45:05 -07001349 mutex_unlock(&fanout_mutex);
1350 return err;
1351}
1352
1353static void fanout_release(struct sock *sk)
1354{
1355 struct packet_sock *po = pkt_sk(sk);
1356 struct packet_fanout *f;
1357
1358 f = po->fanout;
1359 if (!f)
1360 return;
1361
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001362 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001363 po->fanout = NULL;
1364
David S. Millerdc99f602011-07-05 01:45:05 -07001365 if (atomic_dec_and_test(&f->sk_ref)) {
1366 list_del(&f->list);
1367 dev_remove_pack(&f->prot_hook);
1368 kfree(f);
1369 }
1370 mutex_unlock(&fanout_mutex);
1371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001373static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001375static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001377static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1378 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct sock *sk;
1381 struct sockaddr_pkt *spkt;
1382
1383 /*
1384 * When we registered the protocol we saved the socket in the data
1385 * field for just this event.
1386 */
1387
1388 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * Yank back the headers [hope the device set this
1392 * right or kerboom...]
1393 *
1394 * Incoming packets have ll header pulled,
1395 * push it back.
1396 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001397 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 * so that this procedure is noop.
1399 */
1400
1401 if (skb->pkt_type == PACKET_LOOPBACK)
1402 goto out;
1403
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001404 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001405 goto out;
1406
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001407 skb = skb_share_check(skb, GFP_ATOMIC);
1408 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 goto oom;
1410
1411 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001412 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Phil Oester84531c22005-07-12 11:57:52 -07001414 /* drop conntrack reference */
1415 nf_reset(skb);
1416
Herbert Xuffbc6112007-02-04 23:33:10 -08001417 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001419 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * The SOCK_PACKET socket receives _all_ frames.
1423 */
1424
1425 spkt->spkt_family = dev->type;
1426 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1427 spkt->spkt_protocol = skb->protocol;
1428
1429 /*
1430 * Charge the memory to the socket. This is done specifically
1431 * to prevent sockets using all the memory up.
1432 */
1433
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001434 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
1436
1437out:
1438 kfree_skb(skb);
1439oom:
1440 return 0;
1441}
1442
1443
1444/*
1445 * Output a raw packet to a device layer. This bypasses all the other
1446 * protocol layers and you must therefore supply it with a complete frame
1447 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
1450 struct msghdr *msg, size_t len)
1451{
1452 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001453 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001454 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001456 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001458 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001461 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 */
1463
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001464 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001466 return -EINVAL;
1467 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1468 proto = saddr->spkt_protocol;
1469 } else
1470 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001473 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
1475
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001476 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001477retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001478 rcu_read_lock();
1479 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 err = -ENODEV;
1481 if (dev == NULL)
1482 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001483
David S. Millerd5e76b02007-01-25 19:30:36 -08001484 err = -ENETDOWN;
1485 if (!(dev->flags & IFF_UP))
1486 goto out_unlock;
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001489 * You may not queue a frame bigger than the mtu. This is the lowest level
1490 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001492
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001493 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1494 if (!netif_supports_nofcs(dev)) {
1495 err = -EPROTONOSUPPORT;
1496 goto out_unlock;
1497 }
1498 extra_len = 4; /* We're doing our own CRC */
1499 }
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001502 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto out_unlock;
1504
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001505 if (!skb) {
1506 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001507 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001508 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001510 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001511 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001512 if (skb == NULL)
1513 return -ENOBUFS;
1514 /* FIXME: Save some space for broken drivers that write a hard
1515 * header at transmission time by themselves. PPP is the notable
1516 * one here. This should really be fixed at the driver level.
1517 */
1518 skb_reserve(skb, reserved);
1519 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001520
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001521 /* Try to align data part correctly */
1522 if (hhlen) {
1523 skb->data -= hhlen;
1524 skb->tail -= hhlen;
1525 if (len < hhlen)
1526 skb_reset_network_header(skb);
1527 }
1528 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1529 if (err)
1530 goto out_free;
1531 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001534 if (len > (dev->mtu + dev->hard_header_len + extra_len)) {
Ben Greear57f89bf2011-02-11 09:35:18 +00001535 /* Earlier code assumed this would be a VLAN pkt,
1536 * double-check this now that we have the actual
1537 * packet in hand.
1538 */
1539 struct ethhdr *ehdr;
1540 skb_reset_mac_header(skb);
1541 ehdr = eth_hdr(skb);
1542 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1543 err = -EMSGSIZE;
1544 goto out_unlock;
1545 }
1546 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 skb->protocol = proto;
1549 skb->dev = dev;
1550 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001551 skb->mark = sk->sk_mark;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00001552
1553 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001555 if (unlikely(extra_len == 4))
1556 skb->no_fcs = 1;
1557
Jason Wang40893fd2013-03-26 23:11:22 +00001558 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001561 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001562 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001565 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001566out_free:
1567 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return err;
1569}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001571static unsigned int run_filter(const struct sk_buff *skb,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001572 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -08001573 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct sk_filter *filter;
1576
Eric Dumazet80f8f102011-01-18 07:46:52 +00001577 rcu_read_lock();
1578 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001579 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +00001580 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001581 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
David S. Millerdbcb5852007-01-24 15:21:02 -08001583 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00001587 * This function makes lazy skb cloning in hope that most of packets
1588 * are discarded by BPF.
1589 *
1590 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1591 * and skb->cb are mangled. It works because (and until) packets
1592 * falling here are owned by current CPU. Output packets are cloned
1593 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1594 * sequencially, so that if we return skb to original state on exit,
1595 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
1597
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001598static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1599 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct sock *sk;
1602 struct sockaddr_ll *sll;
1603 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001604 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001606 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 if (skb->pkt_type == PACKET_LOOPBACK)
1609 goto drop;
1610
1611 sk = pt->af_packet_priv;
1612 po = pkt_sk(sk);
1613
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001614 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001615 goto drop;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 skb->dev = dev;
1618
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001619 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001621 * exported to higher levels.
1622 *
1623 * Otherwise, the device hides details of its frame
1624 * structure, so that corresponding packet head is
1625 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
1627 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001628 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 else if (skb->pkt_type == PACKET_OUTGOING) {
1630 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001631 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 }
1634
1635 snaplen = skb->len;
1636
David S. Millerdbcb5852007-01-24 15:21:02 -08001637 res = run_filter(skb, sk, snaplen);
1638 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001639 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001640 if (snaplen > res)
1641 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001643 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto drop_n_acct;
1645
1646 if (skb_shared(skb)) {
1647 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1648 if (nskb == NULL)
1649 goto drop_n_acct;
1650
1651 if (skb_head != skb->data) {
1652 skb->data = skb_head;
1653 skb->len = skb_len;
1654 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00001655 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 skb = nskb;
1657 }
1658
Herbert Xuffbc6112007-02-04 23:33:10 -08001659 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
1660 sizeof(skb->cb));
1661
1662 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 sll->sll_family = AF_PACKET;
1664 sll->sll_hatype = dev->type;
1665 sll->sll_protocol = skb->protocol;
1666 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001667 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001668 sll->sll_ifindex = orig_dev->ifindex;
1669 else
1670 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001672 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Herbert Xuffbc6112007-02-04 23:33:10 -08001674 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (pskb_trim(skb, snaplen))
1677 goto drop_n_acct;
1678
1679 skb_set_owner_r(skb, sk);
1680 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00001681 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Phil Oester84531c22005-07-12 11:57:52 -07001683 /* drop conntrack reference */
1684 nf_reset(skb);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 spin_lock(&sk->sk_receive_queue.lock);
1687 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -07001688 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 __skb_queue_tail(&sk->sk_receive_queue, skb);
1690 spin_unlock(&sk->sk_receive_queue.lock);
1691 sk->sk_data_ready(sk, skb->len);
1692 return 0;
1693
1694drop_n_acct:
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00001695 spin_lock(&sk->sk_receive_queue.lock);
1696 po->stats.tp_drops++;
1697 atomic_inc(&sk->sk_drops);
1698 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700drop_n_restore:
1701 if (skb_head != skb->data && skb_shared(skb)) {
1702 skb->data = skb_head;
1703 skb->len = skb_len;
1704 }
1705drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +00001706 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return 0;
1708}
1709
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001710static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
1711 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct sock *sk;
1714 struct packet_sock *po;
1715 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00001716 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001717 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001719 unsigned int snaplen, res;
chetan lokef6fb8f12011-08-19 10:18:16 +00001720 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001721 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001723 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (skb->pkt_type == PACKET_LOOPBACK)
1726 goto drop;
1727
1728 sk = pt->af_packet_priv;
1729 po = pkt_sk(sk);
1730
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001731 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001732 goto drop;
1733
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001734 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001736 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 else if (skb->pkt_type == PACKET_OUTGOING) {
1738 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001739 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 }
1742
Herbert Xu8dc41942007-02-04 23:31:32 -08001743 if (skb->ip_summed == CHECKSUM_PARTIAL)
1744 status |= TP_STATUS_CSUMNOTREADY;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 snaplen = skb->len;
1747
David S. Millerdbcb5852007-01-24 15:21:02 -08001748 res = run_filter(skb, sk, snaplen);
1749 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001750 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001751 if (snaplen > res)
1752 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07001755 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
1756 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00001758 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001759 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07001760 (maclen < 16 ? 16 : maclen)) +
1761 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 macoff = netoff - maclen;
1763 }
chetan lokef6fb8f12011-08-19 10:18:16 +00001764 if (po->tp_version <= TPACKET_V2) {
1765 if (macoff + snaplen > po->rx_ring.frame_size) {
1766 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001767 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001768 if (skb_shared(skb)) {
1769 copy_skb = skb_clone(skb, GFP_ATOMIC);
1770 } else {
1771 copy_skb = skb_get(skb);
1772 skb_head = skb->data;
1773 }
1774 if (copy_skb)
1775 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
chetan lokef6fb8f12011-08-19 10:18:16 +00001777 snaplen = po->rx_ring.frame_size - macoff;
1778 if ((int)snaplen < 0)
1779 snaplen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00001783 h.raw = packet_current_rx_frame(po, skb,
1784 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001785 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto ring_is_full;
chetan lokef6fb8f12011-08-19 10:18:16 +00001787 if (po->tp_version <= TPACKET_V2) {
1788 packet_increment_rx_head(po, &po->rx_ring);
1789 /*
1790 * LOSING will be reported till you read the stats,
1791 * because it's COR - Clear On Read.
1792 * Anyways, moving it for V1/V2 only as V3 doesn't need this
1793 * at packet level.
1794 */
1795 if (po->stats.tp_drops)
1796 status |= TP_STATUS_LOSING;
1797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 po->stats.tp_packets++;
1799 if (copy_skb) {
1800 status |= TP_STATUS_COPY;
1801 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 spin_unlock(&sk->sk_receive_queue.lock);
1804
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001805 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmann7a513842013-04-23 00:39:29 +00001806 if (!tpacket_get_timestamp(skb, &ts, po->tp_tstamp))
1807 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001809 switch (po->tp_version) {
1810 case TPACKET_V1:
1811 h.h1->tp_len = skb->len;
1812 h.h1->tp_snaplen = snaplen;
1813 h.h1->tp_mac = macoff;
1814 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00001815 h.h1->tp_sec = ts.tv_sec;
1816 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001817 hdrlen = sizeof(*h.h1);
1818 break;
1819 case TPACKET_V2:
1820 h.h2->tp_len = skb->len;
1821 h.h2->tp_snaplen = snaplen;
1822 h.h2->tp_mac = macoff;
1823 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001824 h.h2->tp_sec = ts.tv_sec;
1825 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +00001826 if (vlan_tx_tag_present(skb)) {
1827 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
1828 status |= TP_STATUS_VLAN_VALID;
1829 } else {
1830 h.h2->tp_vlan_tci = 0;
1831 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001832 h.h2->tp_padding = 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001833 hdrlen = sizeof(*h.h2);
1834 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00001835 case TPACKET_V3:
1836 /* tp_nxt_offset,vlan are already populated above.
1837 * So DONT clear those fields here
1838 */
1839 h.h3->tp_status |= status;
1840 h.h3->tp_len = skb->len;
1841 h.h3->tp_snaplen = snaplen;
1842 h.h3->tp_mac = macoff;
1843 h.h3->tp_net = netoff;
chetan lokef6fb8f12011-08-19 10:18:16 +00001844 h.h3->tp_sec = ts.tv_sec;
1845 h.h3->tp_nsec = ts.tv_nsec;
1846 hdrlen = sizeof(*h.h3);
1847 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001848 default:
1849 BUG();
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001852 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001853 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 sll->sll_family = AF_PACKET;
1855 sll->sll_hatype = dev->type;
1856 sll->sll_protocol = skb->protocol;
1857 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001858 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001859 sll->sll_ifindex = orig_dev->ifindex;
1860 else
1861 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Ralf Baechlee16aa202006-12-07 00:11:33 -08001863 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +00001864#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 {
Changli Gao0af55bb2010-12-01 02:52:20 +00001866 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
chetan lokef6fb8f12011-08-19 10:18:16 +00001868 if (po->tp_version <= TPACKET_V2) {
1869 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw
1870 + macoff + snaplen);
1871 for (start = h.raw; start < end; start += PAGE_SIZE)
1872 flush_dcache_page(pgv_to_page(start));
1873 }
Chetan Lokecc9f01b2011-07-14 08:36:33 -07001874 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
Changli Gaof6dafa92010-12-07 04:26:16 +00001876#endif
chetan lokef6fb8f12011-08-19 10:18:16 +00001877 if (po->tp_version <= TPACKET_V2)
1878 __packet_set_status(po, h.raw, status);
1879 else
1880 prb_clear_blk_fill_status(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 sk->sk_data_ready(sk, 0);
1883
1884drop_n_restore:
1885 if (skb_head != skb->data && skb_shared(skb)) {
1886 skb->data = skb_head;
1887 skb->len = skb_len;
1888 }
1889drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001890 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892
1893ring_is_full:
1894 po->stats.tp_drops++;
1895 spin_unlock(&sk->sk_receive_queue.lock);
1896
1897 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +00001898 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 goto drop_n_restore;
1900}
1901
Johann Baudy69e3c752009-05-18 22:11:22 -07001902static void tpacket_destruct_skb(struct sk_buff *skb)
1903{
1904 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001905 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -07001906
Johann Baudy69e3c752009-05-18 22:11:22 -07001907 if (likely(po->tx_ring.pg_vec)) {
1908 ph = skb_shinfo(skb)->destructor_arg;
Johann Baudy69e3c752009-05-18 22:11:22 -07001909 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
1910 atomic_dec(&po->tx_ring.pending);
Daniel Borkmann7a513842013-04-23 00:39:29 +00001911 __packet_set_timestamp(po, ph, skb);
Johann Baudy69e3c752009-05-18 22:11:22 -07001912 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
1913 }
1914
1915 sock_wfree(skb);
1916}
1917
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001918static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
1919 void *frame, struct net_device *dev, int size_max,
Herbert Xuae641942011-11-18 02:20:04 +00001920 __be16 proto, unsigned char *addr, int hlen)
Johann Baudy69e3c752009-05-18 22:11:22 -07001921{
Daniel Borkmann184f4892013-04-16 01:57:46 +00001922 union tpacket_uhdr ph;
Johann Baudy69e3c752009-05-18 22:11:22 -07001923 int to_write, offset, len, tp_len, nr_frags, len_max;
1924 struct socket *sock = po->sk.sk_socket;
1925 struct page *page;
1926 void *data;
1927 int err;
1928
1929 ph.raw = frame;
1930
1931 skb->protocol = proto;
1932 skb->dev = dev;
1933 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001934 skb->mark = po->sk.sk_mark;
Willem de Bruijn2e313962013-04-23 00:39:28 +00001935 sock_tx_timestamp(&po->sk, &skb_shinfo(skb)->tx_flags);
Johann Baudy69e3c752009-05-18 22:11:22 -07001936 skb_shinfo(skb)->destructor_arg = ph.raw;
1937
1938 switch (po->tp_version) {
1939 case TPACKET_V2:
1940 tp_len = ph.h2->tp_len;
1941 break;
1942 default:
1943 tp_len = ph.h1->tp_len;
1944 break;
1945 }
1946 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001947 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -07001948 return -EMSGSIZE;
1949 }
1950
Herbert Xuae641942011-11-18 02:20:04 +00001951 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07001952 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001953 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001954
Paul Chavent5920cd3a2012-11-06 23:10:47 +00001955 if (po->tp_tx_has_off) {
1956 int off_min, off_max, off;
1957 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
1958 off_max = po->tx_ring.frame_size - tp_len;
1959 if (sock->type == SOCK_DGRAM) {
1960 switch (po->tp_version) {
1961 case TPACKET_V2:
1962 off = ph.h2->tp_net;
1963 break;
1964 default:
1965 off = ph.h1->tp_net;
1966 break;
1967 }
1968 } else {
1969 switch (po->tp_version) {
1970 case TPACKET_V2:
1971 off = ph.h2->tp_mac;
1972 break;
1973 default:
1974 off = ph.h1->tp_mac;
1975 break;
1976 }
1977 }
1978 if (unlikely((off < off_min) || (off_max < off)))
1979 return -EINVAL;
1980 data = ph.raw + off;
1981 } else {
1982 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
1983 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001984 to_write = tp_len;
1985
1986 if (sock->type == SOCK_DGRAM) {
1987 err = dev_hard_header(skb, dev, ntohs(proto), addr,
1988 NULL, tp_len);
1989 if (unlikely(err < 0))
1990 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001991 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -07001992 /* net device doesn't like empty head */
1993 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001994 pr_err("packet size is too short (%d < %d)\n",
1995 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001996 return -EINVAL;
1997 }
1998
1999 skb_push(skb, dev->hard_header_len);
2000 err = skb_store_bits(skb, 0, data,
2001 dev->hard_header_len);
2002 if (unlikely(err))
2003 return err;
2004
2005 data += dev->hard_header_len;
2006 to_write -= dev->hard_header_len;
2007 }
2008
Johann Baudy69e3c752009-05-18 22:11:22 -07002009 offset = offset_in_page(data);
2010 len_max = PAGE_SIZE - offset;
2011 len = ((to_write > len_max) ? len_max : to_write);
2012
2013 skb->data_len = to_write;
2014 skb->len += to_write;
2015 skb->truesize += to_write;
2016 atomic_add(to_write, &po->sk.sk_wmem_alloc);
2017
2018 while (likely(to_write)) {
2019 nr_frags = skb_shinfo(skb)->nr_frags;
2020
2021 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002022 pr_err("Packet exceed the number of skb frags(%lu)\n",
2023 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002024 return -EFAULT;
2025 }
2026
Changli Gao0af55bb2010-12-01 02:52:20 +00002027 page = pgv_to_page(data);
2028 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002029 flush_dcache_page(page);
2030 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002031 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002032 to_write -= len;
2033 offset = 0;
2034 len_max = PAGE_SIZE;
2035 len = ((to_write > len_max) ? len_max : to_write);
2036 }
2037
2038 return tp_len;
2039}
2040
2041static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2042{
Johann Baudy69e3c752009-05-18 22:11:22 -07002043 struct sk_buff *skb;
2044 struct net_device *dev;
2045 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00002046 bool need_rls_dev = false;
2047 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002048 void *ph;
2049 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -07002050 int tp_len, size_max;
2051 unsigned char *addr;
2052 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002053 int status = TP_STATUS_AVAILABLE;
Herbert Xuae641942011-11-18 02:20:04 +00002054 int hlen, tlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002055
Johann Baudy69e3c752009-05-18 22:11:22 -07002056 mutex_lock(&po->pg_vec_lock);
2057
Johann Baudy69e3c752009-05-18 22:11:22 -07002058 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00002059 dev = po->prot_hook.dev;
Johann Baudy69e3c752009-05-18 22:11:22 -07002060 proto = po->num;
2061 addr = NULL;
2062 } else {
2063 err = -EINVAL;
2064 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2065 goto out;
2066 if (msg->msg_namelen < (saddr->sll_halen
2067 + offsetof(struct sockaddr_ll,
2068 sll_addr)))
2069 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002070 proto = saddr->sll_protocol;
2071 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002072 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
2073 need_rls_dev = true;
Johann Baudy69e3c752009-05-18 22:11:22 -07002074 }
2075
Johann Baudy69e3c752009-05-18 22:11:22 -07002076 err = -ENXIO;
2077 if (unlikely(dev == NULL))
2078 goto out;
2079
2080 reserve = dev->hard_header_len;
2081
2082 err = -ENETDOWN;
2083 if (unlikely(!(dev->flags & IFF_UP)))
2084 goto out_put;
2085
2086 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002087 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002088
2089 if (size_max > dev->mtu + reserve)
2090 size_max = dev->mtu + reserve;
2091
2092 do {
2093 ph = packet_current_frame(po, &po->tx_ring,
2094 TP_STATUS_SEND_REQUEST);
2095
2096 if (unlikely(ph == NULL)) {
2097 schedule();
2098 continue;
2099 }
2100
2101 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002102 hlen = LL_RESERVED_SPACE(dev);
2103 tlen = dev->needed_tailroom;
Johann Baudy69e3c752009-05-18 22:11:22 -07002104 skb = sock_alloc_send_skb(&po->sk,
Herbert Xuae641942011-11-18 02:20:04 +00002105 hlen + tlen + sizeof(struct sockaddr_ll),
Johann Baudy69e3c752009-05-18 22:11:22 -07002106 0, &err);
2107
2108 if (unlikely(skb == NULL))
2109 goto out_status;
2110
2111 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
Herbert Xuae641942011-11-18 02:20:04 +00002112 addr, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002113
2114 if (unlikely(tp_len < 0)) {
2115 if (po->tp_loss) {
2116 __packet_set_status(po, ph,
2117 TP_STATUS_AVAILABLE);
2118 packet_increment_head(&po->tx_ring);
2119 kfree_skb(skb);
2120 continue;
2121 } else {
2122 status = TP_STATUS_WRONG_FORMAT;
2123 err = tp_len;
2124 goto out_status;
2125 }
2126 }
2127
2128 skb->destructor = tpacket_destruct_skb;
2129 __packet_set_status(po, ph, TP_STATUS_SENDING);
2130 atomic_inc(&po->tx_ring.pending);
2131
2132 status = TP_STATUS_SEND_REQUEST;
2133 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002134 if (unlikely(err > 0)) {
2135 err = net_xmit_errno(err);
2136 if (err && __packet_get_status(po, ph) ==
2137 TP_STATUS_AVAILABLE) {
2138 /* skb was destructed already */
2139 skb = NULL;
2140 goto out_status;
2141 }
2142 /*
2143 * skb was dropped but not destructed yet;
2144 * let's treat it like congestion or err < 0
2145 */
2146 err = 0;
2147 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002148 packet_increment_head(&po->tx_ring);
2149 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08002150 } while (likely((ph != NULL) ||
2151 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
2152 (atomic_read(&po->tx_ring.pending))))
2153 );
Johann Baudy69e3c752009-05-18 22:11:22 -07002154
2155 err = len_sum;
2156 goto out_put;
2157
Johann Baudy69e3c752009-05-18 22:11:22 -07002158out_status:
2159 __packet_set_status(po, ph, status);
2160 kfree_skb(skb);
2161out_put:
Ben Greear827d9782011-06-01 07:18:53 +00002162 if (need_rls_dev)
2163 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002164out:
2165 mutex_unlock(&po->pg_vec_lock);
2166 return err;
2167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002169static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2170 size_t reserve, size_t len,
2171 size_t linear, int noblock,
2172 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002173{
2174 struct sk_buff *skb;
2175
2176 /* Under a page? Don't bother with paged skb. */
2177 if (prepad + len < PAGE_SIZE || !linear)
2178 linear = len;
2179
2180 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
2181 err);
2182 if (!skb)
2183 return NULL;
2184
2185 skb_reserve(skb, reserve);
2186 skb_put(skb, linear);
2187 skb->data_len = len - linear;
2188 skb->len += len - linear;
2189
2190 return skb;
2191}
2192
Johann Baudy69e3c752009-05-18 22:11:22 -07002193static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct msghdr *msg, size_t len)
2195{
2196 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002197 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 struct sk_buff *skb;
2199 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002200 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00002201 bool need_rls_dev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00002203 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002204 struct virtio_net_hdr vnet_hdr = { 0 };
2205 int offset = 0;
2206 int vnet_hdr_len;
2207 struct packet_sock *po = pkt_sk(sk);
2208 unsigned short gso_type = 0;
Herbert Xuae641942011-11-18 02:20:04 +00002209 int hlen, tlen;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002210 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002213 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00002217 dev = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 proto = po->num;
2219 addr = NULL;
2220 } else {
2221 err = -EINVAL;
2222 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2223 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002224 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 proto = saddr->sll_protocol;
2227 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002228 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
2229 need_rls_dev = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 err = -ENXIO;
2233 if (dev == NULL)
2234 goto out_unlock;
2235 if (sock->type == SOCK_RAW)
2236 reserve = dev->hard_header_len;
2237
David S. Millerd5e76b02007-01-25 19:30:36 -08002238 err = -ENETDOWN;
2239 if (!(dev->flags & IFF_UP))
2240 goto out_unlock;
2241
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002242 if (po->has_vnet_hdr) {
2243 vnet_hdr_len = sizeof(vnet_hdr);
2244
2245 err = -EINVAL;
2246 if (len < vnet_hdr_len)
2247 goto out_unlock;
2248
2249 len -= vnet_hdr_len;
2250
2251 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
2252 vnet_hdr_len);
2253 if (err < 0)
2254 goto out_unlock;
2255
2256 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2257 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
2258 vnet_hdr.hdr_len))
2259 vnet_hdr.hdr_len = vnet_hdr.csum_start +
2260 vnet_hdr.csum_offset + 2;
2261
2262 err = -EINVAL;
2263 if (vnet_hdr.hdr_len > len)
2264 goto out_unlock;
2265
2266 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2267 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2268 case VIRTIO_NET_HDR_GSO_TCPV4:
2269 gso_type = SKB_GSO_TCPV4;
2270 break;
2271 case VIRTIO_NET_HDR_GSO_TCPV6:
2272 gso_type = SKB_GSO_TCPV6;
2273 break;
2274 case VIRTIO_NET_HDR_GSO_UDP:
2275 gso_type = SKB_GSO_UDP;
2276 break;
2277 default:
2278 goto out_unlock;
2279 }
2280
2281 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
2282 gso_type |= SKB_GSO_TCP_ECN;
2283
2284 if (vnet_hdr.gso_size == 0)
2285 goto out_unlock;
2286
2287 }
2288 }
2289
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002290 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2291 if (!netif_supports_nofcs(dev)) {
2292 err = -EPROTONOSUPPORT;
2293 goto out_unlock;
2294 }
2295 extra_len = 4; /* We're doing our own CRC */
2296 }
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002299 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto out_unlock;
2301
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002302 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002303 hlen = LL_RESERVED_SPACE(dev);
2304 tlen = dev->needed_tailroom;
2305 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, vnet_hdr.hdr_len,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002306 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002307 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 goto out_unlock;
2309
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002310 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002312 err = -EINVAL;
2313 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002314 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002315 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002318 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (err)
2320 goto out_free;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002321
2322 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002324 if (!gso_type && (len > dev->mtu + reserve + extra_len)) {
Ben Greear57f89bf2011-02-11 09:35:18 +00002325 /* Earlier code assumed this would be a VLAN pkt,
2326 * double-check this now that we have the actual
2327 * packet in hand.
2328 */
2329 struct ethhdr *ehdr;
2330 skb_reset_mac_header(skb);
2331 ehdr = eth_hdr(skb);
2332 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
2333 err = -EMSGSIZE;
2334 goto out_free;
2335 }
2336 }
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 skb->protocol = proto;
2339 skb->dev = dev;
2340 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002341 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002343 if (po->has_vnet_hdr) {
2344 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2345 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
2346 vnet_hdr.csum_offset)) {
2347 err = -EINVAL;
2348 goto out_free;
2349 }
2350 }
2351
2352 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
2353 skb_shinfo(skb)->gso_type = gso_type;
2354
2355 /* Header must be checked, and gso_segs computed. */
2356 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2357 skb_shinfo(skb)->gso_segs = 0;
2358
2359 len += vnet_hdr_len;
2360 }
2361
Jason Wang40893fd2013-03-26 23:11:22 +00002362 skb_probe_transport_header(skb, reserve);
Jason Wangc1aad272013-03-25 20:19:57 +00002363
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002364 if (unlikely(extra_len == 4))
2365 skb->no_fcs = 1;
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /*
2368 * Now send it
2369 */
2370
2371 err = dev_queue_xmit(skb);
2372 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2373 goto out_unlock;
2374
Ben Greear827d9782011-06-01 07:18:53 +00002375 if (need_rls_dev)
2376 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002378 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380out_free:
2381 kfree_skb(skb);
2382out_unlock:
Ben Greear827d9782011-06-01 07:18:53 +00002383 if (dev && need_rls_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 dev_put(dev);
2385out:
2386 return err;
2387}
2388
Johann Baudy69e3c752009-05-18 22:11:22 -07002389static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
2390 struct msghdr *msg, size_t len)
2391{
Johann Baudy69e3c752009-05-18 22:11:22 -07002392 struct sock *sk = sock->sk;
2393 struct packet_sock *po = pkt_sk(sk);
2394 if (po->tx_ring.pg_vec)
2395 return tpacket_snd(po, msg);
2396 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002397 return packet_snd(sock, msg, len);
2398}
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400/*
2401 * Close a PACKET socket. This is fairly simple. We immediately go
2402 * to 'closed' state and remove our protocol entry in the device list.
2403 */
2404
2405static int packet_release(struct socket *sock)
2406{
2407 struct sock *sk = sock->sk;
2408 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002409 struct net *net;
chetan lokef6fb8f12011-08-19 10:18:16 +00002410 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 if (!sk)
2413 return 0;
2414
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002415 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 po = pkt_sk(sk);
2417
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002418 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002419 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002420 mutex_unlock(&net->packet.sklist_lock);
2421
2422 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08002423 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002424 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
stephen hemminger808f5112010-02-22 07:57:18 +00002426 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002427 unregister_prot_hook(sk, false);
Ben Greear160ff182011-06-01 07:18:52 +00002428 if (po->prot_hook.dev) {
2429 dev_put(po->prot_hook.dev);
2430 po->prot_hook.dev = NULL;
2431 }
stephen hemminger808f5112010-02-22 07:57:18 +00002432 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Phil Sutter9665d5d2013-02-01 07:21:41 +00002436 if (po->rx_ring.pg_vec) {
2437 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00002438 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002439 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002440
Phil Sutter9665d5d2013-02-01 07:21:41 +00002441 if (po->tx_ring.pg_vec) {
2442 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00002443 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
David S. Millerdc99f602011-07-05 01:45:05 -07002446 fanout_release(sk);
2447
stephen hemminger808f5112010-02-22 07:57:18 +00002448 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /*
2450 * Now the socket is dead. No more input will appear.
2451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 sock_orphan(sk);
2453 sock->sk = NULL;
2454
2455 /* Purge queues */
2456
2457 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08002458 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 sock_put(sk);
2461 return 0;
2462}
2463
2464/*
2465 * Attach a packet hook.
2466 */
2467
Al Viro0e11c912006-11-08 00:26:29 -08002468static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 struct packet_sock *po = pkt_sk(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07002471
Wei Yongjunaef950b2011-12-27 22:32:41 -05002472 if (po->fanout) {
2473 if (dev)
2474 dev_put(dev);
2475
David S. Millerdc99f602011-07-05 01:45:05 -07002476 return -EINVAL;
Wei Yongjunaef950b2011-12-27 22:32:41 -05002477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 lock_sock(sk);
2480
2481 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002482 unregister_prot_hook(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 po->num = protocol;
2484 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00002485 if (po->prot_hook.dev)
2486 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 po->prot_hook.dev = dev;
2488
2489 po->ifindex = dev ? dev->ifindex : 0;
2490
2491 if (protocol == 0)
2492 goto out_unlock;
2493
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002494 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07002495 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002496 } else {
2497 sk->sk_err = ENETDOWN;
2498 if (!sock_flag(sk, SOCK_DEAD))
2499 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
2501
2502out_unlock:
2503 spin_unlock(&po->bind_lock);
2504 release_sock(sk);
2505 return 0;
2506}
2507
2508/*
2509 * Bind a packet socket to a device
2510 */
2511
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002512static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
2513 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002515 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 char name[15];
2517 struct net_device *dev;
2518 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /*
2521 * Check legality
2522 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002523
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002524 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002526 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002528 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00002529 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return err;
2532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2535{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002536 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
2537 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct net_device *dev = NULL;
2539 int err;
2540
2541
2542 /*
2543 * Check legality
2544 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (addr_len < sizeof(struct sockaddr_ll))
2547 return -EINVAL;
2548 if (sll->sll_family != AF_PACKET)
2549 return -EINVAL;
2550
2551 if (sll->sll_ifindex) {
2552 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002553 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (dev == NULL)
2555 goto out;
2556 }
2557 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559out:
2560 return err;
2561}
2562
2563static struct proto packet_proto = {
2564 .name = "PACKET",
2565 .owner = THIS_MODULE,
2566 .obj_size = sizeof(struct packet_sock),
2567};
2568
2569/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002570 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 */
2572
Eric Paris3f378b62009-11-05 22:18:14 -08002573static int packet_create(struct net *net, struct socket *sock, int protocol,
2574 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
2576 struct sock *sk;
2577 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08002578 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 int err;
2580
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002581 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07002583 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
2584 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return -ESOCKTNOSUPPORT;
2586
2587 sock->state = SS_UNCONNECTED;
2588
2589 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07002590 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (sk == NULL)
2592 goto out;
2593
2594 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (sock->type == SOCK_PACKET)
2596 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 sock_init_data(sock, sk);
2599
2600 po = pkt_sk(sk);
2601 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08002602 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08002605 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 /*
2608 * Attach a protocol block
2609 */
2610
2611 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08002612 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (sock->type == SOCK_PACKET)
2616 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 po->prot_hook.af_packet_priv = sk;
2619
Al Viro0e11c912006-11-08 00:26:29 -08002620 if (proto) {
2621 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07002622 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002625 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002626 sk_add_node_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002627 mutex_unlock(&net->packet.sklist_lock);
2628
2629 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08002630 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002631 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00002632
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634out:
2635 return err;
2636}
2637
Richard Cochraned85b562010-04-07 22:41:28 +00002638static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
2639{
2640 struct sock_exterr_skb *serr;
2641 struct sk_buff *skb, *skb2;
2642 int copied, err;
2643
2644 err = -EAGAIN;
2645 skb = skb_dequeue(&sk->sk_error_queue);
2646 if (skb == NULL)
2647 goto out;
2648
2649 copied = skb->len;
2650 if (copied > len) {
2651 msg->msg_flags |= MSG_TRUNC;
2652 copied = len;
2653 }
2654 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2655 if (err)
2656 goto out_free_skb;
2657
2658 sock_recv_timestamp(msg, sk, skb);
2659
2660 serr = SKB_EXT_ERR(skb);
2661 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
2662 sizeof(serr->ee), &serr->ee);
2663
2664 msg->msg_flags |= MSG_ERRQUEUE;
2665 err = copied;
2666
2667 /* Reset and regenerate socket error */
2668 spin_lock_bh(&sk->sk_error_queue.lock);
2669 sk->sk_err = 0;
2670 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
2671 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
2672 spin_unlock_bh(&sk->sk_error_queue.lock);
2673 sk->sk_error_report(sk);
2674 } else
2675 spin_unlock_bh(&sk->sk_error_queue.lock);
2676
2677out_free_skb:
2678 kfree_skb(skb);
2679out:
2680 return err;
2681}
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
2684 * Pull a packet from our receive queue and hand it to the user.
2685 * If necessary we block.
2686 */
2687
2688static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2689 struct msghdr *msg, size_t len, int flags)
2690{
2691 struct sock *sk = sock->sk;
2692 struct sk_buff *skb;
2693 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002694 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002695 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00002698 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 goto out;
2700
2701#if 0
2702 /* What error should we return now? EUNATTACH? */
2703 if (pkt_sk(sk)->ifindex < 0)
2704 return -ENODEV;
2705#endif
2706
Richard Cochraned85b562010-04-07 22:41:28 +00002707 if (flags & MSG_ERRQUEUE) {
2708 err = packet_recv_error(sk, msg, len);
2709 goto out;
2710 }
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 * Call the generic datagram receiver. This handles all sorts
2714 * of horrible races and re-entrancy so we can forget about it
2715 * in the protocol layers.
2716 *
2717 * Now it will return ENETDOWN, if device have just gone down,
2718 * but then it will block.
2719 */
2720
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002721 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002724 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 * handles the blocking we don't see and worry about blocking
2726 * retries.
2727 */
2728
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002729 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 goto out;
2731
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002732 if (pkt_sk(sk)->has_vnet_hdr) {
2733 struct virtio_net_hdr vnet_hdr = { 0 };
2734
2735 err = -EINVAL;
2736 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00002737 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002738 goto out_free;
2739
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00002740 len -= vnet_hdr_len;
2741
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002742 if (skb_is_gso(skb)) {
2743 struct skb_shared_info *sinfo = skb_shinfo(skb);
2744
2745 /* This is a hint as to how much should be linear. */
2746 vnet_hdr.hdr_len = skb_headlen(skb);
2747 vnet_hdr.gso_size = sinfo->gso_size;
2748 if (sinfo->gso_type & SKB_GSO_TCPV4)
2749 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2750 else if (sinfo->gso_type & SKB_GSO_TCPV6)
2751 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2752 else if (sinfo->gso_type & SKB_GSO_UDP)
2753 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
2754 else if (sinfo->gso_type & SKB_GSO_FCOE)
2755 goto out_free;
2756 else
2757 BUG();
2758 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
2759 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2760 } else
2761 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
2762
2763 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2764 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00002765 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002766 vnet_hdr.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00002767 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
2768 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002769 } /* else everything is zero */
2770
2771 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
2772 vnet_hdr_len);
2773 if (err < 0)
2774 goto out_free;
2775 }
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002778 * If the address length field is there to be filled in, we fill
2779 * it in now.
2780 */
2781
Herbert Xuffbc6112007-02-04 23:33:10 -08002782 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002783 if (sock->type == SOCK_PACKET)
2784 msg->msg_namelen = sizeof(struct sockaddr_pkt);
2785 else
2786 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
2787
2788 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 * You lose any data beyond the buffer you gave. If it worries a
2790 * user program they can ask the device for its MTU anyway.
2791 */
2792
2793 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002794 if (copied > len) {
2795 copied = len;
2796 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798
2799 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2800 if (err)
2801 goto out_free;
2802
Neil Horman3b885782009-10-12 13:26:31 -07002803 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08002806 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
2807 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Herbert Xu8dc41942007-02-04 23:31:32 -08002809 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08002810 struct tpacket_auxdata aux;
2811
2812 aux.tp_status = TP_STATUS_USER;
2813 if (skb->ip_summed == CHECKSUM_PARTIAL)
2814 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
2815 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
2816 aux.tp_snaplen = skb->len;
2817 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002818 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00002819 if (vlan_tx_tag_present(skb)) {
2820 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
2821 aux.tp_status |= TP_STATUS_VLAN_VALID;
2822 } else {
2823 aux.tp_vlan_tci = 0;
2824 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07002825 aux.tp_padding = 0;
Herbert Xuffbc6112007-02-04 23:33:10 -08002826 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08002827 }
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /*
2830 * Free or return the buffer as appropriate. Again this
2831 * hides all the races and re-entrancy issues from us.
2832 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002833 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835out_free:
2836 skb_free_datagram(sk, skb);
2837out:
2838 return err;
2839}
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
2842 int *uaddr_len, int peer)
2843{
2844 struct net_device *dev;
2845 struct sock *sk = sock->sk;
2846
2847 if (peer)
2848 return -EOPNOTSUPP;
2849
2850 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002851 rcu_read_lock();
2852 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
2853 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002854 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002855 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002857 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 *uaddr_len = sizeof(*uaddr);
2859
2860 return 0;
2861}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
2864 int *uaddr_len, int peer)
2865{
2866 struct net_device *dev;
2867 struct sock *sk = sock->sk;
2868 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00002869 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 if (peer)
2872 return -EOPNOTSUPP;
2873
2874 sll->sll_family = AF_PACKET;
2875 sll->sll_ifindex = po->ifindex;
2876 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002877 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002878 rcu_read_lock();
2879 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (dev) {
2881 sll->sll_hatype = dev->type;
2882 sll->sll_halen = dev->addr_len;
2883 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 } else {
2885 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
2886 sll->sll_halen = 0;
2887 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01002888 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002889 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 return 0;
2892}
2893
Wang Chen2aeb0b82008-07-14 20:49:46 -07002894static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
2895 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
2897 switch (i->type) {
2898 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002899 if (i->alen != dev->addr_len)
2900 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00002902 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00002904 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002907 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 break;
2909 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002910 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002912 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002913 if (i->alen != dev->addr_len)
2914 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002915 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002916 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002917 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002918 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002919 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002920 default:
2921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07002923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
2926static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
2927{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002928 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (i->ifindex == dev->ifindex)
2930 packet_dev_mc(dev, i, what);
2931 }
2932}
2933
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002934static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 struct packet_sock *po = pkt_sk(sk);
2937 struct packet_mclist *ml, *i;
2938 struct net_device *dev;
2939 int err;
2940
2941 rtnl_lock();
2942
2943 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002944 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (!dev)
2946 goto done;
2947
2948 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00002949 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto done;
2951
2952 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002953 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (i == NULL)
2955 goto done;
2956
2957 err = 0;
2958 for (ml = po->mclist; ml; ml = ml->next) {
2959 if (ml->ifindex == mreq->mr_ifindex &&
2960 ml->type == mreq->mr_type &&
2961 ml->alen == mreq->mr_alen &&
2962 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2963 ml->count++;
2964 /* Free the new element ... */
2965 kfree(i);
2966 goto done;
2967 }
2968 }
2969
2970 i->type = mreq->mr_type;
2971 i->ifindex = mreq->mr_ifindex;
2972 i->alen = mreq->mr_alen;
2973 memcpy(i->addr, mreq->mr_address, i->alen);
2974 i->count = 1;
2975 i->next = po->mclist;
2976 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07002977 err = packet_dev_mc(dev, i, 1);
2978 if (err) {
2979 po->mclist = i->next;
2980 kfree(i);
2981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983done:
2984 rtnl_unlock();
2985 return err;
2986}
2987
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002988static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
2990 struct packet_mclist *ml, **mlp;
2991
2992 rtnl_lock();
2993
2994 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
2995 if (ml->ifindex == mreq->mr_ifindex &&
2996 ml->type == mreq->mr_type &&
2997 ml->alen == mreq->mr_alen &&
2998 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2999 if (--ml->count == 0) {
3000 struct net_device *dev;
3001 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003002 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3003 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 kfree(ml);
3006 }
3007 rtnl_unlock();
3008 return 0;
3009 }
3010 }
3011 rtnl_unlock();
3012 return -EADDRNOTAVAIL;
3013}
3014
3015static void packet_flush_mclist(struct sock *sk)
3016{
3017 struct packet_sock *po = pkt_sk(sk);
3018 struct packet_mclist *ml;
3019
3020 if (!po->mclist)
3021 return;
3022
3023 rtnl_lock();
3024 while ((ml = po->mclist) != NULL) {
3025 struct net_device *dev;
3026
3027 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003028 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3029 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 kfree(ml);
3032 }
3033 rtnl_unlock();
3034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036static int
David S. Millerb7058842009-09-30 16:12:20 -07003037packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003040 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 int ret;
3042
3043 if (level != SOL_PACKET)
3044 return -ENOPROTOOPT;
3045
Johann Baudy69e3c752009-05-18 22:11:22 -07003046 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003047 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 case PACKET_DROP_MEMBERSHIP:
3049 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003050 struct packet_mreq_max mreq;
3051 int len = optlen;
3052 memset(&mreq, 0, sizeof(mreq));
3053 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003055 if (len > sizeof(mreq))
3056 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003057 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003059 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (optname == PACKET_ADD_MEMBERSHIP)
3062 ret = packet_mc_add(sk, &mreq);
3063 else
3064 ret = packet_mc_drop(sk, &mreq);
3065 return ret;
3066 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003069 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 {
chetan lokef6fb8f12011-08-19 10:18:16 +00003071 union tpacket_req_u req_u;
3072 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
chetan lokef6fb8f12011-08-19 10:18:16 +00003074 switch (po->tp_version) {
3075 case TPACKET_V1:
3076 case TPACKET_V2:
3077 len = sizeof(req_u.req);
3078 break;
3079 case TPACKET_V3:
3080 default:
3081 len = sizeof(req_u.req3);
3082 break;
3083 }
3084 if (optlen < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003086 if (pkt_sk(sk)->has_vnet_hdr)
3087 return -EINVAL;
chetan lokef6fb8f12011-08-19 10:18:16 +00003088 if (copy_from_user(&req_u.req, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return -EFAULT;
chetan lokef6fb8f12011-08-19 10:18:16 +00003090 return packet_set_ring(sk, &req_u, 0,
3091 optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 case PACKET_COPY_THRESH:
3094 {
3095 int val;
3096
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003097 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003099 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return -EFAULT;
3101
3102 pkt_sk(sk)->copy_thresh = val;
3103 return 0;
3104 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003105 case PACKET_VERSION:
3106 {
3107 int val;
3108
3109 if (optlen != sizeof(val))
3110 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003111 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003112 return -EBUSY;
3113 if (copy_from_user(&val, optval, sizeof(val)))
3114 return -EFAULT;
3115 switch (val) {
3116 case TPACKET_V1:
3117 case TPACKET_V2:
chetan lokef6fb8f12011-08-19 10:18:16 +00003118 case TPACKET_V3:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003119 po->tp_version = val;
3120 return 0;
3121 default:
3122 return -EINVAL;
3123 }
3124 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003125 case PACKET_RESERVE:
3126 {
3127 unsigned int val;
3128
3129 if (optlen != sizeof(val))
3130 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003131 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07003132 return -EBUSY;
3133 if (copy_from_user(&val, optval, sizeof(val)))
3134 return -EFAULT;
3135 po->tp_reserve = val;
3136 return 0;
3137 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003138 case PACKET_LOSS:
3139 {
3140 unsigned int val;
3141
3142 if (optlen != sizeof(val))
3143 return -EINVAL;
3144 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3145 return -EBUSY;
3146 if (copy_from_user(&val, optval, sizeof(val)))
3147 return -EFAULT;
3148 po->tp_loss = !!val;
3149 return 0;
3150 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003151 case PACKET_AUXDATA:
3152 {
3153 int val;
3154
3155 if (optlen < sizeof(val))
3156 return -EINVAL;
3157 if (copy_from_user(&val, optval, sizeof(val)))
3158 return -EFAULT;
3159
3160 po->auxdata = !!val;
3161 return 0;
3162 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003163 case PACKET_ORIGDEV:
3164 {
3165 int val;
3166
3167 if (optlen < sizeof(val))
3168 return -EINVAL;
3169 if (copy_from_user(&val, optval, sizeof(val)))
3170 return -EFAULT;
3171
3172 po->origdev = !!val;
3173 return 0;
3174 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003175 case PACKET_VNET_HDR:
3176 {
3177 int val;
3178
3179 if (sock->type != SOCK_RAW)
3180 return -EINVAL;
3181 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3182 return -EBUSY;
3183 if (optlen < sizeof(val))
3184 return -EINVAL;
3185 if (copy_from_user(&val, optval, sizeof(val)))
3186 return -EFAULT;
3187
3188 po->has_vnet_hdr = !!val;
3189 return 0;
3190 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003191 case PACKET_TIMESTAMP:
3192 {
3193 int val;
3194
3195 if (optlen != sizeof(val))
3196 return -EINVAL;
3197 if (copy_from_user(&val, optval, sizeof(val)))
3198 return -EFAULT;
3199
3200 po->tp_tstamp = val;
3201 return 0;
3202 }
David S. Millerdc99f602011-07-05 01:45:05 -07003203 case PACKET_FANOUT:
3204 {
3205 int val;
3206
3207 if (optlen != sizeof(val))
3208 return -EINVAL;
3209 if (copy_from_user(&val, optval, sizeof(val)))
3210 return -EFAULT;
3211
3212 return fanout_add(sk, val & 0xffff, val >> 16);
3213 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003214 case PACKET_TX_HAS_OFF:
3215 {
3216 unsigned int val;
3217
3218 if (optlen != sizeof(val))
3219 return -EINVAL;
3220 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3221 return -EBUSY;
3222 if (copy_from_user(&val, optval, sizeof(val)))
3223 return -EFAULT;
3224 po->tp_tx_has_off = !!val;
3225 return 0;
3226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 default:
3228 return -ENOPROTOOPT;
3229 }
3230}
3231
3232static int packet_getsockopt(struct socket *sock, int level, int optname,
3233 char __user *optval, int __user *optlen)
3234{
3235 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003236 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct sock *sk = sock->sk;
3238 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003239 void *data = &val;
Herbert Xu8dc41942007-02-04 23:31:32 -08003240 struct tpacket_stats st;
chetan lokef6fb8f12011-08-19 10:18:16 +00003241 union tpacket_stats_u st_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 if (level != SOL_PACKET)
3244 return -ENOPROTOOPT;
3245
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003246 if (get_user(len, optlen))
3247 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 if (len < 0)
3250 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003251
Johann Baudy69e3c752009-05-18 22:11:22 -07003252 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 spin_lock_bh(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00003255 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003256 lv = sizeof(struct tpacket_stats_v3);
chetan lokef6fb8f12011-08-19 10:18:16 +00003257 memcpy(&st_u.stats3, &po->stats,
Eric Dumazetc06fff62012-04-19 21:56:11 +00003258 sizeof(struct tpacket_stats));
chetan lokef6fb8f12011-08-19 10:18:16 +00003259 st_u.stats3.tp_freeze_q_cnt =
Eric Dumazetc06fff62012-04-19 21:56:11 +00003260 po->stats_u.stats3.tp_freeze_q_cnt;
chetan lokef6fb8f12011-08-19 10:18:16 +00003261 st_u.stats3.tp_packets += po->stats.tp_drops;
3262 data = &st_u.stats3;
3263 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003264 lv = sizeof(struct tpacket_stats);
chetan lokef6fb8f12011-08-19 10:18:16 +00003265 st = po->stats;
3266 st.tp_packets += st.tp_drops;
3267 data = &st;
3268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 memset(&po->stats, 0, sizeof(st));
3270 spin_unlock_bh(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003272 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003273 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003274 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003275 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003276 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003277 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003278 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003279 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003280 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003281 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003282 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003283 break;
3284 case PACKET_HDRLEN:
3285 if (len > sizeof(int))
3286 len = sizeof(int);
3287 if (copy_from_user(&val, optval, len))
3288 return -EFAULT;
3289 switch (val) {
3290 case TPACKET_V1:
3291 val = sizeof(struct tpacket_hdr);
3292 break;
3293 case TPACKET_V2:
3294 val = sizeof(struct tpacket2_hdr);
3295 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00003296 case TPACKET_V3:
3297 val = sizeof(struct tpacket3_hdr);
3298 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003299 default:
3300 return -EINVAL;
3301 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003302 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003303 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003304 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003305 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003306 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003307 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003308 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003309 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003310 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003311 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003312 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003313 val = (po->fanout ?
3314 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003315 ((u32)po->fanout->type << 16) |
3316 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003317 0);
David S. Millerdc99f602011-07-05 01:45:05 -07003318 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003319 case PACKET_TX_HAS_OFF:
3320 val = po->tp_tx_has_off;
3321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 default:
3323 return -ENOPROTOOPT;
3324 }
3325
Eric Dumazetc06fff62012-04-19 21:56:11 +00003326 if (len > lv)
3327 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003328 if (put_user(len, optlen))
3329 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08003330 if (copy_to_user(optval, data, len))
3331 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
3335
3336static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
3337{
3338 struct sock *sk;
Jason Lunzad930652007-02-20 23:19:54 -08003339 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003340 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
stephen hemminger808f5112010-02-22 07:57:18 +00003342 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003343 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct packet_sock *po = pkt_sk(sk);
3345
3346 switch (msg) {
3347 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (po->mclist)
3349 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07003350 /* fallthrough */
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 case NETDEV_DOWN:
3353 if (dev->ifindex == po->ifindex) {
3354 spin_lock(&po->bind_lock);
3355 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07003356 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 sk->sk_err = ENETDOWN;
3358 if (!sock_flag(sk, SOCK_DEAD))
3359 sk->sk_error_report(sk);
3360 }
3361 if (msg == NETDEV_UNREGISTER) {
3362 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00003363 if (po->prot_hook.dev)
3364 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 po->prot_hook.dev = NULL;
3366 }
3367 spin_unlock(&po->bind_lock);
3368 }
3369 break;
3370 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00003371 if (dev->ifindex == po->ifindex) {
3372 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003373 if (po->num)
3374 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00003375 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 break;
3378 }
3379 }
stephen hemminger808f5112010-02-22 07:57:18 +00003380 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return NOTIFY_DONE;
3382}
3383
3384
3385static int packet_ioctl(struct socket *sock, unsigned int cmd,
3386 unsigned long arg)
3387{
3388 struct sock *sk = sock->sk;
3389
Johann Baudy69e3c752009-05-18 22:11:22 -07003390 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003391 case SIOCOUTQ:
3392 {
3393 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07003394
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003395 return put_user(amount, (int __user *)arg);
3396 }
3397 case SIOCINQ:
3398 {
3399 struct sk_buff *skb;
3400 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003402 spin_lock_bh(&sk->sk_receive_queue.lock);
3403 skb = skb_peek(&sk->sk_receive_queue);
3404 if (skb)
3405 amount = skb->len;
3406 spin_unlock_bh(&sk->sk_receive_queue.lock);
3407 return put_user(amount, (int __user *)arg);
3408 }
3409 case SIOCGSTAMP:
3410 return sock_get_timestamp(sk, (struct timeval __user *)arg);
3411 case SIOCGSTAMPNS:
3412 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003415 case SIOCADDRT:
3416 case SIOCDELRT:
3417 case SIOCDARP:
3418 case SIOCGARP:
3419 case SIOCSARP:
3420 case SIOCGIFADDR:
3421 case SIOCSIFADDR:
3422 case SIOCGIFBRDADDR:
3423 case SIOCSIFBRDADDR:
3424 case SIOCGIFNETMASK:
3425 case SIOCSIFNETMASK:
3426 case SIOCGIFDSTADDR:
3427 case SIOCSIFDSTADDR:
3428 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003429 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430#endif
3431
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003432 default:
3433 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 }
3435 return 0;
3436}
3437
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003438static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 poll_table *wait)
3440{
3441 struct sock *sk = sock->sk;
3442 struct packet_sock *po = pkt_sk(sk);
3443 unsigned int mask = datagram_poll(file, sock, wait);
3444
3445 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003446 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f12011-08-19 10:18:16 +00003447 if (!packet_previous_rx_frame(po, &po->rx_ring,
3448 TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 mask |= POLLIN | POLLRDNORM;
3450 }
3451 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003452 spin_lock_bh(&sk->sk_write_queue.lock);
3453 if (po->tx_ring.pg_vec) {
3454 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
3455 mask |= POLLOUT | POLLWRNORM;
3456 }
3457 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return mask;
3459}
3460
3461
3462/* Dirty? Well, I still did not learn better way to account
3463 * for user mmaps.
3464 */
3465
3466static void packet_mm_open(struct vm_area_struct *vma)
3467{
3468 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003469 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (sk)
3473 atomic_inc(&pkt_sk(sk)->mapped);
3474}
3475
3476static void packet_mm_close(struct vm_area_struct *vma)
3477{
3478 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003479 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 if (sk)
3483 atomic_dec(&pkt_sk(sk)->mapped);
3484}
3485
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003486static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003487 .open = packet_mm_open,
3488 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489};
3490
Neil Horman0e3125c2010-11-16 10:26:47 -08003491static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
3492 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 int i;
3495
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003496 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08003497 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003498 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08003499 vfree(pg_vec[i].buffer);
3500 else
3501 free_pages((unsigned long)pg_vec[i].buffer,
3502 order);
3503 pg_vec[i].buffer = NULL;
3504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
3506 kfree(pg_vec);
3507}
3508
Olof Johanssoneea49cc92011-11-02 11:00:49 +00003509static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003510{
Neil Horman0e3125c2010-11-16 10:26:47 -08003511 char *buffer = NULL;
3512 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
3513 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07003514
Neil Horman0e3125c2010-11-16 10:26:47 -08003515 buffer = (char *) __get_free_pages(gfp_flags, order);
3516
3517 if (buffer)
3518 return buffer;
3519
3520 /*
3521 * __get_free_pages failed, fall back to vmalloc
3522 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00003523 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08003524
3525 if (buffer)
3526 return buffer;
3527
3528 /*
3529 * vmalloc failed, lets dig into swap here
3530 */
Neil Horman0e3125c2010-11-16 10:26:47 -08003531 gfp_flags &= ~__GFP_NORETRY;
3532 buffer = (char *)__get_free_pages(gfp_flags, order);
3533 if (buffer)
3534 return buffer;
3535
3536 /*
3537 * complete and utter failure
3538 */
3539 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003540}
3541
Neil Horman0e3125c2010-11-16 10:26:47 -08003542static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003543{
3544 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08003545 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003546 int i;
3547
Neil Horman0e3125c2010-11-16 10:26:47 -08003548 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003549 if (unlikely(!pg_vec))
3550 goto out;
3551
3552 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003553 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003554 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003555 goto out_free_pgvec;
3556 }
3557
3558out:
3559 return pg_vec;
3560
3561out_free_pgvec:
3562 free_pg_vec(pg_vec, order, block_nr);
3563 pg_vec = NULL;
3564 goto out;
3565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
chetan lokef6fb8f12011-08-19 10:18:16 +00003567static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07003568 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
Neil Horman0e3125c2010-11-16 10:26:47 -08003570 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08003572 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07003573 struct packet_ring_buffer *rb;
3574 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08003575 __be16 num;
chetan lokef6fb8f12011-08-19 10:18:16 +00003576 int err = -EINVAL;
3577 /* Added to avoid minimal code churn */
3578 struct tpacket_req *req = &req_u->req;
3579
3580 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
3581 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
3582 WARN(1, "Tx-ring is not supported.\n");
3583 goto out;
3584 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003585
3586 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
3587 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
3588
3589 err = -EBUSY;
3590 if (!closing) {
3591 if (atomic_read(&po->mapped))
3592 goto out;
3593 if (atomic_read(&rb->pending))
3594 goto out;
3595 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07003599 err = -EBUSY;
3600 if (unlikely(rb->pg_vec))
3601 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003603 switch (po->tp_version) {
3604 case TPACKET_V1:
3605 po->tp_hdrlen = TPACKET_HDRLEN;
3606 break;
3607 case TPACKET_V2:
3608 po->tp_hdrlen = TPACKET2_HDRLEN;
3609 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00003610 case TPACKET_V3:
3611 po->tp_hdrlen = TPACKET3_HDRLEN;
3612 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003613 }
3614
Johann Baudy69e3c752009-05-18 22:11:22 -07003615 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003616 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07003617 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003618 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07003619 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003620 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07003621 po->tp_reserve))
3622 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003623 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07003624 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
Johann Baudy69e3c752009-05-18 22:11:22 -07003626 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
3627 if (unlikely(rb->frames_per_block <= 0))
3628 goto out;
3629 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
3630 req->tp_frame_nr))
3631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003634 order = get_order(req->tp_block_size);
3635 pg_vec = alloc_pg_vec(req, order);
3636 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 goto out;
chetan lokef6fb8f12011-08-19 10:18:16 +00003638 switch (po->tp_version) {
3639 case TPACKET_V3:
3640 /* Transmit path is not supported. We checked
3641 * it above but just being paranoid
3642 */
3643 if (!tx_ring)
3644 init_prb_bdqc(po, rb, pg_vec, req_u, tx_ring);
3645 break;
3646 default:
3647 break;
3648 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003649 }
3650 /* Done */
3651 else {
3652 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003653 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07003654 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 }
3656
3657 lock_sock(sk);
3658
3659 /* Detach socket from network */
3660 spin_lock(&po->bind_lock);
3661 was_running = po->running;
3662 num = po->num;
3663 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07003665 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
3667 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 synchronize_net();
3670
3671 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08003672 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (closing || atomic_read(&po->mapped) == 0) {
3674 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07003675 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08003676 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07003677 rb->frame_max = (req->tp_frame_nr - 1);
3678 rb->head = 0;
3679 rb->frame_size = req->tp_frame_size;
3680 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Changli Gaoc053fd92010-12-10 16:02:20 -08003682 swap(rb->pg_vec_order, order);
3683 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Johann Baudy69e3c752009-05-18 22:11:22 -07003685 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
3686 po->prot_hook.func = (po->rx_ring.pg_vec) ?
3687 tpacket_rcv : packet_rcv;
3688 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003690 pr_err("packet_mmap: vma is busy: %d\n",
3691 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
Herbert Xu905db442009-01-30 14:12:06 -08003693 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003696 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07003698 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 }
3700 spin_unlock(&po->bind_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00003701 if (closing && (po->tp_version > TPACKET_V2)) {
3702 /* Because we don't support block-based V3 on tx-ring */
3703 if (!tx_ring)
3704 prb_shutdown_retire_blk_timer(po, tx_ring, rb_queue);
3705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 release_sock(sk);
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 if (pg_vec)
3709 free_pg_vec(pg_vec, order, req->tp_block_nr);
3710out:
3711 return err;
3712}
3713
Johann Baudy69e3c752009-05-18 22:11:22 -07003714static int packet_mmap(struct file *file, struct socket *sock,
3715 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
3717 struct sock *sk = sock->sk;
3718 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07003719 unsigned long size, expected_size;
3720 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 unsigned long start;
3722 int err = -EINVAL;
3723 int i;
3724
3725 if (vma->vm_pgoff)
3726 return -EINVAL;
3727
Herbert Xu905db442009-01-30 14:12:06 -08003728 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003729
3730 expected_size = 0;
3731 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
3732 if (rb->pg_vec) {
3733 expected_size += rb->pg_vec_len
3734 * rb->pg_vec_pages
3735 * PAGE_SIZE;
3736 }
3737 }
3738
3739 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07003741
3742 size = vma->vm_end - vma->vm_start;
3743 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 goto out;
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07003747 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
3748 if (rb->pg_vec == NULL)
3749 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003750
Johann Baudy69e3c752009-05-18 22:11:22 -07003751 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08003752 struct page *page;
3753 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07003754 int pg_num;
3755
Changli Gaoc56b4d92010-12-01 02:52:57 +00003756 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
3757 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07003758 err = vm_insert_page(vma, start, page);
3759 if (unlikely(err))
3760 goto out;
3761 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08003762 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07003763 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003766
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003767 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 vma->vm_ops = &packet_mmap_ops;
3769 err = 0;
3770
3771out:
Herbert Xu905db442009-01-30 14:12:06 -08003772 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 return err;
3774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003776static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 .family = PF_PACKET,
3778 .owner = THIS_MODULE,
3779 .release = packet_release,
3780 .bind = packet_bind_spkt,
3781 .connect = sock_no_connect,
3782 .socketpair = sock_no_socketpair,
3783 .accept = sock_no_accept,
3784 .getname = packet_getname_spkt,
3785 .poll = datagram_poll,
3786 .ioctl = packet_ioctl,
3787 .listen = sock_no_listen,
3788 .shutdown = sock_no_shutdown,
3789 .setsockopt = sock_no_setsockopt,
3790 .getsockopt = sock_no_getsockopt,
3791 .sendmsg = packet_sendmsg_spkt,
3792 .recvmsg = packet_recvmsg,
3793 .mmap = sock_no_mmap,
3794 .sendpage = sock_no_sendpage,
3795};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003797static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 .family = PF_PACKET,
3799 .owner = THIS_MODULE,
3800 .release = packet_release,
3801 .bind = packet_bind,
3802 .connect = sock_no_connect,
3803 .socketpair = sock_no_socketpair,
3804 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003805 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 .poll = packet_poll,
3807 .ioctl = packet_ioctl,
3808 .listen = sock_no_listen,
3809 .shutdown = sock_no_shutdown,
3810 .setsockopt = packet_setsockopt,
3811 .getsockopt = packet_getsockopt,
3812 .sendmsg = packet_sendmsg,
3813 .recvmsg = packet_recvmsg,
3814 .mmap = packet_mmap,
3815 .sendpage = sock_no_sendpage,
3816};
3817
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003818static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 .family = PF_PACKET,
3820 .create = packet_create,
3821 .owner = THIS_MODULE,
3822};
3823
3824static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003825 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826};
3827
3828#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00003831 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Denis V. Luneve372c412007-11-19 22:31:54 -08003833 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003834
3835 rcu_read_lock();
3836 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
3839static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3840{
Herbert Xu1bf40952007-12-16 14:04:02 -08003841 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003842 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
3845static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00003846 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
stephen hemminger808f5112010-02-22 07:57:18 +00003848 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003851static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
3853 if (v == SEQ_START_TOKEN)
3854 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
3855 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00003856 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 const struct packet_sock *po = pkt_sk(s);
3858
3859 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003860 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 s,
3862 atomic_read(&s->sk_refcnt),
3863 s->sk_type,
3864 ntohs(po->num),
3865 po->ifindex,
3866 po->running,
3867 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003868 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003869 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
3871
3872 return 0;
3873}
3874
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003875static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 .start = packet_seq_start,
3877 .next = packet_seq_next,
3878 .stop = packet_seq_stop,
3879 .show = packet_seq_show,
3880};
3881
3882static int packet_seq_open(struct inode *inode, struct file *file)
3883{
Denis V. Luneve372c412007-11-19 22:31:54 -08003884 return seq_open_net(inode, file, &packet_seq_ops,
3885 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887
Arjan van de Venda7071d2007-02-12 00:55:36 -08003888static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 .owner = THIS_MODULE,
3890 .open = packet_seq_open,
3891 .read = seq_read,
3892 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003893 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894};
3895
3896#endif
3897
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003898static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003899{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003900 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08003901 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003902
Gao fengd4beaa62013-02-18 01:34:54 +00003903 if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003904 return -ENOMEM;
3905
3906 return 0;
3907}
3908
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003909static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003910{
Gao fengece31ff2013-02-18 01:34:56 +00003911 remove_proc_entry("packet", net->proc_net);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003912}
3913
3914static struct pernet_operations packet_net_ops = {
3915 .init = packet_net_init,
3916 .exit = packet_net_exit,
3917};
3918
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920static void __exit packet_exit(void)
3921{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003923 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 sock_unregister(PF_PACKET);
3925 proto_unregister(&packet_proto);
3926}
3927
3928static int __init packet_init(void)
3929{
3930 int rc = proto_register(&packet_proto, 0);
3931
3932 if (rc != 0)
3933 goto out;
3934
3935 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003936 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938out:
3939 return rc;
3940}
3941
3942module_init(packet_init);
3943module_exit(packet_exit);
3944MODULE_LICENSE("GPL");
3945MODULE_ALIAS_NETPROTO(PF_PACKET);