blob: 7b8e39a223879c3cb0a9d2a0bc6ff003118379f9 [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 lokef6fb8f102011-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>
Daniel Borkmannb0138402014-01-15 16:25:36 +010091#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_INET
93#include <net/inet_common.h>
94#endif
Willem de Bruijn47dceb82015-08-14 22:31:34 -040095#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Pavel Emelyanov2787b042012-08-13 05:49:39 +000097#include "internal.h"
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 Assumptions:
101 - if device has no dev->hard_header routine, it adds and removes ll header
102 inside itself. In this case ll header is invisible outside of device,
103 but higher levels still should reserve dev->hard_header_len.
104 Some devices are enough clever to reallocate skb, when header
105 will not fit to reserved space (tunnel), another ones are silly
106 (PPP).
107 - packet socket receives packets with pulled ll header,
108 so that SOCK_RAW should push it back.
109
110On receive:
111-----------
112
113Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700114 mac_header -> ll header
115 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700118 mac_header -> ll header
119 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700122 mac_header -> UNKNOWN position. It is very likely, that it points to ll
123 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900124 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700125 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700128 mac_header -> data. ll header is still not built!
129 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131Resume
132 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
133
134
135On transmit:
136------------
137
138dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700139 mac_header -> ll header
140 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700143 mac_header -> data
144 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 We should set nh.raw on output to correct posistion,
147 packet classifier depends on it.
148 */
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Private packet socket structures. */
151
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700152/* identical to struct packet_mreq except it has
153 * a longer address field.
154 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000155struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700156 int mr_ifindex;
157 unsigned short mr_type;
158 unsigned short mr_alen;
159 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
David S. Millera2efcfa2007-05-29 13:12:50 -0700161
Daniel Borkmann184f4892013-04-16 01:57:46 +0000162union tpacket_uhdr {
163 struct tpacket_hdr *h1;
164 struct tpacket2_hdr *h2;
165 struct tpacket3_hdr *h3;
166 void *raw;
167};
168
chetan lokef6fb8f102011-08-19 10:18:16 +0000169static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700170 int closing, int tx_ring);
171
chetan lokef6fb8f102011-08-19 10:18:16 +0000172#define V3_ALIGNMENT (8)
173
chetan lokebc59ba32011-08-25 10:43:30 +0000174#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f102011-08-19 10:18:16 +0000175
176#define BLK_PLUS_PRIV(sz_of_priv) \
177 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
178
chetan lokef6fb8f102011-08-19 10:18:16 +0000179#define PGV_FROM_VMALLOC 1
Johann Baudy69e3c752009-05-18 22:11:22 -0700180
chetan lokef6fb8f102011-08-19 10:18:16 +0000181#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
182#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
183#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
184#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
185#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
186#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
187#define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
188
Johann Baudy69e3c752009-05-18 22:11:22 -0700189struct packet_sock;
190static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000191static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
192 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
chetan lokef6fb8f102011-08-19 10:18:16 +0000194static void *packet_previous_frame(struct packet_sock *po,
195 struct packet_ring_buffer *rb,
196 int status);
197static void packet_increment_head(struct packet_ring_buffer *buff);
chetan lokebc59ba32011-08-25 10:43:30 +0000198static int prb_curr_blk_in_use(struct tpacket_kbdq_core *,
199 struct tpacket_block_desc *);
200static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f102011-08-19 10:18:16 +0000201 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000202static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f102011-08-19 10:18:16 +0000203 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000204static int prb_queue_frozen(struct tpacket_kbdq_core *);
205static void prb_open_block(struct tpacket_kbdq_core *,
206 struct tpacket_block_desc *);
chetan lokef6fb8f102011-08-19 10:18:16 +0000207static void prb_retire_rx_blk_timer_expired(unsigned long);
chetan lokebc59ba32011-08-25 10:43:30 +0000208static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
209static void prb_init_blk_timer(struct packet_sock *,
210 struct tpacket_kbdq_core *,
211 void (*func) (unsigned long));
212static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
213static void prb_clear_rxhash(struct tpacket_kbdq_core *,
214 struct tpacket3_hdr *);
215static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
216 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void packet_flush_mclist(struct sock *sk);
218
Herbert Xuffbc6112007-02-04 23:33:10 -0800219struct packet_skb_cb {
Herbert Xuffbc6112007-02-04 23:33:10 -0800220 union {
221 struct sockaddr_pkt pkt;
Eyal Birger2472d762015-03-01 14:58:28 +0200222 union {
223 /* Trick: alias skb original length with
224 * ll.sll_family and ll.protocol in order
225 * to save room.
226 */
227 unsigned int origlen;
228 struct sockaddr_ll ll;
229 };
Herbert Xuffbc6112007-02-04 23:33:10 -0800230 } sa;
231};
232
233#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800234
chetan lokebc59ba32011-08-25 10:43:30 +0000235#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f102011-08-19 10:18:16 +0000236#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000237 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f102011-08-19 10:18:16 +0000238#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000239 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f102011-08-19 10:18:16 +0000240#define GET_NEXT_PRB_BLK_NUM(x) \
241 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
242 ((x)->kactive_blk_num+1) : 0)
243
David S. Millerdc99f602011-07-05 01:45:05 -0700244static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
245static void __fanout_link(struct sock *sk, struct packet_sock *po);
246
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100247static int packet_direct_xmit(struct sk_buff *skb)
248{
249 struct net_device *dev = skb->dev;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100250 netdev_features_t features;
251 struct netdev_queue *txq;
Daniel Borkmann43279502014-03-27 16:38:30 +0100252 int ret = NETDEV_TX_BUSY;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100253
254 if (unlikely(!netif_running(dev) ||
Daniel Borkmann43279502014-03-27 16:38:30 +0100255 !netif_carrier_ok(dev)))
256 goto drop;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100257
258 features = netif_skb_features(skb);
259 if (skb_needs_linearize(skb, features) &&
Daniel Borkmann43279502014-03-27 16:38:30 +0100260 __skb_linearize(skb))
261 goto drop;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100262
Daniel Borkmann10c51b56232014-08-27 11:11:27 +0200263 txq = skb_get_tx_queue(dev, skb);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100264
Daniel Borkmann43279502014-03-27 16:38:30 +0100265 local_bh_disable();
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100266
Daniel Borkmann43279502014-03-27 16:38:30 +0100267 HARD_TX_LOCK(dev, txq, smp_processor_id());
David S. Miller10b3ad82014-08-29 21:07:24 -0700268 if (!netif_xmit_frozen_or_drv_stopped(txq))
David S. Millerfa2dbdc2014-08-29 21:55:22 -0700269 ret = netdev_start_xmit(skb, dev, txq, false);
Daniel Borkmann43279502014-03-27 16:38:30 +0100270 HARD_TX_UNLOCK(dev, txq);
271
272 local_bh_enable();
273
274 if (!dev_xmit_complete(ret))
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100275 kfree_skb(skb);
Daniel Borkmann43279502014-03-27 16:38:30 +0100276
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100277 return ret;
Daniel Borkmann43279502014-03-27 16:38:30 +0100278drop:
Daniel Borkmann0f97ede2014-04-02 20:52:56 +0200279 atomic_long_inc(&dev->tx_dropped);
Daniel Borkmann43279502014-03-27 16:38:30 +0100280 kfree_skb(skb);
281 return NET_XMIT_DROP;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100282}
283
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100284static struct net_device *packet_cached_dev_get(struct packet_sock *po)
285{
286 struct net_device *dev;
287
288 rcu_read_lock();
289 dev = rcu_dereference(po->cached_dev);
290 if (likely(dev))
291 dev_hold(dev);
292 rcu_read_unlock();
293
294 return dev;
295}
296
297static void packet_cached_dev_assign(struct packet_sock *po,
298 struct net_device *dev)
299{
300 rcu_assign_pointer(po->cached_dev, dev);
301}
302
303static void packet_cached_dev_reset(struct packet_sock *po)
304{
305 RCU_INIT_POINTER(po->cached_dev, NULL);
306}
307
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100308static bool packet_use_direct_xmit(const struct packet_sock *po)
309{
310 return po->xmit == packet_direct_xmit;
311}
312
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100313static u16 __packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100314{
Li Zhong1cbac012013-12-12 22:39:55 +0100315 return (u16) raw_smp_processor_id() % dev->real_num_tx_queues;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100316}
317
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100318static void packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
319{
320 const struct net_device_ops *ops = dev->netdev_ops;
321 u16 queue_index;
322
323 if (ops->ndo_select_queue) {
324 queue_index = ops->ndo_select_queue(dev, skb, NULL,
325 __packet_pick_tx_queue);
326 queue_index = netdev_cap_txqueue(dev, queue_index);
327 } else {
328 queue_index = __packet_pick_tx_queue(dev, skb);
329 }
330
331 skb_set_queue_mapping(skb, queue_index);
332}
333
David S. Millerce06b032011-07-04 01:44:29 -0700334/* register_prot_hook must be invoked with the po->bind_lock held,
335 * or from a context in which asynchronous accesses to the packet
336 * socket is not possible (packet_create()).
337 */
338static void register_prot_hook(struct sock *sk)
339{
340 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100341
David S. Millerce06b032011-07-04 01:44:29 -0700342 if (!po->running) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100343 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700344 __fanout_link(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100345 else
David S. Millerdc99f602011-07-05 01:45:05 -0700346 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100347
David S. Millerce06b032011-07-04 01:44:29 -0700348 sock_hold(sk);
349 po->running = 1;
350 }
351}
352
353/* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
354 * held. If the sync parameter is true, we will temporarily drop
355 * the po->bind_lock and do a synchronize_net to make sure no
356 * asynchronous packet processing paths still refer to the elements
357 * of po->prot_hook. If the sync parameter is false, it is the
358 * callers responsibility to take care of this.
359 */
360static void __unregister_prot_hook(struct sock *sk, bool sync)
361{
362 struct packet_sock *po = pkt_sk(sk);
363
364 po->running = 0;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100365
366 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700367 __fanout_unlink(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100368 else
David S. Millerdc99f602011-07-05 01:45:05 -0700369 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100370
David S. Millerce06b032011-07-04 01:44:29 -0700371 __sock_put(sk);
372
373 if (sync) {
374 spin_unlock(&po->bind_lock);
375 synchronize_net();
376 spin_lock(&po->bind_lock);
377 }
378}
379
380static void unregister_prot_hook(struct sock *sk, bool sync)
381{
382 struct packet_sock *po = pkt_sk(sk);
383
384 if (po->running)
385 __unregister_prot_hook(sk, sync);
386}
387
Michael S. Tsirkin6e580402014-11-24 13:32:16 +0200388static inline struct page * __pure pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000389{
390 if (is_vmalloc_addr(addr))
391 return vmalloc_to_page(addr);
392 return virt_to_page(addr);
393}
394
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700395static void __packet_set_status(struct packet_sock *po, void *frame, int status)
396{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000397 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700398
399 h.raw = frame;
400 switch (po->tp_version) {
401 case TPACKET_V1:
402 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000403 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700404 break;
405 case TPACKET_V2:
406 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000407 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700408 break;
chetan lokef6fb8f102011-08-19 10:18:16 +0000409 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700410 default:
chetan lokef6fb8f102011-08-19 10:18:16 +0000411 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700412 BUG();
413 }
414
415 smp_wmb();
416}
417
418static int __packet_get_status(struct packet_sock *po, void *frame)
419{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000420 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700421
422 smp_rmb();
423
424 h.raw = frame;
425 switch (po->tp_version) {
426 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000427 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700428 return h.h1->tp_status;
429 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000430 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700431 return h.h2->tp_status;
chetan lokef6fb8f102011-08-19 10:18:16 +0000432 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700433 default:
chetan lokef6fb8f102011-08-19 10:18:16 +0000434 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700435 BUG();
436 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Johann Baudy69e3c752009-05-18 22:11:22 -0700439
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000440static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
441 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000442{
443 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
444
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400445 if (shhwtstamps &&
446 (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
447 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
448 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000449
450 if (ktime_to_timespec_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000451 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000452
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000453 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000454}
455
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000456static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
457 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000458{
459 union tpacket_uhdr h;
460 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000461 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000462
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000463 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
464 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000465
466 h.raw = frame;
467 switch (po->tp_version) {
468 case TPACKET_V1:
469 h.h1->tp_sec = ts.tv_sec;
470 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
471 break;
472 case TPACKET_V2:
473 h.h2->tp_sec = ts.tv_sec;
474 h.h2->tp_nsec = ts.tv_nsec;
475 break;
476 case TPACKET_V3:
477 default:
478 WARN(1, "TPACKET version not supported.\n");
479 BUG();
480 }
481
482 /* one flush is safe, as both fields always lie on the same cacheline */
483 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
484 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000485
486 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000487}
488
Johann Baudy69e3c752009-05-18 22:11:22 -0700489static void *packet_lookup_frame(struct packet_sock *po,
490 struct packet_ring_buffer *rb,
491 unsigned int position,
492 int status)
493{
494 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000495 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700496
497 pg_vec_pos = position / rb->frames_per_block;
498 frame_offset = position % rb->frames_per_block;
499
Neil Horman0e3125c2010-11-16 10:26:47 -0800500 h.raw = rb->pg_vec[pg_vec_pos].buffer +
501 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700502
503 if (status != __packet_get_status(po, h.raw))
504 return NULL;
505
506 return h.raw;
507}
508
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000509static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700510 struct packet_ring_buffer *rb,
511 int status)
512{
513 return packet_lookup_frame(po, rb, rb->head, status);
514}
515
chetan lokebc59ba32011-08-25 10:43:30 +0000516static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000517{
518 del_timer_sync(&pkc->retire_blk_timer);
519}
520
521static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +0000522 struct sk_buff_head *rb_queue)
523{
chetan lokebc59ba32011-08-25 10:43:30 +0000524 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f102011-08-19 10:18:16 +0000525
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200526 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000527
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100528 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f102011-08-19 10:18:16 +0000529 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100530 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f102011-08-19 10:18:16 +0000531
532 prb_del_retire_blk_timer(pkc);
533}
534
535static void prb_init_blk_timer(struct packet_sock *po,
chetan lokebc59ba32011-08-25 10:43:30 +0000536 struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000537 void (*func) (unsigned long))
538{
539 init_timer(&pkc->retire_blk_timer);
540 pkc->retire_blk_timer.data = (long)po;
541 pkc->retire_blk_timer.function = func;
542 pkc->retire_blk_timer.expires = jiffies;
543}
544
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530545static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f102011-08-19 10:18:16 +0000546{
chetan lokebc59ba32011-08-25 10:43:30 +0000547 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f102011-08-19 10:18:16 +0000548
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530549 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000550 prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
551}
552
553static int prb_calc_retire_blk_tmo(struct packet_sock *po,
554 int blk_size_in_bytes)
555{
556 struct net_device *dev;
557 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000558 struct ethtool_cmd ecmd;
559 int err;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000560 u32 speed;
chetan lokef6fb8f102011-08-19 10:18:16 +0000561
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000562 rtnl_lock();
563 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
564 if (unlikely(!dev)) {
565 rtnl_unlock();
chetan lokef6fb8f102011-08-19 10:18:16 +0000566 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000567 }
568 err = __ethtool_get_settings(dev, &ecmd);
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000569 speed = ethtool_cmd_speed(&ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000570 rtnl_unlock();
571 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000572 /*
573 * If the link speed is so slow you don't really
574 * need to worry about perf anyways
575 */
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000576 if (speed < SPEED_1000 || speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000577 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000578 } else {
579 msec = 1;
580 div = speed / 1000;
chetan lokef6fb8f102011-08-19 10:18:16 +0000581 }
582 }
583
584 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
585
586 if (div)
587 mbits /= div;
588
589 tmo = mbits * msec;
590
591 if (div)
592 return tmo+1;
593 return tmo;
594}
595
chetan lokebc59ba32011-08-25 10:43:30 +0000596static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f102011-08-19 10:18:16 +0000597 union tpacket_req_u *req_u)
598{
599 p1->feature_req_word = req_u->req3.tp_feature_req_word;
600}
601
602static void init_prb_bdqc(struct packet_sock *po,
603 struct packet_ring_buffer *rb,
604 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530605 union tpacket_req_u *req_u)
chetan lokef6fb8f102011-08-19 10:18:16 +0000606{
Duan Jiong22781a52013-12-06 13:29:36 +0800607 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000608 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000609
610 memset(p1, 0x0, sizeof(*p1));
611
612 p1->knxt_seq_num = 1;
613 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000614 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000615 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f102011-08-19 10:18:16 +0000616 p1->kblk_size = req_u->req3.tp_block_size;
617 p1->knum_blocks = req_u->req3.tp_block_nr;
618 p1->hdrlen = po->tp_hdrlen;
619 p1->version = po->tp_version;
620 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000621 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f102011-08-19 10:18:16 +0000622 if (req_u->req3.tp_retire_blk_tov)
623 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
624 else
625 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
626 req_u->req3.tp_block_size);
627 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
628 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
629
Eric Dumazetdc808112014-08-15 09:16:04 -0700630 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f102011-08-19 10:18:16 +0000631 prb_init_ft_ops(p1, req_u);
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530632 prb_setup_retire_blk_timer(po);
chetan lokef6fb8f102011-08-19 10:18:16 +0000633 prb_open_block(p1, pbd);
634}
635
636/* Do NOT update the last_blk_num first.
637 * Assumes sk_buff_head lock is held.
638 */
chetan lokebc59ba32011-08-25 10:43:30 +0000639static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000640{
641 mod_timer(&pkc->retire_blk_timer,
642 jiffies + pkc->tov_in_jiffies);
643 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
644}
645
646/*
647 * Timer logic:
648 * 1) We refresh the timer only when we open a block.
649 * By doing this we don't waste cycles refreshing the timer
650 * on packet-by-packet basis.
651 *
652 * With a 1MB block-size, on a 1Gbps line, it will take
653 * i) ~8 ms to fill a block + ii) memcpy etc.
654 * In this cut we are not accounting for the memcpy time.
655 *
656 * So, if the user sets the 'tmo' to 10ms then the timer
657 * will never fire while the block is still getting filled
658 * (which is what we want). However, the user could choose
659 * to close a block early and that's fine.
660 *
661 * But when the timer does fire, we check whether or not to refresh it.
662 * Since the tmo granularity is in msecs, it is not too expensive
663 * to refresh the timer, lets say every '8' msecs.
664 * Either the user can set the 'tmo' or we can derive it based on
665 * a) line-speed and b) block-size.
666 * prb_calc_retire_blk_tmo() calculates the tmo.
667 *
668 */
669static void prb_retire_rx_blk_timer_expired(unsigned long data)
670{
671 struct packet_sock *po = (struct packet_sock *)data;
Duan Jiong22781a52013-12-06 13:29:36 +0800672 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000673 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000674 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000675
676 spin_lock(&po->sk.sk_receive_queue.lock);
677
678 frozen = prb_queue_frozen(pkc);
679 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
680
681 if (unlikely(pkc->delete_blk_timer))
682 goto out;
683
684 /* We only need to plug the race when the block is partially filled.
685 * tpacket_rcv:
686 * lock(); increment BLOCK_NUM_PKTS; unlock()
687 * copy_bits() is in progress ...
688 * timer fires on other cpu:
689 * we can't retire the current block because copy_bits
690 * is in progress.
691 *
692 */
693 if (BLOCK_NUM_PKTS(pbd)) {
694 while (atomic_read(&pkc->blk_fill_in_prog)) {
695 /* Waiting for skb_copy_bits to finish... */
696 cpu_relax();
697 }
698 }
699
700 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
701 if (!frozen) {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300702 if (!BLOCK_NUM_PKTS(pbd)) {
703 /* An empty block. Just refresh the timer. */
704 goto refresh_timer;
705 }
chetan lokef6fb8f102011-08-19 10:18:16 +0000706 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
707 if (!prb_dispatch_next_block(pkc, po))
708 goto refresh_timer;
709 else
710 goto out;
711 } else {
712 /* Case 1. Queue was frozen because user-space was
713 * lagging behind.
714 */
715 if (prb_curr_blk_in_use(pkc, pbd)) {
716 /*
717 * Ok, user-space is still behind.
718 * So just refresh the timer.
719 */
720 goto refresh_timer;
721 } else {
722 /* Case 2. queue was frozen,user-space caught up,
723 * now the link went idle && the timer fired.
724 * We don't have a block to close.So we open this
725 * block and restart the timer.
726 * opening a block thaws the queue,restarts timer
727 * Thawing/timer-refresh is a side effect.
728 */
729 prb_open_block(pkc, pbd);
730 goto out;
731 }
732 }
733 }
734
735refresh_timer:
736 _prb_refresh_rx_retire_blk_timer(pkc);
737
738out:
739 spin_unlock(&po->sk.sk_receive_queue.lock);
740}
741
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000742static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000743 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f102011-08-19 10:18:16 +0000744{
745 /* Flush everything minus the block header */
746
747#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
748 u8 *start, *end;
749
750 start = (u8 *)pbd1;
751
752 /* Skip the block header(we know header WILL fit in 4K) */
753 start += PAGE_SIZE;
754
755 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
756 for (; start < end; start += PAGE_SIZE)
757 flush_dcache_page(pgv_to_page(start));
758
759 smp_wmb();
760#endif
761
762 /* Now update the block status. */
763
764 BLOCK_STATUS(pbd1) = status;
765
766 /* Flush the block header */
767
768#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
769 start = (u8 *)pbd1;
770 flush_dcache_page(pgv_to_page(start));
771
772 smp_wmb();
773#endif
774}
775
776/*
777 * Side effect:
778 *
779 * 1) flush the block
780 * 2) Increment active_blk_num
781 *
782 * Note:We DONT refresh the timer on purpose.
783 * Because almost always the next block will be opened.
784 */
chetan lokebc59ba32011-08-25 10:43:30 +0000785static void prb_close_block(struct tpacket_kbdq_core *pkc1,
786 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f102011-08-19 10:18:16 +0000787 struct packet_sock *po, unsigned int stat)
788{
789 __u32 status = TP_STATUS_USER | stat;
790
791 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000792 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
Dan Collinsda413ee2014-12-19 16:49:25 +1300793 struct sock *sk = &po->sk;
chetan lokef6fb8f102011-08-19 10:18:16 +0000794
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000795 if (po->stats.stats3.tp_drops)
chetan lokef6fb8f102011-08-19 10:18:16 +0000796 status |= TP_STATUS_LOSING;
797
798 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
799 last_pkt->tp_next_offset = 0;
800
801 /* Get the ts of the last pkt */
802 if (BLOCK_NUM_PKTS(pbd1)) {
803 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
804 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
805 } else {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300806 /* Ok, we tmo'd - so get the current time.
807 *
808 * It shouldn't really happen as we don't close empty
809 * blocks. See prb_retire_rx_blk_timer_expired().
810 */
chetan lokef6fb8f102011-08-19 10:18:16 +0000811 struct timespec ts;
812 getnstimeofday(&ts);
813 h1->ts_last_pkt.ts_sec = ts.tv_sec;
814 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
815 }
816
817 smp_wmb();
818
819 /* Flush the block */
820 prb_flush_block(pkc1, pbd1, status);
821
Dan Collinsda413ee2014-12-19 16:49:25 +1300822 sk->sk_data_ready(sk);
823
chetan lokef6fb8f102011-08-19 10:18:16 +0000824 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
825}
826
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000827static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000828{
829 pkc->reset_pending_on_curr_blk = 0;
830}
831
832/*
833 * Side effect of opening a block:
834 *
835 * 1) prb_queue is thawed.
836 * 2) retire_blk_timer is refreshed.
837 *
838 */
chetan lokebc59ba32011-08-25 10:43:30 +0000839static void prb_open_block(struct tpacket_kbdq_core *pkc1,
840 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f102011-08-19 10:18:16 +0000841{
842 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000843 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f102011-08-19 10:18:16 +0000844
845 smp_rmb();
846
Daniel Borkmann8da30562013-05-03 02:57:00 +0000847 /* We could have just memset this but we will lose the
848 * flexibility of making the priv area sticky
849 */
chetan lokef6fb8f102011-08-19 10:18:16 +0000850
Daniel Borkmann8da30562013-05-03 02:57:00 +0000851 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
852 BLOCK_NUM_PKTS(pbd1) = 0;
853 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f102011-08-19 10:18:16 +0000854
Daniel Borkmann8da30562013-05-03 02:57:00 +0000855 getnstimeofday(&ts);
chetan lokef6fb8f102011-08-19 10:18:16 +0000856
Daniel Borkmann8da30562013-05-03 02:57:00 +0000857 h1->ts_first_pkt.ts_sec = ts.tv_sec;
858 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f102011-08-19 10:18:16 +0000859
Daniel Borkmann8da30562013-05-03 02:57:00 +0000860 pkc1->pkblk_start = (char *)pbd1;
861 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
862
863 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
864 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
865
866 pbd1->version = pkc1->version;
867 pkc1->prev = pkc1->nxt_offset;
868 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
869
870 prb_thaw_queue(pkc1);
871 _prb_refresh_rx_retire_blk_timer(pkc1);
872
873 smp_wmb();
chetan lokef6fb8f102011-08-19 10:18:16 +0000874}
875
876/*
877 * Queue freeze logic:
878 * 1) Assume tp_block_nr = 8 blocks.
879 * 2) At time 't0', user opens Rx ring.
880 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
881 * 4) user-space is either sleeping or processing block '0'.
882 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
883 * it will close block-7,loop around and try to fill block '0'.
884 * call-flow:
885 * __packet_lookup_frame_in_block
886 * prb_retire_current_block()
887 * prb_dispatch_next_block()
888 * |->(BLOCK_STATUS == USER) evaluates to true
889 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
890 * 6) Now there are two cases:
891 * 6.1) Link goes idle right after the queue is frozen.
892 * But remember, the last open_block() refreshed the timer.
893 * When this timer expires,it will refresh itself so that we can
894 * re-open block-0 in near future.
895 * 6.2) Link is busy and keeps on receiving packets. This is a simple
896 * case and __packet_lookup_frame_in_block will check if block-0
897 * is free and can now be re-used.
898 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000899static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000900 struct packet_sock *po)
901{
902 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000903 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f102011-08-19 10:18:16 +0000904}
905
906#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
907
908/*
909 * If the next block is free then we will dispatch it
910 * and return a good offset.
911 * Else, we will freeze the queue.
912 * So, caller must check the return value.
913 */
chetan lokebc59ba32011-08-25 10:43:30 +0000914static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000915 struct packet_sock *po)
916{
chetan lokebc59ba32011-08-25 10:43:30 +0000917 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000918
919 smp_rmb();
920
921 /* 1. Get current block num */
922 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
923
924 /* 2. If this block is currently in_use then freeze the queue */
925 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
926 prb_freeze_queue(pkc, po);
927 return NULL;
928 }
929
930 /*
931 * 3.
932 * open this block and return the offset where the first packet
933 * needs to get stored.
934 */
935 prb_open_block(pkc, pbd);
936 return (void *)pkc->nxt_offset;
937}
938
chetan lokebc59ba32011-08-25 10:43:30 +0000939static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000940 struct packet_sock *po, unsigned int status)
941{
chetan lokebc59ba32011-08-25 10:43:30 +0000942 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f102011-08-19 10:18:16 +0000943
944 /* retire/close the current block */
945 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
946 /*
947 * Plug the case where copy_bits() is in progress on
948 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
949 * have space to copy the pkt in the current block and
950 * called prb_retire_current_block()
951 *
952 * We don't need to worry about the TMO case because
953 * the timer-handler already handled this case.
954 */
955 if (!(status & TP_STATUS_BLK_TMO)) {
956 while (atomic_read(&pkc->blk_fill_in_prog)) {
957 /* Waiting for skb_copy_bits to finish... */
958 cpu_relax();
959 }
960 }
961 prb_close_block(pkc, pbd, po, status);
962 return;
963 }
chetan lokef6fb8f102011-08-19 10:18:16 +0000964}
965
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000966static int prb_curr_blk_in_use(struct tpacket_kbdq_core *pkc,
chetan lokebc59ba32011-08-25 10:43:30 +0000967 struct tpacket_block_desc *pbd)
chetan lokef6fb8f102011-08-19 10:18:16 +0000968{
969 return TP_STATUS_USER & BLOCK_STATUS(pbd);
970}
971
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000972static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000973{
974 return pkc->reset_pending_on_curr_blk;
975}
976
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000977static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f102011-08-19 10:18:16 +0000978{
chetan lokebc59ba32011-08-25 10:43:30 +0000979 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f102011-08-19 10:18:16 +0000980 atomic_dec(&pkc->blk_fill_in_prog);
981}
982
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000983static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000984 struct tpacket3_hdr *ppd)
985{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800986 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
chetan lokef6fb8f102011-08-19 10:18:16 +0000987}
988
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000989static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000990 struct tpacket3_hdr *ppd)
991{
992 ppd->hv1.tp_rxhash = 0;
993}
994
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000995static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000996 struct tpacket3_hdr *ppd)
997{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100998 if (skb_vlan_tag_present(pkc->skb)) {
999 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09001000 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
1001 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
chetan lokef6fb8f102011-08-19 10:18:16 +00001002 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00001003 ppd->hv1.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09001004 ppd->hv1.tp_vlan_tpid = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00001005 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f102011-08-19 10:18:16 +00001006 }
1007}
1008
chetan lokebc59ba32011-08-25 10:43:30 +00001009static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +00001010 struct tpacket3_hdr *ppd)
1011{
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09001012 ppd->hv1.tp_padding = 0;
chetan lokef6fb8f102011-08-19 10:18:16 +00001013 prb_fill_vlan_info(pkc, ppd);
1014
1015 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
1016 prb_fill_rxhash(pkc, ppd);
1017 else
1018 prb_clear_rxhash(pkc, ppd);
1019}
1020
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001021static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +00001022 struct tpacket_kbdq_core *pkc,
1023 struct tpacket_block_desc *pbd,
chetan lokef6fb8f102011-08-19 10:18:16 +00001024 unsigned int len)
1025{
1026 struct tpacket3_hdr *ppd;
1027
1028 ppd = (struct tpacket3_hdr *)curr;
1029 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
1030 pkc->prev = curr;
1031 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
1032 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1033 BLOCK_NUM_PKTS(pbd) += 1;
1034 atomic_inc(&pkc->blk_fill_in_prog);
1035 prb_run_all_ft_ops(pkc, ppd);
1036}
1037
1038/* Assumes caller has the sk->rx_queue.lock */
1039static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1040 struct sk_buff *skb,
1041 int status,
1042 unsigned int len
1043 )
1044{
chetan lokebc59ba32011-08-25 10:43:30 +00001045 struct tpacket_kbdq_core *pkc;
1046 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +00001047 char *curr, *end;
1048
Joe Perchese3192692012-06-03 17:41:40 +00001049 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +00001050 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1051
1052 /* Queue is frozen when user space is lagging behind */
1053 if (prb_queue_frozen(pkc)) {
1054 /*
1055 * Check if that last block which caused the queue to freeze,
1056 * is still in_use by user-space.
1057 */
1058 if (prb_curr_blk_in_use(pkc, pbd)) {
1059 /* Can't record this packet */
1060 return NULL;
1061 } else {
1062 /*
1063 * Ok, the block was released by user-space.
1064 * Now let's open that block.
1065 * opening a block also thaws the queue.
1066 * Thawing is a side effect.
1067 */
1068 prb_open_block(pkc, pbd);
1069 }
1070 }
1071
1072 smp_mb();
1073 curr = pkc->nxt_offset;
1074 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +00001075 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f102011-08-19 10:18:16 +00001076
1077 /* first try the current block */
1078 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1079 prb_fill_curr_block(curr, pkc, pbd, len);
1080 return (void *)curr;
1081 }
1082
1083 /* Ok, close the current block */
1084 prb_retire_current_block(pkc, po, 0);
1085
1086 /* Now, try to dispatch the next block */
1087 curr = (char *)prb_dispatch_next_block(pkc, po);
1088 if (curr) {
1089 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1090 prb_fill_curr_block(curr, pkc, pbd, len);
1091 return (void *)curr;
1092 }
1093
1094 /*
1095 * No free blocks are available.user_space hasn't caught up yet.
1096 * Queue was just frozen and now this packet will get dropped.
1097 */
1098 return NULL;
1099}
1100
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001101static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001102 struct sk_buff *skb,
1103 int status, unsigned int len)
1104{
1105 char *curr = NULL;
1106 switch (po->tp_version) {
1107 case TPACKET_V1:
1108 case TPACKET_V2:
1109 curr = packet_lookup_frame(po, &po->rx_ring,
1110 po->rx_ring.head, status);
1111 return curr;
1112 case TPACKET_V3:
1113 return __packet_lookup_frame_in_block(po, skb, status, len);
1114 default:
1115 WARN(1, "TPACKET version not supported\n");
1116 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001117 return NULL;
chetan lokef6fb8f102011-08-19 10:18:16 +00001118 }
1119}
1120
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001121static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001122 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001123 unsigned int idx,
chetan lokef6fb8f102011-08-19 10:18:16 +00001124 int status)
1125{
chetan lokebc59ba32011-08-25 10:43:30 +00001126 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001127 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f102011-08-19 10:18:16 +00001128
1129 if (status != BLOCK_STATUS(pbd))
1130 return NULL;
1131 return pbd;
1132}
1133
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001134static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f102011-08-19 10:18:16 +00001135{
1136 unsigned int prev;
1137 if (rb->prb_bdqc.kactive_blk_num)
1138 prev = rb->prb_bdqc.kactive_blk_num-1;
1139 else
1140 prev = rb->prb_bdqc.knum_blocks-1;
1141 return prev;
1142}
1143
1144/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001145static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001146 struct packet_ring_buffer *rb,
1147 int status)
1148{
1149 unsigned int previous = prb_previous_blk_num(rb);
1150 return prb_lookup_block(po, rb, previous, status);
1151}
1152
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001153static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001154 struct packet_ring_buffer *rb,
1155 int status)
1156{
1157 if (po->tp_version <= TPACKET_V2)
1158 return packet_previous_frame(po, rb, status);
1159
1160 return __prb_previous_block(po, rb, status);
1161}
1162
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001163static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001164 struct packet_ring_buffer *rb)
1165{
1166 switch (po->tp_version) {
1167 case TPACKET_V1:
1168 case TPACKET_V2:
1169 return packet_increment_head(rb);
1170 case TPACKET_V3:
1171 default:
1172 WARN(1, "TPACKET version not supported.\n");
1173 BUG();
1174 return;
1175 }
1176}
1177
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001178static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001179 struct packet_ring_buffer *rb,
1180 int status)
1181{
1182 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1183 return packet_lookup_frame(po, rb, previous, status);
1184}
1185
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001186static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001187{
1188 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1189}
1190
Daniel Borkmannb0138402014-01-15 16:25:36 +01001191static void packet_inc_pending(struct packet_ring_buffer *rb)
1192{
1193 this_cpu_inc(*rb->pending_refcnt);
1194}
1195
1196static void packet_dec_pending(struct packet_ring_buffer *rb)
1197{
1198 this_cpu_dec(*rb->pending_refcnt);
1199}
1200
1201static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1202{
1203 unsigned int refcnt = 0;
1204 int cpu;
1205
1206 /* We don't use pending refcount in rx_ring. */
1207 if (rb->pending_refcnt == NULL)
1208 return 0;
1209
1210 for_each_possible_cpu(cpu)
1211 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1212
1213 return refcnt;
1214}
1215
1216static int packet_alloc_pending(struct packet_sock *po)
1217{
1218 po->rx_ring.pending_refcnt = NULL;
1219
1220 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1221 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1222 return -ENOBUFS;
1223
1224 return 0;
1225}
1226
1227static void packet_free_pending(struct packet_sock *po)
1228{
1229 free_percpu(po->tx_ring.pending_refcnt);
1230}
1231
Willem de Bruijn99547292015-05-12 11:56:47 -04001232#define ROOM_POW_OFF 2
1233#define ROOM_NONE 0x0
1234#define ROOM_LOW 0x1
1235#define ROOM_NORMAL 0x2
1236
1237static bool __tpacket_has_room(struct packet_sock *po, int pow_off)
1238{
1239 int idx, len;
1240
1241 len = po->rx_ring.frame_max + 1;
1242 idx = po->rx_ring.head;
1243 if (pow_off)
1244 idx += len >> pow_off;
1245 if (idx >= len)
1246 idx -= len;
1247 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1248}
1249
1250static bool __tpacket_v3_has_room(struct packet_sock *po, int pow_off)
1251{
1252 int idx, len;
1253
1254 len = po->rx_ring.prb_bdqc.knum_blocks;
1255 idx = po->rx_ring.prb_bdqc.kactive_blk_num;
1256 if (pow_off)
1257 idx += len >> pow_off;
1258 if (idx >= len)
1259 idx -= len;
1260 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1261}
1262
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001263static int __packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001264{
1265 struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001266 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001267
Willem de Bruijn99547292015-05-12 11:56:47 -04001268 if (po->prot_hook.func != tpacket_rcv) {
1269 int avail = sk->sk_rcvbuf - atomic_read(&sk->sk_rmem_alloc)
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001270 - (skb ? skb->truesize : 0);
Willem de Bruijn99547292015-05-12 11:56:47 -04001271 if (avail > (sk->sk_rcvbuf >> ROOM_POW_OFF))
1272 return ROOM_NORMAL;
1273 else if (avail > 0)
1274 return ROOM_LOW;
1275 else
1276 return ROOM_NONE;
1277 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001278
Willem de Bruijn99547292015-05-12 11:56:47 -04001279 if (po->tp_version == TPACKET_V3) {
1280 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1281 ret = ROOM_NORMAL;
1282 else if (__tpacket_v3_has_room(po, 0))
1283 ret = ROOM_LOW;
1284 } else {
1285 if (__tpacket_has_room(po, ROOM_POW_OFF))
1286 ret = ROOM_NORMAL;
1287 else if (__tpacket_has_room(po, 0))
1288 ret = ROOM_LOW;
1289 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001290
1291 return ret;
1292}
1293
1294static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1295{
1296 int ret;
1297 bool has_room;
1298
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001299 spin_lock_bh(&po->sk.sk_receive_queue.lock);
1300 ret = __packet_rcv_has_room(po, skb);
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001301 has_room = ret == ROOM_NORMAL;
1302 if (po->pressure == has_room)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001303 po->pressure = !has_room;
1304 spin_unlock_bh(&po->sk.sk_receive_queue.lock);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001305
Willem de Bruijn99547292015-05-12 11:56:47 -04001306 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309static void packet_sock_destruct(struct sock *sk)
1310{
Richard Cochraned85b562010-04-07 22:41:28 +00001311 skb_queue_purge(&sk->sk_error_queue);
1312
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001313 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
1314 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001317 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return;
1319 }
1320
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001321 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001324static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1325{
1326 u32 rxhash;
1327 int i, count = 0;
1328
1329 rxhash = skb_get_hash(skb);
1330 for (i = 0; i < ROLLOVER_HLEN; i++)
1331 if (po->rollover->history[i] == rxhash)
1332 count++;
1333
1334 po->rollover->history[prandom_u32() % ROLLOVER_HLEN] = rxhash;
1335 return count > (ROLLOVER_HLEN >> 1);
1336}
1337
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001338static unsigned int fanout_demux_hash(struct packet_fanout *f,
1339 struct sk_buff *skb,
1340 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001341{
Tom Herbert61b905d2014-03-24 15:34:47 -07001342 return reciprocal_scale(skb_get_hash(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001343}
1344
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001345static unsigned int fanout_demux_lb(struct packet_fanout *f,
1346 struct sk_buff *skb,
1347 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001348{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001349 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001350
Willem de Bruijn468479e2015-06-17 15:59:34 -04001351 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001352}
1353
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001354static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1355 struct sk_buff *skb,
1356 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001357{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001358 return smp_processor_id() % num;
1359}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001360
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001361static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1362 struct sk_buff *skb,
1363 unsigned int num)
1364{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001365 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001366}
1367
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001368static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1369 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001370 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001371 unsigned int num)
1372{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001373 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001374 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001375
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001376 po = pkt_sk(f->arr[idx]);
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001377
1378 if (try_self) {
1379 room = packet_rcv_has_room(po, skb);
1380 if (room == ROOM_NORMAL ||
1381 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1382 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001383 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001384 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001385
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001386 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001387 do {
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001388 po_next = pkt_sk(f->arr[i]);
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001389 if (po_next != po_skip && !po_next->pressure &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001390 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001391 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001392 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001393 atomic_long_inc(&po->rollover->num);
1394 if (room == ROOM_LOW)
1395 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001396 return i;
1397 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001398
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001399 if (++i == num)
1400 i = 0;
1401 } while (i != j);
1402
Willem de Bruijna9b63912015-05-12 11:56:50 -04001403 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001404 return idx;
1405}
1406
Neil Horman2d360972014-01-22 16:01:44 -05001407static unsigned int fanout_demux_qm(struct packet_fanout *f,
1408 struct sk_buff *skb,
1409 unsigned int num)
1410{
1411 return skb_get_queue_mapping(skb) % num;
1412}
1413
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001414static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1415 struct sk_buff *skb,
1416 unsigned int num)
1417{
1418 struct bpf_prog *prog;
1419 unsigned int ret = 0;
1420
1421 rcu_read_lock();
1422 prog = rcu_dereference(f->bpf_prog);
1423 if (prog)
1424 ret = BPF_PROG_RUN(prog, skb) % num;
1425 rcu_read_unlock();
1426
1427 return ret;
1428}
1429
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001430static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1431{
1432 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001433}
1434
David S. Miller95ec3eb2011-07-06 01:56:38 -07001435static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1436 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001437{
1438 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001439 unsigned int num = READ_ONCE(f->num_members);
David S. Millerdc99f602011-07-05 01:45:05 -07001440 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001441 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001442
1443 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
1444 !num) {
1445 kfree_skb(skb);
1446 return 0;
1447 }
1448
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001449 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
1450 skb = ip_check_defrag(skb, IP_DEFRAG_AF_PACKET);
1451 if (!skb)
1452 return 0;
1453 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001454 switch (f->type) {
1455 case PACKET_FANOUT_HASH:
1456 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001457 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001458 break;
1459 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001460 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001461 break;
1462 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001463 idx = fanout_demux_cpu(f, skb, num);
1464 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001465 case PACKET_FANOUT_RND:
1466 idx = fanout_demux_rnd(f, skb, num);
1467 break;
Neil Horman2d360972014-01-22 16:01:44 -05001468 case PACKET_FANOUT_QM:
1469 idx = fanout_demux_qm(f, skb, num);
1470 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001471 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001472 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001473 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001474 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001475 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001476 idx = fanout_demux_bpf(f, skb, num);
1477 break;
David S. Miller7736d332011-07-05 01:43:20 -07001478 }
1479
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001480 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1481 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001482
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001483 po = pkt_sk(f->arr[idx]);
David S. Millerdc99f602011-07-05 01:45:05 -07001484 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1485}
1486
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001487DEFINE_MUTEX(fanout_mutex);
1488EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001489static LIST_HEAD(fanout_list);
1490
1491static void __fanout_link(struct sock *sk, struct packet_sock *po)
1492{
1493 struct packet_fanout *f = po->fanout;
1494
1495 spin_lock(&f->lock);
1496 f->arr[f->num_members] = sk;
1497 smp_wmb();
1498 f->num_members++;
1499 spin_unlock(&f->lock);
1500}
1501
1502static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1503{
1504 struct packet_fanout *f = po->fanout;
1505 int i;
1506
1507 spin_lock(&f->lock);
1508 for (i = 0; i < f->num_members; i++) {
1509 if (f->arr[i] == sk)
1510 break;
1511 }
1512 BUG_ON(i >= f->num_members);
1513 f->arr[i] = f->arr[f->num_members - 1];
1514 f->num_members--;
1515 spin_unlock(&f->lock);
1516}
1517
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001518static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001519{
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001520 if (ptype->af_packet_priv == (void *)((struct packet_sock *)sk)->fanout)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001521 return true;
1522
1523 return false;
1524}
1525
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001526static void fanout_init_data(struct packet_fanout *f)
1527{
1528 switch (f->type) {
1529 case PACKET_FANOUT_LB:
1530 atomic_set(&f->rr_cur, 0);
1531 break;
1532 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001533 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001534 RCU_INIT_POINTER(f->bpf_prog, NULL);
1535 break;
1536 }
1537}
1538
1539static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1540{
1541 struct bpf_prog *old;
1542
1543 spin_lock(&f->lock);
1544 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1545 rcu_assign_pointer(f->bpf_prog, new);
1546 spin_unlock(&f->lock);
1547
1548 if (old) {
1549 synchronize_net();
1550 bpf_prog_destroy(old);
1551 }
1552}
1553
1554static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1555 unsigned int len)
1556{
1557 struct bpf_prog *new;
1558 struct sock_fprog fprog;
1559 int ret;
1560
1561 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1562 return -EPERM;
1563 if (len != sizeof(fprog))
1564 return -EINVAL;
1565 if (copy_from_user(&fprog, data, len))
1566 return -EFAULT;
1567
1568 ret = bpf_prog_create_from_user(&new, &fprog, NULL);
1569 if (ret)
1570 return ret;
1571
1572 __fanout_set_data_bpf(po->fanout, new);
1573 return 0;
1574}
1575
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001576static int fanout_set_data_ebpf(struct packet_sock *po, char __user *data,
1577 unsigned int len)
1578{
1579 struct bpf_prog *new;
1580 u32 fd;
1581
1582 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1583 return -EPERM;
1584 if (len != sizeof(fd))
1585 return -EINVAL;
1586 if (copy_from_user(&fd, data, len))
1587 return -EFAULT;
1588
1589 new = bpf_prog_get(fd);
1590 if (IS_ERR(new))
1591 return PTR_ERR(new);
1592 if (new->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1593 bpf_prog_put(new);
1594 return -EINVAL;
1595 }
1596
1597 __fanout_set_data_bpf(po->fanout, new);
1598 return 0;
1599}
1600
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001601static int fanout_set_data(struct packet_sock *po, char __user *data,
1602 unsigned int len)
1603{
1604 switch (po->fanout->type) {
1605 case PACKET_FANOUT_CBPF:
1606 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001607 case PACKET_FANOUT_EBPF:
1608 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001609 default:
1610 return -EINVAL;
1611 };
1612}
1613
1614static void fanout_release_data(struct packet_fanout *f)
1615{
1616 switch (f->type) {
1617 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001618 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001619 __fanout_set_data_bpf(f, NULL);
1620 };
1621}
1622
David S. Miller7736d332011-07-05 01:43:20 -07001623static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001624{
1625 struct packet_sock *po = pkt_sk(sk);
1626 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001627 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001628 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001629 int err;
1630
1631 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001632 case PACKET_FANOUT_ROLLOVER:
1633 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1634 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001635 case PACKET_FANOUT_HASH:
1636 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001637 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001638 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001639 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001640 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001641 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001642 break;
1643 default:
1644 return -EINVAL;
1645 }
1646
1647 if (!po->running)
1648 return -EINVAL;
1649
1650 if (po->fanout)
1651 return -EALREADY;
1652
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001653 if (type == PACKET_FANOUT_ROLLOVER ||
1654 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001655 po->rollover = kzalloc(sizeof(*po->rollover), GFP_KERNEL);
1656 if (!po->rollover)
1657 return -ENOMEM;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001658 atomic_long_set(&po->rollover->num, 0);
1659 atomic_long_set(&po->rollover->num_huge, 0);
1660 atomic_long_set(&po->rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001661 }
1662
David S. Millerdc99f602011-07-05 01:45:05 -07001663 mutex_lock(&fanout_mutex);
1664 match = NULL;
1665 list_for_each_entry(f, &fanout_list, list) {
1666 if (f->id == id &&
1667 read_pnet(&f->net) == sock_net(sk)) {
1668 match = f;
1669 break;
1670 }
1671 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001672 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001673 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001674 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001675 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001676 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001677 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001678 if (!match)
1679 goto out;
1680 write_pnet(&match->net, sock_net(sk));
1681 match->id = id;
1682 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001683 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001684 INIT_LIST_HEAD(&match->list);
1685 spin_lock_init(&match->lock);
1686 atomic_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001687 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001688 match->prot_hook.type = po->prot_hook.type;
1689 match->prot_hook.dev = po->prot_hook.dev;
1690 match->prot_hook.func = packet_rcv_fanout;
1691 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001692 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001693 dev_add_pack(&match->prot_hook);
1694 list_add(&match->list, &fanout_list);
1695 }
1696 err = -EINVAL;
1697 if (match->type == type &&
1698 match->prot_hook.type == po->prot_hook.type &&
1699 match->prot_hook.dev == po->prot_hook.dev) {
1700 err = -ENOSPC;
1701 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1702 __dev_remove_pack(&po->prot_hook);
1703 po->fanout = match;
1704 atomic_inc(&match->sk_ref);
1705 __fanout_link(sk, po);
1706 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001707 }
1708 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001709out:
David S. Millerdc99f602011-07-05 01:45:05 -07001710 mutex_unlock(&fanout_mutex);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001711 if (err) {
1712 kfree(po->rollover);
1713 po->rollover = NULL;
1714 }
David S. Millerdc99f602011-07-05 01:45:05 -07001715 return err;
1716}
1717
1718static void fanout_release(struct sock *sk)
1719{
1720 struct packet_sock *po = pkt_sk(sk);
1721 struct packet_fanout *f;
1722
1723 f = po->fanout;
1724 if (!f)
1725 return;
1726
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001727 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001728 po->fanout = NULL;
1729
David S. Millerdc99f602011-07-05 01:45:05 -07001730 if (atomic_dec_and_test(&f->sk_ref)) {
1731 list_del(&f->list);
1732 dev_remove_pack(&f->prot_hook);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001733 fanout_release_data(f);
David S. Millerdc99f602011-07-05 01:45:05 -07001734 kfree(f);
1735 }
1736 mutex_unlock(&fanout_mutex);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001737
Willem de Bruijn59f21112015-06-16 12:51:37 -04001738 if (po->rollover)
1739 kfree_rcu(po->rollover, rcu);
David S. Millerdc99f602011-07-05 01:45:05 -07001740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001742static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001744static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001746static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1747 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 struct sock *sk;
1750 struct sockaddr_pkt *spkt;
1751
1752 /*
1753 * When we registered the protocol we saved the socket in the data
1754 * field for just this event.
1755 */
1756
1757 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /*
1760 * Yank back the headers [hope the device set this
1761 * right or kerboom...]
1762 *
1763 * Incoming packets have ll header pulled,
1764 * push it back.
1765 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001766 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * so that this procedure is noop.
1768 */
1769
1770 if (skb->pkt_type == PACKET_LOOPBACK)
1771 goto out;
1772
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001773 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001774 goto out;
1775
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001776 skb = skb_share_check(skb, GFP_ATOMIC);
1777 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto oom;
1779
1780 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001781 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Phil Oester84531c22005-07-12 11:57:52 -07001783 /* drop conntrack reference */
1784 nf_reset(skb);
1785
Herbert Xuffbc6112007-02-04 23:33:10 -08001786 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001788 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 /*
1791 * The SOCK_PACKET socket receives _all_ frames.
1792 */
1793
1794 spkt->spkt_family = dev->type;
1795 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1796 spkt->spkt_protocol = skb->protocol;
1797
1798 /*
1799 * Charge the memory to the socket. This is done specifically
1800 * to prevent sockets using all the memory up.
1801 */
1802
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001803 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return 0;
1805
1806out:
1807 kfree_skb(skb);
1808oom:
1809 return 0;
1810}
1811
1812
1813/*
1814 * Output a raw packet to a device layer. This bypasses all the other
1815 * protocol layers and you must therefore supply it with a complete frame
1816 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001817
Ying Xue1b784142015-03-02 15:37:48 +08001818static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1819 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001822 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001823 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001825 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001827 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001830 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
1832
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001833 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001835 return -EINVAL;
1836 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1837 proto = saddr->spkt_protocol;
1838 } else
1839 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001842 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
1844
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001845 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001846retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001847 rcu_read_lock();
1848 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 err = -ENODEV;
1850 if (dev == NULL)
1851 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001852
David S. Millerd5e76b02007-01-25 19:30:36 -08001853 err = -ENETDOWN;
1854 if (!(dev->flags & IFF_UP))
1855 goto out_unlock;
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001858 * You may not queue a frame bigger than the mtu. This is the lowest level
1859 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001861
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001862 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1863 if (!netif_supports_nofcs(dev)) {
1864 err = -EPROTONOSUPPORT;
1865 goto out_unlock;
1866 }
1867 extra_len = 4; /* We're doing our own CRC */
1868 }
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001871 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 goto out_unlock;
1873
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001874 if (!skb) {
1875 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001876 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001877 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001879 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001880 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001881 if (skb == NULL)
1882 return -ENOBUFS;
1883 /* FIXME: Save some space for broken drivers that write a hard
1884 * header at transmission time by themselves. PPP is the notable
1885 * one here. This should really be fixed at the driver level.
1886 */
1887 skb_reserve(skb, reserved);
1888 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001889
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001890 /* Try to align data part correctly */
1891 if (hhlen) {
1892 skb->data -= hhlen;
1893 skb->tail -= hhlen;
1894 if (len < hhlen)
1895 skb_reset_network_header(skb);
1896 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001897 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001898 if (err)
1899 goto out_free;
1900 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001903 if (len > (dev->mtu + dev->hard_header_len + extra_len)) {
Ben Greear57f89bf2011-02-11 09:35:18 +00001904 /* Earlier code assumed this would be a VLAN pkt,
1905 * double-check this now that we have the actual
1906 * packet in hand.
1907 */
1908 struct ethhdr *ehdr;
1909 skb_reset_mac_header(skb);
1910 ehdr = eth_hdr(skb);
1911 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1912 err = -EMSGSIZE;
1913 goto out_unlock;
1914 }
1915 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 skb->protocol = proto;
1918 skb->dev = dev;
1919 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001920 skb->mark = sk->sk_mark;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00001921
1922 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001924 if (unlikely(extra_len == 4))
1925 skb->no_fcs = 1;
1926
Jason Wang40893fd2013-03-26 23:11:22 +00001927 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001930 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001931 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001934 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001935out_free:
1936 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return err;
1938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001940static unsigned int run_filter(const struct sk_buff *skb,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001941 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -08001942 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 struct sk_filter *filter;
1945
Eric Dumazet80f8f102011-01-18 07:46:52 +00001946 rcu_read_lock();
1947 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001948 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +00001949 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001950 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
David S. Millerdbcb5852007-01-24 15:21:02 -08001952 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00001956 * This function makes lazy skb cloning in hope that most of packets
1957 * are discarded by BPF.
1958 *
1959 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1960 * and skb->cb are mangled. It works because (and until) packets
1961 * falling here are owned by current CPU. Output packets are cloned
1962 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1963 * sequencially, so that if we return skb to original state on exit,
1964 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
1966
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001967static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1968 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct sock *sk;
1971 struct sockaddr_ll *sll;
1972 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001973 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001975 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 if (skb->pkt_type == PACKET_LOOPBACK)
1978 goto drop;
1979
1980 sk = pt->af_packet_priv;
1981 po = pkt_sk(sk);
1982
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001983 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001984 goto drop;
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 skb->dev = dev;
1987
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001988 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001990 * exported to higher levels.
1991 *
1992 * Otherwise, the device hides details of its frame
1993 * structure, so that corresponding packet head is
1994 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
1996 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001997 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 else if (skb->pkt_type == PACKET_OUTGOING) {
1999 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002000 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 }
2003
2004 snaplen = skb->len;
2005
David S. Millerdbcb5852007-01-24 15:21:02 -08002006 res = run_filter(skb, sk, snaplen);
2007 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002008 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08002009 if (snaplen > res)
2010 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002012 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto drop_n_acct;
2014
2015 if (skb_shared(skb)) {
2016 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2017 if (nskb == NULL)
2018 goto drop_n_acct;
2019
2020 if (skb_head != skb->data) {
2021 skb->data = skb_head;
2022 skb->len = skb_len;
2023 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00002024 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 skb = nskb;
2026 }
2027
Eyal Birgerb4772ef2015-03-01 14:58:29 +02002028 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08002029
2030 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002033 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002034 sll->sll_ifindex = orig_dev->ifindex;
2035 else
2036 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002038 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Eyal Birger2472d762015-03-01 14:58:28 +02002040 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2041 * Use their space for storing the original skb length.
2042 */
2043 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (pskb_trim(skb, snaplen))
2046 goto drop_n_acct;
2047
2048 skb_set_owner_r(skb, sk);
2049 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002050 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Phil Oester84531c22005-07-12 11:57:52 -07002052 /* drop conntrack reference */
2053 nf_reset(skb);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002056 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002057 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 __skb_queue_tail(&sk->sk_receive_queue, skb);
2059 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002060 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return 0;
2062
2063drop_n_acct:
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002064 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002065 po->stats.stats1.tp_drops++;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002066 atomic_inc(&sk->sk_drops);
2067 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069drop_n_restore:
2070 if (skb_head != skb->data && skb_shared(skb)) {
2071 skb->data = skb_head;
2072 skb->len = skb_len;
2073 }
2074drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +00002075 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
2077}
2078
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002079static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2080 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct sock *sk;
2083 struct packet_sock *po;
2084 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002085 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002086 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002088 unsigned int snaplen, res;
chetan lokef6fb8f102011-08-19 10:18:16 +00002089 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002090 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002092 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002093 __u32 ts_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Atzm Watanabe51846352013-12-17 22:53:32 +09002095 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2096 * We may add members to them until current aligned size without forcing
2097 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2098 */
2099 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2100 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (skb->pkt_type == PACKET_LOOPBACK)
2103 goto drop;
2104
2105 sk = pt->af_packet_priv;
2106 po = pkt_sk(sk);
2107
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002108 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002109 goto drop;
2110
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002111 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002113 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 else if (skb->pkt_type == PACKET_OUTGOING) {
2115 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002116 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 }
2119
2120 snaplen = skb->len;
2121
David S. Millerdbcb5852007-01-24 15:21:02 -08002122 res = run_filter(skb, sk, snaplen);
2123 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002124 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002125
2126 if (skb->ip_summed == CHECKSUM_PARTIAL)
2127 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002128 else if (skb->pkt_type != PACKET_OUTGOING &&
2129 (skb->ip_summed == CHECKSUM_COMPLETE ||
2130 skb_csum_unnecessary(skb)))
2131 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002132
David S. Millerdbcb5852007-01-24 15:21:02 -08002133 if (snaplen > res)
2134 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002137 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2138 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002140 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002141 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002142 (maclen < 16 ? 16 : maclen)) +
2143 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 macoff = netoff - maclen;
2145 }
chetan lokef6fb8f102011-08-19 10:18:16 +00002146 if (po->tp_version <= TPACKET_V2) {
2147 if (macoff + snaplen > po->rx_ring.frame_size) {
2148 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002149 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002150 if (skb_shared(skb)) {
2151 copy_skb = skb_clone(skb, GFP_ATOMIC);
2152 } else {
2153 copy_skb = skb_get(skb);
2154 skb_head = skb->data;
2155 }
2156 if (copy_skb)
2157 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
chetan lokef6fb8f102011-08-19 10:18:16 +00002159 snaplen = po->rx_ring.frame_size - macoff;
2160 if ((int)snaplen < 0)
2161 snaplen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002163 } else if (unlikely(macoff + snaplen >
2164 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2165 u32 nval;
2166
2167 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2168 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2169 snaplen, nval, macoff);
2170 snaplen = nval;
2171 if (unlikely((int)snaplen < 0)) {
2172 snaplen = 0;
2173 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
2174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00002177 h.raw = packet_current_rx_frame(po, skb,
2178 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002179 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto ring_is_full;
chetan lokef6fb8f102011-08-19 10:18:16 +00002181 if (po->tp_version <= TPACKET_V2) {
2182 packet_increment_rx_head(po, &po->rx_ring);
2183 /*
2184 * LOSING will be reported till you read the stats,
2185 * because it's COR - Clear On Read.
2186 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2187 * at packet level.
2188 */
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002189 if (po->stats.stats1.tp_drops)
chetan lokef6fb8f102011-08-19 10:18:16 +00002190 status |= TP_STATUS_LOSING;
2191 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002192 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (copy_skb) {
2194 status |= TP_STATUS_COPY;
2195 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 spin_unlock(&sk->sk_receive_queue.lock);
2198
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002199 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002200
2201 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Daniel Borkmann7a513842013-04-23 00:39:29 +00002202 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002204 status |= ts_status;
2205
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002206 switch (po->tp_version) {
2207 case TPACKET_V1:
2208 h.h1->tp_len = skb->len;
2209 h.h1->tp_snaplen = snaplen;
2210 h.h1->tp_mac = macoff;
2211 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002212 h.h1->tp_sec = ts.tv_sec;
2213 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002214 hdrlen = sizeof(*h.h1);
2215 break;
2216 case TPACKET_V2:
2217 h.h2->tp_len = skb->len;
2218 h.h2->tp_snaplen = snaplen;
2219 h.h2->tp_mac = macoff;
2220 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002221 h.h2->tp_sec = ts.tv_sec;
2222 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002223 if (skb_vlan_tag_present(skb)) {
2224 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002225 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2226 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002227 } else {
2228 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002229 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002230 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002231 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002232 hdrlen = sizeof(*h.h2);
2233 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00002234 case TPACKET_V3:
2235 /* tp_nxt_offset,vlan are already populated above.
2236 * So DONT clear those fields here
2237 */
2238 h.h3->tp_status |= status;
2239 h.h3->tp_len = skb->len;
2240 h.h3->tp_snaplen = snaplen;
2241 h.h3->tp_mac = macoff;
2242 h.h3->tp_net = netoff;
chetan lokef6fb8f102011-08-19 10:18:16 +00002243 h.h3->tp_sec = ts.tv_sec;
2244 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002245 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f102011-08-19 10:18:16 +00002246 hdrlen = sizeof(*h.h3);
2247 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002248 default:
2249 BUG();
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002252 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002253 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 sll->sll_family = AF_PACKET;
2255 sll->sll_hatype = dev->type;
2256 sll->sll_protocol = skb->protocol;
2257 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002258 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002259 sll->sll_ifindex = orig_dev->ifindex;
2260 else
2261 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Ralf Baechlee16aa202006-12-07 00:11:33 -08002263 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002264
Changli Gaof6dafa92010-12-07 04:26:16 +00002265#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002266 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002267 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002269 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2270 macoff + snaplen);
2271
2272 for (start = h.raw; start < end; start += PAGE_SIZE)
2273 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002275 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002276#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002277
Dan Collinsda413ee2014-12-19 16:49:25 +13002278 if (po->tp_version <= TPACKET_V2) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002279 __packet_set_status(po, h.raw, status);
Dan Collinsda413ee2014-12-19 16:49:25 +13002280 sk->sk_data_ready(sk);
2281 } else {
chetan lokef6fb8f102011-08-19 10:18:16 +00002282 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285drop_n_restore:
2286 if (skb_head != skb->data && skb_shared(skb)) {
2287 skb->data = skb_head;
2288 skb->len = skb_len;
2289 }
2290drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002291 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return 0;
2293
2294ring_is_full:
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002295 po->stats.stats1.tp_drops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 spin_unlock(&sk->sk_receive_queue.lock);
2297
David S. Miller676d2362014-04-11 16:15:36 -04002298 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002299 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto drop_n_restore;
2301}
2302
Johann Baudy69e3c752009-05-18 22:11:22 -07002303static void tpacket_destruct_skb(struct sk_buff *skb)
2304{
2305 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002306
Johann Baudy69e3c752009-05-18 22:11:22 -07002307 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002308 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002309 __u32 ts;
2310
Johann Baudy69e3c752009-05-18 22:11:22 -07002311 ph = skb_shinfo(skb)->destructor_arg;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002312 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002313
2314 ts = __packet_set_timestamp(po, ph, skb);
2315 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Johann Baudy69e3c752009-05-18 22:11:22 -07002316 }
2317
2318 sock_wfree(skb);
2319}
2320
Willem de Bruijn9c707762014-11-19 13:10:16 -05002321static bool ll_header_truncated(const struct net_device *dev, int len)
2322{
2323 /* net device doesn't like empty head */
2324 if (unlikely(len <= dev->hard_header_len)) {
Willem de Bruijneee2f042015-01-08 11:29:18 -05002325 net_warn_ratelimited("%s: packet size is too short (%d <= %d)\n",
Willem de Bruijn9c707762014-11-19 13:10:16 -05002326 current->comm, len, dev->hard_header_len);
2327 return true;
2328 }
2329
2330 return false;
2331}
2332
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002333static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
2334 void *frame, struct net_device *dev, int size_max,
Herbert Xuae641942011-11-18 02:20:04 +00002335 __be16 proto, unsigned char *addr, int hlen)
Johann Baudy69e3c752009-05-18 22:11:22 -07002336{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002337 union tpacket_uhdr ph;
David S. Miller09effa62013-08-07 17:11:00 -07002338 int to_write, offset, len, tp_len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002339 struct socket *sock = po->sk.sk_socket;
2340 struct page *page;
2341 void *data;
2342 int err;
2343
2344 ph.raw = frame;
2345
2346 skb->protocol = proto;
2347 skb->dev = dev;
2348 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002349 skb->mark = po->sk.sk_mark;
Willem de Bruijn2e313962013-04-23 00:39:28 +00002350 sock_tx_timestamp(&po->sk, &skb_shinfo(skb)->tx_flags);
Johann Baudy69e3c752009-05-18 22:11:22 -07002351 skb_shinfo(skb)->destructor_arg = ph.raw;
2352
2353 switch (po->tp_version) {
2354 case TPACKET_V2:
2355 tp_len = ph.h2->tp_len;
2356 break;
2357 default:
2358 tp_len = ph.h1->tp_len;
2359 break;
2360 }
David S. Miller09effa62013-08-07 17:11:00 -07002361 if (unlikely(tp_len > size_max)) {
2362 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2363 return -EMSGSIZE;
2364 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002365
Herbert Xuae641942011-11-18 02:20:04 +00002366 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002367 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002368
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002369 if (!packet_use_direct_xmit(po))
2370 skb_probe_transport_header(skb, 0);
2371 if (unlikely(po->tp_tx_has_off)) {
Paul Chavent5920cd3a2012-11-06 23:10:47 +00002372 int off_min, off_max, off;
2373 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2374 off_max = po->tx_ring.frame_size - tp_len;
2375 if (sock->type == SOCK_DGRAM) {
2376 switch (po->tp_version) {
2377 case TPACKET_V2:
2378 off = ph.h2->tp_net;
2379 break;
2380 default:
2381 off = ph.h1->tp_net;
2382 break;
2383 }
2384 } else {
2385 switch (po->tp_version) {
2386 case TPACKET_V2:
2387 off = ph.h2->tp_mac;
2388 break;
2389 default:
2390 off = ph.h1->tp_mac;
2391 break;
2392 }
2393 }
2394 if (unlikely((off < off_min) || (off_max < off)))
2395 return -EINVAL;
2396 data = ph.raw + off;
2397 } else {
2398 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
2399 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002400 to_write = tp_len;
2401
2402 if (sock->type == SOCK_DGRAM) {
2403 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2404 NULL, tp_len);
2405 if (unlikely(err < 0))
2406 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002407 } else if (dev->hard_header_len) {
Willem de Bruijn9c707762014-11-19 13:10:16 -05002408 if (ll_header_truncated(dev, tp_len))
Johann Baudy69e3c752009-05-18 22:11:22 -07002409 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002410
2411 skb_push(skb, dev->hard_header_len);
2412 err = skb_store_bits(skb, 0, data,
2413 dev->hard_header_len);
2414 if (unlikely(err))
2415 return err;
2416
2417 data += dev->hard_header_len;
2418 to_write -= dev->hard_header_len;
2419 }
2420
Johann Baudy69e3c752009-05-18 22:11:22 -07002421 offset = offset_in_page(data);
2422 len_max = PAGE_SIZE - offset;
2423 len = ((to_write > len_max) ? len_max : to_write);
2424
2425 skb->data_len = to_write;
2426 skb->len += to_write;
2427 skb->truesize += to_write;
2428 atomic_add(to_write, &po->sk.sk_wmem_alloc);
2429
2430 while (likely(to_write)) {
2431 nr_frags = skb_shinfo(skb)->nr_frags;
2432
2433 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002434 pr_err("Packet exceed the number of skb frags(%lu)\n",
2435 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002436 return -EFAULT;
2437 }
2438
Changli Gao0af55bb2010-12-01 02:52:20 +00002439 page = pgv_to_page(data);
2440 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002441 flush_dcache_page(page);
2442 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002443 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002444 to_write -= len;
2445 offset = 0;
2446 len_max = PAGE_SIZE;
2447 len = ((to_write > len_max) ? len_max : to_write);
2448 }
2449
2450 return tp_len;
2451}
2452
2453static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2454{
Johann Baudy69e3c752009-05-18 22:11:22 -07002455 struct sk_buff *skb;
2456 struct net_device *dev;
2457 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002458 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002459 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002460 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002461 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Johann Baudy69e3c752009-05-18 22:11:22 -07002462 int tp_len, size_max;
2463 unsigned char *addr;
2464 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002465 int status = TP_STATUS_AVAILABLE;
Herbert Xuae641942011-11-18 02:20:04 +00002466 int hlen, tlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002467
Johann Baudy69e3c752009-05-18 22:11:22 -07002468 mutex_lock(&po->pg_vec_lock);
2469
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002470 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002471 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002472 proto = po->num;
2473 addr = NULL;
2474 } else {
2475 err = -EINVAL;
2476 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2477 goto out;
2478 if (msg->msg_namelen < (saddr->sll_halen
2479 + offsetof(struct sockaddr_ll,
2480 sll_addr)))
2481 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002482 proto = saddr->sll_protocol;
2483 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002484 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Johann Baudy69e3c752009-05-18 22:11:22 -07002485 }
2486
Johann Baudy69e3c752009-05-18 22:11:22 -07002487 err = -ENXIO;
2488 if (unlikely(dev == NULL))
2489 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002490 err = -ENETDOWN;
2491 if (unlikely(!(dev->flags & IFF_UP)))
2492 goto out_put;
2493
Daniel Borkmann52f14542014-02-28 02:22:06 +01002494 reserve = dev->hard_header_len + VLAN_HLEN;
Johann Baudy69e3c752009-05-18 22:11:22 -07002495 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002496 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002497
David S. Miller09effa62013-08-07 17:11:00 -07002498 if (size_max > dev->mtu + reserve)
2499 size_max = dev->mtu + reserve;
2500
Johann Baudy69e3c752009-05-18 22:11:22 -07002501 do {
2502 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002503 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002504 if (unlikely(ph == NULL)) {
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002505 if (need_wait && need_resched())
2506 schedule();
Johann Baudy69e3c752009-05-18 22:11:22 -07002507 continue;
2508 }
2509
2510 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002511 hlen = LL_RESERVED_SPACE(dev);
2512 tlen = dev->needed_tailroom;
Johann Baudy69e3c752009-05-18 22:11:22 -07002513 skb = sock_alloc_send_skb(&po->sk,
Herbert Xuae641942011-11-18 02:20:04 +00002514 hlen + tlen + sizeof(struct sockaddr_ll),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002515 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002516
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002517 if (unlikely(skb == NULL)) {
2518 /* we assume the socket was initially writeable ... */
2519 if (likely(len_sum > 0))
2520 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002521 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002522 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002523 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
Daniel Borkmann52f14542014-02-28 02:22:06 +01002524 addr, hlen);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002525 if (likely(tp_len >= 0) &&
2526 tp_len > dev->mtu + dev->hard_header_len) {
Daniel Borkmann52f14542014-02-28 02:22:06 +01002527 struct ethhdr *ehdr;
2528 /* Earlier code assumed this would be a VLAN pkt,
2529 * double-check this now that we have the actual
2530 * packet in hand.
2531 */
Johann Baudy69e3c752009-05-18 22:11:22 -07002532
Daniel Borkmann52f14542014-02-28 02:22:06 +01002533 skb_reset_mac_header(skb);
2534 ehdr = eth_hdr(skb);
2535 if (ehdr->h_proto != htons(ETH_P_8021Q))
2536 tp_len = -EMSGSIZE;
2537 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002538 if (unlikely(tp_len < 0)) {
2539 if (po->tp_loss) {
2540 __packet_set_status(po, ph,
2541 TP_STATUS_AVAILABLE);
2542 packet_increment_head(&po->tx_ring);
2543 kfree_skb(skb);
2544 continue;
2545 } else {
2546 status = TP_STATUS_WRONG_FORMAT;
2547 err = tp_len;
2548 goto out_status;
2549 }
2550 }
2551
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002552 packet_pick_tx_queue(dev, skb);
2553
Johann Baudy69e3c752009-05-18 22:11:22 -07002554 skb->destructor = tpacket_destruct_skb;
2555 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002556 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002557
2558 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002559 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002560 if (unlikely(err > 0)) {
2561 err = net_xmit_errno(err);
2562 if (err && __packet_get_status(po, ph) ==
2563 TP_STATUS_AVAILABLE) {
2564 /* skb was destructed already */
2565 skb = NULL;
2566 goto out_status;
2567 }
2568 /*
2569 * skb was dropped but not destructed yet;
2570 * let's treat it like congestion or err < 0
2571 */
2572 err = 0;
2573 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002574 packet_increment_head(&po->tx_ring);
2575 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002576 } while (likely((ph != NULL) ||
2577 /* Note: packet_read_pending() might be slow if we have
2578 * to call it as it's per_cpu variable, but in fast-path
2579 * we already short-circuit the loop with the first
2580 * condition, and luckily don't have to go that path
2581 * anyway.
2582 */
2583 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002584
2585 err = len_sum;
2586 goto out_put;
2587
Johann Baudy69e3c752009-05-18 22:11:22 -07002588out_status:
2589 __packet_set_status(po, ph, status);
2590 kfree_skb(skb);
2591out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002592 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002593out:
2594 mutex_unlock(&po->pg_vec_lock);
2595 return err;
2596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002598static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2599 size_t reserve, size_t len,
2600 size_t linear, int noblock,
2601 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002602{
2603 struct sk_buff *skb;
2604
2605 /* Under a page? Don't bother with paged skb. */
2606 if (prepad + len < PAGE_SIZE || !linear)
2607 linear = len;
2608
2609 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002610 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002611 if (!skb)
2612 return NULL;
2613
2614 skb_reserve(skb, reserve);
2615 skb_put(skb, linear);
2616 skb->data_len = len - linear;
2617 skb->len += len - linear;
2618
2619 return skb;
2620}
2621
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002622static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
2624 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002625 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct sk_buff *skb;
2627 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002628 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00002630 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002631 struct virtio_net_hdr vnet_hdr = { 0 };
2632 int offset = 0;
2633 int vnet_hdr_len;
2634 struct packet_sock *po = pkt_sk(sk);
2635 unsigned short gso_type = 0;
Herbert Xuae641942011-11-18 02:20:04 +00002636 int hlen, tlen;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002637 int extra_len = 0;
Al Viro8feb2fb2014-11-06 01:10:59 -05002638 ssize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002641 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002643
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002644 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002645 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 proto = po->num;
2647 addr = NULL;
2648 } else {
2649 err = -EINVAL;
2650 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2651 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002652 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2653 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 proto = saddr->sll_protocol;
2655 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002656 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002660 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002662 err = -ENETDOWN;
2663 if (unlikely(!(dev->flags & IFF_UP)))
2664 goto out_unlock;
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (sock->type == SOCK_RAW)
2667 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002668 if (po->has_vnet_hdr) {
2669 vnet_hdr_len = sizeof(vnet_hdr);
2670
2671 err = -EINVAL;
2672 if (len < vnet_hdr_len)
2673 goto out_unlock;
2674
2675 len -= vnet_hdr_len;
2676
Al Viro8feb2fb2014-11-06 01:10:59 -05002677 err = -EFAULT;
Al Viroc0371da2014-11-24 10:42:55 -05002678 n = copy_from_iter(&vnet_hdr, vnet_hdr_len, &msg->msg_iter);
Al Viro8feb2fb2014-11-06 01:10:59 -05002679 if (n != vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002680 goto out_unlock;
2681
2682 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002683 (__virtio16_to_cpu(false, vnet_hdr.csum_start) +
2684 __virtio16_to_cpu(false, vnet_hdr.csum_offset) + 2 >
2685 __virtio16_to_cpu(false, vnet_hdr.hdr_len)))
2686 vnet_hdr.hdr_len = __cpu_to_virtio16(false,
2687 __virtio16_to_cpu(false, vnet_hdr.csum_start) +
2688 __virtio16_to_cpu(false, vnet_hdr.csum_offset) + 2);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002689
2690 err = -EINVAL;
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002691 if (__virtio16_to_cpu(false, vnet_hdr.hdr_len) > len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002692 goto out_unlock;
2693
2694 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2695 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2696 case VIRTIO_NET_HDR_GSO_TCPV4:
2697 gso_type = SKB_GSO_TCPV4;
2698 break;
2699 case VIRTIO_NET_HDR_GSO_TCPV6:
2700 gso_type = SKB_GSO_TCPV6;
2701 break;
2702 case VIRTIO_NET_HDR_GSO_UDP:
2703 gso_type = SKB_GSO_UDP;
2704 break;
2705 default:
2706 goto out_unlock;
2707 }
2708
2709 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
2710 gso_type |= SKB_GSO_TCP_ECN;
2711
2712 if (vnet_hdr.gso_size == 0)
2713 goto out_unlock;
2714
2715 }
2716 }
2717
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002718 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2719 if (!netif_supports_nofcs(dev)) {
2720 err = -EPROTONOSUPPORT;
2721 goto out_unlock;
2722 }
2723 extra_len = 4; /* We're doing our own CRC */
2724 }
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002727 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 goto out_unlock;
2729
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002730 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002731 hlen = LL_RESERVED_SPACE(dev);
2732 tlen = dev->needed_tailroom;
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002733 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len,
2734 __virtio16_to_cpu(false, vnet_hdr.hdr_len),
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002735 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002736 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 goto out_unlock;
2738
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002739 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002741 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002742 if (sock->type == SOCK_DGRAM) {
2743 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002744 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002745 goto out_free;
2746 } else {
2747 if (ll_header_truncated(dev, len))
2748 goto out_free;
2749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002752 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (err)
2754 goto out_free;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00002755
2756 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002758 if (!gso_type && (len > dev->mtu + reserve + extra_len)) {
David S. Miller09effa62013-08-07 17:11:00 -07002759 /* Earlier code assumed this would be a VLAN pkt,
2760 * double-check this now that we have the actual
2761 * packet in hand.
2762 */
2763 struct ethhdr *ehdr;
2764 skb_reset_mac_header(skb);
2765 ehdr = eth_hdr(skb);
2766 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
2767 err = -EMSGSIZE;
2768 goto out_free;
2769 }
Ben Greear57f89bf2011-02-11 09:35:18 +00002770 }
2771
David S. Miller09effa62013-08-07 17:11:00 -07002772 skb->protocol = proto;
2773 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002775 skb->mark = sk->sk_mark;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002776
2777 packet_pick_tx_queue(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002779 if (po->has_vnet_hdr) {
2780 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002781 u16 s = __virtio16_to_cpu(false, vnet_hdr.csum_start);
2782 u16 o = __virtio16_to_cpu(false, vnet_hdr.csum_offset);
2783 if (!skb_partial_csum_set(skb, s, o)) {
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002784 err = -EINVAL;
2785 goto out_free;
2786 }
2787 }
2788
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002789 skb_shinfo(skb)->gso_size =
2790 __virtio16_to_cpu(false, vnet_hdr.gso_size);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002791 skb_shinfo(skb)->gso_type = gso_type;
2792
2793 /* Header must be checked, and gso_segs computed. */
2794 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2795 skb_shinfo(skb)->gso_segs = 0;
2796
2797 len += vnet_hdr_len;
2798 }
2799
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002800 if (!packet_use_direct_xmit(po))
2801 skb_probe_transport_header(skb, reserve);
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002802 if (unlikely(extra_len == 4))
2803 skb->no_fcs = 1;
2804
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002805 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2807 goto out_unlock;
2808
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002809 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002811 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813out_free:
2814 kfree_skb(skb);
2815out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002816 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 dev_put(dev);
2818out:
2819 return err;
2820}
2821
Ying Xue1b784142015-03-02 15:37:48 +08002822static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07002823{
Johann Baudy69e3c752009-05-18 22:11:22 -07002824 struct sock *sk = sock->sk;
2825 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002826
Johann Baudy69e3c752009-05-18 22:11:22 -07002827 if (po->tx_ring.pg_vec)
2828 return tpacket_snd(po, msg);
2829 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002830 return packet_snd(sock, msg, len);
2831}
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833/*
2834 * Close a PACKET socket. This is fairly simple. We immediately go
2835 * to 'closed' state and remove our protocol entry in the device list.
2836 */
2837
2838static int packet_release(struct socket *sock)
2839{
2840 struct sock *sk = sock->sk;
2841 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002842 struct net *net;
chetan lokef6fb8f102011-08-19 10:18:16 +00002843 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 if (!sk)
2846 return 0;
2847
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002848 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 po = pkt_sk(sk);
2850
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002851 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002852 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002853 mutex_unlock(&net->packet.sklist_lock);
2854
2855 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08002856 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002857 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
stephen hemminger808f5112010-02-22 07:57:18 +00002859 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002860 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002861 packet_cached_dev_reset(po);
2862
Ben Greear160ff182011-06-01 07:18:52 +00002863 if (po->prot_hook.dev) {
2864 dev_put(po->prot_hook.dev);
2865 po->prot_hook.dev = NULL;
2866 }
stephen hemminger808f5112010-02-22 07:57:18 +00002867 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Phil Sutter9665d5d2013-02-01 07:21:41 +00002871 if (po->rx_ring.pg_vec) {
2872 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00002873 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002874 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002875
Phil Sutter9665d5d2013-02-01 07:21:41 +00002876 if (po->tx_ring.pg_vec) {
2877 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00002878 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
David S. Millerdc99f602011-07-05 01:45:05 -07002881 fanout_release(sk);
2882
stephen hemminger808f5112010-02-22 07:57:18 +00002883 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /*
2885 * Now the socket is dead. No more input will appear.
2886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 sock_orphan(sk);
2888 sock->sk = NULL;
2889
2890 /* Purge queues */
2891
2892 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002893 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08002894 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 sock_put(sk);
2897 return 0;
2898}
2899
2900/*
2901 * Attach a packet hook.
2902 */
2903
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002904static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03002907 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002908 __be16 proto_curr;
2909 bool need_rehook;
David S. Millerdc99f602011-07-05 01:45:05 -07002910
Wei Yongjunaef950b2011-12-27 22:32:41 -05002911 if (po->fanout) {
2912 if (dev)
2913 dev_put(dev);
2914
David S. Millerdc99f602011-07-05 01:45:05 -07002915 return -EINVAL;
Wei Yongjunaef950b2011-12-27 22:32:41 -05002916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 spin_lock(&po->bind_lock);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002920
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002921 proto_curr = po->prot_hook.type;
2922 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002924 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002926 if (need_rehook) {
2927 unregister_prot_hook(sk, true);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002928
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002929 po->num = proto;
2930 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002931 po->prot_hook.dev = dev;
2932
2933 po->ifindex = dev ? dev->ifindex : 0;
2934 packet_cached_dev_assign(po, dev);
2935 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03002936 if (dev_curr)
2937 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002938
2939 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 goto out_unlock;
2941
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002942 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07002943 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002944 } else {
2945 sk->sk_err = ENETDOWN;
2946 if (!sock_flag(sk, SOCK_DEAD))
2947 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
2949
2950out_unlock:
2951 spin_unlock(&po->bind_lock);
2952 release_sock(sk);
2953 return 0;
2954}
2955
2956/*
2957 * Bind a packet socket to a device
2958 */
2959
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002960static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
2961 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002963 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 char name[15];
2965 struct net_device *dev;
2966 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /*
2969 * Check legality
2970 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002971
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002972 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002974 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002976 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00002977 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return err;
2980}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2983{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002984 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
2985 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 struct net_device *dev = NULL;
2987 int err;
2988
2989
2990 /*
2991 * Check legality
2992 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (addr_len < sizeof(struct sockaddr_ll))
2995 return -EINVAL;
2996 if (sll->sll_family != AF_PACKET)
2997 return -EINVAL;
2998
2999 if (sll->sll_ifindex) {
3000 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003001 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (dev == NULL)
3003 goto out;
3004 }
3005 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007out:
3008 return err;
3009}
3010
3011static struct proto packet_proto = {
3012 .name = "PACKET",
3013 .owner = THIS_MODULE,
3014 .obj_size = sizeof(struct packet_sock),
3015};
3016
3017/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003018 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 */
3020
Eric Paris3f378b62009-11-05 22:18:14 -08003021static int packet_create(struct net *net, struct socket *sock, int protocol,
3022 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 struct sock *sk;
3025 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003026 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 int err;
3028
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003029 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003031 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3032 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return -ESOCKTNOSUPPORT;
3034
3035 sock->state = SS_UNCONNECTED;
3036
3037 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003038 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (sk == NULL)
3040 goto out;
3041
3042 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (sock->type == SOCK_PACKET)
3044 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 sock_init_data(sock, sk);
3047
3048 po = pkt_sk(sk);
3049 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003050 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003051 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003052
Daniel Borkmannb0138402014-01-15 16:25:36 +01003053 err = packet_alloc_pending(po);
3054 if (err)
3055 goto out2;
3056
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003057 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003060 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /*
3063 * Attach a protocol block
3064 */
3065
3066 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003067 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003068 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (sock->type == SOCK_PACKET)
3072 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 po->prot_hook.af_packet_priv = sk;
3075
Al Viro0e11c912006-11-08 00:26:29 -08003076 if (proto) {
3077 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07003078 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
3080
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003081 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00003082 sk_add_node_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003083 mutex_unlock(&net->packet.sklist_lock);
3084
3085 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003086 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003087 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003088
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003089 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003090out2:
3091 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092out:
3093 return err;
3094}
3095
3096/*
3097 * Pull a packet from our receive queue and hand it to the user.
3098 * If necessary we block.
3099 */
3100
Ying Xue1b784142015-03-02 15:37:48 +08003101static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3102 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
3104 struct sock *sk = sock->sk;
3105 struct sk_buff *skb;
3106 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003107 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003108 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003111 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 goto out;
3113
3114#if 0
3115 /* What error should we return now? EUNATTACH? */
3116 if (pkt_sk(sk)->ifindex < 0)
3117 return -ENODEV;
3118#endif
3119
Richard Cochraned85b562010-04-07 22:41:28 +00003120 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003121 err = sock_recv_errqueue(sk, msg, len,
3122 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003123 goto out;
3124 }
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 * Call the generic datagram receiver. This handles all sorts
3128 * of horrible races and re-entrancy so we can forget about it
3129 * in the protocol layers.
3130 *
3131 * Now it will return ENETDOWN, if device have just gone down,
3132 * but then it will block.
3133 */
3134
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003135 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003138 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 * handles the blocking we don't see and worry about blocking
3140 * retries.
3141 */
3142
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003143 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 goto out;
3145
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003146 if (pkt_sk(sk)->pressure)
3147 packet_rcv_has_room(pkt_sk(sk), NULL);
3148
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003149 if (pkt_sk(sk)->has_vnet_hdr) {
3150 struct virtio_net_hdr vnet_hdr = { 0 };
3151
3152 err = -EINVAL;
3153 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00003154 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003155 goto out_free;
3156
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00003157 len -= vnet_hdr_len;
3158
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003159 if (skb_is_gso(skb)) {
3160 struct skb_shared_info *sinfo = skb_shinfo(skb);
3161
3162 /* This is a hint as to how much should be linear. */
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02003163 vnet_hdr.hdr_len =
3164 __cpu_to_virtio16(false, skb_headlen(skb));
3165 vnet_hdr.gso_size =
3166 __cpu_to_virtio16(false, sinfo->gso_size);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003167 if (sinfo->gso_type & SKB_GSO_TCPV4)
3168 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
3169 else if (sinfo->gso_type & SKB_GSO_TCPV6)
3170 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
3171 else if (sinfo->gso_type & SKB_GSO_UDP)
3172 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
3173 else if (sinfo->gso_type & SKB_GSO_FCOE)
3174 goto out_free;
3175 else
3176 BUG();
3177 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
3178 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
3179 } else
3180 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
3181
3182 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3183 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02003184 vnet_hdr.csum_start = __cpu_to_virtio16(false,
3185 skb_checksum_start_offset(skb));
3186 vnet_hdr.csum_offset = __cpu_to_virtio16(false,
3187 skb->csum_offset);
Jason Wang10a8d942011-06-10 00:56:17 +00003188 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
3189 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003190 } /* else everything is zero */
3191
Al Viro7eab8d92014-04-06 21:51:23 -04003192 err = memcpy_to_msg(msg, (void *)&vnet_hdr, vnet_hdr_len);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003193 if (err < 0)
3194 goto out_free;
3195 }
3196
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003197 /* You lose any data beyond the buffer you gave. If it worries
3198 * a user program they can ask the device for its MTU
3199 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003202 if (copied > len) {
3203 copied = len;
3204 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206
David S. Miller51f3d022014-11-05 16:46:40 -05003207 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (err)
3209 goto out_free;
3210
Eyal Birger2472d762015-03-01 14:58:28 +02003211 if (sock->type != SOCK_PACKET) {
3212 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3213
3214 /* Original length was stored in sockaddr_ll fields */
3215 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3216 sll->sll_family = AF_PACKET;
3217 sll->sll_protocol = skb->protocol;
3218 }
3219
Neil Horman3b885782009-10-12 13:26:31 -07003220 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003222 if (msg->msg_name) {
3223 /* If the address length field is there to be filled
3224 * in, we fill it in now.
3225 */
3226 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003227 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003228 msg->msg_namelen = sizeof(struct sockaddr_pkt);
3229 } else {
3230 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003231
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003232 msg->msg_namelen = sll->sll_halen +
3233 offsetof(struct sockaddr_ll, sll_addr);
3234 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003235 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
3236 msg->msg_namelen);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Herbert Xu8dc41942007-02-04 23:31:32 -08003239 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003240 struct tpacket_auxdata aux;
3241
3242 aux.tp_status = TP_STATUS_USER;
3243 if (skb->ip_summed == CHECKSUM_PARTIAL)
3244 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003245 else if (skb->pkt_type != PACKET_OUTGOING &&
3246 (skb->ip_summed == CHECKSUM_COMPLETE ||
3247 skb_csum_unnecessary(skb)))
3248 aux.tp_status |= TP_STATUS_CSUM_VALID;
3249
Eyal Birger2472d762015-03-01 14:58:28 +02003250 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003251 aux.tp_snaplen = skb->len;
3252 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003253 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003254 if (skb_vlan_tag_present(skb)) {
3255 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003256 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3257 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003258 } else {
3259 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003260 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003261 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003262 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003263 }
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /*
3266 * Free or return the buffer as appropriate. Again this
3267 * hides all the races and re-entrancy issues from us.
3268 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003269 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271out_free:
3272 skb_free_datagram(sk, skb);
3273out:
3274 return err;
3275}
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
3278 int *uaddr_len, int peer)
3279{
3280 struct net_device *dev;
3281 struct sock *sk = sock->sk;
3282
3283 if (peer)
3284 return -EOPNOTSUPP;
3285
3286 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003287 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003288 rcu_read_lock();
3289 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3290 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003291 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003292 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 *uaddr_len = sizeof(*uaddr);
3294
3295 return 0;
3296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
3299 int *uaddr_len, int peer)
3300{
3301 struct net_device *dev;
3302 struct sock *sk = sock->sk;
3303 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003304 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
3306 if (peer)
3307 return -EOPNOTSUPP;
3308
3309 sll->sll_family = AF_PACKET;
3310 sll->sll_ifindex = po->ifindex;
3311 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003312 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003313 rcu_read_lock();
3314 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (dev) {
3316 sll->sll_hatype = dev->type;
3317 sll->sll_halen = dev->addr_len;
3318 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 } else {
3320 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3321 sll->sll_halen = 0;
3322 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003323 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003324 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 return 0;
3327}
3328
Wang Chen2aeb0b82008-07-14 20:49:46 -07003329static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3330 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
3332 switch (i->type) {
3333 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003334 if (i->alen != dev->addr_len)
3335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003337 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003339 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 break;
3341 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003342 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003344 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003345 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003346 if (i->alen != dev->addr_len)
3347 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003348 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003349 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003350 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003351 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003352 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003353 default:
3354 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003359static void packet_dev_mclist_delete(struct net_device *dev,
3360 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003362 struct packet_mclist *ml;
3363
3364 while ((ml = *mlp) != NULL) {
3365 if (ml->ifindex == dev->ifindex) {
3366 packet_dev_mc(dev, ml, -1);
3367 *mlp = ml->next;
3368 kfree(ml);
3369 } else
3370 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372}
3373
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003374static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
3376 struct packet_sock *po = pkt_sk(sk);
3377 struct packet_mclist *ml, *i;
3378 struct net_device *dev;
3379 int err;
3380
3381 rtnl_lock();
3382
3383 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003384 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (!dev)
3386 goto done;
3387
3388 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003389 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 goto done;
3391
3392 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003393 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (i == NULL)
3395 goto done;
3396
3397 err = 0;
3398 for (ml = po->mclist; ml; ml = ml->next) {
3399 if (ml->ifindex == mreq->mr_ifindex &&
3400 ml->type == mreq->mr_type &&
3401 ml->alen == mreq->mr_alen &&
3402 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3403 ml->count++;
3404 /* Free the new element ... */
3405 kfree(i);
3406 goto done;
3407 }
3408 }
3409
3410 i->type = mreq->mr_type;
3411 i->ifindex = mreq->mr_ifindex;
3412 i->alen = mreq->mr_alen;
3413 memcpy(i->addr, mreq->mr_address, i->alen);
3414 i->count = 1;
3415 i->next = po->mclist;
3416 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003417 err = packet_dev_mc(dev, i, 1);
3418 if (err) {
3419 po->mclist = i->next;
3420 kfree(i);
3421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423done:
3424 rtnl_unlock();
3425 return err;
3426}
3427
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003428static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 struct packet_mclist *ml, **mlp;
3431
3432 rtnl_lock();
3433
3434 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3435 if (ml->ifindex == mreq->mr_ifindex &&
3436 ml->type == mreq->mr_type &&
3437 ml->alen == mreq->mr_alen &&
3438 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3439 if (--ml->count == 0) {
3440 struct net_device *dev;
3441 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003442 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3443 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 kfree(ml);
3446 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 }
3449 }
3450 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
3454static void packet_flush_mclist(struct sock *sk)
3455{
3456 struct packet_sock *po = pkt_sk(sk);
3457 struct packet_mclist *ml;
3458
3459 if (!po->mclist)
3460 return;
3461
3462 rtnl_lock();
3463 while ((ml = po->mclist) != NULL) {
3464 struct net_device *dev;
3465
3466 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003467 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3468 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 kfree(ml);
3471 }
3472 rtnl_unlock();
3473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475static int
David S. Millerb7058842009-09-30 16:12:20 -07003476packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
3478 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003479 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 int ret;
3481
3482 if (level != SOL_PACKET)
3483 return -ENOPROTOOPT;
3484
Johann Baudy69e3c752009-05-18 22:11:22 -07003485 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003486 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 case PACKET_DROP_MEMBERSHIP:
3488 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003489 struct packet_mreq_max mreq;
3490 int len = optlen;
3491 memset(&mreq, 0, sizeof(mreq));
3492 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003494 if (len > sizeof(mreq))
3495 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003496 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003498 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (optname == PACKET_ADD_MEMBERSHIP)
3501 ret = packet_mc_add(sk, &mreq);
3502 else
3503 ret = packet_mc_drop(sk, &mreq);
3504 return ret;
3505 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003508 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 {
chetan lokef6fb8f102011-08-19 10:18:16 +00003510 union tpacket_req_u req_u;
3511 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
chetan lokef6fb8f102011-08-19 10:18:16 +00003513 switch (po->tp_version) {
3514 case TPACKET_V1:
3515 case TPACKET_V2:
3516 len = sizeof(req_u.req);
3517 break;
3518 case TPACKET_V3:
3519 default:
3520 len = sizeof(req_u.req3);
3521 break;
3522 }
3523 if (optlen < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003525 if (pkt_sk(sk)->has_vnet_hdr)
3526 return -EINVAL;
chetan lokef6fb8f102011-08-19 10:18:16 +00003527 if (copy_from_user(&req_u.req, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 return -EFAULT;
chetan lokef6fb8f102011-08-19 10:18:16 +00003529 return packet_set_ring(sk, &req_u, 0,
3530 optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 }
3532 case PACKET_COPY_THRESH:
3533 {
3534 int val;
3535
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003536 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003538 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 return -EFAULT;
3540
3541 pkt_sk(sk)->copy_thresh = val;
3542 return 0;
3543 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003544 case PACKET_VERSION:
3545 {
3546 int val;
3547
3548 if (optlen != sizeof(val))
3549 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003550 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003551 return -EBUSY;
3552 if (copy_from_user(&val, optval, sizeof(val)))
3553 return -EFAULT;
3554 switch (val) {
3555 case TPACKET_V1:
3556 case TPACKET_V2:
chetan lokef6fb8f102011-08-19 10:18:16 +00003557 case TPACKET_V3:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003558 po->tp_version = val;
3559 return 0;
3560 default:
3561 return -EINVAL;
3562 }
3563 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003564 case PACKET_RESERVE:
3565 {
3566 unsigned int val;
3567
3568 if (optlen != sizeof(val))
3569 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003570 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07003571 return -EBUSY;
3572 if (copy_from_user(&val, optval, sizeof(val)))
3573 return -EFAULT;
3574 po->tp_reserve = val;
3575 return 0;
3576 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003577 case PACKET_LOSS:
3578 {
3579 unsigned int val;
3580
3581 if (optlen != sizeof(val))
3582 return -EINVAL;
3583 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3584 return -EBUSY;
3585 if (copy_from_user(&val, optval, sizeof(val)))
3586 return -EFAULT;
3587 po->tp_loss = !!val;
3588 return 0;
3589 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003590 case PACKET_AUXDATA:
3591 {
3592 int val;
3593
3594 if (optlen < sizeof(val))
3595 return -EINVAL;
3596 if (copy_from_user(&val, optval, sizeof(val)))
3597 return -EFAULT;
3598
3599 po->auxdata = !!val;
3600 return 0;
3601 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003602 case PACKET_ORIGDEV:
3603 {
3604 int val;
3605
3606 if (optlen < sizeof(val))
3607 return -EINVAL;
3608 if (copy_from_user(&val, optval, sizeof(val)))
3609 return -EFAULT;
3610
3611 po->origdev = !!val;
3612 return 0;
3613 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003614 case PACKET_VNET_HDR:
3615 {
3616 int val;
3617
3618 if (sock->type != SOCK_RAW)
3619 return -EINVAL;
3620 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3621 return -EBUSY;
3622 if (optlen < sizeof(val))
3623 return -EINVAL;
3624 if (copy_from_user(&val, optval, sizeof(val)))
3625 return -EFAULT;
3626
3627 po->has_vnet_hdr = !!val;
3628 return 0;
3629 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003630 case PACKET_TIMESTAMP:
3631 {
3632 int val;
3633
3634 if (optlen != sizeof(val))
3635 return -EINVAL;
3636 if (copy_from_user(&val, optval, sizeof(val)))
3637 return -EFAULT;
3638
3639 po->tp_tstamp = val;
3640 return 0;
3641 }
David S. Millerdc99f602011-07-05 01:45:05 -07003642 case PACKET_FANOUT:
3643 {
3644 int val;
3645
3646 if (optlen != sizeof(val))
3647 return -EINVAL;
3648 if (copy_from_user(&val, optval, sizeof(val)))
3649 return -EFAULT;
3650
3651 return fanout_add(sk, val & 0xffff, val >> 16);
3652 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003653 case PACKET_FANOUT_DATA:
3654 {
3655 if (!po->fanout)
3656 return -EINVAL;
3657
3658 return fanout_set_data(po, optval, optlen);
3659 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003660 case PACKET_TX_HAS_OFF:
3661 {
3662 unsigned int val;
3663
3664 if (optlen != sizeof(val))
3665 return -EINVAL;
3666 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3667 return -EBUSY;
3668 if (copy_from_user(&val, optval, sizeof(val)))
3669 return -EFAULT;
3670 po->tp_tx_has_off = !!val;
3671 return 0;
3672 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003673 case PACKET_QDISC_BYPASS:
3674 {
3675 int val;
3676
3677 if (optlen != sizeof(val))
3678 return -EINVAL;
3679 if (copy_from_user(&val, optval, sizeof(val)))
3680 return -EFAULT;
3681
3682 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3683 return 0;
3684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 default:
3686 return -ENOPROTOOPT;
3687 }
3688}
3689
3690static int packet_getsockopt(struct socket *sock, int level, int optname,
3691 char __user *optval, int __user *optlen)
3692{
3693 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003694 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 struct sock *sk = sock->sk;
3696 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003697 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003698 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003699 struct tpacket_rollover_stats rstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 if (level != SOL_PACKET)
3702 return -ENOPROTOOPT;
3703
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003704 if (get_user(len, optlen))
3705 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707 if (len < 0)
3708 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003709
Johann Baudy69e3c752009-05-18 22:11:22 -07003710 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003713 memcpy(&st, &po->stats, sizeof(st));
3714 memset(&po->stats, 0, sizeof(po->stats));
3715 spin_unlock_bh(&sk->sk_receive_queue.lock);
3716
chetan lokef6fb8f102011-08-19 10:18:16 +00003717 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003718 lv = sizeof(struct tpacket_stats_v3);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003719 st.stats3.tp_packets += st.stats3.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003720 data = &st.stats3;
chetan lokef6fb8f102011-08-19 10:18:16 +00003721 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003722 lv = sizeof(struct tpacket_stats);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003723 st.stats1.tp_packets += st.stats1.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003724 data = &st.stats1;
chetan lokef6fb8f102011-08-19 10:18:16 +00003725 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003728 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003729 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003730 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003731 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003732 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003733 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003734 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003735 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003736 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003737 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003738 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003739 break;
3740 case PACKET_HDRLEN:
3741 if (len > sizeof(int))
3742 len = sizeof(int);
3743 if (copy_from_user(&val, optval, len))
3744 return -EFAULT;
3745 switch (val) {
3746 case TPACKET_V1:
3747 val = sizeof(struct tpacket_hdr);
3748 break;
3749 case TPACKET_V2:
3750 val = sizeof(struct tpacket2_hdr);
3751 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00003752 case TPACKET_V3:
3753 val = sizeof(struct tpacket3_hdr);
3754 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003755 default:
3756 return -EINVAL;
3757 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003758 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003759 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003760 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003761 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003762 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003763 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003764 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003765 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003766 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003767 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003768 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003769 val = (po->fanout ?
3770 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003771 ((u32)po->fanout->type << 16) |
3772 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003773 0);
David S. Millerdc99f602011-07-05 01:45:05 -07003774 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003775 case PACKET_ROLLOVER_STATS:
3776 if (!po->rollover)
3777 return -EINVAL;
3778 rstats.tp_all = atomic_long_read(&po->rollover->num);
3779 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
3780 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
3781 data = &rstats;
3782 lv = sizeof(rstats);
3783 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003784 case PACKET_TX_HAS_OFF:
3785 val = po->tp_tx_has_off;
3786 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003787 case PACKET_QDISC_BYPASS:
3788 val = packet_use_direct_xmit(po);
3789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 default:
3791 return -ENOPROTOOPT;
3792 }
3793
Eric Dumazetc06fff62012-04-19 21:56:11 +00003794 if (len > lv)
3795 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003796 if (put_user(len, optlen))
3797 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08003798 if (copy_to_user(optval, data, len))
3799 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
3803
Jiri Pirko351638e2013-05-28 01:30:21 +00003804static int packet_notifier(struct notifier_block *this,
3805 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
3807 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00003808 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003809 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
stephen hemminger808f5112010-02-22 07:57:18 +00003811 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003812 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 struct packet_sock *po = pkt_sk(sk);
3814
3815 switch (msg) {
3816 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003818 packet_dev_mclist_delete(dev, &po->mclist);
David S. Millera2efcfa2007-05-29 13:12:50 -07003819 /* fallthrough */
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 case NETDEV_DOWN:
3822 if (dev->ifindex == po->ifindex) {
3823 spin_lock(&po->bind_lock);
3824 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07003825 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 sk->sk_err = ENETDOWN;
3827 if (!sock_flag(sk, SOCK_DEAD))
3828 sk->sk_error_report(sk);
3829 }
3830 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003831 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00003833 if (po->prot_hook.dev)
3834 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 po->prot_hook.dev = NULL;
3836 }
3837 spin_unlock(&po->bind_lock);
3838 }
3839 break;
3840 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00003841 if (dev->ifindex == po->ifindex) {
3842 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003843 if (po->num)
3844 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00003845 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 break;
3848 }
3849 }
stephen hemminger808f5112010-02-22 07:57:18 +00003850 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return NOTIFY_DONE;
3852}
3853
3854
3855static int packet_ioctl(struct socket *sock, unsigned int cmd,
3856 unsigned long arg)
3857{
3858 struct sock *sk = sock->sk;
3859
Johann Baudy69e3c752009-05-18 22:11:22 -07003860 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003861 case SIOCOUTQ:
3862 {
3863 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07003864
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003865 return put_user(amount, (int __user *)arg);
3866 }
3867 case SIOCINQ:
3868 {
3869 struct sk_buff *skb;
3870 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003872 spin_lock_bh(&sk->sk_receive_queue.lock);
3873 skb = skb_peek(&sk->sk_receive_queue);
3874 if (skb)
3875 amount = skb->len;
3876 spin_unlock_bh(&sk->sk_receive_queue.lock);
3877 return put_user(amount, (int __user *)arg);
3878 }
3879 case SIOCGSTAMP:
3880 return sock_get_timestamp(sk, (struct timeval __user *)arg);
3881 case SIOCGSTAMPNS:
3882 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003885 case SIOCADDRT:
3886 case SIOCDELRT:
3887 case SIOCDARP:
3888 case SIOCGARP:
3889 case SIOCSARP:
3890 case SIOCGIFADDR:
3891 case SIOCSIFADDR:
3892 case SIOCGIFBRDADDR:
3893 case SIOCSIFBRDADDR:
3894 case SIOCGIFNETMASK:
3895 case SIOCSIFNETMASK:
3896 case SIOCGIFDSTADDR:
3897 case SIOCSIFDSTADDR:
3898 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003899 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900#endif
3901
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003902 default:
3903 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 }
3905 return 0;
3906}
3907
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003908static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 poll_table *wait)
3910{
3911 struct sock *sk = sock->sk;
3912 struct packet_sock *po = pkt_sk(sk);
3913 unsigned int mask = datagram_poll(file, sock, wait);
3914
3915 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003916 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f102011-08-19 10:18:16 +00003917 if (!packet_previous_rx_frame(po, &po->rx_ring,
3918 TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 mask |= POLLIN | POLLRDNORM;
3920 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003921 if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04003922 po->pressure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003924 spin_lock_bh(&sk->sk_write_queue.lock);
3925 if (po->tx_ring.pg_vec) {
3926 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
3927 mask |= POLLOUT | POLLWRNORM;
3928 }
3929 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return mask;
3931}
3932
3933
3934/* Dirty? Well, I still did not learn better way to account
3935 * for user mmaps.
3936 */
3937
3938static void packet_mm_open(struct vm_area_struct *vma)
3939{
3940 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003941 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (sk)
3945 atomic_inc(&pkt_sk(sk)->mapped);
3946}
3947
3948static void packet_mm_close(struct vm_area_struct *vma)
3949{
3950 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003951 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (sk)
3955 atomic_dec(&pkt_sk(sk)->mapped);
3956}
3957
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003958static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003959 .open = packet_mm_open,
3960 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961};
3962
Neil Horman0e3125c2010-11-16 10:26:47 -08003963static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
3964 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
3966 int i;
3967
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003968 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08003969 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003970 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08003971 vfree(pg_vec[i].buffer);
3972 else
3973 free_pages((unsigned long)pg_vec[i].buffer,
3974 order);
3975 pg_vec[i].buffer = NULL;
3976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
3978 kfree(pg_vec);
3979}
3980
Olof Johanssoneea49cc92011-11-02 11:00:49 +00003981static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003982{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003983 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08003984 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
3985 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07003986
Neil Horman0e3125c2010-11-16 10:26:47 -08003987 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003988 if (buffer)
3989 return buffer;
3990
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003991 /* __get_free_pages failed, fall back to vmalloc */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00003992 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08003993 if (buffer)
3994 return buffer;
3995
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003996 /* vmalloc failed, lets dig into swap here */
Neil Horman0e3125c2010-11-16 10:26:47 -08003997 gfp_flags &= ~__GFP_NORETRY;
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003998 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003999 if (buffer)
4000 return buffer;
4001
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004002 /* complete and utter failure */
Neil Horman0e3125c2010-11-16 10:26:47 -08004003 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004004}
4005
Neil Horman0e3125c2010-11-16 10:26:47 -08004006static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004007{
4008 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004009 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004010 int i;
4011
Neil Horman0e3125c2010-11-16 10:26:47 -08004012 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004013 if (unlikely(!pg_vec))
4014 goto out;
4015
4016 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00004017 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004018 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004019 goto out_free_pgvec;
4020 }
4021
4022out:
4023 return pg_vec;
4024
4025out_free_pgvec:
4026 free_pg_vec(pg_vec, order, block_nr);
4027 pg_vec = NULL;
4028 goto out;
4029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
chetan lokef6fb8f102011-08-19 10:18:16 +00004031static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004032 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Neil Horman0e3125c2010-11-16 10:26:47 -08004034 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08004036 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004037 struct packet_ring_buffer *rb;
4038 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004039 __be16 num;
chetan lokef6fb8f102011-08-19 10:18:16 +00004040 int err = -EINVAL;
4041 /* Added to avoid minimal code churn */
4042 struct tpacket_req *req = &req_u->req;
4043
4044 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
4045 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
4046 WARN(1, "Tx-ring is not supported.\n");
4047 goto out;
4048 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004049
4050 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4051 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4052
4053 err = -EBUSY;
4054 if (!closing) {
4055 if (atomic_read(&po->mapped))
4056 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004057 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004058 goto out;
4059 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004063 err = -EBUSY;
4064 if (unlikely(rb->pg_vec))
4065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004067 switch (po->tp_version) {
4068 case TPACKET_V1:
4069 po->tp_hdrlen = TPACKET_HDRLEN;
4070 break;
4071 case TPACKET_V2:
4072 po->tp_hdrlen = TPACKET2_HDRLEN;
4073 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00004074 case TPACKET_V3:
4075 po->tp_hdrlen = TPACKET3_HDRLEN;
4076 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004077 }
4078
Johann Baudy69e3c752009-05-18 22:11:22 -07004079 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004080 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004081 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004082 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004083 goto out;
Eric Dumazetdc808112014-08-15 09:16:04 -07004084 if (po->tp_version >= TPACKET_V3 &&
4085 (int)(req->tp_block_size -
4086 BLK_PLUS_PRIV(req_u->req3.tp_sizeof_priv)) <= 0)
4087 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004088 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07004089 po->tp_reserve))
4090 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004091 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Johann Baudy69e3c752009-05-18 22:11:22 -07004094 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
4095 if (unlikely(rb->frames_per_block <= 0))
4096 goto out;
4097 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4098 req->tp_frame_nr))
4099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
4101 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004102 order = get_order(req->tp_block_size);
4103 pg_vec = alloc_pg_vec(req, order);
4104 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 goto out;
chetan lokef6fb8f102011-08-19 10:18:16 +00004106 switch (po->tp_version) {
4107 case TPACKET_V3:
4108 /* Transmit path is not supported. We checked
4109 * it above but just being paranoid
4110 */
4111 if (!tx_ring)
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304112 init_prb_bdqc(po, rb, pg_vec, req_u);
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004113 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00004114 default:
4115 break;
4116 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004117 }
4118 /* Done */
4119 else {
4120 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004121 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004122 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124
4125 lock_sock(sk);
4126
4127 /* Detach socket from network */
4128 spin_lock(&po->bind_lock);
4129 was_running = po->running;
4130 num = po->num;
4131 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004133 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 synchronize_net();
4138
4139 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004140 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 if (closing || atomic_read(&po->mapped) == 0) {
4142 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004143 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004144 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07004145 rb->frame_max = (req->tp_frame_nr - 1);
4146 rb->head = 0;
4147 rb->frame_size = req->tp_frame_size;
4148 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Changli Gaoc053fd92010-12-10 16:02:20 -08004150 swap(rb->pg_vec_order, order);
4151 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Johann Baudy69e3c752009-05-18 22:11:22 -07004153 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4154 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4155 tpacket_rcv : packet_rcv;
4156 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004158 pr_err("packet_mmap: vma is busy: %d\n",
4159 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
Herbert Xu905db442009-01-30 14:12:06 -08004161 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
4163 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004164 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004166 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
4168 spin_unlock(&po->bind_lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00004169 if (closing && (po->tp_version > TPACKET_V2)) {
4170 /* Because we don't support block-based V3 on tx-ring */
4171 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004172 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f102011-08-19 10:18:16 +00004173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 release_sock(sk);
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 if (pg_vec)
4177 free_pg_vec(pg_vec, order, req->tp_block_nr);
4178out:
4179 return err;
4180}
4181
Johann Baudy69e3c752009-05-18 22:11:22 -07004182static int packet_mmap(struct file *file, struct socket *sock,
4183 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 struct sock *sk = sock->sk;
4186 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004187 unsigned long size, expected_size;
4188 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 unsigned long start;
4190 int err = -EINVAL;
4191 int i;
4192
4193 if (vma->vm_pgoff)
4194 return -EINVAL;
4195
Herbert Xu905db442009-01-30 14:12:06 -08004196 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004197
4198 expected_size = 0;
4199 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4200 if (rb->pg_vec) {
4201 expected_size += rb->pg_vec_len
4202 * rb->pg_vec_pages
4203 * PAGE_SIZE;
4204 }
4205 }
4206
4207 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004209
4210 size = vma->vm_end - vma->vm_start;
4211 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 goto out;
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004215 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4216 if (rb->pg_vec == NULL)
4217 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004218
Johann Baudy69e3c752009-05-18 22:11:22 -07004219 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004220 struct page *page;
4221 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004222 int pg_num;
4223
Changli Gaoc56b4d92010-12-01 02:52:57 +00004224 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4225 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004226 err = vm_insert_page(vma, start, page);
4227 if (unlikely(err))
4228 goto out;
4229 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004230 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004231 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004234
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004235 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 vma->vm_ops = &packet_mmap_ops;
4237 err = 0;
4238
4239out:
Herbert Xu905db442009-01-30 14:12:06 -08004240 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return err;
4242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004244static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 .family = PF_PACKET,
4246 .owner = THIS_MODULE,
4247 .release = packet_release,
4248 .bind = packet_bind_spkt,
4249 .connect = sock_no_connect,
4250 .socketpair = sock_no_socketpair,
4251 .accept = sock_no_accept,
4252 .getname = packet_getname_spkt,
4253 .poll = datagram_poll,
4254 .ioctl = packet_ioctl,
4255 .listen = sock_no_listen,
4256 .shutdown = sock_no_shutdown,
4257 .setsockopt = sock_no_setsockopt,
4258 .getsockopt = sock_no_getsockopt,
4259 .sendmsg = packet_sendmsg_spkt,
4260 .recvmsg = packet_recvmsg,
4261 .mmap = sock_no_mmap,
4262 .sendpage = sock_no_sendpage,
4263};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004265static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 .family = PF_PACKET,
4267 .owner = THIS_MODULE,
4268 .release = packet_release,
4269 .bind = packet_bind,
4270 .connect = sock_no_connect,
4271 .socketpair = sock_no_socketpair,
4272 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004273 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 .poll = packet_poll,
4275 .ioctl = packet_ioctl,
4276 .listen = sock_no_listen,
4277 .shutdown = sock_no_shutdown,
4278 .setsockopt = packet_setsockopt,
4279 .getsockopt = packet_getsockopt,
4280 .sendmsg = packet_sendmsg,
4281 .recvmsg = packet_recvmsg,
4282 .mmap = packet_mmap,
4283 .sendpage = sock_no_sendpage,
4284};
4285
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004286static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 .family = PF_PACKET,
4288 .create = packet_create,
4289 .owner = THIS_MODULE,
4290};
4291
4292static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004293 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294};
4295
4296#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004299 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Denis V. Luneve372c412007-11-19 22:31:54 -08004301 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004302
4303 rcu_read_lock();
4304 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
4307static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4308{
Herbert Xu1bf40952007-12-16 14:04:02 -08004309 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004310 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311}
4312
4313static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004314 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315{
stephen hemminger808f5112010-02-22 07:57:18 +00004316 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004319static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
4321 if (v == SEQ_START_TOKEN)
4322 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
4323 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004324 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 const struct packet_sock *po = pkt_sk(s);
4326
4327 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004328 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 s,
4330 atomic_read(&s->sk_refcnt),
4331 s->sk_type,
4332 ntohs(po->num),
4333 po->ifindex,
4334 po->running,
4335 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004336 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004337 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
4339
4340 return 0;
4341}
4342
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004343static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 .start = packet_seq_start,
4345 .next = packet_seq_next,
4346 .stop = packet_seq_stop,
4347 .show = packet_seq_show,
4348};
4349
4350static int packet_seq_open(struct inode *inode, struct file *file)
4351{
Denis V. Luneve372c412007-11-19 22:31:54 -08004352 return seq_open_net(inode, file, &packet_seq_ops,
4353 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
4355
Arjan van de Venda7071d2007-02-12 00:55:36 -08004356static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 .owner = THIS_MODULE,
4358 .open = packet_seq_open,
4359 .read = seq_read,
4360 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08004361 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362};
4363
4364#endif
4365
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004366static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004367{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004368 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004369 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004370
Gao fengd4beaa62013-02-18 01:34:54 +00004371 if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004372 return -ENOMEM;
4373
4374 return 0;
4375}
4376
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004377static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004378{
Gao fengece31ff2013-02-18 01:34:56 +00004379 remove_proc_entry("packet", net->proc_net);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004380}
4381
4382static struct pernet_operations packet_net_ops = {
4383 .init = packet_net_init,
4384 .exit = packet_net_exit,
4385};
4386
4387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388static void __exit packet_exit(void)
4389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004391 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 sock_unregister(PF_PACKET);
4393 proto_unregister(&packet_proto);
4394}
4395
4396static int __init packet_init(void)
4397{
4398 int rc = proto_register(&packet_proto, 0);
4399
4400 if (rc != 0)
4401 goto out;
4402
4403 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004404 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406out:
4407 return rc;
4408}
4409
4410module_init(packet_init);
4411module_exit(packet_exit);
4412MODULE_LICENSE("GPL");
4413MODULE_ALIAS_NETPROTO(PF_PACKET);