blob: 7fa847dcea30e481b2f291cc6980a7b887629cd7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * PACKET - implements raw packet sockets.
8 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07009 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Alan Cox, <gw4pts@gw4pts.ampr.org>
12 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090013 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Alan Cox : verify_area() now used correctly
15 * Alan Cox : new skbuff lists, look ma no backlogs!
16 * Alan Cox : tidied skbuff lists.
17 * Alan Cox : Now uses generic datagram routines I
18 * added. Also fixed the peek/read crash
19 * from all old Linux datagram code.
20 * Alan Cox : Uses the improved datagram code.
21 * Alan Cox : Added NULL's for socket options.
22 * Alan Cox : Re-commented the code.
23 * Alan Cox : Use new kernel side addressing
24 * Rob Janssen : Correct MTU usage.
25 * Dave Platt : Counter leaks caused by incorrect
26 * interrupt locking and some slightly
27 * dubious gcc output. Can you read
28 * compiler: it said _VOLATILE_
29 * Richard Kooijman : Timestamp fixes.
30 * Alan Cox : New buffers. Use sk->mac.raw.
31 * Alan Cox : sendmsg/recvmsg support.
32 * Alan Cox : Protocol setting support
33 * Alexey Kuznetsov : Untied from IPv4 stack.
34 * Cyrus Durgin : Fixed kerneld for kmod.
35 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090036 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070038 * Eric Biederman : Allow for > 8 byte hardware addresses.
39 * The convention is that longer addresses
40 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090041 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070042 * and packet_mreq.
Johann Baudy69e3c752009-05-18 22:11:22 -070043 * Johann Baudy : Added TX RING.
chetan lokef6fb8f12011-08-19 10:18:16 +000044 * Chetan Loke : Implemented TPACKET_V3 block abstraction
45 * layer.
46 * Copyright (C) 2011, <lokec@ccs.neu.edu>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080051#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/fcntl.h>
53#include <linux/socket.h>
54#include <linux/in.h>
55#include <linux/inet.h>
56#include <linux/netdevice.h>
57#include <linux/if_packet.h>
58#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080059#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080062#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020063#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/ip.h>
65#include <net/protocol.h>
66#include <linux/skbuff.h>
67#include <net/sock.h>
68#include <linux/errno.h>
69#include <linux/timer.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080070#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <asm/ioctls.h>
72#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040073#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/io.h>
75#include <linux/proc_fs.h>
76#include <linux/seq_file.h>
77#include <linux/poll.h>
78#include <linux/module.h>
79#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080080#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070081#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080082#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000083#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070084#include <linux/net_tstamp.h>
Daniel Borkmannb0138402014-01-15 16:25:36 +010085#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#ifdef CONFIG_INET
87#include <net/inet_common.h>
88#endif
Willem de Bruijn47dceb82015-08-14 22:31:34 -040089#include <linux/bpf.h>
Willem de Bruijn719c44d2016-06-07 12:06:34 -040090#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Pavel Emelyanov2787b042012-08-13 05:49:39 +000092#include "internal.h"
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 Assumptions:
96 - if device has no dev->hard_header routine, it adds and removes ll header
97 inside itself. In this case ll header is invisible outside of device,
98 but higher levels still should reserve dev->hard_header_len.
99 Some devices are enough clever to reallocate skb, when header
100 will not fit to reserved space (tunnel), another ones are silly
101 (PPP).
102 - packet socket receives packets with pulled ll header,
103 so that SOCK_RAW should push it back.
104
105On receive:
106-----------
107
108Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700109 mac_header -> ll header
110 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700113 mac_header -> ll header
114 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700117 mac_header -> UNKNOWN position. It is very likely, that it points to ll
118 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900119 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700120 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700123 mac_header -> data. ll header is still not built!
124 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126Resume
127 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
128
129
130On transmit:
131------------
132
133dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700134 mac_header -> ll header
135 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700138 mac_header -> data
139 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 We should set nh.raw on output to correct posistion,
142 packet classifier depends on it.
143 */
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* Private packet socket structures. */
146
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700147/* identical to struct packet_mreq except it has
148 * a longer address field.
149 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000150struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700151 int mr_ifindex;
152 unsigned short mr_type;
153 unsigned short mr_alen;
154 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
David S. Millera2efcfa2007-05-29 13:12:50 -0700156
Daniel Borkmann184f4892013-04-16 01:57:46 +0000157union tpacket_uhdr {
158 struct tpacket_hdr *h1;
159 struct tpacket2_hdr *h2;
160 struct tpacket3_hdr *h3;
161 void *raw;
162};
163
chetan lokef6fb8f12011-08-19 10:18:16 +0000164static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700165 int closing, int tx_ring);
166
chetan lokef6fb8f12011-08-19 10:18:16 +0000167#define V3_ALIGNMENT (8)
168
chetan lokebc59ba32011-08-25 10:43:30 +0000169#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f12011-08-19 10:18:16 +0000170
171#define BLK_PLUS_PRIV(sz_of_priv) \
172 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
173
chetan lokef6fb8f12011-08-19 10:18:16 +0000174#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
175#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
176#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
177#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
178#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
179#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
180#define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
181
Johann Baudy69e3c752009-05-18 22:11:22 -0700182struct packet_sock;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000183static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
184 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
chetan lokef6fb8f12011-08-19 10:18:16 +0000186static void *packet_previous_frame(struct packet_sock *po,
187 struct packet_ring_buffer *rb,
188 int status);
189static void packet_increment_head(struct packet_ring_buffer *buff);
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300190static int prb_curr_blk_in_use(struct tpacket_block_desc *);
chetan lokebc59ba32011-08-25 10:43:30 +0000191static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000192 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000193static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000194 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000195static int prb_queue_frozen(struct tpacket_kbdq_core *);
196static void prb_open_block(struct tpacket_kbdq_core *,
197 struct tpacket_block_desc *);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700198static void prb_retire_rx_blk_timer_expired(struct timer_list *);
chetan lokebc59ba32011-08-25 10:43:30 +0000199static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
chetan lokebc59ba32011-08-25 10:43:30 +0000200static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
201static void prb_clear_rxhash(struct tpacket_kbdq_core *,
202 struct tpacket3_hdr *);
203static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
204 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static void packet_flush_mclist(struct sock *sk);
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200206static u16 packet_pick_tx_queue(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Herbert Xuffbc6112007-02-04 23:33:10 -0800208struct packet_skb_cb {
Herbert Xuffbc6112007-02-04 23:33:10 -0800209 union {
210 struct sockaddr_pkt pkt;
Eyal Birger2472d762015-03-01 14:58:28 +0200211 union {
212 /* Trick: alias skb original length with
213 * ll.sll_family and ll.protocol in order
214 * to save room.
215 */
216 unsigned int origlen;
217 struct sockaddr_ll ll;
218 };
Herbert Xuffbc6112007-02-04 23:33:10 -0800219 } sa;
220};
221
David Woodhoused3869ef2015-09-23 19:45:08 +0100222#define vio_le() virtio_legacy_is_little_endian()
223
Herbert Xuffbc6112007-02-04 23:33:10 -0800224#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800225
chetan lokebc59ba32011-08-25 10:43:30 +0000226#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f12011-08-19 10:18:16 +0000227#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000228 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000229#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000230 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000231#define GET_NEXT_PRB_BLK_NUM(x) \
232 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
233 ((x)->kactive_blk_num+1) : 0)
234
David S. Millerdc99f602011-07-05 01:45:05 -0700235static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
236static void __fanout_link(struct sock *sk, struct packet_sock *po);
237
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100238static int packet_direct_xmit(struct sk_buff *skb)
239{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200240 return dev_direct_xmit(skb, packet_pick_tx_queue(skb));
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100241}
242
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100243static struct net_device *packet_cached_dev_get(struct packet_sock *po)
244{
245 struct net_device *dev;
246
247 rcu_read_lock();
248 dev = rcu_dereference(po->cached_dev);
249 if (likely(dev))
250 dev_hold(dev);
251 rcu_read_unlock();
252
253 return dev;
254}
255
256static void packet_cached_dev_assign(struct packet_sock *po,
257 struct net_device *dev)
258{
259 rcu_assign_pointer(po->cached_dev, dev);
260}
261
262static void packet_cached_dev_reset(struct packet_sock *po)
263{
264 RCU_INIT_POINTER(po->cached_dev, NULL);
265}
266
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100267static bool packet_use_direct_xmit(const struct packet_sock *po)
268{
269 return po->xmit == packet_direct_xmit;
270}
271
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200272static u16 packet_pick_tx_queue(struct sk_buff *skb)
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100273{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200274 struct net_device *dev = skb->dev;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100275 const struct net_device_ops *ops = dev->netdev_ops;
Paolo Abenib71b5832019-03-20 11:02:05 +0100276 int cpu = raw_smp_processor_id();
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100277 u16 queue_index;
278
Paolo Abenib71b5832019-03-20 11:02:05 +0100279#ifdef CONFIG_XPS
280 skb->sender_cpu = cpu + 1;
281#endif
282 skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100283 if (ops->ndo_select_queue) {
Paolo Abenia350ecc2019-03-20 11:02:06 +0100284 queue_index = ops->ndo_select_queue(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100285 queue_index = netdev_cap_txqueue(dev, queue_index);
286 } else {
Paolo Abenib71b5832019-03-20 11:02:05 +0100287 queue_index = netdev_pick_tx(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100288 }
289
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200290 return queue_index;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100291}
292
Willem de Bruijna6361f02018-04-23 17:37:03 -0400293/* __register_prot_hook must be invoked through register_prot_hook
David S. Millerce06b032011-07-04 01:44:29 -0700294 * or from a context in which asynchronous accesses to the packet
295 * socket is not possible (packet_create()).
296 */
Willem de Bruijna6361f02018-04-23 17:37:03 -0400297static void __register_prot_hook(struct sock *sk)
David S. Millerce06b032011-07-04 01:44:29 -0700298{
299 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100300
David S. Millerce06b032011-07-04 01:44:29 -0700301 if (!po->running) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100302 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700303 __fanout_link(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100304 else
David S. Millerdc99f602011-07-05 01:45:05 -0700305 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100306
David S. Millerce06b032011-07-04 01:44:29 -0700307 sock_hold(sk);
308 po->running = 1;
309 }
310}
311
Willem de Bruijna6361f02018-04-23 17:37:03 -0400312static void register_prot_hook(struct sock *sk)
313{
314 lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
315 __register_prot_hook(sk);
316}
317
318/* If the sync parameter is true, we will temporarily drop
David S. Millerce06b032011-07-04 01:44:29 -0700319 * the po->bind_lock and do a synchronize_net to make sure no
320 * asynchronous packet processing paths still refer to the elements
321 * of po->prot_hook. If the sync parameter is false, it is the
322 * callers responsibility to take care of this.
323 */
324static void __unregister_prot_hook(struct sock *sk, bool sync)
325{
326 struct packet_sock *po = pkt_sk(sk);
327
Willem de Bruijna6361f02018-04-23 17:37:03 -0400328 lockdep_assert_held_once(&po->bind_lock);
329
David S. Millerce06b032011-07-04 01:44:29 -0700330 po->running = 0;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100331
332 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700333 __fanout_unlink(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100334 else
David S. Millerdc99f602011-07-05 01:45:05 -0700335 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100336
David S. Millerce06b032011-07-04 01:44:29 -0700337 __sock_put(sk);
338
339 if (sync) {
340 spin_unlock(&po->bind_lock);
341 synchronize_net();
342 spin_lock(&po->bind_lock);
343 }
344}
345
346static void unregister_prot_hook(struct sock *sk, bool sync)
347{
348 struct packet_sock *po = pkt_sk(sk);
349
350 if (po->running)
351 __unregister_prot_hook(sk, sync);
352}
353
Michael S. Tsirkin6e580402014-11-24 13:32:16 +0200354static inline struct page * __pure pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000355{
356 if (is_vmalloc_addr(addr))
357 return vmalloc_to_page(addr);
358 return virt_to_page(addr);
359}
360
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700361static void __packet_set_status(struct packet_sock *po, void *frame, int status)
362{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000363 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700364
365 h.raw = frame;
366 switch (po->tp_version) {
367 case TPACKET_V1:
368 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000369 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700370 break;
371 case TPACKET_V2:
372 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000373 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700374 break;
chetan lokef6fb8f12011-08-19 10:18:16 +0000375 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800376 h.h3->tp_status = status;
377 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
378 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700379 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000380 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700381 BUG();
382 }
383
384 smp_wmb();
385}
386
387static int __packet_get_status(struct packet_sock *po, void *frame)
388{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000389 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700390
391 smp_rmb();
392
393 h.raw = frame;
394 switch (po->tp_version) {
395 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000396 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700397 return h.h1->tp_status;
398 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000399 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700400 return h.h2->tp_status;
chetan lokef6fb8f12011-08-19 10:18:16 +0000401 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800402 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
403 return h.h3->tp_status;
Johann Baudy69e3c752009-05-18 22:11:22 -0700404 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000405 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700406 BUG();
407 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Johann Baudy69e3c752009-05-18 22:11:22 -0700410
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000411static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
412 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000413{
414 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
415
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400416 if (shhwtstamps &&
417 (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
418 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
419 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000420
421 if (ktime_to_timespec_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000422 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000423
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000424 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000425}
426
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000427static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
428 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000429{
430 union tpacket_uhdr h;
431 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000432 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000433
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000434 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
435 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000436
437 h.raw = frame;
438 switch (po->tp_version) {
439 case TPACKET_V1:
440 h.h1->tp_sec = ts.tv_sec;
441 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
442 break;
443 case TPACKET_V2:
444 h.h2->tp_sec = ts.tv_sec;
445 h.h2->tp_nsec = ts.tv_nsec;
446 break;
447 case TPACKET_V3:
Daniel Borkmann57ea8842017-01-05 02:34:28 +0100448 h.h3->tp_sec = ts.tv_sec;
449 h.h3->tp_nsec = ts.tv_nsec;
450 break;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000451 default:
452 WARN(1, "TPACKET version not supported.\n");
453 BUG();
454 }
455
456 /* one flush is safe, as both fields always lie on the same cacheline */
457 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
458 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000459
460 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000461}
462
Johann Baudy69e3c752009-05-18 22:11:22 -0700463static void *packet_lookup_frame(struct packet_sock *po,
464 struct packet_ring_buffer *rb,
465 unsigned int position,
466 int status)
467{
468 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000469 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700470
471 pg_vec_pos = position / rb->frames_per_block;
472 frame_offset = position % rb->frames_per_block;
473
Neil Horman0e3125c2010-11-16 10:26:47 -0800474 h.raw = rb->pg_vec[pg_vec_pos].buffer +
475 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700476
477 if (status != __packet_get_status(po, h.raw))
478 return NULL;
479
480 return h.raw;
481}
482
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000483static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700484 struct packet_ring_buffer *rb,
485 int status)
486{
487 return packet_lookup_frame(po, rb, rb->head, status);
488}
489
chetan lokebc59ba32011-08-25 10:43:30 +0000490static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000491{
492 del_timer_sync(&pkc->retire_blk_timer);
493}
494
495static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +0000496 struct sk_buff_head *rb_queue)
497{
chetan lokebc59ba32011-08-25 10:43:30 +0000498 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000499
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200500 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000501
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100502 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000503 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100504 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000505
506 prb_del_retire_blk_timer(pkc);
507}
508
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530509static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f12011-08-19 10:18:16 +0000510{
chetan lokebc59ba32011-08-25 10:43:30 +0000511 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000512
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530513 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700514 timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired,
515 0);
516 pkc->retire_blk_timer.expires = jiffies;
chetan lokef6fb8f12011-08-19 10:18:16 +0000517}
518
519static int prb_calc_retire_blk_tmo(struct packet_sock *po,
520 int blk_size_in_bytes)
521{
522 struct net_device *dev;
523 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800524 struct ethtool_link_ksettings ecmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000525 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +0000526
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000527 rtnl_lock();
528 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
529 if (unlikely(!dev)) {
530 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000531 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000532 }
David Decotigny7cad1ba2016-02-24 10:58:10 -0800533 err = __ethtool_get_link_ksettings(dev, &ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000534 rtnl_unlock();
535 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000536 /*
537 * If the link speed is so slow you don't really
538 * need to worry about perf anyways
539 */
David Decotigny7cad1ba2016-02-24 10:58:10 -0800540 if (ecmd.base.speed < SPEED_1000 ||
541 ecmd.base.speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000542 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000543 } else {
544 msec = 1;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800545 div = ecmd.base.speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000546 }
547 }
548
549 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
550
551 if (div)
552 mbits /= div;
553
554 tmo = mbits * msec;
555
556 if (div)
557 return tmo+1;
558 return tmo;
559}
560
chetan lokebc59ba32011-08-25 10:43:30 +0000561static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000562 union tpacket_req_u *req_u)
563{
564 p1->feature_req_word = req_u->req3.tp_feature_req_word;
565}
566
567static void init_prb_bdqc(struct packet_sock *po,
568 struct packet_ring_buffer *rb,
569 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530570 union tpacket_req_u *req_u)
chetan lokef6fb8f12011-08-19 10:18:16 +0000571{
Duan Jiong22781a52013-12-06 13:29:36 +0800572 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000573 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000574
575 memset(p1, 0x0, sizeof(*p1));
576
577 p1->knxt_seq_num = 1;
578 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000579 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000580 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000581 p1->kblk_size = req_u->req3.tp_block_size;
582 p1->knum_blocks = req_u->req3.tp_block_nr;
583 p1->hdrlen = po->tp_hdrlen;
584 p1->version = po->tp_version;
585 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000586 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000587 if (req_u->req3.tp_retire_blk_tov)
588 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
589 else
590 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
591 req_u->req3.tp_block_size);
592 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
593 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
594
Eric Dumazetdc808112014-08-15 09:16:04 -0700595 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000596 prb_init_ft_ops(p1, req_u);
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530597 prb_setup_retire_blk_timer(po);
chetan lokef6fb8f12011-08-19 10:18:16 +0000598 prb_open_block(p1, pbd);
599}
600
601/* Do NOT update the last_blk_num first.
602 * Assumes sk_buff_head lock is held.
603 */
chetan lokebc59ba32011-08-25 10:43:30 +0000604static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000605{
606 mod_timer(&pkc->retire_blk_timer,
607 jiffies + pkc->tov_in_jiffies);
608 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
609}
610
611/*
612 * Timer logic:
613 * 1) We refresh the timer only when we open a block.
614 * By doing this we don't waste cycles refreshing the timer
615 * on packet-by-packet basis.
616 *
617 * With a 1MB block-size, on a 1Gbps line, it will take
618 * i) ~8 ms to fill a block + ii) memcpy etc.
619 * In this cut we are not accounting for the memcpy time.
620 *
621 * So, if the user sets the 'tmo' to 10ms then the timer
622 * will never fire while the block is still getting filled
623 * (which is what we want). However, the user could choose
624 * to close a block early and that's fine.
625 *
626 * But when the timer does fire, we check whether or not to refresh it.
627 * Since the tmo granularity is in msecs, it is not too expensive
628 * to refresh the timer, lets say every '8' msecs.
629 * Either the user can set the 'tmo' or we can derive it based on
630 * a) line-speed and b) block-size.
631 * prb_calc_retire_blk_tmo() calculates the tmo.
632 *
633 */
Kees Cook17bfd8c2017-10-24 01:46:26 -0700634static void prb_retire_rx_blk_timer_expired(struct timer_list *t)
chetan lokef6fb8f12011-08-19 10:18:16 +0000635{
Kees Cook17bfd8c2017-10-24 01:46:26 -0700636 struct packet_sock *po =
637 from_timer(po, t, rx_ring.prb_bdqc.retire_blk_timer);
Duan Jiong22781a52013-12-06 13:29:36 +0800638 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000639 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000640 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000641
642 spin_lock(&po->sk.sk_receive_queue.lock);
643
644 frozen = prb_queue_frozen(pkc);
645 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
646
647 if (unlikely(pkc->delete_blk_timer))
648 goto out;
649
650 /* We only need to plug the race when the block is partially filled.
651 * tpacket_rcv:
652 * lock(); increment BLOCK_NUM_PKTS; unlock()
653 * copy_bits() is in progress ...
654 * timer fires on other cpu:
655 * we can't retire the current block because copy_bits
656 * is in progress.
657 *
658 */
659 if (BLOCK_NUM_PKTS(pbd)) {
660 while (atomic_read(&pkc->blk_fill_in_prog)) {
661 /* Waiting for skb_copy_bits to finish... */
662 cpu_relax();
663 }
664 }
665
666 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
667 if (!frozen) {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300668 if (!BLOCK_NUM_PKTS(pbd)) {
669 /* An empty block. Just refresh the timer. */
670 goto refresh_timer;
671 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000672 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
673 if (!prb_dispatch_next_block(pkc, po))
674 goto refresh_timer;
675 else
676 goto out;
677 } else {
678 /* Case 1. Queue was frozen because user-space was
679 * lagging behind.
680 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300681 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +0000682 /*
683 * Ok, user-space is still behind.
684 * So just refresh the timer.
685 */
686 goto refresh_timer;
687 } else {
688 /* Case 2. queue was frozen,user-space caught up,
689 * now the link went idle && the timer fired.
690 * We don't have a block to close.So we open this
691 * block and restart the timer.
692 * opening a block thaws the queue,restarts timer
693 * Thawing/timer-refresh is a side effect.
694 */
695 prb_open_block(pkc, pbd);
696 goto out;
697 }
698 }
699 }
700
701refresh_timer:
702 _prb_refresh_rx_retire_blk_timer(pkc);
703
704out:
705 spin_unlock(&po->sk.sk_receive_queue.lock);
706}
707
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000708static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000709 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000710{
711 /* Flush everything minus the block header */
712
713#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
714 u8 *start, *end;
715
716 start = (u8 *)pbd1;
717
718 /* Skip the block header(we know header WILL fit in 4K) */
719 start += PAGE_SIZE;
720
721 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
722 for (; start < end; start += PAGE_SIZE)
723 flush_dcache_page(pgv_to_page(start));
724
725 smp_wmb();
726#endif
727
728 /* Now update the block status. */
729
730 BLOCK_STATUS(pbd1) = status;
731
732 /* Flush the block header */
733
734#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
735 start = (u8 *)pbd1;
736 flush_dcache_page(pgv_to_page(start));
737
738 smp_wmb();
739#endif
740}
741
742/*
743 * Side effect:
744 *
745 * 1) flush the block
746 * 2) Increment active_blk_num
747 *
748 * Note:We DONT refresh the timer on purpose.
749 * Because almost always the next block will be opened.
750 */
chetan lokebc59ba32011-08-25 10:43:30 +0000751static void prb_close_block(struct tpacket_kbdq_core *pkc1,
752 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000753 struct packet_sock *po, unsigned int stat)
754{
755 __u32 status = TP_STATUS_USER | stat;
756
757 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000758 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
Dan Collinsda413ee2014-12-19 16:49:25 +1300759 struct sock *sk = &po->sk;
chetan lokef6fb8f12011-08-19 10:18:16 +0000760
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000761 if (po->stats.stats3.tp_drops)
chetan lokef6fb8f12011-08-19 10:18:16 +0000762 status |= TP_STATUS_LOSING;
763
764 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
765 last_pkt->tp_next_offset = 0;
766
767 /* Get the ts of the last pkt */
768 if (BLOCK_NUM_PKTS(pbd1)) {
769 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
770 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
771 } else {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300772 /* Ok, we tmo'd - so get the current time.
773 *
774 * It shouldn't really happen as we don't close empty
775 * blocks. See prb_retire_rx_blk_timer_expired().
776 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000777 struct timespec ts;
778 getnstimeofday(&ts);
779 h1->ts_last_pkt.ts_sec = ts.tv_sec;
780 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
781 }
782
783 smp_wmb();
784
785 /* Flush the block */
786 prb_flush_block(pkc1, pbd1, status);
787
Dan Collinsda413ee2014-12-19 16:49:25 +1300788 sk->sk_data_ready(sk);
789
chetan lokef6fb8f12011-08-19 10:18:16 +0000790 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
791}
792
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000793static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000794{
795 pkc->reset_pending_on_curr_blk = 0;
796}
797
798/*
799 * Side effect of opening a block:
800 *
801 * 1) prb_queue is thawed.
802 * 2) retire_blk_timer is refreshed.
803 *
804 */
chetan lokebc59ba32011-08-25 10:43:30 +0000805static void prb_open_block(struct tpacket_kbdq_core *pkc1,
806 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000807{
808 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000809 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000810
811 smp_rmb();
812
Daniel Borkmann8da30562013-05-03 02:57:00 +0000813 /* We could have just memset this but we will lose the
814 * flexibility of making the priv area sticky
815 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000816
Daniel Borkmann8da30562013-05-03 02:57:00 +0000817 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
818 BLOCK_NUM_PKTS(pbd1) = 0;
819 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000820
Daniel Borkmann8da30562013-05-03 02:57:00 +0000821 getnstimeofday(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000822
Daniel Borkmann8da30562013-05-03 02:57:00 +0000823 h1->ts_first_pkt.ts_sec = ts.tv_sec;
824 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f12011-08-19 10:18:16 +0000825
Daniel Borkmann8da30562013-05-03 02:57:00 +0000826 pkc1->pkblk_start = (char *)pbd1;
827 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
828
829 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
830 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
831
832 pbd1->version = pkc1->version;
833 pkc1->prev = pkc1->nxt_offset;
834 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
835
836 prb_thaw_queue(pkc1);
837 _prb_refresh_rx_retire_blk_timer(pkc1);
838
839 smp_wmb();
chetan lokef6fb8f12011-08-19 10:18:16 +0000840}
841
842/*
843 * Queue freeze logic:
844 * 1) Assume tp_block_nr = 8 blocks.
845 * 2) At time 't0', user opens Rx ring.
846 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
847 * 4) user-space is either sleeping or processing block '0'.
848 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
849 * it will close block-7,loop around and try to fill block '0'.
850 * call-flow:
851 * __packet_lookup_frame_in_block
852 * prb_retire_current_block()
853 * prb_dispatch_next_block()
854 * |->(BLOCK_STATUS == USER) evaluates to true
855 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
856 * 6) Now there are two cases:
857 * 6.1) Link goes idle right after the queue is frozen.
858 * But remember, the last open_block() refreshed the timer.
859 * When this timer expires,it will refresh itself so that we can
860 * re-open block-0 in near future.
861 * 6.2) Link is busy and keeps on receiving packets. This is a simple
862 * case and __packet_lookup_frame_in_block will check if block-0
863 * is free and can now be re-used.
864 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000865static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000866 struct packet_sock *po)
867{
868 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000869 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f12011-08-19 10:18:16 +0000870}
871
872#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
873
874/*
875 * If the next block is free then we will dispatch it
876 * and return a good offset.
877 * Else, we will freeze the queue.
878 * So, caller must check the return value.
879 */
chetan lokebc59ba32011-08-25 10:43:30 +0000880static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000881 struct packet_sock *po)
882{
chetan lokebc59ba32011-08-25 10:43:30 +0000883 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000884
885 smp_rmb();
886
887 /* 1. Get current block num */
888 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
889
890 /* 2. If this block is currently in_use then freeze the queue */
891 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
892 prb_freeze_queue(pkc, po);
893 return NULL;
894 }
895
896 /*
897 * 3.
898 * open this block and return the offset where the first packet
899 * needs to get stored.
900 */
901 prb_open_block(pkc, pbd);
902 return (void *)pkc->nxt_offset;
903}
904
chetan lokebc59ba32011-08-25 10:43:30 +0000905static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000906 struct packet_sock *po, unsigned int status)
907{
chetan lokebc59ba32011-08-25 10:43:30 +0000908 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000909
910 /* retire/close the current block */
911 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
912 /*
913 * Plug the case where copy_bits() is in progress on
914 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
915 * have space to copy the pkt in the current block and
916 * called prb_retire_current_block()
917 *
918 * We don't need to worry about the TMO case because
919 * the timer-handler already handled this case.
920 */
921 if (!(status & TP_STATUS_BLK_TMO)) {
922 while (atomic_read(&pkc->blk_fill_in_prog)) {
923 /* Waiting for skb_copy_bits to finish... */
924 cpu_relax();
925 }
926 }
927 prb_close_block(pkc, pbd, po, status);
928 return;
929 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000930}
931
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300932static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000933{
934 return TP_STATUS_USER & BLOCK_STATUS(pbd);
935}
936
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000937static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000938{
939 return pkc->reset_pending_on_curr_blk;
940}
941
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000942static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +0000943{
chetan lokebc59ba32011-08-25 10:43:30 +0000944 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000945 atomic_dec(&pkc->blk_fill_in_prog);
946}
947
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000948static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000949 struct tpacket3_hdr *ppd)
950{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800951 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000952}
953
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000954static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000955 struct tpacket3_hdr *ppd)
956{
957 ppd->hv1.tp_rxhash = 0;
958}
959
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000960static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000961 struct tpacket3_hdr *ppd)
962{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100963 if (skb_vlan_tag_present(pkc->skb)) {
964 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900965 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
966 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
chetan lokef6fb8f12011-08-19 10:18:16 +0000967 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000968 ppd->hv1.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900969 ppd->hv1.tp_vlan_tpid = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000970 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +0000971 }
972}
973
chetan lokebc59ba32011-08-25 10:43:30 +0000974static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000975 struct tpacket3_hdr *ppd)
976{
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900977 ppd->hv1.tp_padding = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000978 prb_fill_vlan_info(pkc, ppd);
979
980 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
981 prb_fill_rxhash(pkc, ppd);
982 else
983 prb_clear_rxhash(pkc, ppd);
984}
985
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000986static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000987 struct tpacket_kbdq_core *pkc,
988 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +0000989 unsigned int len)
990{
991 struct tpacket3_hdr *ppd;
992
993 ppd = (struct tpacket3_hdr *)curr;
994 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
995 pkc->prev = curr;
996 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
997 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
998 BLOCK_NUM_PKTS(pbd) += 1;
999 atomic_inc(&pkc->blk_fill_in_prog);
1000 prb_run_all_ft_ops(pkc, ppd);
1001}
1002
1003/* Assumes caller has the sk->rx_queue.lock */
1004static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1005 struct sk_buff *skb,
chetan lokef6fb8f12011-08-19 10:18:16 +00001006 unsigned int len
1007 )
1008{
chetan lokebc59ba32011-08-25 10:43:30 +00001009 struct tpacket_kbdq_core *pkc;
1010 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +00001011 char *curr, *end;
1012
Joe Perchese3192692012-06-03 17:41:40 +00001013 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +00001014 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1015
1016 /* Queue is frozen when user space is lagging behind */
1017 if (prb_queue_frozen(pkc)) {
1018 /*
1019 * Check if that last block which caused the queue to freeze,
1020 * is still in_use by user-space.
1021 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +03001022 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001023 /* Can't record this packet */
1024 return NULL;
1025 } else {
1026 /*
1027 * Ok, the block was released by user-space.
1028 * Now let's open that block.
1029 * opening a block also thaws the queue.
1030 * Thawing is a side effect.
1031 */
1032 prb_open_block(pkc, pbd);
1033 }
1034 }
1035
1036 smp_mb();
1037 curr = pkc->nxt_offset;
1038 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +00001039 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +00001040
1041 /* first try the current block */
1042 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1043 prb_fill_curr_block(curr, pkc, pbd, len);
1044 return (void *)curr;
1045 }
1046
1047 /* Ok, close the current block */
1048 prb_retire_current_block(pkc, po, 0);
1049
1050 /* Now, try to dispatch the next block */
1051 curr = (char *)prb_dispatch_next_block(pkc, po);
1052 if (curr) {
1053 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1054 prb_fill_curr_block(curr, pkc, pbd, len);
1055 return (void *)curr;
1056 }
1057
1058 /*
1059 * No free blocks are available.user_space hasn't caught up yet.
1060 * Queue was just frozen and now this packet will get dropped.
1061 */
1062 return NULL;
1063}
1064
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001065static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001066 struct sk_buff *skb,
1067 int status, unsigned int len)
1068{
1069 char *curr = NULL;
1070 switch (po->tp_version) {
1071 case TPACKET_V1:
1072 case TPACKET_V2:
1073 curr = packet_lookup_frame(po, &po->rx_ring,
1074 po->rx_ring.head, status);
1075 return curr;
1076 case TPACKET_V3:
Mao Wenan46088052019-06-11 09:32:13 +08001077 return __packet_lookup_frame_in_block(po, skb, len);
chetan lokef6fb8f12011-08-19 10:18:16 +00001078 default:
1079 WARN(1, "TPACKET version not supported\n");
1080 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001081 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +00001082 }
1083}
1084
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001085static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001086 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001087 unsigned int idx,
chetan lokef6fb8f12011-08-19 10:18:16 +00001088 int status)
1089{
chetan lokebc59ba32011-08-25 10:43:30 +00001090 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001091 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001092
1093 if (status != BLOCK_STATUS(pbd))
1094 return NULL;
1095 return pbd;
1096}
1097
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001098static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001099{
1100 unsigned int prev;
1101 if (rb->prb_bdqc.kactive_blk_num)
1102 prev = rb->prb_bdqc.kactive_blk_num-1;
1103 else
1104 prev = rb->prb_bdqc.knum_blocks-1;
1105 return prev;
1106}
1107
1108/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001109static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001110 struct packet_ring_buffer *rb,
1111 int status)
1112{
1113 unsigned int previous = prb_previous_blk_num(rb);
1114 return prb_lookup_block(po, rb, previous, status);
1115}
1116
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001117static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001118 struct packet_ring_buffer *rb,
1119 int status)
1120{
1121 if (po->tp_version <= TPACKET_V2)
1122 return packet_previous_frame(po, rb, status);
1123
1124 return __prb_previous_block(po, rb, status);
1125}
1126
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001127static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001128 struct packet_ring_buffer *rb)
1129{
1130 switch (po->tp_version) {
1131 case TPACKET_V1:
1132 case TPACKET_V2:
1133 return packet_increment_head(rb);
1134 case TPACKET_V3:
1135 default:
1136 WARN(1, "TPACKET version not supported.\n");
1137 BUG();
1138 return;
1139 }
1140}
1141
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001142static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001143 struct packet_ring_buffer *rb,
1144 int status)
1145{
1146 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1147 return packet_lookup_frame(po, rb, previous, status);
1148}
1149
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001150static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001151{
1152 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1153}
1154
Daniel Borkmannb0138402014-01-15 16:25:36 +01001155static void packet_inc_pending(struct packet_ring_buffer *rb)
1156{
1157 this_cpu_inc(*rb->pending_refcnt);
1158}
1159
1160static void packet_dec_pending(struct packet_ring_buffer *rb)
1161{
1162 this_cpu_dec(*rb->pending_refcnt);
1163}
1164
1165static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1166{
1167 unsigned int refcnt = 0;
1168 int cpu;
1169
1170 /* We don't use pending refcount in rx_ring. */
1171 if (rb->pending_refcnt == NULL)
1172 return 0;
1173
1174 for_each_possible_cpu(cpu)
1175 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1176
1177 return refcnt;
1178}
1179
1180static int packet_alloc_pending(struct packet_sock *po)
1181{
1182 po->rx_ring.pending_refcnt = NULL;
1183
1184 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1185 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1186 return -ENOBUFS;
1187
1188 return 0;
1189}
1190
1191static void packet_free_pending(struct packet_sock *po)
1192{
1193 free_percpu(po->tx_ring.pending_refcnt);
1194}
1195
Willem de Bruijn99547292015-05-12 11:56:47 -04001196#define ROOM_POW_OFF 2
1197#define ROOM_NONE 0x0
1198#define ROOM_LOW 0x1
1199#define ROOM_NORMAL 0x2
1200
1201static bool __tpacket_has_room(struct packet_sock *po, int pow_off)
1202{
1203 int idx, len;
1204
1205 len = po->rx_ring.frame_max + 1;
1206 idx = po->rx_ring.head;
1207 if (pow_off)
1208 idx += len >> pow_off;
1209 if (idx >= len)
1210 idx -= len;
1211 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1212}
1213
1214static bool __tpacket_v3_has_room(struct packet_sock *po, int pow_off)
1215{
1216 int idx, len;
1217
1218 len = po->rx_ring.prb_bdqc.knum_blocks;
1219 idx = po->rx_ring.prb_bdqc.kactive_blk_num;
1220 if (pow_off)
1221 idx += len >> pow_off;
1222 if (idx >= len)
1223 idx -= len;
1224 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1225}
1226
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001227static int __packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001228{
1229 struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001230 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001231
Willem de Bruijn99547292015-05-12 11:56:47 -04001232 if (po->prot_hook.func != tpacket_rcv) {
1233 int avail = sk->sk_rcvbuf - atomic_read(&sk->sk_rmem_alloc)
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001234 - (skb ? skb->truesize : 0);
Willem de Bruijn99547292015-05-12 11:56:47 -04001235 if (avail > (sk->sk_rcvbuf >> ROOM_POW_OFF))
1236 return ROOM_NORMAL;
1237 else if (avail > 0)
1238 return ROOM_LOW;
1239 else
1240 return ROOM_NONE;
1241 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001242
Willem de Bruijn99547292015-05-12 11:56:47 -04001243 if (po->tp_version == TPACKET_V3) {
1244 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1245 ret = ROOM_NORMAL;
1246 else if (__tpacket_v3_has_room(po, 0))
1247 ret = ROOM_LOW;
1248 } else {
1249 if (__tpacket_has_room(po, ROOM_POW_OFF))
1250 ret = ROOM_NORMAL;
1251 else if (__tpacket_has_room(po, 0))
1252 ret = ROOM_LOW;
1253 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001254
1255 return ret;
1256}
1257
1258static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1259{
1260 int ret;
1261 bool has_room;
1262
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001263 spin_lock_bh(&po->sk.sk_receive_queue.lock);
1264 ret = __packet_rcv_has_room(po, skb);
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001265 has_room = ret == ROOM_NORMAL;
1266 if (po->pressure == has_room)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001267 po->pressure = !has_room;
1268 spin_unlock_bh(&po->sk.sk_receive_queue.lock);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001269
Willem de Bruijn99547292015-05-12 11:56:47 -04001270 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273static void packet_sock_destruct(struct sock *sk)
1274{
Richard Cochraned85b562010-04-07 22:41:28 +00001275 skb_queue_purge(&sk->sk_error_queue);
1276
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001277 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +03001278 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001281 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return;
1283 }
1284
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001285 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001288static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1289{
1290 u32 rxhash;
1291 int i, count = 0;
1292
1293 rxhash = skb_get_hash(skb);
1294 for (i = 0; i < ROLLOVER_HLEN; i++)
1295 if (po->rollover->history[i] == rxhash)
1296 count++;
1297
1298 po->rollover->history[prandom_u32() % ROLLOVER_HLEN] = rxhash;
1299 return count > (ROLLOVER_HLEN >> 1);
1300}
1301
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001302static unsigned int fanout_demux_hash(struct packet_fanout *f,
1303 struct sk_buff *skb,
1304 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001305{
David S. Millereb70db82016-07-01 16:07:50 -04001306 return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001307}
1308
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001309static unsigned int fanout_demux_lb(struct packet_fanout *f,
1310 struct sk_buff *skb,
1311 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001312{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001313 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001314
Willem de Bruijn468479e2015-06-17 15:59:34 -04001315 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001316}
1317
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001318static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1319 struct sk_buff *skb,
1320 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001321{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001322 return smp_processor_id() % num;
1323}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001324
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001325static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1326 struct sk_buff *skb,
1327 unsigned int num)
1328{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001329 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001330}
1331
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001332static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1333 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001334 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001335 unsigned int num)
1336{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001337 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001338 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001339
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001340 po = pkt_sk(f->arr[idx]);
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001341
1342 if (try_self) {
1343 room = packet_rcv_has_room(po, skb);
1344 if (room == ROOM_NORMAL ||
1345 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1346 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001347 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001348 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001349
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001350 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001351 do {
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001352 po_next = pkt_sk(f->arr[i]);
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001353 if (po_next != po_skip && !po_next->pressure &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001354 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001355 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001356 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001357 atomic_long_inc(&po->rollover->num);
1358 if (room == ROOM_LOW)
1359 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001360 return i;
1361 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001362
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001363 if (++i == num)
1364 i = 0;
1365 } while (i != j);
1366
Willem de Bruijna9b63912015-05-12 11:56:50 -04001367 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001368 return idx;
1369}
1370
Neil Horman2d360972014-01-22 16:01:44 -05001371static unsigned int fanout_demux_qm(struct packet_fanout *f,
1372 struct sk_buff *skb,
1373 unsigned int num)
1374{
1375 return skb_get_queue_mapping(skb) % num;
1376}
1377
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001378static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1379 struct sk_buff *skb,
1380 unsigned int num)
1381{
1382 struct bpf_prog *prog;
1383 unsigned int ret = 0;
1384
1385 rcu_read_lock();
1386 prog = rcu_dereference(f->bpf_prog);
1387 if (prog)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001388 ret = bpf_prog_run_clear_cb(prog, skb) % num;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001389 rcu_read_unlock();
1390
1391 return ret;
1392}
1393
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001394static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1395{
1396 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001397}
1398
David S. Miller95ec3eb2011-07-06 01:56:38 -07001399static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1400 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001401{
1402 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001403 unsigned int num = READ_ONCE(f->num_members);
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001404 struct net *net = read_pnet(&f->net);
David S. Millerdc99f602011-07-05 01:45:05 -07001405 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001406 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001407
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001408 if (!net_eq(dev_net(dev), net) || !num) {
David S. Millerdc99f602011-07-05 01:45:05 -07001409 kfree_skb(skb);
1410 return 0;
1411 }
1412
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001413 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001414 skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001415 if (!skb)
1416 return 0;
1417 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001418 switch (f->type) {
1419 case PACKET_FANOUT_HASH:
1420 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001421 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001422 break;
1423 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001424 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001425 break;
1426 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001427 idx = fanout_demux_cpu(f, skb, num);
1428 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001429 case PACKET_FANOUT_RND:
1430 idx = fanout_demux_rnd(f, skb, num);
1431 break;
Neil Horman2d360972014-01-22 16:01:44 -05001432 case PACKET_FANOUT_QM:
1433 idx = fanout_demux_qm(f, skb, num);
1434 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001435 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001436 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001437 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001438 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001439 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001440 idx = fanout_demux_bpf(f, skb, num);
1441 break;
David S. Miller7736d332011-07-05 01:43:20 -07001442 }
1443
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001444 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1445 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001446
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001447 po = pkt_sk(f->arr[idx]);
David S. Millerdc99f602011-07-05 01:45:05 -07001448 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1449}
1450
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001451DEFINE_MUTEX(fanout_mutex);
1452EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001453static LIST_HEAD(fanout_list);
Mike Maloney4a69a862017-04-21 10:56:11 -04001454static u16 fanout_next_id;
David S. Millerdc99f602011-07-05 01:45:05 -07001455
1456static void __fanout_link(struct sock *sk, struct packet_sock *po)
1457{
1458 struct packet_fanout *f = po->fanout;
1459
1460 spin_lock(&f->lock);
1461 f->arr[f->num_members] = sk;
1462 smp_wmb();
1463 f->num_members++;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001464 if (f->num_members == 1)
1465 dev_add_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001466 spin_unlock(&f->lock);
1467}
1468
1469static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1470{
1471 struct packet_fanout *f = po->fanout;
1472 int i;
1473
1474 spin_lock(&f->lock);
1475 for (i = 0; i < f->num_members; i++) {
1476 if (f->arr[i] == sk)
1477 break;
1478 }
1479 BUG_ON(i >= f->num_members);
1480 f->arr[i] = f->arr[f->num_members - 1];
1481 f->num_members--;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001482 if (f->num_members == 0)
1483 __dev_remove_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001484 spin_unlock(&f->lock);
1485}
1486
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001487static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001488{
Eric Dumazet161642e2015-10-09 11:29:32 -07001489 if (sk->sk_family != PF_PACKET)
1490 return false;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001491
Eric Dumazet161642e2015-10-09 11:29:32 -07001492 return ptype->af_packet_priv == pkt_sk(sk)->fanout;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001493}
1494
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001495static void fanout_init_data(struct packet_fanout *f)
1496{
1497 switch (f->type) {
1498 case PACKET_FANOUT_LB:
1499 atomic_set(&f->rr_cur, 0);
1500 break;
1501 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001502 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001503 RCU_INIT_POINTER(f->bpf_prog, NULL);
1504 break;
1505 }
1506}
1507
1508static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1509{
1510 struct bpf_prog *old;
1511
1512 spin_lock(&f->lock);
1513 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1514 rcu_assign_pointer(f->bpf_prog, new);
1515 spin_unlock(&f->lock);
1516
1517 if (old) {
1518 synchronize_net();
1519 bpf_prog_destroy(old);
1520 }
1521}
1522
1523static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1524 unsigned int len)
1525{
1526 struct bpf_prog *new;
1527 struct sock_fprog fprog;
1528 int ret;
1529
1530 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1531 return -EPERM;
1532 if (len != sizeof(fprog))
1533 return -EINVAL;
1534 if (copy_from_user(&fprog, data, len))
1535 return -EFAULT;
1536
Daniel Borkmannbab18992015-10-02 15:17:33 +02001537 ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001538 if (ret)
1539 return ret;
1540
1541 __fanout_set_data_bpf(po->fanout, new);
1542 return 0;
1543}
1544
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001545static int fanout_set_data_ebpf(struct packet_sock *po, char __user *data,
1546 unsigned int len)
1547{
1548 struct bpf_prog *new;
1549 u32 fd;
1550
1551 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1552 return -EPERM;
1553 if (len != sizeof(fd))
1554 return -EINVAL;
1555 if (copy_from_user(&fd, data, len))
1556 return -EFAULT;
1557
Daniel Borkmann113214b2016-06-30 17:24:44 +02001558 new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001559 if (IS_ERR(new))
1560 return PTR_ERR(new);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001561
1562 __fanout_set_data_bpf(po->fanout, new);
1563 return 0;
1564}
1565
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001566static int fanout_set_data(struct packet_sock *po, char __user *data,
1567 unsigned int len)
1568{
1569 switch (po->fanout->type) {
1570 case PACKET_FANOUT_CBPF:
1571 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001572 case PACKET_FANOUT_EBPF:
1573 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001574 default:
1575 return -EINVAL;
zhong jiang07d53ae2018-08-04 19:41:41 +08001576 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001577}
1578
1579static void fanout_release_data(struct packet_fanout *f)
1580{
1581 switch (f->type) {
1582 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001583 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001584 __fanout_set_data_bpf(f, NULL);
zhong jiang07d53ae2018-08-04 19:41:41 +08001585 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001586}
1587
Mike Maloney4a69a862017-04-21 10:56:11 -04001588static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
1589{
1590 struct packet_fanout *f;
1591
1592 list_for_each_entry(f, &fanout_list, list) {
1593 if (f->id == candidate_id &&
1594 read_pnet(&f->net) == sock_net(sk)) {
1595 return false;
1596 }
1597 }
1598 return true;
1599}
1600
1601static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
1602{
1603 u16 id = fanout_next_id;
1604
1605 do {
1606 if (__fanout_id_is_free(sk, id)) {
1607 *new_id = id;
1608 fanout_next_id = id + 1;
1609 return true;
1610 }
1611
1612 id++;
1613 } while (id != fanout_next_id);
1614
1615 return false;
1616}
1617
David S. Miller7736d332011-07-05 01:43:20 -07001618static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001619{
Eric Dumazetd199fab2017-02-14 09:03:51 -08001620 struct packet_rollover *rollover = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001621 struct packet_sock *po = pkt_sk(sk);
1622 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001623 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001624 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001625 int err;
1626
1627 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001628 case PACKET_FANOUT_ROLLOVER:
1629 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1630 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001631 case PACKET_FANOUT_HASH:
1632 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001633 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001634 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001635 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001636 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001637 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001638 break;
1639 default:
1640 return -EINVAL;
1641 }
1642
Eric Dumazetd199fab2017-02-14 09:03:51 -08001643 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001644
Eric Dumazetd199fab2017-02-14 09:03:51 -08001645 err = -EALREADY;
David S. Millerdc99f602011-07-05 01:45:05 -07001646 if (po->fanout)
Eric Dumazetd199fab2017-02-14 09:03:51 -08001647 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001648
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001649 if (type == PACKET_FANOUT_ROLLOVER ||
1650 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Eric Dumazetd199fab2017-02-14 09:03:51 -08001651 err = -ENOMEM;
1652 rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
1653 if (!rollover)
1654 goto out;
1655 atomic_long_set(&rollover->num, 0);
1656 atomic_long_set(&rollover->num_huge, 0);
1657 atomic_long_set(&rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001658 }
1659
Mike Maloney4a69a862017-04-21 10:56:11 -04001660 if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
1661 if (id != 0) {
1662 err = -EINVAL;
1663 goto out;
1664 }
1665 if (!fanout_find_new_id(sk, &id)) {
1666 err = -ENOMEM;
1667 goto out;
1668 }
1669 /* ephemeral flag for the first socket in the group: drop it */
1670 flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
1671 }
1672
David S. Millerdc99f602011-07-05 01:45:05 -07001673 match = NULL;
1674 list_for_each_entry(f, &fanout_list, list) {
1675 if (f->id == id &&
1676 read_pnet(&f->net) == sock_net(sk)) {
1677 match = f;
1678 break;
1679 }
1680 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001681 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001682 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001683 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001684 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001685 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001686 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001687 if (!match)
1688 goto out;
1689 write_pnet(&match->net, sock_net(sk));
1690 match->id = id;
1691 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001692 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001693 INIT_LIST_HEAD(&match->list);
1694 spin_lock_init(&match->lock);
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001695 refcount_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001696 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001697 match->prot_hook.type = po->prot_hook.type;
1698 match->prot_hook.dev = po->prot_hook.dev;
1699 match->prot_hook.func = packet_rcv_fanout;
1700 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001701 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001702 list_add(&match->list, &fanout_list);
1703 }
1704 err = -EINVAL;
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001705
1706 spin_lock(&po->bind_lock);
1707 if (po->running &&
1708 match->type == type &&
Eric Dumazetafe62c62011-07-07 06:41:29 -07001709 match->prot_hook.type == po->prot_hook.type &&
1710 match->prot_hook.dev == po->prot_hook.dev) {
1711 err = -ENOSPC;
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001712 if (refcount_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001713 __dev_remove_pack(&po->prot_hook);
1714 po->fanout = match;
Mike Maloney57f015f2017-11-28 10:44:29 -05001715 po->rollover = rollover;
1716 rollover = NULL;
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001717 refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001718 __fanout_link(sk, po);
1719 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001720 }
1721 }
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001722 spin_unlock(&po->bind_lock);
1723
1724 if (err && !refcount_read(&match->sk_ref)) {
1725 list_del(&match->list);
1726 kfree(match);
1727 }
1728
Eric Dumazetafe62c62011-07-07 06:41:29 -07001729out:
Mike Maloney57f015f2017-11-28 10:44:29 -05001730 kfree(rollover);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001731 mutex_unlock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001732 return err;
1733}
1734
Anoob Soman2bd624b2017-02-15 20:25:39 +00001735/* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
1736 * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
1737 * It is the responsibility of the caller to call fanout_release_data() and
1738 * free the returned packet_fanout (after synchronize_net())
1739 */
1740static struct packet_fanout *fanout_release(struct sock *sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001741{
1742 struct packet_sock *po = pkt_sk(sk);
1743 struct packet_fanout *f;
1744
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001745 mutex_lock(&fanout_mutex);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001746 f = po->fanout;
1747 if (f) {
1748 po->fanout = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001749
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001750 if (refcount_dec_and_test(&f->sk_ref))
Eric Dumazetd199fab2017-02-14 09:03:51 -08001751 list_del(&f->list);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001752 else
1753 f = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001754 }
1755 mutex_unlock(&fanout_mutex);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001756
1757 return f;
David S. Millerdc99f602011-07-05 01:45:05 -07001758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001760static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
1761 struct sk_buff *skb)
1762{
1763 /* Earlier code assumed this would be a VLAN pkt, double-check
1764 * this now that we have the actual packet in hand. We can only
1765 * do this check on Ethernet devices.
1766 */
1767 if (unlikely(dev->type != ARPHRD_ETHER))
1768 return false;
1769
1770 skb_reset_mac_header(skb);
1771 return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
1772}
1773
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001774static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001776static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001778static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1779 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 struct sock *sk;
1782 struct sockaddr_pkt *spkt;
1783
1784 /*
1785 * When we registered the protocol we saved the socket in the data
1786 * field for just this event.
1787 */
1788
1789 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /*
1792 * Yank back the headers [hope the device set this
1793 * right or kerboom...]
1794 *
1795 * Incoming packets have ll header pulled,
1796 * push it back.
1797 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001798 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * so that this procedure is noop.
1800 */
1801
1802 if (skb->pkt_type == PACKET_LOOPBACK)
1803 goto out;
1804
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001805 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001806 goto out;
1807
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001808 skb = skb_share_check(skb, GFP_ATOMIC);
1809 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto oom;
1811
1812 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001813 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Phil Oester84531c22005-07-12 11:57:52 -07001815 /* drop conntrack reference */
1816 nf_reset(skb);
1817
Herbert Xuffbc6112007-02-04 23:33:10 -08001818 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001820 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 /*
1823 * The SOCK_PACKET socket receives _all_ frames.
1824 */
1825
1826 spkt->spkt_family = dev->type;
1827 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1828 spkt->spkt_protocol = skb->protocol;
1829
1830 /*
1831 * Charge the memory to the socket. This is done specifically
1832 * to prevent sockets using all the memory up.
1833 */
1834
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001835 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return 0;
1837
1838out:
1839 kfree_skb(skb);
1840oom:
1841 return 0;
1842}
1843
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001844static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
1845{
Yoshiki Komachi18bed892019-03-18 14:39:52 +09001846 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
1847 sock->type == SOCK_RAW) {
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001848 skb_reset_mac_header(skb);
1849 skb->protocol = dev_parse_header_protocol(skb);
1850 }
1851
1852 skb_probe_transport_header(skb);
1853}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855/*
1856 * Output a raw packet to a device layer. This bypasses all the other
1857 * protocol layers and you must therefore supply it with a complete frame
1858 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001859
Ying Xue1b784142015-03-02 15:37:48 +08001860static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1861 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001864 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001865 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct net_device *dev;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001867 struct sockcm_cookie sockc;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001868 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001870 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001873 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
1875
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001876 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001878 return -EINVAL;
1879 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1880 proto = saddr->spkt_protocol;
1881 } else
1882 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001885 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 */
1887
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001888 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001889retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001890 rcu_read_lock();
1891 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 err = -ENODEV;
1893 if (dev == NULL)
1894 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001895
David S. Millerd5e76b02007-01-25 19:30:36 -08001896 err = -ENETDOWN;
1897 if (!(dev->flags & IFF_UP))
1898 goto out_unlock;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001901 * You may not queue a frame bigger than the mtu. This is the lowest level
1902 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001904
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001905 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1906 if (!netif_supports_nofcs(dev)) {
1907 err = -EPROTONOSUPPORT;
1908 goto out_unlock;
1909 }
1910 extra_len = 4; /* We're doing our own CRC */
1911 }
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001914 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto out_unlock;
1916
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001917 if (!skb) {
1918 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001919 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001920 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001922 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001923 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001924 if (skb == NULL)
1925 return -ENOBUFS;
1926 /* FIXME: Save some space for broken drivers that write a hard
1927 * header at transmission time by themselves. PPP is the notable
1928 * one here. This should really be fixed at the driver level.
1929 */
1930 skb_reserve(skb, reserved);
1931 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001932
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001933 /* Try to align data part correctly */
1934 if (hhlen) {
1935 skb->data -= hhlen;
1936 skb->tail -= hhlen;
1937 if (len < hhlen)
1938 skb_reset_network_header(skb);
1939 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001940 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001941 if (err)
1942 goto out_free;
1943 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05001946 if (!dev_validate_header(dev, skb->data, len)) {
1947 err = -EINVAL;
1948 goto out_unlock;
1949 }
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001950 if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
1951 !packet_extra_vlan_len_allowed(dev, skb)) {
1952 err = -EMSGSIZE;
1953 goto out_unlock;
Ben Greear57f89bf2011-02-11 09:35:18 +00001954 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001955
Willem de Bruijn657a0662018-07-06 10:12:56 -04001956 sockcm_init(&sockc, sk);
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001957 if (msg->msg_controllen) {
1958 err = sock_cmsg_send(sk, msg, &sockc);
Soheil Hassas Yeganehf8e77182016-07-20 18:01:18 -04001959 if (unlikely(err))
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001960 goto out_unlock;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001961 }
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 skb->protocol = proto;
1964 skb->dev = dev;
1965 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001966 skb->mark = sk->sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07001967 skb->tstamp = sockc.transmit_time;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00001968
Willem de Bruijn8f932f72018-12-17 12:24:00 -05001969 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001971 if (unlikely(extra_len == 4))
1972 skb->no_fcs = 1;
1973
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001974 packet_parse_headers(skb, sock);
Jason Wangc1aad272013-03-25 20:19:57 +00001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001977 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001978 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001981 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001982out_free:
1983 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return err;
1985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001987static unsigned int run_filter(struct sk_buff *skb,
1988 const struct sock *sk,
1989 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct sk_filter *filter;
1992
Eric Dumazet80f8f102011-01-18 07:46:52 +00001993 rcu_read_lock();
1994 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001995 if (filter != NULL)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001996 res = bpf_prog_run_clear_cb(filter->prog, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
David S. Millerdbcb5852007-01-24 15:21:02 -08001999 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002002static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
2003 size_t *len)
2004{
2005 struct virtio_net_hdr vnet_hdr;
2006
2007 if (*len < sizeof(vnet_hdr))
2008 return -EINVAL;
2009 *len -= sizeof(vnet_hdr);
2010
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002011 if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002012 return -EINVAL;
2013
2014 return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
2015}
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00002018 * This function makes lazy skb cloning in hope that most of packets
2019 * are discarded by BPF.
2020 *
2021 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
2022 * and skb->cb are mangled. It works because (and until) packets
2023 * falling here are owned by current CPU. Output packets are cloned
2024 * by dev_queue_xmit_nit(), input packets are processed by net_bh
2025 * sequencially, so that if we return skb to original state on exit,
2026 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 */
2028
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002029static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
2030 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 struct sock *sk;
2033 struct sockaddr_ll *sll;
2034 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002035 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002037 unsigned int snaplen, res;
Weongyo Jeongda378452016-04-14 14:10:04 -07002038 bool is_drop_n_account = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 if (skb->pkt_type == PACKET_LOOPBACK)
2041 goto drop;
2042
2043 sk = pt->af_packet_priv;
2044 po = pkt_sk(sk);
2045
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002046 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002047 goto drop;
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 skb->dev = dev;
2050
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002051 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002053 * exported to higher levels.
2054 *
2055 * Otherwise, the device hides details of its frame
2056 * structure, so that corresponding packet head is
2057 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
2059 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002060 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 else if (skb->pkt_type == PACKET_OUTGOING) {
2062 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002063 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065 }
2066
2067 snaplen = skb->len;
2068
David S. Millerdbcb5852007-01-24 15:21:02 -08002069 res = run_filter(skb, sk, snaplen);
2070 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002071 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08002072 if (snaplen > res)
2073 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002075 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 goto drop_n_acct;
2077
2078 if (skb_shared(skb)) {
2079 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2080 if (nskb == NULL)
2081 goto drop_n_acct;
2082
2083 if (skb_head != skb->data) {
2084 skb->data = skb_head;
2085 skb->len = skb_len;
2086 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00002087 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 skb = nskb;
2089 }
2090
Eyal Birgerb4772ef2015-03-01 14:58:29 +02002091 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08002092
2093 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002096 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002097 sll->sll_ifindex = orig_dev->ifindex;
2098 else
2099 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002101 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Eyal Birger2472d762015-03-01 14:58:28 +02002103 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2104 * Use their space for storing the original skb length.
2105 */
2106 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (pskb_trim(skb, snaplen))
2109 goto drop_n_acct;
2110
2111 skb_set_owner_r(skb, sk);
2112 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002113 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Phil Oester84531c22005-07-12 11:57:52 -07002115 /* drop conntrack reference */
2116 nf_reset(skb);
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002119 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002120 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 __skb_queue_tail(&sk->sk_receive_queue, skb);
2122 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002123 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
2125
2126drop_n_acct:
Weongyo Jeongda378452016-04-14 14:10:04 -07002127 is_drop_n_account = true;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002128 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002129 po->stats.stats1.tp_drops++;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002130 atomic_inc(&sk->sk_drops);
2131 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133drop_n_restore:
2134 if (skb_head != skb->data && skb_shared(skb)) {
2135 skb->data = skb_head;
2136 skb->len = skb_len;
2137 }
2138drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002139 if (!is_drop_n_account)
2140 consume_skb(skb);
2141 else
2142 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144}
2145
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002146static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2147 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 struct sock *sk;
2150 struct packet_sock *po;
2151 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002152 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002153 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002155 unsigned int snaplen, res;
chetan lokef6fb8f12011-08-19 10:18:16 +00002156 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002157 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002159 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002160 __u32 ts_status;
Weongyo Jeongda378452016-04-14 14:10:04 -07002161 bool is_drop_n_account = false;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002162 bool do_vnet = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Atzm Watanabe51846352013-12-17 22:53:32 +09002164 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2165 * We may add members to them until current aligned size without forcing
2166 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2167 */
2168 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2169 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (skb->pkt_type == PACKET_LOOPBACK)
2172 goto drop;
2173
2174 sk = pt->af_packet_priv;
2175 po = pkt_sk(sk);
2176
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002177 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002178 goto drop;
2179
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002180 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002182 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 else if (skb->pkt_type == PACKET_OUTGOING) {
2184 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002185 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187 }
2188
2189 snaplen = skb->len;
2190
David S. Millerdbcb5852007-01-24 15:21:02 -08002191 res = run_filter(skb, sk, snaplen);
2192 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002193 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002194
2195 if (skb->ip_summed == CHECKSUM_PARTIAL)
2196 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002197 else if (skb->pkt_type != PACKET_OUTGOING &&
2198 (skb->ip_summed == CHECKSUM_COMPLETE ||
2199 skb_csum_unnecessary(skb)))
2200 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002201
David S. Millerdbcb5852007-01-24 15:21:02 -08002202 if (snaplen > res)
2203 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002206 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2207 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002209 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002210 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002211 (maclen < 16 ? 16 : maclen)) +
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002212 po->tp_reserve;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002213 if (po->has_vnet_hdr) {
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002214 netoff += sizeof(struct virtio_net_hdr);
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002215 do_vnet = true;
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 macoff = netoff - maclen;
2218 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002219 if (po->tp_version <= TPACKET_V2) {
2220 if (macoff + snaplen > po->rx_ring.frame_size) {
2221 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002222 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002223 if (skb_shared(skb)) {
2224 copy_skb = skb_clone(skb, GFP_ATOMIC);
2225 } else {
2226 copy_skb = skb_get(skb);
2227 skb_head = skb->data;
2228 }
2229 if (copy_skb)
2230 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002232 snaplen = po->rx_ring.frame_size - macoff;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002233 if ((int)snaplen < 0) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002234 snaplen = 0;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002235 do_vnet = false;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002238 } else if (unlikely(macoff + snaplen >
2239 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2240 u32 nval;
2241
2242 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2243 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2244 snaplen, nval, macoff);
2245 snaplen = nval;
2246 if (unlikely((int)snaplen < 0)) {
2247 snaplen = 0;
2248 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002249 do_vnet = false;
Eric Dumazetdc808112014-08-15 09:16:04 -07002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00002253 h.raw = packet_current_rx_frame(po, skb,
2254 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002255 if (!h.raw)
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002256 goto drop_n_account;
chetan lokef6fb8f12011-08-19 10:18:16 +00002257 if (po->tp_version <= TPACKET_V2) {
2258 packet_increment_rx_head(po, &po->rx_ring);
2259 /*
2260 * LOSING will be reported till you read the stats,
2261 * because it's COR - Clear On Read.
2262 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2263 * at packet level.
2264 */
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002265 if (po->stats.stats1.tp_drops)
chetan lokef6fb8f12011-08-19 10:18:16 +00002266 status |= TP_STATUS_LOSING;
2267 }
Eric Dumazet945d0152018-06-21 14:16:02 -07002268
2269 if (do_vnet &&
2270 virtio_net_hdr_from_skb(skb, h.raw + macoff -
2271 sizeof(struct virtio_net_hdr),
2272 vio_le(), true, 0))
2273 goto drop_n_account;
2274
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002275 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (copy_skb) {
2277 status |= TP_STATUS_COPY;
2278 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 spin_unlock(&sk->sk_receive_queue.lock);
2281
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002282 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002283
2284 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Daniel Borkmann7a513842013-04-23 00:39:29 +00002285 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002287 status |= ts_status;
2288
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002289 switch (po->tp_version) {
2290 case TPACKET_V1:
2291 h.h1->tp_len = skb->len;
2292 h.h1->tp_snaplen = snaplen;
2293 h.h1->tp_mac = macoff;
2294 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002295 h.h1->tp_sec = ts.tv_sec;
2296 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002297 hdrlen = sizeof(*h.h1);
2298 break;
2299 case TPACKET_V2:
2300 h.h2->tp_len = skb->len;
2301 h.h2->tp_snaplen = snaplen;
2302 h.h2->tp_mac = macoff;
2303 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002304 h.h2->tp_sec = ts.tv_sec;
2305 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002306 if (skb_vlan_tag_present(skb)) {
2307 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002308 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2309 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002310 } else {
2311 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002312 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002313 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002314 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002315 hdrlen = sizeof(*h.h2);
2316 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00002317 case TPACKET_V3:
2318 /* tp_nxt_offset,vlan are already populated above.
2319 * So DONT clear those fields here
2320 */
2321 h.h3->tp_status |= status;
2322 h.h3->tp_len = skb->len;
2323 h.h3->tp_snaplen = snaplen;
2324 h.h3->tp_mac = macoff;
2325 h.h3->tp_net = netoff;
chetan lokef6fb8f12011-08-19 10:18:16 +00002326 h.h3->tp_sec = ts.tv_sec;
2327 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002328 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f12011-08-19 10:18:16 +00002329 hdrlen = sizeof(*h.h3);
2330 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002331 default:
2332 BUG();
2333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002335 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002336 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 sll->sll_family = AF_PACKET;
2338 sll->sll_hatype = dev->type;
2339 sll->sll_protocol = skb->protocol;
2340 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002341 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002342 sll->sll_ifindex = orig_dev->ifindex;
2343 else
2344 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Ralf Baechlee16aa202006-12-07 00:11:33 -08002346 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002347
Changli Gaof6dafa92010-12-07 04:26:16 +00002348#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002349 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002350 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002352 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2353 macoff + snaplen);
2354
2355 for (start = h.raw; start < end; start += PAGE_SIZE)
2356 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002358 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002359#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002360
Dan Collinsda413ee2014-12-19 16:49:25 +13002361 if (po->tp_version <= TPACKET_V2) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002362 __packet_set_status(po, h.raw, status);
Dan Collinsda413ee2014-12-19 16:49:25 +13002363 sk->sk_data_ready(sk);
2364 } else {
chetan lokef6fb8f12011-08-19 10:18:16 +00002365 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368drop_n_restore:
2369 if (skb_head != skb->data && skb_shared(skb)) {
2370 skb->data = skb_head;
2371 skb->len = skb_len;
2372 }
2373drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002374 if (!is_drop_n_account)
2375 consume_skb(skb);
2376 else
2377 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return 0;
2379
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002380drop_n_account:
Weongyo Jeongda378452016-04-14 14:10:04 -07002381 is_drop_n_account = true;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002382 po->stats.stats1.tp_drops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 spin_unlock(&sk->sk_receive_queue.lock);
2384
David S. Miller676d2362014-04-11 16:15:36 -04002385 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002386 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto drop_n_restore;
2388}
2389
Johann Baudy69e3c752009-05-18 22:11:22 -07002390static void tpacket_destruct_skb(struct sk_buff *skb)
2391{
2392 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002393
Johann Baudy69e3c752009-05-18 22:11:22 -07002394 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002395 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002396 __u32 ts;
2397
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002398 ph = skb_zcopy_get_nouarg(skb);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002399 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002400
2401 ts = __packet_set_timestamp(po, ph, skb);
2402 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Johann Baudy69e3c752009-05-18 22:11:22 -07002403 }
2404
2405 sock_wfree(skb);
2406}
2407
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002408static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
2409{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002410 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2411 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2412 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
2413 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
2414 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
2415 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2416 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
2417
2418 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
2419 return -EINVAL;
2420
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002421 return 0;
2422}
2423
2424static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
2425 struct virtio_net_hdr *vnet_hdr)
2426{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002427 if (*len < sizeof(*vnet_hdr))
2428 return -EINVAL;
2429 *len -= sizeof(*vnet_hdr);
2430
Al Virocbbd26b2016-11-01 22:09:04 -04002431 if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002432 return -EFAULT;
2433
2434 return __packet_snd_vnet_parse(vnet_hdr, *len);
2435}
2436
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002437static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002438 void *frame, struct net_device *dev, void *data, int tp_len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002439 __be16 proto, unsigned char *addr, int hlen, int copylen,
2440 const struct sockcm_cookie *sockc)
Johann Baudy69e3c752009-05-18 22:11:22 -07002441{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002442 union tpacket_uhdr ph;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002443 int to_write, offset, len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002444 struct socket *sock = po->sk.sk_socket;
2445 struct page *page;
Johann Baudy69e3c752009-05-18 22:11:22 -07002446 int err;
2447
2448 ph.raw = frame;
2449
2450 skb->protocol = proto;
2451 skb->dev = dev;
2452 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002453 skb->mark = po->sk.sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002454 skb->tstamp = sockc->transmit_time;
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002455 skb_setup_tx_timestamp(skb, sockc->tsflags);
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002456 skb_zcopy_set_nouarg(skb, ph.raw);
Johann Baudy69e3c752009-05-18 22:11:22 -07002457
Herbert Xuae641942011-11-18 02:20:04 +00002458 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002459 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002460
Johann Baudy69e3c752009-05-18 22:11:22 -07002461 to_write = tp_len;
2462
2463 if (sock->type == SOCK_DGRAM) {
2464 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2465 NULL, tp_len);
2466 if (unlikely(err < 0))
2467 return -EINVAL;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002468 } else if (copylen) {
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002469 int hdrlen = min_t(int, copylen, tp_len);
2470
Johann Baudy69e3c752009-05-18 22:11:22 -07002471 skb_push(skb, dev->hard_header_len);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002472 skb_put(skb, copylen - dev->hard_header_len);
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002473 err = skb_store_bits(skb, 0, data, hdrlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002474 if (unlikely(err))
2475 return err;
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002476 if (!dev_validate_header(dev, skb->data, hdrlen))
2477 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002478
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002479 data += hdrlen;
2480 to_write -= hdrlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002481 }
2482
Johann Baudy69e3c752009-05-18 22:11:22 -07002483 offset = offset_in_page(data);
2484 len_max = PAGE_SIZE - offset;
2485 len = ((to_write > len_max) ? len_max : to_write);
2486
2487 skb->data_len = to_write;
2488 skb->len += to_write;
2489 skb->truesize += to_write;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002490 refcount_add(to_write, &po->sk.sk_wmem_alloc);
Johann Baudy69e3c752009-05-18 22:11:22 -07002491
2492 while (likely(to_write)) {
2493 nr_frags = skb_shinfo(skb)->nr_frags;
2494
2495 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002496 pr_err("Packet exceed the number of skb frags(%lu)\n",
2497 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002498 return -EFAULT;
2499 }
2500
Changli Gao0af55bb2010-12-01 02:52:20 +00002501 page = pgv_to_page(data);
2502 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002503 flush_dcache_page(page);
2504 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002505 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002506 to_write -= len;
2507 offset = 0;
2508 len_max = PAGE_SIZE;
2509 len = ((to_write > len_max) ? len_max : to_write);
2510 }
2511
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002512 packet_parse_headers(skb, sock);
Daniel Borkmannefdfa2f2015-11-11 23:25:40 +01002513
Johann Baudy69e3c752009-05-18 22:11:22 -07002514 return tp_len;
2515}
2516
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002517static int tpacket_parse_header(struct packet_sock *po, void *frame,
2518 int size_max, void **data)
2519{
2520 union tpacket_uhdr ph;
2521 int tp_len, off;
2522
2523 ph.raw = frame;
2524
2525 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002526 case TPACKET_V3:
2527 if (ph.h3->tp_next_offset != 0) {
2528 pr_warn_once("variable sized slot not supported");
2529 return -EINVAL;
2530 }
2531 tp_len = ph.h3->tp_len;
2532 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002533 case TPACKET_V2:
2534 tp_len = ph.h2->tp_len;
2535 break;
2536 default:
2537 tp_len = ph.h1->tp_len;
2538 break;
2539 }
2540 if (unlikely(tp_len > size_max)) {
2541 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2542 return -EMSGSIZE;
2543 }
2544
2545 if (unlikely(po->tp_tx_has_off)) {
2546 int off_min, off_max;
2547
2548 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2549 off_max = po->tx_ring.frame_size - tp_len;
2550 if (po->sk.sk_type == SOCK_DGRAM) {
2551 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002552 case TPACKET_V3:
2553 off = ph.h3->tp_net;
2554 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002555 case TPACKET_V2:
2556 off = ph.h2->tp_net;
2557 break;
2558 default:
2559 off = ph.h1->tp_net;
2560 break;
2561 }
2562 } else {
2563 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002564 case TPACKET_V3:
2565 off = ph.h3->tp_mac;
2566 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002567 case TPACKET_V2:
2568 off = ph.h2->tp_mac;
2569 break;
2570 default:
2571 off = ph.h1->tp_mac;
2572 break;
2573 }
2574 }
2575 if (unlikely((off < off_min) || (off_max < off)))
2576 return -EINVAL;
2577 } else {
2578 off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2579 }
2580
2581 *data = frame + off;
2582 return tp_len;
2583}
2584
Johann Baudy69e3c752009-05-18 22:11:22 -07002585static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2586{
Johann Baudy69e3c752009-05-18 22:11:22 -07002587 struct sk_buff *skb;
2588 struct net_device *dev;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002589 struct virtio_net_hdr *vnet_hdr = NULL;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002590 struct sockcm_cookie sockc;
Johann Baudy69e3c752009-05-18 22:11:22 -07002591 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002592 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002593 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002594 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002595 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002596 unsigned char *addr = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002597 int tp_len, size_max;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002598 void *data;
Johann Baudy69e3c752009-05-18 22:11:22 -07002599 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002600 int status = TP_STATUS_AVAILABLE;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002601 int hlen, tlen, copylen = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002602
Johann Baudy69e3c752009-05-18 22:11:22 -07002603 mutex_lock(&po->pg_vec_lock);
2604
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002605 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002606 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002607 proto = po->num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002608 } else {
2609 err = -EINVAL;
2610 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2611 goto out;
2612 if (msg->msg_namelen < (saddr->sll_halen
2613 + offsetof(struct sockaddr_ll,
2614 sll_addr)))
2615 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002616 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002617 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002618 if (po->sk.sk_socket->type == SOCK_DGRAM) {
2619 if (dev && msg->msg_namelen < dev->addr_len +
2620 offsetof(struct sockaddr_ll, sll_addr))
2621 goto out_put;
2622 addr = saddr->sll_addr;
2623 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002624 }
2625
Johann Baudy69e3c752009-05-18 22:11:22 -07002626 err = -ENXIO;
2627 if (unlikely(dev == NULL))
2628 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002629 err = -ENETDOWN;
2630 if (unlikely(!(dev->flags & IFF_UP)))
2631 goto out_put;
2632
Willem de Bruijn657a0662018-07-06 10:12:56 -04002633 sockcm_init(&sockc, &po->sk);
Douglas Caetano dos Santosd19b1832017-05-12 15:19:15 -03002634 if (msg->msg_controllen) {
2635 err = sock_cmsg_send(&po->sk, msg, &sockc);
2636 if (unlikely(err))
2637 goto out_put;
2638 }
2639
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002640 if (po->sk.sk_socket->type == SOCK_RAW)
2641 reserve = dev->hard_header_len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002642 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002643 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002644
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002645 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002646 size_max = dev->mtu + reserve + VLAN_HLEN;
David S. Miller09effa62013-08-07 17:11:00 -07002647
Johann Baudy69e3c752009-05-18 22:11:22 -07002648 do {
2649 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002650 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002651 if (unlikely(ph == NULL)) {
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002652 if (need_wait && need_resched())
2653 schedule();
Johann Baudy69e3c752009-05-18 22:11:22 -07002654 continue;
2655 }
2656
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002657 skb = NULL;
2658 tp_len = tpacket_parse_header(po, ph, size_max, &data);
2659 if (tp_len < 0)
2660 goto tpacket_error;
2661
Johann Baudy69e3c752009-05-18 22:11:22 -07002662 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002663 hlen = LL_RESERVED_SPACE(dev);
2664 tlen = dev->needed_tailroom;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002665 if (po->has_vnet_hdr) {
2666 vnet_hdr = data;
2667 data += sizeof(*vnet_hdr);
2668 tp_len -= sizeof(*vnet_hdr);
2669 if (tp_len < 0 ||
2670 __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
2671 tp_len = -EINVAL;
2672 goto tpacket_error;
2673 }
2674 copylen = __virtio16_to_cpu(vio_le(),
2675 vnet_hdr->hdr_len);
2676 }
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002677 copylen = max_t(int, copylen, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002678 skb = sock_alloc_send_skb(&po->sk,
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002679 hlen + tlen + sizeof(struct sockaddr_ll) +
2680 (copylen - dev->hard_header_len),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002681 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002682
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002683 if (unlikely(skb == NULL)) {
2684 /* we assume the socket was initially writeable ... */
2685 if (likely(len_sum > 0))
2686 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002687 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002688 }
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002689 tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002690 addr, hlen, copylen, &sockc);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002691 if (likely(tp_len >= 0) &&
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002692 tp_len > dev->mtu + reserve &&
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002693 !po->has_vnet_hdr &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002694 !packet_extra_vlan_len_allowed(dev, skb))
2695 tp_len = -EMSGSIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002696
2697 if (unlikely(tp_len < 0)) {
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002698tpacket_error:
Johann Baudy69e3c752009-05-18 22:11:22 -07002699 if (po->tp_loss) {
2700 __packet_set_status(po, ph,
2701 TP_STATUS_AVAILABLE);
2702 packet_increment_head(&po->tx_ring);
2703 kfree_skb(skb);
2704 continue;
2705 } else {
2706 status = TP_STATUS_WRONG_FORMAT;
2707 err = tp_len;
2708 goto out_status;
2709 }
2710 }
2711
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002712 if (po->has_vnet_hdr) {
2713 if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
2714 tp_len = -EINVAL;
2715 goto tpacket_error;
2716 }
2717 virtio_net_hdr_set_proto(skb, vnet_hdr);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002718 }
2719
Johann Baudy69e3c752009-05-18 22:11:22 -07002720 skb->destructor = tpacket_destruct_skb;
2721 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002722 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002723
2724 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002725 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002726 if (unlikely(err > 0)) {
2727 err = net_xmit_errno(err);
2728 if (err && __packet_get_status(po, ph) ==
2729 TP_STATUS_AVAILABLE) {
2730 /* skb was destructed already */
2731 skb = NULL;
2732 goto out_status;
2733 }
2734 /*
2735 * skb was dropped but not destructed yet;
2736 * let's treat it like congestion or err < 0
2737 */
2738 err = 0;
2739 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002740 packet_increment_head(&po->tx_ring);
2741 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002742 } while (likely((ph != NULL) ||
2743 /* Note: packet_read_pending() might be slow if we have
2744 * to call it as it's per_cpu variable, but in fast-path
2745 * we already short-circuit the loop with the first
2746 * condition, and luckily don't have to go that path
2747 * anyway.
2748 */
2749 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002750
2751 err = len_sum;
2752 goto out_put;
2753
Johann Baudy69e3c752009-05-18 22:11:22 -07002754out_status:
2755 __packet_set_status(po, ph, status);
2756 kfree_skb(skb);
2757out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002758 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002759out:
2760 mutex_unlock(&po->pg_vec_lock);
2761 return err;
2762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002764static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2765 size_t reserve, size_t len,
2766 size_t linear, int noblock,
2767 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002768{
2769 struct sk_buff *skb;
2770
2771 /* Under a page? Don't bother with paged skb. */
2772 if (prepad + len < PAGE_SIZE || !linear)
2773 linear = len;
2774
2775 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002776 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002777 if (!skb)
2778 return NULL;
2779
2780 skb_reserve(skb, reserve);
2781 skb_put(skb, linear);
2782 skb->data_len = len - linear;
2783 skb->len += len - linear;
2784
2785 return skb;
2786}
2787
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002788static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002791 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 struct sk_buff *skb;
2793 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002794 __be16 proto;
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002795 unsigned char *addr = NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002796 int err, reserve = 0;
Edward Jeec7d39e32015-10-08 14:56:49 -07002797 struct sockcm_cookie sockc;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002798 struct virtio_net_hdr vnet_hdr = { 0 };
2799 int offset = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002800 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002801 bool has_vnet_hdr = false;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002802 int hlen, tlen, linear;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002803 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002806 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002808
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002809 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002810 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 proto = po->num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 } else {
2813 err = -EINVAL;
2814 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2815 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002816 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002819 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002820 if (sock->type == SOCK_DGRAM) {
2821 if (dev && msg->msg_namelen < dev->addr_len +
2822 offsetof(struct sockaddr_ll, sll_addr))
2823 goto out_unlock;
2824 addr = saddr->sll_addr;
2825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002829 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002831 err = -ENETDOWN;
2832 if (unlikely(!(dev->flags & IFF_UP)))
2833 goto out_unlock;
2834
Willem de Bruijn657a0662018-07-06 10:12:56 -04002835 sockcm_init(&sockc, sk);
Edward Jeec7d39e32015-10-08 14:56:49 -07002836 sockc.mark = sk->sk_mark;
2837 if (msg->msg_controllen) {
2838 err = sock_cmsg_send(sk, msg, &sockc);
2839 if (unlikely(err))
2840 goto out_unlock;
2841 }
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (sock->type == SOCK_RAW)
2844 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002845 if (po->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002846 err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
2847 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002848 goto out_unlock;
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002849 has_vnet_hdr = true;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002850 }
2851
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002852 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2853 if (!netif_supports_nofcs(dev)) {
2854 err = -EPROTONOSUPPORT;
2855 goto out_unlock;
2856 }
2857 extra_len = 4; /* We're doing our own CRC */
2858 }
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 err = -EMSGSIZE;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002861 if (!vnet_hdr.gso_type &&
2862 (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 goto out_unlock;
2864
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002865 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002866 hlen = LL_RESERVED_SPACE(dev);
2867 tlen = dev->needed_tailroom;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002868 linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
2869 linear = max(linear, min_t(int, len, dev->hard_header_len));
2870 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002871 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002872 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 goto out_unlock;
2874
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002875 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002877 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002878 if (sock->type == SOCK_DGRAM) {
2879 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002880 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002881 goto out_free;
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002882 } else if (reserve) {
Willem de Bruijn9aad13b2018-05-24 18:10:30 -04002883 skb_reserve(skb, -reserve);
Nicolas Dichtel88a81212019-01-17 11:27:22 +01002884 if (len < reserve + sizeof(struct ipv6hdr) &&
2885 dev->min_header_len != dev->hard_header_len)
Willem de Bruijn993675a2018-07-11 12:00:45 -04002886 skb_reset_network_header(skb);
Willem de Bruijn9c707762014-11-19 13:10:16 -05002887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002890 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (err)
2892 goto out_free;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002893
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002894 if (sock->type == SOCK_RAW &&
2895 !dev_validate_header(dev, skb->data, len)) {
2896 err = -EINVAL;
2897 goto out_free;
2898 }
2899
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002900 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002902 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002903 !packet_extra_vlan_len_allowed(dev, skb)) {
2904 err = -EMSGSIZE;
2905 goto out_free;
Ben Greear57f89bf2011-02-11 09:35:18 +00002906 }
2907
David S. Miller09effa62013-08-07 17:11:00 -07002908 skb->protocol = proto;
2909 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 skb->priority = sk->sk_priority;
Edward Jeec7d39e32015-10-08 14:56:49 -07002911 skb->mark = sockc.mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002912 skb->tstamp = sockc.transmit_time;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002913
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002914 if (has_vnet_hdr) {
Jarno Rajahalmedb60eb5f2016-11-18 15:40:41 -08002915 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002916 if (err)
2917 goto out_free;
2918 len += sizeof(vnet_hdr);
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002919 virtio_net_hdr_set_proto(skb, &vnet_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002920 }
2921
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002922 packet_parse_headers(skb, sock);
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01002923
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002924 if (unlikely(extra_len == 4))
2925 skb->no_fcs = 1;
2926
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002927 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2929 goto out_unlock;
2930
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002931 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002933 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935out_free:
2936 kfree_skb(skb);
2937out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002938 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 dev_put(dev);
2940out:
2941 return err;
2942}
2943
Ying Xue1b784142015-03-02 15:37:48 +08002944static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07002945{
Johann Baudy69e3c752009-05-18 22:11:22 -07002946 struct sock *sk = sock->sk;
2947 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002948
Johann Baudy69e3c752009-05-18 22:11:22 -07002949 if (po->tx_ring.pg_vec)
2950 return tpacket_snd(po, msg);
2951 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002952 return packet_snd(sock, msg, len);
2953}
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955/*
2956 * Close a PACKET socket. This is fairly simple. We immediately go
2957 * to 'closed' state and remove our protocol entry in the device list.
2958 */
2959
2960static int packet_release(struct socket *sock)
2961{
2962 struct sock *sk = sock->sk;
2963 struct packet_sock *po;
Anoob Soman2bd624b2017-02-15 20:25:39 +00002964 struct packet_fanout *f;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002965 struct net *net;
chetan lokef6fb8f12011-08-19 10:18:16 +00002966 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 if (!sk)
2969 return 0;
2970
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002971 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 po = pkt_sk(sk);
2973
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002974 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002975 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002976 mutex_unlock(&net->packet.sklist_lock);
2977
2978 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08002979 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002980 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
stephen hemminger808f5112010-02-22 07:57:18 +00002982 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002983 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002984 packet_cached_dev_reset(po);
2985
Ben Greear160ff182011-06-01 07:18:52 +00002986 if (po->prot_hook.dev) {
2987 dev_put(po->prot_hook.dev);
2988 po->prot_hook.dev = NULL;
2989 }
stephen hemminger808f5112010-02-22 07:57:18 +00002990 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Eric Dumazet5171b372018-04-15 17:52:04 -07002994 lock_sock(sk);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002995 if (po->rx_ring.pg_vec) {
2996 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00002997 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002998 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002999
Phil Sutter9665d5d2013-02-01 07:21:41 +00003000 if (po->tx_ring.pg_vec) {
3001 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003002 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003003 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003004 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Anoob Soman2bd624b2017-02-15 20:25:39 +00003006 f = fanout_release(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07003007
stephen hemminger808f5112010-02-22 07:57:18 +00003008 synchronize_net();
Anoob Soman2bd624b2017-02-15 20:25:39 +00003009
Willem de Bruijnafa09252019-05-31 12:37:23 -04003010 kfree(po->rollover);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003011 if (f) {
3012 fanout_release_data(f);
3013 kfree(f);
3014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 /*
3016 * Now the socket is dead. No more input will appear.
3017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 sock_orphan(sk);
3019 sock->sk = NULL;
3020
3021 /* Purge queues */
3022
3023 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01003024 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003025 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 sock_put(sk);
3028 return 0;
3029}
3030
3031/*
3032 * Attach a packet hook.
3033 */
3034
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003035static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
3036 __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003039 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003040 __be16 proto_curr;
3041 bool need_rehook;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003042 struct net_device *dev = NULL;
3043 int ret = 0;
3044 bool unlisted = false;
David S. Millerdc99f602011-07-05 01:45:05 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 spin_lock(&po->bind_lock);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003048 rcu_read_lock();
3049
Willem de Bruijn49716132017-09-26 12:19:37 -04003050 if (po->fanout) {
3051 ret = -EINVAL;
3052 goto out_unlock;
3053 }
3054
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003055 if (name) {
3056 dev = dev_get_by_name_rcu(sock_net(sk), name);
3057 if (!dev) {
3058 ret = -ENODEV;
3059 goto out_unlock;
3060 }
3061 } else if (ifindex) {
3062 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
3063 if (!dev) {
3064 ret = -ENODEV;
3065 goto out_unlock;
3066 }
3067 }
3068
3069 if (dev)
3070 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003071
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003072 proto_curr = po->prot_hook.type;
3073 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003075 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003077 if (need_rehook) {
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003078 if (po->running) {
3079 rcu_read_unlock();
Eric Dumazet15fe0762017-11-28 08:03:30 -08003080 /* prevents packet_notifier() from calling
3081 * register_prot_hook()
3082 */
3083 po->num = 0;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003084 __unregister_prot_hook(sk, true);
3085 rcu_read_lock();
3086 dev_curr = po->prot_hook.dev;
3087 if (dev)
3088 unlisted = !dev_get_by_index_rcu(sock_net(sk),
3089 dev->ifindex);
3090 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003091
Eric Dumazet15fe0762017-11-28 08:03:30 -08003092 BUG_ON(po->running);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003093 po->num = proto;
3094 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003095
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003096 if (unlikely(unlisted)) {
3097 dev_put(dev);
3098 po->prot_hook.dev = NULL;
3099 po->ifindex = -1;
3100 packet_cached_dev_reset(po);
3101 } else {
3102 po->prot_hook.dev = dev;
3103 po->ifindex = dev ? dev->ifindex : 0;
3104 packet_cached_dev_assign(po, dev);
3105 }
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003106 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003107 if (dev_curr)
3108 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003109
3110 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 goto out_unlock;
3112
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003113 if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
David S. Millerce06b032011-07-04 01:44:29 -07003114 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08003115 } else {
3116 sk->sk_err = ENETDOWN;
3117 if (!sock_flag(sk, SOCK_DEAD))
3118 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
3120
3121out_unlock:
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003122 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 spin_unlock(&po->bind_lock);
3124 release_sock(sk);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128/*
3129 * Bind a packet socket to a device
3130 */
3131
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003132static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
3133 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003135 struct sock *sk = sock->sk;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003136 char name[sizeof(uaddr->sa_data) + 1];
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 /*
3139 * Check legality
3140 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003141
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003142 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return -EINVAL;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003144 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
3145 * zero-terminated.
3146 */
3147 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
3148 name[sizeof(uaddr->sa_data)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003150 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3154{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003155 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
3156 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 /*
3159 * Check legality
3160 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 if (addr_len < sizeof(struct sockaddr_ll))
3163 return -EINVAL;
3164 if (sll->sll_family != AF_PACKET)
3165 return -EINVAL;
3166
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003167 return packet_do_bind(sk, NULL, sll->sll_ifindex,
3168 sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
3171static struct proto packet_proto = {
3172 .name = "PACKET",
3173 .owner = THIS_MODULE,
3174 .obj_size = sizeof(struct packet_sock),
3175};
3176
3177/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003178 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 */
3180
Eric Paris3f378b62009-11-05 22:18:14 -08003181static int packet_create(struct net *net, struct socket *sock, int protocol,
3182 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
3184 struct sock *sk;
3185 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003186 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 int err;
3188
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003189 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003191 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3192 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return -ESOCKTNOSUPPORT;
3194
3195 sock->state = SS_UNCONNECTED;
3196
3197 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003198 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (sk == NULL)
3200 goto out;
3201
3202 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (sock->type == SOCK_PACKET)
3204 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 sock_init_data(sock, sk);
3207
3208 po = pkt_sk(sk);
3209 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003210 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003211 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003212
Daniel Borkmannb0138402014-01-15 16:25:36 +01003213 err = packet_alloc_pending(po);
3214 if (err)
3215 goto out2;
3216
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003217 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003220 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 /*
3223 * Attach a protocol block
3224 */
3225
3226 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003227 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003228 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (sock->type == SOCK_PACKET)
3232 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 po->prot_hook.af_packet_priv = sk;
3235
Al Viro0e11c912006-11-08 00:26:29 -08003236 if (proto) {
3237 po->prot_hook.type = proto;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003238 __register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003241 mutex_lock(&net->packet.sklist_lock);
Maxime Chevalliera4dc6a42019-03-16 14:41:30 +01003242 sk_add_node_tail_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003243 mutex_unlock(&net->packet.sklist_lock);
3244
3245 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003246 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003247 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003248
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003249 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003250out2:
3251 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252out:
3253 return err;
3254}
3255
3256/*
3257 * Pull a packet from our receive queue and hand it to the user.
3258 * If necessary we block.
3259 */
3260
Ying Xue1b784142015-03-02 15:37:48 +08003261static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3262 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 struct sock *sk = sock->sk;
3265 struct sk_buff *skb;
3266 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003267 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003268 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003271 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 goto out;
3273
3274#if 0
3275 /* What error should we return now? EUNATTACH? */
3276 if (pkt_sk(sk)->ifindex < 0)
3277 return -ENODEV;
3278#endif
3279
Richard Cochraned85b562010-04-07 22:41:28 +00003280 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003281 err = sock_recv_errqueue(sk, msg, len,
3282 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003283 goto out;
3284 }
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 * Call the generic datagram receiver. This handles all sorts
3288 * of horrible races and re-entrancy so we can forget about it
3289 * in the protocol layers.
3290 *
3291 * Now it will return ENETDOWN, if device have just gone down,
3292 * but then it will block.
3293 */
3294
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003295 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003298 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 * handles the blocking we don't see and worry about blocking
3300 * retries.
3301 */
3302
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003303 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto out;
3305
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003306 if (pkt_sk(sk)->pressure)
3307 packet_rcv_has_room(pkt_sk(sk), NULL);
3308
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003309 if (pkt_sk(sk)->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003310 err = packet_rcv_vnet(msg, skb, &len);
3311 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003312 goto out_free;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003313 vnet_hdr_len = sizeof(struct virtio_net_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003314 }
3315
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003316 /* You lose any data beyond the buffer you gave. If it worries
3317 * a user program they can ask the device for its MTU
3318 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003321 if (copied > len) {
3322 copied = len;
3323 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325
David S. Miller51f3d022014-11-05 16:46:40 -05003326 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 if (err)
3328 goto out_free;
3329
Eyal Birger2472d762015-03-01 14:58:28 +02003330 if (sock->type != SOCK_PACKET) {
3331 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3332
3333 /* Original length was stored in sockaddr_ll fields */
3334 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3335 sll->sll_family = AF_PACKET;
3336 sll->sll_protocol = skb->protocol;
3337 }
3338
Neil Horman3b885782009-10-12 13:26:31 -07003339 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003341 if (msg->msg_name) {
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003342 int copy_len;
3343
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003344 /* If the address length field is there to be filled
3345 * in, we fill it in now.
3346 */
3347 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003348 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003349 msg->msg_namelen = sizeof(struct sockaddr_pkt);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003350 copy_len = msg->msg_namelen;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003351 } else {
3352 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003353
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003354 msg->msg_namelen = sll->sll_halen +
3355 offsetof(struct sockaddr_ll, sll_addr);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003356 copy_len = msg->msg_namelen;
3357 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
3358 memset(msg->msg_name +
3359 offsetof(struct sockaddr_ll, sll_addr),
3360 0, sizeof(sll->sll_addr));
3361 msg->msg_namelen = sizeof(struct sockaddr_ll);
3362 }
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003363 }
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003364 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Herbert Xu8dc41942007-02-04 23:31:32 -08003367 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003368 struct tpacket_auxdata aux;
3369
3370 aux.tp_status = TP_STATUS_USER;
3371 if (skb->ip_summed == CHECKSUM_PARTIAL)
3372 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003373 else if (skb->pkt_type != PACKET_OUTGOING &&
3374 (skb->ip_summed == CHECKSUM_COMPLETE ||
3375 skb_csum_unnecessary(skb)))
3376 aux.tp_status |= TP_STATUS_CSUM_VALID;
3377
Eyal Birger2472d762015-03-01 14:58:28 +02003378 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003379 aux.tp_snaplen = skb->len;
3380 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003381 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003382 if (skb_vlan_tag_present(skb)) {
3383 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003384 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3385 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003386 } else {
3387 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003388 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003389 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003390 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003391 }
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 /*
3394 * Free or return the buffer as appropriate. Again this
3395 * hides all the races and re-entrancy issues from us.
3396 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003397 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399out_free:
3400 skb_free_datagram(sk, skb);
3401out:
3402 return err;
3403}
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003406 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 struct net_device *dev;
3409 struct sock *sk = sock->sk;
3410
3411 if (peer)
3412 return -EOPNOTSUPP;
3413
3414 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003415 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003416 rcu_read_lock();
3417 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3418 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003419 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003420 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003422 return sizeof(*uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003426 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 struct net_device *dev;
3429 struct sock *sk = sock->sk;
3430 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003431 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 if (peer)
3434 return -EOPNOTSUPP;
3435
3436 sll->sll_family = AF_PACKET;
3437 sll->sll_ifindex = po->ifindex;
3438 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003439 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003440 rcu_read_lock();
3441 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 if (dev) {
3443 sll->sll_hatype = dev->type;
3444 sll->sll_halen = dev->addr_len;
3445 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 } else {
3447 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3448 sll->sll_halen = 0;
3449 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003450 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003452 return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Wang Chen2aeb0b82008-07-14 20:49:46 -07003455static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3456 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 switch (i->type) {
3459 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003460 if (i->alen != dev->addr_len)
3461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003463 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003465 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 break;
3467 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003468 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003470 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003471 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003472 if (i->alen != dev->addr_len)
3473 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003474 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003475 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003476 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003477 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003478 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003479 default:
3480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003485static void packet_dev_mclist_delete(struct net_device *dev,
3486 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003488 struct packet_mclist *ml;
3489
3490 while ((ml = *mlp) != NULL) {
3491 if (ml->ifindex == dev->ifindex) {
3492 packet_dev_mc(dev, ml, -1);
3493 *mlp = ml->next;
3494 kfree(ml);
3495 } else
3496 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498}
3499
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003500static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 struct packet_sock *po = pkt_sk(sk);
3503 struct packet_mclist *ml, *i;
3504 struct net_device *dev;
3505 int err;
3506
3507 rtnl_lock();
3508
3509 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003510 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (!dev)
3512 goto done;
3513
3514 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003515 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 goto done;
3517
3518 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003519 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 if (i == NULL)
3521 goto done;
3522
3523 err = 0;
3524 for (ml = po->mclist; ml; ml = ml->next) {
3525 if (ml->ifindex == mreq->mr_ifindex &&
3526 ml->type == mreq->mr_type &&
3527 ml->alen == mreq->mr_alen &&
3528 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3529 ml->count++;
3530 /* Free the new element ... */
3531 kfree(i);
3532 goto done;
3533 }
3534 }
3535
3536 i->type = mreq->mr_type;
3537 i->ifindex = mreq->mr_ifindex;
3538 i->alen = mreq->mr_alen;
3539 memcpy(i->addr, mreq->mr_address, i->alen);
Mathias Krause309cf372016-04-10 12:52:28 +02003540 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 i->count = 1;
3542 i->next = po->mclist;
3543 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003544 err = packet_dev_mc(dev, i, 1);
3545 if (err) {
3546 po->mclist = i->next;
3547 kfree(i);
3548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550done:
3551 rtnl_unlock();
3552 return err;
3553}
3554
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003555static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 struct packet_mclist *ml, **mlp;
3558
3559 rtnl_lock();
3560
3561 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3562 if (ml->ifindex == mreq->mr_ifindex &&
3563 ml->type == mreq->mr_type &&
3564 ml->alen == mreq->mr_alen &&
3565 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3566 if (--ml->count == 0) {
3567 struct net_device *dev;
3568 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003569 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3570 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 kfree(ml);
3573 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 }
3576 }
3577 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
3581static void packet_flush_mclist(struct sock *sk)
3582{
3583 struct packet_sock *po = pkt_sk(sk);
3584 struct packet_mclist *ml;
3585
3586 if (!po->mclist)
3587 return;
3588
3589 rtnl_lock();
3590 while ((ml = po->mclist) != NULL) {
3591 struct net_device *dev;
3592
3593 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003594 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3595 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 kfree(ml);
3598 }
3599 rtnl_unlock();
3600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602static int
David S. Millerb7058842009-09-30 16:12:20 -07003603packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003606 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 int ret;
3608
3609 if (level != SOL_PACKET)
3610 return -ENOPROTOOPT;
3611
Johann Baudy69e3c752009-05-18 22:11:22 -07003612 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003613 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 case PACKET_DROP_MEMBERSHIP:
3615 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003616 struct packet_mreq_max mreq;
3617 int len = optlen;
3618 memset(&mreq, 0, sizeof(mreq));
3619 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003621 if (len > sizeof(mreq))
3622 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003623 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003625 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3626 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 if (optname == PACKET_ADD_MEMBERSHIP)
3628 ret = packet_mc_add(sk, &mreq);
3629 else
3630 ret = packet_mc_drop(sk, &mreq);
3631 return ret;
3632 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003635 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 {
chetan lokef6fb8f12011-08-19 10:18:16 +00003637 union tpacket_req_u req_u;
3638 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Eric Dumazet5171b372018-04-15 17:52:04 -07003640 lock_sock(sk);
chetan lokef6fb8f12011-08-19 10:18:16 +00003641 switch (po->tp_version) {
3642 case TPACKET_V1:
3643 case TPACKET_V2:
3644 len = sizeof(req_u.req);
3645 break;
3646 case TPACKET_V3:
3647 default:
3648 len = sizeof(req_u.req3);
3649 break;
3650 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003651 if (optlen < len) {
3652 ret = -EINVAL;
3653 } else {
3654 if (copy_from_user(&req_u.req, optval, len))
3655 ret = -EFAULT;
3656 else
3657 ret = packet_set_ring(sk, &req_u, 0,
3658 optname == PACKET_TX_RING);
3659 }
3660 release_sock(sk);
3661 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
3663 case PACKET_COPY_THRESH:
3664 {
3665 int val;
3666
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003667 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003669 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 return -EFAULT;
3671
3672 pkt_sk(sk)->copy_thresh = val;
3673 return 0;
3674 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003675 case PACKET_VERSION:
3676 {
3677 int val;
3678
3679 if (optlen != sizeof(val))
3680 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003681 if (copy_from_user(&val, optval, sizeof(val)))
3682 return -EFAULT;
3683 switch (val) {
3684 case TPACKET_V1:
3685 case TPACKET_V2:
chetan lokef6fb8f12011-08-19 10:18:16 +00003686 case TPACKET_V3:
Philip Pettersson84ac7262016-11-30 14:55:36 -08003687 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003688 default:
3689 return -EINVAL;
3690 }
Philip Pettersson84ac7262016-11-30 14:55:36 -08003691 lock_sock(sk);
3692 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3693 ret = -EBUSY;
3694 } else {
3695 po->tp_version = val;
3696 ret = 0;
3697 }
3698 release_sock(sk);
3699 return ret;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003700 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003701 case PACKET_RESERVE:
3702 {
3703 unsigned int val;
3704
3705 if (optlen != sizeof(val))
3706 return -EINVAL;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003707 if (copy_from_user(&val, optval, sizeof(val)))
3708 return -EFAULT;
Andrey Konovalovbcc53642017-03-29 16:11:22 +02003709 if (val > INT_MAX)
3710 return -EINVAL;
Willem de Bruijnc27927e2017-08-10 12:41:58 -04003711 lock_sock(sk);
3712 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3713 ret = -EBUSY;
3714 } else {
3715 po->tp_reserve = val;
3716 ret = 0;
3717 }
3718 release_sock(sk);
3719 return ret;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003720 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003721 case PACKET_LOSS:
3722 {
3723 unsigned int val;
3724
3725 if (optlen != sizeof(val))
3726 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003727 if (copy_from_user(&val, optval, sizeof(val)))
3728 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003729
3730 lock_sock(sk);
3731 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3732 ret = -EBUSY;
3733 } else {
3734 po->tp_loss = !!val;
3735 ret = 0;
3736 }
3737 release_sock(sk);
3738 return ret;
Johann Baudy69e3c752009-05-18 22:11:22 -07003739 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003740 case PACKET_AUXDATA:
3741 {
3742 int val;
3743
3744 if (optlen < sizeof(val))
3745 return -EINVAL;
3746 if (copy_from_user(&val, optval, sizeof(val)))
3747 return -EFAULT;
3748
Willem de Bruijna6361f02018-04-23 17:37:03 -04003749 lock_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003750 po->auxdata = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003751 release_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003752 return 0;
3753 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003754 case PACKET_ORIGDEV:
3755 {
3756 int val;
3757
3758 if (optlen < sizeof(val))
3759 return -EINVAL;
3760 if (copy_from_user(&val, optval, sizeof(val)))
3761 return -EFAULT;
3762
Willem de Bruijna6361f02018-04-23 17:37:03 -04003763 lock_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003764 po->origdev = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003765 release_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003766 return 0;
3767 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003768 case PACKET_VNET_HDR:
3769 {
3770 int val;
3771
3772 if (sock->type != SOCK_RAW)
3773 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003774 if (optlen < sizeof(val))
3775 return -EINVAL;
3776 if (copy_from_user(&val, optval, sizeof(val)))
3777 return -EFAULT;
3778
Willem de Bruijna6361f02018-04-23 17:37:03 -04003779 lock_sock(sk);
3780 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3781 ret = -EBUSY;
3782 } else {
3783 po->has_vnet_hdr = !!val;
3784 ret = 0;
3785 }
3786 release_sock(sk);
3787 return ret;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003788 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003789 case PACKET_TIMESTAMP:
3790 {
3791 int val;
3792
3793 if (optlen != sizeof(val))
3794 return -EINVAL;
3795 if (copy_from_user(&val, optval, sizeof(val)))
3796 return -EFAULT;
3797
3798 po->tp_tstamp = val;
3799 return 0;
3800 }
David S. Millerdc99f602011-07-05 01:45:05 -07003801 case PACKET_FANOUT:
3802 {
3803 int val;
3804
3805 if (optlen != sizeof(val))
3806 return -EINVAL;
3807 if (copy_from_user(&val, optval, sizeof(val)))
3808 return -EFAULT;
3809
3810 return fanout_add(sk, val & 0xffff, val >> 16);
3811 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003812 case PACKET_FANOUT_DATA:
3813 {
3814 if (!po->fanout)
3815 return -EINVAL;
3816
3817 return fanout_set_data(po, optval, optlen);
3818 }
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003819 case PACKET_IGNORE_OUTGOING:
3820 {
3821 int val;
3822
3823 if (optlen != sizeof(val))
3824 return -EINVAL;
3825 if (copy_from_user(&val, optval, sizeof(val)))
3826 return -EFAULT;
3827 if (val < 0 || val > 1)
3828 return -EINVAL;
3829
3830 po->prot_hook.ignore_outgoing = !!val;
3831 return 0;
3832 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003833 case PACKET_TX_HAS_OFF:
3834 {
3835 unsigned int val;
3836
3837 if (optlen != sizeof(val))
3838 return -EINVAL;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003839 if (copy_from_user(&val, optval, sizeof(val)))
3840 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003841
3842 lock_sock(sk);
3843 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3844 ret = -EBUSY;
3845 } else {
3846 po->tp_tx_has_off = !!val;
3847 ret = 0;
3848 }
3849 release_sock(sk);
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003850 return 0;
3851 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003852 case PACKET_QDISC_BYPASS:
3853 {
3854 int val;
3855
3856 if (optlen != sizeof(val))
3857 return -EINVAL;
3858 if (copy_from_user(&val, optval, sizeof(val)))
3859 return -EFAULT;
3860
3861 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3862 return 0;
3863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 default:
3865 return -ENOPROTOOPT;
3866 }
3867}
3868
3869static int packet_getsockopt(struct socket *sock, int level, int optname,
3870 char __user *optval, int __user *optlen)
3871{
3872 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003873 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 struct sock *sk = sock->sk;
3875 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003876 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003877 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003878 struct tpacket_rollover_stats rstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
3880 if (level != SOL_PACKET)
3881 return -ENOPROTOOPT;
3882
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003883 if (get_user(len, optlen))
3884 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 if (len < 0)
3887 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003888
Johann Baudy69e3c752009-05-18 22:11:22 -07003889 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003892 memcpy(&st, &po->stats, sizeof(st));
3893 memset(&po->stats, 0, sizeof(po->stats));
3894 spin_unlock_bh(&sk->sk_receive_queue.lock);
3895
chetan lokef6fb8f12011-08-19 10:18:16 +00003896 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003897 lv = sizeof(struct tpacket_stats_v3);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003898 st.stats3.tp_packets += st.stats3.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003899 data = &st.stats3;
chetan lokef6fb8f12011-08-19 10:18:16 +00003900 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003901 lv = sizeof(struct tpacket_stats);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003902 st.stats1.tp_packets += st.stats1.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003903 data = &st.stats1;
chetan lokef6fb8f12011-08-19 10:18:16 +00003904 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003907 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003908 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003909 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003910 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003911 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003912 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003913 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003914 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003915 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003916 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003917 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003918 break;
3919 case PACKET_HDRLEN:
3920 if (len > sizeof(int))
3921 len = sizeof(int);
Alexander Potapenkofd2c83b2017-04-25 18:51:46 +02003922 if (len < sizeof(int))
3923 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003924 if (copy_from_user(&val, optval, len))
3925 return -EFAULT;
3926 switch (val) {
3927 case TPACKET_V1:
3928 val = sizeof(struct tpacket_hdr);
3929 break;
3930 case TPACKET_V2:
3931 val = sizeof(struct tpacket2_hdr);
3932 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00003933 case TPACKET_V3:
3934 val = sizeof(struct tpacket3_hdr);
3935 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003936 default:
3937 return -EINVAL;
3938 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003939 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003940 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003941 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003942 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003943 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003944 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003945 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003946 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003947 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003948 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003949 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003950 val = (po->fanout ?
3951 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003952 ((u32)po->fanout->type << 16) |
3953 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003954 0);
David S. Millerdc99f602011-07-05 01:45:05 -07003955 break;
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003956 case PACKET_IGNORE_OUTGOING:
3957 val = po->prot_hook.ignore_outgoing;
3958 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003959 case PACKET_ROLLOVER_STATS:
Mike Maloney57f015f2017-11-28 10:44:29 -05003960 if (!po->rollover)
Willem de Bruijna9b63912015-05-12 11:56:50 -04003961 return -EINVAL;
Mike Maloney57f015f2017-11-28 10:44:29 -05003962 rstats.tp_all = atomic_long_read(&po->rollover->num);
3963 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
3964 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
3965 data = &rstats;
3966 lv = sizeof(rstats);
Willem de Bruijna9b63912015-05-12 11:56:50 -04003967 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003968 case PACKET_TX_HAS_OFF:
3969 val = po->tp_tx_has_off;
3970 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003971 case PACKET_QDISC_BYPASS:
3972 val = packet_use_direct_xmit(po);
3973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 default:
3975 return -ENOPROTOOPT;
3976 }
3977
Eric Dumazetc06fff62012-04-19 21:56:11 +00003978 if (len > lv)
3979 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003980 if (put_user(len, optlen))
3981 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08003982 if (copy_to_user(optval, data, len))
3983 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
3987
Willem de Bruijn719c44d2016-06-07 12:06:34 -04003988#ifdef CONFIG_COMPAT
3989static int compat_packet_setsockopt(struct socket *sock, int level, int optname,
3990 char __user *optval, unsigned int optlen)
3991{
3992 struct packet_sock *po = pkt_sk(sock->sk);
3993
3994 if (level != SOL_PACKET)
3995 return -ENOPROTOOPT;
3996
3997 if (optname == PACKET_FANOUT_DATA &&
3998 po->fanout && po->fanout->type == PACKET_FANOUT_CBPF) {
3999 optval = (char __user *)get_compat_bpf_fprog(optval);
4000 if (!optval)
4001 return -EFAULT;
4002 optlen = sizeof(struct sock_fprog);
4003 }
4004
4005 return packet_setsockopt(sock, level, optname, optval, optlen);
4006}
4007#endif
4008
Jiri Pirko351638e2013-05-28 01:30:21 +00004009static int packet_notifier(struct notifier_block *this,
4010 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
4012 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00004013 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004014 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
stephen hemminger808f5112010-02-22 07:57:18 +00004016 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004017 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 struct packet_sock *po = pkt_sk(sk);
4019
4020 switch (msg) {
4021 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07004023 packet_dev_mclist_delete(dev, &po->mclist);
David S. Millera2efcfa2007-05-29 13:12:50 -07004024 /* fallthrough */
4025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 case NETDEV_DOWN:
4027 if (dev->ifindex == po->ifindex) {
4028 spin_lock(&po->bind_lock);
4029 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07004030 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 sk->sk_err = ENETDOWN;
4032 if (!sock_flag(sk, SOCK_DEAD))
4033 sk->sk_error_report(sk);
4034 }
4035 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01004036 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00004038 if (po->prot_hook.dev)
4039 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 po->prot_hook.dev = NULL;
4041 }
4042 spin_unlock(&po->bind_lock);
4043 }
4044 break;
4045 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00004046 if (dev->ifindex == po->ifindex) {
4047 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004048 if (po->num)
4049 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00004050 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 break;
4053 }
4054 }
stephen hemminger808f5112010-02-22 07:57:18 +00004055 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return NOTIFY_DONE;
4057}
4058
4059
4060static int packet_ioctl(struct socket *sock, unsigned int cmd,
4061 unsigned long arg)
4062{
4063 struct sock *sk = sock->sk;
4064
Johann Baudy69e3c752009-05-18 22:11:22 -07004065 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004066 case SIOCOUTQ:
4067 {
4068 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07004069
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004070 return put_user(amount, (int __user *)arg);
4071 }
4072 case SIOCINQ:
4073 {
4074 struct sk_buff *skb;
4075 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004077 spin_lock_bh(&sk->sk_receive_queue.lock);
4078 skb = skb_peek(&sk->sk_receive_queue);
4079 if (skb)
4080 amount = skb->len;
4081 spin_unlock_bh(&sk->sk_receive_queue.lock);
4082 return put_user(amount, (int __user *)arg);
4083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004085 case SIOCADDRT:
4086 case SIOCDELRT:
4087 case SIOCDARP:
4088 case SIOCGARP:
4089 case SIOCSARP:
4090 case SIOCGIFADDR:
4091 case SIOCSIFADDR:
4092 case SIOCGIFBRDADDR:
4093 case SIOCSIFBRDADDR:
4094 case SIOCGIFNETMASK:
4095 case SIOCSIFNETMASK:
4096 case SIOCGIFDSTADDR:
4097 case SIOCSIFDSTADDR:
4098 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004099 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100#endif
4101
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004102 default:
4103 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
4105 return 0;
4106}
4107
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004108static __poll_t packet_poll(struct file *file, struct socket *sock,
4109 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct sock *sk = sock->sk;
4112 struct packet_sock *po = pkt_sk(sk);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004113 __poll_t mask = datagram_poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004116 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004117 if (!packet_previous_rx_frame(po, &po->rx_ring,
4118 TP_STATUS_KERNEL))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004119 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04004121 if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04004122 po->pressure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004124 spin_lock_bh(&sk->sk_write_queue.lock);
4125 if (po->tx_ring.pg_vec) {
4126 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004127 mask |= EPOLLOUT | EPOLLWRNORM;
Johann Baudy69e3c752009-05-18 22:11:22 -07004128 }
4129 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return mask;
4131}
4132
4133
4134/* Dirty? Well, I still did not learn better way to account
4135 * for user mmaps.
4136 */
4137
4138static void packet_mm_open(struct vm_area_struct *vma)
4139{
4140 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004141 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (sk)
4145 atomic_inc(&pkt_sk(sk)->mapped);
4146}
4147
4148static void packet_mm_close(struct vm_area_struct *vma)
4149{
4150 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004151 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 if (sk)
4155 atomic_dec(&pkt_sk(sk)->mapped);
4156}
4157
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004158static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004159 .open = packet_mm_open,
4160 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161};
4162
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004163static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
4164 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
4166 int i;
4167
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004168 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004169 if (likely(pg_vec[i].buffer)) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004170 if (is_vmalloc_addr(pg_vec[i].buffer))
4171 vfree(pg_vec[i].buffer);
4172 else
4173 free_pages((unsigned long)pg_vec[i].buffer,
4174 order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004175 pg_vec[i].buffer = NULL;
4176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 }
4178 kfree(pg_vec);
4179}
4180
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004181static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004182{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004183 char *buffer;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004184 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
4185 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07004186
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004187 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004188 if (buffer)
4189 return buffer;
4190
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004191 /* __get_free_pages failed, fall back to vmalloc */
4192 buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
4193 if (buffer)
4194 return buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08004195
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004196 /* vmalloc failed, lets dig into swap here */
4197 gfp_flags &= ~__GFP_NORETRY;
4198 buffer = (char *) __get_free_pages(gfp_flags, order);
4199 if (buffer)
4200 return buffer;
4201
4202 /* complete and utter failure */
4203 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004204}
4205
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004206static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004207{
4208 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004209 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004210 int i;
4211
Christoph Paasch398f0132019-03-18 23:14:52 -07004212 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004213 if (unlikely(!pg_vec))
4214 goto out;
4215
4216 for (i = 0; i < block_nr; i++) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004217 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004218 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004219 goto out_free_pgvec;
4220 }
4221
4222out:
4223 return pg_vec;
4224
4225out_free_pgvec:
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004226 free_pg_vec(pg_vec, order, block_nr);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004227 pg_vec = NULL;
4228 goto out;
4229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
chetan lokef6fb8f12011-08-19 10:18:16 +00004231static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004232 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
Neil Horman0e3125c2010-11-16 10:26:47 -08004234 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 struct packet_sock *po = pkt_sk(sk);
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004236 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004237 struct packet_ring_buffer *rb;
4238 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004239 __be16 num;
chetan lokef6fb8f12011-08-19 10:18:16 +00004240 int err = -EINVAL;
4241 /* Added to avoid minimal code churn */
4242 struct tpacket_req *req = &req_u->req;
4243
Johann Baudy69e3c752009-05-18 22:11:22 -07004244 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4245 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4246
4247 err = -EBUSY;
4248 if (!closing) {
4249 if (atomic_read(&po->mapped))
4250 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004251 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004252 goto out;
4253 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 if (req->tp_block_nr) {
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004256 unsigned int min_frame_size;
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004259 err = -EBUSY;
4260 if (unlikely(rb->pg_vec))
4261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004263 switch (po->tp_version) {
4264 case TPACKET_V1:
4265 po->tp_hdrlen = TPACKET_HDRLEN;
4266 break;
4267 case TPACKET_V2:
4268 po->tp_hdrlen = TPACKET2_HDRLEN;
4269 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004270 case TPACKET_V3:
4271 po->tp_hdrlen = TPACKET3_HDRLEN;
4272 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004273 }
4274
Johann Baudy69e3c752009-05-18 22:11:22 -07004275 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004276 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004277 goto out;
Tobias Klauser90836b62015-11-17 10:40:21 +01004278 if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004279 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004280 min_frame_size = po->tp_hdrlen + po->tp_reserve;
Eric Dumazetdc808112014-08-15 09:16:04 -07004281 if (po->tp_version >= TPACKET_V3 &&
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004282 req->tp_block_size <
4283 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
Eric Dumazetdc808112014-08-15 09:16:04 -07004284 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004285 if (unlikely(req->tp_frame_size < min_frame_size))
Johann Baudy69e3c752009-05-18 22:11:22 -07004286 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004287 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004288 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Tobias Klauser4194b492015-11-17 10:38:36 +01004290 rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
4291 if (unlikely(rb->frames_per_block == 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004292 goto out;
Kal Conleyfc628142019-02-10 09:57:11 +01004293 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
Andrey Konovalov8f8d28e2017-03-29 16:11:21 +02004294 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004295 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4296 req->tp_frame_nr))
4297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 err = -ENOMEM;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004300 order = get_order(req->tp_block_size);
4301 pg_vec = alloc_pg_vec(req, order);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004302 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 goto out;
chetan lokef6fb8f12011-08-19 10:18:16 +00004304 switch (po->tp_version) {
4305 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004306 /* Block transmit is not supported yet */
4307 if (!tx_ring) {
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304308 init_prb_bdqc(po, rb, pg_vec, req_u);
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004309 } else {
4310 struct tpacket_req3 *req3 = &req_u->req3;
4311
4312 if (req3->tp_retire_blk_tov ||
4313 req3->tp_sizeof_priv ||
4314 req3->tp_feature_req_word) {
4315 err = -EINVAL;
4316 goto out;
4317 }
4318 }
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004319 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004320 default:
4321 break;
4322 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004323 }
4324 /* Done */
4325 else {
4326 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004327 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 }
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 /* Detach socket from network */
4333 spin_lock(&po->bind_lock);
4334 was_running = po->running;
4335 num = po->num;
4336 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004338 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
4340 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 synchronize_net();
4343
4344 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004345 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 if (closing || atomic_read(&po->mapped) == 0) {
4347 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004348 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004349 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07004350 rb->frame_max = (req->tp_frame_nr - 1);
4351 rb->head = 0;
4352 rb->frame_size = req->tp_frame_size;
4353 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004355 swap(rb->pg_vec_order, order);
Changli Gaoc053fd92010-12-10 16:02:20 -08004356 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Johann Baudy69e3c752009-05-18 22:11:22 -07004358 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4359 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4360 tpacket_rcv : packet_rcv;
4361 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004363 pr_err("packet_mmap: vma is busy: %d\n",
4364 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 }
Herbert Xu905db442009-01-30 14:12:06 -08004366 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004369 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004371 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 }
4373 spin_unlock(&po->bind_lock);
WANG Congc800aaf2017-07-24 10:07:32 -07004374 if (pg_vec && (po->tp_version > TPACKET_V2)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004375 /* Because we don't support block-based V3 on tx-ring */
4376 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004377 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f12011-08-19 10:18:16 +00004378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 if (pg_vec)
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004381 free_pg_vec(pg_vec, order, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382out:
4383 return err;
4384}
4385
Johann Baudy69e3c752009-05-18 22:11:22 -07004386static int packet_mmap(struct file *file, struct socket *sock,
4387 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
4389 struct sock *sk = sock->sk;
4390 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004391 unsigned long size, expected_size;
4392 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 unsigned long start;
4394 int err = -EINVAL;
4395 int i;
4396
4397 if (vma->vm_pgoff)
4398 return -EINVAL;
4399
Herbert Xu905db442009-01-30 14:12:06 -08004400 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004401
4402 expected_size = 0;
4403 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4404 if (rb->pg_vec) {
4405 expected_size += rb->pg_vec_len
4406 * rb->pg_vec_pages
4407 * PAGE_SIZE;
4408 }
4409 }
4410
4411 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004413
4414 size = vma->vm_end - vma->vm_start;
4415 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 goto out;
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004419 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4420 if (rb->pg_vec == NULL)
4421 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004422
Johann Baudy69e3c752009-05-18 22:11:22 -07004423 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004424 struct page *page;
4425 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004426 int pg_num;
4427
Changli Gaoc56b4d92010-12-01 02:52:57 +00004428 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4429 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004430 err = vm_insert_page(vma, start, page);
4431 if (unlikely(err))
4432 goto out;
4433 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004434 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004435 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004438
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004439 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 vma->vm_ops = &packet_mmap_ops;
4441 err = 0;
4442
4443out:
Herbert Xu905db442009-01-30 14:12:06 -08004444 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 return err;
4446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004448static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 .family = PF_PACKET,
4450 .owner = THIS_MODULE,
4451 .release = packet_release,
4452 .bind = packet_bind_spkt,
4453 .connect = sock_no_connect,
4454 .socketpair = sock_no_socketpair,
4455 .accept = sock_no_accept,
4456 .getname = packet_getname_spkt,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004457 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004459 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 .listen = sock_no_listen,
4461 .shutdown = sock_no_shutdown,
4462 .setsockopt = sock_no_setsockopt,
4463 .getsockopt = sock_no_getsockopt,
4464 .sendmsg = packet_sendmsg_spkt,
4465 .recvmsg = packet_recvmsg,
4466 .mmap = sock_no_mmap,
4467 .sendpage = sock_no_sendpage,
4468};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004470static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 .family = PF_PACKET,
4472 .owner = THIS_MODULE,
4473 .release = packet_release,
4474 .bind = packet_bind,
4475 .connect = sock_no_connect,
4476 .socketpair = sock_no_socketpair,
4477 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004478 .getname = packet_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004479 .poll = packet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004481 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 .listen = sock_no_listen,
4483 .shutdown = sock_no_shutdown,
4484 .setsockopt = packet_setsockopt,
4485 .getsockopt = packet_getsockopt,
Willem de Bruijn719c44d2016-06-07 12:06:34 -04004486#ifdef CONFIG_COMPAT
4487 .compat_setsockopt = compat_packet_setsockopt,
4488#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 .sendmsg = packet_sendmsg,
4490 .recvmsg = packet_recvmsg,
4491 .mmap = packet_mmap,
4492 .sendpage = sock_no_sendpage,
4493};
4494
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004495static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 .family = PF_PACKET,
4497 .create = packet_create,
4498 .owner = THIS_MODULE,
4499};
4500
4501static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004502 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503};
4504
4505#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004508 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
Denis V. Luneve372c412007-11-19 22:31:54 -08004510 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004511
4512 rcu_read_lock();
4513 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4517{
Herbert Xu1bf40952007-12-16 14:04:02 -08004518 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004519 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
4522static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004523 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
stephen hemminger808f5112010-02-22 07:57:18 +00004525 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004528static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 if (v == SEQ_START_TOKEN)
4531 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
4532 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004533 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 const struct packet_sock *po = pkt_sk(s);
4535
4536 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004537 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004539 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 s->sk_type,
4541 ntohs(po->num),
4542 po->ifindex,
4543 po->running,
4544 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004545 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004546 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 }
4548
4549 return 0;
4550}
4551
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004552static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 .start = packet_seq_start,
4554 .next = packet_seq_next,
4555 .stop = packet_seq_stop,
4556 .show = packet_seq_show,
4557};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558#endif
4559
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004560static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004561{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004562 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004563 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004564
Christoph Hellwigc3506372018-04-10 19:42:55 +02004565 if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
4566 sizeof(struct seq_net_private)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004567 return -ENOMEM;
4568
4569 return 0;
4570}
4571
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004572static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004573{
Gao fengece31ff2013-02-18 01:34:56 +00004574 remove_proc_entry("packet", net->proc_net);
Vasily Averin669f8f12017-11-12 22:27:49 +03004575 WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004576}
4577
4578static struct pernet_operations packet_net_ops = {
4579 .init = packet_net_init,
4580 .exit = packet_net_exit,
4581};
4582
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584static void __exit packet_exit(void)
4585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004587 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 sock_unregister(PF_PACKET);
4589 proto_unregister(&packet_proto);
4590}
4591
4592static int __init packet_init(void)
4593{
YueHaibing36096f22019-05-09 22:52:20 +08004594 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
YueHaibing36096f22019-05-09 22:52:20 +08004596 rc = proto_register(&packet_proto, 0);
4597 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 goto out;
YueHaibing36096f22019-05-09 22:52:20 +08004599 rc = sock_register(&packet_family_ops);
4600 if (rc)
4601 goto out_proto;
4602 rc = register_pernet_subsys(&packet_net_ops);
4603 if (rc)
4604 goto out_sock;
4605 rc = register_netdevice_notifier(&packet_netdev_notifier);
4606 if (rc)
4607 goto out_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
YueHaibing36096f22019-05-09 22:52:20 +08004609 return 0;
4610
4611out_pernet:
4612 unregister_pernet_subsys(&packet_net_ops);
4613out_sock:
4614 sock_unregister(PF_PACKET);
4615out_proto:
4616 proto_unregister(&packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617out:
4618 return rc;
4619}
4620
4621module_init(packet_init);
4622module_exit(packet_exit);
4623MODULE_LICENSE("GPL");
4624MODULE_ALIAS_NETPROTO(PF_PACKET);