blob: 8869d07013e6b9eeb97d66cd8df8228d6c17e0fc [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:
1475 idx = fanout_demux_bpf(f, skb, num);
1476 break;
David S. Miller7736d332011-07-05 01:43:20 -07001477 }
1478
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001479 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1480 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001481
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001482 po = pkt_sk(f->arr[idx]);
David S. Millerdc99f602011-07-05 01:45:05 -07001483 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1484}
1485
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001486DEFINE_MUTEX(fanout_mutex);
1487EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001488static LIST_HEAD(fanout_list);
1489
1490static void __fanout_link(struct sock *sk, struct packet_sock *po)
1491{
1492 struct packet_fanout *f = po->fanout;
1493
1494 spin_lock(&f->lock);
1495 f->arr[f->num_members] = sk;
1496 smp_wmb();
1497 f->num_members++;
1498 spin_unlock(&f->lock);
1499}
1500
1501static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1502{
1503 struct packet_fanout *f = po->fanout;
1504 int i;
1505
1506 spin_lock(&f->lock);
1507 for (i = 0; i < f->num_members; i++) {
1508 if (f->arr[i] == sk)
1509 break;
1510 }
1511 BUG_ON(i >= f->num_members);
1512 f->arr[i] = f->arr[f->num_members - 1];
1513 f->num_members--;
1514 spin_unlock(&f->lock);
1515}
1516
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001517static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001518{
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001519 if (ptype->af_packet_priv == (void *)((struct packet_sock *)sk)->fanout)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001520 return true;
1521
1522 return false;
1523}
1524
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001525static void fanout_init_data(struct packet_fanout *f)
1526{
1527 switch (f->type) {
1528 case PACKET_FANOUT_LB:
1529 atomic_set(&f->rr_cur, 0);
1530 break;
1531 case PACKET_FANOUT_CBPF:
1532 RCU_INIT_POINTER(f->bpf_prog, NULL);
1533 break;
1534 }
1535}
1536
1537static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1538{
1539 struct bpf_prog *old;
1540
1541 spin_lock(&f->lock);
1542 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1543 rcu_assign_pointer(f->bpf_prog, new);
1544 spin_unlock(&f->lock);
1545
1546 if (old) {
1547 synchronize_net();
1548 bpf_prog_destroy(old);
1549 }
1550}
1551
1552static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1553 unsigned int len)
1554{
1555 struct bpf_prog *new;
1556 struct sock_fprog fprog;
1557 int ret;
1558
1559 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1560 return -EPERM;
1561 if (len != sizeof(fprog))
1562 return -EINVAL;
1563 if (copy_from_user(&fprog, data, len))
1564 return -EFAULT;
1565
1566 ret = bpf_prog_create_from_user(&new, &fprog, NULL);
1567 if (ret)
1568 return ret;
1569
1570 __fanout_set_data_bpf(po->fanout, new);
1571 return 0;
1572}
1573
1574static int fanout_set_data(struct packet_sock *po, char __user *data,
1575 unsigned int len)
1576{
1577 switch (po->fanout->type) {
1578 case PACKET_FANOUT_CBPF:
1579 return fanout_set_data_cbpf(po, data, len);
1580 default:
1581 return -EINVAL;
1582 };
1583}
1584
1585static void fanout_release_data(struct packet_fanout *f)
1586{
1587 switch (f->type) {
1588 case PACKET_FANOUT_CBPF:
1589 __fanout_set_data_bpf(f, NULL);
1590 };
1591}
1592
David S. Miller7736d332011-07-05 01:43:20 -07001593static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001594{
1595 struct packet_sock *po = pkt_sk(sk);
1596 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001597 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001598 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001599 int err;
1600
1601 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001602 case PACKET_FANOUT_ROLLOVER:
1603 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1604 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001605 case PACKET_FANOUT_HASH:
1606 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001607 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001608 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001609 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001610 case PACKET_FANOUT_CBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001611 break;
1612 default:
1613 return -EINVAL;
1614 }
1615
1616 if (!po->running)
1617 return -EINVAL;
1618
1619 if (po->fanout)
1620 return -EALREADY;
1621
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001622 if (type == PACKET_FANOUT_ROLLOVER ||
1623 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001624 po->rollover = kzalloc(sizeof(*po->rollover), GFP_KERNEL);
1625 if (!po->rollover)
1626 return -ENOMEM;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001627 atomic_long_set(&po->rollover->num, 0);
1628 atomic_long_set(&po->rollover->num_huge, 0);
1629 atomic_long_set(&po->rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001630 }
1631
David S. Millerdc99f602011-07-05 01:45:05 -07001632 mutex_lock(&fanout_mutex);
1633 match = NULL;
1634 list_for_each_entry(f, &fanout_list, list) {
1635 if (f->id == id &&
1636 read_pnet(&f->net) == sock_net(sk)) {
1637 match = f;
1638 break;
1639 }
1640 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001641 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001642 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001643 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001644 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001645 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001646 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001647 if (!match)
1648 goto out;
1649 write_pnet(&match->net, sock_net(sk));
1650 match->id = id;
1651 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001652 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001653 INIT_LIST_HEAD(&match->list);
1654 spin_lock_init(&match->lock);
1655 atomic_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001656 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001657 match->prot_hook.type = po->prot_hook.type;
1658 match->prot_hook.dev = po->prot_hook.dev;
1659 match->prot_hook.func = packet_rcv_fanout;
1660 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001661 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001662 dev_add_pack(&match->prot_hook);
1663 list_add(&match->list, &fanout_list);
1664 }
1665 err = -EINVAL;
1666 if (match->type == type &&
1667 match->prot_hook.type == po->prot_hook.type &&
1668 match->prot_hook.dev == po->prot_hook.dev) {
1669 err = -ENOSPC;
1670 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1671 __dev_remove_pack(&po->prot_hook);
1672 po->fanout = match;
1673 atomic_inc(&match->sk_ref);
1674 __fanout_link(sk, po);
1675 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001676 }
1677 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001678out:
David S. Millerdc99f602011-07-05 01:45:05 -07001679 mutex_unlock(&fanout_mutex);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001680 if (err) {
1681 kfree(po->rollover);
1682 po->rollover = NULL;
1683 }
David S. Millerdc99f602011-07-05 01:45:05 -07001684 return err;
1685}
1686
1687static void fanout_release(struct sock *sk)
1688{
1689 struct packet_sock *po = pkt_sk(sk);
1690 struct packet_fanout *f;
1691
1692 f = po->fanout;
1693 if (!f)
1694 return;
1695
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001696 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001697 po->fanout = NULL;
1698
David S. Millerdc99f602011-07-05 01:45:05 -07001699 if (atomic_dec_and_test(&f->sk_ref)) {
1700 list_del(&f->list);
1701 dev_remove_pack(&f->prot_hook);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001702 fanout_release_data(f);
David S. Millerdc99f602011-07-05 01:45:05 -07001703 kfree(f);
1704 }
1705 mutex_unlock(&fanout_mutex);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001706
Willem de Bruijn59f21112015-06-16 12:51:37 -04001707 if (po->rollover)
1708 kfree_rcu(po->rollover, rcu);
David S. Millerdc99f602011-07-05 01:45:05 -07001709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001711static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001713static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001715static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1716 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 struct sock *sk;
1719 struct sockaddr_pkt *spkt;
1720
1721 /*
1722 * When we registered the protocol we saved the socket in the data
1723 * field for just this event.
1724 */
1725
1726 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /*
1729 * Yank back the headers [hope the device set this
1730 * right or kerboom...]
1731 *
1732 * Incoming packets have ll header pulled,
1733 * push it back.
1734 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001735 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * so that this procedure is noop.
1737 */
1738
1739 if (skb->pkt_type == PACKET_LOOPBACK)
1740 goto out;
1741
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001742 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001743 goto out;
1744
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001745 skb = skb_share_check(skb, GFP_ATOMIC);
1746 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto oom;
1748
1749 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001750 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Phil Oester84531c22005-07-12 11:57:52 -07001752 /* drop conntrack reference */
1753 nf_reset(skb);
1754
Herbert Xuffbc6112007-02-04 23:33:10 -08001755 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001757 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /*
1760 * The SOCK_PACKET socket receives _all_ frames.
1761 */
1762
1763 spkt->spkt_family = dev->type;
1764 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1765 spkt->spkt_protocol = skb->protocol;
1766
1767 /*
1768 * Charge the memory to the socket. This is done specifically
1769 * to prevent sockets using all the memory up.
1770 */
1771
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001772 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774
1775out:
1776 kfree_skb(skb);
1777oom:
1778 return 0;
1779}
1780
1781
1782/*
1783 * Output a raw packet to a device layer. This bypasses all the other
1784 * protocol layers and you must therefore supply it with a complete frame
1785 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001786
Ying Xue1b784142015-03-02 15:37:48 +08001787static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1788 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001791 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001792 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001794 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001796 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001799 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
1801
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001802 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001804 return -EINVAL;
1805 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1806 proto = saddr->spkt_protocol;
1807 } else
1808 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001811 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
1813
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001814 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001815retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001816 rcu_read_lock();
1817 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 err = -ENODEV;
1819 if (dev == NULL)
1820 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001821
David S. Millerd5e76b02007-01-25 19:30:36 -08001822 err = -ENETDOWN;
1823 if (!(dev->flags & IFF_UP))
1824 goto out_unlock;
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001827 * You may not queue a frame bigger than the mtu. This is the lowest level
1828 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001830
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001831 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1832 if (!netif_supports_nofcs(dev)) {
1833 err = -EPROTONOSUPPORT;
1834 goto out_unlock;
1835 }
1836 extra_len = 4; /* We're doing our own CRC */
1837 }
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001840 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto out_unlock;
1842
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001843 if (!skb) {
1844 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001845 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001846 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001848 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001849 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001850 if (skb == NULL)
1851 return -ENOBUFS;
1852 /* FIXME: Save some space for broken drivers that write a hard
1853 * header at transmission time by themselves. PPP is the notable
1854 * one here. This should really be fixed at the driver level.
1855 */
1856 skb_reserve(skb, reserved);
1857 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001858
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001859 /* Try to align data part correctly */
1860 if (hhlen) {
1861 skb->data -= hhlen;
1862 skb->tail -= hhlen;
1863 if (len < hhlen)
1864 skb_reset_network_header(skb);
1865 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001866 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001867 if (err)
1868 goto out_free;
1869 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001872 if (len > (dev->mtu + dev->hard_header_len + extra_len)) {
Ben Greear57f89bf2011-02-11 09:35:18 +00001873 /* Earlier code assumed this would be a VLAN pkt,
1874 * double-check this now that we have the actual
1875 * packet in hand.
1876 */
1877 struct ethhdr *ehdr;
1878 skb_reset_mac_header(skb);
1879 ehdr = eth_hdr(skb);
1880 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1881 err = -EMSGSIZE;
1882 goto out_unlock;
1883 }
1884 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 skb->protocol = proto;
1887 skb->dev = dev;
1888 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001889 skb->mark = sk->sk_mark;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00001890
1891 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001893 if (unlikely(extra_len == 4))
1894 skb->no_fcs = 1;
1895
Jason Wang40893fd2013-03-26 23:11:22 +00001896 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001899 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001900 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001903 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001904out_free:
1905 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return err;
1907}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001909static unsigned int run_filter(const struct sk_buff *skb,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001910 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -08001911 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 struct sk_filter *filter;
1914
Eric Dumazet80f8f102011-01-18 07:46:52 +00001915 rcu_read_lock();
1916 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001917 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +00001918 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001919 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
David S. Millerdbcb5852007-01-24 15:21:02 -08001921 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
1924/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00001925 * This function makes lazy skb cloning in hope that most of packets
1926 * are discarded by BPF.
1927 *
1928 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1929 * and skb->cb are mangled. It works because (and until) packets
1930 * falling here are owned by current CPU. Output packets are cloned
1931 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1932 * sequencially, so that if we return skb to original state on exit,
1933 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 */
1935
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001936static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1937 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 struct sock *sk;
1940 struct sockaddr_ll *sll;
1941 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001942 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001944 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (skb->pkt_type == PACKET_LOOPBACK)
1947 goto drop;
1948
1949 sk = pt->af_packet_priv;
1950 po = pkt_sk(sk);
1951
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001952 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001953 goto drop;
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 skb->dev = dev;
1956
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001957 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001959 * exported to higher levels.
1960 *
1961 * Otherwise, the device hides details of its frame
1962 * structure, so that corresponding packet head is
1963 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001966 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 else if (skb->pkt_type == PACKET_OUTGOING) {
1968 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001969 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 }
1972
1973 snaplen = skb->len;
1974
David S. Millerdbcb5852007-01-24 15:21:02 -08001975 res = run_filter(skb, sk, snaplen);
1976 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001977 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001978 if (snaplen > res)
1979 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001981 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 goto drop_n_acct;
1983
1984 if (skb_shared(skb)) {
1985 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1986 if (nskb == NULL)
1987 goto drop_n_acct;
1988
1989 if (skb_head != skb->data) {
1990 skb->data = skb_head;
1991 skb->len = skb_len;
1992 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00001993 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 skb = nskb;
1995 }
1996
Eyal Birgerb4772ef2015-03-01 14:58:29 +02001997 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08001998
1999 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002002 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002003 sll->sll_ifindex = orig_dev->ifindex;
2004 else
2005 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002007 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Eyal Birger2472d762015-03-01 14:58:28 +02002009 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2010 * Use their space for storing the original skb length.
2011 */
2012 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (pskb_trim(skb, snaplen))
2015 goto drop_n_acct;
2016
2017 skb_set_owner_r(skb, sk);
2018 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002019 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Phil Oester84531c22005-07-12 11:57:52 -07002021 /* drop conntrack reference */
2022 nf_reset(skb);
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002025 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002026 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 __skb_queue_tail(&sk->sk_receive_queue, skb);
2028 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002029 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 0;
2031
2032drop_n_acct:
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002033 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002034 po->stats.stats1.tp_drops++;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002035 atomic_inc(&sk->sk_drops);
2036 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038drop_n_restore:
2039 if (skb_head != skb->data && skb_shared(skb)) {
2040 skb->data = skb_head;
2041 skb->len = skb_len;
2042 }
2043drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +00002044 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return 0;
2046}
2047
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002048static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2049 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct sock *sk;
2052 struct packet_sock *po;
2053 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002054 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002055 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002057 unsigned int snaplen, res;
chetan lokef6fb8f102011-08-19 10:18:16 +00002058 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002059 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002061 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002062 __u32 ts_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Atzm Watanabe51846352013-12-17 22:53:32 +09002064 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2065 * We may add members to them until current aligned size without forcing
2066 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2067 */
2068 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2069 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (skb->pkt_type == PACKET_LOOPBACK)
2072 goto drop;
2073
2074 sk = pt->af_packet_priv;
2075 po = pkt_sk(sk);
2076
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002077 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002078 goto drop;
2079
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002080 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002082 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 else if (skb->pkt_type == PACKET_OUTGOING) {
2084 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002085 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 }
2088
2089 snaplen = skb->len;
2090
David S. Millerdbcb5852007-01-24 15:21:02 -08002091 res = run_filter(skb, sk, snaplen);
2092 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002093 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002094
2095 if (skb->ip_summed == CHECKSUM_PARTIAL)
2096 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002097 else if (skb->pkt_type != PACKET_OUTGOING &&
2098 (skb->ip_summed == CHECKSUM_COMPLETE ||
2099 skb_csum_unnecessary(skb)))
2100 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002101
David S. Millerdbcb5852007-01-24 15:21:02 -08002102 if (snaplen > res)
2103 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002106 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2107 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002109 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002110 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002111 (maclen < 16 ? 16 : maclen)) +
2112 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 macoff = netoff - maclen;
2114 }
chetan lokef6fb8f102011-08-19 10:18:16 +00002115 if (po->tp_version <= TPACKET_V2) {
2116 if (macoff + snaplen > po->rx_ring.frame_size) {
2117 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002118 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002119 if (skb_shared(skb)) {
2120 copy_skb = skb_clone(skb, GFP_ATOMIC);
2121 } else {
2122 copy_skb = skb_get(skb);
2123 skb_head = skb->data;
2124 }
2125 if (copy_skb)
2126 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
chetan lokef6fb8f102011-08-19 10:18:16 +00002128 snaplen = po->rx_ring.frame_size - macoff;
2129 if ((int)snaplen < 0)
2130 snaplen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002132 } else if (unlikely(macoff + snaplen >
2133 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2134 u32 nval;
2135
2136 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2137 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2138 snaplen, nval, macoff);
2139 snaplen = nval;
2140 if (unlikely((int)snaplen < 0)) {
2141 snaplen = 0;
2142 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00002146 h.raw = packet_current_rx_frame(po, skb,
2147 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002148 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto ring_is_full;
chetan lokef6fb8f102011-08-19 10:18:16 +00002150 if (po->tp_version <= TPACKET_V2) {
2151 packet_increment_rx_head(po, &po->rx_ring);
2152 /*
2153 * LOSING will be reported till you read the stats,
2154 * because it's COR - Clear On Read.
2155 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2156 * at packet level.
2157 */
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002158 if (po->stats.stats1.tp_drops)
chetan lokef6fb8f102011-08-19 10:18:16 +00002159 status |= TP_STATUS_LOSING;
2160 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002161 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (copy_skb) {
2163 status |= TP_STATUS_COPY;
2164 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 spin_unlock(&sk->sk_receive_queue.lock);
2167
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002168 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002169
2170 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Daniel Borkmann7a513842013-04-23 00:39:29 +00002171 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002173 status |= ts_status;
2174
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002175 switch (po->tp_version) {
2176 case TPACKET_V1:
2177 h.h1->tp_len = skb->len;
2178 h.h1->tp_snaplen = snaplen;
2179 h.h1->tp_mac = macoff;
2180 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002181 h.h1->tp_sec = ts.tv_sec;
2182 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002183 hdrlen = sizeof(*h.h1);
2184 break;
2185 case TPACKET_V2:
2186 h.h2->tp_len = skb->len;
2187 h.h2->tp_snaplen = snaplen;
2188 h.h2->tp_mac = macoff;
2189 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002190 h.h2->tp_sec = ts.tv_sec;
2191 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002192 if (skb_vlan_tag_present(skb)) {
2193 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002194 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2195 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002196 } else {
2197 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002198 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002199 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002200 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002201 hdrlen = sizeof(*h.h2);
2202 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00002203 case TPACKET_V3:
2204 /* tp_nxt_offset,vlan are already populated above.
2205 * So DONT clear those fields here
2206 */
2207 h.h3->tp_status |= status;
2208 h.h3->tp_len = skb->len;
2209 h.h3->tp_snaplen = snaplen;
2210 h.h3->tp_mac = macoff;
2211 h.h3->tp_net = netoff;
chetan lokef6fb8f102011-08-19 10:18:16 +00002212 h.h3->tp_sec = ts.tv_sec;
2213 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002214 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f102011-08-19 10:18:16 +00002215 hdrlen = sizeof(*h.h3);
2216 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002217 default:
2218 BUG();
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002221 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002222 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 sll->sll_family = AF_PACKET;
2224 sll->sll_hatype = dev->type;
2225 sll->sll_protocol = skb->protocol;
2226 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002227 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002228 sll->sll_ifindex = orig_dev->ifindex;
2229 else
2230 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Ralf Baechlee16aa202006-12-07 00:11:33 -08002232 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002233
Changli Gaof6dafa92010-12-07 04:26:16 +00002234#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002235 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002236 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002238 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2239 macoff + snaplen);
2240
2241 for (start = h.raw; start < end; start += PAGE_SIZE)
2242 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002244 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002245#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002246
Dan Collinsda413ee2014-12-19 16:49:25 +13002247 if (po->tp_version <= TPACKET_V2) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002248 __packet_set_status(po, h.raw, status);
Dan Collinsda413ee2014-12-19 16:49:25 +13002249 sk->sk_data_ready(sk);
2250 } else {
chetan lokef6fb8f102011-08-19 10:18:16 +00002251 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254drop_n_restore:
2255 if (skb_head != skb->data && skb_shared(skb)) {
2256 skb->data = skb_head;
2257 skb->len = skb_len;
2258 }
2259drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002260 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return 0;
2262
2263ring_is_full:
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002264 po->stats.stats1.tp_drops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 spin_unlock(&sk->sk_receive_queue.lock);
2266
David S. Miller676d2362014-04-11 16:15:36 -04002267 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002268 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto drop_n_restore;
2270}
2271
Johann Baudy69e3c752009-05-18 22:11:22 -07002272static void tpacket_destruct_skb(struct sk_buff *skb)
2273{
2274 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002275
Johann Baudy69e3c752009-05-18 22:11:22 -07002276 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002277 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002278 __u32 ts;
2279
Johann Baudy69e3c752009-05-18 22:11:22 -07002280 ph = skb_shinfo(skb)->destructor_arg;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002281 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002282
2283 ts = __packet_set_timestamp(po, ph, skb);
2284 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Johann Baudy69e3c752009-05-18 22:11:22 -07002285 }
2286
2287 sock_wfree(skb);
2288}
2289
Willem de Bruijn9c707762014-11-19 13:10:16 -05002290static bool ll_header_truncated(const struct net_device *dev, int len)
2291{
2292 /* net device doesn't like empty head */
2293 if (unlikely(len <= dev->hard_header_len)) {
Willem de Bruijneee2f042015-01-08 11:29:18 -05002294 net_warn_ratelimited("%s: packet size is too short (%d <= %d)\n",
Willem de Bruijn9c707762014-11-19 13:10:16 -05002295 current->comm, len, dev->hard_header_len);
2296 return true;
2297 }
2298
2299 return false;
2300}
2301
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002302static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
2303 void *frame, struct net_device *dev, int size_max,
Herbert Xuae641942011-11-18 02:20:04 +00002304 __be16 proto, unsigned char *addr, int hlen)
Johann Baudy69e3c752009-05-18 22:11:22 -07002305{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002306 union tpacket_uhdr ph;
David S. Miller09effa62013-08-07 17:11:00 -07002307 int to_write, offset, len, tp_len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002308 struct socket *sock = po->sk.sk_socket;
2309 struct page *page;
2310 void *data;
2311 int err;
2312
2313 ph.raw = frame;
2314
2315 skb->protocol = proto;
2316 skb->dev = dev;
2317 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002318 skb->mark = po->sk.sk_mark;
Willem de Bruijn2e313962013-04-23 00:39:28 +00002319 sock_tx_timestamp(&po->sk, &skb_shinfo(skb)->tx_flags);
Johann Baudy69e3c752009-05-18 22:11:22 -07002320 skb_shinfo(skb)->destructor_arg = ph.raw;
2321
2322 switch (po->tp_version) {
2323 case TPACKET_V2:
2324 tp_len = ph.h2->tp_len;
2325 break;
2326 default:
2327 tp_len = ph.h1->tp_len;
2328 break;
2329 }
David S. Miller09effa62013-08-07 17:11:00 -07002330 if (unlikely(tp_len > size_max)) {
2331 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2332 return -EMSGSIZE;
2333 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002334
Herbert Xuae641942011-11-18 02:20:04 +00002335 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002336 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002337
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002338 if (!packet_use_direct_xmit(po))
2339 skb_probe_transport_header(skb, 0);
2340 if (unlikely(po->tp_tx_has_off)) {
Paul Chavent5920cd3a2012-11-06 23:10:47 +00002341 int off_min, off_max, off;
2342 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2343 off_max = po->tx_ring.frame_size - tp_len;
2344 if (sock->type == SOCK_DGRAM) {
2345 switch (po->tp_version) {
2346 case TPACKET_V2:
2347 off = ph.h2->tp_net;
2348 break;
2349 default:
2350 off = ph.h1->tp_net;
2351 break;
2352 }
2353 } else {
2354 switch (po->tp_version) {
2355 case TPACKET_V2:
2356 off = ph.h2->tp_mac;
2357 break;
2358 default:
2359 off = ph.h1->tp_mac;
2360 break;
2361 }
2362 }
2363 if (unlikely((off < off_min) || (off_max < off)))
2364 return -EINVAL;
2365 data = ph.raw + off;
2366 } else {
2367 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
2368 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002369 to_write = tp_len;
2370
2371 if (sock->type == SOCK_DGRAM) {
2372 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2373 NULL, tp_len);
2374 if (unlikely(err < 0))
2375 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002376 } else if (dev->hard_header_len) {
Willem de Bruijn9c707762014-11-19 13:10:16 -05002377 if (ll_header_truncated(dev, tp_len))
Johann Baudy69e3c752009-05-18 22:11:22 -07002378 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002379
2380 skb_push(skb, dev->hard_header_len);
2381 err = skb_store_bits(skb, 0, data,
2382 dev->hard_header_len);
2383 if (unlikely(err))
2384 return err;
2385
2386 data += dev->hard_header_len;
2387 to_write -= dev->hard_header_len;
2388 }
2389
Johann Baudy69e3c752009-05-18 22:11:22 -07002390 offset = offset_in_page(data);
2391 len_max = PAGE_SIZE - offset;
2392 len = ((to_write > len_max) ? len_max : to_write);
2393
2394 skb->data_len = to_write;
2395 skb->len += to_write;
2396 skb->truesize += to_write;
2397 atomic_add(to_write, &po->sk.sk_wmem_alloc);
2398
2399 while (likely(to_write)) {
2400 nr_frags = skb_shinfo(skb)->nr_frags;
2401
2402 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002403 pr_err("Packet exceed the number of skb frags(%lu)\n",
2404 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002405 return -EFAULT;
2406 }
2407
Changli Gao0af55bb2010-12-01 02:52:20 +00002408 page = pgv_to_page(data);
2409 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002410 flush_dcache_page(page);
2411 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002412 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002413 to_write -= len;
2414 offset = 0;
2415 len_max = PAGE_SIZE;
2416 len = ((to_write > len_max) ? len_max : to_write);
2417 }
2418
2419 return tp_len;
2420}
2421
2422static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2423{
Johann Baudy69e3c752009-05-18 22:11:22 -07002424 struct sk_buff *skb;
2425 struct net_device *dev;
2426 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002427 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002428 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002429 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002430 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Johann Baudy69e3c752009-05-18 22:11:22 -07002431 int tp_len, size_max;
2432 unsigned char *addr;
2433 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002434 int status = TP_STATUS_AVAILABLE;
Herbert Xuae641942011-11-18 02:20:04 +00002435 int hlen, tlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002436
Johann Baudy69e3c752009-05-18 22:11:22 -07002437 mutex_lock(&po->pg_vec_lock);
2438
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002439 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002440 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002441 proto = po->num;
2442 addr = NULL;
2443 } else {
2444 err = -EINVAL;
2445 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2446 goto out;
2447 if (msg->msg_namelen < (saddr->sll_halen
2448 + offsetof(struct sockaddr_ll,
2449 sll_addr)))
2450 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002451 proto = saddr->sll_protocol;
2452 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002453 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Johann Baudy69e3c752009-05-18 22:11:22 -07002454 }
2455
Johann Baudy69e3c752009-05-18 22:11:22 -07002456 err = -ENXIO;
2457 if (unlikely(dev == NULL))
2458 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002459 err = -ENETDOWN;
2460 if (unlikely(!(dev->flags & IFF_UP)))
2461 goto out_put;
2462
Daniel Borkmann52f14542014-02-28 02:22:06 +01002463 reserve = dev->hard_header_len + VLAN_HLEN;
Johann Baudy69e3c752009-05-18 22:11:22 -07002464 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002465 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002466
David S. Miller09effa62013-08-07 17:11:00 -07002467 if (size_max > dev->mtu + reserve)
2468 size_max = dev->mtu + reserve;
2469
Johann Baudy69e3c752009-05-18 22:11:22 -07002470 do {
2471 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002472 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002473 if (unlikely(ph == NULL)) {
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002474 if (need_wait && need_resched())
2475 schedule();
Johann Baudy69e3c752009-05-18 22:11:22 -07002476 continue;
2477 }
2478
2479 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002480 hlen = LL_RESERVED_SPACE(dev);
2481 tlen = dev->needed_tailroom;
Johann Baudy69e3c752009-05-18 22:11:22 -07002482 skb = sock_alloc_send_skb(&po->sk,
Herbert Xuae641942011-11-18 02:20:04 +00002483 hlen + tlen + sizeof(struct sockaddr_ll),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002484 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002485
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002486 if (unlikely(skb == NULL)) {
2487 /* we assume the socket was initially writeable ... */
2488 if (likely(len_sum > 0))
2489 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002490 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002491 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002492 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
Daniel Borkmann52f14542014-02-28 02:22:06 +01002493 addr, hlen);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002494 if (likely(tp_len >= 0) &&
2495 tp_len > dev->mtu + dev->hard_header_len) {
Daniel Borkmann52f14542014-02-28 02:22:06 +01002496 struct ethhdr *ehdr;
2497 /* Earlier code assumed this would be a VLAN pkt,
2498 * double-check this now that we have the actual
2499 * packet in hand.
2500 */
Johann Baudy69e3c752009-05-18 22:11:22 -07002501
Daniel Borkmann52f14542014-02-28 02:22:06 +01002502 skb_reset_mac_header(skb);
2503 ehdr = eth_hdr(skb);
2504 if (ehdr->h_proto != htons(ETH_P_8021Q))
2505 tp_len = -EMSGSIZE;
2506 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002507 if (unlikely(tp_len < 0)) {
2508 if (po->tp_loss) {
2509 __packet_set_status(po, ph,
2510 TP_STATUS_AVAILABLE);
2511 packet_increment_head(&po->tx_ring);
2512 kfree_skb(skb);
2513 continue;
2514 } else {
2515 status = TP_STATUS_WRONG_FORMAT;
2516 err = tp_len;
2517 goto out_status;
2518 }
2519 }
2520
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002521 packet_pick_tx_queue(dev, skb);
2522
Johann Baudy69e3c752009-05-18 22:11:22 -07002523 skb->destructor = tpacket_destruct_skb;
2524 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002525 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002526
2527 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002528 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002529 if (unlikely(err > 0)) {
2530 err = net_xmit_errno(err);
2531 if (err && __packet_get_status(po, ph) ==
2532 TP_STATUS_AVAILABLE) {
2533 /* skb was destructed already */
2534 skb = NULL;
2535 goto out_status;
2536 }
2537 /*
2538 * skb was dropped but not destructed yet;
2539 * let's treat it like congestion or err < 0
2540 */
2541 err = 0;
2542 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002543 packet_increment_head(&po->tx_ring);
2544 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002545 } while (likely((ph != NULL) ||
2546 /* Note: packet_read_pending() might be slow if we have
2547 * to call it as it's per_cpu variable, but in fast-path
2548 * we already short-circuit the loop with the first
2549 * condition, and luckily don't have to go that path
2550 * anyway.
2551 */
2552 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002553
2554 err = len_sum;
2555 goto out_put;
2556
Johann Baudy69e3c752009-05-18 22:11:22 -07002557out_status:
2558 __packet_set_status(po, ph, status);
2559 kfree_skb(skb);
2560out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002561 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002562out:
2563 mutex_unlock(&po->pg_vec_lock);
2564 return err;
2565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002567static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2568 size_t reserve, size_t len,
2569 size_t linear, int noblock,
2570 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002571{
2572 struct sk_buff *skb;
2573
2574 /* Under a page? Don't bother with paged skb. */
2575 if (prepad + len < PAGE_SIZE || !linear)
2576 linear = len;
2577
2578 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002579 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002580 if (!skb)
2581 return NULL;
2582
2583 skb_reserve(skb, reserve);
2584 skb_put(skb, linear);
2585 skb->data_len = len - linear;
2586 skb->len += len - linear;
2587
2588 return skb;
2589}
2590
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002591static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002594 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 struct sk_buff *skb;
2596 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002597 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00002599 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002600 struct virtio_net_hdr vnet_hdr = { 0 };
2601 int offset = 0;
2602 int vnet_hdr_len;
2603 struct packet_sock *po = pkt_sk(sk);
2604 unsigned short gso_type = 0;
Herbert Xuae641942011-11-18 02:20:04 +00002605 int hlen, tlen;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002606 int extra_len = 0;
Al Viro8feb2fb2014-11-06 01:10:59 -05002607 ssize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002610 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002612
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002613 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002614 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 proto = po->num;
2616 addr = NULL;
2617 } else {
2618 err = -EINVAL;
2619 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2620 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002621 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 proto = saddr->sll_protocol;
2624 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002625 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002629 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002631 err = -ENETDOWN;
2632 if (unlikely(!(dev->flags & IFF_UP)))
2633 goto out_unlock;
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (sock->type == SOCK_RAW)
2636 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002637 if (po->has_vnet_hdr) {
2638 vnet_hdr_len = sizeof(vnet_hdr);
2639
2640 err = -EINVAL;
2641 if (len < vnet_hdr_len)
2642 goto out_unlock;
2643
2644 len -= vnet_hdr_len;
2645
Al Viro8feb2fb2014-11-06 01:10:59 -05002646 err = -EFAULT;
Al Viroc0371da2014-11-24 10:42:55 -05002647 n = copy_from_iter(&vnet_hdr, vnet_hdr_len, &msg->msg_iter);
Al Viro8feb2fb2014-11-06 01:10:59 -05002648 if (n != vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002649 goto out_unlock;
2650
2651 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002652 (__virtio16_to_cpu(false, vnet_hdr.csum_start) +
2653 __virtio16_to_cpu(false, vnet_hdr.csum_offset) + 2 >
2654 __virtio16_to_cpu(false, vnet_hdr.hdr_len)))
2655 vnet_hdr.hdr_len = __cpu_to_virtio16(false,
2656 __virtio16_to_cpu(false, vnet_hdr.csum_start) +
2657 __virtio16_to_cpu(false, vnet_hdr.csum_offset) + 2);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002658
2659 err = -EINVAL;
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002660 if (__virtio16_to_cpu(false, vnet_hdr.hdr_len) > len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002661 goto out_unlock;
2662
2663 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2664 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2665 case VIRTIO_NET_HDR_GSO_TCPV4:
2666 gso_type = SKB_GSO_TCPV4;
2667 break;
2668 case VIRTIO_NET_HDR_GSO_TCPV6:
2669 gso_type = SKB_GSO_TCPV6;
2670 break;
2671 case VIRTIO_NET_HDR_GSO_UDP:
2672 gso_type = SKB_GSO_UDP;
2673 break;
2674 default:
2675 goto out_unlock;
2676 }
2677
2678 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
2679 gso_type |= SKB_GSO_TCP_ECN;
2680
2681 if (vnet_hdr.gso_size == 0)
2682 goto out_unlock;
2683
2684 }
2685 }
2686
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002687 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2688 if (!netif_supports_nofcs(dev)) {
2689 err = -EPROTONOSUPPORT;
2690 goto out_unlock;
2691 }
2692 extra_len = 4; /* We're doing our own CRC */
2693 }
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002696 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 goto out_unlock;
2698
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002699 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002700 hlen = LL_RESERVED_SPACE(dev);
2701 tlen = dev->needed_tailroom;
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002702 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len,
2703 __virtio16_to_cpu(false, vnet_hdr.hdr_len),
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002704 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002705 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 goto out_unlock;
2707
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002708 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002710 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002711 if (sock->type == SOCK_DGRAM) {
2712 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002713 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002714 goto out_free;
2715 } else {
2716 if (ll_header_truncated(dev, len))
2717 goto out_free;
2718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002721 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 if (err)
2723 goto out_free;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00002724
2725 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002727 if (!gso_type && (len > dev->mtu + reserve + extra_len)) {
David S. Miller09effa62013-08-07 17:11:00 -07002728 /* Earlier code assumed this would be a VLAN pkt,
2729 * double-check this now that we have the actual
2730 * packet in hand.
2731 */
2732 struct ethhdr *ehdr;
2733 skb_reset_mac_header(skb);
2734 ehdr = eth_hdr(skb);
2735 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
2736 err = -EMSGSIZE;
2737 goto out_free;
2738 }
Ben Greear57f89bf2011-02-11 09:35:18 +00002739 }
2740
David S. Miller09effa62013-08-07 17:11:00 -07002741 skb->protocol = proto;
2742 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002744 skb->mark = sk->sk_mark;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002745
2746 packet_pick_tx_queue(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002748 if (po->has_vnet_hdr) {
2749 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002750 u16 s = __virtio16_to_cpu(false, vnet_hdr.csum_start);
2751 u16 o = __virtio16_to_cpu(false, vnet_hdr.csum_offset);
2752 if (!skb_partial_csum_set(skb, s, o)) {
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002753 err = -EINVAL;
2754 goto out_free;
2755 }
2756 }
2757
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02002758 skb_shinfo(skb)->gso_size =
2759 __virtio16_to_cpu(false, vnet_hdr.gso_size);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002760 skb_shinfo(skb)->gso_type = gso_type;
2761
2762 /* Header must be checked, and gso_segs computed. */
2763 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2764 skb_shinfo(skb)->gso_segs = 0;
2765
2766 len += vnet_hdr_len;
2767 }
2768
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002769 if (!packet_use_direct_xmit(po))
2770 skb_probe_transport_header(skb, reserve);
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002771 if (unlikely(extra_len == 4))
2772 skb->no_fcs = 1;
2773
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002774 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2776 goto out_unlock;
2777
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002778 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002780 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782out_free:
2783 kfree_skb(skb);
2784out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002785 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 dev_put(dev);
2787out:
2788 return err;
2789}
2790
Ying Xue1b784142015-03-02 15:37:48 +08002791static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07002792{
Johann Baudy69e3c752009-05-18 22:11:22 -07002793 struct sock *sk = sock->sk;
2794 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002795
Johann Baudy69e3c752009-05-18 22:11:22 -07002796 if (po->tx_ring.pg_vec)
2797 return tpacket_snd(po, msg);
2798 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002799 return packet_snd(sock, msg, len);
2800}
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802/*
2803 * Close a PACKET socket. This is fairly simple. We immediately go
2804 * to 'closed' state and remove our protocol entry in the device list.
2805 */
2806
2807static int packet_release(struct socket *sock)
2808{
2809 struct sock *sk = sock->sk;
2810 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002811 struct net *net;
chetan lokef6fb8f102011-08-19 10:18:16 +00002812 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 if (!sk)
2815 return 0;
2816
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002817 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 po = pkt_sk(sk);
2819
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002820 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002821 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002822 mutex_unlock(&net->packet.sklist_lock);
2823
2824 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08002825 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002826 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
stephen hemminger808f5112010-02-22 07:57:18 +00002828 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002829 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002830 packet_cached_dev_reset(po);
2831
Ben Greear160ff182011-06-01 07:18:52 +00002832 if (po->prot_hook.dev) {
2833 dev_put(po->prot_hook.dev);
2834 po->prot_hook.dev = NULL;
2835 }
stephen hemminger808f5112010-02-22 07:57:18 +00002836 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Phil Sutter9665d5d2013-02-01 07:21:41 +00002840 if (po->rx_ring.pg_vec) {
2841 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00002842 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002843 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002844
Phil Sutter9665d5d2013-02-01 07:21:41 +00002845 if (po->tx_ring.pg_vec) {
2846 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00002847 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
David S. Millerdc99f602011-07-05 01:45:05 -07002850 fanout_release(sk);
2851
stephen hemminger808f5112010-02-22 07:57:18 +00002852 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 /*
2854 * Now the socket is dead. No more input will appear.
2855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 sock_orphan(sk);
2857 sock->sk = NULL;
2858
2859 /* Purge queues */
2860
2861 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002862 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08002863 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 sock_put(sk);
2866 return 0;
2867}
2868
2869/*
2870 * Attach a packet hook.
2871 */
2872
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002873static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03002876 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002877 __be16 proto_curr;
2878 bool need_rehook;
David S. Millerdc99f602011-07-05 01:45:05 -07002879
Wei Yongjunaef950b2011-12-27 22:32:41 -05002880 if (po->fanout) {
2881 if (dev)
2882 dev_put(dev);
2883
David S. Millerdc99f602011-07-05 01:45:05 -07002884 return -EINVAL;
Wei Yongjunaef950b2011-12-27 22:32:41 -05002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 spin_lock(&po->bind_lock);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002889
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002890 proto_curr = po->prot_hook.type;
2891 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002893 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002895 if (need_rehook) {
2896 unregister_prot_hook(sk, true);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002897
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002898 po->num = proto;
2899 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002900 po->prot_hook.dev = dev;
2901
2902 po->ifindex = dev ? dev->ifindex : 0;
2903 packet_cached_dev_assign(po, dev);
2904 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03002905 if (dev_curr)
2906 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01002907
2908 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 goto out_unlock;
2910
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002911 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07002912 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002913 } else {
2914 sk->sk_err = ENETDOWN;
2915 if (!sock_flag(sk, SOCK_DEAD))
2916 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918
2919out_unlock:
2920 spin_unlock(&po->bind_lock);
2921 release_sock(sk);
2922 return 0;
2923}
2924
2925/*
2926 * Bind a packet socket to a device
2927 */
2928
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002929static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
2930 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002932 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 char name[15];
2934 struct net_device *dev;
2935 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 /*
2938 * Check legality
2939 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002940
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002941 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002943 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002945 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00002946 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return err;
2949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2952{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002953 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
2954 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct net_device *dev = NULL;
2956 int err;
2957
2958
2959 /*
2960 * Check legality
2961 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (addr_len < sizeof(struct sockaddr_ll))
2964 return -EINVAL;
2965 if (sll->sll_family != AF_PACKET)
2966 return -EINVAL;
2967
2968 if (sll->sll_ifindex) {
2969 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002970 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (dev == NULL)
2972 goto out;
2973 }
2974 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976out:
2977 return err;
2978}
2979
2980static struct proto packet_proto = {
2981 .name = "PACKET",
2982 .owner = THIS_MODULE,
2983 .obj_size = sizeof(struct packet_sock),
2984};
2985
2986/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002987 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
2989
Eric Paris3f378b62009-11-05 22:18:14 -08002990static int packet_create(struct net *net, struct socket *sock, int protocol,
2991 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct sock *sk;
2994 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08002995 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 int err;
2997
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002998 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003000 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3001 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return -ESOCKTNOSUPPORT;
3003
3004 sock->state = SS_UNCONNECTED;
3005
3006 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003007 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (sk == NULL)
3009 goto out;
3010
3011 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (sock->type == SOCK_PACKET)
3013 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 sock_init_data(sock, sk);
3016
3017 po = pkt_sk(sk);
3018 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003019 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003020 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003021
Daniel Borkmannb0138402014-01-15 16:25:36 +01003022 err = packet_alloc_pending(po);
3023 if (err)
3024 goto out2;
3025
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003026 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003029 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /*
3032 * Attach a protocol block
3033 */
3034
3035 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003036 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003037 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (sock->type == SOCK_PACKET)
3041 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 po->prot_hook.af_packet_priv = sk;
3044
Al Viro0e11c912006-11-08 00:26:29 -08003045 if (proto) {
3046 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07003047 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003050 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00003051 sk_add_node_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003052 mutex_unlock(&net->packet.sklist_lock);
3053
3054 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003055 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003056 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003057
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003058 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003059out2:
3060 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061out:
3062 return err;
3063}
3064
3065/*
3066 * Pull a packet from our receive queue and hand it to the user.
3067 * If necessary we block.
3068 */
3069
Ying Xue1b784142015-03-02 15:37:48 +08003070static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3071 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 struct sock *sk = sock->sk;
3074 struct sk_buff *skb;
3075 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003076 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003077 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003080 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 goto out;
3082
3083#if 0
3084 /* What error should we return now? EUNATTACH? */
3085 if (pkt_sk(sk)->ifindex < 0)
3086 return -ENODEV;
3087#endif
3088
Richard Cochraned85b562010-04-07 22:41:28 +00003089 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003090 err = sock_recv_errqueue(sk, msg, len,
3091 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003092 goto out;
3093 }
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 * Call the generic datagram receiver. This handles all sorts
3097 * of horrible races and re-entrancy so we can forget about it
3098 * in the protocol layers.
3099 *
3100 * Now it will return ENETDOWN, if device have just gone down,
3101 * but then it will block.
3102 */
3103
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003104 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003107 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * handles the blocking we don't see and worry about blocking
3109 * retries.
3110 */
3111
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003112 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 goto out;
3114
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003115 if (pkt_sk(sk)->pressure)
3116 packet_rcv_has_room(pkt_sk(sk), NULL);
3117
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003118 if (pkt_sk(sk)->has_vnet_hdr) {
3119 struct virtio_net_hdr vnet_hdr = { 0 };
3120
3121 err = -EINVAL;
3122 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00003123 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003124 goto out_free;
3125
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00003126 len -= vnet_hdr_len;
3127
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003128 if (skb_is_gso(skb)) {
3129 struct skb_shared_info *sinfo = skb_shinfo(skb);
3130
3131 /* This is a hint as to how much should be linear. */
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02003132 vnet_hdr.hdr_len =
3133 __cpu_to_virtio16(false, skb_headlen(skb));
3134 vnet_hdr.gso_size =
3135 __cpu_to_virtio16(false, sinfo->gso_size);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003136 if (sinfo->gso_type & SKB_GSO_TCPV4)
3137 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
3138 else if (sinfo->gso_type & SKB_GSO_TCPV6)
3139 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
3140 else if (sinfo->gso_type & SKB_GSO_UDP)
3141 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
3142 else if (sinfo->gso_type & SKB_GSO_FCOE)
3143 goto out_free;
3144 else
3145 BUG();
3146 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
3147 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
3148 } else
3149 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
3150
3151 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3152 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michael S. Tsirkindc9e5152014-11-23 18:16:19 +02003153 vnet_hdr.csum_start = __cpu_to_virtio16(false,
3154 skb_checksum_start_offset(skb));
3155 vnet_hdr.csum_offset = __cpu_to_virtio16(false,
3156 skb->csum_offset);
Jason Wang10a8d942011-06-10 00:56:17 +00003157 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
3158 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003159 } /* else everything is zero */
3160
Al Viro7eab8d92014-04-06 21:51:23 -04003161 err = memcpy_to_msg(msg, (void *)&vnet_hdr, vnet_hdr_len);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003162 if (err < 0)
3163 goto out_free;
3164 }
3165
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003166 /* You lose any data beyond the buffer you gave. If it worries
3167 * a user program they can ask the device for its MTU
3168 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003171 if (copied > len) {
3172 copied = len;
3173 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175
David S. Miller51f3d022014-11-05 16:46:40 -05003176 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (err)
3178 goto out_free;
3179
Eyal Birger2472d762015-03-01 14:58:28 +02003180 if (sock->type != SOCK_PACKET) {
3181 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3182
3183 /* Original length was stored in sockaddr_ll fields */
3184 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3185 sll->sll_family = AF_PACKET;
3186 sll->sll_protocol = skb->protocol;
3187 }
3188
Neil Horman3b885782009-10-12 13:26:31 -07003189 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003191 if (msg->msg_name) {
3192 /* If the address length field is there to be filled
3193 * in, we fill it in now.
3194 */
3195 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003196 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003197 msg->msg_namelen = sizeof(struct sockaddr_pkt);
3198 } else {
3199 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003200
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003201 msg->msg_namelen = sll->sll_halen +
3202 offsetof(struct sockaddr_ll, sll_addr);
3203 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003204 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
3205 msg->msg_namelen);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Herbert Xu8dc41942007-02-04 23:31:32 -08003208 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003209 struct tpacket_auxdata aux;
3210
3211 aux.tp_status = TP_STATUS_USER;
3212 if (skb->ip_summed == CHECKSUM_PARTIAL)
3213 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003214 else if (skb->pkt_type != PACKET_OUTGOING &&
3215 (skb->ip_summed == CHECKSUM_COMPLETE ||
3216 skb_csum_unnecessary(skb)))
3217 aux.tp_status |= TP_STATUS_CSUM_VALID;
3218
Eyal Birger2472d762015-03-01 14:58:28 +02003219 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003220 aux.tp_snaplen = skb->len;
3221 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003222 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003223 if (skb_vlan_tag_present(skb)) {
3224 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003225 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3226 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003227 } else {
3228 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003229 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003230 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003231 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003232 }
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 /*
3235 * Free or return the buffer as appropriate. Again this
3236 * hides all the races and re-entrancy issues from us.
3237 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003238 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240out_free:
3241 skb_free_datagram(sk, skb);
3242out:
3243 return err;
3244}
3245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
3247 int *uaddr_len, int peer)
3248{
3249 struct net_device *dev;
3250 struct sock *sk = sock->sk;
3251
3252 if (peer)
3253 return -EOPNOTSUPP;
3254
3255 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003256 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003257 rcu_read_lock();
3258 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3259 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003260 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003261 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 *uaddr_len = sizeof(*uaddr);
3263
3264 return 0;
3265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
3268 int *uaddr_len, int peer)
3269{
3270 struct net_device *dev;
3271 struct sock *sk = sock->sk;
3272 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003273 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 if (peer)
3276 return -EOPNOTSUPP;
3277
3278 sll->sll_family = AF_PACKET;
3279 sll->sll_ifindex = po->ifindex;
3280 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003281 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003282 rcu_read_lock();
3283 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (dev) {
3285 sll->sll_hatype = dev->type;
3286 sll->sll_halen = dev->addr_len;
3287 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 } else {
3289 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3290 sll->sll_halen = 0;
3291 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003292 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003293 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 return 0;
3296}
3297
Wang Chen2aeb0b82008-07-14 20:49:46 -07003298static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3299 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
3301 switch (i->type) {
3302 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003303 if (i->alen != dev->addr_len)
3304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003306 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003308 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 break;
3310 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003311 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003313 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003314 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003315 if (i->alen != dev->addr_len)
3316 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003317 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003318 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003319 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003320 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003321 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003322 default:
3323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003328static void packet_dev_mclist_delete(struct net_device *dev,
3329 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003331 struct packet_mclist *ml;
3332
3333 while ((ml = *mlp) != NULL) {
3334 if (ml->ifindex == dev->ifindex) {
3335 packet_dev_mc(dev, ml, -1);
3336 *mlp = ml->next;
3337 kfree(ml);
3338 } else
3339 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
3341}
3342
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003343static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 struct packet_sock *po = pkt_sk(sk);
3346 struct packet_mclist *ml, *i;
3347 struct net_device *dev;
3348 int err;
3349
3350 rtnl_lock();
3351
3352 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003353 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (!dev)
3355 goto done;
3356
3357 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003358 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 goto done;
3360
3361 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003362 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (i == NULL)
3364 goto done;
3365
3366 err = 0;
3367 for (ml = po->mclist; ml; ml = ml->next) {
3368 if (ml->ifindex == mreq->mr_ifindex &&
3369 ml->type == mreq->mr_type &&
3370 ml->alen == mreq->mr_alen &&
3371 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3372 ml->count++;
3373 /* Free the new element ... */
3374 kfree(i);
3375 goto done;
3376 }
3377 }
3378
3379 i->type = mreq->mr_type;
3380 i->ifindex = mreq->mr_ifindex;
3381 i->alen = mreq->mr_alen;
3382 memcpy(i->addr, mreq->mr_address, i->alen);
3383 i->count = 1;
3384 i->next = po->mclist;
3385 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003386 err = packet_dev_mc(dev, i, 1);
3387 if (err) {
3388 po->mclist = i->next;
3389 kfree(i);
3390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392done:
3393 rtnl_unlock();
3394 return err;
3395}
3396
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003397static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 struct packet_mclist *ml, **mlp;
3400
3401 rtnl_lock();
3402
3403 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3404 if (ml->ifindex == mreq->mr_ifindex &&
3405 ml->type == mreq->mr_type &&
3406 ml->alen == mreq->mr_alen &&
3407 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3408 if (--ml->count == 0) {
3409 struct net_device *dev;
3410 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003411 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3412 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 kfree(ml);
3415 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
3418 }
3419 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
3423static void packet_flush_mclist(struct sock *sk)
3424{
3425 struct packet_sock *po = pkt_sk(sk);
3426 struct packet_mclist *ml;
3427
3428 if (!po->mclist)
3429 return;
3430
3431 rtnl_lock();
3432 while ((ml = po->mclist) != NULL) {
3433 struct net_device *dev;
3434
3435 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003436 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3437 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 kfree(ml);
3440 }
3441 rtnl_unlock();
3442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
3444static int
David S. Millerb7058842009-09-30 16:12:20 -07003445packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
3447 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003448 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 int ret;
3450
3451 if (level != SOL_PACKET)
3452 return -ENOPROTOOPT;
3453
Johann Baudy69e3c752009-05-18 22:11:22 -07003454 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003455 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 case PACKET_DROP_MEMBERSHIP:
3457 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003458 struct packet_mreq_max mreq;
3459 int len = optlen;
3460 memset(&mreq, 0, sizeof(mreq));
3461 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003463 if (len > sizeof(mreq))
3464 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003465 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003467 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (optname == PACKET_ADD_MEMBERSHIP)
3470 ret = packet_mc_add(sk, &mreq);
3471 else
3472 ret = packet_mc_drop(sk, &mreq);
3473 return ret;
3474 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003477 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 {
chetan lokef6fb8f102011-08-19 10:18:16 +00003479 union tpacket_req_u req_u;
3480 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
chetan lokef6fb8f102011-08-19 10:18:16 +00003482 switch (po->tp_version) {
3483 case TPACKET_V1:
3484 case TPACKET_V2:
3485 len = sizeof(req_u.req);
3486 break;
3487 case TPACKET_V3:
3488 default:
3489 len = sizeof(req_u.req3);
3490 break;
3491 }
3492 if (optlen < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003494 if (pkt_sk(sk)->has_vnet_hdr)
3495 return -EINVAL;
chetan lokef6fb8f102011-08-19 10:18:16 +00003496 if (copy_from_user(&req_u.req, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return -EFAULT;
chetan lokef6fb8f102011-08-19 10:18:16 +00003498 return packet_set_ring(sk, &req_u, 0,
3499 optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501 case PACKET_COPY_THRESH:
3502 {
3503 int val;
3504
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003505 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003507 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return -EFAULT;
3509
3510 pkt_sk(sk)->copy_thresh = val;
3511 return 0;
3512 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003513 case PACKET_VERSION:
3514 {
3515 int val;
3516
3517 if (optlen != sizeof(val))
3518 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003519 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003520 return -EBUSY;
3521 if (copy_from_user(&val, optval, sizeof(val)))
3522 return -EFAULT;
3523 switch (val) {
3524 case TPACKET_V1:
3525 case TPACKET_V2:
chetan lokef6fb8f102011-08-19 10:18:16 +00003526 case TPACKET_V3:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003527 po->tp_version = val;
3528 return 0;
3529 default:
3530 return -EINVAL;
3531 }
3532 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003533 case PACKET_RESERVE:
3534 {
3535 unsigned int val;
3536
3537 if (optlen != sizeof(val))
3538 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003539 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07003540 return -EBUSY;
3541 if (copy_from_user(&val, optval, sizeof(val)))
3542 return -EFAULT;
3543 po->tp_reserve = val;
3544 return 0;
3545 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003546 case PACKET_LOSS:
3547 {
3548 unsigned int val;
3549
3550 if (optlen != sizeof(val))
3551 return -EINVAL;
3552 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3553 return -EBUSY;
3554 if (copy_from_user(&val, optval, sizeof(val)))
3555 return -EFAULT;
3556 po->tp_loss = !!val;
3557 return 0;
3558 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003559 case PACKET_AUXDATA:
3560 {
3561 int val;
3562
3563 if (optlen < sizeof(val))
3564 return -EINVAL;
3565 if (copy_from_user(&val, optval, sizeof(val)))
3566 return -EFAULT;
3567
3568 po->auxdata = !!val;
3569 return 0;
3570 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003571 case PACKET_ORIGDEV:
3572 {
3573 int val;
3574
3575 if (optlen < sizeof(val))
3576 return -EINVAL;
3577 if (copy_from_user(&val, optval, sizeof(val)))
3578 return -EFAULT;
3579
3580 po->origdev = !!val;
3581 return 0;
3582 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003583 case PACKET_VNET_HDR:
3584 {
3585 int val;
3586
3587 if (sock->type != SOCK_RAW)
3588 return -EINVAL;
3589 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3590 return -EBUSY;
3591 if (optlen < sizeof(val))
3592 return -EINVAL;
3593 if (copy_from_user(&val, optval, sizeof(val)))
3594 return -EFAULT;
3595
3596 po->has_vnet_hdr = !!val;
3597 return 0;
3598 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003599 case PACKET_TIMESTAMP:
3600 {
3601 int val;
3602
3603 if (optlen != sizeof(val))
3604 return -EINVAL;
3605 if (copy_from_user(&val, optval, sizeof(val)))
3606 return -EFAULT;
3607
3608 po->tp_tstamp = val;
3609 return 0;
3610 }
David S. Millerdc99f602011-07-05 01:45:05 -07003611 case PACKET_FANOUT:
3612 {
3613 int val;
3614
3615 if (optlen != sizeof(val))
3616 return -EINVAL;
3617 if (copy_from_user(&val, optval, sizeof(val)))
3618 return -EFAULT;
3619
3620 return fanout_add(sk, val & 0xffff, val >> 16);
3621 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003622 case PACKET_FANOUT_DATA:
3623 {
3624 if (!po->fanout)
3625 return -EINVAL;
3626
3627 return fanout_set_data(po, optval, optlen);
3628 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003629 case PACKET_TX_HAS_OFF:
3630 {
3631 unsigned int val;
3632
3633 if (optlen != sizeof(val))
3634 return -EINVAL;
3635 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3636 return -EBUSY;
3637 if (copy_from_user(&val, optval, sizeof(val)))
3638 return -EFAULT;
3639 po->tp_tx_has_off = !!val;
3640 return 0;
3641 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003642 case PACKET_QDISC_BYPASS:
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 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3652 return 0;
3653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 default:
3655 return -ENOPROTOOPT;
3656 }
3657}
3658
3659static int packet_getsockopt(struct socket *sock, int level, int optname,
3660 char __user *optval, int __user *optlen)
3661{
3662 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003663 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 struct sock *sk = sock->sk;
3665 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003666 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003667 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003668 struct tpacket_rollover_stats rstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670 if (level != SOL_PACKET)
3671 return -ENOPROTOOPT;
3672
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003673 if (get_user(len, optlen))
3674 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 if (len < 0)
3677 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003678
Johann Baudy69e3c752009-05-18 22:11:22 -07003679 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003682 memcpy(&st, &po->stats, sizeof(st));
3683 memset(&po->stats, 0, sizeof(po->stats));
3684 spin_unlock_bh(&sk->sk_receive_queue.lock);
3685
chetan lokef6fb8f102011-08-19 10:18:16 +00003686 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003687 lv = sizeof(struct tpacket_stats_v3);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003688 st.stats3.tp_packets += st.stats3.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003689 data = &st.stats3;
chetan lokef6fb8f102011-08-19 10:18:16 +00003690 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003691 lv = sizeof(struct tpacket_stats);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003692 st.stats1.tp_packets += st.stats1.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003693 data = &st.stats1;
chetan lokef6fb8f102011-08-19 10:18:16 +00003694 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003697 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003698 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003699 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003700 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003701 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003702 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003703 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003704 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003705 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003706 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003707 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003708 break;
3709 case PACKET_HDRLEN:
3710 if (len > sizeof(int))
3711 len = sizeof(int);
3712 if (copy_from_user(&val, optval, len))
3713 return -EFAULT;
3714 switch (val) {
3715 case TPACKET_V1:
3716 val = sizeof(struct tpacket_hdr);
3717 break;
3718 case TPACKET_V2:
3719 val = sizeof(struct tpacket2_hdr);
3720 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00003721 case TPACKET_V3:
3722 val = sizeof(struct tpacket3_hdr);
3723 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003724 default:
3725 return -EINVAL;
3726 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003727 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003728 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003729 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003730 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003731 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003732 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003733 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003734 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003735 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003736 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003737 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003738 val = (po->fanout ?
3739 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003740 ((u32)po->fanout->type << 16) |
3741 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003742 0);
David S. Millerdc99f602011-07-05 01:45:05 -07003743 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003744 case PACKET_ROLLOVER_STATS:
3745 if (!po->rollover)
3746 return -EINVAL;
3747 rstats.tp_all = atomic_long_read(&po->rollover->num);
3748 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
3749 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
3750 data = &rstats;
3751 lv = sizeof(rstats);
3752 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003753 case PACKET_TX_HAS_OFF:
3754 val = po->tp_tx_has_off;
3755 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003756 case PACKET_QDISC_BYPASS:
3757 val = packet_use_direct_xmit(po);
3758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 default:
3760 return -ENOPROTOOPT;
3761 }
3762
Eric Dumazetc06fff62012-04-19 21:56:11 +00003763 if (len > lv)
3764 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003765 if (put_user(len, optlen))
3766 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08003767 if (copy_to_user(optval, data, len))
3768 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
3772
Jiri Pirko351638e2013-05-28 01:30:21 +00003773static int packet_notifier(struct notifier_block *this,
3774 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00003777 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003778 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
stephen hemminger808f5112010-02-22 07:57:18 +00003780 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003781 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 struct packet_sock *po = pkt_sk(sk);
3783
3784 switch (msg) {
3785 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003787 packet_dev_mclist_delete(dev, &po->mclist);
David S. Millera2efcfa2007-05-29 13:12:50 -07003788 /* fallthrough */
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 case NETDEV_DOWN:
3791 if (dev->ifindex == po->ifindex) {
3792 spin_lock(&po->bind_lock);
3793 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07003794 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 sk->sk_err = ENETDOWN;
3796 if (!sock_flag(sk, SOCK_DEAD))
3797 sk->sk_error_report(sk);
3798 }
3799 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003800 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00003802 if (po->prot_hook.dev)
3803 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 po->prot_hook.dev = NULL;
3805 }
3806 spin_unlock(&po->bind_lock);
3807 }
3808 break;
3809 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00003810 if (dev->ifindex == po->ifindex) {
3811 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003812 if (po->num)
3813 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00003814 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 break;
3817 }
3818 }
stephen hemminger808f5112010-02-22 07:57:18 +00003819 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return NOTIFY_DONE;
3821}
3822
3823
3824static int packet_ioctl(struct socket *sock, unsigned int cmd,
3825 unsigned long arg)
3826{
3827 struct sock *sk = sock->sk;
3828
Johann Baudy69e3c752009-05-18 22:11:22 -07003829 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003830 case SIOCOUTQ:
3831 {
3832 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07003833
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003834 return put_user(amount, (int __user *)arg);
3835 }
3836 case SIOCINQ:
3837 {
3838 struct sk_buff *skb;
3839 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003841 spin_lock_bh(&sk->sk_receive_queue.lock);
3842 skb = skb_peek(&sk->sk_receive_queue);
3843 if (skb)
3844 amount = skb->len;
3845 spin_unlock_bh(&sk->sk_receive_queue.lock);
3846 return put_user(amount, (int __user *)arg);
3847 }
3848 case SIOCGSTAMP:
3849 return sock_get_timestamp(sk, (struct timeval __user *)arg);
3850 case SIOCGSTAMPNS:
3851 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003854 case SIOCADDRT:
3855 case SIOCDELRT:
3856 case SIOCDARP:
3857 case SIOCGARP:
3858 case SIOCSARP:
3859 case SIOCGIFADDR:
3860 case SIOCSIFADDR:
3861 case SIOCGIFBRDADDR:
3862 case SIOCSIFBRDADDR:
3863 case SIOCGIFNETMASK:
3864 case SIOCSIFNETMASK:
3865 case SIOCGIFDSTADDR:
3866 case SIOCSIFDSTADDR:
3867 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003868 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869#endif
3870
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003871 default:
3872 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
3874 return 0;
3875}
3876
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003877static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 poll_table *wait)
3879{
3880 struct sock *sk = sock->sk;
3881 struct packet_sock *po = pkt_sk(sk);
3882 unsigned int mask = datagram_poll(file, sock, wait);
3883
3884 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003885 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f102011-08-19 10:18:16 +00003886 if (!packet_previous_rx_frame(po, &po->rx_ring,
3887 TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 mask |= POLLIN | POLLRDNORM;
3889 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003890 if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04003891 po->pressure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003893 spin_lock_bh(&sk->sk_write_queue.lock);
3894 if (po->tx_ring.pg_vec) {
3895 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
3896 mask |= POLLOUT | POLLWRNORM;
3897 }
3898 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return mask;
3900}
3901
3902
3903/* Dirty? Well, I still did not learn better way to account
3904 * for user mmaps.
3905 */
3906
3907static void packet_mm_open(struct vm_area_struct *vma)
3908{
3909 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003910 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 if (sk)
3914 atomic_inc(&pkt_sk(sk)->mapped);
3915}
3916
3917static void packet_mm_close(struct vm_area_struct *vma)
3918{
3919 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003920 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (sk)
3924 atomic_dec(&pkt_sk(sk)->mapped);
3925}
3926
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003927static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003928 .open = packet_mm_open,
3929 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930};
3931
Neil Horman0e3125c2010-11-16 10:26:47 -08003932static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
3933 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 int i;
3936
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003937 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08003938 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003939 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08003940 vfree(pg_vec[i].buffer);
3941 else
3942 free_pages((unsigned long)pg_vec[i].buffer,
3943 order);
3944 pg_vec[i].buffer = NULL;
3945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947 kfree(pg_vec);
3948}
3949
Olof Johanssoneea49cc92011-11-02 11:00:49 +00003950static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003951{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003952 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08003953 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
3954 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07003955
Neil Horman0e3125c2010-11-16 10:26:47 -08003956 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003957 if (buffer)
3958 return buffer;
3959
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003960 /* __get_free_pages failed, fall back to vmalloc */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00003961 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08003962 if (buffer)
3963 return buffer;
3964
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003965 /* vmalloc failed, lets dig into swap here */
Neil Horman0e3125c2010-11-16 10:26:47 -08003966 gfp_flags &= ~__GFP_NORETRY;
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003967 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003968 if (buffer)
3969 return buffer;
3970
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01003971 /* complete and utter failure */
Neil Horman0e3125c2010-11-16 10:26:47 -08003972 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003973}
3974
Neil Horman0e3125c2010-11-16 10:26:47 -08003975static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003976{
3977 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08003978 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003979 int i;
3980
Neil Horman0e3125c2010-11-16 10:26:47 -08003981 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003982 if (unlikely(!pg_vec))
3983 goto out;
3984
3985 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003986 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003987 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003988 goto out_free_pgvec;
3989 }
3990
3991out:
3992 return pg_vec;
3993
3994out_free_pgvec:
3995 free_pg_vec(pg_vec, order, block_nr);
3996 pg_vec = NULL;
3997 goto out;
3998}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
chetan lokef6fb8f102011-08-19 10:18:16 +00004000static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004001 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Neil Horman0e3125c2010-11-16 10:26:47 -08004003 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08004005 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004006 struct packet_ring_buffer *rb;
4007 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004008 __be16 num;
chetan lokef6fb8f102011-08-19 10:18:16 +00004009 int err = -EINVAL;
4010 /* Added to avoid minimal code churn */
4011 struct tpacket_req *req = &req_u->req;
4012
4013 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
4014 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
4015 WARN(1, "Tx-ring is not supported.\n");
4016 goto out;
4017 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004018
4019 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4020 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4021
4022 err = -EBUSY;
4023 if (!closing) {
4024 if (atomic_read(&po->mapped))
4025 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004026 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004027 goto out;
4028 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004032 err = -EBUSY;
4033 if (unlikely(rb->pg_vec))
4034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004036 switch (po->tp_version) {
4037 case TPACKET_V1:
4038 po->tp_hdrlen = TPACKET_HDRLEN;
4039 break;
4040 case TPACKET_V2:
4041 po->tp_hdrlen = TPACKET2_HDRLEN;
4042 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00004043 case TPACKET_V3:
4044 po->tp_hdrlen = TPACKET3_HDRLEN;
4045 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004046 }
4047
Johann Baudy69e3c752009-05-18 22:11:22 -07004048 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004049 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004050 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004051 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004052 goto out;
Eric Dumazetdc808112014-08-15 09:16:04 -07004053 if (po->tp_version >= TPACKET_V3 &&
4054 (int)(req->tp_block_size -
4055 BLK_PLUS_PRIV(req_u->req3.tp_sizeof_priv)) <= 0)
4056 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004057 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07004058 po->tp_reserve))
4059 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004060 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Johann Baudy69e3c752009-05-18 22:11:22 -07004063 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
4064 if (unlikely(rb->frames_per_block <= 0))
4065 goto out;
4066 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4067 req->tp_frame_nr))
4068 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
4070 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004071 order = get_order(req->tp_block_size);
4072 pg_vec = alloc_pg_vec(req, order);
4073 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 goto out;
chetan lokef6fb8f102011-08-19 10:18:16 +00004075 switch (po->tp_version) {
4076 case TPACKET_V3:
4077 /* Transmit path is not supported. We checked
4078 * it above but just being paranoid
4079 */
4080 if (!tx_ring)
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304081 init_prb_bdqc(po, rb, pg_vec, req_u);
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004082 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00004083 default:
4084 break;
4085 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004086 }
4087 /* Done */
4088 else {
4089 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004090 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004091 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 }
4093
4094 lock_sock(sk);
4095
4096 /* Detach socket from network */
4097 spin_lock(&po->bind_lock);
4098 was_running = po->running;
4099 num = po->num;
4100 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004102 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 }
4104 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 synchronize_net();
4107
4108 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004109 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 if (closing || atomic_read(&po->mapped) == 0) {
4111 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004112 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004113 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07004114 rb->frame_max = (req->tp_frame_nr - 1);
4115 rb->head = 0;
4116 rb->frame_size = req->tp_frame_size;
4117 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Changli Gaoc053fd92010-12-10 16:02:20 -08004119 swap(rb->pg_vec_order, order);
4120 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Johann Baudy69e3c752009-05-18 22:11:22 -07004122 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4123 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4124 tpacket_rcv : packet_rcv;
4125 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004127 pr_err("packet_mmap: vma is busy: %d\n",
4128 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 }
Herbert Xu905db442009-01-30 14:12:06 -08004130 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004133 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004135 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
4137 spin_unlock(&po->bind_lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00004138 if (closing && (po->tp_version > TPACKET_V2)) {
4139 /* Because we don't support block-based V3 on tx-ring */
4140 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004141 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f102011-08-19 10:18:16 +00004142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 release_sock(sk);
4144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 if (pg_vec)
4146 free_pg_vec(pg_vec, order, req->tp_block_nr);
4147out:
4148 return err;
4149}
4150
Johann Baudy69e3c752009-05-18 22:11:22 -07004151static int packet_mmap(struct file *file, struct socket *sock,
4152 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 struct sock *sk = sock->sk;
4155 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004156 unsigned long size, expected_size;
4157 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 unsigned long start;
4159 int err = -EINVAL;
4160 int i;
4161
4162 if (vma->vm_pgoff)
4163 return -EINVAL;
4164
Herbert Xu905db442009-01-30 14:12:06 -08004165 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004166
4167 expected_size = 0;
4168 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4169 if (rb->pg_vec) {
4170 expected_size += rb->pg_vec_len
4171 * rb->pg_vec_pages
4172 * PAGE_SIZE;
4173 }
4174 }
4175
4176 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004178
4179 size = vma->vm_end - vma->vm_start;
4180 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 goto out;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004184 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4185 if (rb->pg_vec == NULL)
4186 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004187
Johann Baudy69e3c752009-05-18 22:11:22 -07004188 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004189 struct page *page;
4190 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004191 int pg_num;
4192
Changli Gaoc56b4d92010-12-01 02:52:57 +00004193 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4194 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004195 err = vm_insert_page(vma, start, page);
4196 if (unlikely(err))
4197 goto out;
4198 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004199 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004200 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004203
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004204 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 vma->vm_ops = &packet_mmap_ops;
4206 err = 0;
4207
4208out:
Herbert Xu905db442009-01-30 14:12:06 -08004209 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 return err;
4211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004213static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 .family = PF_PACKET,
4215 .owner = THIS_MODULE,
4216 .release = packet_release,
4217 .bind = packet_bind_spkt,
4218 .connect = sock_no_connect,
4219 .socketpair = sock_no_socketpair,
4220 .accept = sock_no_accept,
4221 .getname = packet_getname_spkt,
4222 .poll = datagram_poll,
4223 .ioctl = packet_ioctl,
4224 .listen = sock_no_listen,
4225 .shutdown = sock_no_shutdown,
4226 .setsockopt = sock_no_setsockopt,
4227 .getsockopt = sock_no_getsockopt,
4228 .sendmsg = packet_sendmsg_spkt,
4229 .recvmsg = packet_recvmsg,
4230 .mmap = sock_no_mmap,
4231 .sendpage = sock_no_sendpage,
4232};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004234static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 .family = PF_PACKET,
4236 .owner = THIS_MODULE,
4237 .release = packet_release,
4238 .bind = packet_bind,
4239 .connect = sock_no_connect,
4240 .socketpair = sock_no_socketpair,
4241 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004242 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 .poll = packet_poll,
4244 .ioctl = packet_ioctl,
4245 .listen = sock_no_listen,
4246 .shutdown = sock_no_shutdown,
4247 .setsockopt = packet_setsockopt,
4248 .getsockopt = packet_getsockopt,
4249 .sendmsg = packet_sendmsg,
4250 .recvmsg = packet_recvmsg,
4251 .mmap = packet_mmap,
4252 .sendpage = sock_no_sendpage,
4253};
4254
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004255static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 .family = PF_PACKET,
4257 .create = packet_create,
4258 .owner = THIS_MODULE,
4259};
4260
4261static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004262 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263};
4264
4265#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004268 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Denis V. Luneve372c412007-11-19 22:31:54 -08004270 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004271
4272 rcu_read_lock();
4273 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
4276static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4277{
Herbert Xu1bf40952007-12-16 14:04:02 -08004278 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004279 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
4282static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004283 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
stephen hemminger808f5112010-02-22 07:57:18 +00004285 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004288static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
4290 if (v == SEQ_START_TOKEN)
4291 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
4292 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004293 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 const struct packet_sock *po = pkt_sk(s);
4295
4296 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004297 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 s,
4299 atomic_read(&s->sk_refcnt),
4300 s->sk_type,
4301 ntohs(po->num),
4302 po->ifindex,
4303 po->running,
4304 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004305 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004306 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 }
4308
4309 return 0;
4310}
4311
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004312static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 .start = packet_seq_start,
4314 .next = packet_seq_next,
4315 .stop = packet_seq_stop,
4316 .show = packet_seq_show,
4317};
4318
4319static int packet_seq_open(struct inode *inode, struct file *file)
4320{
Denis V. Luneve372c412007-11-19 22:31:54 -08004321 return seq_open_net(inode, file, &packet_seq_ops,
4322 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
Arjan van de Venda7071d2007-02-12 00:55:36 -08004325static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 .owner = THIS_MODULE,
4327 .open = packet_seq_open,
4328 .read = seq_read,
4329 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08004330 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331};
4332
4333#endif
4334
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004335static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004336{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004337 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004338 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004339
Gao fengd4beaa62013-02-18 01:34:54 +00004340 if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004341 return -ENOMEM;
4342
4343 return 0;
4344}
4345
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004346static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004347{
Gao fengece31ff2013-02-18 01:34:56 +00004348 remove_proc_entry("packet", net->proc_net);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004349}
4350
4351static struct pernet_operations packet_net_ops = {
4352 .init = packet_net_init,
4353 .exit = packet_net_exit,
4354};
4355
4356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357static void __exit packet_exit(void)
4358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004360 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 sock_unregister(PF_PACKET);
4362 proto_unregister(&packet_proto);
4363}
4364
4365static int __init packet_init(void)
4366{
4367 int rc = proto_register(&packet_proto, 0);
4368
4369 if (rc != 0)
4370 goto out;
4371
4372 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004373 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375out:
4376 return rc;
4377}
4378
4379module_init(packet_init);
4380module_exit(packet_exit);
4381MODULE_LICENSE("GPL");
4382MODULE_ALIAS_NETPROTO(PF_PACKET);