blob: e5b0986215d25ee7d646c4de027bab4ecf897fcc [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
Eric Dumazet96f657e2019-06-12 09:52:26 -0700387static int __packet_get_status(const struct packet_sock *po, void *frame)
Johann Baudy69e3c752009-05-18 22:11:22 -0700388{
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
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100411static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts,
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000412 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) &&
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100418 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts))
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400419 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000420
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100421 if (ktime_to_timespec64_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;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100431 struct timespec64 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;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100438 /*
439 * versions 1 through 3 overflow the timestamps in y2106, since they
440 * all store the seconds in a 32-bit unsigned integer.
441 * If we create a version 4, that should have a 64-bit timestamp,
442 * either 64-bit seconds + 32-bit nanoseconds, or just 64-bit
443 * nanoseconds.
444 */
Willem de Bruijn2e313962013-04-23 00:39:28 +0000445 switch (po->tp_version) {
446 case TPACKET_V1:
447 h.h1->tp_sec = ts.tv_sec;
448 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
449 break;
450 case TPACKET_V2:
451 h.h2->tp_sec = ts.tv_sec;
452 h.h2->tp_nsec = ts.tv_nsec;
453 break;
454 case TPACKET_V3:
Daniel Borkmann57ea8842017-01-05 02:34:28 +0100455 h.h3->tp_sec = ts.tv_sec;
456 h.h3->tp_nsec = ts.tv_nsec;
457 break;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000458 default:
459 WARN(1, "TPACKET version not supported.\n");
460 BUG();
461 }
462
463 /* one flush is safe, as both fields always lie on the same cacheline */
464 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
465 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000466
467 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000468}
469
Eric Dumazetd4b5bd92019-06-12 09:52:27 -0700470static void *packet_lookup_frame(const struct packet_sock *po,
471 const struct packet_ring_buffer *rb,
472 unsigned int position,
473 int status)
Johann Baudy69e3c752009-05-18 22:11:22 -0700474{
475 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000476 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700477
478 pg_vec_pos = position / rb->frames_per_block;
479 frame_offset = position % rb->frames_per_block;
480
Neil Horman0e3125c2010-11-16 10:26:47 -0800481 h.raw = rb->pg_vec[pg_vec_pos].buffer +
482 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700483
484 if (status != __packet_get_status(po, h.raw))
485 return NULL;
486
487 return h.raw;
488}
489
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000490static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700491 struct packet_ring_buffer *rb,
492 int status)
493{
494 return packet_lookup_frame(po, rb, rb->head, status);
495}
496
chetan lokebc59ba32011-08-25 10:43:30 +0000497static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000498{
499 del_timer_sync(&pkc->retire_blk_timer);
500}
501
502static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +0000503 struct sk_buff_head *rb_queue)
504{
chetan lokebc59ba32011-08-25 10:43:30 +0000505 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000506
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200507 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000508
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100509 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000510 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100511 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000512
513 prb_del_retire_blk_timer(pkc);
514}
515
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530516static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f12011-08-19 10:18:16 +0000517{
chetan lokebc59ba32011-08-25 10:43:30 +0000518 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000519
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530520 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700521 timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired,
522 0);
523 pkc->retire_blk_timer.expires = jiffies;
chetan lokef6fb8f12011-08-19 10:18:16 +0000524}
525
526static int prb_calc_retire_blk_tmo(struct packet_sock *po,
527 int blk_size_in_bytes)
528{
529 struct net_device *dev;
Mao Wenan0914d2b2019-12-23 18:42:57 +0800530 unsigned int mbits, div;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800531 struct ethtool_link_ksettings ecmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000532 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +0000533
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000534 rtnl_lock();
535 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
536 if (unlikely(!dev)) {
537 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000538 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000539 }
David Decotigny7cad1ba2016-02-24 10:58:10 -0800540 err = __ethtool_get_link_ksettings(dev, &ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000541 rtnl_unlock();
Mao Wenan0914d2b2019-12-23 18:42:57 +0800542 if (err)
Mao Wenanb43d1f92019-12-09 21:31:25 +0800543 return DEFAULT_PRB_RETIRE_TOV;
chetan lokef6fb8f12011-08-19 10:18:16 +0000544
Mao Wenan0914d2b2019-12-23 18:42:57 +0800545 /* If the link speed is so slow you don't really
546 * need to worry about perf anyways
547 */
548 if (ecmd.base.speed < SPEED_1000 ||
549 ecmd.base.speed == SPEED_UNKNOWN)
550 return DEFAULT_PRB_RETIRE_TOV;
551
552 div = ecmd.base.speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000553 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
554
555 if (div)
556 mbits /= div;
557
chetan lokef6fb8f12011-08-19 10:18:16 +0000558 if (div)
Mao Wenan0914d2b2019-12-23 18:42:57 +0800559 return mbits + 1;
560 return mbits;
chetan lokef6fb8f12011-08-19 10:18:16 +0000561}
562
chetan lokebc59ba32011-08-25 10:43:30 +0000563static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000564 union tpacket_req_u *req_u)
565{
566 p1->feature_req_word = req_u->req3.tp_feature_req_word;
567}
568
569static void init_prb_bdqc(struct packet_sock *po,
570 struct packet_ring_buffer *rb,
571 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530572 union tpacket_req_u *req_u)
chetan lokef6fb8f12011-08-19 10:18:16 +0000573{
Duan Jiong22781a52013-12-06 13:29:36 +0800574 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000575 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000576
577 memset(p1, 0x0, sizeof(*p1));
578
579 p1->knxt_seq_num = 1;
580 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000581 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000582 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000583 p1->kblk_size = req_u->req3.tp_block_size;
584 p1->knum_blocks = req_u->req3.tp_block_nr;
585 p1->hdrlen = po->tp_hdrlen;
586 p1->version = po->tp_version;
587 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000588 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000589 if (req_u->req3.tp_retire_blk_tov)
590 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
591 else
592 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
593 req_u->req3.tp_block_size);
594 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
595 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
596
Eric Dumazetdc808112014-08-15 09:16:04 -0700597 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000598 prb_init_ft_ops(p1, req_u);
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530599 prb_setup_retire_blk_timer(po);
chetan lokef6fb8f12011-08-19 10:18:16 +0000600 prb_open_block(p1, pbd);
601}
602
603/* Do NOT update the last_blk_num first.
604 * Assumes sk_buff_head lock is held.
605 */
chetan lokebc59ba32011-08-25 10:43:30 +0000606static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000607{
608 mod_timer(&pkc->retire_blk_timer,
609 jiffies + pkc->tov_in_jiffies);
610 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
611}
612
613/*
614 * Timer logic:
615 * 1) We refresh the timer only when we open a block.
616 * By doing this we don't waste cycles refreshing the timer
617 * on packet-by-packet basis.
618 *
619 * With a 1MB block-size, on a 1Gbps line, it will take
620 * i) ~8 ms to fill a block + ii) memcpy etc.
621 * In this cut we are not accounting for the memcpy time.
622 *
623 * So, if the user sets the 'tmo' to 10ms then the timer
624 * will never fire while the block is still getting filled
625 * (which is what we want). However, the user could choose
626 * to close a block early and that's fine.
627 *
628 * But when the timer does fire, we check whether or not to refresh it.
629 * Since the tmo granularity is in msecs, it is not too expensive
630 * to refresh the timer, lets say every '8' msecs.
631 * Either the user can set the 'tmo' or we can derive it based on
632 * a) line-speed and b) block-size.
633 * prb_calc_retire_blk_tmo() calculates the tmo.
634 *
635 */
Kees Cook17bfd8c2017-10-24 01:46:26 -0700636static void prb_retire_rx_blk_timer_expired(struct timer_list *t)
chetan lokef6fb8f12011-08-19 10:18:16 +0000637{
Kees Cook17bfd8c2017-10-24 01:46:26 -0700638 struct packet_sock *po =
639 from_timer(po, t, rx_ring.prb_bdqc.retire_blk_timer);
Duan Jiong22781a52013-12-06 13:29:36 +0800640 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000641 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000642 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000643
644 spin_lock(&po->sk.sk_receive_queue.lock);
645
646 frozen = prb_queue_frozen(pkc);
647 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
648
649 if (unlikely(pkc->delete_blk_timer))
650 goto out;
651
652 /* We only need to plug the race when the block is partially filled.
653 * tpacket_rcv:
654 * lock(); increment BLOCK_NUM_PKTS; unlock()
655 * copy_bits() is in progress ...
656 * timer fires on other cpu:
657 * we can't retire the current block because copy_bits
658 * is in progress.
659 *
660 */
661 if (BLOCK_NUM_PKTS(pbd)) {
662 while (atomic_read(&pkc->blk_fill_in_prog)) {
663 /* Waiting for skb_copy_bits to finish... */
664 cpu_relax();
665 }
666 }
667
668 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
669 if (!frozen) {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300670 if (!BLOCK_NUM_PKTS(pbd)) {
671 /* An empty block. Just refresh the timer. */
672 goto refresh_timer;
673 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000674 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
675 if (!prb_dispatch_next_block(pkc, po))
676 goto refresh_timer;
677 else
678 goto out;
679 } else {
680 /* Case 1. Queue was frozen because user-space was
681 * lagging behind.
682 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300683 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +0000684 /*
685 * Ok, user-space is still behind.
686 * So just refresh the timer.
687 */
688 goto refresh_timer;
689 } else {
690 /* Case 2. queue was frozen,user-space caught up,
691 * now the link went idle && the timer fired.
692 * We don't have a block to close.So we open this
693 * block and restart the timer.
694 * opening a block thaws the queue,restarts timer
695 * Thawing/timer-refresh is a side effect.
696 */
697 prb_open_block(pkc, pbd);
698 goto out;
699 }
700 }
701 }
702
703refresh_timer:
704 _prb_refresh_rx_retire_blk_timer(pkc);
705
706out:
707 spin_unlock(&po->sk.sk_receive_queue.lock);
708}
709
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000710static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000711 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000712{
713 /* Flush everything minus the block header */
714
715#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
716 u8 *start, *end;
717
718 start = (u8 *)pbd1;
719
720 /* Skip the block header(we know header WILL fit in 4K) */
721 start += PAGE_SIZE;
722
723 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
724 for (; start < end; start += PAGE_SIZE)
725 flush_dcache_page(pgv_to_page(start));
726
727 smp_wmb();
728#endif
729
730 /* Now update the block status. */
731
732 BLOCK_STATUS(pbd1) = status;
733
734 /* Flush the block header */
735
736#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
737 start = (u8 *)pbd1;
738 flush_dcache_page(pgv_to_page(start));
739
740 smp_wmb();
741#endif
742}
743
744/*
745 * Side effect:
746 *
747 * 1) flush the block
748 * 2) Increment active_blk_num
749 *
750 * Note:We DONT refresh the timer on purpose.
751 * Because almost always the next block will be opened.
752 */
chetan lokebc59ba32011-08-25 10:43:30 +0000753static void prb_close_block(struct tpacket_kbdq_core *pkc1,
754 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000755 struct packet_sock *po, unsigned int stat)
756{
757 __u32 status = TP_STATUS_USER | stat;
758
759 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000760 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
Dan Collinsda413ee2014-12-19 16:49:25 +1300761 struct sock *sk = &po->sk;
chetan lokef6fb8f12011-08-19 10:18:16 +0000762
Eric Dumazet8e8e2952019-06-12 09:52:30 -0700763 if (atomic_read(&po->tp_drops))
chetan lokef6fb8f12011-08-19 10:18:16 +0000764 status |= TP_STATUS_LOSING;
765
766 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
767 last_pkt->tp_next_offset = 0;
768
769 /* Get the ts of the last pkt */
770 if (BLOCK_NUM_PKTS(pbd1)) {
771 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
772 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
773 } else {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300774 /* Ok, we tmo'd - so get the current time.
775 *
776 * It shouldn't really happen as we don't close empty
777 * blocks. See prb_retire_rx_blk_timer_expired().
778 */
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100779 struct timespec64 ts;
780 ktime_get_real_ts64(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000781 h1->ts_last_pkt.ts_sec = ts.tv_sec;
782 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
783 }
784
785 smp_wmb();
786
787 /* Flush the block */
788 prb_flush_block(pkc1, pbd1, status);
789
Dan Collinsda413ee2014-12-19 16:49:25 +1300790 sk->sk_data_ready(sk);
791
chetan lokef6fb8f12011-08-19 10:18:16 +0000792 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
793}
794
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000795static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000796{
797 pkc->reset_pending_on_curr_blk = 0;
798}
799
800/*
801 * Side effect of opening a block:
802 *
803 * 1) prb_queue is thawed.
804 * 2) retire_blk_timer is refreshed.
805 *
806 */
chetan lokebc59ba32011-08-25 10:43:30 +0000807static void prb_open_block(struct tpacket_kbdq_core *pkc1,
808 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000809{
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100810 struct timespec64 ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000811 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000812
813 smp_rmb();
814
Daniel Borkmann8da30562013-05-03 02:57:00 +0000815 /* We could have just memset this but we will lose the
816 * flexibility of making the priv area sticky
817 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000818
Daniel Borkmann8da30562013-05-03 02:57:00 +0000819 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
820 BLOCK_NUM_PKTS(pbd1) = 0;
821 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000822
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100823 ktime_get_real_ts64(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000824
Daniel Borkmann8da30562013-05-03 02:57:00 +0000825 h1->ts_first_pkt.ts_sec = ts.tv_sec;
826 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f12011-08-19 10:18:16 +0000827
Daniel Borkmann8da30562013-05-03 02:57:00 +0000828 pkc1->pkblk_start = (char *)pbd1;
829 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
830
831 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
832 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
833
834 pbd1->version = pkc1->version;
835 pkc1->prev = pkc1->nxt_offset;
836 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
837
838 prb_thaw_queue(pkc1);
839 _prb_refresh_rx_retire_blk_timer(pkc1);
840
841 smp_wmb();
chetan lokef6fb8f12011-08-19 10:18:16 +0000842}
843
844/*
845 * Queue freeze logic:
846 * 1) Assume tp_block_nr = 8 blocks.
847 * 2) At time 't0', user opens Rx ring.
848 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
849 * 4) user-space is either sleeping or processing block '0'.
850 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
851 * it will close block-7,loop around and try to fill block '0'.
852 * call-flow:
853 * __packet_lookup_frame_in_block
854 * prb_retire_current_block()
855 * prb_dispatch_next_block()
856 * |->(BLOCK_STATUS == USER) evaluates to true
857 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
858 * 6) Now there are two cases:
859 * 6.1) Link goes idle right after the queue is frozen.
860 * But remember, the last open_block() refreshed the timer.
861 * When this timer expires,it will refresh itself so that we can
862 * re-open block-0 in near future.
863 * 6.2) Link is busy and keeps on receiving packets. This is a simple
864 * case and __packet_lookup_frame_in_block will check if block-0
865 * is free and can now be re-used.
866 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000867static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000868 struct packet_sock *po)
869{
870 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000871 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f12011-08-19 10:18:16 +0000872}
873
874#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
875
876/*
877 * If the next block is free then we will dispatch it
878 * and return a good offset.
879 * Else, we will freeze the queue.
880 * So, caller must check the return value.
881 */
chetan lokebc59ba32011-08-25 10:43:30 +0000882static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000883 struct packet_sock *po)
884{
chetan lokebc59ba32011-08-25 10:43:30 +0000885 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000886
887 smp_rmb();
888
889 /* 1. Get current block num */
890 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
891
892 /* 2. If this block is currently in_use then freeze the queue */
893 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
894 prb_freeze_queue(pkc, po);
895 return NULL;
896 }
897
898 /*
899 * 3.
900 * open this block and return the offset where the first packet
901 * needs to get stored.
902 */
903 prb_open_block(pkc, pbd);
904 return (void *)pkc->nxt_offset;
905}
906
chetan lokebc59ba32011-08-25 10:43:30 +0000907static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000908 struct packet_sock *po, unsigned int status)
909{
chetan lokebc59ba32011-08-25 10:43:30 +0000910 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000911
912 /* retire/close the current block */
913 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
914 /*
915 * Plug the case where copy_bits() is in progress on
916 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
917 * have space to copy the pkt in the current block and
918 * called prb_retire_current_block()
919 *
920 * We don't need to worry about the TMO case because
921 * the timer-handler already handled this case.
922 */
923 if (!(status & TP_STATUS_BLK_TMO)) {
924 while (atomic_read(&pkc->blk_fill_in_prog)) {
925 /* Waiting for skb_copy_bits to finish... */
926 cpu_relax();
927 }
928 }
929 prb_close_block(pkc, pbd, po, status);
930 return;
931 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000932}
933
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300934static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000935{
936 return TP_STATUS_USER & BLOCK_STATUS(pbd);
937}
938
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000939static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000940{
941 return pkc->reset_pending_on_curr_blk;
942}
943
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000944static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +0000945{
chetan lokebc59ba32011-08-25 10:43:30 +0000946 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000947 atomic_dec(&pkc->blk_fill_in_prog);
948}
949
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000950static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000951 struct tpacket3_hdr *ppd)
952{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800953 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000954}
955
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000956static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000957 struct tpacket3_hdr *ppd)
958{
959 ppd->hv1.tp_rxhash = 0;
960}
961
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000962static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000963 struct tpacket3_hdr *ppd)
964{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100965 if (skb_vlan_tag_present(pkc->skb)) {
966 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900967 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
968 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
chetan lokef6fb8f12011-08-19 10:18:16 +0000969 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000970 ppd->hv1.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900971 ppd->hv1.tp_vlan_tpid = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000972 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +0000973 }
974}
975
chetan lokebc59ba32011-08-25 10:43:30 +0000976static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000977 struct tpacket3_hdr *ppd)
978{
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900979 ppd->hv1.tp_padding = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000980 prb_fill_vlan_info(pkc, ppd);
981
982 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
983 prb_fill_rxhash(pkc, ppd);
984 else
985 prb_clear_rxhash(pkc, ppd);
986}
987
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000988static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000989 struct tpacket_kbdq_core *pkc,
990 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +0000991 unsigned int len)
992{
993 struct tpacket3_hdr *ppd;
994
995 ppd = (struct tpacket3_hdr *)curr;
996 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
997 pkc->prev = curr;
998 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
999 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1000 BLOCK_NUM_PKTS(pbd) += 1;
1001 atomic_inc(&pkc->blk_fill_in_prog);
1002 prb_run_all_ft_ops(pkc, ppd);
1003}
1004
1005/* Assumes caller has the sk->rx_queue.lock */
1006static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1007 struct sk_buff *skb,
chetan lokef6fb8f12011-08-19 10:18:16 +00001008 unsigned int len
1009 )
1010{
chetan lokebc59ba32011-08-25 10:43:30 +00001011 struct tpacket_kbdq_core *pkc;
1012 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +00001013 char *curr, *end;
1014
Joe Perchese3192692012-06-03 17:41:40 +00001015 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +00001016 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1017
1018 /* Queue is frozen when user space is lagging behind */
1019 if (prb_queue_frozen(pkc)) {
1020 /*
1021 * Check if that last block which caused the queue to freeze,
1022 * is still in_use by user-space.
1023 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +03001024 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001025 /* Can't record this packet */
1026 return NULL;
1027 } else {
1028 /*
1029 * Ok, the block was released by user-space.
1030 * Now let's open that block.
1031 * opening a block also thaws the queue.
1032 * Thawing is a side effect.
1033 */
1034 prb_open_block(pkc, pbd);
1035 }
1036 }
1037
1038 smp_mb();
1039 curr = pkc->nxt_offset;
1040 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +00001041 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +00001042
1043 /* first try the current block */
1044 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1045 prb_fill_curr_block(curr, pkc, pbd, len);
1046 return (void *)curr;
1047 }
1048
1049 /* Ok, close the current block */
1050 prb_retire_current_block(pkc, po, 0);
1051
1052 /* Now, try to dispatch the next block */
1053 curr = (char *)prb_dispatch_next_block(pkc, po);
1054 if (curr) {
1055 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1056 prb_fill_curr_block(curr, pkc, pbd, len);
1057 return (void *)curr;
1058 }
1059
1060 /*
1061 * No free blocks are available.user_space hasn't caught up yet.
1062 * Queue was just frozen and now this packet will get dropped.
1063 */
1064 return NULL;
1065}
1066
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001067static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001068 struct sk_buff *skb,
1069 int status, unsigned int len)
1070{
1071 char *curr = NULL;
1072 switch (po->tp_version) {
1073 case TPACKET_V1:
1074 case TPACKET_V2:
1075 curr = packet_lookup_frame(po, &po->rx_ring,
1076 po->rx_ring.head, status);
1077 return curr;
1078 case TPACKET_V3:
Mao Wenan46088052019-06-11 09:32:13 +08001079 return __packet_lookup_frame_in_block(po, skb, len);
chetan lokef6fb8f12011-08-19 10:18:16 +00001080 default:
1081 WARN(1, "TPACKET version not supported\n");
1082 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001083 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +00001084 }
1085}
1086
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001087static void *prb_lookup_block(const struct packet_sock *po,
1088 const struct packet_ring_buffer *rb,
1089 unsigned int idx,
1090 int status)
chetan lokef6fb8f12011-08-19 10:18:16 +00001091{
chetan lokebc59ba32011-08-25 10:43:30 +00001092 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001093 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001094
1095 if (status != BLOCK_STATUS(pbd))
1096 return NULL;
1097 return pbd;
1098}
1099
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001100static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001101{
1102 unsigned int prev;
1103 if (rb->prb_bdqc.kactive_blk_num)
1104 prev = rb->prb_bdqc.kactive_blk_num-1;
1105 else
1106 prev = rb->prb_bdqc.knum_blocks-1;
1107 return prev;
1108}
1109
1110/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001111static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001112 struct packet_ring_buffer *rb,
1113 int status)
1114{
1115 unsigned int previous = prb_previous_blk_num(rb);
1116 return prb_lookup_block(po, rb, previous, status);
1117}
1118
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001119static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001120 struct packet_ring_buffer *rb,
1121 int status)
1122{
1123 if (po->tp_version <= TPACKET_V2)
1124 return packet_previous_frame(po, rb, status);
1125
1126 return __prb_previous_block(po, rb, status);
1127}
1128
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001129static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001130 struct packet_ring_buffer *rb)
1131{
1132 switch (po->tp_version) {
1133 case TPACKET_V1:
1134 case TPACKET_V2:
1135 return packet_increment_head(rb);
1136 case TPACKET_V3:
1137 default:
1138 WARN(1, "TPACKET version not supported.\n");
1139 BUG();
1140 return;
1141 }
1142}
1143
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001144static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001145 struct packet_ring_buffer *rb,
1146 int status)
1147{
1148 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1149 return packet_lookup_frame(po, rb, previous, status);
1150}
1151
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001152static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001153{
1154 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1155}
1156
Daniel Borkmannb0138402014-01-15 16:25:36 +01001157static void packet_inc_pending(struct packet_ring_buffer *rb)
1158{
1159 this_cpu_inc(*rb->pending_refcnt);
1160}
1161
1162static void packet_dec_pending(struct packet_ring_buffer *rb)
1163{
1164 this_cpu_dec(*rb->pending_refcnt);
1165}
1166
1167static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1168{
1169 unsigned int refcnt = 0;
1170 int cpu;
1171
1172 /* We don't use pending refcount in rx_ring. */
1173 if (rb->pending_refcnt == NULL)
1174 return 0;
1175
1176 for_each_possible_cpu(cpu)
1177 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1178
1179 return refcnt;
1180}
1181
1182static int packet_alloc_pending(struct packet_sock *po)
1183{
1184 po->rx_ring.pending_refcnt = NULL;
1185
1186 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1187 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1188 return -ENOBUFS;
1189
1190 return 0;
1191}
1192
1193static void packet_free_pending(struct packet_sock *po)
1194{
1195 free_percpu(po->tx_ring.pending_refcnt);
1196}
1197
Willem de Bruijn99547292015-05-12 11:56:47 -04001198#define ROOM_POW_OFF 2
1199#define ROOM_NONE 0x0
1200#define ROOM_LOW 0x1
1201#define ROOM_NORMAL 0x2
1202
Eric Dumazetd4b5bd92019-06-12 09:52:27 -07001203static bool __tpacket_has_room(const struct packet_sock *po, int pow_off)
Willem de Bruijn99547292015-05-12 11:56:47 -04001204{
1205 int idx, len;
1206
Eric Dumazetd4b5bd92019-06-12 09:52:27 -07001207 len = READ_ONCE(po->rx_ring.frame_max) + 1;
1208 idx = READ_ONCE(po->rx_ring.head);
Willem de Bruijn99547292015-05-12 11:56:47 -04001209 if (pow_off)
1210 idx += len >> pow_off;
1211 if (idx >= len)
1212 idx -= len;
1213 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1214}
1215
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001216static bool __tpacket_v3_has_room(const struct packet_sock *po, int pow_off)
Willem de Bruijn99547292015-05-12 11:56:47 -04001217{
1218 int idx, len;
1219
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001220 len = READ_ONCE(po->rx_ring.prb_bdqc.knum_blocks);
1221 idx = READ_ONCE(po->rx_ring.prb_bdqc.kactive_blk_num);
Willem de Bruijn99547292015-05-12 11:56:47 -04001222 if (pow_off)
1223 idx += len >> pow_off;
1224 if (idx >= len)
1225 idx -= len;
1226 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1227}
1228
Eric Dumazet0338a142019-06-12 09:52:29 -07001229static int __packet_rcv_has_room(const struct packet_sock *po,
1230 const struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001231{
Eric Dumazet0338a142019-06-12 09:52:29 -07001232 const struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001233 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001234
Willem de Bruijn99547292015-05-12 11:56:47 -04001235 if (po->prot_hook.func != tpacket_rcv) {
Eric Dumazet0338a142019-06-12 09:52:29 -07001236 int rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1237 int avail = rcvbuf - atomic_read(&sk->sk_rmem_alloc)
1238 - (skb ? skb->truesize : 0);
1239
1240 if (avail > (rcvbuf >> ROOM_POW_OFF))
Willem de Bruijn99547292015-05-12 11:56:47 -04001241 return ROOM_NORMAL;
1242 else if (avail > 0)
1243 return ROOM_LOW;
1244 else
1245 return ROOM_NONE;
1246 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001247
Willem de Bruijn99547292015-05-12 11:56:47 -04001248 if (po->tp_version == TPACKET_V3) {
1249 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1250 ret = ROOM_NORMAL;
1251 else if (__tpacket_v3_has_room(po, 0))
1252 ret = ROOM_LOW;
1253 } else {
1254 if (__tpacket_has_room(po, ROOM_POW_OFF))
1255 ret = ROOM_NORMAL;
1256 else if (__tpacket_has_room(po, 0))
1257 ret = ROOM_LOW;
1258 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001259
1260 return ret;
1261}
1262
1263static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1264{
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001265 int pressure, ret;
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001266
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001267 ret = __packet_rcv_has_room(po, skb);
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001268 pressure = ret != ROOM_NORMAL;
1269
1270 if (READ_ONCE(po->pressure) != pressure)
1271 WRITE_ONCE(po->pressure, pressure);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001272
Willem de Bruijn99547292015-05-12 11:56:47 -04001273 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001274}
1275
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07001276static void packet_rcv_try_clear_pressure(struct packet_sock *po)
1277{
1278 if (READ_ONCE(po->pressure) &&
1279 __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
1280 WRITE_ONCE(po->pressure, 0);
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static void packet_sock_destruct(struct sock *sk)
1284{
Richard Cochraned85b562010-04-07 22:41:28 +00001285 skb_queue_purge(&sk->sk_error_queue);
1286
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001287 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +03001288 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001291 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return;
1293 }
1294
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001295 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001298static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1299{
Eric Dumazetb756ad92019-11-08 05:07:46 -08001300 u32 *history = po->rollover->history;
1301 u32 victim, rxhash;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001302 int i, count = 0;
1303
1304 rxhash = skb_get_hash(skb);
1305 for (i = 0; i < ROLLOVER_HLEN; i++)
Eric Dumazetb756ad92019-11-08 05:07:46 -08001306 if (READ_ONCE(history[i]) == rxhash)
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001307 count++;
1308
Eric Dumazetb756ad92019-11-08 05:07:46 -08001309 victim = prandom_u32() % ROLLOVER_HLEN;
1310
1311 /* Avoid dirtying the cache line if possible */
1312 if (READ_ONCE(history[victim]) != rxhash)
1313 WRITE_ONCE(history[victim], rxhash);
1314
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001315 return count > (ROLLOVER_HLEN >> 1);
1316}
1317
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001318static unsigned int fanout_demux_hash(struct packet_fanout *f,
1319 struct sk_buff *skb,
1320 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001321{
David S. Millereb70db82016-07-01 16:07:50 -04001322 return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001323}
1324
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001325static unsigned int fanout_demux_lb(struct packet_fanout *f,
1326 struct sk_buff *skb,
1327 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001328{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001329 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001330
Willem de Bruijn468479e2015-06-17 15:59:34 -04001331 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001332}
1333
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001334static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1335 struct sk_buff *skb,
1336 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001337{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001338 return smp_processor_id() % num;
1339}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001340
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001341static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1342 struct sk_buff *skb,
1343 unsigned int num)
1344{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001345 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001346}
1347
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001348static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1349 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001350 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001351 unsigned int num)
1352{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001353 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001354 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001355
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001356 po = pkt_sk(f->arr[idx]);
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001357
1358 if (try_self) {
1359 room = packet_rcv_has_room(po, skb);
1360 if (room == ROOM_NORMAL ||
1361 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1362 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001363 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001364 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001365
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001366 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001367 do {
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001368 po_next = pkt_sk(f->arr[i]);
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001369 if (po_next != po_skip && !READ_ONCE(po_next->pressure) &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001370 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001371 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001372 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001373 atomic_long_inc(&po->rollover->num);
1374 if (room == ROOM_LOW)
1375 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001376 return i;
1377 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001378
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001379 if (++i == num)
1380 i = 0;
1381 } while (i != j);
1382
Willem de Bruijna9b63912015-05-12 11:56:50 -04001383 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001384 return idx;
1385}
1386
Neil Horman2d360972014-01-22 16:01:44 -05001387static unsigned int fanout_demux_qm(struct packet_fanout *f,
1388 struct sk_buff *skb,
1389 unsigned int num)
1390{
1391 return skb_get_queue_mapping(skb) % num;
1392}
1393
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001394static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1395 struct sk_buff *skb,
1396 unsigned int num)
1397{
1398 struct bpf_prog *prog;
1399 unsigned int ret = 0;
1400
1401 rcu_read_lock();
1402 prog = rcu_dereference(f->bpf_prog);
1403 if (prog)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001404 ret = bpf_prog_run_clear_cb(prog, skb) % num;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001405 rcu_read_unlock();
1406
1407 return ret;
1408}
1409
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001410static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1411{
1412 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001413}
1414
David S. Miller95ec3eb2011-07-06 01:56:38 -07001415static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1416 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001417{
1418 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001419 unsigned int num = READ_ONCE(f->num_members);
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001420 struct net *net = read_pnet(&f->net);
David S. Millerdc99f602011-07-05 01:45:05 -07001421 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001422 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001423
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001424 if (!net_eq(dev_net(dev), net) || !num) {
David S. Millerdc99f602011-07-05 01:45:05 -07001425 kfree_skb(skb);
1426 return 0;
1427 }
1428
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001429 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001430 skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001431 if (!skb)
1432 return 0;
1433 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001434 switch (f->type) {
1435 case PACKET_FANOUT_HASH:
1436 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001437 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001438 break;
1439 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001440 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001441 break;
1442 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001443 idx = fanout_demux_cpu(f, skb, num);
1444 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001445 case PACKET_FANOUT_RND:
1446 idx = fanout_demux_rnd(f, skb, num);
1447 break;
Neil Horman2d360972014-01-22 16:01:44 -05001448 case PACKET_FANOUT_QM:
1449 idx = fanout_demux_qm(f, skb, num);
1450 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001451 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001452 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001453 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001454 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001455 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001456 idx = fanout_demux_bpf(f, skb, num);
1457 break;
David S. Miller7736d332011-07-05 01:43:20 -07001458 }
1459
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001460 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1461 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001462
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001463 po = pkt_sk(f->arr[idx]);
David S. Millerdc99f602011-07-05 01:45:05 -07001464 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1465}
1466
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001467DEFINE_MUTEX(fanout_mutex);
1468EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001469static LIST_HEAD(fanout_list);
Mike Maloney4a69a862017-04-21 10:56:11 -04001470static u16 fanout_next_id;
David S. Millerdc99f602011-07-05 01:45:05 -07001471
1472static void __fanout_link(struct sock *sk, struct packet_sock *po)
1473{
1474 struct packet_fanout *f = po->fanout;
1475
1476 spin_lock(&f->lock);
1477 f->arr[f->num_members] = sk;
1478 smp_wmb();
1479 f->num_members++;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001480 if (f->num_members == 1)
1481 dev_add_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001482 spin_unlock(&f->lock);
1483}
1484
1485static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1486{
1487 struct packet_fanout *f = po->fanout;
1488 int i;
1489
1490 spin_lock(&f->lock);
1491 for (i = 0; i < f->num_members; i++) {
1492 if (f->arr[i] == sk)
1493 break;
1494 }
1495 BUG_ON(i >= f->num_members);
1496 f->arr[i] = f->arr[f->num_members - 1];
1497 f->num_members--;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001498 if (f->num_members == 0)
1499 __dev_remove_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001500 spin_unlock(&f->lock);
1501}
1502
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001503static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001504{
Eric Dumazet161642e2015-10-09 11:29:32 -07001505 if (sk->sk_family != PF_PACKET)
1506 return false;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001507
Eric Dumazet161642e2015-10-09 11:29:32 -07001508 return ptype->af_packet_priv == pkt_sk(sk)->fanout;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001509}
1510
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001511static void fanout_init_data(struct packet_fanout *f)
1512{
1513 switch (f->type) {
1514 case PACKET_FANOUT_LB:
1515 atomic_set(&f->rr_cur, 0);
1516 break;
1517 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001518 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001519 RCU_INIT_POINTER(f->bpf_prog, NULL);
1520 break;
1521 }
1522}
1523
1524static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1525{
1526 struct bpf_prog *old;
1527
1528 spin_lock(&f->lock);
1529 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1530 rcu_assign_pointer(f->bpf_prog, new);
1531 spin_unlock(&f->lock);
1532
1533 if (old) {
1534 synchronize_net();
1535 bpf_prog_destroy(old);
1536 }
1537}
1538
1539static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1540 unsigned int len)
1541{
1542 struct bpf_prog *new;
1543 struct sock_fprog fprog;
1544 int ret;
1545
1546 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1547 return -EPERM;
1548 if (len != sizeof(fprog))
1549 return -EINVAL;
1550 if (copy_from_user(&fprog, data, len))
1551 return -EFAULT;
1552
Daniel Borkmannbab18992015-10-02 15:17:33 +02001553 ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001554 if (ret)
1555 return ret;
1556
1557 __fanout_set_data_bpf(po->fanout, new);
1558 return 0;
1559}
1560
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001561static int fanout_set_data_ebpf(struct packet_sock *po, char __user *data,
1562 unsigned int len)
1563{
1564 struct bpf_prog *new;
1565 u32 fd;
1566
1567 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1568 return -EPERM;
1569 if (len != sizeof(fd))
1570 return -EINVAL;
1571 if (copy_from_user(&fd, data, len))
1572 return -EFAULT;
1573
Daniel Borkmann113214b2016-06-30 17:24:44 +02001574 new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001575 if (IS_ERR(new))
1576 return PTR_ERR(new);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001577
1578 __fanout_set_data_bpf(po->fanout, new);
1579 return 0;
1580}
1581
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001582static int fanout_set_data(struct packet_sock *po, char __user *data,
1583 unsigned int len)
1584{
1585 switch (po->fanout->type) {
1586 case PACKET_FANOUT_CBPF:
1587 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001588 case PACKET_FANOUT_EBPF:
1589 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001590 default:
1591 return -EINVAL;
zhong jiang07d53ae2018-08-04 19:41:41 +08001592 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001593}
1594
1595static void fanout_release_data(struct packet_fanout *f)
1596{
1597 switch (f->type) {
1598 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001599 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001600 __fanout_set_data_bpf(f, NULL);
zhong jiang07d53ae2018-08-04 19:41:41 +08001601 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001602}
1603
Mike Maloney4a69a862017-04-21 10:56:11 -04001604static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
1605{
1606 struct packet_fanout *f;
1607
1608 list_for_each_entry(f, &fanout_list, list) {
1609 if (f->id == candidate_id &&
1610 read_pnet(&f->net) == sock_net(sk)) {
1611 return false;
1612 }
1613 }
1614 return true;
1615}
1616
1617static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
1618{
1619 u16 id = fanout_next_id;
1620
1621 do {
1622 if (__fanout_id_is_free(sk, id)) {
1623 *new_id = id;
1624 fanout_next_id = id + 1;
1625 return true;
1626 }
1627
1628 id++;
1629 } while (id != fanout_next_id);
1630
1631 return false;
1632}
1633
David S. Miller7736d332011-07-05 01:43:20 -07001634static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001635{
Eric Dumazetd199fab2017-02-14 09:03:51 -08001636 struct packet_rollover *rollover = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001637 struct packet_sock *po = pkt_sk(sk);
1638 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001639 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001640 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001641 int err;
1642
1643 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001644 case PACKET_FANOUT_ROLLOVER:
1645 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1646 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001647 case PACKET_FANOUT_HASH:
1648 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001649 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001650 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001651 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001652 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001653 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001654 break;
1655 default:
1656 return -EINVAL;
1657 }
1658
Eric Dumazetd199fab2017-02-14 09:03:51 -08001659 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001660
Eric Dumazetd199fab2017-02-14 09:03:51 -08001661 err = -EALREADY;
David S. Millerdc99f602011-07-05 01:45:05 -07001662 if (po->fanout)
Eric Dumazetd199fab2017-02-14 09:03:51 -08001663 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001664
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001665 if (type == PACKET_FANOUT_ROLLOVER ||
1666 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Eric Dumazetd199fab2017-02-14 09:03:51 -08001667 err = -ENOMEM;
1668 rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
1669 if (!rollover)
1670 goto out;
1671 atomic_long_set(&rollover->num, 0);
1672 atomic_long_set(&rollover->num_huge, 0);
1673 atomic_long_set(&rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001674 }
1675
Mike Maloney4a69a862017-04-21 10:56:11 -04001676 if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
1677 if (id != 0) {
1678 err = -EINVAL;
1679 goto out;
1680 }
1681 if (!fanout_find_new_id(sk, &id)) {
1682 err = -ENOMEM;
1683 goto out;
1684 }
1685 /* ephemeral flag for the first socket in the group: drop it */
1686 flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
1687 }
1688
David S. Millerdc99f602011-07-05 01:45:05 -07001689 match = NULL;
1690 list_for_each_entry(f, &fanout_list, list) {
1691 if (f->id == id &&
1692 read_pnet(&f->net) == sock_net(sk)) {
1693 match = f;
1694 break;
1695 }
1696 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001697 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001698 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001699 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001700 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001701 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001702 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001703 if (!match)
1704 goto out;
1705 write_pnet(&match->net, sock_net(sk));
1706 match->id = id;
1707 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001708 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001709 INIT_LIST_HEAD(&match->list);
1710 spin_lock_init(&match->lock);
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001711 refcount_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001712 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001713 match->prot_hook.type = po->prot_hook.type;
1714 match->prot_hook.dev = po->prot_hook.dev;
1715 match->prot_hook.func = packet_rcv_fanout;
1716 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001717 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001718 list_add(&match->list, &fanout_list);
1719 }
1720 err = -EINVAL;
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001721
1722 spin_lock(&po->bind_lock);
1723 if (po->running &&
1724 match->type == type &&
Eric Dumazetafe62c62011-07-07 06:41:29 -07001725 match->prot_hook.type == po->prot_hook.type &&
1726 match->prot_hook.dev == po->prot_hook.dev) {
1727 err = -ENOSPC;
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001728 if (refcount_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001729 __dev_remove_pack(&po->prot_hook);
1730 po->fanout = match;
Mike Maloney57f015f2017-11-28 10:44:29 -05001731 po->rollover = rollover;
1732 rollover = NULL;
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001733 refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001734 __fanout_link(sk, po);
1735 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001736 }
1737 }
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001738 spin_unlock(&po->bind_lock);
1739
1740 if (err && !refcount_read(&match->sk_ref)) {
1741 list_del(&match->list);
1742 kfree(match);
1743 }
1744
Eric Dumazetafe62c62011-07-07 06:41:29 -07001745out:
Mike Maloney57f015f2017-11-28 10:44:29 -05001746 kfree(rollover);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001747 mutex_unlock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001748 return err;
1749}
1750
Anoob Soman2bd624b2017-02-15 20:25:39 +00001751/* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
1752 * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
1753 * It is the responsibility of the caller to call fanout_release_data() and
1754 * free the returned packet_fanout (after synchronize_net())
1755 */
1756static struct packet_fanout *fanout_release(struct sock *sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001757{
1758 struct packet_sock *po = pkt_sk(sk);
1759 struct packet_fanout *f;
1760
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001761 mutex_lock(&fanout_mutex);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001762 f = po->fanout;
1763 if (f) {
1764 po->fanout = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001765
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001766 if (refcount_dec_and_test(&f->sk_ref))
Eric Dumazetd199fab2017-02-14 09:03:51 -08001767 list_del(&f->list);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001768 else
1769 f = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001770 }
1771 mutex_unlock(&fanout_mutex);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001772
1773 return f;
David S. Millerdc99f602011-07-05 01:45:05 -07001774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001776static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
1777 struct sk_buff *skb)
1778{
1779 /* Earlier code assumed this would be a VLAN pkt, double-check
1780 * this now that we have the actual packet in hand. We can only
1781 * do this check on Ethernet devices.
1782 */
1783 if (unlikely(dev->type != ARPHRD_ETHER))
1784 return false;
1785
1786 skb_reset_mac_header(skb);
1787 return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
1788}
1789
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001790static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001792static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001794static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1795 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 struct sock *sk;
1798 struct sockaddr_pkt *spkt;
1799
1800 /*
1801 * When we registered the protocol we saved the socket in the data
1802 * field for just this event.
1803 */
1804
1805 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * Yank back the headers [hope the device set this
1809 * right or kerboom...]
1810 *
1811 * Incoming packets have ll header pulled,
1812 * push it back.
1813 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001814 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * so that this procedure is noop.
1816 */
1817
1818 if (skb->pkt_type == PACKET_LOOPBACK)
1819 goto out;
1820
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001821 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001822 goto out;
1823
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001824 skb = skb_share_check(skb, GFP_ATOMIC);
1825 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto oom;
1827
1828 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001829 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Phil Oester84531c22005-07-12 11:57:52 -07001831 /* drop conntrack reference */
Florian Westphal895b5c92019-09-29 20:54:03 +02001832 nf_reset_ct(skb);
Phil Oester84531c22005-07-12 11:57:52 -07001833
Herbert Xuffbc6112007-02-04 23:33:10 -08001834 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001836 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /*
1839 * The SOCK_PACKET socket receives _all_ frames.
1840 */
1841
1842 spkt->spkt_family = dev->type;
1843 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1844 spkt->spkt_protocol = skb->protocol;
1845
1846 /*
1847 * Charge the memory to the socket. This is done specifically
1848 * to prevent sockets using all the memory up.
1849 */
1850
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001851 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853
1854out:
1855 kfree_skb(skb);
1856oom:
1857 return 0;
1858}
1859
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001860static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
1861{
Yoshiki Komachi18bed892019-03-18 14:39:52 +09001862 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
1863 sock->type == SOCK_RAW) {
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001864 skb_reset_mac_header(skb);
1865 skb->protocol = dev_parse_header_protocol(skb);
1866 }
1867
1868 skb_probe_transport_header(skb);
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/*
1872 * Output a raw packet to a device layer. This bypasses all the other
1873 * protocol layers and you must therefore supply it with a complete frame
1874 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001875
Ying Xue1b784142015-03-02 15:37:48 +08001876static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1877 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001880 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001881 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 struct net_device *dev;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001883 struct sockcm_cookie sockc;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001884 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001886 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001889 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
1891
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001892 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001894 return -EINVAL;
1895 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1896 proto = saddr->spkt_protocol;
1897 } else
1898 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001901 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
1903
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001904 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001905retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001906 rcu_read_lock();
1907 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 err = -ENODEV;
1909 if (dev == NULL)
1910 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001911
David S. Millerd5e76b02007-01-25 19:30:36 -08001912 err = -ENETDOWN;
1913 if (!(dev->flags & IFF_UP))
1914 goto out_unlock;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001917 * You may not queue a frame bigger than the mtu. This is the lowest level
1918 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001920
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001921 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1922 if (!netif_supports_nofcs(dev)) {
1923 err = -EPROTONOSUPPORT;
1924 goto out_unlock;
1925 }
1926 extra_len = 4; /* We're doing our own CRC */
1927 }
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001930 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 goto out_unlock;
1932
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001933 if (!skb) {
1934 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001935 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001936 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001938 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001939 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001940 if (skb == NULL)
1941 return -ENOBUFS;
1942 /* FIXME: Save some space for broken drivers that write a hard
1943 * header at transmission time by themselves. PPP is the notable
1944 * one here. This should really be fixed at the driver level.
1945 */
1946 skb_reserve(skb, reserved);
1947 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001948
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001949 /* Try to align data part correctly */
1950 if (hhlen) {
1951 skb->data -= hhlen;
1952 skb->tail -= hhlen;
1953 if (len < hhlen)
1954 skb_reset_network_header(skb);
1955 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001956 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001957 if (err)
1958 goto out_free;
1959 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05001962 if (!dev_validate_header(dev, skb->data, len)) {
1963 err = -EINVAL;
1964 goto out_unlock;
1965 }
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001966 if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
1967 !packet_extra_vlan_len_allowed(dev, skb)) {
1968 err = -EMSGSIZE;
1969 goto out_unlock;
Ben Greear57f89bf2011-02-11 09:35:18 +00001970 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001971
Willem de Bruijn657a0662018-07-06 10:12:56 -04001972 sockcm_init(&sockc, sk);
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001973 if (msg->msg_controllen) {
1974 err = sock_cmsg_send(sk, msg, &sockc);
Soheil Hassas Yeganehf8e77182016-07-20 18:01:18 -04001975 if (unlikely(err))
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001976 goto out_unlock;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001977 }
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 skb->protocol = proto;
1980 skb->dev = dev;
1981 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001982 skb->mark = sk->sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07001983 skb->tstamp = sockc.transmit_time;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00001984
Willem de Bruijn8f932f72018-12-17 12:24:00 -05001985 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001987 if (unlikely(extra_len == 4))
1988 skb->no_fcs = 1;
1989
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001990 packet_parse_headers(skb, sock);
Jason Wangc1aad272013-03-25 20:19:57 +00001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001993 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001994 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001997 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001998out_free:
1999 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return err;
2001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002003static unsigned int run_filter(struct sk_buff *skb,
2004 const struct sock *sk,
2005 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 struct sk_filter *filter;
2008
Eric Dumazet80f8f102011-01-18 07:46:52 +00002009 rcu_read_lock();
2010 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08002011 if (filter != NULL)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002012 res = bpf_prog_run_clear_cb(filter->prog, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00002013 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
David S. Millerdbcb5852007-01-24 15:21:02 -08002015 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002018static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
2019 size_t *len)
2020{
2021 struct virtio_net_hdr vnet_hdr;
2022
2023 if (*len < sizeof(vnet_hdr))
2024 return -EINVAL;
2025 *len -= sizeof(vnet_hdr);
2026
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002027 if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002028 return -EINVAL;
2029
2030 return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
2031}
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00002034 * This function makes lazy skb cloning in hope that most of packets
2035 * are discarded by BPF.
2036 *
2037 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
2038 * and skb->cb are mangled. It works because (and until) packets
2039 * falling here are owned by current CPU. Output packets are cloned
2040 * by dev_queue_xmit_nit(), input packets are processed by net_bh
2041 * sequencially, so that if we return skb to original state on exit,
2042 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
2044
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002045static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
2046 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct sock *sk;
2049 struct sockaddr_ll *sll;
2050 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002051 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002053 unsigned int snaplen, res;
Weongyo Jeongda378452016-04-14 14:10:04 -07002054 bool is_drop_n_account = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 if (skb->pkt_type == PACKET_LOOPBACK)
2057 goto drop;
2058
2059 sk = pt->af_packet_priv;
2060 po = pkt_sk(sk);
2061
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002062 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002063 goto drop;
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 skb->dev = dev;
2066
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002067 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002069 * exported to higher levels.
2070 *
2071 * Otherwise, the device hides details of its frame
2072 * structure, so that corresponding packet head is
2073 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
2075 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002076 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 else if (skb->pkt_type == PACKET_OUTGOING) {
2078 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002079 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 }
2082
2083 snaplen = skb->len;
2084
David S. Millerdbcb5852007-01-24 15:21:02 -08002085 res = run_filter(skb, sk, snaplen);
2086 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002087 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08002088 if (snaplen > res)
2089 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002091 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto drop_n_acct;
2093
2094 if (skb_shared(skb)) {
2095 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2096 if (nskb == NULL)
2097 goto drop_n_acct;
2098
2099 if (skb_head != skb->data) {
2100 skb->data = skb_head;
2101 skb->len = skb_len;
2102 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00002103 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 skb = nskb;
2105 }
2106
Eyal Birgerb4772ef2015-03-01 14:58:29 +02002107 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08002108
2109 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002112 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002113 sll->sll_ifindex = orig_dev->ifindex;
2114 else
2115 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002117 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Eyal Birger2472d762015-03-01 14:58:28 +02002119 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2120 * Use their space for storing the original skb length.
2121 */
2122 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (pskb_trim(skb, snaplen))
2125 goto drop_n_acct;
2126
2127 skb_set_owner_r(skb, sk);
2128 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002129 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Phil Oester84531c22005-07-12 11:57:52 -07002131 /* drop conntrack reference */
Florian Westphal895b5c92019-09-29 20:54:03 +02002132 nf_reset_ct(skb);
Phil Oester84531c22005-07-12 11:57:52 -07002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002135 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002136 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 __skb_queue_tail(&sk->sk_receive_queue, skb);
2138 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002139 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return 0;
2141
2142drop_n_acct:
Weongyo Jeongda378452016-04-14 14:10:04 -07002143 is_drop_n_account = true;
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002144 atomic_inc(&po->tp_drops);
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002145 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147drop_n_restore:
2148 if (skb_head != skb->data && skb_shared(skb)) {
2149 skb->data = skb_head;
2150 skb->len = skb_len;
2151 }
2152drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002153 if (!is_drop_n_account)
2154 consume_skb(skb);
2155 else
2156 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return 0;
2158}
2159
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002160static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2161 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 struct sock *sk;
2164 struct packet_sock *po;
2165 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002166 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002167 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002169 unsigned int snaplen, res;
chetan lokef6fb8f12011-08-19 10:18:16 +00002170 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002171 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 struct sk_buff *copy_skb = NULL;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +01002173 struct timespec64 ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002174 __u32 ts_status;
Weongyo Jeongda378452016-04-14 14:10:04 -07002175 bool is_drop_n_account = false;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002176 bool do_vnet = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Atzm Watanabe51846352013-12-17 22:53:32 +09002178 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2179 * We may add members to them until current aligned size without forcing
2180 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2181 */
2182 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2183 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (skb->pkt_type == PACKET_LOOPBACK)
2186 goto drop;
2187
2188 sk = pt->af_packet_priv;
2189 po = pkt_sk(sk);
2190
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002191 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002192 goto drop;
2193
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002194 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002196 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 else if (skb->pkt_type == PACKET_OUTGOING) {
2198 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002199 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 }
2202
2203 snaplen = skb->len;
2204
David S. Millerdbcb5852007-01-24 15:21:02 -08002205 res = run_filter(skb, sk, snaplen);
2206 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002207 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002208
Eric Dumazet2c51c622019-06-12 09:52:31 -07002209 /* If we are flooded, just give up */
2210 if (__packet_rcv_has_room(po, skb) == ROOM_NONE) {
2211 atomic_inc(&po->tp_drops);
2212 goto drop_n_restore;
2213 }
2214
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002215 if (skb->ip_summed == CHECKSUM_PARTIAL)
2216 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002217 else if (skb->pkt_type != PACKET_OUTGOING &&
2218 (skb->ip_summed == CHECKSUM_COMPLETE ||
2219 skb_csum_unnecessary(skb)))
2220 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002221
David S. Millerdbcb5852007-01-24 15:21:02 -08002222 if (snaplen > res)
2223 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002226 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2227 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002229 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002230 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002231 (maclen < 16 ? 16 : maclen)) +
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002232 po->tp_reserve;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002233 if (po->has_vnet_hdr) {
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002234 netoff += sizeof(struct virtio_net_hdr);
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002235 do_vnet = true;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 macoff = netoff - maclen;
2238 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002239 if (po->tp_version <= TPACKET_V2) {
2240 if (macoff + snaplen > po->rx_ring.frame_size) {
2241 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002242 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002243 if (skb_shared(skb)) {
2244 copy_skb = skb_clone(skb, GFP_ATOMIC);
2245 } else {
2246 copy_skb = skb_get(skb);
2247 skb_head = skb->data;
2248 }
2249 if (copy_skb)
2250 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002252 snaplen = po->rx_ring.frame_size - macoff;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002253 if ((int)snaplen < 0) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002254 snaplen = 0;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002255 do_vnet = false;
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002258 } else if (unlikely(macoff + snaplen >
2259 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2260 u32 nval;
2261
2262 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2263 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2264 snaplen, nval, macoff);
2265 snaplen = nval;
2266 if (unlikely((int)snaplen < 0)) {
2267 snaplen = 0;
2268 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002269 do_vnet = false;
Eric Dumazetdc808112014-08-15 09:16:04 -07002270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00002273 h.raw = packet_current_rx_frame(po, skb,
2274 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002275 if (!h.raw)
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002276 goto drop_n_account;
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002277
2278 if (do_vnet &&
2279 virtio_net_hdr_from_skb(skb, h.raw + macoff -
2280 sizeof(struct virtio_net_hdr),
2281 vio_le(), true, 0))
2282 goto drop_n_account;
2283
chetan lokef6fb8f12011-08-19 10:18:16 +00002284 if (po->tp_version <= TPACKET_V2) {
2285 packet_increment_rx_head(po, &po->rx_ring);
2286 /*
2287 * LOSING will be reported till you read the stats,
2288 * because it's COR - Clear On Read.
2289 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2290 * at packet level.
2291 */
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002292 if (atomic_read(&po->tp_drops))
chetan lokef6fb8f12011-08-19 10:18:16 +00002293 status |= TP_STATUS_LOSING;
2294 }
Eric Dumazet945d0152018-06-21 14:16:02 -07002295
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002296 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (copy_skb) {
2298 status |= TP_STATUS_COPY;
2299 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 spin_unlock(&sk->sk_receive_queue.lock);
2302
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002303 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002304
2305 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Arnd Bergmannd413fcb2017-11-27 10:09:24 +01002306 ktime_get_real_ts64(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002308 status |= ts_status;
2309
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002310 switch (po->tp_version) {
2311 case TPACKET_V1:
2312 h.h1->tp_len = skb->len;
2313 h.h1->tp_snaplen = snaplen;
2314 h.h1->tp_mac = macoff;
2315 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002316 h.h1->tp_sec = ts.tv_sec;
2317 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002318 hdrlen = sizeof(*h.h1);
2319 break;
2320 case TPACKET_V2:
2321 h.h2->tp_len = skb->len;
2322 h.h2->tp_snaplen = snaplen;
2323 h.h2->tp_mac = macoff;
2324 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002325 h.h2->tp_sec = ts.tv_sec;
2326 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002327 if (skb_vlan_tag_present(skb)) {
2328 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002329 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2330 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002331 } else {
2332 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002333 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002334 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002335 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002336 hdrlen = sizeof(*h.h2);
2337 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00002338 case TPACKET_V3:
2339 /* tp_nxt_offset,vlan are already populated above.
2340 * So DONT clear those fields here
2341 */
2342 h.h3->tp_status |= status;
2343 h.h3->tp_len = skb->len;
2344 h.h3->tp_snaplen = snaplen;
2345 h.h3->tp_mac = macoff;
2346 h.h3->tp_net = netoff;
chetan lokef6fb8f12011-08-19 10:18:16 +00002347 h.h3->tp_sec = ts.tv_sec;
2348 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002349 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f12011-08-19 10:18:16 +00002350 hdrlen = sizeof(*h.h3);
2351 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002352 default:
2353 BUG();
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002356 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002357 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 sll->sll_family = AF_PACKET;
2359 sll->sll_hatype = dev->type;
2360 sll->sll_protocol = skb->protocol;
2361 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002362 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002363 sll->sll_ifindex = orig_dev->ifindex;
2364 else
2365 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Ralf Baechlee16aa202006-12-07 00:11:33 -08002367 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002368
Changli Gaof6dafa92010-12-07 04:26:16 +00002369#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002370 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002371 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002373 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2374 macoff + snaplen);
2375
2376 for (start = h.raw; start < end; start += PAGE_SIZE)
2377 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002379 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002380#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002381
Dan Collinsda413ee2014-12-19 16:49:25 +13002382 if (po->tp_version <= TPACKET_V2) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002383 __packet_set_status(po, h.raw, status);
Dan Collinsda413ee2014-12-19 16:49:25 +13002384 sk->sk_data_ready(sk);
2385 } else {
chetan lokef6fb8f12011-08-19 10:18:16 +00002386 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389drop_n_restore:
2390 if (skb_head != skb->data && skb_shared(skb)) {
2391 skb->data = skb_head;
2392 skb->len = skb_len;
2393 }
2394drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002395 if (!is_drop_n_account)
2396 consume_skb(skb);
2397 else
2398 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return 0;
2400
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002401drop_n_account:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 spin_unlock(&sk->sk_receive_queue.lock);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002403 atomic_inc(&po->tp_drops);
2404 is_drop_n_account = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
David S. Miller676d2362014-04-11 16:15:36 -04002406 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002407 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto drop_n_restore;
2409}
2410
Johann Baudy69e3c752009-05-18 22:11:22 -07002411static void tpacket_destruct_skb(struct sk_buff *skb)
2412{
2413 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002414
Johann Baudy69e3c752009-05-18 22:11:22 -07002415 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002416 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002417 __u32 ts;
2418
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002419 ph = skb_zcopy_get_nouarg(skb);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002420 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002421
2422 ts = __packet_set_timestamp(po, ph, skb);
2423 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Neil Horman89ed5b52019-06-25 17:57:49 -04002424
2425 if (!packet_read_pending(&po->tx_ring))
2426 complete(&po->skb_completion);
Johann Baudy69e3c752009-05-18 22:11:22 -07002427 }
2428
2429 sock_wfree(skb);
2430}
2431
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002432static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
2433{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002434 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2435 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2436 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
2437 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
2438 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
2439 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2440 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
2441
2442 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
2443 return -EINVAL;
2444
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002445 return 0;
2446}
2447
2448static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
2449 struct virtio_net_hdr *vnet_hdr)
2450{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002451 if (*len < sizeof(*vnet_hdr))
2452 return -EINVAL;
2453 *len -= sizeof(*vnet_hdr);
2454
Al Virocbbd26b2016-11-01 22:09:04 -04002455 if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002456 return -EFAULT;
2457
2458 return __packet_snd_vnet_parse(vnet_hdr, *len);
2459}
2460
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002461static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002462 void *frame, struct net_device *dev, void *data, int tp_len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002463 __be16 proto, unsigned char *addr, int hlen, int copylen,
2464 const struct sockcm_cookie *sockc)
Johann Baudy69e3c752009-05-18 22:11:22 -07002465{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002466 union tpacket_uhdr ph;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002467 int to_write, offset, len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002468 struct socket *sock = po->sk.sk_socket;
2469 struct page *page;
Johann Baudy69e3c752009-05-18 22:11:22 -07002470 int err;
2471
2472 ph.raw = frame;
2473
2474 skb->protocol = proto;
2475 skb->dev = dev;
2476 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002477 skb->mark = po->sk.sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002478 skb->tstamp = sockc->transmit_time;
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002479 skb_setup_tx_timestamp(skb, sockc->tsflags);
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002480 skb_zcopy_set_nouarg(skb, ph.raw);
Johann Baudy69e3c752009-05-18 22:11:22 -07002481
Herbert Xuae641942011-11-18 02:20:04 +00002482 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002483 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002484
Johann Baudy69e3c752009-05-18 22:11:22 -07002485 to_write = tp_len;
2486
2487 if (sock->type == SOCK_DGRAM) {
2488 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2489 NULL, tp_len);
2490 if (unlikely(err < 0))
2491 return -EINVAL;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002492 } else if (copylen) {
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002493 int hdrlen = min_t(int, copylen, tp_len);
2494
Johann Baudy69e3c752009-05-18 22:11:22 -07002495 skb_push(skb, dev->hard_header_len);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002496 skb_put(skb, copylen - dev->hard_header_len);
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002497 err = skb_store_bits(skb, 0, data, hdrlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002498 if (unlikely(err))
2499 return err;
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002500 if (!dev_validate_header(dev, skb->data, hdrlen))
2501 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002502
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002503 data += hdrlen;
2504 to_write -= hdrlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002505 }
2506
Johann Baudy69e3c752009-05-18 22:11:22 -07002507 offset = offset_in_page(data);
2508 len_max = PAGE_SIZE - offset;
2509 len = ((to_write > len_max) ? len_max : to_write);
2510
2511 skb->data_len = to_write;
2512 skb->len += to_write;
2513 skb->truesize += to_write;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002514 refcount_add(to_write, &po->sk.sk_wmem_alloc);
Johann Baudy69e3c752009-05-18 22:11:22 -07002515
2516 while (likely(to_write)) {
2517 nr_frags = skb_shinfo(skb)->nr_frags;
2518
2519 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002520 pr_err("Packet exceed the number of skb frags(%lu)\n",
2521 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002522 return -EFAULT;
2523 }
2524
Changli Gao0af55bb2010-12-01 02:52:20 +00002525 page = pgv_to_page(data);
2526 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002527 flush_dcache_page(page);
2528 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002529 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002530 to_write -= len;
2531 offset = 0;
2532 len_max = PAGE_SIZE;
2533 len = ((to_write > len_max) ? len_max : to_write);
2534 }
2535
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002536 packet_parse_headers(skb, sock);
Daniel Borkmannefdfa2f2015-11-11 23:25:40 +01002537
Johann Baudy69e3c752009-05-18 22:11:22 -07002538 return tp_len;
2539}
2540
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002541static int tpacket_parse_header(struct packet_sock *po, void *frame,
2542 int size_max, void **data)
2543{
2544 union tpacket_uhdr ph;
2545 int tp_len, off;
2546
2547 ph.raw = frame;
2548
2549 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002550 case TPACKET_V3:
2551 if (ph.h3->tp_next_offset != 0) {
2552 pr_warn_once("variable sized slot not supported");
2553 return -EINVAL;
2554 }
2555 tp_len = ph.h3->tp_len;
2556 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002557 case TPACKET_V2:
2558 tp_len = ph.h2->tp_len;
2559 break;
2560 default:
2561 tp_len = ph.h1->tp_len;
2562 break;
2563 }
2564 if (unlikely(tp_len > size_max)) {
2565 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2566 return -EMSGSIZE;
2567 }
2568
2569 if (unlikely(po->tp_tx_has_off)) {
2570 int off_min, off_max;
2571
2572 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2573 off_max = po->tx_ring.frame_size - tp_len;
2574 if (po->sk.sk_type == SOCK_DGRAM) {
2575 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002576 case TPACKET_V3:
2577 off = ph.h3->tp_net;
2578 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002579 case TPACKET_V2:
2580 off = ph.h2->tp_net;
2581 break;
2582 default:
2583 off = ph.h1->tp_net;
2584 break;
2585 }
2586 } else {
2587 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002588 case TPACKET_V3:
2589 off = ph.h3->tp_mac;
2590 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002591 case TPACKET_V2:
2592 off = ph.h2->tp_mac;
2593 break;
2594 default:
2595 off = ph.h1->tp_mac;
2596 break;
2597 }
2598 }
2599 if (unlikely((off < off_min) || (off_max < off)))
2600 return -EINVAL;
2601 } else {
2602 off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2603 }
2604
2605 *data = frame + off;
2606 return tp_len;
2607}
2608
Johann Baudy69e3c752009-05-18 22:11:22 -07002609static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2610{
Neil Horman89ed5b52019-06-25 17:57:49 -04002611 struct sk_buff *skb = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002612 struct net_device *dev;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002613 struct virtio_net_hdr *vnet_hdr = NULL;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002614 struct sockcm_cookie sockc;
Johann Baudy69e3c752009-05-18 22:11:22 -07002615 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002616 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002617 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002618 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002619 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002620 unsigned char *addr = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002621 int tp_len, size_max;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002622 void *data;
Johann Baudy69e3c752009-05-18 22:11:22 -07002623 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002624 int status = TP_STATUS_AVAILABLE;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002625 int hlen, tlen, copylen = 0;
Neil Horman89ed5b52019-06-25 17:57:49 -04002626 long timeo = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002627
Johann Baudy69e3c752009-05-18 22:11:22 -07002628 mutex_lock(&po->pg_vec_lock);
2629
Eric Dumazet32d31822019-08-14 02:11:57 -07002630 /* packet_sendmsg() check on tx_ring.pg_vec was lockless,
2631 * we need to confirm it under protection of pg_vec_lock.
2632 */
2633 if (unlikely(!po->tx_ring.pg_vec)) {
2634 err = -EBUSY;
2635 goto out;
2636 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002637 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002638 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002639 proto = po->num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002640 } else {
2641 err = -EINVAL;
2642 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2643 goto out;
2644 if (msg->msg_namelen < (saddr->sll_halen
2645 + offsetof(struct sockaddr_ll,
2646 sll_addr)))
2647 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002648 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002649 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002650 if (po->sk.sk_socket->type == SOCK_DGRAM) {
2651 if (dev && msg->msg_namelen < dev->addr_len +
2652 offsetof(struct sockaddr_ll, sll_addr))
2653 goto out_put;
2654 addr = saddr->sll_addr;
2655 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002656 }
2657
Johann Baudy69e3c752009-05-18 22:11:22 -07002658 err = -ENXIO;
2659 if (unlikely(dev == NULL))
2660 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002661 err = -ENETDOWN;
2662 if (unlikely(!(dev->flags & IFF_UP)))
2663 goto out_put;
2664
Willem de Bruijn657a0662018-07-06 10:12:56 -04002665 sockcm_init(&sockc, &po->sk);
Douglas Caetano dos Santosd19b1832017-05-12 15:19:15 -03002666 if (msg->msg_controllen) {
2667 err = sock_cmsg_send(&po->sk, msg, &sockc);
2668 if (unlikely(err))
2669 goto out_put;
2670 }
2671
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002672 if (po->sk.sk_socket->type == SOCK_RAW)
2673 reserve = dev->hard_header_len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002674 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002675 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002676
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002677 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002678 size_max = dev->mtu + reserve + VLAN_HLEN;
David S. Miller09effa62013-08-07 17:11:00 -07002679
Neil Horman89ed5b52019-06-25 17:57:49 -04002680 reinit_completion(&po->skb_completion);
2681
Johann Baudy69e3c752009-05-18 22:11:22 -07002682 do {
2683 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002684 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002685 if (unlikely(ph == NULL)) {
Neil Horman89ed5b52019-06-25 17:57:49 -04002686 if (need_wait && skb) {
2687 timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
2688 timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
2689 if (timeo <= 0) {
2690 err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
2691 goto out_put;
2692 }
2693 }
2694 /* check for additional frames */
Johann Baudy69e3c752009-05-18 22:11:22 -07002695 continue;
2696 }
2697
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002698 skb = NULL;
2699 tp_len = tpacket_parse_header(po, ph, size_max, &data);
2700 if (tp_len < 0)
2701 goto tpacket_error;
2702
Johann Baudy69e3c752009-05-18 22:11:22 -07002703 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002704 hlen = LL_RESERVED_SPACE(dev);
2705 tlen = dev->needed_tailroom;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002706 if (po->has_vnet_hdr) {
2707 vnet_hdr = data;
2708 data += sizeof(*vnet_hdr);
2709 tp_len -= sizeof(*vnet_hdr);
2710 if (tp_len < 0 ||
2711 __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
2712 tp_len = -EINVAL;
2713 goto tpacket_error;
2714 }
2715 copylen = __virtio16_to_cpu(vio_le(),
2716 vnet_hdr->hdr_len);
2717 }
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002718 copylen = max_t(int, copylen, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002719 skb = sock_alloc_send_skb(&po->sk,
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002720 hlen + tlen + sizeof(struct sockaddr_ll) +
2721 (copylen - dev->hard_header_len),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002722 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002723
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002724 if (unlikely(skb == NULL)) {
2725 /* we assume the socket was initially writeable ... */
2726 if (likely(len_sum > 0))
2727 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002728 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002729 }
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002730 tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002731 addr, hlen, copylen, &sockc);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002732 if (likely(tp_len >= 0) &&
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002733 tp_len > dev->mtu + reserve &&
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002734 !po->has_vnet_hdr &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002735 !packet_extra_vlan_len_allowed(dev, skb))
2736 tp_len = -EMSGSIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002737
2738 if (unlikely(tp_len < 0)) {
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002739tpacket_error:
Johann Baudy69e3c752009-05-18 22:11:22 -07002740 if (po->tp_loss) {
2741 __packet_set_status(po, ph,
2742 TP_STATUS_AVAILABLE);
2743 packet_increment_head(&po->tx_ring);
2744 kfree_skb(skb);
2745 continue;
2746 } else {
2747 status = TP_STATUS_WRONG_FORMAT;
2748 err = tp_len;
2749 goto out_status;
2750 }
2751 }
2752
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002753 if (po->has_vnet_hdr) {
2754 if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
2755 tp_len = -EINVAL;
2756 goto tpacket_error;
2757 }
2758 virtio_net_hdr_set_proto(skb, vnet_hdr);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002759 }
2760
Johann Baudy69e3c752009-05-18 22:11:22 -07002761 skb->destructor = tpacket_destruct_skb;
2762 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002763 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002764
2765 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002766 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002767 if (unlikely(err > 0)) {
2768 err = net_xmit_errno(err);
2769 if (err && __packet_get_status(po, ph) ==
2770 TP_STATUS_AVAILABLE) {
2771 /* skb was destructed already */
2772 skb = NULL;
2773 goto out_status;
2774 }
2775 /*
2776 * skb was dropped but not destructed yet;
2777 * let's treat it like congestion or err < 0
2778 */
2779 err = 0;
2780 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002781 packet_increment_head(&po->tx_ring);
2782 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002783 } while (likely((ph != NULL) ||
2784 /* Note: packet_read_pending() might be slow if we have
2785 * to call it as it's per_cpu variable, but in fast-path
2786 * we already short-circuit the loop with the first
2787 * condition, and luckily don't have to go that path
2788 * anyway.
2789 */
2790 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002791
2792 err = len_sum;
2793 goto out_put;
2794
Johann Baudy69e3c752009-05-18 22:11:22 -07002795out_status:
2796 __packet_set_status(po, ph, status);
2797 kfree_skb(skb);
2798out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002799 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002800out:
2801 mutex_unlock(&po->pg_vec_lock);
2802 return err;
2803}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002805static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2806 size_t reserve, size_t len,
2807 size_t linear, int noblock,
2808 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002809{
2810 struct sk_buff *skb;
2811
2812 /* Under a page? Don't bother with paged skb. */
2813 if (prepad + len < PAGE_SIZE || !linear)
2814 linear = len;
2815
2816 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002817 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002818 if (!skb)
2819 return NULL;
2820
2821 skb_reserve(skb, reserve);
2822 skb_put(skb, linear);
2823 skb->data_len = len - linear;
2824 skb->len += len - linear;
2825
2826 return skb;
2827}
2828
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002829static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002832 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct sk_buff *skb;
2834 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002835 __be16 proto;
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002836 unsigned char *addr = NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002837 int err, reserve = 0;
Edward Jeec7d39e32015-10-08 14:56:49 -07002838 struct sockcm_cookie sockc;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002839 struct virtio_net_hdr vnet_hdr = { 0 };
2840 int offset = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002841 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002842 bool has_vnet_hdr = false;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002843 int hlen, tlen, linear;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002844 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002847 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002849
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002850 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002851 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 proto = po->num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 } else {
2854 err = -EINVAL;
2855 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2856 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002857 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002860 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002861 if (sock->type == SOCK_DGRAM) {
2862 if (dev && msg->msg_namelen < dev->addr_len +
2863 offsetof(struct sockaddr_ll, sll_addr))
2864 goto out_unlock;
2865 addr = saddr->sll_addr;
2866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002870 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002872 err = -ENETDOWN;
2873 if (unlikely(!(dev->flags & IFF_UP)))
2874 goto out_unlock;
2875
Willem de Bruijn657a0662018-07-06 10:12:56 -04002876 sockcm_init(&sockc, sk);
Edward Jeec7d39e32015-10-08 14:56:49 -07002877 sockc.mark = sk->sk_mark;
2878 if (msg->msg_controllen) {
2879 err = sock_cmsg_send(sk, msg, &sockc);
2880 if (unlikely(err))
2881 goto out_unlock;
2882 }
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (sock->type == SOCK_RAW)
2885 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002886 if (po->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002887 err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
2888 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002889 goto out_unlock;
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002890 has_vnet_hdr = true;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002891 }
2892
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002893 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2894 if (!netif_supports_nofcs(dev)) {
2895 err = -EPROTONOSUPPORT;
2896 goto out_unlock;
2897 }
2898 extra_len = 4; /* We're doing our own CRC */
2899 }
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 err = -EMSGSIZE;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002902 if (!vnet_hdr.gso_type &&
2903 (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 goto out_unlock;
2905
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002906 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002907 hlen = LL_RESERVED_SPACE(dev);
2908 tlen = dev->needed_tailroom;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002909 linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
2910 linear = max(linear, min_t(int, len, dev->hard_header_len));
2911 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002912 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002913 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 goto out_unlock;
2915
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002916 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002918 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002919 if (sock->type == SOCK_DGRAM) {
2920 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002921 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002922 goto out_free;
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002923 } else if (reserve) {
Willem de Bruijn9aad13b2018-05-24 18:10:30 -04002924 skb_reserve(skb, -reserve);
Nicolas Dichtel88a81212019-01-17 11:27:22 +01002925 if (len < reserve + sizeof(struct ipv6hdr) &&
2926 dev->min_header_len != dev->hard_header_len)
Willem de Bruijn993675a2018-07-11 12:00:45 -04002927 skb_reset_network_header(skb);
Willem de Bruijn9c707762014-11-19 13:10:16 -05002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002931 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (err)
2933 goto out_free;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002934
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002935 if (sock->type == SOCK_RAW &&
2936 !dev_validate_header(dev, skb->data, len)) {
2937 err = -EINVAL;
2938 goto out_free;
2939 }
2940
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002941 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002943 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002944 !packet_extra_vlan_len_allowed(dev, skb)) {
2945 err = -EMSGSIZE;
2946 goto out_free;
Ben Greear57f89bf2011-02-11 09:35:18 +00002947 }
2948
David S. Miller09effa62013-08-07 17:11:00 -07002949 skb->protocol = proto;
2950 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 skb->priority = sk->sk_priority;
Edward Jeec7d39e32015-10-08 14:56:49 -07002952 skb->mark = sockc.mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002953 skb->tstamp = sockc.transmit_time;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002954
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002955 if (has_vnet_hdr) {
Jarno Rajahalmedb60eb5f2016-11-18 15:40:41 -08002956 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002957 if (err)
2958 goto out_free;
2959 len += sizeof(vnet_hdr);
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002960 virtio_net_hdr_set_proto(skb, &vnet_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002961 }
2962
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002963 packet_parse_headers(skb, sock);
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01002964
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002965 if (unlikely(extra_len == 4))
2966 skb->no_fcs = 1;
2967
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002968 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2970 goto out_unlock;
2971
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002972 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002974 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976out_free:
2977 kfree_skb(skb);
2978out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002979 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 dev_put(dev);
2981out:
2982 return err;
2983}
2984
Ying Xue1b784142015-03-02 15:37:48 +08002985static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07002986{
Johann Baudy69e3c752009-05-18 22:11:22 -07002987 struct sock *sk = sock->sk;
2988 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002989
Johann Baudy69e3c752009-05-18 22:11:22 -07002990 if (po->tx_ring.pg_vec)
2991 return tpacket_snd(po, msg);
2992 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002993 return packet_snd(sock, msg, len);
2994}
2995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996/*
2997 * Close a PACKET socket. This is fairly simple. We immediately go
2998 * to 'closed' state and remove our protocol entry in the device list.
2999 */
3000
3001static int packet_release(struct socket *sock)
3002{
3003 struct sock *sk = sock->sk;
3004 struct packet_sock *po;
Anoob Soman2bd624b2017-02-15 20:25:39 +00003005 struct packet_fanout *f;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003006 struct net *net;
chetan lokef6fb8f12011-08-19 10:18:16 +00003007 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 if (!sk)
3010 return 0;
3011
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003012 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 po = pkt_sk(sk);
3014
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003015 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00003016 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003017 mutex_unlock(&net->packet.sklist_lock);
3018
3019 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08003020 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003021 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
stephen hemminger808f5112010-02-22 07:57:18 +00003023 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003024 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003025 packet_cached_dev_reset(po);
3026
Ben Greear160ff182011-06-01 07:18:52 +00003027 if (po->prot_hook.dev) {
3028 dev_put(po->prot_hook.dev);
3029 po->prot_hook.dev = NULL;
3030 }
stephen hemminger808f5112010-02-22 07:57:18 +00003031 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Eric Dumazet5171b372018-04-15 17:52:04 -07003035 lock_sock(sk);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003036 if (po->rx_ring.pg_vec) {
3037 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003038 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003039 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003040
Phil Sutter9665d5d2013-02-01 07:21:41 +00003041 if (po->tx_ring.pg_vec) {
3042 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003043 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003044 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003045 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Anoob Soman2bd624b2017-02-15 20:25:39 +00003047 f = fanout_release(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07003048
stephen hemminger808f5112010-02-22 07:57:18 +00003049 synchronize_net();
Anoob Soman2bd624b2017-02-15 20:25:39 +00003050
Willem de Bruijnafa09252019-05-31 12:37:23 -04003051 kfree(po->rollover);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003052 if (f) {
3053 fanout_release_data(f);
3054 kfree(f);
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 /*
3057 * Now the socket is dead. No more input will appear.
3058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 sock_orphan(sk);
3060 sock->sk = NULL;
3061
3062 /* Purge queues */
3063
3064 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01003065 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003066 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 sock_put(sk);
3069 return 0;
3070}
3071
3072/*
3073 * Attach a packet hook.
3074 */
3075
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003076static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
3077 __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
3079 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003080 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003081 __be16 proto_curr;
3082 bool need_rehook;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003083 struct net_device *dev = NULL;
3084 int ret = 0;
3085 bool unlisted = false;
David S. Millerdc99f602011-07-05 01:45:05 -07003086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 spin_lock(&po->bind_lock);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003089 rcu_read_lock();
3090
Willem de Bruijn49716132017-09-26 12:19:37 -04003091 if (po->fanout) {
3092 ret = -EINVAL;
3093 goto out_unlock;
3094 }
3095
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003096 if (name) {
3097 dev = dev_get_by_name_rcu(sock_net(sk), name);
3098 if (!dev) {
3099 ret = -ENODEV;
3100 goto out_unlock;
3101 }
3102 } else if (ifindex) {
3103 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
3104 if (!dev) {
3105 ret = -ENODEV;
3106 goto out_unlock;
3107 }
3108 }
3109
3110 if (dev)
3111 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003112
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003113 proto_curr = po->prot_hook.type;
3114 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003116 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003118 if (need_rehook) {
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003119 if (po->running) {
3120 rcu_read_unlock();
Eric Dumazet15fe0762017-11-28 08:03:30 -08003121 /* prevents packet_notifier() from calling
3122 * register_prot_hook()
3123 */
3124 po->num = 0;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003125 __unregister_prot_hook(sk, true);
3126 rcu_read_lock();
3127 dev_curr = po->prot_hook.dev;
3128 if (dev)
3129 unlisted = !dev_get_by_index_rcu(sock_net(sk),
3130 dev->ifindex);
3131 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003132
Eric Dumazet15fe0762017-11-28 08:03:30 -08003133 BUG_ON(po->running);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003134 po->num = proto;
3135 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003136
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003137 if (unlikely(unlisted)) {
3138 dev_put(dev);
3139 po->prot_hook.dev = NULL;
3140 po->ifindex = -1;
3141 packet_cached_dev_reset(po);
3142 } else {
3143 po->prot_hook.dev = dev;
3144 po->ifindex = dev ? dev->ifindex : 0;
3145 packet_cached_dev_assign(po, dev);
3146 }
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003147 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003148 if (dev_curr)
3149 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003150
3151 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 goto out_unlock;
3153
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003154 if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
David S. Millerce06b032011-07-04 01:44:29 -07003155 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08003156 } else {
3157 sk->sk_err = ENETDOWN;
3158 if (!sock_flag(sk, SOCK_DEAD))
3159 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161
3162out_unlock:
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003163 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 spin_unlock(&po->bind_lock);
3165 release_sock(sk);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
3169/*
3170 * Bind a packet socket to a device
3171 */
3172
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003173static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
3174 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003176 struct sock *sk = sock->sk;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003177 char name[sizeof(uaddr->sa_data) + 1];
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 /*
3180 * Check legality
3181 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003182
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003183 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return -EINVAL;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003185 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
3186 * zero-terminated.
3187 */
3188 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
3189 name[sizeof(uaddr->sa_data)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003191 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3195{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003196 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
3197 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 /*
3200 * Check legality
3201 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (addr_len < sizeof(struct sockaddr_ll))
3204 return -EINVAL;
3205 if (sll->sll_family != AF_PACKET)
3206 return -EINVAL;
3207
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003208 return packet_do_bind(sk, NULL, sll->sll_ifindex,
3209 sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
3212static struct proto packet_proto = {
3213 .name = "PACKET",
3214 .owner = THIS_MODULE,
3215 .obj_size = sizeof(struct packet_sock),
3216};
3217
3218/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003219 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 */
3221
Eric Paris3f378b62009-11-05 22:18:14 -08003222static int packet_create(struct net *net, struct socket *sock, int protocol,
3223 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
3225 struct sock *sk;
3226 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003227 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 int err;
3229
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003230 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003232 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3233 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return -ESOCKTNOSUPPORT;
3235
3236 sock->state = SS_UNCONNECTED;
3237
3238 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003239 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (sk == NULL)
3241 goto out;
3242
3243 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (sock->type == SOCK_PACKET)
3245 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 sock_init_data(sock, sk);
3248
3249 po = pkt_sk(sk);
Neil Horman89ed5b52019-06-25 17:57:49 -04003250 init_completion(&po->skb_completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003252 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003253 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003254
Daniel Borkmannb0138402014-01-15 16:25:36 +01003255 err = packet_alloc_pending(po);
3256 if (err)
3257 goto out2;
3258
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003259 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003262 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 /*
3265 * Attach a protocol block
3266 */
3267
3268 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003269 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003270 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (sock->type == SOCK_PACKET)
3274 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 po->prot_hook.af_packet_priv = sk;
3277
Al Viro0e11c912006-11-08 00:26:29 -08003278 if (proto) {
3279 po->prot_hook.type = proto;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003280 __register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
3282
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003283 mutex_lock(&net->packet.sklist_lock);
Maxime Chevalliera4dc6a42019-03-16 14:41:30 +01003284 sk_add_node_tail_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003285 mutex_unlock(&net->packet.sklist_lock);
3286
3287 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003288 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003289 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003290
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003291 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003292out2:
3293 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294out:
3295 return err;
3296}
3297
3298/*
3299 * Pull a packet from our receive queue and hand it to the user.
3300 * If necessary we block.
3301 */
3302
Ying Xue1b784142015-03-02 15:37:48 +08003303static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3304 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 struct sock *sk = sock->sk;
3307 struct sk_buff *skb;
3308 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003309 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003310 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003313 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 goto out;
3315
3316#if 0
3317 /* What error should we return now? EUNATTACH? */
3318 if (pkt_sk(sk)->ifindex < 0)
3319 return -ENODEV;
3320#endif
3321
Richard Cochraned85b562010-04-07 22:41:28 +00003322 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003323 err = sock_recv_errqueue(sk, msg, len,
3324 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003325 goto out;
3326 }
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 * Call the generic datagram receiver. This handles all sorts
3330 * of horrible races and re-entrancy so we can forget about it
3331 * in the protocol layers.
3332 *
3333 * Now it will return ENETDOWN, if device have just gone down,
3334 * but then it will block.
3335 */
3336
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003337 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003340 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 * handles the blocking we don't see and worry about blocking
3342 * retries.
3343 */
3344
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003345 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 goto out;
3347
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07003348 packet_rcv_try_clear_pressure(pkt_sk(sk));
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003349
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003350 if (pkt_sk(sk)->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003351 err = packet_rcv_vnet(msg, skb, &len);
3352 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003353 goto out_free;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003354 vnet_hdr_len = sizeof(struct virtio_net_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003355 }
3356
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003357 /* You lose any data beyond the buffer you gave. If it worries
3358 * a user program they can ask the device for its MTU
3359 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003362 if (copied > len) {
3363 copied = len;
3364 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
3366
David S. Miller51f3d022014-11-05 16:46:40 -05003367 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (err)
3369 goto out_free;
3370
Eyal Birger2472d762015-03-01 14:58:28 +02003371 if (sock->type != SOCK_PACKET) {
3372 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3373
3374 /* Original length was stored in sockaddr_ll fields */
3375 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3376 sll->sll_family = AF_PACKET;
3377 sll->sll_protocol = skb->protocol;
3378 }
3379
Neil Horman3b885782009-10-12 13:26:31 -07003380 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003382 if (msg->msg_name) {
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003383 int copy_len;
3384
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003385 /* If the address length field is there to be filled
3386 * in, we fill it in now.
3387 */
3388 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003389 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003390 msg->msg_namelen = sizeof(struct sockaddr_pkt);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003391 copy_len = msg->msg_namelen;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003392 } else {
3393 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003394
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003395 msg->msg_namelen = sll->sll_halen +
3396 offsetof(struct sockaddr_ll, sll_addr);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003397 copy_len = msg->msg_namelen;
3398 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
3399 memset(msg->msg_name +
3400 offsetof(struct sockaddr_ll, sll_addr),
3401 0, sizeof(sll->sll_addr));
3402 msg->msg_namelen = sizeof(struct sockaddr_ll);
3403 }
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003404 }
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003405 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Herbert Xu8dc41942007-02-04 23:31:32 -08003408 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003409 struct tpacket_auxdata aux;
3410
3411 aux.tp_status = TP_STATUS_USER;
3412 if (skb->ip_summed == CHECKSUM_PARTIAL)
3413 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003414 else if (skb->pkt_type != PACKET_OUTGOING &&
3415 (skb->ip_summed == CHECKSUM_COMPLETE ||
3416 skb_csum_unnecessary(skb)))
3417 aux.tp_status |= TP_STATUS_CSUM_VALID;
3418
Eyal Birger2472d762015-03-01 14:58:28 +02003419 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003420 aux.tp_snaplen = skb->len;
3421 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003422 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003423 if (skb_vlan_tag_present(skb)) {
3424 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003425 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3426 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003427 } else {
3428 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003429 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003430 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003431 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003432 }
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 /*
3435 * Free or return the buffer as appropriate. Again this
3436 * hides all the races and re-entrancy issues from us.
3437 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003438 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440out_free:
3441 skb_free_datagram(sk, skb);
3442out:
3443 return err;
3444}
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003447 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
3449 struct net_device *dev;
3450 struct sock *sk = sock->sk;
3451
3452 if (peer)
3453 return -EOPNOTSUPP;
3454
3455 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003456 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003457 rcu_read_lock();
3458 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3459 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003460 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003461 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003463 return sizeof(*uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003467 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
3469 struct net_device *dev;
3470 struct sock *sk = sock->sk;
3471 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003472 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 if (peer)
3475 return -EOPNOTSUPP;
3476
3477 sll->sll_family = AF_PACKET;
3478 sll->sll_ifindex = po->ifindex;
3479 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003480 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003481 rcu_read_lock();
3482 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 if (dev) {
3484 sll->sll_hatype = dev->type;
3485 sll->sll_halen = dev->addr_len;
3486 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 } else {
3488 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3489 sll->sll_halen = 0;
3490 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003491 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003493 return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
Wang Chen2aeb0b82008-07-14 20:49:46 -07003496static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3497 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 switch (i->type) {
3500 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003501 if (i->alen != dev->addr_len)
3502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003504 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003506 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 break;
3508 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003509 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003511 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003512 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003513 if (i->alen != dev->addr_len)
3514 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003515 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003516 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003517 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003518 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003519 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003520 default:
3521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003526static void packet_dev_mclist_delete(struct net_device *dev,
3527 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003529 struct packet_mclist *ml;
3530
3531 while ((ml = *mlp) != NULL) {
3532 if (ml->ifindex == dev->ifindex) {
3533 packet_dev_mc(dev, ml, -1);
3534 *mlp = ml->next;
3535 kfree(ml);
3536 } else
3537 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
3539}
3540
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003541static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 struct packet_sock *po = pkt_sk(sk);
3544 struct packet_mclist *ml, *i;
3545 struct net_device *dev;
3546 int err;
3547
3548 rtnl_lock();
3549
3550 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003551 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (!dev)
3553 goto done;
3554
3555 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003556 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 goto done;
3558
3559 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003560 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 if (i == NULL)
3562 goto done;
3563
3564 err = 0;
3565 for (ml = po->mclist; ml; ml = ml->next) {
3566 if (ml->ifindex == mreq->mr_ifindex &&
3567 ml->type == mreq->mr_type &&
3568 ml->alen == mreq->mr_alen &&
3569 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3570 ml->count++;
3571 /* Free the new element ... */
3572 kfree(i);
3573 goto done;
3574 }
3575 }
3576
3577 i->type = mreq->mr_type;
3578 i->ifindex = mreq->mr_ifindex;
3579 i->alen = mreq->mr_alen;
3580 memcpy(i->addr, mreq->mr_address, i->alen);
Mathias Krause309cf372016-04-10 12:52:28 +02003581 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 i->count = 1;
3583 i->next = po->mclist;
3584 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003585 err = packet_dev_mc(dev, i, 1);
3586 if (err) {
3587 po->mclist = i->next;
3588 kfree(i);
3589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591done:
3592 rtnl_unlock();
3593 return err;
3594}
3595
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003596static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
3598 struct packet_mclist *ml, **mlp;
3599
3600 rtnl_lock();
3601
3602 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3603 if (ml->ifindex == mreq->mr_ifindex &&
3604 ml->type == mreq->mr_type &&
3605 ml->alen == mreq->mr_alen &&
3606 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3607 if (--ml->count == 0) {
3608 struct net_device *dev;
3609 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003610 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3611 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 kfree(ml);
3614 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617 }
3618 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
3622static void packet_flush_mclist(struct sock *sk)
3623{
3624 struct packet_sock *po = pkt_sk(sk);
3625 struct packet_mclist *ml;
3626
3627 if (!po->mclist)
3628 return;
3629
3630 rtnl_lock();
3631 while ((ml = po->mclist) != NULL) {
3632 struct net_device *dev;
3633
3634 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003635 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3636 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 kfree(ml);
3639 }
3640 rtnl_unlock();
3641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643static int
David S. Millerb7058842009-09-30 16:12:20 -07003644packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003647 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 int ret;
3649
3650 if (level != SOL_PACKET)
3651 return -ENOPROTOOPT;
3652
Johann Baudy69e3c752009-05-18 22:11:22 -07003653 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003654 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 case PACKET_DROP_MEMBERSHIP:
3656 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003657 struct packet_mreq_max mreq;
3658 int len = optlen;
3659 memset(&mreq, 0, sizeof(mreq));
3660 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003662 if (len > sizeof(mreq))
3663 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003664 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003666 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 if (optname == PACKET_ADD_MEMBERSHIP)
3669 ret = packet_mc_add(sk, &mreq);
3670 else
3671 ret = packet_mc_drop(sk, &mreq);
3672 return ret;
3673 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003676 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 {
chetan lokef6fb8f12011-08-19 10:18:16 +00003678 union tpacket_req_u req_u;
3679 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Eric Dumazet5171b372018-04-15 17:52:04 -07003681 lock_sock(sk);
chetan lokef6fb8f12011-08-19 10:18:16 +00003682 switch (po->tp_version) {
3683 case TPACKET_V1:
3684 case TPACKET_V2:
3685 len = sizeof(req_u.req);
3686 break;
3687 case TPACKET_V3:
3688 default:
3689 len = sizeof(req_u.req3);
3690 break;
3691 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003692 if (optlen < len) {
3693 ret = -EINVAL;
3694 } else {
3695 if (copy_from_user(&req_u.req, optval, len))
3696 ret = -EFAULT;
3697 else
3698 ret = packet_set_ring(sk, &req_u, 0,
3699 optname == PACKET_TX_RING);
3700 }
3701 release_sock(sk);
3702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
3704 case PACKET_COPY_THRESH:
3705 {
3706 int val;
3707
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003708 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003710 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return -EFAULT;
3712
3713 pkt_sk(sk)->copy_thresh = val;
3714 return 0;
3715 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003716 case PACKET_VERSION:
3717 {
3718 int val;
3719
3720 if (optlen != sizeof(val))
3721 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003722 if (copy_from_user(&val, optval, sizeof(val)))
3723 return -EFAULT;
3724 switch (val) {
3725 case TPACKET_V1:
3726 case TPACKET_V2:
chetan lokef6fb8f12011-08-19 10:18:16 +00003727 case TPACKET_V3:
Philip Pettersson84ac7262016-11-30 14:55:36 -08003728 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003729 default:
3730 return -EINVAL;
3731 }
Philip Pettersson84ac7262016-11-30 14:55:36 -08003732 lock_sock(sk);
3733 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3734 ret = -EBUSY;
3735 } else {
3736 po->tp_version = val;
3737 ret = 0;
3738 }
3739 release_sock(sk);
3740 return ret;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003741 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003742 case PACKET_RESERVE:
3743 {
3744 unsigned int val;
3745
3746 if (optlen != sizeof(val))
3747 return -EINVAL;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003748 if (copy_from_user(&val, optval, sizeof(val)))
3749 return -EFAULT;
Andrey Konovalovbcc53642017-03-29 16:11:22 +02003750 if (val > INT_MAX)
3751 return -EINVAL;
Willem de Bruijnc27927e2017-08-10 12:41:58 -04003752 lock_sock(sk);
3753 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3754 ret = -EBUSY;
3755 } else {
3756 po->tp_reserve = val;
3757 ret = 0;
3758 }
3759 release_sock(sk);
3760 return ret;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003761 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003762 case PACKET_LOSS:
3763 {
3764 unsigned int val;
3765
3766 if (optlen != sizeof(val))
3767 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003768 if (copy_from_user(&val, optval, sizeof(val)))
3769 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003770
3771 lock_sock(sk);
3772 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3773 ret = -EBUSY;
3774 } else {
3775 po->tp_loss = !!val;
3776 ret = 0;
3777 }
3778 release_sock(sk);
3779 return ret;
Johann Baudy69e3c752009-05-18 22:11:22 -07003780 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003781 case PACKET_AUXDATA:
3782 {
3783 int val;
3784
3785 if (optlen < sizeof(val))
3786 return -EINVAL;
3787 if (copy_from_user(&val, optval, sizeof(val)))
3788 return -EFAULT;
3789
Willem de Bruijna6361f02018-04-23 17:37:03 -04003790 lock_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003791 po->auxdata = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003792 release_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003793 return 0;
3794 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003795 case PACKET_ORIGDEV:
3796 {
3797 int val;
3798
3799 if (optlen < sizeof(val))
3800 return -EINVAL;
3801 if (copy_from_user(&val, optval, sizeof(val)))
3802 return -EFAULT;
3803
Willem de Bruijna6361f02018-04-23 17:37:03 -04003804 lock_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003805 po->origdev = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003806 release_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003807 return 0;
3808 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003809 case PACKET_VNET_HDR:
3810 {
3811 int val;
3812
3813 if (sock->type != SOCK_RAW)
3814 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003815 if (optlen < sizeof(val))
3816 return -EINVAL;
3817 if (copy_from_user(&val, optval, sizeof(val)))
3818 return -EFAULT;
3819
Willem de Bruijna6361f02018-04-23 17:37:03 -04003820 lock_sock(sk);
3821 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3822 ret = -EBUSY;
3823 } else {
3824 po->has_vnet_hdr = !!val;
3825 ret = 0;
3826 }
3827 release_sock(sk);
3828 return ret;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003829 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003830 case PACKET_TIMESTAMP:
3831 {
3832 int val;
3833
3834 if (optlen != sizeof(val))
3835 return -EINVAL;
3836 if (copy_from_user(&val, optval, sizeof(val)))
3837 return -EFAULT;
3838
3839 po->tp_tstamp = val;
3840 return 0;
3841 }
David S. Millerdc99f602011-07-05 01:45:05 -07003842 case PACKET_FANOUT:
3843 {
3844 int val;
3845
3846 if (optlen != sizeof(val))
3847 return -EINVAL;
3848 if (copy_from_user(&val, optval, sizeof(val)))
3849 return -EFAULT;
3850
3851 return fanout_add(sk, val & 0xffff, val >> 16);
3852 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003853 case PACKET_FANOUT_DATA:
3854 {
3855 if (!po->fanout)
3856 return -EINVAL;
3857
3858 return fanout_set_data(po, optval, optlen);
3859 }
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003860 case PACKET_IGNORE_OUTGOING:
3861 {
3862 int val;
3863
3864 if (optlen != sizeof(val))
3865 return -EINVAL;
3866 if (copy_from_user(&val, optval, sizeof(val)))
3867 return -EFAULT;
3868 if (val < 0 || val > 1)
3869 return -EINVAL;
3870
3871 po->prot_hook.ignore_outgoing = !!val;
3872 return 0;
3873 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003874 case PACKET_TX_HAS_OFF:
3875 {
3876 unsigned int val;
3877
3878 if (optlen != sizeof(val))
3879 return -EINVAL;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003880 if (copy_from_user(&val, optval, sizeof(val)))
3881 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003882
3883 lock_sock(sk);
3884 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3885 ret = -EBUSY;
3886 } else {
3887 po->tp_tx_has_off = !!val;
3888 ret = 0;
3889 }
3890 release_sock(sk);
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003891 return 0;
3892 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003893 case PACKET_QDISC_BYPASS:
3894 {
3895 int val;
3896
3897 if (optlen != sizeof(val))
3898 return -EINVAL;
3899 if (copy_from_user(&val, optval, sizeof(val)))
3900 return -EFAULT;
3901
3902 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3903 return 0;
3904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 default:
3906 return -ENOPROTOOPT;
3907 }
3908}
3909
3910static int packet_getsockopt(struct socket *sock, int level, int optname,
3911 char __user *optval, int __user *optlen)
3912{
3913 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003914 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct sock *sk = sock->sk;
3916 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003917 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003918 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003919 struct tpacket_rollover_stats rstats;
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003920 int drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
3922 if (level != SOL_PACKET)
3923 return -ENOPROTOOPT;
3924
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003925 if (get_user(len, optlen))
3926 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 if (len < 0)
3929 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003930
Johann Baudy69e3c752009-05-18 22:11:22 -07003931 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003934 memcpy(&st, &po->stats, sizeof(st));
3935 memset(&po->stats, 0, sizeof(po->stats));
3936 spin_unlock_bh(&sk->sk_receive_queue.lock);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003937 drops = atomic_xchg(&po->tp_drops, 0);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003938
chetan lokef6fb8f12011-08-19 10:18:16 +00003939 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003940 lv = sizeof(struct tpacket_stats_v3);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003941 st.stats3.tp_drops = drops;
3942 st.stats3.tp_packets += drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003943 data = &st.stats3;
chetan lokef6fb8f12011-08-19 10:18:16 +00003944 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003945 lv = sizeof(struct tpacket_stats);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003946 st.stats1.tp_drops = drops;
3947 st.stats1.tp_packets += drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003948 data = &st.stats1;
chetan lokef6fb8f12011-08-19 10:18:16 +00003949 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003952 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003953 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003954 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003955 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003956 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003957 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003958 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003959 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003960 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003961 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003962 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003963 break;
3964 case PACKET_HDRLEN:
3965 if (len > sizeof(int))
3966 len = sizeof(int);
Alexander Potapenkofd2c83b2017-04-25 18:51:46 +02003967 if (len < sizeof(int))
3968 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003969 if (copy_from_user(&val, optval, len))
3970 return -EFAULT;
3971 switch (val) {
3972 case TPACKET_V1:
3973 val = sizeof(struct tpacket_hdr);
3974 break;
3975 case TPACKET_V2:
3976 val = sizeof(struct tpacket2_hdr);
3977 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00003978 case TPACKET_V3:
3979 val = sizeof(struct tpacket3_hdr);
3980 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003981 default:
3982 return -EINVAL;
3983 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003984 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003985 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003986 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003987 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003988 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003989 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003990 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003991 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003992 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003993 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003994 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003995 val = (po->fanout ?
3996 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003997 ((u32)po->fanout->type << 16) |
3998 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003999 0);
David S. Millerdc99f602011-07-05 01:45:05 -07004000 break;
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02004001 case PACKET_IGNORE_OUTGOING:
4002 val = po->prot_hook.ignore_outgoing;
4003 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04004004 case PACKET_ROLLOVER_STATS:
Mike Maloney57f015f2017-11-28 10:44:29 -05004005 if (!po->rollover)
Willem de Bruijna9b63912015-05-12 11:56:50 -04004006 return -EINVAL;
Mike Maloney57f015f2017-11-28 10:44:29 -05004007 rstats.tp_all = atomic_long_read(&po->rollover->num);
4008 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
4009 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
4010 data = &rstats;
4011 lv = sizeof(rstats);
Willem de Bruijna9b63912015-05-12 11:56:50 -04004012 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00004013 case PACKET_TX_HAS_OFF:
4014 val = po->tp_tx_has_off;
4015 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01004016 case PACKET_QDISC_BYPASS:
4017 val = packet_use_direct_xmit(po);
4018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 default:
4020 return -ENOPROTOOPT;
4021 }
4022
Eric Dumazetc06fff62012-04-19 21:56:11 +00004023 if (len > lv)
4024 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004025 if (put_user(len, optlen))
4026 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08004027 if (copy_to_user(optval, data, len))
4028 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
4032
Willem de Bruijn719c44d2016-06-07 12:06:34 -04004033#ifdef CONFIG_COMPAT
4034static int compat_packet_setsockopt(struct socket *sock, int level, int optname,
4035 char __user *optval, unsigned int optlen)
4036{
4037 struct packet_sock *po = pkt_sk(sock->sk);
4038
4039 if (level != SOL_PACKET)
4040 return -ENOPROTOOPT;
4041
4042 if (optname == PACKET_FANOUT_DATA &&
4043 po->fanout && po->fanout->type == PACKET_FANOUT_CBPF) {
4044 optval = (char __user *)get_compat_bpf_fprog(optval);
4045 if (!optval)
4046 return -EFAULT;
4047 optlen = sizeof(struct sock_fprog);
4048 }
4049
4050 return packet_setsockopt(sock, level, optname, optval, optlen);
4051}
4052#endif
4053
Jiri Pirko351638e2013-05-28 01:30:21 +00004054static int packet_notifier(struct notifier_block *this,
4055 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
4057 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00004058 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004059 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
stephen hemminger808f5112010-02-22 07:57:18 +00004061 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004062 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 struct packet_sock *po = pkt_sk(sk);
4064
4065 switch (msg) {
4066 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07004068 packet_dev_mclist_delete(dev, &po->mclist);
David S. Millera2efcfa2007-05-29 13:12:50 -07004069 /* fallthrough */
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 case NETDEV_DOWN:
4072 if (dev->ifindex == po->ifindex) {
4073 spin_lock(&po->bind_lock);
4074 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07004075 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 sk->sk_err = ENETDOWN;
4077 if (!sock_flag(sk, SOCK_DEAD))
4078 sk->sk_error_report(sk);
4079 }
4080 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01004081 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00004083 if (po->prot_hook.dev)
4084 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 po->prot_hook.dev = NULL;
4086 }
4087 spin_unlock(&po->bind_lock);
4088 }
4089 break;
4090 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00004091 if (dev->ifindex == po->ifindex) {
4092 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004093 if (po->num)
4094 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00004095 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 break;
4098 }
4099 }
stephen hemminger808f5112010-02-22 07:57:18 +00004100 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return NOTIFY_DONE;
4102}
4103
4104
4105static int packet_ioctl(struct socket *sock, unsigned int cmd,
4106 unsigned long arg)
4107{
4108 struct sock *sk = sock->sk;
4109
Johann Baudy69e3c752009-05-18 22:11:22 -07004110 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004111 case SIOCOUTQ:
4112 {
4113 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07004114
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004115 return put_user(amount, (int __user *)arg);
4116 }
4117 case SIOCINQ:
4118 {
4119 struct sk_buff *skb;
4120 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004122 spin_lock_bh(&sk->sk_receive_queue.lock);
4123 skb = skb_peek(&sk->sk_receive_queue);
4124 if (skb)
4125 amount = skb->len;
4126 spin_unlock_bh(&sk->sk_receive_queue.lock);
4127 return put_user(amount, (int __user *)arg);
4128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004130 case SIOCADDRT:
4131 case SIOCDELRT:
4132 case SIOCDARP:
4133 case SIOCGARP:
4134 case SIOCSARP:
4135 case SIOCGIFADDR:
4136 case SIOCSIFADDR:
4137 case SIOCGIFBRDADDR:
4138 case SIOCSIFBRDADDR:
4139 case SIOCGIFNETMASK:
4140 case SIOCSIFNETMASK:
4141 case SIOCGIFDSTADDR:
4142 case SIOCSIFDSTADDR:
4143 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004144 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145#endif
4146
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004147 default:
4148 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
4150 return 0;
4151}
4152
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004153static __poll_t packet_poll(struct file *file, struct socket *sock,
4154 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
4156 struct sock *sk = sock->sk;
4157 struct packet_sock *po = pkt_sk(sk);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004158 __poll_t mask = datagram_poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004161 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004162 if (!packet_previous_rx_frame(po, &po->rx_ring,
4163 TP_STATUS_KERNEL))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004164 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07004166 packet_rcv_try_clear_pressure(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004168 spin_lock_bh(&sk->sk_write_queue.lock);
4169 if (po->tx_ring.pg_vec) {
4170 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004171 mask |= EPOLLOUT | EPOLLWRNORM;
Johann Baudy69e3c752009-05-18 22:11:22 -07004172 }
4173 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return mask;
4175}
4176
4177
4178/* Dirty? Well, I still did not learn better way to account
4179 * for user mmaps.
4180 */
4181
4182static void packet_mm_open(struct vm_area_struct *vma)
4183{
4184 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004185 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if (sk)
4189 atomic_inc(&pkt_sk(sk)->mapped);
4190}
4191
4192static void packet_mm_close(struct vm_area_struct *vma)
4193{
4194 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004195 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 if (sk)
4199 atomic_dec(&pkt_sk(sk)->mapped);
4200}
4201
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004202static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004203 .open = packet_mm_open,
4204 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205};
4206
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004207static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
4208 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
4210 int i;
4211
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004212 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004213 if (likely(pg_vec[i].buffer)) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004214 if (is_vmalloc_addr(pg_vec[i].buffer))
4215 vfree(pg_vec[i].buffer);
4216 else
4217 free_pages((unsigned long)pg_vec[i].buffer,
4218 order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004219 pg_vec[i].buffer = NULL;
4220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
4222 kfree(pg_vec);
4223}
4224
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004225static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004226{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004227 char *buffer;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004228 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
4229 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07004230
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004231 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004232 if (buffer)
4233 return buffer;
4234
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004235 /* __get_free_pages failed, fall back to vmalloc */
4236 buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
4237 if (buffer)
4238 return buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08004239
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004240 /* vmalloc failed, lets dig into swap here */
4241 gfp_flags &= ~__GFP_NORETRY;
4242 buffer = (char *) __get_free_pages(gfp_flags, order);
4243 if (buffer)
4244 return buffer;
4245
4246 /* complete and utter failure */
4247 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004248}
4249
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004250static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004251{
4252 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004253 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004254 int i;
4255
Christoph Paasch398f0132019-03-18 23:14:52 -07004256 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004257 if (unlikely(!pg_vec))
4258 goto out;
4259
4260 for (i = 0; i < block_nr; i++) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004261 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004262 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004263 goto out_free_pgvec;
4264 }
4265
4266out:
4267 return pg_vec;
4268
4269out_free_pgvec:
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004270 free_pg_vec(pg_vec, order, block_nr);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004271 pg_vec = NULL;
4272 goto out;
4273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
chetan lokef6fb8f12011-08-19 10:18:16 +00004275static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004276 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277{
Neil Horman0e3125c2010-11-16 10:26:47 -08004278 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 struct packet_sock *po = pkt_sk(sk);
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004280 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004281 struct packet_ring_buffer *rb;
4282 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004283 __be16 num;
chetan lokef6fb8f12011-08-19 10:18:16 +00004284 int err = -EINVAL;
4285 /* Added to avoid minimal code churn */
4286 struct tpacket_req *req = &req_u->req;
4287
Johann Baudy69e3c752009-05-18 22:11:22 -07004288 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4289 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4290
4291 err = -EBUSY;
4292 if (!closing) {
4293 if (atomic_read(&po->mapped))
4294 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004295 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004296 goto out;
4297 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 if (req->tp_block_nr) {
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004300 unsigned int min_frame_size;
4301
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004303 err = -EBUSY;
4304 if (unlikely(rb->pg_vec))
4305 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004307 switch (po->tp_version) {
4308 case TPACKET_V1:
4309 po->tp_hdrlen = TPACKET_HDRLEN;
4310 break;
4311 case TPACKET_V2:
4312 po->tp_hdrlen = TPACKET2_HDRLEN;
4313 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004314 case TPACKET_V3:
4315 po->tp_hdrlen = TPACKET3_HDRLEN;
4316 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004317 }
4318
Johann Baudy69e3c752009-05-18 22:11:22 -07004319 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004320 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004321 goto out;
Tobias Klauser90836b62015-11-17 10:40:21 +01004322 if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004323 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004324 min_frame_size = po->tp_hdrlen + po->tp_reserve;
Eric Dumazetdc808112014-08-15 09:16:04 -07004325 if (po->tp_version >= TPACKET_V3 &&
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004326 req->tp_block_size <
4327 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
Eric Dumazetdc808112014-08-15 09:16:04 -07004328 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004329 if (unlikely(req->tp_frame_size < min_frame_size))
Johann Baudy69e3c752009-05-18 22:11:22 -07004330 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004331 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Tobias Klauser4194b492015-11-17 10:38:36 +01004334 rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
4335 if (unlikely(rb->frames_per_block == 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004336 goto out;
Kal Conleyfc628142019-02-10 09:57:11 +01004337 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
Andrey Konovalov8f8d28e2017-03-29 16:11:21 +02004338 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004339 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4340 req->tp_frame_nr))
4341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 err = -ENOMEM;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004344 order = get_order(req->tp_block_size);
4345 pg_vec = alloc_pg_vec(req, order);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004346 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 goto out;
chetan lokef6fb8f12011-08-19 10:18:16 +00004348 switch (po->tp_version) {
4349 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004350 /* Block transmit is not supported yet */
4351 if (!tx_ring) {
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304352 init_prb_bdqc(po, rb, pg_vec, req_u);
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004353 } else {
4354 struct tpacket_req3 *req3 = &req_u->req3;
4355
4356 if (req3->tp_retire_blk_tov ||
4357 req3->tp_sizeof_priv ||
4358 req3->tp_feature_req_word) {
4359 err = -EINVAL;
Eric Dumazet55655e32019-06-24 02:38:20 -07004360 goto out_free_pg_vec;
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004361 }
4362 }
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004363 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004364 default:
4365 break;
4366 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004367 }
4368 /* Done */
4369 else {
4370 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004371 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 }
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376 /* Detach socket from network */
4377 spin_lock(&po->bind_lock);
4378 was_running = po->running;
4379 num = po->num;
4380 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004382 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 }
4384 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 synchronize_net();
4387
4388 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004389 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (closing || atomic_read(&po->mapped) == 0) {
4391 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004392 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004393 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07004394 rb->frame_max = (req->tp_frame_nr - 1);
4395 rb->head = 0;
4396 rb->frame_size = req->tp_frame_size;
4397 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004399 swap(rb->pg_vec_order, order);
Changli Gaoc053fd92010-12-10 16:02:20 -08004400 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Johann Baudy69e3c752009-05-18 22:11:22 -07004402 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4403 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4404 tpacket_rcv : packet_rcv;
4405 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004407 pr_err("packet_mmap: vma is busy: %d\n",
4408 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
Herbert Xu905db442009-01-30 14:12:06 -08004410 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004413 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004415 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
4417 spin_unlock(&po->bind_lock);
WANG Congc800aaf2017-07-24 10:07:32 -07004418 if (pg_vec && (po->tp_version > TPACKET_V2)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004419 /* Because we don't support block-based V3 on tx-ring */
4420 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004421 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f12011-08-19 10:18:16 +00004422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Eric Dumazet55655e32019-06-24 02:38:20 -07004424out_free_pg_vec:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (pg_vec)
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004426 free_pg_vec(pg_vec, order, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427out:
4428 return err;
4429}
4430
Johann Baudy69e3c752009-05-18 22:11:22 -07004431static int packet_mmap(struct file *file, struct socket *sock,
4432 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
4434 struct sock *sk = sock->sk;
4435 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004436 unsigned long size, expected_size;
4437 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 unsigned long start;
4439 int err = -EINVAL;
4440 int i;
4441
4442 if (vma->vm_pgoff)
4443 return -EINVAL;
4444
Herbert Xu905db442009-01-30 14:12:06 -08004445 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004446
4447 expected_size = 0;
4448 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4449 if (rb->pg_vec) {
4450 expected_size += rb->pg_vec_len
4451 * rb->pg_vec_pages
4452 * PAGE_SIZE;
4453 }
4454 }
4455
4456 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004458
4459 size = vma->vm_end - vma->vm_start;
4460 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 goto out;
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004464 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4465 if (rb->pg_vec == NULL)
4466 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004467
Johann Baudy69e3c752009-05-18 22:11:22 -07004468 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004469 struct page *page;
4470 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004471 int pg_num;
4472
Changli Gaoc56b4d92010-12-01 02:52:57 +00004473 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4474 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004475 err = vm_insert_page(vma, start, page);
4476 if (unlikely(err))
4477 goto out;
4478 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004479 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004480 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004483
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004484 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 vma->vm_ops = &packet_mmap_ops;
4486 err = 0;
4487
4488out:
Herbert Xu905db442009-01-30 14:12:06 -08004489 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return err;
4491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004493static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 .family = PF_PACKET,
4495 .owner = THIS_MODULE,
4496 .release = packet_release,
4497 .bind = packet_bind_spkt,
4498 .connect = sock_no_connect,
4499 .socketpair = sock_no_socketpair,
4500 .accept = sock_no_accept,
4501 .getname = packet_getname_spkt,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004502 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004504 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 .listen = sock_no_listen,
4506 .shutdown = sock_no_shutdown,
4507 .setsockopt = sock_no_setsockopt,
4508 .getsockopt = sock_no_getsockopt,
4509 .sendmsg = packet_sendmsg_spkt,
4510 .recvmsg = packet_recvmsg,
4511 .mmap = sock_no_mmap,
4512 .sendpage = sock_no_sendpage,
4513};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004515static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 .family = PF_PACKET,
4517 .owner = THIS_MODULE,
4518 .release = packet_release,
4519 .bind = packet_bind,
4520 .connect = sock_no_connect,
4521 .socketpair = sock_no_socketpair,
4522 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004523 .getname = packet_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004524 .poll = packet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004526 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 .listen = sock_no_listen,
4528 .shutdown = sock_no_shutdown,
4529 .setsockopt = packet_setsockopt,
4530 .getsockopt = packet_getsockopt,
Willem de Bruijn719c44d2016-06-07 12:06:34 -04004531#ifdef CONFIG_COMPAT
4532 .compat_setsockopt = compat_packet_setsockopt,
4533#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 .sendmsg = packet_sendmsg,
4535 .recvmsg = packet_recvmsg,
4536 .mmap = packet_mmap,
4537 .sendpage = sock_no_sendpage,
4538};
4539
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004540static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 .family = PF_PACKET,
4542 .create = packet_create,
4543 .owner = THIS_MODULE,
4544};
4545
4546static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004547 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548};
4549
4550#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004553 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
Denis V. Luneve372c412007-11-19 22:31:54 -08004555 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004556
4557 rcu_read_lock();
4558 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559}
4560
4561static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4562{
Herbert Xu1bf40952007-12-16 14:04:02 -08004563 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004564 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
4567static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004568 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
stephen hemminger808f5112010-02-22 07:57:18 +00004570 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004573static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
4575 if (v == SEQ_START_TOKEN)
4576 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
4577 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004578 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 const struct packet_sock *po = pkt_sk(s);
4580
4581 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004582 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004584 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 s->sk_type,
4586 ntohs(po->num),
4587 po->ifindex,
4588 po->running,
4589 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004590 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004591 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 }
4593
4594 return 0;
4595}
4596
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004597static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 .start = packet_seq_start,
4599 .next = packet_seq_next,
4600 .stop = packet_seq_stop,
4601 .show = packet_seq_show,
4602};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603#endif
4604
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004605static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004606{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004607 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004608 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004609
Christoph Hellwigc3506372018-04-10 19:42:55 +02004610 if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
4611 sizeof(struct seq_net_private)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004612 return -ENOMEM;
4613
4614 return 0;
4615}
4616
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004617static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004618{
Gao fengece31ff2013-02-18 01:34:56 +00004619 remove_proc_entry("packet", net->proc_net);
Vasily Averin669f8f12017-11-12 22:27:49 +03004620 WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004621}
4622
4623static struct pernet_operations packet_net_ops = {
4624 .init = packet_net_init,
4625 .exit = packet_net_exit,
4626};
4627
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629static void __exit packet_exit(void)
4630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004632 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 sock_unregister(PF_PACKET);
4634 proto_unregister(&packet_proto);
4635}
4636
4637static int __init packet_init(void)
4638{
YueHaibing36096f22019-05-09 22:52:20 +08004639 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
YueHaibing36096f22019-05-09 22:52:20 +08004641 rc = proto_register(&packet_proto, 0);
4642 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 goto out;
YueHaibing36096f22019-05-09 22:52:20 +08004644 rc = sock_register(&packet_family_ops);
4645 if (rc)
4646 goto out_proto;
4647 rc = register_pernet_subsys(&packet_net_ops);
4648 if (rc)
4649 goto out_sock;
4650 rc = register_netdevice_notifier(&packet_netdev_notifier);
4651 if (rc)
4652 goto out_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
YueHaibing36096f22019-05-09 22:52:20 +08004654 return 0;
4655
4656out_pernet:
4657 unregister_pernet_subsys(&packet_net_ops);
4658out_sock:
4659 sock_unregister(PF_PACKET);
4660out_proto:
4661 proto_unregister(&packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662out:
4663 return rc;
4664}
4665
4666module_init(packet_init);
4667module_exit(packet_exit);
4668MODULE_LICENSE("GPL");
4669MODULE_ALIAS_NETPROTO(PF_PACKET);