blob: 74e6e45a8e8435a556ce813c410a1f4146dd05b6 [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
Jakub Kicinskicc698372020-11-20 14:50:52 -080049#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080052#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/fcntl.h>
54#include <linux/socket.h>
55#include <linux/in.h>
56#include <linux/inet.h>
57#include <linux/netdevice.h>
58#include <linux/if_packet.h>
59#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080060#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090062#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080063#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020064#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/ip.h>
66#include <net/protocol.h>
67#include <linux/skbuff.h>
68#include <net/sock.h>
69#include <linux/errno.h>
70#include <linux/timer.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/ioctls.h>
73#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040074#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/io.h>
76#include <linux/proc_fs.h>
77#include <linux/seq_file.h>
78#include <linux/poll.h>
79#include <linux/module.h>
80#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080081#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070082#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080083#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000084#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070085#include <linux/net_tstamp.h>
Daniel Borkmannb0138402014-01-15 16:25:36 +010086#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#ifdef CONFIG_INET
88#include <net/inet_common.h>
89#endif
Willem de Bruijn47dceb82015-08-14 22:31:34 -040090#include <linux/bpf.h>
Willem de Bruijn719c44d2016-06-07 12:06:34 -040091#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Pavel Emelyanov2787b042012-08-13 05:49:39 +000093#include "internal.h"
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 Assumptions:
Eyal Birgerd5496992020-11-21 08:28:17 +020097 - If the device has no dev->header_ops->create, there is no LL header
98 visible above the device. In this case, its hard_header_len should be 0.
Xie Heb4c58812020-09-14 00:41:54 -070099 The device may prepend its own header internally. In this case, its
100 needed_headroom should be set to the space needed for it to add its
101 internal header.
102 For example, a WiFi driver pretending to be an Ethernet driver should
103 set its hard_header_len to be the Ethernet header length, and set its
104 needed_headroom to be (the real WiFi header length - the fake Ethernet
105 header length).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 - packet socket receives packets with pulled ll header,
107 so that SOCK_RAW should push it back.
108
109On receive:
110-----------
111
Eyal Birgerd5496992020-11-21 08:28:17 +0200112Incoming, dev_has_header(dev) == true
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700113 mac_header -> ll header
114 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Eyal Birgerd5496992020-11-21 08:28:17 +0200116Outgoing, dev_has_header(dev) == true
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700117 mac_header -> ll header
118 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Eyal Birgerd5496992020-11-21 08:28:17 +0200120Incoming, dev_has_header(dev) == false
Xie Heb79a80b2020-09-16 05:23:08 -0700121 mac_header -> data
122 However drivers often make it point to the ll header.
123 This is incorrect because the ll header should be invisible to us.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700124 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Eyal Birgerd5496992020-11-21 08:28:17 +0200126Outgoing, dev_has_header(dev) == false
Xie Heb79a80b2020-09-16 05:23:08 -0700127 mac_header -> data. ll header is invisible to us.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700128 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130Resume
Eyal Birgerd5496992020-11-21 08:28:17 +0200131 If dev_has_header(dev) == false we are unable to restore the ll header,
Xie Heb79a80b2020-09-16 05:23:08 -0700132 because it is invisible to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134
135On transmit:
136------------
137
Xie He21c859742021-02-05 14:41:24 -0800138dev_has_header(dev) == true
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700139 mac_header -> ll header
140 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Xie He21c859742021-02-05 14:41:24 -0800142dev_has_header(dev) == false (ll header is invisible to us)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700143 mac_header -> data
144 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Xie He09599722020-09-18 06:56:16 -0700146 We should set network_header on output to the correct position,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 packet classifier depends on it.
148 */
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Private packet socket structures. */
151
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700152/* identical to struct packet_mreq except it has
153 * a longer address field.
154 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000155struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700156 int mr_ifindex;
157 unsigned short mr_type;
158 unsigned short mr_alen;
159 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
David S. Millera2efcfa2007-05-29 13:12:50 -0700161
Daniel Borkmann184f4892013-04-16 01:57:46 +0000162union tpacket_uhdr {
163 struct tpacket_hdr *h1;
164 struct tpacket2_hdr *h2;
165 struct tpacket3_hdr *h3;
166 void *raw;
167};
168
chetan lokef6fb8f12011-08-19 10:18:16 +0000169static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700170 int closing, int tx_ring);
171
chetan lokef6fb8f12011-08-19 10:18:16 +0000172#define V3_ALIGNMENT (8)
173
chetan lokebc59ba32011-08-25 10:43:30 +0000174#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f12011-08-19 10:18:16 +0000175
176#define BLK_PLUS_PRIV(sz_of_priv) \
177 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
178
chetan lokef6fb8f12011-08-19 10:18:16 +0000179#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
180#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
181#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
182#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
183#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
184#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
chetan lokef6fb8f12011-08-19 10:18:16 +0000185
Johann Baudy69e3c752009-05-18 22:11:22 -0700186struct packet_sock;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000187static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
188 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
chetan lokef6fb8f12011-08-19 10:18:16 +0000190static void *packet_previous_frame(struct packet_sock *po,
191 struct packet_ring_buffer *rb,
192 int status);
193static void packet_increment_head(struct packet_ring_buffer *buff);
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300194static int prb_curr_blk_in_use(struct tpacket_block_desc *);
chetan lokebc59ba32011-08-25 10:43:30 +0000195static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000196 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000197static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000198 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000199static int prb_queue_frozen(struct tpacket_kbdq_core *);
200static void prb_open_block(struct tpacket_kbdq_core *,
201 struct tpacket_block_desc *);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700202static void prb_retire_rx_blk_timer_expired(struct timer_list *);
chetan lokebc59ba32011-08-25 10:43:30 +0000203static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
chetan lokebc59ba32011-08-25 10:43:30 +0000204static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
205static void prb_clear_rxhash(struct tpacket_kbdq_core *,
206 struct tpacket3_hdr *);
207static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
208 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static void packet_flush_mclist(struct sock *sk);
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200210static u16 packet_pick_tx_queue(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Herbert Xuffbc6112007-02-04 23:33:10 -0800212struct packet_skb_cb {
Herbert Xuffbc6112007-02-04 23:33:10 -0800213 union {
214 struct sockaddr_pkt pkt;
Eyal Birger2472d762015-03-01 14:58:28 +0200215 union {
216 /* Trick: alias skb original length with
217 * ll.sll_family and ll.protocol in order
218 * to save room.
219 */
220 unsigned int origlen;
221 struct sockaddr_ll ll;
222 };
Herbert Xuffbc6112007-02-04 23:33:10 -0800223 } sa;
224};
225
David Woodhoused3869ef2015-09-23 19:45:08 +0100226#define vio_le() virtio_legacy_is_little_endian()
227
Herbert Xuffbc6112007-02-04 23:33:10 -0800228#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800229
chetan lokebc59ba32011-08-25 10:43:30 +0000230#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f12011-08-19 10:18:16 +0000231#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000232 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000233#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000234 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000235#define GET_NEXT_PRB_BLK_NUM(x) \
236 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
237 ((x)->kactive_blk_num+1) : 0)
238
David S. Millerdc99f602011-07-05 01:45:05 -0700239static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
240static void __fanout_link(struct sock *sk, struct packet_sock *po);
241
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100242static int packet_direct_xmit(struct sk_buff *skb)
243{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200244 return dev_direct_xmit(skb, packet_pick_tx_queue(skb));
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100245}
246
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100247static struct net_device *packet_cached_dev_get(struct packet_sock *po)
248{
249 struct net_device *dev;
250
251 rcu_read_lock();
252 dev = rcu_dereference(po->cached_dev);
253 if (likely(dev))
254 dev_hold(dev);
255 rcu_read_unlock();
256
257 return dev;
258}
259
260static void packet_cached_dev_assign(struct packet_sock *po,
261 struct net_device *dev)
262{
263 rcu_assign_pointer(po->cached_dev, dev);
264}
265
266static void packet_cached_dev_reset(struct packet_sock *po)
267{
268 RCU_INIT_POINTER(po->cached_dev, NULL);
269}
270
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100271static bool packet_use_direct_xmit(const struct packet_sock *po)
272{
273 return po->xmit == packet_direct_xmit;
274}
275
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200276static u16 packet_pick_tx_queue(struct sk_buff *skb)
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100277{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200278 struct net_device *dev = skb->dev;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100279 const struct net_device_ops *ops = dev->netdev_ops;
Paolo Abenib71b5832019-03-20 11:02:05 +0100280 int cpu = raw_smp_processor_id();
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100281 u16 queue_index;
282
Paolo Abenib71b5832019-03-20 11:02:05 +0100283#ifdef CONFIG_XPS
284 skb->sender_cpu = cpu + 1;
285#endif
286 skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100287 if (ops->ndo_select_queue) {
Paolo Abenia350ecc2019-03-20 11:02:06 +0100288 queue_index = ops->ndo_select_queue(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100289 queue_index = netdev_cap_txqueue(dev, queue_index);
290 } else {
Paolo Abenib71b5832019-03-20 11:02:05 +0100291 queue_index = netdev_pick_tx(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100292 }
293
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200294 return queue_index;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100295}
296
Willem de Bruijna6361f02018-04-23 17:37:03 -0400297/* __register_prot_hook must be invoked through register_prot_hook
David S. Millerce06b032011-07-04 01:44:29 -0700298 * or from a context in which asynchronous accesses to the packet
299 * socket is not possible (packet_create()).
300 */
Willem de Bruijna6361f02018-04-23 17:37:03 -0400301static void __register_prot_hook(struct sock *sk)
David S. Millerce06b032011-07-04 01:44:29 -0700302{
303 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100304
David S. Millerce06b032011-07-04 01:44:29 -0700305 if (!po->running) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100306 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700307 __fanout_link(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100308 else
David S. Millerdc99f602011-07-05 01:45:05 -0700309 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100310
David S. Millerce06b032011-07-04 01:44:29 -0700311 sock_hold(sk);
312 po->running = 1;
313 }
314}
315
Willem de Bruijna6361f02018-04-23 17:37:03 -0400316static void register_prot_hook(struct sock *sk)
317{
318 lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
319 __register_prot_hook(sk);
320}
321
322/* If the sync parameter is true, we will temporarily drop
David S. Millerce06b032011-07-04 01:44:29 -0700323 * the po->bind_lock and do a synchronize_net to make sure no
324 * asynchronous packet processing paths still refer to the elements
325 * of po->prot_hook. If the sync parameter is false, it is the
326 * callers responsibility to take care of this.
327 */
328static void __unregister_prot_hook(struct sock *sk, bool sync)
329{
330 struct packet_sock *po = pkt_sk(sk);
331
Willem de Bruijna6361f02018-04-23 17:37:03 -0400332 lockdep_assert_held_once(&po->bind_lock);
333
David S. Millerce06b032011-07-04 01:44:29 -0700334 po->running = 0;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100335
336 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700337 __fanout_unlink(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100338 else
David S. Millerdc99f602011-07-05 01:45:05 -0700339 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100340
David S. Millerce06b032011-07-04 01:44:29 -0700341 __sock_put(sk);
342
343 if (sync) {
344 spin_unlock(&po->bind_lock);
345 synchronize_net();
346 spin_lock(&po->bind_lock);
347 }
348}
349
350static void unregister_prot_hook(struct sock *sk, bool sync)
351{
352 struct packet_sock *po = pkt_sk(sk);
353
354 if (po->running)
355 __unregister_prot_hook(sk, sync);
356}
357
Michael S. Tsirkin6e580402014-11-24 13:32:16 +0200358static inline struct page * __pure pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000359{
360 if (is_vmalloc_addr(addr))
361 return vmalloc_to_page(addr);
362 return virt_to_page(addr);
363}
364
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700365static void __packet_set_status(struct packet_sock *po, void *frame, int status)
366{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000367 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700368
369 h.raw = frame;
370 switch (po->tp_version) {
371 case TPACKET_V1:
372 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000373 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700374 break;
375 case TPACKET_V2:
376 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000377 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700378 break;
chetan lokef6fb8f12011-08-19 10:18:16 +0000379 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800380 h.h3->tp_status = status;
381 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
382 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700383 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000384 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700385 BUG();
386 }
387
388 smp_wmb();
389}
390
Eric Dumazet96f657e2019-06-12 09:52:26 -0700391static int __packet_get_status(const struct packet_sock *po, void *frame)
Johann Baudy69e3c752009-05-18 22:11:22 -0700392{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000393 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700394
395 smp_rmb();
396
397 h.raw = frame;
398 switch (po->tp_version) {
399 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000400 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700401 return h.h1->tp_status;
402 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000403 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700404 return h.h2->tp_status;
chetan lokef6fb8f12011-08-19 10:18:16 +0000405 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800406 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
407 return h.h3->tp_status;
Johann Baudy69e3c752009-05-18 22:11:22 -0700408 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000409 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700410 BUG();
411 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
Johann Baudy69e3c752009-05-18 22:11:22 -0700414
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100415static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts,
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000416 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000417{
418 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
419
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400420 if (shhwtstamps &&
421 (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100422 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts))
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400423 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000424
Richard Sanger171c3b12021-05-12 13:31:22 +1200425 if ((flags & SOF_TIMESTAMPING_SOFTWARE) &&
426 ktime_to_timespec64_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000427 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000428
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000429 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000430}
431
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000432static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
433 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000434{
435 union tpacket_uhdr h;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100436 struct timespec64 ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000437 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000438
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000439 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
440 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000441
442 h.raw = frame;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100443 /*
444 * versions 1 through 3 overflow the timestamps in y2106, since they
445 * all store the seconds in a 32-bit unsigned integer.
446 * If we create a version 4, that should have a 64-bit timestamp,
447 * either 64-bit seconds + 32-bit nanoseconds, or just 64-bit
448 * nanoseconds.
449 */
Willem de Bruijn2e313962013-04-23 00:39:28 +0000450 switch (po->tp_version) {
451 case TPACKET_V1:
452 h.h1->tp_sec = ts.tv_sec;
453 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
454 break;
455 case TPACKET_V2:
456 h.h2->tp_sec = ts.tv_sec;
457 h.h2->tp_nsec = ts.tv_nsec;
458 break;
459 case TPACKET_V3:
Daniel Borkmann57ea8842017-01-05 02:34:28 +0100460 h.h3->tp_sec = ts.tv_sec;
461 h.h3->tp_nsec = ts.tv_nsec;
462 break;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000463 default:
464 WARN(1, "TPACKET version not supported.\n");
465 BUG();
466 }
467
468 /* one flush is safe, as both fields always lie on the same cacheline */
469 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
470 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000471
472 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000473}
474
Eric Dumazetd4b5bd92019-06-12 09:52:27 -0700475static void *packet_lookup_frame(const struct packet_sock *po,
476 const struct packet_ring_buffer *rb,
477 unsigned int position,
478 int status)
Johann Baudy69e3c752009-05-18 22:11:22 -0700479{
480 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000481 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700482
483 pg_vec_pos = position / rb->frames_per_block;
484 frame_offset = position % rb->frames_per_block;
485
Neil Horman0e3125c2010-11-16 10:26:47 -0800486 h.raw = rb->pg_vec[pg_vec_pos].buffer +
487 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700488
489 if (status != __packet_get_status(po, h.raw))
490 return NULL;
491
492 return h.raw;
493}
494
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000495static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700496 struct packet_ring_buffer *rb,
497 int status)
498{
499 return packet_lookup_frame(po, rb, rb->head, status);
500}
501
chetan lokebc59ba32011-08-25 10:43:30 +0000502static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000503{
504 del_timer_sync(&pkc->retire_blk_timer);
505}
506
507static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +0000508 struct sk_buff_head *rb_queue)
509{
chetan lokebc59ba32011-08-25 10:43:30 +0000510 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000511
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200512 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000513
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100514 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000515 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100516 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000517
518 prb_del_retire_blk_timer(pkc);
519}
520
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530521static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f12011-08-19 10:18:16 +0000522{
chetan lokebc59ba32011-08-25 10:43:30 +0000523 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000524
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530525 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700526 timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired,
527 0);
528 pkc->retire_blk_timer.expires = jiffies;
chetan lokef6fb8f12011-08-19 10:18:16 +0000529}
530
531static int prb_calc_retire_blk_tmo(struct packet_sock *po,
532 int blk_size_in_bytes)
533{
534 struct net_device *dev;
Mao Wenan0914d2b2019-12-23 18:42:57 +0800535 unsigned int mbits, div;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800536 struct ethtool_link_ksettings ecmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000537 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +0000538
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000539 rtnl_lock();
540 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
541 if (unlikely(!dev)) {
542 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000543 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000544 }
David Decotigny7cad1ba2016-02-24 10:58:10 -0800545 err = __ethtool_get_link_ksettings(dev, &ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000546 rtnl_unlock();
Mao Wenan0914d2b2019-12-23 18:42:57 +0800547 if (err)
Mao Wenanb43d1f92019-12-09 21:31:25 +0800548 return DEFAULT_PRB_RETIRE_TOV;
chetan lokef6fb8f12011-08-19 10:18:16 +0000549
Mao Wenan0914d2b2019-12-23 18:42:57 +0800550 /* If the link speed is so slow you don't really
551 * need to worry about perf anyways
552 */
553 if (ecmd.base.speed < SPEED_1000 ||
554 ecmd.base.speed == SPEED_UNKNOWN)
555 return DEFAULT_PRB_RETIRE_TOV;
556
557 div = ecmd.base.speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000558 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
559
560 if (div)
561 mbits /= div;
562
chetan lokef6fb8f12011-08-19 10:18:16 +0000563 if (div)
Mao Wenan0914d2b2019-12-23 18:42:57 +0800564 return mbits + 1;
565 return mbits;
chetan lokef6fb8f12011-08-19 10:18:16 +0000566}
567
chetan lokebc59ba32011-08-25 10:43:30 +0000568static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000569 union tpacket_req_u *req_u)
570{
571 p1->feature_req_word = req_u->req3.tp_feature_req_word;
572}
573
574static void init_prb_bdqc(struct packet_sock *po,
575 struct packet_ring_buffer *rb,
576 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530577 union tpacket_req_u *req_u)
chetan lokef6fb8f12011-08-19 10:18:16 +0000578{
Duan Jiong22781a52013-12-06 13:29:36 +0800579 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000580 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000581
582 memset(p1, 0x0, sizeof(*p1));
583
584 p1->knxt_seq_num = 1;
585 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000586 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000587 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000588 p1->kblk_size = req_u->req3.tp_block_size;
589 p1->knum_blocks = req_u->req3.tp_block_nr;
590 p1->hdrlen = po->tp_hdrlen;
591 p1->version = po->tp_version;
592 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000593 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000594 if (req_u->req3.tp_retire_blk_tov)
595 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
596 else
597 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
598 req_u->req3.tp_block_size);
599 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
600 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
John Ogness632ca502020-07-07 17:28:04 +0206601 rwlock_init(&p1->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000602
Eric Dumazetdc808112014-08-15 09:16:04 -0700603 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000604 prb_init_ft_ops(p1, req_u);
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530605 prb_setup_retire_blk_timer(po);
chetan lokef6fb8f12011-08-19 10:18:16 +0000606 prb_open_block(p1, pbd);
607}
608
609/* Do NOT update the last_blk_num first.
610 * Assumes sk_buff_head lock is held.
611 */
chetan lokebc59ba32011-08-25 10:43:30 +0000612static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000613{
614 mod_timer(&pkc->retire_blk_timer,
615 jiffies + pkc->tov_in_jiffies);
616 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
617}
618
619/*
620 * Timer logic:
621 * 1) We refresh the timer only when we open a block.
622 * By doing this we don't waste cycles refreshing the timer
623 * on packet-by-packet basis.
624 *
625 * With a 1MB block-size, on a 1Gbps line, it will take
626 * i) ~8 ms to fill a block + ii) memcpy etc.
627 * In this cut we are not accounting for the memcpy time.
628 *
629 * So, if the user sets the 'tmo' to 10ms then the timer
630 * will never fire while the block is still getting filled
631 * (which is what we want). However, the user could choose
632 * to close a block early and that's fine.
633 *
634 * But when the timer does fire, we check whether or not to refresh it.
635 * Since the tmo granularity is in msecs, it is not too expensive
636 * to refresh the timer, lets say every '8' msecs.
637 * Either the user can set the 'tmo' or we can derive it based on
638 * a) line-speed and b) block-size.
639 * prb_calc_retire_blk_tmo() calculates the tmo.
640 *
641 */
Kees Cook17bfd8c2017-10-24 01:46:26 -0700642static void prb_retire_rx_blk_timer_expired(struct timer_list *t)
chetan lokef6fb8f12011-08-19 10:18:16 +0000643{
Kees Cook17bfd8c2017-10-24 01:46:26 -0700644 struct packet_sock *po =
645 from_timer(po, t, rx_ring.prb_bdqc.retire_blk_timer);
Duan Jiong22781a52013-12-06 13:29:36 +0800646 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000647 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000648 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000649
650 spin_lock(&po->sk.sk_receive_queue.lock);
651
652 frozen = prb_queue_frozen(pkc);
653 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
654
655 if (unlikely(pkc->delete_blk_timer))
656 goto out;
657
658 /* We only need to plug the race when the block is partially filled.
659 * tpacket_rcv:
660 * lock(); increment BLOCK_NUM_PKTS; unlock()
661 * copy_bits() is in progress ...
662 * timer fires on other cpu:
663 * we can't retire the current block because copy_bits
664 * is in progress.
665 *
666 */
667 if (BLOCK_NUM_PKTS(pbd)) {
John Ogness632ca502020-07-07 17:28:04 +0206668 /* Waiting for skb_copy_bits to finish... */
669 write_lock(&pkc->blk_fill_in_prog_lock);
670 write_unlock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000671 }
672
673 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
674 if (!frozen) {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300675 if (!BLOCK_NUM_PKTS(pbd)) {
676 /* An empty block. Just refresh the timer. */
677 goto refresh_timer;
678 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000679 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
680 if (!prb_dispatch_next_block(pkc, po))
681 goto refresh_timer;
682 else
683 goto out;
684 } else {
685 /* Case 1. Queue was frozen because user-space was
686 * lagging behind.
687 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300688 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +0000689 /*
690 * Ok, user-space is still behind.
691 * So just refresh the timer.
692 */
693 goto refresh_timer;
694 } else {
695 /* Case 2. queue was frozen,user-space caught up,
696 * now the link went idle && the timer fired.
697 * We don't have a block to close.So we open this
698 * block and restart the timer.
699 * opening a block thaws the queue,restarts timer
700 * Thawing/timer-refresh is a side effect.
701 */
702 prb_open_block(pkc, pbd);
703 goto out;
704 }
705 }
706 }
707
708refresh_timer:
709 _prb_refresh_rx_retire_blk_timer(pkc);
710
711out:
712 spin_unlock(&po->sk.sk_receive_queue.lock);
713}
714
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000715static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000716 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000717{
718 /* Flush everything minus the block header */
719
720#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
721 u8 *start, *end;
722
723 start = (u8 *)pbd1;
724
725 /* Skip the block header(we know header WILL fit in 4K) */
726 start += PAGE_SIZE;
727
728 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
729 for (; start < end; start += PAGE_SIZE)
730 flush_dcache_page(pgv_to_page(start));
731
732 smp_wmb();
733#endif
734
735 /* Now update the block status. */
736
737 BLOCK_STATUS(pbd1) = status;
738
739 /* Flush the block header */
740
741#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
742 start = (u8 *)pbd1;
743 flush_dcache_page(pgv_to_page(start));
744
745 smp_wmb();
746#endif
747}
748
749/*
750 * Side effect:
751 *
752 * 1) flush the block
753 * 2) Increment active_blk_num
754 *
755 * Note:We DONT refresh the timer on purpose.
756 * Because almost always the next block will be opened.
757 */
chetan lokebc59ba32011-08-25 10:43:30 +0000758static void prb_close_block(struct tpacket_kbdq_core *pkc1,
759 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000760 struct packet_sock *po, unsigned int stat)
761{
762 __u32 status = TP_STATUS_USER | stat;
763
764 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000765 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
Dan Collinsda413ee2014-12-19 16:49:25 +1300766 struct sock *sk = &po->sk;
chetan lokef6fb8f12011-08-19 10:18:16 +0000767
Eric Dumazet8e8e2952019-06-12 09:52:30 -0700768 if (atomic_read(&po->tp_drops))
chetan lokef6fb8f12011-08-19 10:18:16 +0000769 status |= TP_STATUS_LOSING;
770
771 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
772 last_pkt->tp_next_offset = 0;
773
774 /* Get the ts of the last pkt */
775 if (BLOCK_NUM_PKTS(pbd1)) {
776 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
777 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
778 } else {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300779 /* Ok, we tmo'd - so get the current time.
780 *
781 * It shouldn't really happen as we don't close empty
782 * blocks. See prb_retire_rx_blk_timer_expired().
783 */
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100784 struct timespec64 ts;
785 ktime_get_real_ts64(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000786 h1->ts_last_pkt.ts_sec = ts.tv_sec;
787 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
788 }
789
790 smp_wmb();
791
792 /* Flush the block */
793 prb_flush_block(pkc1, pbd1, status);
794
Dan Collinsda413ee2014-12-19 16:49:25 +1300795 sk->sk_data_ready(sk);
796
chetan lokef6fb8f12011-08-19 10:18:16 +0000797 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
798}
799
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000800static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000801{
802 pkc->reset_pending_on_curr_blk = 0;
803}
804
805/*
806 * Side effect of opening a block:
807 *
808 * 1) prb_queue is thawed.
809 * 2) retire_blk_timer is refreshed.
810 *
811 */
chetan lokebc59ba32011-08-25 10:43:30 +0000812static void prb_open_block(struct tpacket_kbdq_core *pkc1,
813 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000814{
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100815 struct timespec64 ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000816 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000817
818 smp_rmb();
819
Daniel Borkmann8da30562013-05-03 02:57:00 +0000820 /* We could have just memset this but we will lose the
821 * flexibility of making the priv area sticky
822 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000823
Daniel Borkmann8da30562013-05-03 02:57:00 +0000824 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
825 BLOCK_NUM_PKTS(pbd1) = 0;
826 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000827
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100828 ktime_get_real_ts64(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000829
Daniel Borkmann8da30562013-05-03 02:57:00 +0000830 h1->ts_first_pkt.ts_sec = ts.tv_sec;
831 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f12011-08-19 10:18:16 +0000832
Daniel Borkmann8da30562013-05-03 02:57:00 +0000833 pkc1->pkblk_start = (char *)pbd1;
834 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
835
836 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
837 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
838
839 pbd1->version = pkc1->version;
840 pkc1->prev = pkc1->nxt_offset;
841 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
842
843 prb_thaw_queue(pkc1);
844 _prb_refresh_rx_retire_blk_timer(pkc1);
845
846 smp_wmb();
chetan lokef6fb8f12011-08-19 10:18:16 +0000847}
848
849/*
850 * Queue freeze logic:
851 * 1) Assume tp_block_nr = 8 blocks.
852 * 2) At time 't0', user opens Rx ring.
853 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
854 * 4) user-space is either sleeping or processing block '0'.
855 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
856 * it will close block-7,loop around and try to fill block '0'.
857 * call-flow:
858 * __packet_lookup_frame_in_block
859 * prb_retire_current_block()
860 * prb_dispatch_next_block()
861 * |->(BLOCK_STATUS == USER) evaluates to true
862 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
863 * 6) Now there are two cases:
864 * 6.1) Link goes idle right after the queue is frozen.
865 * But remember, the last open_block() refreshed the timer.
866 * When this timer expires,it will refresh itself so that we can
867 * re-open block-0 in near future.
868 * 6.2) Link is busy and keeps on receiving packets. This is a simple
869 * case and __packet_lookup_frame_in_block will check if block-0
870 * is free and can now be re-used.
871 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000872static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000873 struct packet_sock *po)
874{
875 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000876 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f12011-08-19 10:18:16 +0000877}
878
879#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
880
881/*
882 * If the next block is free then we will dispatch it
883 * and return a good offset.
884 * Else, we will freeze the queue.
885 * So, caller must check the return value.
886 */
chetan lokebc59ba32011-08-25 10:43:30 +0000887static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000888 struct packet_sock *po)
889{
chetan lokebc59ba32011-08-25 10:43:30 +0000890 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000891
892 smp_rmb();
893
894 /* 1. Get current block num */
895 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
896
897 /* 2. If this block is currently in_use then freeze the queue */
898 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
899 prb_freeze_queue(pkc, po);
900 return NULL;
901 }
902
903 /*
904 * 3.
905 * open this block and return the offset where the first packet
906 * needs to get stored.
907 */
908 prb_open_block(pkc, pbd);
909 return (void *)pkc->nxt_offset;
910}
911
chetan lokebc59ba32011-08-25 10:43:30 +0000912static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000913 struct packet_sock *po, unsigned int status)
914{
chetan lokebc59ba32011-08-25 10:43:30 +0000915 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000916
917 /* retire/close the current block */
918 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
919 /*
920 * Plug the case where copy_bits() is in progress on
921 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
922 * have space to copy the pkt in the current block and
923 * called prb_retire_current_block()
924 *
925 * We don't need to worry about the TMO case because
926 * the timer-handler already handled this case.
927 */
928 if (!(status & TP_STATUS_BLK_TMO)) {
John Ogness632ca502020-07-07 17:28:04 +0206929 /* Waiting for skb_copy_bits to finish... */
930 write_lock(&pkc->blk_fill_in_prog_lock);
931 write_unlock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000932 }
933 prb_close_block(pkc, pbd, po, status);
934 return;
935 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000936}
937
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300938static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000939{
940 return TP_STATUS_USER & BLOCK_STATUS(pbd);
941}
942
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000943static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000944{
945 return pkc->reset_pending_on_curr_blk;
946}
947
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000948static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
John Ogness88fd1cb2020-08-13 21:45:25 +0206949 __releases(&pkc->blk_fill_in_prog_lock)
chetan lokef6fb8f12011-08-19 10:18:16 +0000950{
chetan lokebc59ba32011-08-25 10:43:30 +0000951 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
John Ogness632ca502020-07-07 17:28:04 +0206952
953 read_unlock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000954}
955
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000956static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000957 struct tpacket3_hdr *ppd)
958{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800959 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000960}
961
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000962static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000963 struct tpacket3_hdr *ppd)
964{
965 ppd->hv1.tp_rxhash = 0;
966}
967
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000968static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000969 struct tpacket3_hdr *ppd)
970{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100971 if (skb_vlan_tag_present(pkc->skb)) {
972 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900973 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
974 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
chetan lokef6fb8f12011-08-19 10:18:16 +0000975 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000976 ppd->hv1.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900977 ppd->hv1.tp_vlan_tpid = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000978 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +0000979 }
980}
981
chetan lokebc59ba32011-08-25 10:43:30 +0000982static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000983 struct tpacket3_hdr *ppd)
984{
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900985 ppd->hv1.tp_padding = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000986 prb_fill_vlan_info(pkc, ppd);
987
988 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
989 prb_fill_rxhash(pkc, ppd);
990 else
991 prb_clear_rxhash(pkc, ppd);
992}
993
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000994static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000995 struct tpacket_kbdq_core *pkc,
996 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +0000997 unsigned int len)
John Ogness88fd1cb2020-08-13 21:45:25 +0206998 __acquires(&pkc->blk_fill_in_prog_lock)
chetan lokef6fb8f12011-08-19 10:18:16 +0000999{
1000 struct tpacket3_hdr *ppd;
1001
1002 ppd = (struct tpacket3_hdr *)curr;
1003 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
1004 pkc->prev = curr;
1005 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
1006 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1007 BLOCK_NUM_PKTS(pbd) += 1;
John Ogness632ca502020-07-07 17:28:04 +02061008 read_lock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00001009 prb_run_all_ft_ops(pkc, ppd);
1010}
1011
1012/* Assumes caller has the sk->rx_queue.lock */
1013static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1014 struct sk_buff *skb,
chetan lokef6fb8f12011-08-19 10:18:16 +00001015 unsigned int len
1016 )
1017{
chetan lokebc59ba32011-08-25 10:43:30 +00001018 struct tpacket_kbdq_core *pkc;
1019 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +00001020 char *curr, *end;
1021
Joe Perchese3192692012-06-03 17:41:40 +00001022 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +00001023 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1024
1025 /* Queue is frozen when user space is lagging behind */
1026 if (prb_queue_frozen(pkc)) {
1027 /*
1028 * Check if that last block which caused the queue to freeze,
1029 * is still in_use by user-space.
1030 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +03001031 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001032 /* Can't record this packet */
1033 return NULL;
1034 } else {
1035 /*
1036 * Ok, the block was released by user-space.
1037 * Now let's open that block.
1038 * opening a block also thaws the queue.
1039 * Thawing is a side effect.
1040 */
1041 prb_open_block(pkc, pbd);
1042 }
1043 }
1044
1045 smp_mb();
1046 curr = pkc->nxt_offset;
1047 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +00001048 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +00001049
1050 /* first try the current block */
1051 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1052 prb_fill_curr_block(curr, pkc, pbd, len);
1053 return (void *)curr;
1054 }
1055
1056 /* Ok, close the current block */
1057 prb_retire_current_block(pkc, po, 0);
1058
1059 /* Now, try to dispatch the next block */
1060 curr = (char *)prb_dispatch_next_block(pkc, po);
1061 if (curr) {
1062 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1063 prb_fill_curr_block(curr, pkc, pbd, len);
1064 return (void *)curr;
1065 }
1066
1067 /*
1068 * No free blocks are available.user_space hasn't caught up yet.
1069 * Queue was just frozen and now this packet will get dropped.
1070 */
1071 return NULL;
1072}
1073
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001074static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001075 struct sk_buff *skb,
1076 int status, unsigned int len)
1077{
1078 char *curr = NULL;
1079 switch (po->tp_version) {
1080 case TPACKET_V1:
1081 case TPACKET_V2:
1082 curr = packet_lookup_frame(po, &po->rx_ring,
1083 po->rx_ring.head, status);
1084 return curr;
1085 case TPACKET_V3:
Mao Wenan46088052019-06-11 09:32:13 +08001086 return __packet_lookup_frame_in_block(po, skb, len);
chetan lokef6fb8f12011-08-19 10:18:16 +00001087 default:
1088 WARN(1, "TPACKET version not supported\n");
1089 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001090 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +00001091 }
1092}
1093
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001094static void *prb_lookup_block(const struct packet_sock *po,
1095 const struct packet_ring_buffer *rb,
1096 unsigned int idx,
1097 int status)
chetan lokef6fb8f12011-08-19 10:18:16 +00001098{
chetan lokebc59ba32011-08-25 10:43:30 +00001099 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001100 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001101
1102 if (status != BLOCK_STATUS(pbd))
1103 return NULL;
1104 return pbd;
1105}
1106
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001107static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001108{
1109 unsigned int prev;
1110 if (rb->prb_bdqc.kactive_blk_num)
1111 prev = rb->prb_bdqc.kactive_blk_num-1;
1112 else
1113 prev = rb->prb_bdqc.knum_blocks-1;
1114 return prev;
1115}
1116
1117/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001118static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001119 struct packet_ring_buffer *rb,
1120 int status)
1121{
1122 unsigned int previous = prb_previous_blk_num(rb);
1123 return prb_lookup_block(po, rb, previous, status);
1124}
1125
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001126static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001127 struct packet_ring_buffer *rb,
1128 int status)
1129{
1130 if (po->tp_version <= TPACKET_V2)
1131 return packet_previous_frame(po, rb, status);
1132
1133 return __prb_previous_block(po, rb, status);
1134}
1135
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001136static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001137 struct packet_ring_buffer *rb)
1138{
1139 switch (po->tp_version) {
1140 case TPACKET_V1:
1141 case TPACKET_V2:
1142 return packet_increment_head(rb);
1143 case TPACKET_V3:
1144 default:
1145 WARN(1, "TPACKET version not supported.\n");
1146 BUG();
1147 return;
1148 }
1149}
1150
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001151static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001152 struct packet_ring_buffer *rb,
1153 int status)
1154{
1155 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1156 return packet_lookup_frame(po, rb, previous, status);
1157}
1158
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001159static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001160{
1161 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1162}
1163
Daniel Borkmannb0138402014-01-15 16:25:36 +01001164static void packet_inc_pending(struct packet_ring_buffer *rb)
1165{
1166 this_cpu_inc(*rb->pending_refcnt);
1167}
1168
1169static void packet_dec_pending(struct packet_ring_buffer *rb)
1170{
1171 this_cpu_dec(*rb->pending_refcnt);
1172}
1173
1174static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1175{
1176 unsigned int refcnt = 0;
1177 int cpu;
1178
1179 /* We don't use pending refcount in rx_ring. */
1180 if (rb->pending_refcnt == NULL)
1181 return 0;
1182
1183 for_each_possible_cpu(cpu)
1184 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1185
1186 return refcnt;
1187}
1188
1189static int packet_alloc_pending(struct packet_sock *po)
1190{
1191 po->rx_ring.pending_refcnt = NULL;
1192
1193 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1194 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1195 return -ENOBUFS;
1196
1197 return 0;
1198}
1199
1200static void packet_free_pending(struct packet_sock *po)
1201{
1202 free_percpu(po->tx_ring.pending_refcnt);
1203}
1204
Willem de Bruijn99547292015-05-12 11:56:47 -04001205#define ROOM_POW_OFF 2
1206#define ROOM_NONE 0x0
1207#define ROOM_LOW 0x1
1208#define ROOM_NORMAL 0x2
1209
Eric Dumazetd4b5bd92019-06-12 09:52:27 -07001210static bool __tpacket_has_room(const struct packet_sock *po, int pow_off)
Willem de Bruijn99547292015-05-12 11:56:47 -04001211{
1212 int idx, len;
1213
Eric Dumazetd4b5bd92019-06-12 09:52:27 -07001214 len = READ_ONCE(po->rx_ring.frame_max) + 1;
1215 idx = READ_ONCE(po->rx_ring.head);
Willem de Bruijn99547292015-05-12 11:56:47 -04001216 if (pow_off)
1217 idx += len >> pow_off;
1218 if (idx >= len)
1219 idx -= len;
1220 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1221}
1222
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001223static bool __tpacket_v3_has_room(const struct packet_sock *po, int pow_off)
Willem de Bruijn99547292015-05-12 11:56:47 -04001224{
1225 int idx, len;
1226
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001227 len = READ_ONCE(po->rx_ring.prb_bdqc.knum_blocks);
1228 idx = READ_ONCE(po->rx_ring.prb_bdqc.kactive_blk_num);
Willem de Bruijn99547292015-05-12 11:56:47 -04001229 if (pow_off)
1230 idx += len >> pow_off;
1231 if (idx >= len)
1232 idx -= len;
1233 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1234}
1235
Eric Dumazet0338a142019-06-12 09:52:29 -07001236static int __packet_rcv_has_room(const struct packet_sock *po,
1237 const struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001238{
Eric Dumazet0338a142019-06-12 09:52:29 -07001239 const struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001240 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001241
Willem de Bruijn99547292015-05-12 11:56:47 -04001242 if (po->prot_hook.func != tpacket_rcv) {
Eric Dumazet0338a142019-06-12 09:52:29 -07001243 int rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1244 int avail = rcvbuf - atomic_read(&sk->sk_rmem_alloc)
1245 - (skb ? skb->truesize : 0);
1246
1247 if (avail > (rcvbuf >> ROOM_POW_OFF))
Willem de Bruijn99547292015-05-12 11:56:47 -04001248 return ROOM_NORMAL;
1249 else if (avail > 0)
1250 return ROOM_LOW;
1251 else
1252 return ROOM_NONE;
1253 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001254
Willem de Bruijn99547292015-05-12 11:56:47 -04001255 if (po->tp_version == TPACKET_V3) {
1256 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1257 ret = ROOM_NORMAL;
1258 else if (__tpacket_v3_has_room(po, 0))
1259 ret = ROOM_LOW;
1260 } else {
1261 if (__tpacket_has_room(po, ROOM_POW_OFF))
1262 ret = ROOM_NORMAL;
1263 else if (__tpacket_has_room(po, 0))
1264 ret = ROOM_LOW;
1265 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001266
1267 return ret;
1268}
1269
1270static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1271{
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001272 int pressure, ret;
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001273
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001274 ret = __packet_rcv_has_room(po, skb);
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001275 pressure = ret != ROOM_NORMAL;
1276
1277 if (READ_ONCE(po->pressure) != pressure)
1278 WRITE_ONCE(po->pressure, pressure);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001279
Willem de Bruijn99547292015-05-12 11:56:47 -04001280 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001281}
1282
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07001283static void packet_rcv_try_clear_pressure(struct packet_sock *po)
1284{
1285 if (READ_ONCE(po->pressure) &&
1286 __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
1287 WRITE_ONCE(po->pressure, 0);
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static void packet_sock_destruct(struct sock *sk)
1291{
Richard Cochraned85b562010-04-07 22:41:28 +00001292 skb_queue_purge(&sk->sk_error_queue);
1293
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001294 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +03001295 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001298 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return;
1300 }
1301
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001302 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001305static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1306{
Eric Dumazetb756ad92019-11-08 05:07:46 -08001307 u32 *history = po->rollover->history;
1308 u32 victim, rxhash;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001309 int i, count = 0;
1310
1311 rxhash = skb_get_hash(skb);
1312 for (i = 0; i < ROLLOVER_HLEN; i++)
Eric Dumazetb756ad92019-11-08 05:07:46 -08001313 if (READ_ONCE(history[i]) == rxhash)
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001314 count++;
1315
Eric Dumazetb756ad92019-11-08 05:07:46 -08001316 victim = prandom_u32() % ROLLOVER_HLEN;
1317
1318 /* Avoid dirtying the cache line if possible */
1319 if (READ_ONCE(history[victim]) != rxhash)
1320 WRITE_ONCE(history[victim], rxhash);
1321
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001322 return count > (ROLLOVER_HLEN >> 1);
1323}
1324
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001325static unsigned int fanout_demux_hash(struct packet_fanout *f,
1326 struct sk_buff *skb,
1327 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001328{
David S. Millereb70db82016-07-01 16:07:50 -04001329 return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001330}
1331
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001332static unsigned int fanout_demux_lb(struct packet_fanout *f,
1333 struct sk_buff *skb,
1334 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001335{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001336 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001337
Willem de Bruijn468479e2015-06-17 15:59:34 -04001338 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001339}
1340
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001341static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1342 struct sk_buff *skb,
1343 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001344{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001345 return smp_processor_id() % num;
1346}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001347
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001348static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1349 struct sk_buff *skb,
1350 unsigned int num)
1351{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001352 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001353}
1354
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001355static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1356 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001357 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001358 unsigned int num)
1359{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001360 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001361 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001362
Eric Dumazet94f633e2021-04-14 12:36:44 -07001363 po = pkt_sk(rcu_dereference(f->arr[idx]));
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001364
1365 if (try_self) {
1366 room = packet_rcv_has_room(po, skb);
1367 if (room == ROOM_NORMAL ||
1368 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1369 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001370 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001371 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001372
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001373 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001374 do {
Eric Dumazet94f633e2021-04-14 12:36:44 -07001375 po_next = pkt_sk(rcu_dereference(f->arr[i]));
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001376 if (po_next != po_skip && !READ_ONCE(po_next->pressure) &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001377 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001378 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001379 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001380 atomic_long_inc(&po->rollover->num);
1381 if (room == ROOM_LOW)
1382 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001383 return i;
1384 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001385
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001386 if (++i == num)
1387 i = 0;
1388 } while (i != j);
1389
Willem de Bruijna9b63912015-05-12 11:56:50 -04001390 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001391 return idx;
1392}
1393
Neil Horman2d360972014-01-22 16:01:44 -05001394static unsigned int fanout_demux_qm(struct packet_fanout *f,
1395 struct sk_buff *skb,
1396 unsigned int num)
1397{
1398 return skb_get_queue_mapping(skb) % num;
1399}
1400
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001401static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1402 struct sk_buff *skb,
1403 unsigned int num)
1404{
1405 struct bpf_prog *prog;
1406 unsigned int ret = 0;
1407
1408 rcu_read_lock();
1409 prog = rcu_dereference(f->bpf_prog);
1410 if (prog)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001411 ret = bpf_prog_run_clear_cb(prog, skb) % num;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001412 rcu_read_unlock();
1413
1414 return ret;
1415}
1416
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001417static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1418{
1419 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001420}
1421
David S. Miller95ec3eb2011-07-06 01:56:38 -07001422static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1423 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001424{
1425 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001426 unsigned int num = READ_ONCE(f->num_members);
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001427 struct net *net = read_pnet(&f->net);
David S. Millerdc99f602011-07-05 01:45:05 -07001428 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001429 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001430
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001431 if (!net_eq(dev_net(dev), net) || !num) {
David S. Millerdc99f602011-07-05 01:45:05 -07001432 kfree_skb(skb);
1433 return 0;
1434 }
1435
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001436 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001437 skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001438 if (!skb)
1439 return 0;
1440 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001441 switch (f->type) {
1442 case PACKET_FANOUT_HASH:
1443 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001444 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001445 break;
1446 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001447 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001448 break;
1449 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001450 idx = fanout_demux_cpu(f, skb, num);
1451 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001452 case PACKET_FANOUT_RND:
1453 idx = fanout_demux_rnd(f, skb, num);
1454 break;
Neil Horman2d360972014-01-22 16:01:44 -05001455 case PACKET_FANOUT_QM:
1456 idx = fanout_demux_qm(f, skb, num);
1457 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001458 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001459 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001460 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001461 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001462 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001463 idx = fanout_demux_bpf(f, skb, num);
1464 break;
David S. Miller7736d332011-07-05 01:43:20 -07001465 }
1466
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001467 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1468 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001469
Eric Dumazet94f633e2021-04-14 12:36:44 -07001470 po = pkt_sk(rcu_dereference(f->arr[idx]));
David S. Millerdc99f602011-07-05 01:45:05 -07001471 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1472}
1473
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001474DEFINE_MUTEX(fanout_mutex);
1475EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001476static LIST_HEAD(fanout_list);
Mike Maloney4a69a862017-04-21 10:56:11 -04001477static u16 fanout_next_id;
David S. Millerdc99f602011-07-05 01:45:05 -07001478
1479static void __fanout_link(struct sock *sk, struct packet_sock *po)
1480{
1481 struct packet_fanout *f = po->fanout;
1482
1483 spin_lock(&f->lock);
Eric Dumazet94f633e2021-04-14 12:36:44 -07001484 rcu_assign_pointer(f->arr[f->num_members], sk);
David S. Millerdc99f602011-07-05 01:45:05 -07001485 smp_wmb();
1486 f->num_members++;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001487 if (f->num_members == 1)
1488 dev_add_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001489 spin_unlock(&f->lock);
1490}
1491
1492static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1493{
1494 struct packet_fanout *f = po->fanout;
1495 int i;
1496
1497 spin_lock(&f->lock);
1498 for (i = 0; i < f->num_members; i++) {
Eric Dumazet94f633e2021-04-14 12:36:44 -07001499 if (rcu_dereference_protected(f->arr[i],
1500 lockdep_is_held(&f->lock)) == sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001501 break;
1502 }
1503 BUG_ON(i >= f->num_members);
Eric Dumazet94f633e2021-04-14 12:36:44 -07001504 rcu_assign_pointer(f->arr[i],
1505 rcu_dereference_protected(f->arr[f->num_members - 1],
1506 lockdep_is_held(&f->lock)));
David S. Millerdc99f602011-07-05 01:45:05 -07001507 f->num_members--;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001508 if (f->num_members == 0)
1509 __dev_remove_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001510 spin_unlock(&f->lock);
1511}
1512
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001513static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001514{
Eric Dumazet161642e2015-10-09 11:29:32 -07001515 if (sk->sk_family != PF_PACKET)
1516 return false;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001517
Eric Dumazet161642e2015-10-09 11:29:32 -07001518 return ptype->af_packet_priv == pkt_sk(sk)->fanout;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001519}
1520
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001521static void fanout_init_data(struct packet_fanout *f)
1522{
1523 switch (f->type) {
1524 case PACKET_FANOUT_LB:
1525 atomic_set(&f->rr_cur, 0);
1526 break;
1527 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001528 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001529 RCU_INIT_POINTER(f->bpf_prog, NULL);
1530 break;
1531 }
1532}
1533
1534static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1535{
1536 struct bpf_prog *old;
1537
1538 spin_lock(&f->lock);
1539 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1540 rcu_assign_pointer(f->bpf_prog, new);
1541 spin_unlock(&f->lock);
1542
1543 if (old) {
1544 synchronize_net();
1545 bpf_prog_destroy(old);
1546 }
1547}
1548
Christoph Hellwigb1ea9ff2020-07-23 08:08:47 +02001549static int fanout_set_data_cbpf(struct packet_sock *po, sockptr_t data,
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001550 unsigned int len)
1551{
1552 struct bpf_prog *new;
1553 struct sock_fprog fprog;
1554 int ret;
1555
1556 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1557 return -EPERM;
Christoph Hellwig4d295e52020-07-17 08:23:13 +02001558
1559 ret = copy_bpf_fprog_from_user(&fprog, data, len);
1560 if (ret)
1561 return ret;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001562
Daniel Borkmannbab18992015-10-02 15:17:33 +02001563 ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001564 if (ret)
1565 return ret;
1566
1567 __fanout_set_data_bpf(po->fanout, new);
1568 return 0;
1569}
1570
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001571static int fanout_set_data_ebpf(struct packet_sock *po, sockptr_t data,
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001572 unsigned int len)
1573{
1574 struct bpf_prog *new;
1575 u32 fd;
1576
1577 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1578 return -EPERM;
1579 if (len != sizeof(fd))
1580 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001581 if (copy_from_sockptr(&fd, data, len))
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001582 return -EFAULT;
1583
Daniel Borkmann113214b2016-06-30 17:24:44 +02001584 new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001585 if (IS_ERR(new))
1586 return PTR_ERR(new);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001587
1588 __fanout_set_data_bpf(po->fanout, new);
1589 return 0;
1590}
1591
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001592static int fanout_set_data(struct packet_sock *po, sockptr_t data,
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001593 unsigned int len)
1594{
1595 switch (po->fanout->type) {
1596 case PACKET_FANOUT_CBPF:
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001597 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001598 case PACKET_FANOUT_EBPF:
1599 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001600 default:
1601 return -EINVAL;
zhong jiang07d53ae2018-08-04 19:41:41 +08001602 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001603}
1604
1605static void fanout_release_data(struct packet_fanout *f)
1606{
1607 switch (f->type) {
1608 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001609 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001610 __fanout_set_data_bpf(f, NULL);
zhong jiang07d53ae2018-08-04 19:41:41 +08001611 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001612}
1613
Mike Maloney4a69a862017-04-21 10:56:11 -04001614static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
1615{
1616 struct packet_fanout *f;
1617
1618 list_for_each_entry(f, &fanout_list, list) {
1619 if (f->id == candidate_id &&
1620 read_pnet(&f->net) == sock_net(sk)) {
1621 return false;
1622 }
1623 }
1624 return true;
1625}
1626
1627static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
1628{
1629 u16 id = fanout_next_id;
1630
1631 do {
1632 if (__fanout_id_is_free(sk, id)) {
1633 *new_id = id;
1634 fanout_next_id = id + 1;
1635 return true;
1636 }
1637
1638 id++;
1639 } while (id != fanout_next_id);
1640
1641 return false;
1642}
1643
Tanner Love9c661b02020-11-06 13:07:40 -05001644static int fanout_add(struct sock *sk, struct fanout_args *args)
David S. Millerdc99f602011-07-05 01:45:05 -07001645{
Eric Dumazetd199fab2017-02-14 09:03:51 -08001646 struct packet_rollover *rollover = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001647 struct packet_sock *po = pkt_sk(sk);
Tanner Love9c661b02020-11-06 13:07:40 -05001648 u16 type_flags = args->type_flags;
David S. Millerdc99f602011-07-05 01:45:05 -07001649 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001650 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001651 u8 flags = type_flags >> 8;
Tanner Love9c661b02020-11-06 13:07:40 -05001652 u16 id = args->id;
David S. Millerdc99f602011-07-05 01:45:05 -07001653 int err;
1654
1655 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001656 case PACKET_FANOUT_ROLLOVER:
1657 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1658 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001659 case PACKET_FANOUT_HASH:
1660 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001661 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001662 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001663 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001664 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001665 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001666 break;
1667 default:
1668 return -EINVAL;
1669 }
1670
Eric Dumazetd199fab2017-02-14 09:03:51 -08001671 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001672
Eric Dumazetd199fab2017-02-14 09:03:51 -08001673 err = -EALREADY;
David S. Millerdc99f602011-07-05 01:45:05 -07001674 if (po->fanout)
Eric Dumazetd199fab2017-02-14 09:03:51 -08001675 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001676
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001677 if (type == PACKET_FANOUT_ROLLOVER ||
1678 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Eric Dumazetd199fab2017-02-14 09:03:51 -08001679 err = -ENOMEM;
1680 rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
1681 if (!rollover)
1682 goto out;
1683 atomic_long_set(&rollover->num, 0);
1684 atomic_long_set(&rollover->num_huge, 0);
1685 atomic_long_set(&rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001686 }
1687
Mike Maloney4a69a862017-04-21 10:56:11 -04001688 if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
1689 if (id != 0) {
1690 err = -EINVAL;
1691 goto out;
1692 }
1693 if (!fanout_find_new_id(sk, &id)) {
1694 err = -ENOMEM;
1695 goto out;
1696 }
1697 /* ephemeral flag for the first socket in the group: drop it */
1698 flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
1699 }
1700
David S. Millerdc99f602011-07-05 01:45:05 -07001701 match = NULL;
1702 list_for_each_entry(f, &fanout_list, list) {
1703 if (f->id == id &&
1704 read_pnet(&f->net) == sock_net(sk)) {
1705 match = f;
1706 break;
1707 }
1708 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001709 err = -EINVAL;
Tanner Love9c661b02020-11-06 13:07:40 -05001710 if (match) {
1711 if (match->flags != flags)
1712 goto out;
1713 if (args->max_num_members &&
1714 args->max_num_members != match->max_num_members)
1715 goto out;
1716 } else {
1717 if (args->max_num_members > PACKET_FANOUT_MAX)
1718 goto out;
1719 if (!args->max_num_members)
1720 /* legacy PACKET_FANOUT_MAX */
1721 args->max_num_members = 256;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001722 err = -ENOMEM;
Tanner Love9c661b02020-11-06 13:07:40 -05001723 match = kvzalloc(struct_size(match, arr, args->max_num_members),
1724 GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001725 if (!match)
1726 goto out;
1727 write_pnet(&match->net, sock_net(sk));
1728 match->id = id;
1729 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001730 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001731 INIT_LIST_HEAD(&match->list);
1732 spin_lock_init(&match->lock);
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001733 refcount_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001734 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001735 match->prot_hook.type = po->prot_hook.type;
1736 match->prot_hook.dev = po->prot_hook.dev;
1737 match->prot_hook.func = packet_rcv_fanout;
1738 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001739 match->prot_hook.id_match = match_fanout_group;
Tanner Love9c661b02020-11-06 13:07:40 -05001740 match->max_num_members = args->max_num_members;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001741 list_add(&match->list, &fanout_list);
1742 }
1743 err = -EINVAL;
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001744
1745 spin_lock(&po->bind_lock);
1746 if (po->running &&
1747 match->type == type &&
Eric Dumazetafe62c62011-07-07 06:41:29 -07001748 match->prot_hook.type == po->prot_hook.type &&
1749 match->prot_hook.dev == po->prot_hook.dev) {
1750 err = -ENOSPC;
Tanner Love9c661b02020-11-06 13:07:40 -05001751 if (refcount_read(&match->sk_ref) < match->max_num_members) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001752 __dev_remove_pack(&po->prot_hook);
1753 po->fanout = match;
Mike Maloney57f015f2017-11-28 10:44:29 -05001754 po->rollover = rollover;
1755 rollover = NULL;
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001756 refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001757 __fanout_link(sk, po);
1758 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001759 }
1760 }
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001761 spin_unlock(&po->bind_lock);
1762
1763 if (err && !refcount_read(&match->sk_ref)) {
1764 list_del(&match->list);
Tanner Love9c661b02020-11-06 13:07:40 -05001765 kvfree(match);
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001766 }
1767
Eric Dumazetafe62c62011-07-07 06:41:29 -07001768out:
Mike Maloney57f015f2017-11-28 10:44:29 -05001769 kfree(rollover);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001770 mutex_unlock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001771 return err;
1772}
1773
Anoob Soman2bd624b2017-02-15 20:25:39 +00001774/* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
1775 * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
1776 * It is the responsibility of the caller to call fanout_release_data() and
1777 * free the returned packet_fanout (after synchronize_net())
1778 */
1779static struct packet_fanout *fanout_release(struct sock *sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001780{
1781 struct packet_sock *po = pkt_sk(sk);
1782 struct packet_fanout *f;
1783
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001784 mutex_lock(&fanout_mutex);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001785 f = po->fanout;
1786 if (f) {
1787 po->fanout = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001788
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001789 if (refcount_dec_and_test(&f->sk_ref))
Eric Dumazetd199fab2017-02-14 09:03:51 -08001790 list_del(&f->list);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001791 else
1792 f = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001793 }
1794 mutex_unlock(&fanout_mutex);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001795
1796 return f;
David S. Millerdc99f602011-07-05 01:45:05 -07001797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001799static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
1800 struct sk_buff *skb)
1801{
1802 /* Earlier code assumed this would be a VLAN pkt, double-check
1803 * this now that we have the actual packet in hand. We can only
1804 * do this check on Ethernet devices.
1805 */
1806 if (unlikely(dev->type != ARPHRD_ETHER))
1807 return false;
1808
1809 skb_reset_mac_header(skb);
1810 return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
1811}
1812
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001813static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001815static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001817static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1818 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 struct sock *sk;
1821 struct sockaddr_pkt *spkt;
1822
1823 /*
1824 * When we registered the protocol we saved the socket in the data
1825 * field for just this event.
1826 */
1827
1828 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * Yank back the headers [hope the device set this
1832 * right or kerboom...]
1833 *
1834 * Incoming packets have ll header pulled,
1835 * push it back.
1836 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001837 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * so that this procedure is noop.
1839 */
1840
1841 if (skb->pkt_type == PACKET_LOOPBACK)
1842 goto out;
1843
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001844 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001845 goto out;
1846
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001847 skb = skb_share_check(skb, GFP_ATOMIC);
1848 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto oom;
1850
1851 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001852 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Phil Oester84531c22005-07-12 11:57:52 -07001854 /* drop conntrack reference */
Florian Westphal895b5c92019-09-29 20:54:03 +02001855 nf_reset_ct(skb);
Phil Oester84531c22005-07-12 11:57:52 -07001856
Herbert Xuffbc6112007-02-04 23:33:10 -08001857 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001859 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /*
1862 * The SOCK_PACKET socket receives _all_ frames.
1863 */
1864
1865 spkt->spkt_family = dev->type;
1866 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1867 spkt->spkt_protocol = skb->protocol;
1868
1869 /*
1870 * Charge the memory to the socket. This is done specifically
1871 * to prevent sockets using all the memory up.
1872 */
1873
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001874 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 0;
1876
1877out:
1878 kfree_skb(skb);
1879oom:
1880 return 0;
1881}
1882
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001883static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
1884{
Yoshiki Komachi18bed892019-03-18 14:39:52 +09001885 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
1886 sock->type == SOCK_RAW) {
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001887 skb_reset_mac_header(skb);
1888 skb->protocol = dev_parse_header_protocol(skb);
1889 }
1890
1891 skb_probe_transport_header(skb);
1892}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894/*
1895 * Output a raw packet to a device layer. This bypasses all the other
1896 * protocol layers and you must therefore supply it with a complete frame
1897 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001898
Ying Xue1b784142015-03-02 15:37:48 +08001899static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1900 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001903 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001904 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct net_device *dev;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001906 struct sockcm_cookie sockc;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001907 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001909 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001912 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
1914
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001915 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001917 return -EINVAL;
1918 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1919 proto = saddr->spkt_protocol;
1920 } else
1921 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001924 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 */
1926
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001927 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001928retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001929 rcu_read_lock();
1930 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 err = -ENODEV;
1932 if (dev == NULL)
1933 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001934
David S. Millerd5e76b02007-01-25 19:30:36 -08001935 err = -ENETDOWN;
1936 if (!(dev->flags & IFF_UP))
1937 goto out_unlock;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001940 * You may not queue a frame bigger than the mtu. This is the lowest level
1941 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001943
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001944 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1945 if (!netif_supports_nofcs(dev)) {
1946 err = -EPROTONOSUPPORT;
1947 goto out_unlock;
1948 }
1949 extra_len = 4; /* We're doing our own CRC */
1950 }
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001953 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto out_unlock;
1955
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001956 if (!skb) {
1957 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001958 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001959 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001961 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001962 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001963 if (skb == NULL)
1964 return -ENOBUFS;
1965 /* FIXME: Save some space for broken drivers that write a hard
1966 * header at transmission time by themselves. PPP is the notable
1967 * one here. This should really be fixed at the driver level.
1968 */
1969 skb_reserve(skb, reserved);
1970 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001971
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001972 /* Try to align data part correctly */
1973 if (hhlen) {
1974 skb->data -= hhlen;
1975 skb->tail -= hhlen;
1976 if (len < hhlen)
1977 skb_reset_network_header(skb);
1978 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001979 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001980 if (err)
1981 goto out_free;
1982 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05001985 if (!dev_validate_header(dev, skb->data, len)) {
1986 err = -EINVAL;
1987 goto out_unlock;
1988 }
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001989 if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
1990 !packet_extra_vlan_len_allowed(dev, skb)) {
1991 err = -EMSGSIZE;
1992 goto out_unlock;
Ben Greear57f89bf2011-02-11 09:35:18 +00001993 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001994
Willem de Bruijn657a0662018-07-06 10:12:56 -04001995 sockcm_init(&sockc, sk);
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001996 if (msg->msg_controllen) {
1997 err = sock_cmsg_send(sk, msg, &sockc);
Soheil Hassas Yeganehf8e77182016-07-20 18:01:18 -04001998 if (unlikely(err))
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001999 goto out_unlock;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002000 }
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 skb->protocol = proto;
2003 skb->dev = dev;
2004 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002005 skb->mark = sk->sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002006 skb->tstamp = sockc.transmit_time;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002007
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002008 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002010 if (unlikely(extra_len == 4))
2011 skb->no_fcs = 1;
2012
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002013 packet_parse_headers(skb, sock);
Jason Wangc1aad272013-03-25 20:19:57 +00002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002016 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002017 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01002020 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00002021out_free:
2022 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return err;
2024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002026static unsigned int run_filter(struct sk_buff *skb,
2027 const struct sock *sk,
2028 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct sk_filter *filter;
2031
Eric Dumazet80f8f102011-01-18 07:46:52 +00002032 rcu_read_lock();
2033 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08002034 if (filter != NULL)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002035 res = bpf_prog_run_clear_cb(filter->prog, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00002036 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
David S. Millerdbcb5852007-01-24 15:21:02 -08002038 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002041static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
2042 size_t *len)
2043{
2044 struct virtio_net_hdr vnet_hdr;
2045
2046 if (*len < sizeof(vnet_hdr))
2047 return -EINVAL;
2048 *len -= sizeof(vnet_hdr);
2049
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002050 if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002051 return -EINVAL;
2052
2053 return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
2054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00002057 * This function makes lazy skb cloning in hope that most of packets
2058 * are discarded by BPF.
2059 *
2060 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
2061 * and skb->cb are mangled. It works because (and until) packets
2062 * falling here are owned by current CPU. Output packets are cloned
2063 * by dev_queue_xmit_nit(), input packets are processed by net_bh
Wang Hai0e4161d2021-03-24 14:19:31 +08002064 * sequentially, so that if we return skb to original state on exit,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002065 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
2067
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002068static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
2069 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 struct sock *sk;
2072 struct sockaddr_ll *sll;
2073 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002074 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002076 unsigned int snaplen, res;
Weongyo Jeongda378452016-04-14 14:10:04 -07002077 bool is_drop_n_account = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 if (skb->pkt_type == PACKET_LOOPBACK)
2080 goto drop;
2081
2082 sk = pt->af_packet_priv;
2083 po = pkt_sk(sk);
2084
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002085 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002086 goto drop;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 skb->dev = dev;
2089
Eyal Birgerd5496992020-11-21 08:28:17 +02002090 if (dev_has_header(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002092 * exported to higher levels.
2093 *
2094 * Otherwise, the device hides details of its frame
2095 * structure, so that corresponding packet head is
2096 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
2098 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002099 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 else if (skb->pkt_type == PACKET_OUTGOING) {
2101 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002102 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104 }
2105
2106 snaplen = skb->len;
2107
David S. Millerdbcb5852007-01-24 15:21:02 -08002108 res = run_filter(skb, sk, snaplen);
2109 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002110 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08002111 if (snaplen > res)
2112 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002114 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto drop_n_acct;
2116
2117 if (skb_shared(skb)) {
2118 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2119 if (nskb == NULL)
2120 goto drop_n_acct;
2121
2122 if (skb_head != skb->data) {
2123 skb->data = skb_head;
2124 skb->len = skb_len;
2125 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00002126 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 skb = nskb;
2128 }
2129
Eyal Birgerb4772ef2015-03-01 14:58:29 +02002130 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08002131
2132 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002135 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002136 sll->sll_ifindex = orig_dev->ifindex;
2137 else
2138 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002140 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Eyal Birger2472d762015-03-01 14:58:28 +02002142 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2143 * Use their space for storing the original skb length.
2144 */
2145 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (pskb_trim(skb, snaplen))
2148 goto drop_n_acct;
2149
2150 skb_set_owner_r(skb, sk);
2151 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002152 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Phil Oester84531c22005-07-12 11:57:52 -07002154 /* drop conntrack reference */
Florian Westphal895b5c92019-09-29 20:54:03 +02002155 nf_reset_ct(skb);
Phil Oester84531c22005-07-12 11:57:52 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002158 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002159 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 __skb_queue_tail(&sk->sk_receive_queue, skb);
2161 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002162 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return 0;
2164
2165drop_n_acct:
Weongyo Jeongda378452016-04-14 14:10:04 -07002166 is_drop_n_account = true;
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002167 atomic_inc(&po->tp_drops);
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002168 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170drop_n_restore:
2171 if (skb_head != skb->data && skb_shared(skb)) {
2172 skb->data = skb_head;
2173 skb->len = skb_len;
2174 }
2175drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002176 if (!is_drop_n_account)
2177 consume_skb(skb);
2178 else
2179 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return 0;
2181}
2182
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002183static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2184 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct sock *sk;
2187 struct packet_sock *po;
2188 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002189 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002190 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002192 unsigned int snaplen, res;
chetan lokef6fb8f12011-08-19 10:18:16 +00002193 unsigned long status = TP_STATUS_USER;
Or Cohenacf69c92020-09-03 21:05:28 -07002194 unsigned short macoff, hdrlen;
2195 unsigned int netoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 struct sk_buff *copy_skb = NULL;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +01002197 struct timespec64 ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002198 __u32 ts_status;
Weongyo Jeongda378452016-04-14 14:10:04 -07002199 bool is_drop_n_account = false;
Willem de Bruijn61fad682020-03-13 12:18:09 -04002200 unsigned int slot_id = 0;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002201 bool do_vnet = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Atzm Watanabe51846352013-12-17 22:53:32 +09002203 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2204 * We may add members to them until current aligned size without forcing
2205 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2206 */
2207 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2208 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (skb->pkt_type == PACKET_LOOPBACK)
2211 goto drop;
2212
2213 sk = pt->af_packet_priv;
2214 po = pkt_sk(sk);
2215
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002216 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002217 goto drop;
2218
Eyal Birgerd5496992020-11-21 08:28:17 +02002219 if (dev_has_header(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002221 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 else if (skb->pkt_type == PACKET_OUTGOING) {
2223 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002224 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226 }
2227
2228 snaplen = skb->len;
2229
David S. Millerdbcb5852007-01-24 15:21:02 -08002230 res = run_filter(skb, sk, snaplen);
2231 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002232 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002233
Eric Dumazet2c51c622019-06-12 09:52:31 -07002234 /* If we are flooded, just give up */
2235 if (__packet_rcv_has_room(po, skb) == ROOM_NONE) {
2236 atomic_inc(&po->tp_drops);
2237 goto drop_n_restore;
2238 }
2239
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002240 if (skb->ip_summed == CHECKSUM_PARTIAL)
2241 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002242 else if (skb->pkt_type != PACKET_OUTGOING &&
2243 (skb->ip_summed == CHECKSUM_COMPLETE ||
2244 skb_csum_unnecessary(skb)))
2245 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002246
David S. Millerdbcb5852007-01-24 15:21:02 -08002247 if (snaplen > res)
2248 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002251 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2252 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002254 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002255 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002256 (maclen < 16 ? 16 : maclen)) +
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002257 po->tp_reserve;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002258 if (po->has_vnet_hdr) {
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002259 netoff += sizeof(struct virtio_net_hdr);
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002260 do_vnet = true;
2261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 macoff = netoff - maclen;
2263 }
Or Cohenacf69c92020-09-03 21:05:28 -07002264 if (netoff > USHRT_MAX) {
2265 atomic_inc(&po->tp_drops);
2266 goto drop_n_restore;
2267 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002268 if (po->tp_version <= TPACKET_V2) {
2269 if (macoff + snaplen > po->rx_ring.frame_size) {
2270 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002271 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002272 if (skb_shared(skb)) {
2273 copy_skb = skb_clone(skb, GFP_ATOMIC);
2274 } else {
2275 copy_skb = skb_get(skb);
2276 skb_head = skb->data;
2277 }
2278 if (copy_skb)
2279 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002281 snaplen = po->rx_ring.frame_size - macoff;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002282 if ((int)snaplen < 0) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002283 snaplen = 0;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002284 do_vnet = false;
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002287 } else if (unlikely(macoff + snaplen >
2288 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2289 u32 nval;
2290
2291 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2292 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2293 snaplen, nval, macoff);
2294 snaplen = nval;
2295 if (unlikely((int)snaplen < 0)) {
2296 snaplen = 0;
2297 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002298 do_vnet = false;
Eric Dumazetdc808112014-08-15 09:16:04 -07002299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00002302 h.raw = packet_current_rx_frame(po, skb,
2303 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002304 if (!h.raw)
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002305 goto drop_n_account;
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002306
Willem de Bruijn61fad682020-03-13 12:18:09 -04002307 if (po->tp_version <= TPACKET_V2) {
2308 slot_id = po->rx_ring.head;
2309 if (test_bit(slot_id, po->rx_ring.rx_owner_map))
2310 goto drop_n_account;
2311 __set_bit(slot_id, po->rx_ring.rx_owner_map);
2312 }
2313
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002314 if (do_vnet &&
2315 virtio_net_hdr_from_skb(skb, h.raw + macoff -
2316 sizeof(struct virtio_net_hdr),
John Ogness88fd1cb2020-08-13 21:45:25 +02062317 vio_le(), true, 0)) {
2318 if (po->tp_version == TPACKET_V3)
2319 prb_clear_blk_fill_status(&po->rx_ring);
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002320 goto drop_n_account;
John Ogness88fd1cb2020-08-13 21:45:25 +02062321 }
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002322
chetan lokef6fb8f12011-08-19 10:18:16 +00002323 if (po->tp_version <= TPACKET_V2) {
2324 packet_increment_rx_head(po, &po->rx_ring);
2325 /*
2326 * LOSING will be reported till you read the stats,
2327 * because it's COR - Clear On Read.
2328 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2329 * at packet level.
2330 */
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002331 if (atomic_read(&po->tp_drops))
chetan lokef6fb8f12011-08-19 10:18:16 +00002332 status |= TP_STATUS_LOSING;
2333 }
Eric Dumazet945d0152018-06-21 14:16:02 -07002334
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002335 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (copy_skb) {
2337 status |= TP_STATUS_COPY;
2338 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 spin_unlock(&sk->sk_receive_queue.lock);
2341
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002342 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002343
Richard Sanger171c3b12021-05-12 13:31:22 +12002344 /* Always timestamp; prefer an existing software timestamp taken
2345 * closer to the time of capture.
2346 */
2347 ts_status = tpacket_get_timestamp(skb, &ts,
2348 po->tp_tstamp | SOF_TIMESTAMPING_SOFTWARE);
2349 if (!ts_status)
Arnd Bergmannd413fcb2017-11-27 10:09:24 +01002350 ktime_get_real_ts64(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002352 status |= ts_status;
2353
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002354 switch (po->tp_version) {
2355 case TPACKET_V1:
2356 h.h1->tp_len = skb->len;
2357 h.h1->tp_snaplen = snaplen;
2358 h.h1->tp_mac = macoff;
2359 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002360 h.h1->tp_sec = ts.tv_sec;
2361 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002362 hdrlen = sizeof(*h.h1);
2363 break;
2364 case TPACKET_V2:
2365 h.h2->tp_len = skb->len;
2366 h.h2->tp_snaplen = snaplen;
2367 h.h2->tp_mac = macoff;
2368 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002369 h.h2->tp_sec = ts.tv_sec;
2370 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002371 if (skb_vlan_tag_present(skb)) {
2372 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002373 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2374 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002375 } else {
2376 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002377 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002378 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002379 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002380 hdrlen = sizeof(*h.h2);
2381 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00002382 case TPACKET_V3:
2383 /* tp_nxt_offset,vlan are already populated above.
2384 * So DONT clear those fields here
2385 */
2386 h.h3->tp_status |= status;
2387 h.h3->tp_len = skb->len;
2388 h.h3->tp_snaplen = snaplen;
2389 h.h3->tp_mac = macoff;
2390 h.h3->tp_net = netoff;
chetan lokef6fb8f12011-08-19 10:18:16 +00002391 h.h3->tp_sec = ts.tv_sec;
2392 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002393 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f12011-08-19 10:18:16 +00002394 hdrlen = sizeof(*h.h3);
2395 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002396 default:
2397 BUG();
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002400 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002401 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 sll->sll_family = AF_PACKET;
2403 sll->sll_hatype = dev->type;
2404 sll->sll_protocol = skb->protocol;
2405 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002406 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002407 sll->sll_ifindex = orig_dev->ifindex;
2408 else
2409 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Ralf Baechlee16aa202006-12-07 00:11:33 -08002411 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002412
Changli Gaof6dafa92010-12-07 04:26:16 +00002413#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002414 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002415 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002417 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2418 macoff + snaplen);
2419
2420 for (start = h.raw; start < end; start += PAGE_SIZE)
2421 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002423 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002424#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002425
Dan Collinsda413ee2014-12-19 16:49:25 +13002426 if (po->tp_version <= TPACKET_V2) {
Willem de Bruijn61fad682020-03-13 12:18:09 -04002427 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00002428 __packet_set_status(po, h.raw, status);
Willem de Bruijn61fad682020-03-13 12:18:09 -04002429 __clear_bit(slot_id, po->rx_ring.rx_owner_map);
2430 spin_unlock(&sk->sk_receive_queue.lock);
Dan Collinsda413ee2014-12-19 16:49:25 +13002431 sk->sk_data_ready(sk);
John Ogness88fd1cb2020-08-13 21:45:25 +02062432 } else if (po->tp_version == TPACKET_V3) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002433 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436drop_n_restore:
2437 if (skb_head != skb->data && skb_shared(skb)) {
2438 skb->data = skb_head;
2439 skb->len = skb_len;
2440 }
2441drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002442 if (!is_drop_n_account)
2443 consume_skb(skb);
2444 else
2445 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return 0;
2447
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002448drop_n_account:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 spin_unlock(&sk->sk_receive_queue.lock);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002450 atomic_inc(&po->tp_drops);
2451 is_drop_n_account = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
David S. Miller676d2362014-04-11 16:15:36 -04002453 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002454 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto drop_n_restore;
2456}
2457
Johann Baudy69e3c752009-05-18 22:11:22 -07002458static void tpacket_destruct_skb(struct sk_buff *skb)
2459{
2460 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002461
Johann Baudy69e3c752009-05-18 22:11:22 -07002462 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002463 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002464 __u32 ts;
2465
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002466 ph = skb_zcopy_get_nouarg(skb);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002467 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002468
2469 ts = __packet_set_timestamp(po, ph, skb);
2470 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Neil Horman89ed5b52019-06-25 17:57:49 -04002471
2472 if (!packet_read_pending(&po->tx_ring))
2473 complete(&po->skb_completion);
Johann Baudy69e3c752009-05-18 22:11:22 -07002474 }
2475
2476 sock_wfree(skb);
2477}
2478
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002479static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
2480{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002481 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2482 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2483 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
2484 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
2485 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
2486 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2487 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
2488
2489 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
2490 return -EINVAL;
2491
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002492 return 0;
2493}
2494
2495static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
2496 struct virtio_net_hdr *vnet_hdr)
2497{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002498 if (*len < sizeof(*vnet_hdr))
2499 return -EINVAL;
2500 *len -= sizeof(*vnet_hdr);
2501
Al Virocbbd26b2016-11-01 22:09:04 -04002502 if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002503 return -EFAULT;
2504
2505 return __packet_snd_vnet_parse(vnet_hdr, *len);
2506}
2507
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002508static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002509 void *frame, struct net_device *dev, void *data, int tp_len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002510 __be16 proto, unsigned char *addr, int hlen, int copylen,
2511 const struct sockcm_cookie *sockc)
Johann Baudy69e3c752009-05-18 22:11:22 -07002512{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002513 union tpacket_uhdr ph;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002514 int to_write, offset, len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002515 struct socket *sock = po->sk.sk_socket;
2516 struct page *page;
Johann Baudy69e3c752009-05-18 22:11:22 -07002517 int err;
2518
2519 ph.raw = frame;
2520
2521 skb->protocol = proto;
2522 skb->dev = dev;
2523 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002524 skb->mark = po->sk.sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002525 skb->tstamp = sockc->transmit_time;
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002526 skb_setup_tx_timestamp(skb, sockc->tsflags);
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002527 skb_zcopy_set_nouarg(skb, ph.raw);
Johann Baudy69e3c752009-05-18 22:11:22 -07002528
Herbert Xuae641942011-11-18 02:20:04 +00002529 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002530 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002531
Johann Baudy69e3c752009-05-18 22:11:22 -07002532 to_write = tp_len;
2533
2534 if (sock->type == SOCK_DGRAM) {
2535 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2536 NULL, tp_len);
2537 if (unlikely(err < 0))
2538 return -EINVAL;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002539 } else if (copylen) {
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002540 int hdrlen = min_t(int, copylen, tp_len);
2541
Johann Baudy69e3c752009-05-18 22:11:22 -07002542 skb_push(skb, dev->hard_header_len);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002543 skb_put(skb, copylen - dev->hard_header_len);
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002544 err = skb_store_bits(skb, 0, data, hdrlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002545 if (unlikely(err))
2546 return err;
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002547 if (!dev_validate_header(dev, skb->data, hdrlen))
2548 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002549
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002550 data += hdrlen;
2551 to_write -= hdrlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002552 }
2553
Johann Baudy69e3c752009-05-18 22:11:22 -07002554 offset = offset_in_page(data);
2555 len_max = PAGE_SIZE - offset;
2556 len = ((to_write > len_max) ? len_max : to_write);
2557
2558 skb->data_len = to_write;
2559 skb->len += to_write;
2560 skb->truesize += to_write;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002561 refcount_add(to_write, &po->sk.sk_wmem_alloc);
Johann Baudy69e3c752009-05-18 22:11:22 -07002562
2563 while (likely(to_write)) {
2564 nr_frags = skb_shinfo(skb)->nr_frags;
2565
2566 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002567 pr_err("Packet exceed the number of skb frags(%lu)\n",
2568 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002569 return -EFAULT;
2570 }
2571
Changli Gao0af55bb2010-12-01 02:52:20 +00002572 page = pgv_to_page(data);
2573 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002574 flush_dcache_page(page);
2575 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002576 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002577 to_write -= len;
2578 offset = 0;
2579 len_max = PAGE_SIZE;
2580 len = ((to_write > len_max) ? len_max : to_write);
2581 }
2582
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002583 packet_parse_headers(skb, sock);
Daniel Borkmannefdfa2f2015-11-11 23:25:40 +01002584
Johann Baudy69e3c752009-05-18 22:11:22 -07002585 return tp_len;
2586}
2587
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002588static int tpacket_parse_header(struct packet_sock *po, void *frame,
2589 int size_max, void **data)
2590{
2591 union tpacket_uhdr ph;
2592 int tp_len, off;
2593
2594 ph.raw = frame;
2595
2596 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002597 case TPACKET_V3:
2598 if (ph.h3->tp_next_offset != 0) {
2599 pr_warn_once("variable sized slot not supported");
2600 return -EINVAL;
2601 }
2602 tp_len = ph.h3->tp_len;
2603 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002604 case TPACKET_V2:
2605 tp_len = ph.h2->tp_len;
2606 break;
2607 default:
2608 tp_len = ph.h1->tp_len;
2609 break;
2610 }
2611 if (unlikely(tp_len > size_max)) {
2612 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2613 return -EMSGSIZE;
2614 }
2615
2616 if (unlikely(po->tp_tx_has_off)) {
2617 int off_min, off_max;
2618
2619 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2620 off_max = po->tx_ring.frame_size - tp_len;
2621 if (po->sk.sk_type == SOCK_DGRAM) {
2622 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002623 case TPACKET_V3:
2624 off = ph.h3->tp_net;
2625 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002626 case TPACKET_V2:
2627 off = ph.h2->tp_net;
2628 break;
2629 default:
2630 off = ph.h1->tp_net;
2631 break;
2632 }
2633 } else {
2634 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002635 case TPACKET_V3:
2636 off = ph.h3->tp_mac;
2637 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002638 case TPACKET_V2:
2639 off = ph.h2->tp_mac;
2640 break;
2641 default:
2642 off = ph.h1->tp_mac;
2643 break;
2644 }
2645 }
2646 if (unlikely((off < off_min) || (off_max < off)))
2647 return -EINVAL;
2648 } else {
2649 off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2650 }
2651
2652 *data = frame + off;
2653 return tp_len;
2654}
2655
Johann Baudy69e3c752009-05-18 22:11:22 -07002656static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2657{
Neil Horman89ed5b52019-06-25 17:57:49 -04002658 struct sk_buff *skb = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002659 struct net_device *dev;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002660 struct virtio_net_hdr *vnet_hdr = NULL;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002661 struct sockcm_cookie sockc;
Johann Baudy69e3c752009-05-18 22:11:22 -07002662 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002663 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002664 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002665 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002666 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002667 unsigned char *addr = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002668 int tp_len, size_max;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002669 void *data;
Johann Baudy69e3c752009-05-18 22:11:22 -07002670 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002671 int status = TP_STATUS_AVAILABLE;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002672 int hlen, tlen, copylen = 0;
Neil Horman89ed5b52019-06-25 17:57:49 -04002673 long timeo = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002674
Johann Baudy69e3c752009-05-18 22:11:22 -07002675 mutex_lock(&po->pg_vec_lock);
2676
Eric Dumazet32d31822019-08-14 02:11:57 -07002677 /* packet_sendmsg() check on tx_ring.pg_vec was lockless,
2678 * we need to confirm it under protection of pg_vec_lock.
2679 */
2680 if (unlikely(!po->tx_ring.pg_vec)) {
2681 err = -EBUSY;
2682 goto out;
2683 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002684 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002685 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002686 proto = po->num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002687 } else {
2688 err = -EINVAL;
2689 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2690 goto out;
2691 if (msg->msg_namelen < (saddr->sll_halen
2692 + offsetof(struct sockaddr_ll,
2693 sll_addr)))
2694 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002695 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002696 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002697 if (po->sk.sk_socket->type == SOCK_DGRAM) {
2698 if (dev && msg->msg_namelen < dev->addr_len +
2699 offsetof(struct sockaddr_ll, sll_addr))
2700 goto out_put;
2701 addr = saddr->sll_addr;
2702 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002703 }
2704
Johann Baudy69e3c752009-05-18 22:11:22 -07002705 err = -ENXIO;
2706 if (unlikely(dev == NULL))
2707 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002708 err = -ENETDOWN;
2709 if (unlikely(!(dev->flags & IFF_UP)))
2710 goto out_put;
2711
Willem de Bruijn657a0662018-07-06 10:12:56 -04002712 sockcm_init(&sockc, &po->sk);
Douglas Caetano dos Santosd19b1832017-05-12 15:19:15 -03002713 if (msg->msg_controllen) {
2714 err = sock_cmsg_send(&po->sk, msg, &sockc);
2715 if (unlikely(err))
2716 goto out_put;
2717 }
2718
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002719 if (po->sk.sk_socket->type == SOCK_RAW)
2720 reserve = dev->hard_header_len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002721 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002722 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002723
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002724 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002725 size_max = dev->mtu + reserve + VLAN_HLEN;
David S. Miller09effa62013-08-07 17:11:00 -07002726
Neil Horman89ed5b52019-06-25 17:57:49 -04002727 reinit_completion(&po->skb_completion);
2728
Johann Baudy69e3c752009-05-18 22:11:22 -07002729 do {
2730 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002731 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002732 if (unlikely(ph == NULL)) {
Neil Horman89ed5b52019-06-25 17:57:49 -04002733 if (need_wait && skb) {
2734 timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
2735 timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
2736 if (timeo <= 0) {
2737 err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
2738 goto out_put;
2739 }
2740 }
2741 /* check for additional frames */
Johann Baudy69e3c752009-05-18 22:11:22 -07002742 continue;
2743 }
2744
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002745 skb = NULL;
2746 tp_len = tpacket_parse_header(po, ph, size_max, &data);
2747 if (tp_len < 0)
2748 goto tpacket_error;
2749
Johann Baudy69e3c752009-05-18 22:11:22 -07002750 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002751 hlen = LL_RESERVED_SPACE(dev);
2752 tlen = dev->needed_tailroom;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002753 if (po->has_vnet_hdr) {
2754 vnet_hdr = data;
2755 data += sizeof(*vnet_hdr);
2756 tp_len -= sizeof(*vnet_hdr);
2757 if (tp_len < 0 ||
2758 __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
2759 tp_len = -EINVAL;
2760 goto tpacket_error;
2761 }
2762 copylen = __virtio16_to_cpu(vio_le(),
2763 vnet_hdr->hdr_len);
2764 }
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002765 copylen = max_t(int, copylen, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002766 skb = sock_alloc_send_skb(&po->sk,
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002767 hlen + tlen + sizeof(struct sockaddr_ll) +
2768 (copylen - dev->hard_header_len),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002769 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002770
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002771 if (unlikely(skb == NULL)) {
2772 /* we assume the socket was initially writeable ... */
2773 if (likely(len_sum > 0))
2774 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002775 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002776 }
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002777 tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002778 addr, hlen, copylen, &sockc);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002779 if (likely(tp_len >= 0) &&
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002780 tp_len > dev->mtu + reserve &&
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002781 !po->has_vnet_hdr &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002782 !packet_extra_vlan_len_allowed(dev, skb))
2783 tp_len = -EMSGSIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002784
2785 if (unlikely(tp_len < 0)) {
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002786tpacket_error:
Johann Baudy69e3c752009-05-18 22:11:22 -07002787 if (po->tp_loss) {
2788 __packet_set_status(po, ph,
2789 TP_STATUS_AVAILABLE);
2790 packet_increment_head(&po->tx_ring);
2791 kfree_skb(skb);
2792 continue;
2793 } else {
2794 status = TP_STATUS_WRONG_FORMAT;
2795 err = tp_len;
2796 goto out_status;
2797 }
2798 }
2799
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002800 if (po->has_vnet_hdr) {
2801 if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
2802 tp_len = -EINVAL;
2803 goto tpacket_error;
2804 }
2805 virtio_net_hdr_set_proto(skb, vnet_hdr);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002806 }
2807
Johann Baudy69e3c752009-05-18 22:11:22 -07002808 skb->destructor = tpacket_destruct_skb;
2809 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002810 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002811
2812 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002813 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002814 if (unlikely(err > 0)) {
2815 err = net_xmit_errno(err);
2816 if (err && __packet_get_status(po, ph) ==
2817 TP_STATUS_AVAILABLE) {
2818 /* skb was destructed already */
2819 skb = NULL;
2820 goto out_status;
2821 }
2822 /*
2823 * skb was dropped but not destructed yet;
2824 * let's treat it like congestion or err < 0
2825 */
2826 err = 0;
2827 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002828 packet_increment_head(&po->tx_ring);
2829 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002830 } while (likely((ph != NULL) ||
2831 /* Note: packet_read_pending() might be slow if we have
2832 * to call it as it's per_cpu variable, but in fast-path
2833 * we already short-circuit the loop with the first
2834 * condition, and luckily don't have to go that path
2835 * anyway.
2836 */
2837 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002838
2839 err = len_sum;
2840 goto out_put;
2841
Johann Baudy69e3c752009-05-18 22:11:22 -07002842out_status:
2843 __packet_set_status(po, ph, status);
2844 kfree_skb(skb);
2845out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002846 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002847out:
2848 mutex_unlock(&po->pg_vec_lock);
2849 return err;
2850}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002852static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2853 size_t reserve, size_t len,
2854 size_t linear, int noblock,
2855 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002856{
2857 struct sk_buff *skb;
2858
2859 /* Under a page? Don't bother with paged skb. */
2860 if (prepad + len < PAGE_SIZE || !linear)
2861 linear = len;
2862
2863 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002864 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002865 if (!skb)
2866 return NULL;
2867
2868 skb_reserve(skb, reserve);
2869 skb_put(skb, linear);
2870 skb->data_len = len - linear;
2871 skb->len += len - linear;
2872
2873 return skb;
2874}
2875
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002876static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002879 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct sk_buff *skb;
2881 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002882 __be16 proto;
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002883 unsigned char *addr = NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002884 int err, reserve = 0;
Edward Jeec7d39e32015-10-08 14:56:49 -07002885 struct sockcm_cookie sockc;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002886 struct virtio_net_hdr vnet_hdr = { 0 };
2887 int offset = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002888 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002889 bool has_vnet_hdr = false;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002890 int hlen, tlen, linear;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002891 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002894 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002896
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002897 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002898 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 proto = po->num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 } else {
2901 err = -EINVAL;
2902 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2903 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002904 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2905 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002907 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002908 if (sock->type == SOCK_DGRAM) {
2909 if (dev && msg->msg_namelen < dev->addr_len +
2910 offsetof(struct sockaddr_ll, sll_addr))
2911 goto out_unlock;
2912 addr = saddr->sll_addr;
2913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002917 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002919 err = -ENETDOWN;
2920 if (unlikely(!(dev->flags & IFF_UP)))
2921 goto out_unlock;
2922
Willem de Bruijn657a0662018-07-06 10:12:56 -04002923 sockcm_init(&sockc, sk);
Edward Jeec7d39e32015-10-08 14:56:49 -07002924 sockc.mark = sk->sk_mark;
2925 if (msg->msg_controllen) {
2926 err = sock_cmsg_send(sk, msg, &sockc);
2927 if (unlikely(err))
2928 goto out_unlock;
2929 }
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (sock->type == SOCK_RAW)
2932 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002933 if (po->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002934 err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
2935 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002936 goto out_unlock;
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002937 has_vnet_hdr = true;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002938 }
2939
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002940 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2941 if (!netif_supports_nofcs(dev)) {
2942 err = -EPROTONOSUPPORT;
2943 goto out_unlock;
2944 }
2945 extra_len = 4; /* We're doing our own CRC */
2946 }
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 err = -EMSGSIZE;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002949 if (!vnet_hdr.gso_type &&
2950 (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 goto out_unlock;
2952
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002953 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002954 hlen = LL_RESERVED_SPACE(dev);
2955 tlen = dev->needed_tailroom;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002956 linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
2957 linear = max(linear, min_t(int, len, dev->hard_header_len));
2958 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002959 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002960 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 goto out_unlock;
2962
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002963 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002965 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002966 if (sock->type == SOCK_DGRAM) {
2967 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002968 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002969 goto out_free;
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002970 } else if (reserve) {
Willem de Bruijn9aad13b2018-05-24 18:10:30 -04002971 skb_reserve(skb, -reserve);
Nicolas Dichtel88a81212019-01-17 11:27:22 +01002972 if (len < reserve + sizeof(struct ipv6hdr) &&
2973 dev->min_header_len != dev->hard_header_len)
Willem de Bruijn993675a2018-07-11 12:00:45 -04002974 skb_reset_network_header(skb);
Willem de Bruijn9c707762014-11-19 13:10:16 -05002975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002978 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (err)
2980 goto out_free;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002981
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002982 if (sock->type == SOCK_RAW &&
2983 !dev_validate_header(dev, skb->data, len)) {
2984 err = -EINVAL;
2985 goto out_free;
2986 }
2987
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002988 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002990 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002991 !packet_extra_vlan_len_allowed(dev, skb)) {
2992 err = -EMSGSIZE;
2993 goto out_free;
Ben Greear57f89bf2011-02-11 09:35:18 +00002994 }
2995
David S. Miller09effa62013-08-07 17:11:00 -07002996 skb->protocol = proto;
2997 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 skb->priority = sk->sk_priority;
Edward Jeec7d39e32015-10-08 14:56:49 -07002999 skb->mark = sockc.mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07003000 skb->tstamp = sockc.transmit_time;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01003001
Willem de Bruijnda7c9562017-09-26 12:20:17 -04003002 if (has_vnet_hdr) {
Jarno Rajahalmedb60eb5f2016-11-18 15:40:41 -08003003 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003004 if (err)
3005 goto out_free;
3006 len += sizeof(vnet_hdr);
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00003007 virtio_net_hdr_set_proto(skb, &vnet_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003008 }
3009
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00003010 packet_parse_headers(skb, sock);
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01003011
Ben Greear3bdc0eb2012-02-11 15:39:30 +00003012 if (unlikely(extra_len == 4))
3013 skb->no_fcs = 1;
3014
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003015 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (err > 0 && (err = net_xmit_errno(err)) != 0)
3017 goto out_unlock;
3018
Daniel Borkmanne40526c2013-11-21 16:50:58 +01003019 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003021 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023out_free:
3024 kfree_skb(skb);
3025out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01003026 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 dev_put(dev);
3028out:
3029 return err;
3030}
3031
Ying Xue1b784142015-03-02 15:37:48 +08003032static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07003033{
Johann Baudy69e3c752009-05-18 22:11:22 -07003034 struct sock *sk = sock->sk;
3035 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003036
Eric Dumazetd1b5bee2021-06-10 09:00:12 -07003037 /* Reading tx_ring.pg_vec without holding pg_vec_lock is racy.
3038 * tpacket_snd() will redo the check safely.
3039 */
3040 if (data_race(po->tx_ring.pg_vec))
Johann Baudy69e3c752009-05-18 22:11:22 -07003041 return tpacket_snd(po, msg);
Eric Dumazetd1b5bee2021-06-10 09:00:12 -07003042
3043 return packet_snd(sock, msg, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07003044}
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046/*
3047 * Close a PACKET socket. This is fairly simple. We immediately go
3048 * to 'closed' state and remove our protocol entry in the device list.
3049 */
3050
3051static int packet_release(struct socket *sock)
3052{
3053 struct sock *sk = sock->sk;
3054 struct packet_sock *po;
Anoob Soman2bd624b2017-02-15 20:25:39 +00003055 struct packet_fanout *f;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003056 struct net *net;
chetan lokef6fb8f12011-08-19 10:18:16 +00003057 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 if (!sk)
3060 return 0;
3061
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003062 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 po = pkt_sk(sk);
3064
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003065 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00003066 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003067 mutex_unlock(&net->packet.sklist_lock);
3068
3069 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08003070 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003071 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
stephen hemminger808f5112010-02-22 07:57:18 +00003073 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003074 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003075 packet_cached_dev_reset(po);
3076
Ben Greear160ff182011-06-01 07:18:52 +00003077 if (po->prot_hook.dev) {
3078 dev_put(po->prot_hook.dev);
3079 po->prot_hook.dev = NULL;
3080 }
stephen hemminger808f5112010-02-22 07:57:18 +00003081 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Eric Dumazet5171b372018-04-15 17:52:04 -07003085 lock_sock(sk);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003086 if (po->rx_ring.pg_vec) {
3087 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003088 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003089 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003090
Phil Sutter9665d5d2013-02-01 07:21:41 +00003091 if (po->tx_ring.pg_vec) {
3092 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003093 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003094 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003095 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Anoob Soman2bd624b2017-02-15 20:25:39 +00003097 f = fanout_release(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07003098
stephen hemminger808f5112010-02-22 07:57:18 +00003099 synchronize_net();
Anoob Soman2bd624b2017-02-15 20:25:39 +00003100
Willem de Bruijnafa09252019-05-31 12:37:23 -04003101 kfree(po->rollover);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003102 if (f) {
3103 fanout_release_data(f);
Tanner Love9c661b02020-11-06 13:07:40 -05003104 kvfree(f);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /*
3107 * Now the socket is dead. No more input will appear.
3108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 sock_orphan(sk);
3110 sock->sk = NULL;
3111
3112 /* Purge queues */
3113
3114 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01003115 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003116 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 sock_put(sk);
3119 return 0;
3120}
3121
3122/*
3123 * Attach a packet hook.
3124 */
3125
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003126static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
3127 __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
3129 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003130 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003131 __be16 proto_curr;
3132 bool need_rehook;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003133 struct net_device *dev = NULL;
3134 int ret = 0;
3135 bool unlisted = false;
David S. Millerdc99f602011-07-05 01:45:05 -07003136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 spin_lock(&po->bind_lock);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003139 rcu_read_lock();
3140
Willem de Bruijn49716132017-09-26 12:19:37 -04003141 if (po->fanout) {
3142 ret = -EINVAL;
3143 goto out_unlock;
3144 }
3145
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003146 if (name) {
3147 dev = dev_get_by_name_rcu(sock_net(sk), name);
3148 if (!dev) {
3149 ret = -ENODEV;
3150 goto out_unlock;
3151 }
3152 } else if (ifindex) {
3153 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
3154 if (!dev) {
3155 ret = -ENODEV;
3156 goto out_unlock;
3157 }
3158 }
3159
3160 if (dev)
3161 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003162
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003163 proto_curr = po->prot_hook.type;
3164 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003166 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003168 if (need_rehook) {
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003169 if (po->running) {
3170 rcu_read_unlock();
Eric Dumazet15fe0762017-11-28 08:03:30 -08003171 /* prevents packet_notifier() from calling
3172 * register_prot_hook()
3173 */
3174 po->num = 0;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003175 __unregister_prot_hook(sk, true);
3176 rcu_read_lock();
3177 dev_curr = po->prot_hook.dev;
3178 if (dev)
3179 unlisted = !dev_get_by_index_rcu(sock_net(sk),
3180 dev->ifindex);
3181 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003182
Eric Dumazet15fe0762017-11-28 08:03:30 -08003183 BUG_ON(po->running);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003184 po->num = proto;
3185 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003186
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003187 if (unlikely(unlisted)) {
3188 dev_put(dev);
3189 po->prot_hook.dev = NULL;
3190 po->ifindex = -1;
3191 packet_cached_dev_reset(po);
3192 } else {
3193 po->prot_hook.dev = dev;
3194 po->ifindex = dev ? dev->ifindex : 0;
3195 packet_cached_dev_assign(po, dev);
3196 }
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003197 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003198 if (dev_curr)
3199 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003200
3201 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 goto out_unlock;
3203
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003204 if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
David S. Millerce06b032011-07-04 01:44:29 -07003205 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08003206 } else {
3207 sk->sk_err = ENETDOWN;
3208 if (!sock_flag(sk, SOCK_DEAD))
3209 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211
3212out_unlock:
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003213 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 spin_unlock(&po->bind_lock);
3215 release_sock(sk);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003216 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
3219/*
3220 * Bind a packet socket to a device
3221 */
3222
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003223static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
3224 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003226 struct sock *sk = sock->sk;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003227 char name[sizeof(uaddr->sa_data) + 1];
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 /*
3230 * Check legality
3231 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003232
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003233 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return -EINVAL;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003235 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
3236 * zero-terminated.
3237 */
3238 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
3239 name[sizeof(uaddr->sa_data)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003241 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3245{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003246 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
3247 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 /*
3250 * Check legality
3251 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (addr_len < sizeof(struct sockaddr_ll))
3254 return -EINVAL;
3255 if (sll->sll_family != AF_PACKET)
3256 return -EINVAL;
3257
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003258 return packet_do_bind(sk, NULL, sll->sll_ifindex,
3259 sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
3262static struct proto packet_proto = {
3263 .name = "PACKET",
3264 .owner = THIS_MODULE,
3265 .obj_size = sizeof(struct packet_sock),
3266};
3267
3268/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003269 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 */
3271
Eric Paris3f378b62009-11-05 22:18:14 -08003272static int packet_create(struct net *net, struct socket *sock, int protocol,
3273 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
3275 struct sock *sk;
3276 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003277 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 int err;
3279
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003280 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003282 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3283 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return -ESOCKTNOSUPPORT;
3285
3286 sock->state = SS_UNCONNECTED;
3287
3288 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003289 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (sk == NULL)
3291 goto out;
3292
3293 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (sock->type == SOCK_PACKET)
3295 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 sock_init_data(sock, sk);
3298
3299 po = pkt_sk(sk);
Neil Horman89ed5b52019-06-25 17:57:49 -04003300 init_completion(&po->skb_completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003302 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003303 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003304
Daniel Borkmannb0138402014-01-15 16:25:36 +01003305 err = packet_alloc_pending(po);
3306 if (err)
3307 goto out2;
3308
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003309 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003312 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314 /*
3315 * Attach a protocol block
3316 */
3317
3318 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003319 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003320 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (sock->type == SOCK_PACKET)
3324 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 po->prot_hook.af_packet_priv = sk;
3327
Al Viro0e11c912006-11-08 00:26:29 -08003328 if (proto) {
3329 po->prot_hook.type = proto;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003330 __register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
3332
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003333 mutex_lock(&net->packet.sklist_lock);
Maxime Chevalliera4dc6a42019-03-16 14:41:30 +01003334 sk_add_node_tail_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003335 mutex_unlock(&net->packet.sklist_lock);
3336
3337 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003338 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003339 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003340
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003341 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003342out2:
3343 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344out:
3345 return err;
3346}
3347
3348/*
3349 * Pull a packet from our receive queue and hand it to the user.
3350 * If necessary we block.
3351 */
3352
Ying Xue1b784142015-03-02 15:37:48 +08003353static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3354 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 struct sock *sk = sock->sk;
3357 struct sk_buff *skb;
3358 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003359 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003360 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003363 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 goto out;
3365
3366#if 0
3367 /* What error should we return now? EUNATTACH? */
3368 if (pkt_sk(sk)->ifindex < 0)
3369 return -ENODEV;
3370#endif
3371
Richard Cochraned85b562010-04-07 22:41:28 +00003372 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003373 err = sock_recv_errqueue(sk, msg, len,
3374 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003375 goto out;
3376 }
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 * Call the generic datagram receiver. This handles all sorts
3380 * of horrible races and re-entrancy so we can forget about it
3381 * in the protocol layers.
3382 *
3383 * Now it will return ENETDOWN, if device have just gone down,
3384 * but then it will block.
3385 */
3386
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003387 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003390 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 * handles the blocking we don't see and worry about blocking
3392 * retries.
3393 */
3394
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003395 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 goto out;
3397
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07003398 packet_rcv_try_clear_pressure(pkt_sk(sk));
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003399
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003400 if (pkt_sk(sk)->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003401 err = packet_rcv_vnet(msg, skb, &len);
3402 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003403 goto out_free;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003404 vnet_hdr_len = sizeof(struct virtio_net_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003405 }
3406
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003407 /* You lose any data beyond the buffer you gave. If it worries
3408 * a user program they can ask the device for its MTU
3409 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003412 if (copied > len) {
3413 copied = len;
3414 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416
David S. Miller51f3d022014-11-05 16:46:40 -05003417 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (err)
3419 goto out_free;
3420
Eyal Birger2472d762015-03-01 14:58:28 +02003421 if (sock->type != SOCK_PACKET) {
3422 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3423
3424 /* Original length was stored in sockaddr_ll fields */
3425 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3426 sll->sll_family = AF_PACKET;
3427 sll->sll_protocol = skb->protocol;
3428 }
3429
Neil Horman3b885782009-10-12 13:26:31 -07003430 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003432 if (msg->msg_name) {
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003433 int copy_len;
3434
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003435 /* If the address length field is there to be filled
3436 * in, we fill it in now.
3437 */
3438 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003439 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003440 msg->msg_namelen = sizeof(struct sockaddr_pkt);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003441 copy_len = msg->msg_namelen;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003442 } else {
3443 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003444
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003445 msg->msg_namelen = sll->sll_halen +
3446 offsetof(struct sockaddr_ll, sll_addr);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003447 copy_len = msg->msg_namelen;
3448 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
3449 memset(msg->msg_name +
3450 offsetof(struct sockaddr_ll, sll_addr),
3451 0, sizeof(sll->sll_addr));
3452 msg->msg_namelen = sizeof(struct sockaddr_ll);
3453 }
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003454 }
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003455 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Herbert Xu8dc41942007-02-04 23:31:32 -08003458 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003459 struct tpacket_auxdata aux;
3460
3461 aux.tp_status = TP_STATUS_USER;
3462 if (skb->ip_summed == CHECKSUM_PARTIAL)
3463 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003464 else if (skb->pkt_type != PACKET_OUTGOING &&
3465 (skb->ip_summed == CHECKSUM_COMPLETE ||
3466 skb_csum_unnecessary(skb)))
3467 aux.tp_status |= TP_STATUS_CSUM_VALID;
3468
Eyal Birger2472d762015-03-01 14:58:28 +02003469 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003470 aux.tp_snaplen = skb->len;
3471 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003472 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003473 if (skb_vlan_tag_present(skb)) {
3474 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003475 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3476 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003477 } else {
3478 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003479 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003480 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003481 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003482 }
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 /*
3485 * Free or return the buffer as appropriate. Again this
3486 * hides all the races and re-entrancy issues from us.
3487 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003488 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490out_free:
3491 skb_free_datagram(sk, skb);
3492out:
3493 return err;
3494}
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003497 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 struct net_device *dev;
3500 struct sock *sk = sock->sk;
3501
3502 if (peer)
3503 return -EOPNOTSUPP;
3504
3505 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003506 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003507 rcu_read_lock();
3508 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3509 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003510 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003511 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003513 return sizeof(*uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003517 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 struct net_device *dev;
3520 struct sock *sk = sock->sk;
3521 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003522 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524 if (peer)
3525 return -EOPNOTSUPP;
3526
3527 sll->sll_family = AF_PACKET;
3528 sll->sll_ifindex = po->ifindex;
3529 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003530 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003531 rcu_read_lock();
3532 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (dev) {
3534 sll->sll_hatype = dev->type;
3535 sll->sll_halen = dev->addr_len;
3536 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 } else {
3538 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3539 sll->sll_halen = 0;
3540 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003541 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003543 return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Wang Chen2aeb0b82008-07-14 20:49:46 -07003546static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3547 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 switch (i->type) {
3550 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003551 if (i->alen != dev->addr_len)
3552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003554 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003556 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 break;
3558 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003559 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003561 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003562 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003563 if (i->alen != dev->addr_len)
3564 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003565 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003566 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003567 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003568 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003569 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003570 default:
3571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003576static void packet_dev_mclist_delete(struct net_device *dev,
3577 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003579 struct packet_mclist *ml;
3580
3581 while ((ml = *mlp) != NULL) {
3582 if (ml->ifindex == dev->ifindex) {
3583 packet_dev_mc(dev, ml, -1);
3584 *mlp = ml->next;
3585 kfree(ml);
3586 } else
3587 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
3589}
3590
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003591static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 struct packet_sock *po = pkt_sk(sk);
3594 struct packet_mclist *ml, *i;
3595 struct net_device *dev;
3596 int err;
3597
3598 rtnl_lock();
3599
3600 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003601 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (!dev)
3603 goto done;
3604
3605 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003606 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 goto done;
3608
3609 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003610 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (i == NULL)
3612 goto done;
3613
3614 err = 0;
3615 for (ml = po->mclist; ml; ml = ml->next) {
3616 if (ml->ifindex == mreq->mr_ifindex &&
3617 ml->type == mreq->mr_type &&
3618 ml->alen == mreq->mr_alen &&
3619 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3620 ml->count++;
3621 /* Free the new element ... */
3622 kfree(i);
3623 goto done;
3624 }
3625 }
3626
3627 i->type = mreq->mr_type;
3628 i->ifindex = mreq->mr_ifindex;
3629 i->alen = mreq->mr_alen;
3630 memcpy(i->addr, mreq->mr_address, i->alen);
Mathias Krause309cf372016-04-10 12:52:28 +02003631 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 i->count = 1;
3633 i->next = po->mclist;
3634 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003635 err = packet_dev_mc(dev, i, 1);
3636 if (err) {
3637 po->mclist = i->next;
3638 kfree(i);
3639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641done:
3642 rtnl_unlock();
3643 return err;
3644}
3645
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003646static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 struct packet_mclist *ml, **mlp;
3649
3650 rtnl_lock();
3651
3652 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3653 if (ml->ifindex == mreq->mr_ifindex &&
3654 ml->type == mreq->mr_type &&
3655 ml->alen == mreq->mr_alen &&
3656 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3657 if (--ml->count == 0) {
3658 struct net_device *dev;
3659 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003660 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3661 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 kfree(ml);
3664 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
3667 }
3668 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
3672static void packet_flush_mclist(struct sock *sk)
3673{
3674 struct packet_sock *po = pkt_sk(sk);
3675 struct packet_mclist *ml;
3676
3677 if (!po->mclist)
3678 return;
3679
3680 rtnl_lock();
3681 while ((ml = po->mclist) != NULL) {
3682 struct net_device *dev;
3683
3684 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003685 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3686 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 kfree(ml);
3689 }
3690 rtnl_unlock();
3691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693static int
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003694packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
3695 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
3697 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003698 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 int ret;
3700
3701 if (level != SOL_PACKET)
3702 return -ENOPROTOOPT;
3703
Johann Baudy69e3c752009-05-18 22:11:22 -07003704 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003705 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 case PACKET_DROP_MEMBERSHIP:
3707 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003708 struct packet_mreq_max mreq;
3709 int len = optlen;
3710 memset(&mreq, 0, sizeof(mreq));
3711 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003713 if (len > sizeof(mreq))
3714 len = sizeof(mreq);
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003715 if (copy_from_sockptr(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003717 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3718 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (optname == PACKET_ADD_MEMBERSHIP)
3720 ret = packet_mc_add(sk, &mreq);
3721 else
3722 ret = packet_mc_drop(sk, &mreq);
3723 return ret;
3724 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003727 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 {
chetan lokef6fb8f12011-08-19 10:18:16 +00003729 union tpacket_req_u req_u;
3730 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Eric Dumazet5171b372018-04-15 17:52:04 -07003732 lock_sock(sk);
chetan lokef6fb8f12011-08-19 10:18:16 +00003733 switch (po->tp_version) {
3734 case TPACKET_V1:
3735 case TPACKET_V2:
3736 len = sizeof(req_u.req);
3737 break;
3738 case TPACKET_V3:
3739 default:
3740 len = sizeof(req_u.req3);
3741 break;
3742 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003743 if (optlen < len) {
3744 ret = -EINVAL;
3745 } else {
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003746 if (copy_from_sockptr(&req_u.req, optval, len))
Eric Dumazet5171b372018-04-15 17:52:04 -07003747 ret = -EFAULT;
3748 else
3749 ret = packet_set_ring(sk, &req_u, 0,
3750 optname == PACKET_TX_RING);
3751 }
3752 release_sock(sk);
3753 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
3755 case PACKET_COPY_THRESH:
3756 {
3757 int val;
3758
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003759 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003761 if (copy_from_sockptr(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return -EFAULT;
3763
3764 pkt_sk(sk)->copy_thresh = val;
3765 return 0;
3766 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003767 case PACKET_VERSION:
3768 {
3769 int val;
3770
3771 if (optlen != sizeof(val))
3772 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003773 if (copy_from_sockptr(&val, optval, sizeof(val)))
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003774 return -EFAULT;
3775 switch (val) {
3776 case TPACKET_V1:
3777 case TPACKET_V2:
chetan lokef6fb8f12011-08-19 10:18:16 +00003778 case TPACKET_V3:
Philip Pettersson84ac7262016-11-30 14:55:36 -08003779 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003780 default:
3781 return -EINVAL;
3782 }
Philip Pettersson84ac7262016-11-30 14:55:36 -08003783 lock_sock(sk);
3784 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3785 ret = -EBUSY;
3786 } else {
3787 po->tp_version = val;
3788 ret = 0;
3789 }
3790 release_sock(sk);
3791 return ret;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003792 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003793 case PACKET_RESERVE:
3794 {
3795 unsigned int val;
3796
3797 if (optlen != sizeof(val))
3798 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003799 if (copy_from_sockptr(&val, optval, sizeof(val)))
Patrick McHardy8913336a2008-07-18 18:05:19 -07003800 return -EFAULT;
Andrey Konovalovbcc53642017-03-29 16:11:22 +02003801 if (val > INT_MAX)
3802 return -EINVAL;
Willem de Bruijnc27927e2017-08-10 12:41:58 -04003803 lock_sock(sk);
3804 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3805 ret = -EBUSY;
3806 } else {
3807 po->tp_reserve = val;
3808 ret = 0;
3809 }
3810 release_sock(sk);
3811 return ret;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003812 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003813 case PACKET_LOSS:
3814 {
3815 unsigned int val;
3816
3817 if (optlen != sizeof(val))
3818 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003819 if (copy_from_sockptr(&val, optval, sizeof(val)))
Johann Baudy69e3c752009-05-18 22:11:22 -07003820 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003821
3822 lock_sock(sk);
3823 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3824 ret = -EBUSY;
3825 } else {
3826 po->tp_loss = !!val;
3827 ret = 0;
3828 }
3829 release_sock(sk);
3830 return ret;
Johann Baudy69e3c752009-05-18 22:11:22 -07003831 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003832 case PACKET_AUXDATA:
3833 {
3834 int val;
3835
3836 if (optlen < sizeof(val))
3837 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003838 if (copy_from_sockptr(&val, optval, sizeof(val)))
Herbert Xu8dc41942007-02-04 23:31:32 -08003839 return -EFAULT;
3840
Willem de Bruijna6361f02018-04-23 17:37:03 -04003841 lock_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003842 po->auxdata = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003843 release_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003844 return 0;
3845 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003846 case PACKET_ORIGDEV:
3847 {
3848 int val;
3849
3850 if (optlen < sizeof(val))
3851 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003852 if (copy_from_sockptr(&val, optval, sizeof(val)))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003853 return -EFAULT;
3854
Willem de Bruijna6361f02018-04-23 17:37:03 -04003855 lock_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003856 po->origdev = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003857 release_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003858 return 0;
3859 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003860 case PACKET_VNET_HDR:
3861 {
3862 int val;
3863
3864 if (sock->type != SOCK_RAW)
3865 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003866 if (optlen < sizeof(val))
3867 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003868 if (copy_from_sockptr(&val, optval, sizeof(val)))
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003869 return -EFAULT;
3870
Willem de Bruijna6361f02018-04-23 17:37:03 -04003871 lock_sock(sk);
3872 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3873 ret = -EBUSY;
3874 } else {
3875 po->has_vnet_hdr = !!val;
3876 ret = 0;
3877 }
3878 release_sock(sk);
3879 return ret;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003880 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003881 case PACKET_TIMESTAMP:
3882 {
3883 int val;
3884
3885 if (optlen != sizeof(val))
3886 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003887 if (copy_from_sockptr(&val, optval, sizeof(val)))
Scott McMillan614f60f2010-06-02 05:53:56 -07003888 return -EFAULT;
3889
3890 po->tp_tstamp = val;
3891 return 0;
3892 }
David S. Millerdc99f602011-07-05 01:45:05 -07003893 case PACKET_FANOUT:
3894 {
Tanner Love9c661b02020-11-06 13:07:40 -05003895 struct fanout_args args = { 0 };
David S. Millerdc99f602011-07-05 01:45:05 -07003896
Tanner Love9c661b02020-11-06 13:07:40 -05003897 if (optlen != sizeof(int) && optlen != sizeof(args))
David S. Millerdc99f602011-07-05 01:45:05 -07003898 return -EINVAL;
Tanner Love9c661b02020-11-06 13:07:40 -05003899 if (copy_from_sockptr(&args, optval, optlen))
David S. Millerdc99f602011-07-05 01:45:05 -07003900 return -EFAULT;
3901
Tanner Love9c661b02020-11-06 13:07:40 -05003902 return fanout_add(sk, &args);
David S. Millerdc99f602011-07-05 01:45:05 -07003903 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003904 case PACKET_FANOUT_DATA:
3905 {
3906 if (!po->fanout)
3907 return -EINVAL;
3908
3909 return fanout_set_data(po, optval, optlen);
3910 }
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003911 case PACKET_IGNORE_OUTGOING:
3912 {
3913 int val;
3914
3915 if (optlen != sizeof(val))
3916 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003917 if (copy_from_sockptr(&val, optval, sizeof(val)))
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003918 return -EFAULT;
3919 if (val < 0 || val > 1)
3920 return -EINVAL;
3921
3922 po->prot_hook.ignore_outgoing = !!val;
3923 return 0;
3924 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003925 case PACKET_TX_HAS_OFF:
3926 {
3927 unsigned int val;
3928
3929 if (optlen != sizeof(val))
3930 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003931 if (copy_from_sockptr(&val, optval, sizeof(val)))
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003932 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003933
3934 lock_sock(sk);
3935 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3936 ret = -EBUSY;
3937 } else {
3938 po->tp_tx_has_off = !!val;
3939 ret = 0;
3940 }
3941 release_sock(sk);
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003942 return 0;
3943 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003944 case PACKET_QDISC_BYPASS:
3945 {
3946 int val;
3947
3948 if (optlen != sizeof(val))
3949 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003950 if (copy_from_sockptr(&val, optval, sizeof(val)))
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003951 return -EFAULT;
3952
3953 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3954 return 0;
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 default:
3957 return -ENOPROTOOPT;
3958 }
3959}
3960
3961static int packet_getsockopt(struct socket *sock, int level, int optname,
3962 char __user *optval, int __user *optlen)
3963{
3964 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003965 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 struct sock *sk = sock->sk;
3967 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003968 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003969 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003970 struct tpacket_rollover_stats rstats;
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003971 int drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
3973 if (level != SOL_PACKET)
3974 return -ENOPROTOOPT;
3975
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003976 if (get_user(len, optlen))
3977 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 if (len < 0)
3980 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003981
Johann Baudy69e3c752009-05-18 22:11:22 -07003982 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003985 memcpy(&st, &po->stats, sizeof(st));
3986 memset(&po->stats, 0, sizeof(po->stats));
3987 spin_unlock_bh(&sk->sk_receive_queue.lock);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003988 drops = atomic_xchg(&po->tp_drops, 0);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003989
chetan lokef6fb8f12011-08-19 10:18:16 +00003990 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003991 lv = sizeof(struct tpacket_stats_v3);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003992 st.stats3.tp_drops = drops;
3993 st.stats3.tp_packets += drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003994 data = &st.stats3;
chetan lokef6fb8f12011-08-19 10:18:16 +00003995 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003996 lv = sizeof(struct tpacket_stats);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07003997 st.stats1.tp_drops = drops;
3998 st.stats1.tp_packets += drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003999 data = &st.stats1;
chetan lokef6fb8f12011-08-19 10:18:16 +00004000 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08004003 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08004004 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08004005 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07004006 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07004007 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07004008 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08004009 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08004010 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08004011 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004012 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004013 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004014 break;
4015 case PACKET_HDRLEN:
4016 if (len > sizeof(int))
4017 len = sizeof(int);
Alexander Potapenkofd2c83b2017-04-25 18:51:46 +02004018 if (len < sizeof(int))
4019 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004020 if (copy_from_user(&val, optval, len))
4021 return -EFAULT;
4022 switch (val) {
4023 case TPACKET_V1:
4024 val = sizeof(struct tpacket_hdr);
4025 break;
4026 case TPACKET_V2:
4027 val = sizeof(struct tpacket2_hdr);
4028 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004029 case TPACKET_V3:
4030 val = sizeof(struct tpacket3_hdr);
4031 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004032 default:
4033 return -EINVAL;
4034 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004035 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004036 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07004037 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004038 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07004039 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07004040 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07004041 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07004042 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07004043 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07004044 break;
David S. Millerdc99f602011-07-05 01:45:05 -07004045 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07004046 val = (po->fanout ?
4047 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00004048 ((u32)po->fanout->type << 16) |
4049 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07004050 0);
David S. Millerdc99f602011-07-05 01:45:05 -07004051 break;
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02004052 case PACKET_IGNORE_OUTGOING:
4053 val = po->prot_hook.ignore_outgoing;
4054 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04004055 case PACKET_ROLLOVER_STATS:
Mike Maloney57f015f2017-11-28 10:44:29 -05004056 if (!po->rollover)
Willem de Bruijna9b63912015-05-12 11:56:50 -04004057 return -EINVAL;
Mike Maloney57f015f2017-11-28 10:44:29 -05004058 rstats.tp_all = atomic_long_read(&po->rollover->num);
4059 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
4060 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
4061 data = &rstats;
4062 lv = sizeof(rstats);
Willem de Bruijna9b63912015-05-12 11:56:50 -04004063 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00004064 case PACKET_TX_HAS_OFF:
4065 val = po->tp_tx_has_off;
4066 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01004067 case PACKET_QDISC_BYPASS:
4068 val = packet_use_direct_xmit(po);
4069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 default:
4071 return -ENOPROTOOPT;
4072 }
4073
Eric Dumazetc06fff62012-04-19 21:56:11 +00004074 if (len > lv)
4075 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004076 if (put_user(len, optlen))
4077 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08004078 if (copy_to_user(optval, data, len))
4079 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
Jiri Pirko351638e2013-05-28 01:30:21 +00004083static int packet_notifier(struct notifier_block *this,
4084 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
4086 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00004087 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004088 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
stephen hemminger808f5112010-02-22 07:57:18 +00004090 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004091 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 struct packet_sock *po = pkt_sk(sk);
4093
4094 switch (msg) {
4095 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07004097 packet_dev_mclist_delete(dev, &po->mclist);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004098 fallthrough;
David S. Millera2efcfa2007-05-29 13:12:50 -07004099
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 case NETDEV_DOWN:
4101 if (dev->ifindex == po->ifindex) {
4102 spin_lock(&po->bind_lock);
4103 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07004104 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 sk->sk_err = ENETDOWN;
4106 if (!sock_flag(sk, SOCK_DEAD))
4107 sk->sk_error_report(sk);
4108 }
4109 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01004110 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00004112 if (po->prot_hook.dev)
4113 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 po->prot_hook.dev = NULL;
4115 }
4116 spin_unlock(&po->bind_lock);
4117 }
4118 break;
4119 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00004120 if (dev->ifindex == po->ifindex) {
4121 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004122 if (po->num)
4123 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00004124 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 break;
4127 }
4128 }
stephen hemminger808f5112010-02-22 07:57:18 +00004129 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return NOTIFY_DONE;
4131}
4132
4133
4134static int packet_ioctl(struct socket *sock, unsigned int cmd,
4135 unsigned long arg)
4136{
4137 struct sock *sk = sock->sk;
4138
Johann Baudy69e3c752009-05-18 22:11:22 -07004139 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004140 case SIOCOUTQ:
4141 {
4142 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07004143
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004144 return put_user(amount, (int __user *)arg);
4145 }
4146 case SIOCINQ:
4147 {
4148 struct sk_buff *skb;
4149 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004151 spin_lock_bh(&sk->sk_receive_queue.lock);
4152 skb = skb_peek(&sk->sk_receive_queue);
4153 if (skb)
4154 amount = skb->len;
4155 spin_unlock_bh(&sk->sk_receive_queue.lock);
4156 return put_user(amount, (int __user *)arg);
4157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004159 case SIOCADDRT:
4160 case SIOCDELRT:
4161 case SIOCDARP:
4162 case SIOCGARP:
4163 case SIOCSARP:
4164 case SIOCGIFADDR:
4165 case SIOCSIFADDR:
4166 case SIOCGIFBRDADDR:
4167 case SIOCSIFBRDADDR:
4168 case SIOCGIFNETMASK:
4169 case SIOCSIFNETMASK:
4170 case SIOCGIFDSTADDR:
4171 case SIOCSIFDSTADDR:
4172 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004173 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174#endif
4175
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004176 default:
4177 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
4179 return 0;
4180}
4181
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004182static __poll_t packet_poll(struct file *file, struct socket *sock,
4183 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 struct sock *sk = sock->sk;
4186 struct packet_sock *po = pkt_sk(sk);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004187 __poll_t mask = datagram_poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004190 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004191 if (!packet_previous_rx_frame(po, &po->rx_ring,
4192 TP_STATUS_KERNEL))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004193 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07004195 packet_rcv_try_clear_pressure(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004197 spin_lock_bh(&sk->sk_write_queue.lock);
4198 if (po->tx_ring.pg_vec) {
4199 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004200 mask |= EPOLLOUT | EPOLLWRNORM;
Johann Baudy69e3c752009-05-18 22:11:22 -07004201 }
4202 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return mask;
4204}
4205
4206
4207/* Dirty? Well, I still did not learn better way to account
4208 * for user mmaps.
4209 */
4210
4211static void packet_mm_open(struct vm_area_struct *vma)
4212{
4213 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004214 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (sk)
4218 atomic_inc(&pkt_sk(sk)->mapped);
4219}
4220
4221static void packet_mm_close(struct vm_area_struct *vma)
4222{
4223 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004224 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 if (sk)
4228 atomic_dec(&pkt_sk(sk)->mapped);
4229}
4230
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004231static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004232 .open = packet_mm_open,
4233 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234};
4235
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004236static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
4237 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
4239 int i;
4240
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004241 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004242 if (likely(pg_vec[i].buffer)) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004243 if (is_vmalloc_addr(pg_vec[i].buffer))
4244 vfree(pg_vec[i].buffer);
4245 else
4246 free_pages((unsigned long)pg_vec[i].buffer,
4247 order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004248 pg_vec[i].buffer = NULL;
4249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 }
4251 kfree(pg_vec);
4252}
4253
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004254static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004255{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004256 char *buffer;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004257 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
4258 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07004259
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004260 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004261 if (buffer)
4262 return buffer;
4263
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004264 /* __get_free_pages failed, fall back to vmalloc */
4265 buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
4266 if (buffer)
4267 return buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08004268
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004269 /* vmalloc failed, lets dig into swap here */
4270 gfp_flags &= ~__GFP_NORETRY;
4271 buffer = (char *) __get_free_pages(gfp_flags, order);
4272 if (buffer)
4273 return buffer;
4274
4275 /* complete and utter failure */
4276 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004277}
4278
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004279static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004280{
4281 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004282 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004283 int i;
4284
Christoph Paasch398f0132019-03-18 23:14:52 -07004285 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004286 if (unlikely(!pg_vec))
4287 goto out;
4288
4289 for (i = 0; i < block_nr; i++) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004290 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004291 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004292 goto out_free_pgvec;
4293 }
4294
4295out:
4296 return pg_vec;
4297
4298out_free_pgvec:
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004299 free_pg_vec(pg_vec, order, block_nr);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004300 pg_vec = NULL;
4301 goto out;
4302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
chetan lokef6fb8f12011-08-19 10:18:16 +00004304static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004305 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Neil Horman0e3125c2010-11-16 10:26:47 -08004307 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijn61fad682020-03-13 12:18:09 -04004309 unsigned long *rx_owner_map = NULL;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004310 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004311 struct packet_ring_buffer *rb;
4312 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004313 __be16 num;
Colin Ian King2a6d6c32020-07-01 16:04:33 +01004314 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +00004315 /* Added to avoid minimal code churn */
4316 struct tpacket_req *req = &req_u->req;
4317
Johann Baudy69e3c752009-05-18 22:11:22 -07004318 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4319 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4320
4321 err = -EBUSY;
4322 if (!closing) {
4323 if (atomic_read(&po->mapped))
4324 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004325 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004326 goto out;
4327 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (req->tp_block_nr) {
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004330 unsigned int min_frame_size;
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004333 err = -EBUSY;
4334 if (unlikely(rb->pg_vec))
4335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004337 switch (po->tp_version) {
4338 case TPACKET_V1:
4339 po->tp_hdrlen = TPACKET_HDRLEN;
4340 break;
4341 case TPACKET_V2:
4342 po->tp_hdrlen = TPACKET2_HDRLEN;
4343 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004344 case TPACKET_V3:
4345 po->tp_hdrlen = TPACKET3_HDRLEN;
4346 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004347 }
4348
Johann Baudy69e3c752009-05-18 22:11:22 -07004349 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004350 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004351 goto out;
Tobias Klauser90836b62015-11-17 10:40:21 +01004352 if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004353 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004354 min_frame_size = po->tp_hdrlen + po->tp_reserve;
Eric Dumazetdc808112014-08-15 09:16:04 -07004355 if (po->tp_version >= TPACKET_V3 &&
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004356 req->tp_block_size <
4357 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
Eric Dumazetdc808112014-08-15 09:16:04 -07004358 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004359 if (unlikely(req->tp_frame_size < min_frame_size))
Johann Baudy69e3c752009-05-18 22:11:22 -07004360 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004361 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Tobias Klauser4194b492015-11-17 10:38:36 +01004364 rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
4365 if (unlikely(rb->frames_per_block == 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004366 goto out;
Kal Conleyfc628142019-02-10 09:57:11 +01004367 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
Andrey Konovalov8f8d28e2017-03-29 16:11:21 +02004368 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004369 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4370 req->tp_frame_nr))
4371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 err = -ENOMEM;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004374 order = get_order(req->tp_block_size);
4375 pg_vec = alloc_pg_vec(req, order);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004376 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 goto out;
chetan lokef6fb8f12011-08-19 10:18:16 +00004378 switch (po->tp_version) {
4379 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004380 /* Block transmit is not supported yet */
4381 if (!tx_ring) {
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304382 init_prb_bdqc(po, rb, pg_vec, req_u);
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004383 } else {
4384 struct tpacket_req3 *req3 = &req_u->req3;
4385
4386 if (req3->tp_retire_blk_tov ||
4387 req3->tp_sizeof_priv ||
4388 req3->tp_feature_req_word) {
4389 err = -EINVAL;
Eric Dumazet55655e32019-06-24 02:38:20 -07004390 goto out_free_pg_vec;
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004391 }
4392 }
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004393 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004394 default:
Willem de Bruijn61fad682020-03-13 12:18:09 -04004395 if (!tx_ring) {
4396 rx_owner_map = bitmap_alloc(req->tp_frame_nr,
4397 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
4398 if (!rx_owner_map)
4399 goto out_free_pg_vec;
4400 }
chetan lokef6fb8f12011-08-19 10:18:16 +00004401 break;
4402 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004403 }
4404 /* Done */
4405 else {
4406 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004407 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412 /* Detach socket from network */
4413 spin_lock(&po->bind_lock);
4414 was_running = po->running;
4415 num = po->num;
4416 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004418 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 }
4420 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 synchronize_net();
4423
4424 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004425 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 if (closing || atomic_read(&po->mapped) == 0) {
4427 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004428 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004429 swap(rb->pg_vec, pg_vec);
Willem de Bruijn61fad682020-03-13 12:18:09 -04004430 if (po->tp_version <= TPACKET_V2)
4431 swap(rb->rx_owner_map, rx_owner_map);
Johann Baudy69e3c752009-05-18 22:11:22 -07004432 rb->frame_max = (req->tp_frame_nr - 1);
4433 rb->head = 0;
4434 rb->frame_size = req->tp_frame_size;
4435 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004437 swap(rb->pg_vec_order, order);
Changli Gaoc053fd92010-12-10 16:02:20 -08004438 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Johann Baudy69e3c752009-05-18 22:11:22 -07004440 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4441 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4442 tpacket_rcv : packet_rcv;
4443 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004445 pr_err("packet_mmap: vma is busy: %d\n",
4446 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 }
Herbert Xu905db442009-01-30 14:12:06 -08004448 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
4450 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004451 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004453 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455 spin_unlock(&po->bind_lock);
WANG Congc800aaf2017-07-24 10:07:32 -07004456 if (pg_vec && (po->tp_version > TPACKET_V2)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004457 /* Because we don't support block-based V3 on tx-ring */
4458 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004459 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f12011-08-19 10:18:16 +00004460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Eric Dumazet55655e32019-06-24 02:38:20 -07004462out_free_pg_vec:
Willem de Bruijn61fad682020-03-13 12:18:09 -04004463 bitmap_free(rx_owner_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 if (pg_vec)
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004465 free_pg_vec(pg_vec, order, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466out:
4467 return err;
4468}
4469
Johann Baudy69e3c752009-05-18 22:11:22 -07004470static int packet_mmap(struct file *file, struct socket *sock,
4471 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
4473 struct sock *sk = sock->sk;
4474 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004475 unsigned long size, expected_size;
4476 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 unsigned long start;
4478 int err = -EINVAL;
4479 int i;
4480
4481 if (vma->vm_pgoff)
4482 return -EINVAL;
4483
Herbert Xu905db442009-01-30 14:12:06 -08004484 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004485
4486 expected_size = 0;
4487 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4488 if (rb->pg_vec) {
4489 expected_size += rb->pg_vec_len
4490 * rb->pg_vec_pages
4491 * PAGE_SIZE;
4492 }
4493 }
4494
4495 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004497
4498 size = vma->vm_end - vma->vm_start;
4499 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 goto out;
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004503 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4504 if (rb->pg_vec == NULL)
4505 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004506
Johann Baudy69e3c752009-05-18 22:11:22 -07004507 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004508 struct page *page;
4509 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004510 int pg_num;
4511
Changli Gaoc56b4d92010-12-01 02:52:57 +00004512 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4513 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004514 err = vm_insert_page(vma, start, page);
4515 if (unlikely(err))
4516 goto out;
4517 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004518 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004519 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004522
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004523 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 vma->vm_ops = &packet_mmap_ops;
4525 err = 0;
4526
4527out:
Herbert Xu905db442009-01-30 14:12:06 -08004528 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return err;
4530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004532static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 .family = PF_PACKET,
4534 .owner = THIS_MODULE,
4535 .release = packet_release,
4536 .bind = packet_bind_spkt,
4537 .connect = sock_no_connect,
4538 .socketpair = sock_no_socketpair,
4539 .accept = sock_no_accept,
4540 .getname = packet_getname_spkt,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004541 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004543 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 .listen = sock_no_listen,
4545 .shutdown = sock_no_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 .sendmsg = packet_sendmsg_spkt,
4547 .recvmsg = packet_recvmsg,
4548 .mmap = sock_no_mmap,
4549 .sendpage = sock_no_sendpage,
4550};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004552static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 .family = PF_PACKET,
4554 .owner = THIS_MODULE,
4555 .release = packet_release,
4556 .bind = packet_bind,
4557 .connect = sock_no_connect,
4558 .socketpair = sock_no_socketpair,
4559 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004560 .getname = packet_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004561 .poll = packet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004563 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 .listen = sock_no_listen,
4565 .shutdown = sock_no_shutdown,
4566 .setsockopt = packet_setsockopt,
4567 .getsockopt = packet_getsockopt,
4568 .sendmsg = packet_sendmsg,
4569 .recvmsg = packet_recvmsg,
4570 .mmap = packet_mmap,
4571 .sendpage = sock_no_sendpage,
4572};
4573
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004574static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 .family = PF_PACKET,
4576 .create = packet_create,
4577 .owner = THIS_MODULE,
4578};
4579
4580static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004581 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582};
4583
4584#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004587 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588{
Denis V. Luneve372c412007-11-19 22:31:54 -08004589 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004590
4591 rcu_read_lock();
4592 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593}
4594
4595static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4596{
Herbert Xu1bf40952007-12-16 14:04:02 -08004597 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004598 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
4601static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004602 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
stephen hemminger808f5112010-02-22 07:57:18 +00004604 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605}
4606
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004607static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608{
4609 if (v == SEQ_START_TOKEN)
Baruch Siachabdcd062020-12-16 09:28:04 +02004610 seq_printf(seq,
4611 "%*sRefCnt Type Proto Iface R Rmem User Inode\n",
4612 IS_ENABLED(CONFIG_64BIT) ? -17 : -9, "sk");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004614 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 const struct packet_sock *po = pkt_sk(s);
4616
4617 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004618 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004620 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 s->sk_type,
4622 ntohs(po->num),
4623 po->ifindex,
4624 po->running,
4625 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004626 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004627 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
4629
4630 return 0;
4631}
4632
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004633static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 .start = packet_seq_start,
4635 .next = packet_seq_next,
4636 .stop = packet_seq_stop,
4637 .show = packet_seq_show,
4638};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639#endif
4640
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004641static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004642{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004643 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004644 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004645
Yonatan Linika268e0f2020-12-14 22:25:50 +02004646#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02004647 if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
4648 sizeof(struct seq_net_private)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004649 return -ENOMEM;
Yonatan Linika268e0f2020-12-14 22:25:50 +02004650#endif /* CONFIG_PROC_FS */
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004651
4652 return 0;
4653}
4654
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004655static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004656{
Gao fengece31ff2013-02-18 01:34:56 +00004657 remove_proc_entry("packet", net->proc_net);
Vasily Averin669f8f12017-11-12 22:27:49 +03004658 WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004659}
4660
4661static struct pernet_operations packet_net_ops = {
4662 .init = packet_net_init,
4663 .exit = packet_net_exit,
4664};
4665
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667static void __exit packet_exit(void)
4668{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004670 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 sock_unregister(PF_PACKET);
4672 proto_unregister(&packet_proto);
4673}
4674
4675static int __init packet_init(void)
4676{
YueHaibing36096f22019-05-09 22:52:20 +08004677 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
YueHaibing36096f22019-05-09 22:52:20 +08004679 rc = proto_register(&packet_proto, 0);
4680 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 goto out;
YueHaibing36096f22019-05-09 22:52:20 +08004682 rc = sock_register(&packet_family_ops);
4683 if (rc)
4684 goto out_proto;
4685 rc = register_pernet_subsys(&packet_net_ops);
4686 if (rc)
4687 goto out_sock;
4688 rc = register_netdevice_notifier(&packet_netdev_notifier);
4689 if (rc)
4690 goto out_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
YueHaibing36096f22019-05-09 22:52:20 +08004692 return 0;
4693
4694out_pernet:
4695 unregister_pernet_subsys(&packet_net_ops);
4696out_sock:
4697 sock_unregister(PF_PACKET);
4698out_proto:
4699 proto_unregister(&packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700out:
4701 return rc;
4702}
4703
4704module_init(packet_init);
4705module_exit(packet_exit);
4706MODULE_LICENSE("GPL");
4707MODULE_ALIAS_NETPROTO(PF_PACKET);