blob: ec8ea27733acafe6b360a83e8e63e79ed92d7f67 [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
Willem de Bruijn2e313962013-04-23 00:39:28 +0000342static void __packet_set_timestamp(struct packet_sock *po, void *frame,
343 ktime_t tstamp)
344{
345 union tpacket_uhdr h;
346 struct timespec ts;
347
348 if (!ktime_to_timespec_cond(tstamp, &ts) ||
349 !sock_flag(&po->sk, SOCK_TIMESTAMPING_SOFTWARE))
350 return;
351
352 h.raw = frame;
353 switch (po->tp_version) {
354 case TPACKET_V1:
355 h.h1->tp_sec = ts.tv_sec;
356 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
357 break;
358 case TPACKET_V2:
359 h.h2->tp_sec = ts.tv_sec;
360 h.h2->tp_nsec = ts.tv_nsec;
361 break;
362 case TPACKET_V3:
363 default:
364 WARN(1, "TPACKET version not supported.\n");
365 BUG();
366 }
367
368 /* one flush is safe, as both fields always lie on the same cacheline */
369 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
370 smp_wmb();
371}
372
Johann Baudy69e3c752009-05-18 22:11:22 -0700373static void *packet_lookup_frame(struct packet_sock *po,
374 struct packet_ring_buffer *rb,
375 unsigned int position,
376 int status)
377{
378 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000379 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700380
381 pg_vec_pos = position / rb->frames_per_block;
382 frame_offset = position % rb->frames_per_block;
383
Neil Horman0e3125c2010-11-16 10:26:47 -0800384 h.raw = rb->pg_vec[pg_vec_pos].buffer +
385 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700386
387 if (status != __packet_get_status(po, h.raw))
388 return NULL;
389
390 return h.raw;
391}
392
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000393static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700394 struct packet_ring_buffer *rb,
395 int status)
396{
397 return packet_lookup_frame(po, rb, rb->head, status);
398}
399
chetan lokebc59ba32011-08-25 10:43:30 +0000400static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000401{
402 del_timer_sync(&pkc->retire_blk_timer);
403}
404
405static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
406 int tx_ring,
407 struct sk_buff_head *rb_queue)
408{
chetan lokebc59ba32011-08-25 10:43:30 +0000409 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000410
411 pkc = tx_ring ? &po->tx_ring.prb_bdqc : &po->rx_ring.prb_bdqc;
412
413 spin_lock(&rb_queue->lock);
414 pkc->delete_blk_timer = 1;
415 spin_unlock(&rb_queue->lock);
416
417 prb_del_retire_blk_timer(pkc);
418}
419
420static void prb_init_blk_timer(struct packet_sock *po,
chetan lokebc59ba32011-08-25 10:43:30 +0000421 struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000422 void (*func) (unsigned long))
423{
424 init_timer(&pkc->retire_blk_timer);
425 pkc->retire_blk_timer.data = (long)po;
426 pkc->retire_blk_timer.function = func;
427 pkc->retire_blk_timer.expires = jiffies;
428}
429
430static void prb_setup_retire_blk_timer(struct packet_sock *po, int tx_ring)
431{
chetan lokebc59ba32011-08-25 10:43:30 +0000432 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000433
434 if (tx_ring)
435 BUG();
436
437 pkc = tx_ring ? &po->tx_ring.prb_bdqc : &po->rx_ring.prb_bdqc;
438 prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
439}
440
441static int prb_calc_retire_blk_tmo(struct packet_sock *po,
442 int blk_size_in_bytes)
443{
444 struct net_device *dev;
445 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000446 struct ethtool_cmd ecmd;
447 int err;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000448 u32 speed;
chetan lokef6fb8f12011-08-19 10:18:16 +0000449
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000450 rtnl_lock();
451 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
452 if (unlikely(!dev)) {
453 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000454 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000455 }
456 err = __ethtool_get_settings(dev, &ecmd);
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000457 speed = ethtool_cmd_speed(&ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000458 rtnl_unlock();
459 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000460 /*
461 * If the link speed is so slow you don't really
462 * need to worry about perf anyways
463 */
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000464 if (speed < SPEED_1000 || speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000465 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000466 } else {
467 msec = 1;
468 div = speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000469 }
470 }
471
472 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
473
474 if (div)
475 mbits /= div;
476
477 tmo = mbits * msec;
478
479 if (div)
480 return tmo+1;
481 return tmo;
482}
483
chetan lokebc59ba32011-08-25 10:43:30 +0000484static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000485 union tpacket_req_u *req_u)
486{
487 p1->feature_req_word = req_u->req3.tp_feature_req_word;
488}
489
490static void init_prb_bdqc(struct packet_sock *po,
491 struct packet_ring_buffer *rb,
492 struct pgv *pg_vec,
493 union tpacket_req_u *req_u, int tx_ring)
494{
chetan lokebc59ba32011-08-25 10:43:30 +0000495 struct tpacket_kbdq_core *p1 = &rb->prb_bdqc;
496 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000497
498 memset(p1, 0x0, sizeof(*p1));
499
500 p1->knxt_seq_num = 1;
501 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000502 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000503 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000504 p1->kblk_size = req_u->req3.tp_block_size;
505 p1->knum_blocks = req_u->req3.tp_block_nr;
506 p1->hdrlen = po->tp_hdrlen;
507 p1->version = po->tp_version;
508 p1->last_kactive_blk_num = 0;
509 po->stats_u.stats3.tp_freeze_q_cnt = 0;
510 if (req_u->req3.tp_retire_blk_tov)
511 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
512 else
513 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
514 req_u->req3.tp_block_size);
515 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
516 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
517
518 prb_init_ft_ops(p1, req_u);
519 prb_setup_retire_blk_timer(po, tx_ring);
520 prb_open_block(p1, pbd);
521}
522
523/* Do NOT update the last_blk_num first.
524 * Assumes sk_buff_head lock is held.
525 */
chetan lokebc59ba32011-08-25 10:43:30 +0000526static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000527{
528 mod_timer(&pkc->retire_blk_timer,
529 jiffies + pkc->tov_in_jiffies);
530 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
531}
532
533/*
534 * Timer logic:
535 * 1) We refresh the timer only when we open a block.
536 * By doing this we don't waste cycles refreshing the timer
537 * on packet-by-packet basis.
538 *
539 * With a 1MB block-size, on a 1Gbps line, it will take
540 * i) ~8 ms to fill a block + ii) memcpy etc.
541 * In this cut we are not accounting for the memcpy time.
542 *
543 * So, if the user sets the 'tmo' to 10ms then the timer
544 * will never fire while the block is still getting filled
545 * (which is what we want). However, the user could choose
546 * to close a block early and that's fine.
547 *
548 * But when the timer does fire, we check whether or not to refresh it.
549 * Since the tmo granularity is in msecs, it is not too expensive
550 * to refresh the timer, lets say every '8' msecs.
551 * Either the user can set the 'tmo' or we can derive it based on
552 * a) line-speed and b) block-size.
553 * prb_calc_retire_blk_tmo() calculates the tmo.
554 *
555 */
556static void prb_retire_rx_blk_timer_expired(unsigned long data)
557{
558 struct packet_sock *po = (struct packet_sock *)data;
chetan lokebc59ba32011-08-25 10:43:30 +0000559 struct tpacket_kbdq_core *pkc = &po->rx_ring.prb_bdqc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000560 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000561 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000562
563 spin_lock(&po->sk.sk_receive_queue.lock);
564
565 frozen = prb_queue_frozen(pkc);
566 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
567
568 if (unlikely(pkc->delete_blk_timer))
569 goto out;
570
571 /* We only need to plug the race when the block is partially filled.
572 * tpacket_rcv:
573 * lock(); increment BLOCK_NUM_PKTS; unlock()
574 * copy_bits() is in progress ...
575 * timer fires on other cpu:
576 * we can't retire the current block because copy_bits
577 * is in progress.
578 *
579 */
580 if (BLOCK_NUM_PKTS(pbd)) {
581 while (atomic_read(&pkc->blk_fill_in_prog)) {
582 /* Waiting for skb_copy_bits to finish... */
583 cpu_relax();
584 }
585 }
586
587 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
588 if (!frozen) {
589 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
590 if (!prb_dispatch_next_block(pkc, po))
591 goto refresh_timer;
592 else
593 goto out;
594 } else {
595 /* Case 1. Queue was frozen because user-space was
596 * lagging behind.
597 */
598 if (prb_curr_blk_in_use(pkc, pbd)) {
599 /*
600 * Ok, user-space is still behind.
601 * So just refresh the timer.
602 */
603 goto refresh_timer;
604 } else {
605 /* Case 2. queue was frozen,user-space caught up,
606 * now the link went idle && the timer fired.
607 * We don't have a block to close.So we open this
608 * block and restart the timer.
609 * opening a block thaws the queue,restarts timer
610 * Thawing/timer-refresh is a side effect.
611 */
612 prb_open_block(pkc, pbd);
613 goto out;
614 }
615 }
616 }
617
618refresh_timer:
619 _prb_refresh_rx_retire_blk_timer(pkc);
620
621out:
622 spin_unlock(&po->sk.sk_receive_queue.lock);
623}
624
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000625static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000626 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000627{
628 /* Flush everything minus the block header */
629
630#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
631 u8 *start, *end;
632
633 start = (u8 *)pbd1;
634
635 /* Skip the block header(we know header WILL fit in 4K) */
636 start += PAGE_SIZE;
637
638 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
639 for (; start < end; start += PAGE_SIZE)
640 flush_dcache_page(pgv_to_page(start));
641
642 smp_wmb();
643#endif
644
645 /* Now update the block status. */
646
647 BLOCK_STATUS(pbd1) = status;
648
649 /* Flush the block header */
650
651#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
652 start = (u8 *)pbd1;
653 flush_dcache_page(pgv_to_page(start));
654
655 smp_wmb();
656#endif
657}
658
659/*
660 * Side effect:
661 *
662 * 1) flush the block
663 * 2) Increment active_blk_num
664 *
665 * Note:We DONT refresh the timer on purpose.
666 * Because almost always the next block will be opened.
667 */
chetan lokebc59ba32011-08-25 10:43:30 +0000668static void prb_close_block(struct tpacket_kbdq_core *pkc1,
669 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000670 struct packet_sock *po, unsigned int stat)
671{
672 __u32 status = TP_STATUS_USER | stat;
673
674 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000675 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000676
677 if (po->stats.tp_drops)
678 status |= TP_STATUS_LOSING;
679
680 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
681 last_pkt->tp_next_offset = 0;
682
683 /* Get the ts of the last pkt */
684 if (BLOCK_NUM_PKTS(pbd1)) {
685 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
686 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
687 } else {
688 /* Ok, we tmo'd - so get the current time */
689 struct timespec ts;
690 getnstimeofday(&ts);
691 h1->ts_last_pkt.ts_sec = ts.tv_sec;
692 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
693 }
694
695 smp_wmb();
696
697 /* Flush the block */
698 prb_flush_block(pkc1, pbd1, status);
699
700 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
701}
702
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000703static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000704{
705 pkc->reset_pending_on_curr_blk = 0;
706}
707
708/*
709 * Side effect of opening a block:
710 *
711 * 1) prb_queue is thawed.
712 * 2) retire_blk_timer is refreshed.
713 *
714 */
chetan lokebc59ba32011-08-25 10:43:30 +0000715static void prb_open_block(struct tpacket_kbdq_core *pkc1,
716 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000717{
718 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000719 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000720
721 smp_rmb();
722
723 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd1))) {
724
725 /* We could have just memset this but we will lose the
726 * flexibility of making the priv area sticky
727 */
728 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
729 BLOCK_NUM_PKTS(pbd1) = 0;
730 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
731 getnstimeofday(&ts);
732 h1->ts_first_pkt.ts_sec = ts.tv_sec;
733 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
734 pkc1->pkblk_start = (char *)pbd1;
Joe Perchese3192692012-06-03 17:41:40 +0000735 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000736 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
737 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
738 pbd1->version = pkc1->version;
739 pkc1->prev = pkc1->nxt_offset;
740 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
741 prb_thaw_queue(pkc1);
742 _prb_refresh_rx_retire_blk_timer(pkc1);
743
744 smp_wmb();
745
746 return;
747 }
748
749 WARN(1, "ERROR block:%p is NOT FREE status:%d kactive_blk_num:%d\n",
750 pbd1, BLOCK_STATUS(pbd1), pkc1->kactive_blk_num);
751 dump_stack();
752 BUG();
753}
754
755/*
756 * Queue freeze logic:
757 * 1) Assume tp_block_nr = 8 blocks.
758 * 2) At time 't0', user opens Rx ring.
759 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
760 * 4) user-space is either sleeping or processing block '0'.
761 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
762 * it will close block-7,loop around and try to fill block '0'.
763 * call-flow:
764 * __packet_lookup_frame_in_block
765 * prb_retire_current_block()
766 * prb_dispatch_next_block()
767 * |->(BLOCK_STATUS == USER) evaluates to true
768 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
769 * 6) Now there are two cases:
770 * 6.1) Link goes idle right after the queue is frozen.
771 * But remember, the last open_block() refreshed the timer.
772 * When this timer expires,it will refresh itself so that we can
773 * re-open block-0 in near future.
774 * 6.2) Link is busy and keeps on receiving packets. This is a simple
775 * case and __packet_lookup_frame_in_block will check if block-0
776 * is free and can now be re-used.
777 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000778static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000779 struct packet_sock *po)
780{
781 pkc->reset_pending_on_curr_blk = 1;
782 po->stats_u.stats3.tp_freeze_q_cnt++;
783}
784
785#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
786
787/*
788 * If the next block is free then we will dispatch it
789 * and return a good offset.
790 * Else, we will freeze the queue.
791 * So, caller must check the return value.
792 */
chetan lokebc59ba32011-08-25 10:43:30 +0000793static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000794 struct packet_sock *po)
795{
chetan lokebc59ba32011-08-25 10:43:30 +0000796 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000797
798 smp_rmb();
799
800 /* 1. Get current block num */
801 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
802
803 /* 2. If this block is currently in_use then freeze the queue */
804 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
805 prb_freeze_queue(pkc, po);
806 return NULL;
807 }
808
809 /*
810 * 3.
811 * open this block and return the offset where the first packet
812 * needs to get stored.
813 */
814 prb_open_block(pkc, pbd);
815 return (void *)pkc->nxt_offset;
816}
817
chetan lokebc59ba32011-08-25 10:43:30 +0000818static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000819 struct packet_sock *po, unsigned int status)
820{
chetan lokebc59ba32011-08-25 10:43:30 +0000821 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000822
823 /* retire/close the current block */
824 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
825 /*
826 * Plug the case where copy_bits() is in progress on
827 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
828 * have space to copy the pkt in the current block and
829 * called prb_retire_current_block()
830 *
831 * We don't need to worry about the TMO case because
832 * the timer-handler already handled this case.
833 */
834 if (!(status & TP_STATUS_BLK_TMO)) {
835 while (atomic_read(&pkc->blk_fill_in_prog)) {
836 /* Waiting for skb_copy_bits to finish... */
837 cpu_relax();
838 }
839 }
840 prb_close_block(pkc, pbd, po, status);
841 return;
842 }
843
844 WARN(1, "ERROR-pbd[%d]:%p\n", pkc->kactive_blk_num, pbd);
845 dump_stack();
846 BUG();
847}
848
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000849static int prb_curr_blk_in_use(struct tpacket_kbdq_core *pkc,
chetan lokebc59ba32011-08-25 10:43:30 +0000850 struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000851{
852 return TP_STATUS_USER & BLOCK_STATUS(pbd);
853}
854
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000855static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000856{
857 return pkc->reset_pending_on_curr_blk;
858}
859
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000860static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +0000861{
chetan lokebc59ba32011-08-25 10:43:30 +0000862 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000863 atomic_dec(&pkc->blk_fill_in_prog);
864}
865
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000866static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000867 struct tpacket3_hdr *ppd)
868{
869 ppd->hv1.tp_rxhash = skb_get_rxhash(pkc->skb);
870}
871
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000872static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000873 struct tpacket3_hdr *ppd)
874{
875 ppd->hv1.tp_rxhash = 0;
876}
877
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000878static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000879 struct tpacket3_hdr *ppd)
880{
881 if (vlan_tx_tag_present(pkc->skb)) {
882 ppd->hv1.tp_vlan_tci = vlan_tx_tag_get(pkc->skb);
883 ppd->tp_status = TP_STATUS_VLAN_VALID;
884 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000885 ppd->hv1.tp_vlan_tci = 0;
886 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +0000887 }
888}
889
chetan lokebc59ba32011-08-25 10:43:30 +0000890static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000891 struct tpacket3_hdr *ppd)
892{
893 prb_fill_vlan_info(pkc, ppd);
894
895 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
896 prb_fill_rxhash(pkc, ppd);
897 else
898 prb_clear_rxhash(pkc, ppd);
899}
900
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000901static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000902 struct tpacket_kbdq_core *pkc,
903 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +0000904 unsigned int len)
905{
906 struct tpacket3_hdr *ppd;
907
908 ppd = (struct tpacket3_hdr *)curr;
909 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
910 pkc->prev = curr;
911 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
912 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
913 BLOCK_NUM_PKTS(pbd) += 1;
914 atomic_inc(&pkc->blk_fill_in_prog);
915 prb_run_all_ft_ops(pkc, ppd);
916}
917
918/* Assumes caller has the sk->rx_queue.lock */
919static void *__packet_lookup_frame_in_block(struct packet_sock *po,
920 struct sk_buff *skb,
921 int status,
922 unsigned int len
923 )
924{
chetan lokebc59ba32011-08-25 10:43:30 +0000925 struct tpacket_kbdq_core *pkc;
926 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000927 char *curr, *end;
928
Joe Perchese3192692012-06-03 17:41:40 +0000929 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000930 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
931
932 /* Queue is frozen when user space is lagging behind */
933 if (prb_queue_frozen(pkc)) {
934 /*
935 * Check if that last block which caused the queue to freeze,
936 * is still in_use by user-space.
937 */
938 if (prb_curr_blk_in_use(pkc, pbd)) {
939 /* Can't record this packet */
940 return NULL;
941 } else {
942 /*
943 * Ok, the block was released by user-space.
944 * Now let's open that block.
945 * opening a block also thaws the queue.
946 * Thawing is a side effect.
947 */
948 prb_open_block(pkc, pbd);
949 }
950 }
951
952 smp_mb();
953 curr = pkc->nxt_offset;
954 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +0000955 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +0000956
957 /* first try the current block */
958 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
959 prb_fill_curr_block(curr, pkc, pbd, len);
960 return (void *)curr;
961 }
962
963 /* Ok, close the current block */
964 prb_retire_current_block(pkc, po, 0);
965
966 /* Now, try to dispatch the next block */
967 curr = (char *)prb_dispatch_next_block(pkc, po);
968 if (curr) {
969 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
970 prb_fill_curr_block(curr, pkc, pbd, len);
971 return (void *)curr;
972 }
973
974 /*
975 * No free blocks are available.user_space hasn't caught up yet.
976 * Queue was just frozen and now this packet will get dropped.
977 */
978 return NULL;
979}
980
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000981static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +0000982 struct sk_buff *skb,
983 int status, unsigned int len)
984{
985 char *curr = NULL;
986 switch (po->tp_version) {
987 case TPACKET_V1:
988 case TPACKET_V2:
989 curr = packet_lookup_frame(po, &po->rx_ring,
990 po->rx_ring.head, status);
991 return curr;
992 case TPACKET_V3:
993 return __packet_lookup_frame_in_block(po, skb, status, len);
994 default:
995 WARN(1, "TPACKET version not supported\n");
996 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +0000997 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +0000998 }
999}
1000
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001001static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001002 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001003 unsigned int idx,
chetan lokef6fb8f12011-08-19 10:18:16 +00001004 int status)
1005{
chetan lokebc59ba32011-08-25 10:43:30 +00001006 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001007 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001008
1009 if (status != BLOCK_STATUS(pbd))
1010 return NULL;
1011 return pbd;
1012}
1013
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001014static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001015{
1016 unsigned int prev;
1017 if (rb->prb_bdqc.kactive_blk_num)
1018 prev = rb->prb_bdqc.kactive_blk_num-1;
1019 else
1020 prev = rb->prb_bdqc.knum_blocks-1;
1021 return prev;
1022}
1023
1024/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001025static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001026 struct packet_ring_buffer *rb,
1027 int status)
1028{
1029 unsigned int previous = prb_previous_blk_num(rb);
1030 return prb_lookup_block(po, rb, previous, status);
1031}
1032
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001033static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001034 struct packet_ring_buffer *rb,
1035 int status)
1036{
1037 if (po->tp_version <= TPACKET_V2)
1038 return packet_previous_frame(po, rb, status);
1039
1040 return __prb_previous_block(po, rb, status);
1041}
1042
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001043static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001044 struct packet_ring_buffer *rb)
1045{
1046 switch (po->tp_version) {
1047 case TPACKET_V1:
1048 case TPACKET_V2:
1049 return packet_increment_head(rb);
1050 case TPACKET_V3:
1051 default:
1052 WARN(1, "TPACKET version not supported.\n");
1053 BUG();
1054 return;
1055 }
1056}
1057
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001058static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001059 struct packet_ring_buffer *rb,
1060 int status)
1061{
1062 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1063 return packet_lookup_frame(po, rb, previous, status);
1064}
1065
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001066static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001067{
1068 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1069}
1070
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001071static bool packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1072{
1073 struct sock *sk = &po->sk;
1074 bool has_room;
1075
1076 if (po->prot_hook.func != tpacket_rcv)
1077 return (atomic_read(&sk->sk_rmem_alloc) + skb->truesize)
1078 <= sk->sk_rcvbuf;
1079
1080 spin_lock(&sk->sk_receive_queue.lock);
1081 if (po->tp_version == TPACKET_V3)
1082 has_room = prb_lookup_block(po, &po->rx_ring,
1083 po->rx_ring.prb_bdqc.kactive_blk_num,
1084 TP_STATUS_KERNEL);
1085 else
1086 has_room = packet_lookup_frame(po, &po->rx_ring,
1087 po->rx_ring.head,
1088 TP_STATUS_KERNEL);
1089 spin_unlock(&sk->sk_receive_queue.lock);
1090
1091 return has_room;
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094static void packet_sock_destruct(struct sock *sk)
1095{
Richard Cochraned85b562010-04-07 22:41:28 +00001096 skb_queue_purge(&sk->sk_error_queue);
1097
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001098 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
1099 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001102 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
1105
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001106 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
David S. Millerdc99f602011-07-05 01:45:05 -07001109static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
1110{
1111 int x = atomic_read(&f->rr_cur) + 1;
1112
1113 if (x >= num)
1114 x = 0;
1115
1116 return x;
1117}
1118
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001119static unsigned int fanout_demux_hash(struct packet_fanout *f,
1120 struct sk_buff *skb,
1121 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001122{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001123 return (((u64)skb->rxhash) * num) >> 32;
David S. Millerdc99f602011-07-05 01:45:05 -07001124}
1125
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001126static unsigned int fanout_demux_lb(struct packet_fanout *f,
1127 struct sk_buff *skb,
1128 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001129{
1130 int cur, old;
1131
1132 cur = atomic_read(&f->rr_cur);
1133 while ((old = atomic_cmpxchg(&f->rr_cur, cur,
1134 fanout_rr_next(f, num))) != cur)
1135 cur = old;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001136 return cur;
David S. Millerdc99f602011-07-05 01:45:05 -07001137}
1138
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001139static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1140 struct sk_buff *skb,
1141 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001142{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001143 return smp_processor_id() % num;
1144}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001145
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001146static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1147 struct sk_buff *skb,
1148 unsigned int idx, unsigned int skip,
1149 unsigned int num)
1150{
1151 unsigned int i, j;
1152
1153 i = j = min_t(int, f->next[idx], num - 1);
1154 do {
1155 if (i != skip && packet_rcv_has_room(pkt_sk(f->arr[i]), skb)) {
1156 if (i != j)
1157 f->next[idx] = i;
1158 return i;
1159 }
1160 if (++i == num)
1161 i = 0;
1162 } while (i != j);
1163
1164 return idx;
1165}
1166
1167static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1168{
1169 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001170}
1171
David S. Miller95ec3eb2011-07-06 01:56:38 -07001172static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1173 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001174{
1175 struct packet_fanout *f = pt->af_packet_priv;
1176 unsigned int num = f->num_members;
1177 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001178 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001179
1180 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
1181 !num) {
1182 kfree_skb(skb);
1183 return 0;
1184 }
1185
David S. Miller95ec3eb2011-07-06 01:56:38 -07001186 switch (f->type) {
1187 case PACKET_FANOUT_HASH:
1188 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001189 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +00001190 skb = ip_check_defrag(skb, IP_DEFRAG_AF_PACKET);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001191 if (!skb)
1192 return 0;
1193 }
1194 skb_get_rxhash(skb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001195 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001196 break;
1197 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001198 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001199 break;
1200 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001201 idx = fanout_demux_cpu(f, skb, num);
1202 break;
1203 case PACKET_FANOUT_ROLLOVER:
1204 idx = fanout_demux_rollover(f, skb, 0, (unsigned int) -1, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001205 break;
David S. Miller7736d332011-07-05 01:43:20 -07001206 }
1207
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001208 po = pkt_sk(f->arr[idx]);
1209 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER) &&
1210 unlikely(!packet_rcv_has_room(po, skb))) {
1211 idx = fanout_demux_rollover(f, skb, idx, idx, num);
1212 po = pkt_sk(f->arr[idx]);
1213 }
David S. Millerdc99f602011-07-05 01:45:05 -07001214
1215 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1216}
1217
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001218DEFINE_MUTEX(fanout_mutex);
1219EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001220static LIST_HEAD(fanout_list);
1221
1222static void __fanout_link(struct sock *sk, struct packet_sock *po)
1223{
1224 struct packet_fanout *f = po->fanout;
1225
1226 spin_lock(&f->lock);
1227 f->arr[f->num_members] = sk;
1228 smp_wmb();
1229 f->num_members++;
1230 spin_unlock(&f->lock);
1231}
1232
1233static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1234{
1235 struct packet_fanout *f = po->fanout;
1236 int i;
1237
1238 spin_lock(&f->lock);
1239 for (i = 0; i < f->num_members; i++) {
1240 if (f->arr[i] == sk)
1241 break;
1242 }
1243 BUG_ON(i >= f->num_members);
1244 f->arr[i] = f->arr[f->num_members - 1];
1245 f->num_members--;
1246 spin_unlock(&f->lock);
1247}
1248
Fengguang Wua0dfb262012-08-23 19:51:21 +08001249static bool match_fanout_group(struct packet_type *ptype, struct sock * sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001250{
1251 if (ptype->af_packet_priv == (void*)((struct packet_sock *)sk)->fanout)
1252 return true;
1253
1254 return false;
1255}
1256
David S. Miller7736d332011-07-05 01:43:20 -07001257static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001258{
1259 struct packet_sock *po = pkt_sk(sk);
1260 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001261 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001262 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001263 int err;
1264
1265 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001266 case PACKET_FANOUT_ROLLOVER:
1267 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1268 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001269 case PACKET_FANOUT_HASH:
1270 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001271 case PACKET_FANOUT_CPU:
David S. Millerdc99f602011-07-05 01:45:05 -07001272 break;
1273 default:
1274 return -EINVAL;
1275 }
1276
1277 if (!po->running)
1278 return -EINVAL;
1279
1280 if (po->fanout)
1281 return -EALREADY;
1282
1283 mutex_lock(&fanout_mutex);
1284 match = NULL;
1285 list_for_each_entry(f, &fanout_list, list) {
1286 if (f->id == id &&
1287 read_pnet(&f->net) == sock_net(sk)) {
1288 match = f;
1289 break;
1290 }
1291 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001292 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001293 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001294 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001295 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001296 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001297 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001298 if (!match)
1299 goto out;
1300 write_pnet(&match->net, sock_net(sk));
1301 match->id = id;
1302 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001303 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001304 atomic_set(&match->rr_cur, 0);
1305 INIT_LIST_HEAD(&match->list);
1306 spin_lock_init(&match->lock);
1307 atomic_set(&match->sk_ref, 0);
1308 match->prot_hook.type = po->prot_hook.type;
1309 match->prot_hook.dev = po->prot_hook.dev;
1310 match->prot_hook.func = packet_rcv_fanout;
1311 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001312 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001313 dev_add_pack(&match->prot_hook);
1314 list_add(&match->list, &fanout_list);
1315 }
1316 err = -EINVAL;
1317 if (match->type == type &&
1318 match->prot_hook.type == po->prot_hook.type &&
1319 match->prot_hook.dev == po->prot_hook.dev) {
1320 err = -ENOSPC;
1321 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1322 __dev_remove_pack(&po->prot_hook);
1323 po->fanout = match;
1324 atomic_inc(&match->sk_ref);
1325 __fanout_link(sk, po);
1326 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001327 }
1328 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001329out:
David S. Millerdc99f602011-07-05 01:45:05 -07001330 mutex_unlock(&fanout_mutex);
1331 return err;
1332}
1333
1334static void fanout_release(struct sock *sk)
1335{
1336 struct packet_sock *po = pkt_sk(sk);
1337 struct packet_fanout *f;
1338
1339 f = po->fanout;
1340 if (!f)
1341 return;
1342
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001343 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001344 po->fanout = NULL;
1345
David S. Millerdc99f602011-07-05 01:45:05 -07001346 if (atomic_dec_and_test(&f->sk_ref)) {
1347 list_del(&f->list);
1348 dev_remove_pack(&f->prot_hook);
1349 kfree(f);
1350 }
1351 mutex_unlock(&fanout_mutex);
1352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001354static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001356static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001358static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1359 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct sock *sk;
1362 struct sockaddr_pkt *spkt;
1363
1364 /*
1365 * When we registered the protocol we saved the socket in the data
1366 * field for just this event.
1367 */
1368
1369 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * Yank back the headers [hope the device set this
1373 * right or kerboom...]
1374 *
1375 * Incoming packets have ll header pulled,
1376 * push it back.
1377 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001378 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * so that this procedure is noop.
1380 */
1381
1382 if (skb->pkt_type == PACKET_LOOPBACK)
1383 goto out;
1384
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001385 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001386 goto out;
1387
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001388 skb = skb_share_check(skb, GFP_ATOMIC);
1389 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 goto oom;
1391
1392 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001393 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Phil Oester84531c22005-07-12 11:57:52 -07001395 /* drop conntrack reference */
1396 nf_reset(skb);
1397
Herbert Xuffbc6112007-02-04 23:33:10 -08001398 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001400 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /*
1403 * The SOCK_PACKET socket receives _all_ frames.
1404 */
1405
1406 spkt->spkt_family = dev->type;
1407 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1408 spkt->spkt_protocol = skb->protocol;
1409
1410 /*
1411 * Charge the memory to the socket. This is done specifically
1412 * to prevent sockets using all the memory up.
1413 */
1414
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001415 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return 0;
1417
1418out:
1419 kfree_skb(skb);
1420oom:
1421 return 0;
1422}
1423
1424
1425/*
1426 * Output a raw packet to a device layer. This bypasses all the other
1427 * protocol layers and you must therefore supply it with a complete frame
1428 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
1431 struct msghdr *msg, size_t len)
1432{
1433 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001434 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001435 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001437 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001439 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001442 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001445 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001447 return -EINVAL;
1448 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1449 proto = saddr->spkt_protocol;
1450 } else
1451 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001454 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
1456
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001457 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001458retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001459 rcu_read_lock();
1460 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 err = -ENODEV;
1462 if (dev == NULL)
1463 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001464
David S. Millerd5e76b02007-01-25 19:30:36 -08001465 err = -ENETDOWN;
1466 if (!(dev->flags & IFF_UP))
1467 goto out_unlock;
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001470 * You may not queue a frame bigger than the mtu. This is the lowest level
1471 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001473
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001474 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1475 if (!netif_supports_nofcs(dev)) {
1476 err = -EPROTONOSUPPORT;
1477 goto out_unlock;
1478 }
1479 extra_len = 4; /* We're doing our own CRC */
1480 }
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001483 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 goto out_unlock;
1485
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001486 if (!skb) {
1487 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001488 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001489 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001491 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001492 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001493 if (skb == NULL)
1494 return -ENOBUFS;
1495 /* FIXME: Save some space for broken drivers that write a hard
1496 * header at transmission time by themselves. PPP is the notable
1497 * one here. This should really be fixed at the driver level.
1498 */
1499 skb_reserve(skb, reserved);
1500 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001501
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001502 /* Try to align data part correctly */
1503 if (hhlen) {
1504 skb->data -= hhlen;
1505 skb->tail -= hhlen;
1506 if (len < hhlen)
1507 skb_reset_network_header(skb);
1508 }
1509 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1510 if (err)
1511 goto out_free;
1512 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001515 if (len > (dev->mtu + dev->hard_header_len + extra_len)) {
Ben Greear57f89bf2011-02-11 09:35:18 +00001516 /* Earlier code assumed this would be a VLAN pkt,
1517 * double-check this now that we have the actual
1518 * packet in hand.
1519 */
1520 struct ethhdr *ehdr;
1521 skb_reset_mac_header(skb);
1522 ehdr = eth_hdr(skb);
1523 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1524 err = -EMSGSIZE;
1525 goto out_unlock;
1526 }
1527 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 skb->protocol = proto;
1530 skb->dev = dev;
1531 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001532 skb->mark = sk->sk_mark;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00001533
1534 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001536 if (unlikely(extra_len == 4))
1537 skb->no_fcs = 1;
1538
Jason Wang40893fd2013-03-26 23:11:22 +00001539 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001542 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001543 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001546 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001547out_free:
1548 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return err;
1550}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001552static unsigned int run_filter(const struct sk_buff *skb,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001553 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -08001554 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 struct sk_filter *filter;
1557
Eric Dumazet80f8f102011-01-18 07:46:52 +00001558 rcu_read_lock();
1559 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001560 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +00001561 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001562 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David S. Millerdbcb5852007-01-24 15:21:02 -08001564 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00001568 * This function makes lazy skb cloning in hope that most of packets
1569 * are discarded by BPF.
1570 *
1571 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1572 * and skb->cb are mangled. It works because (and until) packets
1573 * falling here are owned by current CPU. Output packets are cloned
1574 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1575 * sequencially, so that if we return skb to original state on exit,
1576 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 */
1578
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001579static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1580 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct sock *sk;
1583 struct sockaddr_ll *sll;
1584 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001585 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001587 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 if (skb->pkt_type == PACKET_LOOPBACK)
1590 goto drop;
1591
1592 sk = pt->af_packet_priv;
1593 po = pkt_sk(sk);
1594
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001595 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001596 goto drop;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 skb->dev = dev;
1599
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001600 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001602 * exported to higher levels.
1603 *
1604 * Otherwise, the device hides details of its frame
1605 * structure, so that corresponding packet head is
1606 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
1608 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001609 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 else if (skb->pkt_type == PACKET_OUTGOING) {
1611 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001612 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 }
1615
1616 snaplen = skb->len;
1617
David S. Millerdbcb5852007-01-24 15:21:02 -08001618 res = run_filter(skb, sk, snaplen);
1619 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001620 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001621 if (snaplen > res)
1622 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001624 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto drop_n_acct;
1626
1627 if (skb_shared(skb)) {
1628 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1629 if (nskb == NULL)
1630 goto drop_n_acct;
1631
1632 if (skb_head != skb->data) {
1633 skb->data = skb_head;
1634 skb->len = skb_len;
1635 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00001636 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 skb = nskb;
1638 }
1639
Herbert Xuffbc6112007-02-04 23:33:10 -08001640 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
1641 sizeof(skb->cb));
1642
1643 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 sll->sll_family = AF_PACKET;
1645 sll->sll_hatype = dev->type;
1646 sll->sll_protocol = skb->protocol;
1647 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001648 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001649 sll->sll_ifindex = orig_dev->ifindex;
1650 else
1651 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001653 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Herbert Xuffbc6112007-02-04 23:33:10 -08001655 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (pskb_trim(skb, snaplen))
1658 goto drop_n_acct;
1659
1660 skb_set_owner_r(skb, sk);
1661 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00001662 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Phil Oester84531c22005-07-12 11:57:52 -07001664 /* drop conntrack reference */
1665 nf_reset(skb);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 spin_lock(&sk->sk_receive_queue.lock);
1668 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -07001669 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 __skb_queue_tail(&sk->sk_receive_queue, skb);
1671 spin_unlock(&sk->sk_receive_queue.lock);
1672 sk->sk_data_ready(sk, skb->len);
1673 return 0;
1674
1675drop_n_acct:
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00001676 spin_lock(&sk->sk_receive_queue.lock);
1677 po->stats.tp_drops++;
1678 atomic_inc(&sk->sk_drops);
1679 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681drop_n_restore:
1682 if (skb_head != skb->data && skb_shared(skb)) {
1683 skb->data = skb_head;
1684 skb->len = skb_len;
1685 }
1686drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +00001687 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return 0;
1689}
1690
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00001691static void tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
1692 unsigned int flags)
1693{
1694 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
1695
1696 if (shhwtstamps) {
1697 if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
1698 ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
1699 return;
1700 if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
1701 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
1702 return;
1703 }
1704
1705 if (ktime_to_timespec_cond(skb->tstamp, ts))
1706 return;
1707
1708 getnstimeofday(ts);
1709}
1710
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001711static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
1712 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct sock *sk;
1715 struct packet_sock *po;
1716 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00001717 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001718 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001720 unsigned int snaplen, res;
chetan lokef6fb8f12011-08-19 10:18:16 +00001721 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001722 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001724 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 if (skb->pkt_type == PACKET_LOOPBACK)
1727 goto drop;
1728
1729 sk = pt->af_packet_priv;
1730 po = pkt_sk(sk);
1731
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001732 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001733 goto drop;
1734
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001735 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001737 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 else if (skb->pkt_type == PACKET_OUTGOING) {
1739 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001740 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742 }
1743
Herbert Xu8dc41942007-02-04 23:31:32 -08001744 if (skb->ip_summed == CHECKSUM_PARTIAL)
1745 status |= TP_STATUS_CSUMNOTREADY;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 snaplen = skb->len;
1748
David S. Millerdbcb5852007-01-24 15:21:02 -08001749 res = run_filter(skb, sk, snaplen);
1750 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001751 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001752 if (snaplen > res)
1753 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07001756 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
1757 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00001759 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001760 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07001761 (maclen < 16 ? 16 : maclen)) +
1762 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 macoff = netoff - maclen;
1764 }
chetan lokef6fb8f12011-08-19 10:18:16 +00001765 if (po->tp_version <= TPACKET_V2) {
1766 if (macoff + snaplen > po->rx_ring.frame_size) {
1767 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001768 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001769 if (skb_shared(skb)) {
1770 copy_skb = skb_clone(skb, GFP_ATOMIC);
1771 } else {
1772 copy_skb = skb_get(skb);
1773 skb_head = skb->data;
1774 }
1775 if (copy_skb)
1776 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
chetan lokef6fb8f12011-08-19 10:18:16 +00001778 snaplen = po->rx_ring.frame_size - macoff;
1779 if ((int)snaplen < 0)
1780 snaplen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00001784 h.raw = packet_current_rx_frame(po, skb,
1785 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001786 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto ring_is_full;
chetan lokef6fb8f12011-08-19 10:18:16 +00001788 if (po->tp_version <= TPACKET_V2) {
1789 packet_increment_rx_head(po, &po->rx_ring);
1790 /*
1791 * LOSING will be reported till you read the stats,
1792 * because it's COR - Clear On Read.
1793 * Anyways, moving it for V1/V2 only as V3 doesn't need this
1794 * at packet level.
1795 */
1796 if (po->stats.tp_drops)
1797 status |= TP_STATUS_LOSING;
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 po->stats.tp_packets++;
1800 if (copy_skb) {
1801 status |= TP_STATUS_COPY;
1802 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 spin_unlock(&sk->sk_receive_queue.lock);
1805
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001806 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00001807 tpacket_get_timestamp(skb, &ts, po->tp_tstamp);
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);
Willem de Bruijn2e313962013-04-23 00:39:28 +00001911 __packet_set_timestamp(po, ph, skb->tstamp);
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 Borkmannbf84a012013-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 Dobriyanf0f37e22009-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);