blob: 71854a16afbbc1c06005e48a65cdb7007d61b019 [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
Baruch Siachdc41c4a2021-09-09 20:49:47 +030049#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
50
Jakub Kicinskicc698372020-11-20 14:50:52 -080051#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080054#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/fcntl.h>
56#include <linux/socket.h>
57#include <linux/in.h>
58#include <linux/inet.h>
59#include <linux/netdevice.h>
60#include <linux/if_packet.h>
61#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080062#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080065#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020066#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/ip.h>
68#include <net/protocol.h>
69#include <linux/skbuff.h>
70#include <net/sock.h>
71#include <linux/errno.h>
72#include <linux/timer.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080073#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/ioctls.h>
75#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040076#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <asm/io.h>
78#include <linux/proc_fs.h>
79#include <linux/seq_file.h>
80#include <linux/poll.h>
81#include <linux/module.h>
82#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080083#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070084#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080085#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000086#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070087#include <linux/net_tstamp.h>
Daniel Borkmannb0138402014-01-15 16:25:36 +010088#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#ifdef CONFIG_INET
90#include <net/inet_common.h>
91#endif
Willem de Bruijn47dceb82015-08-14 22:31:34 -040092#include <linux/bpf.h>
Willem de Bruijn719c44d2016-06-07 12:06:34 -040093#include <net/compat.h>
Pablo Neira Ayuso0d7308c2021-10-08 22:06:04 +020094#include <linux/netfilter_netdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Pavel Emelyanov2787b042012-08-13 05:49:39 +000096#include "internal.h"
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 Assumptions:
Eyal Birgerd5496992020-11-21 08:28:17 +0200100 - If the device has no dev->header_ops->create, there is no LL header
101 visible above the device. In this case, its hard_header_len should be 0.
Xie Heb4c58812020-09-14 00:41:54 -0700102 The device may prepend its own header internally. In this case, its
103 needed_headroom should be set to the space needed for it to add its
104 internal header.
105 For example, a WiFi driver pretending to be an Ethernet driver should
106 set its hard_header_len to be the Ethernet header length, and set its
107 needed_headroom to be (the real WiFi header length - the fake Ethernet
108 header length).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 - packet socket receives packets with pulled ll header,
110 so that SOCK_RAW should push it back.
111
112On receive:
113-----------
114
Eyal Birgerd5496992020-11-21 08:28:17 +0200115Incoming, dev_has_header(dev) == true
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700116 mac_header -> ll header
117 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Eyal Birgerd5496992020-11-21 08:28:17 +0200119Outgoing, dev_has_header(dev) == true
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700120 mac_header -> ll header
121 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Eyal Birgerd5496992020-11-21 08:28:17 +0200123Incoming, dev_has_header(dev) == false
Xie Heb79a80b2020-09-16 05:23:08 -0700124 mac_header -> data
125 However drivers often make it point to the ll header.
126 This is incorrect because the ll header should be invisible to us.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700127 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Eyal Birgerd5496992020-11-21 08:28:17 +0200129Outgoing, dev_has_header(dev) == false
Xie Heb79a80b2020-09-16 05:23:08 -0700130 mac_header -> data. ll header is invisible to us.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700131 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133Resume
Eyal Birgerd5496992020-11-21 08:28:17 +0200134 If dev_has_header(dev) == false we are unable to restore the ll header,
Xie Heb79a80b2020-09-16 05:23:08 -0700135 because it is invisible to us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137
138On transmit:
139------------
140
Xie He21c859742021-02-05 14:41:24 -0800141dev_has_header(dev) == true
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700142 mac_header -> ll header
143 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Xie He21c859742021-02-05 14:41:24 -0800145dev_has_header(dev) == false (ll header is invisible to us)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700146 mac_header -> data
147 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Xie He09599722020-09-18 06:56:16 -0700149 We should set network_header on output to the correct position,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 packet classifier depends on it.
151 */
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Private packet socket structures. */
154
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700155/* identical to struct packet_mreq except it has
156 * a longer address field.
157 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000158struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700159 int mr_ifindex;
160 unsigned short mr_type;
161 unsigned short mr_alen;
162 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
David S. Millera2efcfa2007-05-29 13:12:50 -0700164
Daniel Borkmann184f4892013-04-16 01:57:46 +0000165union tpacket_uhdr {
166 struct tpacket_hdr *h1;
167 struct tpacket2_hdr *h2;
168 struct tpacket3_hdr *h3;
169 void *raw;
170};
171
chetan lokef6fb8f12011-08-19 10:18:16 +0000172static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700173 int closing, int tx_ring);
174
chetan lokef6fb8f12011-08-19 10:18:16 +0000175#define V3_ALIGNMENT (8)
176
chetan lokebc59ba32011-08-25 10:43:30 +0000177#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f12011-08-19 10:18:16 +0000178
179#define BLK_PLUS_PRIV(sz_of_priv) \
180 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
181
chetan lokef6fb8f12011-08-19 10:18:16 +0000182#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
183#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
184#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
185#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
186#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
187#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
chetan lokef6fb8f12011-08-19 10:18:16 +0000188
Johann Baudy69e3c752009-05-18 22:11:22 -0700189struct packet_sock;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000190static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
191 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
chetan lokef6fb8f12011-08-19 10:18:16 +0000193static void *packet_previous_frame(struct packet_sock *po,
194 struct packet_ring_buffer *rb,
195 int status);
196static void packet_increment_head(struct packet_ring_buffer *buff);
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300197static int prb_curr_blk_in_use(struct tpacket_block_desc *);
chetan lokebc59ba32011-08-25 10:43:30 +0000198static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000199 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000200static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000201 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000202static int prb_queue_frozen(struct tpacket_kbdq_core *);
203static void prb_open_block(struct tpacket_kbdq_core *,
204 struct tpacket_block_desc *);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700205static void prb_retire_rx_blk_timer_expired(struct timer_list *);
chetan lokebc59ba32011-08-25 10:43:30 +0000206static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
chetan lokebc59ba32011-08-25 10:43:30 +0000207static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
208static void prb_clear_rxhash(struct tpacket_kbdq_core *,
209 struct tpacket3_hdr *);
210static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
211 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static void packet_flush_mclist(struct sock *sk);
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200213static u16 packet_pick_tx_queue(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Herbert Xuffbc6112007-02-04 23:33:10 -0800215struct packet_skb_cb {
Herbert Xuffbc6112007-02-04 23:33:10 -0800216 union {
217 struct sockaddr_pkt pkt;
Eyal Birger2472d762015-03-01 14:58:28 +0200218 union {
219 /* Trick: alias skb original length with
220 * ll.sll_family and ll.protocol in order
221 * to save room.
222 */
223 unsigned int origlen;
224 struct sockaddr_ll ll;
225 };
Herbert Xuffbc6112007-02-04 23:33:10 -0800226 } sa;
227};
228
David Woodhoused3869ef2015-09-23 19:45:08 +0100229#define vio_le() virtio_legacy_is_little_endian()
230
Herbert Xuffbc6112007-02-04 23:33:10 -0800231#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800232
chetan lokebc59ba32011-08-25 10:43:30 +0000233#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f12011-08-19 10:18:16 +0000234#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000235 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000236#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000237 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000238#define GET_NEXT_PRB_BLK_NUM(x) \
239 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
240 ((x)->kactive_blk_num+1) : 0)
241
David S. Millerdc99f602011-07-05 01:45:05 -0700242static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
243static void __fanout_link(struct sock *sk, struct packet_sock *po);
244
Pablo Neira Ayuso0d7308c2021-10-08 22:06:04 +0200245#ifdef CONFIG_NETFILTER_EGRESS
246static noinline struct sk_buff *nf_hook_direct_egress(struct sk_buff *skb)
247{
248 struct sk_buff *next, *head = NULL, *tail;
249 int rc;
250
251 rcu_read_lock();
252 for (; skb != NULL; skb = next) {
253 next = skb->next;
254 skb_mark_not_on_list(skb);
255
256 if (!nf_hook_egress(skb, &rc, skb->dev))
257 continue;
258
259 if (!head)
260 head = skb;
261 else
262 tail->next = skb;
263
264 tail = skb;
265 }
266 rcu_read_unlock();
267
268 return head;
269}
270#endif
271
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100272static int packet_direct_xmit(struct sk_buff *skb)
273{
Pablo Neira Ayuso0d7308c2021-10-08 22:06:04 +0200274#ifdef CONFIG_NETFILTER_EGRESS
275 if (nf_hook_egress_active()) {
276 skb = nf_hook_direct_egress(skb);
277 if (!skb)
278 return NET_XMIT_DROP;
279 }
280#endif
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200281 return dev_direct_xmit(skb, packet_pick_tx_queue(skb));
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100282}
283
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100284static struct net_device *packet_cached_dev_get(struct packet_sock *po)
285{
286 struct net_device *dev;
287
288 rcu_read_lock();
289 dev = rcu_dereference(po->cached_dev);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800290 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100291 rcu_read_unlock();
292
293 return dev;
294}
295
296static void packet_cached_dev_assign(struct packet_sock *po,
297 struct net_device *dev)
298{
299 rcu_assign_pointer(po->cached_dev, dev);
300}
301
302static void packet_cached_dev_reset(struct packet_sock *po)
303{
304 RCU_INIT_POINTER(po->cached_dev, NULL);
305}
306
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100307static bool packet_use_direct_xmit(const struct packet_sock *po)
308{
309 return po->xmit == packet_direct_xmit;
310}
311
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200312static u16 packet_pick_tx_queue(struct sk_buff *skb)
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100313{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200314 struct net_device *dev = skb->dev;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100315 const struct net_device_ops *ops = dev->netdev_ops;
Paolo Abenib71b5832019-03-20 11:02:05 +0100316 int cpu = raw_smp_processor_id();
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100317 u16 queue_index;
318
Paolo Abenib71b5832019-03-20 11:02:05 +0100319#ifdef CONFIG_XPS
320 skb->sender_cpu = cpu + 1;
321#endif
322 skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100323 if (ops->ndo_select_queue) {
Paolo Abenia350ecc2019-03-20 11:02:06 +0100324 queue_index = ops->ndo_select_queue(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100325 queue_index = netdev_cap_txqueue(dev, queue_index);
326 } else {
Paolo Abenib71b5832019-03-20 11:02:05 +0100327 queue_index = netdev_pick_tx(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100328 }
329
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200330 return queue_index;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100331}
332
Willem de Bruijna6361f02018-04-23 17:37:03 -0400333/* __register_prot_hook must be invoked through register_prot_hook
David S. Millerce06b032011-07-04 01:44:29 -0700334 * or from a context in which asynchronous accesses to the packet
335 * socket is not possible (packet_create()).
336 */
Willem de Bruijna6361f02018-04-23 17:37:03 -0400337static void __register_prot_hook(struct sock *sk)
David S. Millerce06b032011-07-04 01:44:29 -0700338{
339 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100340
David S. Millerce06b032011-07-04 01:44:29 -0700341 if (!po->running) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100342 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700343 __fanout_link(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100344 else
David S. Millerdc99f602011-07-05 01:45:05 -0700345 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100346
David S. Millerce06b032011-07-04 01:44:29 -0700347 sock_hold(sk);
348 po->running = 1;
349 }
350}
351
Willem de Bruijna6361f02018-04-23 17:37:03 -0400352static void register_prot_hook(struct sock *sk)
353{
354 lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
355 __register_prot_hook(sk);
356}
357
358/* If the sync parameter is true, we will temporarily drop
David S. Millerce06b032011-07-04 01:44:29 -0700359 * the po->bind_lock and do a synchronize_net to make sure no
360 * asynchronous packet processing paths still refer to the elements
361 * of po->prot_hook. If the sync parameter is false, it is the
362 * callers responsibility to take care of this.
363 */
364static void __unregister_prot_hook(struct sock *sk, bool sync)
365{
366 struct packet_sock *po = pkt_sk(sk);
367
Willem de Bruijna6361f02018-04-23 17:37:03 -0400368 lockdep_assert_held_once(&po->bind_lock);
369
David S. Millerce06b032011-07-04 01:44:29 -0700370 po->running = 0;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100371
372 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700373 __fanout_unlink(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100374 else
David S. Millerdc99f602011-07-05 01:45:05 -0700375 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100376
David S. Millerce06b032011-07-04 01:44:29 -0700377 __sock_put(sk);
378
379 if (sync) {
380 spin_unlock(&po->bind_lock);
381 synchronize_net();
382 spin_lock(&po->bind_lock);
383 }
384}
385
386static void unregister_prot_hook(struct sock *sk, bool sync)
387{
388 struct packet_sock *po = pkt_sk(sk);
389
390 if (po->running)
391 __unregister_prot_hook(sk, sync);
392}
393
Michael S. Tsirkin6e580402014-11-24 13:32:16 +0200394static inline struct page * __pure pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000395{
396 if (is_vmalloc_addr(addr))
397 return vmalloc_to_page(addr);
398 return virt_to_page(addr);
399}
400
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700401static void __packet_set_status(struct packet_sock *po, void *frame, int status)
402{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000403 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700404
405 h.raw = frame;
406 switch (po->tp_version) {
407 case TPACKET_V1:
408 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000409 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700410 break;
411 case TPACKET_V2:
412 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000413 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700414 break;
chetan lokef6fb8f12011-08-19 10:18:16 +0000415 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800416 h.h3->tp_status = status;
417 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
418 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700419 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000420 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700421 BUG();
422 }
423
424 smp_wmb();
425}
426
Eric Dumazet96f657e2019-06-12 09:52:26 -0700427static int __packet_get_status(const struct packet_sock *po, void *frame)
Johann Baudy69e3c752009-05-18 22:11:22 -0700428{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000429 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700430
431 smp_rmb();
432
433 h.raw = frame;
434 switch (po->tp_version) {
435 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000436 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700437 return h.h1->tp_status;
438 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000439 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700440 return h.h2->tp_status;
chetan lokef6fb8f12011-08-19 10:18:16 +0000441 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800442 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
443 return h.h3->tp_status;
Johann Baudy69e3c752009-05-18 22:11:22 -0700444 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000445 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700446 BUG();
447 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
Johann Baudy69e3c752009-05-18 22:11:22 -0700450
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100451static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts,
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000452 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000453{
454 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
455
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400456 if (shhwtstamps &&
457 (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100458 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts))
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400459 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000460
Richard Sanger171c3b12021-05-12 13:31:22 +1200461 if ((flags & SOF_TIMESTAMPING_SOFTWARE) &&
462 ktime_to_timespec64_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000463 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000464
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000465 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000466}
467
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000468static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
469 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000470{
471 union tpacket_uhdr h;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100472 struct timespec64 ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000473 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000474
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000475 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
476 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000477
478 h.raw = frame;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100479 /*
480 * versions 1 through 3 overflow the timestamps in y2106, since they
481 * all store the seconds in a 32-bit unsigned integer.
482 * If we create a version 4, that should have a 64-bit timestamp,
483 * either 64-bit seconds + 32-bit nanoseconds, or just 64-bit
484 * nanoseconds.
485 */
Willem de Bruijn2e313962013-04-23 00:39:28 +0000486 switch (po->tp_version) {
487 case TPACKET_V1:
488 h.h1->tp_sec = ts.tv_sec;
489 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
490 break;
491 case TPACKET_V2:
492 h.h2->tp_sec = ts.tv_sec;
493 h.h2->tp_nsec = ts.tv_nsec;
494 break;
495 case TPACKET_V3:
Daniel Borkmann57ea8842017-01-05 02:34:28 +0100496 h.h3->tp_sec = ts.tv_sec;
497 h.h3->tp_nsec = ts.tv_nsec;
498 break;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000499 default:
500 WARN(1, "TPACKET version not supported.\n");
501 BUG();
502 }
503
504 /* one flush is safe, as both fields always lie on the same cacheline */
505 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
506 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000507
508 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000509}
510
Eric Dumazetd4b5bd92019-06-12 09:52:27 -0700511static void *packet_lookup_frame(const struct packet_sock *po,
512 const struct packet_ring_buffer *rb,
513 unsigned int position,
514 int status)
Johann Baudy69e3c752009-05-18 22:11:22 -0700515{
516 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000517 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700518
519 pg_vec_pos = position / rb->frames_per_block;
520 frame_offset = position % rb->frames_per_block;
521
Neil Horman0e3125c2010-11-16 10:26:47 -0800522 h.raw = rb->pg_vec[pg_vec_pos].buffer +
523 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700524
525 if (status != __packet_get_status(po, h.raw))
526 return NULL;
527
528 return h.raw;
529}
530
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000531static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700532 struct packet_ring_buffer *rb,
533 int status)
534{
535 return packet_lookup_frame(po, rb, rb->head, status);
536}
537
chetan lokebc59ba32011-08-25 10:43:30 +0000538static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000539{
540 del_timer_sync(&pkc->retire_blk_timer);
541}
542
543static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +0000544 struct sk_buff_head *rb_queue)
545{
chetan lokebc59ba32011-08-25 10:43:30 +0000546 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000547
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200548 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000549
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100550 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000551 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100552 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000553
554 prb_del_retire_blk_timer(pkc);
555}
556
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530557static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f12011-08-19 10:18:16 +0000558{
chetan lokebc59ba32011-08-25 10:43:30 +0000559 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000560
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530561 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700562 timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired,
563 0);
564 pkc->retire_blk_timer.expires = jiffies;
chetan lokef6fb8f12011-08-19 10:18:16 +0000565}
566
567static int prb_calc_retire_blk_tmo(struct packet_sock *po,
568 int blk_size_in_bytes)
569{
570 struct net_device *dev;
Mao Wenan0914d2b2019-12-23 18:42:57 +0800571 unsigned int mbits, div;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800572 struct ethtool_link_ksettings ecmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000573 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +0000574
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000575 rtnl_lock();
576 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
577 if (unlikely(!dev)) {
578 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000579 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000580 }
David Decotigny7cad1ba2016-02-24 10:58:10 -0800581 err = __ethtool_get_link_ksettings(dev, &ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000582 rtnl_unlock();
Mao Wenan0914d2b2019-12-23 18:42:57 +0800583 if (err)
Mao Wenanb43d1f92019-12-09 21:31:25 +0800584 return DEFAULT_PRB_RETIRE_TOV;
chetan lokef6fb8f12011-08-19 10:18:16 +0000585
Mao Wenan0914d2b2019-12-23 18:42:57 +0800586 /* If the link speed is so slow you don't really
587 * need to worry about perf anyways
588 */
589 if (ecmd.base.speed < SPEED_1000 ||
590 ecmd.base.speed == SPEED_UNKNOWN)
591 return DEFAULT_PRB_RETIRE_TOV;
592
593 div = ecmd.base.speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000594 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
595
596 if (div)
597 mbits /= div;
598
chetan lokef6fb8f12011-08-19 10:18:16 +0000599 if (div)
Mao Wenan0914d2b2019-12-23 18:42:57 +0800600 return mbits + 1;
601 return mbits;
chetan lokef6fb8f12011-08-19 10:18:16 +0000602}
603
chetan lokebc59ba32011-08-25 10:43:30 +0000604static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000605 union tpacket_req_u *req_u)
606{
607 p1->feature_req_word = req_u->req3.tp_feature_req_word;
608}
609
610static void init_prb_bdqc(struct packet_sock *po,
611 struct packet_ring_buffer *rb,
612 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530613 union tpacket_req_u *req_u)
chetan lokef6fb8f12011-08-19 10:18:16 +0000614{
Duan Jiong22781a52013-12-06 13:29:36 +0800615 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000616 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000617
618 memset(p1, 0x0, sizeof(*p1));
619
620 p1->knxt_seq_num = 1;
621 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000622 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000623 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000624 p1->kblk_size = req_u->req3.tp_block_size;
625 p1->knum_blocks = req_u->req3.tp_block_nr;
626 p1->hdrlen = po->tp_hdrlen;
627 p1->version = po->tp_version;
628 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000629 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000630 if (req_u->req3.tp_retire_blk_tov)
631 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
632 else
633 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
634 req_u->req3.tp_block_size);
635 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
636 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
John Ogness632ca502020-07-07 17:28:04 +0206637 rwlock_init(&p1->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000638
Eric Dumazetdc808112014-08-15 09:16:04 -0700639 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000640 prb_init_ft_ops(p1, req_u);
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530641 prb_setup_retire_blk_timer(po);
chetan lokef6fb8f12011-08-19 10:18:16 +0000642 prb_open_block(p1, pbd);
643}
644
645/* Do NOT update the last_blk_num first.
646 * Assumes sk_buff_head lock is held.
647 */
chetan lokebc59ba32011-08-25 10:43:30 +0000648static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000649{
650 mod_timer(&pkc->retire_blk_timer,
651 jiffies + pkc->tov_in_jiffies);
652 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
653}
654
655/*
656 * Timer logic:
657 * 1) We refresh the timer only when we open a block.
658 * By doing this we don't waste cycles refreshing the timer
659 * on packet-by-packet basis.
660 *
661 * With a 1MB block-size, on a 1Gbps line, it will take
662 * i) ~8 ms to fill a block + ii) memcpy etc.
663 * In this cut we are not accounting for the memcpy time.
664 *
665 * So, if the user sets the 'tmo' to 10ms then the timer
666 * will never fire while the block is still getting filled
667 * (which is what we want). However, the user could choose
668 * to close a block early and that's fine.
669 *
670 * But when the timer does fire, we check whether or not to refresh it.
671 * Since the tmo granularity is in msecs, it is not too expensive
672 * to refresh the timer, lets say every '8' msecs.
673 * Either the user can set the 'tmo' or we can derive it based on
674 * a) line-speed and b) block-size.
675 * prb_calc_retire_blk_tmo() calculates the tmo.
676 *
677 */
Kees Cook17bfd8c2017-10-24 01:46:26 -0700678static void prb_retire_rx_blk_timer_expired(struct timer_list *t)
chetan lokef6fb8f12011-08-19 10:18:16 +0000679{
Kees Cook17bfd8c2017-10-24 01:46:26 -0700680 struct packet_sock *po =
681 from_timer(po, t, rx_ring.prb_bdqc.retire_blk_timer);
Duan Jiong22781a52013-12-06 13:29:36 +0800682 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000683 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000684 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000685
686 spin_lock(&po->sk.sk_receive_queue.lock);
687
688 frozen = prb_queue_frozen(pkc);
689 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
690
691 if (unlikely(pkc->delete_blk_timer))
692 goto out;
693
694 /* We only need to plug the race when the block is partially filled.
695 * tpacket_rcv:
696 * lock(); increment BLOCK_NUM_PKTS; unlock()
697 * copy_bits() is in progress ...
698 * timer fires on other cpu:
699 * we can't retire the current block because copy_bits
700 * is in progress.
701 *
702 */
703 if (BLOCK_NUM_PKTS(pbd)) {
John Ogness632ca502020-07-07 17:28:04 +0206704 /* Waiting for skb_copy_bits to finish... */
705 write_lock(&pkc->blk_fill_in_prog_lock);
706 write_unlock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000707 }
708
709 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
710 if (!frozen) {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300711 if (!BLOCK_NUM_PKTS(pbd)) {
712 /* An empty block. Just refresh the timer. */
713 goto refresh_timer;
714 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000715 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
716 if (!prb_dispatch_next_block(pkc, po))
717 goto refresh_timer;
718 else
719 goto out;
720 } else {
721 /* Case 1. Queue was frozen because user-space was
722 * lagging behind.
723 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300724 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +0000725 /*
726 * Ok, user-space is still behind.
727 * So just refresh the timer.
728 */
729 goto refresh_timer;
730 } else {
731 /* Case 2. queue was frozen,user-space caught up,
732 * now the link went idle && the timer fired.
733 * We don't have a block to close.So we open this
734 * block and restart the timer.
735 * opening a block thaws the queue,restarts timer
736 * Thawing/timer-refresh is a side effect.
737 */
738 prb_open_block(pkc, pbd);
739 goto out;
740 }
741 }
742 }
743
744refresh_timer:
745 _prb_refresh_rx_retire_blk_timer(pkc);
746
747out:
748 spin_unlock(&po->sk.sk_receive_queue.lock);
749}
750
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000751static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000752 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000753{
754 /* Flush everything minus the block header */
755
756#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
757 u8 *start, *end;
758
759 start = (u8 *)pbd1;
760
761 /* Skip the block header(we know header WILL fit in 4K) */
762 start += PAGE_SIZE;
763
764 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
765 for (; start < end; start += PAGE_SIZE)
766 flush_dcache_page(pgv_to_page(start));
767
768 smp_wmb();
769#endif
770
771 /* Now update the block status. */
772
773 BLOCK_STATUS(pbd1) = status;
774
775 /* Flush the block header */
776
777#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
778 start = (u8 *)pbd1;
779 flush_dcache_page(pgv_to_page(start));
780
781 smp_wmb();
782#endif
783}
784
785/*
786 * Side effect:
787 *
788 * 1) flush the block
789 * 2) Increment active_blk_num
790 *
791 * Note:We DONT refresh the timer on purpose.
792 * Because almost always the next block will be opened.
793 */
chetan lokebc59ba32011-08-25 10:43:30 +0000794static void prb_close_block(struct tpacket_kbdq_core *pkc1,
795 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000796 struct packet_sock *po, unsigned int stat)
797{
798 __u32 status = TP_STATUS_USER | stat;
799
800 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000801 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
Dan Collinsda413ee2014-12-19 16:49:25 +1300802 struct sock *sk = &po->sk;
chetan lokef6fb8f12011-08-19 10:18:16 +0000803
Eric Dumazet8e8e2952019-06-12 09:52:30 -0700804 if (atomic_read(&po->tp_drops))
chetan lokef6fb8f12011-08-19 10:18:16 +0000805 status |= TP_STATUS_LOSING;
806
807 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
808 last_pkt->tp_next_offset = 0;
809
810 /* Get the ts of the last pkt */
811 if (BLOCK_NUM_PKTS(pbd1)) {
812 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
813 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
814 } else {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300815 /* Ok, we tmo'd - so get the current time.
816 *
817 * It shouldn't really happen as we don't close empty
818 * blocks. See prb_retire_rx_blk_timer_expired().
819 */
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100820 struct timespec64 ts;
821 ktime_get_real_ts64(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000822 h1->ts_last_pkt.ts_sec = ts.tv_sec;
823 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
824 }
825
826 smp_wmb();
827
828 /* Flush the block */
829 prb_flush_block(pkc1, pbd1, status);
830
Dan Collinsda413ee2014-12-19 16:49:25 +1300831 sk->sk_data_ready(sk);
832
chetan lokef6fb8f12011-08-19 10:18:16 +0000833 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
834}
835
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000836static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000837{
838 pkc->reset_pending_on_curr_blk = 0;
839}
840
841/*
842 * Side effect of opening a block:
843 *
844 * 1) prb_queue is thawed.
845 * 2) retire_blk_timer is refreshed.
846 *
847 */
chetan lokebc59ba32011-08-25 10:43:30 +0000848static void prb_open_block(struct tpacket_kbdq_core *pkc1,
849 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000850{
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100851 struct timespec64 ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000852 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000853
854 smp_rmb();
855
Daniel Borkmann8da30562013-05-03 02:57:00 +0000856 /* We could have just memset this but we will lose the
857 * flexibility of making the priv area sticky
858 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000859
Daniel Borkmann8da30562013-05-03 02:57:00 +0000860 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
861 BLOCK_NUM_PKTS(pbd1) = 0;
862 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000863
Arnd Bergmannd413fcb2017-11-27 10:09:24 +0100864 ktime_get_real_ts64(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000865
Daniel Borkmann8da30562013-05-03 02:57:00 +0000866 h1->ts_first_pkt.ts_sec = ts.tv_sec;
867 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f12011-08-19 10:18:16 +0000868
Daniel Borkmann8da30562013-05-03 02:57:00 +0000869 pkc1->pkblk_start = (char *)pbd1;
870 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
871
872 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
873 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
874
875 pbd1->version = pkc1->version;
876 pkc1->prev = pkc1->nxt_offset;
877 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
878
879 prb_thaw_queue(pkc1);
880 _prb_refresh_rx_retire_blk_timer(pkc1);
881
882 smp_wmb();
chetan lokef6fb8f12011-08-19 10:18:16 +0000883}
884
885/*
886 * Queue freeze logic:
887 * 1) Assume tp_block_nr = 8 blocks.
888 * 2) At time 't0', user opens Rx ring.
889 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
890 * 4) user-space is either sleeping or processing block '0'.
891 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
892 * it will close block-7,loop around and try to fill block '0'.
893 * call-flow:
894 * __packet_lookup_frame_in_block
895 * prb_retire_current_block()
896 * prb_dispatch_next_block()
897 * |->(BLOCK_STATUS == USER) evaluates to true
898 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
899 * 6) Now there are two cases:
900 * 6.1) Link goes idle right after the queue is frozen.
901 * But remember, the last open_block() refreshed the timer.
902 * When this timer expires,it will refresh itself so that we can
903 * re-open block-0 in near future.
904 * 6.2) Link is busy and keeps on receiving packets. This is a simple
905 * case and __packet_lookup_frame_in_block will check if block-0
906 * is free and can now be re-used.
907 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000908static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000909 struct packet_sock *po)
910{
911 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000912 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f12011-08-19 10:18:16 +0000913}
914
915#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
916
917/*
918 * If the next block is free then we will dispatch it
919 * and return a good offset.
920 * Else, we will freeze the queue.
921 * So, caller must check the return value.
922 */
chetan lokebc59ba32011-08-25 10:43:30 +0000923static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000924 struct packet_sock *po)
925{
chetan lokebc59ba32011-08-25 10:43:30 +0000926 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000927
928 smp_rmb();
929
930 /* 1. Get current block num */
931 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
932
933 /* 2. If this block is currently in_use then freeze the queue */
934 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
935 prb_freeze_queue(pkc, po);
936 return NULL;
937 }
938
939 /*
940 * 3.
941 * open this block and return the offset where the first packet
942 * needs to get stored.
943 */
944 prb_open_block(pkc, pbd);
945 return (void *)pkc->nxt_offset;
946}
947
chetan lokebc59ba32011-08-25 10:43:30 +0000948static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000949 struct packet_sock *po, unsigned int status)
950{
chetan lokebc59ba32011-08-25 10:43:30 +0000951 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000952
953 /* retire/close the current block */
954 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
955 /*
956 * Plug the case where copy_bits() is in progress on
957 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
958 * have space to copy the pkt in the current block and
959 * called prb_retire_current_block()
960 *
961 * We don't need to worry about the TMO case because
962 * the timer-handler already handled this case.
963 */
964 if (!(status & TP_STATUS_BLK_TMO)) {
John Ogness632ca502020-07-07 17:28:04 +0206965 /* Waiting for skb_copy_bits to finish... */
966 write_lock(&pkc->blk_fill_in_prog_lock);
967 write_unlock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000968 }
969 prb_close_block(pkc, pbd, po, status);
970 return;
971 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000972}
973
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300974static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000975{
976 return TP_STATUS_USER & BLOCK_STATUS(pbd);
977}
978
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000979static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000980{
981 return pkc->reset_pending_on_curr_blk;
982}
983
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000984static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
John Ogness88fd1cb2020-08-13 21:45:25 +0206985 __releases(&pkc->blk_fill_in_prog_lock)
chetan lokef6fb8f12011-08-19 10:18:16 +0000986{
chetan lokebc59ba32011-08-25 10:43:30 +0000987 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
John Ogness632ca502020-07-07 17:28:04 +0206988
989 read_unlock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000990}
991
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000992static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000993 struct tpacket3_hdr *ppd)
994{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800995 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000996}
997
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000998static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000999 struct tpacket3_hdr *ppd)
1000{
1001 ppd->hv1.tp_rxhash = 0;
1002}
1003
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001004static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +00001005 struct tpacket3_hdr *ppd)
1006{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001007 if (skb_vlan_tag_present(pkc->skb)) {
1008 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09001009 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
1010 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
chetan lokef6fb8f12011-08-19 10:18:16 +00001011 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00001012 ppd->hv1.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09001013 ppd->hv1.tp_vlan_tpid = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00001014 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +00001015 }
1016}
1017
chetan lokebc59ba32011-08-25 10:43:30 +00001018static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +00001019 struct tpacket3_hdr *ppd)
1020{
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09001021 ppd->hv1.tp_padding = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +00001022 prb_fill_vlan_info(pkc, ppd);
1023
1024 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
1025 prb_fill_rxhash(pkc, ppd);
1026 else
1027 prb_clear_rxhash(pkc, ppd);
1028}
1029
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001030static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +00001031 struct tpacket_kbdq_core *pkc,
1032 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +00001033 unsigned int len)
John Ogness88fd1cb2020-08-13 21:45:25 +02061034 __acquires(&pkc->blk_fill_in_prog_lock)
chetan lokef6fb8f12011-08-19 10:18:16 +00001035{
1036 struct tpacket3_hdr *ppd;
1037
1038 ppd = (struct tpacket3_hdr *)curr;
1039 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
1040 pkc->prev = curr;
1041 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
1042 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1043 BLOCK_NUM_PKTS(pbd) += 1;
John Ogness632ca502020-07-07 17:28:04 +02061044 read_lock(&pkc->blk_fill_in_prog_lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00001045 prb_run_all_ft_ops(pkc, ppd);
1046}
1047
1048/* Assumes caller has the sk->rx_queue.lock */
1049static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1050 struct sk_buff *skb,
chetan lokef6fb8f12011-08-19 10:18:16 +00001051 unsigned int len
1052 )
1053{
chetan lokebc59ba32011-08-25 10:43:30 +00001054 struct tpacket_kbdq_core *pkc;
1055 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +00001056 char *curr, *end;
1057
Joe Perchese3192692012-06-03 17:41:40 +00001058 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +00001059 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1060
1061 /* Queue is frozen when user space is lagging behind */
1062 if (prb_queue_frozen(pkc)) {
1063 /*
1064 * Check if that last block which caused the queue to freeze,
1065 * is still in_use by user-space.
1066 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +03001067 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001068 /* Can't record this packet */
1069 return NULL;
1070 } else {
1071 /*
1072 * Ok, the block was released by user-space.
1073 * Now let's open that block.
1074 * opening a block also thaws the queue.
1075 * Thawing is a side effect.
1076 */
1077 prb_open_block(pkc, pbd);
1078 }
1079 }
1080
1081 smp_mb();
1082 curr = pkc->nxt_offset;
1083 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +00001084 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +00001085
1086 /* first try the current block */
1087 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1088 prb_fill_curr_block(curr, pkc, pbd, len);
1089 return (void *)curr;
1090 }
1091
1092 /* Ok, close the current block */
1093 prb_retire_current_block(pkc, po, 0);
1094
1095 /* Now, try to dispatch the next block */
1096 curr = (char *)prb_dispatch_next_block(pkc, po);
1097 if (curr) {
1098 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1099 prb_fill_curr_block(curr, pkc, pbd, len);
1100 return (void *)curr;
1101 }
1102
1103 /*
1104 * No free blocks are available.user_space hasn't caught up yet.
1105 * Queue was just frozen and now this packet will get dropped.
1106 */
1107 return NULL;
1108}
1109
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001110static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001111 struct sk_buff *skb,
1112 int status, unsigned int len)
1113{
1114 char *curr = NULL;
1115 switch (po->tp_version) {
1116 case TPACKET_V1:
1117 case TPACKET_V2:
1118 curr = packet_lookup_frame(po, &po->rx_ring,
1119 po->rx_ring.head, status);
1120 return curr;
1121 case TPACKET_V3:
Mao Wenan46088052019-06-11 09:32:13 +08001122 return __packet_lookup_frame_in_block(po, skb, len);
chetan lokef6fb8f12011-08-19 10:18:16 +00001123 default:
1124 WARN(1, "TPACKET version not supported\n");
1125 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001126 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +00001127 }
1128}
1129
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001130static void *prb_lookup_block(const struct packet_sock *po,
1131 const struct packet_ring_buffer *rb,
1132 unsigned int idx,
1133 int status)
chetan lokef6fb8f12011-08-19 10:18:16 +00001134{
chetan lokebc59ba32011-08-25 10:43:30 +00001135 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001136 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001137
1138 if (status != BLOCK_STATUS(pbd))
1139 return NULL;
1140 return pbd;
1141}
1142
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001143static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001144{
1145 unsigned int prev;
1146 if (rb->prb_bdqc.kactive_blk_num)
1147 prev = rb->prb_bdqc.kactive_blk_num-1;
1148 else
1149 prev = rb->prb_bdqc.knum_blocks-1;
1150 return prev;
1151}
1152
1153/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001154static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001155 struct packet_ring_buffer *rb,
1156 int status)
1157{
1158 unsigned int previous = prb_previous_blk_num(rb);
1159 return prb_lookup_block(po, rb, previous, status);
1160}
1161
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001162static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001163 struct packet_ring_buffer *rb,
1164 int status)
1165{
1166 if (po->tp_version <= TPACKET_V2)
1167 return packet_previous_frame(po, rb, status);
1168
1169 return __prb_previous_block(po, rb, status);
1170}
1171
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001172static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001173 struct packet_ring_buffer *rb)
1174{
1175 switch (po->tp_version) {
1176 case TPACKET_V1:
1177 case TPACKET_V2:
1178 return packet_increment_head(rb);
1179 case TPACKET_V3:
1180 default:
1181 WARN(1, "TPACKET version not supported.\n");
1182 BUG();
1183 return;
1184 }
1185}
1186
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001187static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001188 struct packet_ring_buffer *rb,
1189 int status)
1190{
1191 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1192 return packet_lookup_frame(po, rb, previous, status);
1193}
1194
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001195static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001196{
1197 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1198}
1199
Daniel Borkmannb0138402014-01-15 16:25:36 +01001200static void packet_inc_pending(struct packet_ring_buffer *rb)
1201{
1202 this_cpu_inc(*rb->pending_refcnt);
1203}
1204
1205static void packet_dec_pending(struct packet_ring_buffer *rb)
1206{
1207 this_cpu_dec(*rb->pending_refcnt);
1208}
1209
1210static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1211{
1212 unsigned int refcnt = 0;
1213 int cpu;
1214
1215 /* We don't use pending refcount in rx_ring. */
1216 if (rb->pending_refcnt == NULL)
1217 return 0;
1218
1219 for_each_possible_cpu(cpu)
1220 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1221
1222 return refcnt;
1223}
1224
1225static int packet_alloc_pending(struct packet_sock *po)
1226{
1227 po->rx_ring.pending_refcnt = NULL;
1228
1229 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1230 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1231 return -ENOBUFS;
1232
1233 return 0;
1234}
1235
1236static void packet_free_pending(struct packet_sock *po)
1237{
1238 free_percpu(po->tx_ring.pending_refcnt);
1239}
1240
Willem de Bruijn99547292015-05-12 11:56:47 -04001241#define ROOM_POW_OFF 2
1242#define ROOM_NONE 0x0
1243#define ROOM_LOW 0x1
1244#define ROOM_NORMAL 0x2
1245
Eric Dumazetd4b5bd92019-06-12 09:52:27 -07001246static bool __tpacket_has_room(const struct packet_sock *po, int pow_off)
Willem de Bruijn99547292015-05-12 11:56:47 -04001247{
1248 int idx, len;
1249
Eric Dumazetd4b5bd92019-06-12 09:52:27 -07001250 len = READ_ONCE(po->rx_ring.frame_max) + 1;
1251 idx = READ_ONCE(po->rx_ring.head);
Willem de Bruijn99547292015-05-12 11:56:47 -04001252 if (pow_off)
1253 idx += len >> pow_off;
1254 if (idx >= len)
1255 idx -= len;
1256 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1257}
1258
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001259static bool __tpacket_v3_has_room(const struct packet_sock *po, int pow_off)
Willem de Bruijn99547292015-05-12 11:56:47 -04001260{
1261 int idx, len;
1262
Eric Dumazetdcf70ce2019-06-12 09:52:28 -07001263 len = READ_ONCE(po->rx_ring.prb_bdqc.knum_blocks);
1264 idx = READ_ONCE(po->rx_ring.prb_bdqc.kactive_blk_num);
Willem de Bruijn99547292015-05-12 11:56:47 -04001265 if (pow_off)
1266 idx += len >> pow_off;
1267 if (idx >= len)
1268 idx -= len;
1269 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1270}
1271
Eric Dumazet0338a142019-06-12 09:52:29 -07001272static int __packet_rcv_has_room(const struct packet_sock *po,
1273 const struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001274{
Eric Dumazet0338a142019-06-12 09:52:29 -07001275 const struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001276 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001277
Willem de Bruijn99547292015-05-12 11:56:47 -04001278 if (po->prot_hook.func != tpacket_rcv) {
Eric Dumazet0338a142019-06-12 09:52:29 -07001279 int rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1280 int avail = rcvbuf - atomic_read(&sk->sk_rmem_alloc)
1281 - (skb ? skb->truesize : 0);
1282
1283 if (avail > (rcvbuf >> ROOM_POW_OFF))
Willem de Bruijn99547292015-05-12 11:56:47 -04001284 return ROOM_NORMAL;
1285 else if (avail > 0)
1286 return ROOM_LOW;
1287 else
1288 return ROOM_NONE;
1289 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001290
Willem de Bruijn99547292015-05-12 11:56:47 -04001291 if (po->tp_version == TPACKET_V3) {
1292 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1293 ret = ROOM_NORMAL;
1294 else if (__tpacket_v3_has_room(po, 0))
1295 ret = ROOM_LOW;
1296 } else {
1297 if (__tpacket_has_room(po, ROOM_POW_OFF))
1298 ret = ROOM_NORMAL;
1299 else if (__tpacket_has_room(po, 0))
1300 ret = ROOM_LOW;
1301 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001302
1303 return ret;
1304}
1305
1306static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1307{
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001308 int pressure, ret;
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001309
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001310 ret = __packet_rcv_has_room(po, skb);
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001311 pressure = ret != ROOM_NORMAL;
1312
1313 if (READ_ONCE(po->pressure) != pressure)
1314 WRITE_ONCE(po->pressure, pressure);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001315
Willem de Bruijn99547292015-05-12 11:56:47 -04001316 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001317}
1318
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07001319static void packet_rcv_try_clear_pressure(struct packet_sock *po)
1320{
1321 if (READ_ONCE(po->pressure) &&
1322 __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
1323 WRITE_ONCE(po->pressure, 0);
1324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326static void packet_sock_destruct(struct sock *sk)
1327{
Richard Cochraned85b562010-04-07 22:41:28 +00001328 skb_queue_purge(&sk->sk_error_queue);
1329
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001330 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +03001331 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001334 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return;
1336 }
1337
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001338 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001341static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1342{
Eric Dumazetb756ad92019-11-08 05:07:46 -08001343 u32 *history = po->rollover->history;
1344 u32 victim, rxhash;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001345 int i, count = 0;
1346
1347 rxhash = skb_get_hash(skb);
1348 for (i = 0; i < ROLLOVER_HLEN; i++)
Eric Dumazetb756ad92019-11-08 05:07:46 -08001349 if (READ_ONCE(history[i]) == rxhash)
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001350 count++;
1351
Eric Dumazetb756ad92019-11-08 05:07:46 -08001352 victim = prandom_u32() % ROLLOVER_HLEN;
1353
1354 /* Avoid dirtying the cache line if possible */
1355 if (READ_ONCE(history[victim]) != rxhash)
1356 WRITE_ONCE(history[victim], rxhash);
1357
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001358 return count > (ROLLOVER_HLEN >> 1);
1359}
1360
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001361static unsigned int fanout_demux_hash(struct packet_fanout *f,
1362 struct sk_buff *skb,
1363 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001364{
David S. Millereb70db82016-07-01 16:07:50 -04001365 return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001366}
1367
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001368static unsigned int fanout_demux_lb(struct packet_fanout *f,
1369 struct sk_buff *skb,
1370 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001371{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001372 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001373
Willem de Bruijn468479e2015-06-17 15:59:34 -04001374 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001375}
1376
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001377static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1378 struct sk_buff *skb,
1379 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001380{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001381 return smp_processor_id() % num;
1382}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001383
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001384static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1385 struct sk_buff *skb,
1386 unsigned int num)
1387{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001388 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001389}
1390
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001391static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1392 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001393 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001394 unsigned int num)
1395{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001396 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001397 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001398
Eric Dumazet94f633e2021-04-14 12:36:44 -07001399 po = pkt_sk(rcu_dereference(f->arr[idx]));
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001400
1401 if (try_self) {
1402 room = packet_rcv_has_room(po, skb);
1403 if (room == ROOM_NORMAL ||
1404 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1405 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001406 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001407 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001408
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001409 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001410 do {
Eric Dumazet94f633e2021-04-14 12:36:44 -07001411 po_next = pkt_sk(rcu_dereference(f->arr[i]));
Eric Dumazet3a2bb842019-06-12 09:52:32 -07001412 if (po_next != po_skip && !READ_ONCE(po_next->pressure) &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001413 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001414 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001415 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001416 atomic_long_inc(&po->rollover->num);
1417 if (room == ROOM_LOW)
1418 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001419 return i;
1420 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001421
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001422 if (++i == num)
1423 i = 0;
1424 } while (i != j);
1425
Willem de Bruijna9b63912015-05-12 11:56:50 -04001426 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001427 return idx;
1428}
1429
Neil Horman2d360972014-01-22 16:01:44 -05001430static unsigned int fanout_demux_qm(struct packet_fanout *f,
1431 struct sk_buff *skb,
1432 unsigned int num)
1433{
1434 return skb_get_queue_mapping(skb) % num;
1435}
1436
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001437static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1438 struct sk_buff *skb,
1439 unsigned int num)
1440{
1441 struct bpf_prog *prog;
1442 unsigned int ret = 0;
1443
1444 rcu_read_lock();
1445 prog = rcu_dereference(f->bpf_prog);
1446 if (prog)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001447 ret = bpf_prog_run_clear_cb(prog, skb) % num;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001448 rcu_read_unlock();
1449
1450 return ret;
1451}
1452
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001453static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1454{
1455 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001456}
1457
David S. Miller95ec3eb2011-07-06 01:56:38 -07001458static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1459 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001460{
1461 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001462 unsigned int num = READ_ONCE(f->num_members);
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001463 struct net *net = read_pnet(&f->net);
David S. Millerdc99f602011-07-05 01:45:05 -07001464 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001465 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001466
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001467 if (!net_eq(dev_net(dev), net) || !num) {
David S. Millerdc99f602011-07-05 01:45:05 -07001468 kfree_skb(skb);
1469 return 0;
1470 }
1471
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001472 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001473 skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001474 if (!skb)
1475 return 0;
1476 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001477 switch (f->type) {
1478 case PACKET_FANOUT_HASH:
1479 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001480 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001481 break;
1482 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001483 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001484 break;
1485 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001486 idx = fanout_demux_cpu(f, skb, num);
1487 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001488 case PACKET_FANOUT_RND:
1489 idx = fanout_demux_rnd(f, skb, num);
1490 break;
Neil Horman2d360972014-01-22 16:01:44 -05001491 case PACKET_FANOUT_QM:
1492 idx = fanout_demux_qm(f, skb, num);
1493 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001494 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001495 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001496 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001497 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001498 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001499 idx = fanout_demux_bpf(f, skb, num);
1500 break;
David S. Miller7736d332011-07-05 01:43:20 -07001501 }
1502
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001503 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1504 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001505
Eric Dumazet94f633e2021-04-14 12:36:44 -07001506 po = pkt_sk(rcu_dereference(f->arr[idx]));
David S. Millerdc99f602011-07-05 01:45:05 -07001507 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1508}
1509
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001510DEFINE_MUTEX(fanout_mutex);
1511EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001512static LIST_HEAD(fanout_list);
Mike Maloney4a69a862017-04-21 10:56:11 -04001513static u16 fanout_next_id;
David S. Millerdc99f602011-07-05 01:45:05 -07001514
1515static void __fanout_link(struct sock *sk, struct packet_sock *po)
1516{
1517 struct packet_fanout *f = po->fanout;
1518
1519 spin_lock(&f->lock);
Eric Dumazet94f633e2021-04-14 12:36:44 -07001520 rcu_assign_pointer(f->arr[f->num_members], sk);
David S. Millerdc99f602011-07-05 01:45:05 -07001521 smp_wmb();
1522 f->num_members++;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001523 if (f->num_members == 1)
1524 dev_add_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001525 spin_unlock(&f->lock);
1526}
1527
1528static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1529{
1530 struct packet_fanout *f = po->fanout;
1531 int i;
1532
1533 spin_lock(&f->lock);
1534 for (i = 0; i < f->num_members; i++) {
Eric Dumazet94f633e2021-04-14 12:36:44 -07001535 if (rcu_dereference_protected(f->arr[i],
1536 lockdep_is_held(&f->lock)) == sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001537 break;
1538 }
1539 BUG_ON(i >= f->num_members);
Eric Dumazet94f633e2021-04-14 12:36:44 -07001540 rcu_assign_pointer(f->arr[i],
1541 rcu_dereference_protected(f->arr[f->num_members - 1],
1542 lockdep_is_held(&f->lock)));
David S. Millerdc99f602011-07-05 01:45:05 -07001543 f->num_members--;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001544 if (f->num_members == 0)
1545 __dev_remove_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001546 spin_unlock(&f->lock);
1547}
1548
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001549static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001550{
Eric Dumazet161642e2015-10-09 11:29:32 -07001551 if (sk->sk_family != PF_PACKET)
1552 return false;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001553
Eric Dumazet161642e2015-10-09 11:29:32 -07001554 return ptype->af_packet_priv == pkt_sk(sk)->fanout;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001555}
1556
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001557static void fanout_init_data(struct packet_fanout *f)
1558{
1559 switch (f->type) {
1560 case PACKET_FANOUT_LB:
1561 atomic_set(&f->rr_cur, 0);
1562 break;
1563 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001564 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001565 RCU_INIT_POINTER(f->bpf_prog, NULL);
1566 break;
1567 }
1568}
1569
1570static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1571{
1572 struct bpf_prog *old;
1573
1574 spin_lock(&f->lock);
1575 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1576 rcu_assign_pointer(f->bpf_prog, new);
1577 spin_unlock(&f->lock);
1578
1579 if (old) {
1580 synchronize_net();
1581 bpf_prog_destroy(old);
1582 }
1583}
1584
Christoph Hellwigb1ea9ff2020-07-23 08:08:47 +02001585static int fanout_set_data_cbpf(struct packet_sock *po, sockptr_t data,
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001586 unsigned int len)
1587{
1588 struct bpf_prog *new;
1589 struct sock_fprog fprog;
1590 int ret;
1591
1592 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1593 return -EPERM;
Christoph Hellwig4d295e52020-07-17 08:23:13 +02001594
1595 ret = copy_bpf_fprog_from_user(&fprog, data, len);
1596 if (ret)
1597 return ret;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001598
Daniel Borkmannbab18992015-10-02 15:17:33 +02001599 ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001600 if (ret)
1601 return ret;
1602
1603 __fanout_set_data_bpf(po->fanout, new);
1604 return 0;
1605}
1606
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001607static int fanout_set_data_ebpf(struct packet_sock *po, sockptr_t data,
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001608 unsigned int len)
1609{
1610 struct bpf_prog *new;
1611 u32 fd;
1612
1613 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1614 return -EPERM;
1615 if (len != sizeof(fd))
1616 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001617 if (copy_from_sockptr(&fd, data, len))
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001618 return -EFAULT;
1619
Daniel Borkmann113214b2016-06-30 17:24:44 +02001620 new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001621 if (IS_ERR(new))
1622 return PTR_ERR(new);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001623
1624 __fanout_set_data_bpf(po->fanout, new);
1625 return 0;
1626}
1627
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001628static int fanout_set_data(struct packet_sock *po, sockptr_t data,
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001629 unsigned int len)
1630{
1631 switch (po->fanout->type) {
1632 case PACKET_FANOUT_CBPF:
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001633 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001634 case PACKET_FANOUT_EBPF:
1635 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001636 default:
1637 return -EINVAL;
zhong jiang07d53ae2018-08-04 19:41:41 +08001638 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001639}
1640
1641static void fanout_release_data(struct packet_fanout *f)
1642{
1643 switch (f->type) {
1644 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001645 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001646 __fanout_set_data_bpf(f, NULL);
zhong jiang07d53ae2018-08-04 19:41:41 +08001647 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001648}
1649
Mike Maloney4a69a862017-04-21 10:56:11 -04001650static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
1651{
1652 struct packet_fanout *f;
1653
1654 list_for_each_entry(f, &fanout_list, list) {
1655 if (f->id == candidate_id &&
1656 read_pnet(&f->net) == sock_net(sk)) {
1657 return false;
1658 }
1659 }
1660 return true;
1661}
1662
1663static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
1664{
1665 u16 id = fanout_next_id;
1666
1667 do {
1668 if (__fanout_id_is_free(sk, id)) {
1669 *new_id = id;
1670 fanout_next_id = id + 1;
1671 return true;
1672 }
1673
1674 id++;
1675 } while (id != fanout_next_id);
1676
1677 return false;
1678}
1679
Tanner Love9c661b02020-11-06 13:07:40 -05001680static int fanout_add(struct sock *sk, struct fanout_args *args)
David S. Millerdc99f602011-07-05 01:45:05 -07001681{
Eric Dumazetd199fab2017-02-14 09:03:51 -08001682 struct packet_rollover *rollover = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001683 struct packet_sock *po = pkt_sk(sk);
Tanner Love9c661b02020-11-06 13:07:40 -05001684 u16 type_flags = args->type_flags;
David S. Millerdc99f602011-07-05 01:45:05 -07001685 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001686 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001687 u8 flags = type_flags >> 8;
Tanner Love9c661b02020-11-06 13:07:40 -05001688 u16 id = args->id;
David S. Millerdc99f602011-07-05 01:45:05 -07001689 int err;
1690
1691 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001692 case PACKET_FANOUT_ROLLOVER:
1693 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1694 return -EINVAL;
Gustavo A. R. Silva5af5a022020-11-20 12:38:20 -06001695 break;
David S. Millerdc99f602011-07-05 01:45:05 -07001696 case PACKET_FANOUT_HASH:
1697 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001698 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001699 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001700 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001701 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001702 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001703 break;
1704 default:
1705 return -EINVAL;
1706 }
1707
Eric Dumazetd199fab2017-02-14 09:03:51 -08001708 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001709
Eric Dumazetd199fab2017-02-14 09:03:51 -08001710 err = -EALREADY;
David S. Millerdc99f602011-07-05 01:45:05 -07001711 if (po->fanout)
Eric Dumazetd199fab2017-02-14 09:03:51 -08001712 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001713
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001714 if (type == PACKET_FANOUT_ROLLOVER ||
1715 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Eric Dumazetd199fab2017-02-14 09:03:51 -08001716 err = -ENOMEM;
1717 rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
1718 if (!rollover)
1719 goto out;
1720 atomic_long_set(&rollover->num, 0);
1721 atomic_long_set(&rollover->num_huge, 0);
1722 atomic_long_set(&rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001723 }
1724
Mike Maloney4a69a862017-04-21 10:56:11 -04001725 if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
1726 if (id != 0) {
1727 err = -EINVAL;
1728 goto out;
1729 }
1730 if (!fanout_find_new_id(sk, &id)) {
1731 err = -ENOMEM;
1732 goto out;
1733 }
1734 /* ephemeral flag for the first socket in the group: drop it */
1735 flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
1736 }
1737
David S. Millerdc99f602011-07-05 01:45:05 -07001738 match = NULL;
1739 list_for_each_entry(f, &fanout_list, list) {
1740 if (f->id == id &&
1741 read_pnet(&f->net) == sock_net(sk)) {
1742 match = f;
1743 break;
1744 }
1745 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001746 err = -EINVAL;
Tanner Love9c661b02020-11-06 13:07:40 -05001747 if (match) {
1748 if (match->flags != flags)
1749 goto out;
1750 if (args->max_num_members &&
1751 args->max_num_members != match->max_num_members)
1752 goto out;
1753 } else {
1754 if (args->max_num_members > PACKET_FANOUT_MAX)
1755 goto out;
1756 if (!args->max_num_members)
1757 /* legacy PACKET_FANOUT_MAX */
1758 args->max_num_members = 256;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001759 err = -ENOMEM;
Tanner Love9c661b02020-11-06 13:07:40 -05001760 match = kvzalloc(struct_size(match, arr, args->max_num_members),
1761 GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001762 if (!match)
1763 goto out;
1764 write_pnet(&match->net, sock_net(sk));
1765 match->id = id;
1766 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001767 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001768 INIT_LIST_HEAD(&match->list);
1769 spin_lock_init(&match->lock);
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001770 refcount_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001771 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001772 match->prot_hook.type = po->prot_hook.type;
1773 match->prot_hook.dev = po->prot_hook.dev;
1774 match->prot_hook.func = packet_rcv_fanout;
1775 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001776 match->prot_hook.id_match = match_fanout_group;
Tanner Love9c661b02020-11-06 13:07:40 -05001777 match->max_num_members = args->max_num_members;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001778 list_add(&match->list, &fanout_list);
1779 }
1780 err = -EINVAL;
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001781
1782 spin_lock(&po->bind_lock);
1783 if (po->running &&
1784 match->type == type &&
Eric Dumazetafe62c62011-07-07 06:41:29 -07001785 match->prot_hook.type == po->prot_hook.type &&
1786 match->prot_hook.dev == po->prot_hook.dev) {
1787 err = -ENOSPC;
Tanner Love9c661b02020-11-06 13:07:40 -05001788 if (refcount_read(&match->sk_ref) < match->max_num_members) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001789 __dev_remove_pack(&po->prot_hook);
1790 po->fanout = match;
Mike Maloney57f015f2017-11-28 10:44:29 -05001791 po->rollover = rollover;
1792 rollover = NULL;
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001793 refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001794 __fanout_link(sk, po);
1795 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001796 }
1797 }
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001798 spin_unlock(&po->bind_lock);
1799
1800 if (err && !refcount_read(&match->sk_ref)) {
1801 list_del(&match->list);
Tanner Love9c661b02020-11-06 13:07:40 -05001802 kvfree(match);
Willem de Bruijn008ba2a2017-09-14 17:14:41 -04001803 }
1804
Eric Dumazetafe62c62011-07-07 06:41:29 -07001805out:
Mike Maloney57f015f2017-11-28 10:44:29 -05001806 kfree(rollover);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001807 mutex_unlock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001808 return err;
1809}
1810
Anoob Soman2bd624b2017-02-15 20:25:39 +00001811/* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
1812 * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
1813 * It is the responsibility of the caller to call fanout_release_data() and
1814 * free the returned packet_fanout (after synchronize_net())
1815 */
1816static struct packet_fanout *fanout_release(struct sock *sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001817{
1818 struct packet_sock *po = pkt_sk(sk);
1819 struct packet_fanout *f;
1820
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001821 mutex_lock(&fanout_mutex);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001822 f = po->fanout;
1823 if (f) {
1824 po->fanout = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001825
Reshetova, Elenafb5c2c12017-06-30 13:08:10 +03001826 if (refcount_dec_and_test(&f->sk_ref))
Eric Dumazetd199fab2017-02-14 09:03:51 -08001827 list_del(&f->list);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001828 else
1829 f = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001830 }
1831 mutex_unlock(&fanout_mutex);
Anoob Soman2bd624b2017-02-15 20:25:39 +00001832
1833 return f;
David S. Millerdc99f602011-07-05 01:45:05 -07001834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001836static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
1837 struct sk_buff *skb)
1838{
1839 /* Earlier code assumed this would be a VLAN pkt, double-check
1840 * this now that we have the actual packet in hand. We can only
1841 * do this check on Ethernet devices.
1842 */
1843 if (unlikely(dev->type != ARPHRD_ETHER))
1844 return false;
1845
1846 skb_reset_mac_header(skb);
1847 return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
1848}
1849
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001850static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001852static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001854static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1855 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct sock *sk;
1858 struct sockaddr_pkt *spkt;
1859
1860 /*
1861 * When we registered the protocol we saved the socket in the data
1862 * field for just this event.
1863 */
1864
1865 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 /*
1868 * Yank back the headers [hope the device set this
1869 * right or kerboom...]
1870 *
1871 * Incoming packets have ll header pulled,
1872 * push it back.
1873 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001874 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * so that this procedure is noop.
1876 */
1877
1878 if (skb->pkt_type == PACKET_LOOPBACK)
1879 goto out;
1880
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001881 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001882 goto out;
1883
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001884 skb = skb_share_check(skb, GFP_ATOMIC);
1885 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto oom;
1887
1888 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001889 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Phil Oester84531c22005-07-12 11:57:52 -07001891 /* drop conntrack reference */
Florian Westphal895b5c92019-09-29 20:54:03 +02001892 nf_reset_ct(skb);
Phil Oester84531c22005-07-12 11:57:52 -07001893
Herbert Xuffbc6112007-02-04 23:33:10 -08001894 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001896 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /*
1899 * The SOCK_PACKET socket receives _all_ frames.
1900 */
1901
1902 spkt->spkt_family = dev->type;
1903 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1904 spkt->spkt_protocol = skb->protocol;
1905
1906 /*
1907 * Charge the memory to the socket. This is done specifically
1908 * to prevent sockets using all the memory up.
1909 */
1910
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001911 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
1913
1914out:
1915 kfree_skb(skb);
1916oom:
1917 return 0;
1918}
1919
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001920static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
1921{
Yoshiki Komachi18bed892019-03-18 14:39:52 +09001922 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
1923 sock->type == SOCK_RAW) {
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001924 skb_reset_mac_header(skb);
1925 skb->protocol = dev_parse_header_protocol(skb);
1926 }
1927
1928 skb_probe_transport_header(skb);
1929}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931/*
1932 * Output a raw packet to a device layer. This bypasses all the other
1933 * protocol layers and you must therefore supply it with a complete frame
1934 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001935
Ying Xue1b784142015-03-02 15:37:48 +08001936static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1937 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001940 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001941 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct net_device *dev;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001943 struct sockcm_cookie sockc;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001944 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001946 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001949 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 */
1951
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001952 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001954 return -EINVAL;
1955 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1956 proto = saddr->spkt_protocol;
1957 } else
1958 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001961 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
1963
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001964 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001965retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001966 rcu_read_lock();
1967 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 err = -ENODEV;
1969 if (dev == NULL)
1970 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001971
David S. Millerd5e76b02007-01-25 19:30:36 -08001972 err = -ENETDOWN;
1973 if (!(dev->flags & IFF_UP))
1974 goto out_unlock;
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001977 * You may not queue a frame bigger than the mtu. This is the lowest level
1978 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001980
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001981 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1982 if (!netif_supports_nofcs(dev)) {
1983 err = -EPROTONOSUPPORT;
1984 goto out_unlock;
1985 }
1986 extra_len = 4; /* We're doing our own CRC */
1987 }
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001990 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 goto out_unlock;
1992
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001993 if (!skb) {
1994 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001995 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001996 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001998 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001999 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00002000 if (skb == NULL)
2001 return -ENOBUFS;
2002 /* FIXME: Save some space for broken drivers that write a hard
2003 * header at transmission time by themselves. PPP is the notable
2004 * one here. This should really be fixed at the driver level.
2005 */
2006 skb_reserve(skb, reserved);
2007 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002008
Eric Dumazet1a35ca82009-12-15 05:47:03 +00002009 /* Try to align data part correctly */
2010 if (hhlen) {
2011 skb->data -= hhlen;
2012 skb->tail -= hhlen;
2013 if (len < hhlen)
2014 skb_reset_network_header(skb);
2015 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04002016 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00002017 if (err)
2018 goto out_free;
2019 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002022 if (!dev_validate_header(dev, skb->data, len)) {
2023 err = -EINVAL;
2024 goto out_unlock;
2025 }
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002026 if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
2027 !packet_extra_vlan_len_allowed(dev, skb)) {
2028 err = -EMSGSIZE;
2029 goto out_unlock;
Ben Greear57f89bf2011-02-11 09:35:18 +00002030 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00002031
Willem de Bruijn657a0662018-07-06 10:12:56 -04002032 sockcm_init(&sockc, sk);
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002033 if (msg->msg_controllen) {
2034 err = sock_cmsg_send(sk, msg, &sockc);
Soheil Hassas Yeganehf8e77182016-07-20 18:01:18 -04002035 if (unlikely(err))
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002036 goto out_unlock;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002037 }
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 skb->protocol = proto;
2040 skb->dev = dev;
2041 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002042 skb->mark = sk->sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002043 skb->tstamp = sockc.transmit_time;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002044
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002045 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002047 if (unlikely(extra_len == 4))
2048 skb->no_fcs = 1;
2049
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002050 packet_parse_headers(skb, sock);
Jason Wangc1aad272013-03-25 20:19:57 +00002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01002053 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002054 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01002057 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00002058out_free:
2059 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return err;
2061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002063static unsigned int run_filter(struct sk_buff *skb,
2064 const struct sock *sk,
2065 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct sk_filter *filter;
2068
Eric Dumazet80f8f102011-01-18 07:46:52 +00002069 rcu_read_lock();
2070 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08002071 if (filter != NULL)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002072 res = bpf_prog_run_clear_cb(filter->prog, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00002073 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
David S. Millerdbcb5852007-01-24 15:21:02 -08002075 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002078static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
2079 size_t *len)
2080{
2081 struct virtio_net_hdr vnet_hdr;
2082
2083 if (*len < sizeof(vnet_hdr))
2084 return -EINVAL;
2085 *len -= sizeof(vnet_hdr);
2086
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002087 if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002088 return -EINVAL;
2089
2090 return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
2091}
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00002094 * This function makes lazy skb cloning in hope that most of packets
2095 * are discarded by BPF.
2096 *
2097 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
2098 * and skb->cb are mangled. It works because (and until) packets
2099 * falling here are owned by current CPU. Output packets are cloned
2100 * by dev_queue_xmit_nit(), input packets are processed by net_bh
Wang Hai0e4161d2021-03-24 14:19:31 +08002101 * sequentially, so that if we return skb to original state on exit,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002102 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 */
2104
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002105static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
2106 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 struct sock *sk;
2109 struct sockaddr_ll *sll;
2110 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002111 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002113 unsigned int snaplen, res;
Weongyo Jeongda378452016-04-14 14:10:04 -07002114 bool is_drop_n_account = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 if (skb->pkt_type == PACKET_LOOPBACK)
2117 goto drop;
2118
2119 sk = pt->af_packet_priv;
2120 po = pkt_sk(sk);
2121
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002122 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002123 goto drop;
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 skb->dev = dev;
2126
Eyal Birgerd5496992020-11-21 08:28:17 +02002127 if (dev_has_header(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002129 * exported to higher levels.
2130 *
2131 * Otherwise, the device hides details of its frame
2132 * structure, so that corresponding packet head is
2133 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 */
2135 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002136 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 else if (skb->pkt_type == PACKET_OUTGOING) {
2138 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002139 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
2142
2143 snaplen = skb->len;
2144
David S. Millerdbcb5852007-01-24 15:21:02 -08002145 res = run_filter(skb, sk, snaplen);
2146 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002147 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08002148 if (snaplen > res)
2149 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002151 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto drop_n_acct;
2153
2154 if (skb_shared(skb)) {
2155 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2156 if (nskb == NULL)
2157 goto drop_n_acct;
2158
2159 if (skb_head != skb->data) {
2160 skb->data = skb_head;
2161 skb->len = skb_len;
2162 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00002163 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 skb = nskb;
2165 }
2166
Eyal Birgerb4772ef2015-03-01 14:58:29 +02002167 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08002168
2169 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002172 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002173 sll->sll_ifindex = orig_dev->ifindex;
2174 else
2175 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002177 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Eyal Birger2472d762015-03-01 14:58:28 +02002179 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2180 * Use their space for storing the original skb length.
2181 */
2182 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (pskb_trim(skb, snaplen))
2185 goto drop_n_acct;
2186
2187 skb_set_owner_r(skb, sk);
2188 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002189 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Phil Oester84531c22005-07-12 11:57:52 -07002191 /* drop conntrack reference */
Florian Westphal895b5c92019-09-29 20:54:03 +02002192 nf_reset_ct(skb);
Phil Oester84531c22005-07-12 11:57:52 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002195 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002196 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 __skb_queue_tail(&sk->sk_receive_queue, skb);
2198 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002199 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
2201
2202drop_n_acct:
Weongyo Jeongda378452016-04-14 14:10:04 -07002203 is_drop_n_account = true;
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002204 atomic_inc(&po->tp_drops);
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002205 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207drop_n_restore:
2208 if (skb_head != skb->data && skb_shared(skb)) {
2209 skb->data = skb_head;
2210 skb->len = skb_len;
2211 }
2212drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002213 if (!is_drop_n_account)
2214 consume_skb(skb);
2215 else
2216 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218}
2219
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002220static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2221 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 struct sock *sk;
2224 struct packet_sock *po;
2225 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002226 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002227 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002229 unsigned int snaplen, res;
chetan lokef6fb8f12011-08-19 10:18:16 +00002230 unsigned long status = TP_STATUS_USER;
Or Cohenacf69c92020-09-03 21:05:28 -07002231 unsigned short macoff, hdrlen;
2232 unsigned int netoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 struct sk_buff *copy_skb = NULL;
Arnd Bergmannd413fcb2017-11-27 10:09:24 +01002234 struct timespec64 ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002235 __u32 ts_status;
Weongyo Jeongda378452016-04-14 14:10:04 -07002236 bool is_drop_n_account = false;
Willem de Bruijn61fad682020-03-13 12:18:09 -04002237 unsigned int slot_id = 0;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002238 bool do_vnet = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Atzm Watanabe51846352013-12-17 22:53:32 +09002240 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2241 * We may add members to them until current aligned size without forcing
2242 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2243 */
2244 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2245 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (skb->pkt_type == PACKET_LOOPBACK)
2248 goto drop;
2249
2250 sk = pt->af_packet_priv;
2251 po = pkt_sk(sk);
2252
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002253 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002254 goto drop;
2255
Eyal Birgerd5496992020-11-21 08:28:17 +02002256 if (dev_has_header(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002258 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 else if (skb->pkt_type == PACKET_OUTGOING) {
2260 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002261 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263 }
2264
2265 snaplen = skb->len;
2266
David S. Millerdbcb5852007-01-24 15:21:02 -08002267 res = run_filter(skb, sk, snaplen);
2268 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002269 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002270
Eric Dumazet2c51c622019-06-12 09:52:31 -07002271 /* If we are flooded, just give up */
2272 if (__packet_rcv_has_room(po, skb) == ROOM_NONE) {
2273 atomic_inc(&po->tp_drops);
2274 goto drop_n_restore;
2275 }
2276
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002277 if (skb->ip_summed == CHECKSUM_PARTIAL)
2278 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002279 else if (skb->pkt_type != PACKET_OUTGOING &&
2280 (skb->ip_summed == CHECKSUM_COMPLETE ||
2281 skb_csum_unnecessary(skb)))
2282 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002283
David S. Millerdbcb5852007-01-24 15:21:02 -08002284 if (snaplen > res)
2285 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002288 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2289 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002291 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002292 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002293 (maclen < 16 ? 16 : maclen)) +
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002294 po->tp_reserve;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002295 if (po->has_vnet_hdr) {
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002296 netoff += sizeof(struct virtio_net_hdr);
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002297 do_vnet = true;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 macoff = netoff - maclen;
2300 }
Or Cohenacf69c92020-09-03 21:05:28 -07002301 if (netoff > USHRT_MAX) {
2302 atomic_inc(&po->tp_drops);
2303 goto drop_n_restore;
2304 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002305 if (po->tp_version <= TPACKET_V2) {
2306 if (macoff + snaplen > po->rx_ring.frame_size) {
2307 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002308 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002309 if (skb_shared(skb)) {
2310 copy_skb = skb_clone(skb, GFP_ATOMIC);
2311 } else {
2312 copy_skb = skb_get(skb);
2313 skb_head = skb->data;
2314 }
2315 if (copy_skb)
2316 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
chetan lokef6fb8f12011-08-19 10:18:16 +00002318 snaplen = po->rx_ring.frame_size - macoff;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002319 if ((int)snaplen < 0) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002320 snaplen = 0;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002321 do_vnet = false;
2322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002324 } else if (unlikely(macoff + snaplen >
2325 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2326 u32 nval;
2327
2328 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2329 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2330 snaplen, nval, macoff);
2331 snaplen = nval;
2332 if (unlikely((int)snaplen < 0)) {
2333 snaplen = 0;
2334 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002335 do_vnet = false;
Eric Dumazetdc808112014-08-15 09:16:04 -07002336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00002339 h.raw = packet_current_rx_frame(po, skb,
2340 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002341 if (!h.raw)
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002342 goto drop_n_account;
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002343
Willem de Bruijn61fad682020-03-13 12:18:09 -04002344 if (po->tp_version <= TPACKET_V2) {
2345 slot_id = po->rx_ring.head;
2346 if (test_bit(slot_id, po->rx_ring.rx_owner_map))
2347 goto drop_n_account;
2348 __set_bit(slot_id, po->rx_ring.rx_owner_map);
2349 }
2350
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002351 if (do_vnet &&
2352 virtio_net_hdr_from_skb(skb, h.raw + macoff -
2353 sizeof(struct virtio_net_hdr),
John Ogness88fd1cb2020-08-13 21:45:25 +02062354 vio_le(), true, 0)) {
2355 if (po->tp_version == TPACKET_V3)
2356 prb_clear_blk_fill_status(&po->rx_ring);
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002357 goto drop_n_account;
John Ogness88fd1cb2020-08-13 21:45:25 +02062358 }
Willem de Bruijn46e4c422020-03-09 11:34:35 -04002359
chetan lokef6fb8f12011-08-19 10:18:16 +00002360 if (po->tp_version <= TPACKET_V2) {
2361 packet_increment_rx_head(po, &po->rx_ring);
2362 /*
2363 * LOSING will be reported till you read the stats,
2364 * because it's COR - Clear On Read.
2365 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2366 * at packet level.
2367 */
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002368 if (atomic_read(&po->tp_drops))
chetan lokef6fb8f12011-08-19 10:18:16 +00002369 status |= TP_STATUS_LOSING;
2370 }
Eric Dumazet945d0152018-06-21 14:16:02 -07002371
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002372 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (copy_skb) {
2374 status |= TP_STATUS_COPY;
2375 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 spin_unlock(&sk->sk_receive_queue.lock);
2378
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002379 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002380
Richard Sanger171c3b12021-05-12 13:31:22 +12002381 /* Always timestamp; prefer an existing software timestamp taken
2382 * closer to the time of capture.
2383 */
2384 ts_status = tpacket_get_timestamp(skb, &ts,
2385 po->tp_tstamp | SOF_TIMESTAMPING_SOFTWARE);
2386 if (!ts_status)
Arnd Bergmannd413fcb2017-11-27 10:09:24 +01002387 ktime_get_real_ts64(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002389 status |= ts_status;
2390
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002391 switch (po->tp_version) {
2392 case TPACKET_V1:
2393 h.h1->tp_len = skb->len;
2394 h.h1->tp_snaplen = snaplen;
2395 h.h1->tp_mac = macoff;
2396 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002397 h.h1->tp_sec = ts.tv_sec;
2398 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002399 hdrlen = sizeof(*h.h1);
2400 break;
2401 case TPACKET_V2:
2402 h.h2->tp_len = skb->len;
2403 h.h2->tp_snaplen = snaplen;
2404 h.h2->tp_mac = macoff;
2405 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002406 h.h2->tp_sec = ts.tv_sec;
2407 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002408 if (skb_vlan_tag_present(skb)) {
2409 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002410 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2411 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002412 } else {
2413 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002414 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002415 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002416 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002417 hdrlen = sizeof(*h.h2);
2418 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00002419 case TPACKET_V3:
2420 /* tp_nxt_offset,vlan are already populated above.
2421 * So DONT clear those fields here
2422 */
2423 h.h3->tp_status |= status;
2424 h.h3->tp_len = skb->len;
2425 h.h3->tp_snaplen = snaplen;
2426 h.h3->tp_mac = macoff;
2427 h.h3->tp_net = netoff;
chetan lokef6fb8f12011-08-19 10:18:16 +00002428 h.h3->tp_sec = ts.tv_sec;
2429 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002430 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f12011-08-19 10:18:16 +00002431 hdrlen = sizeof(*h.h3);
2432 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002433 default:
2434 BUG();
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002437 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002438 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 sll->sll_family = AF_PACKET;
2440 sll->sll_hatype = dev->type;
2441 sll->sll_protocol = skb->protocol;
2442 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002443 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002444 sll->sll_ifindex = orig_dev->ifindex;
2445 else
2446 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Ralf Baechlee16aa202006-12-07 00:11:33 -08002448 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002449
Changli Gaof6dafa92010-12-07 04:26:16 +00002450#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002451 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002452 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002454 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2455 macoff + snaplen);
2456
2457 for (start = h.raw; start < end; start += PAGE_SIZE)
2458 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002460 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002461#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002462
Dan Collinsda413ee2014-12-19 16:49:25 +13002463 if (po->tp_version <= TPACKET_V2) {
Willem de Bruijn61fad682020-03-13 12:18:09 -04002464 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f12011-08-19 10:18:16 +00002465 __packet_set_status(po, h.raw, status);
Willem de Bruijn61fad682020-03-13 12:18:09 -04002466 __clear_bit(slot_id, po->rx_ring.rx_owner_map);
2467 spin_unlock(&sk->sk_receive_queue.lock);
Dan Collinsda413ee2014-12-19 16:49:25 +13002468 sk->sk_data_ready(sk);
John Ogness88fd1cb2020-08-13 21:45:25 +02062469 } else if (po->tp_version == TPACKET_V3) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002470 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473drop_n_restore:
2474 if (skb_head != skb->data && skb_shared(skb)) {
2475 skb->data = skb_head;
2476 skb->len = skb_len;
2477 }
2478drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002479 if (!is_drop_n_account)
2480 consume_skb(skb);
2481 else
2482 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return 0;
2484
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002485drop_n_account:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 spin_unlock(&sk->sk_receive_queue.lock);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07002487 atomic_inc(&po->tp_drops);
2488 is_drop_n_account = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
David S. Miller676d2362014-04-11 16:15:36 -04002490 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002491 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto drop_n_restore;
2493}
2494
Johann Baudy69e3c752009-05-18 22:11:22 -07002495static void tpacket_destruct_skb(struct sk_buff *skb)
2496{
2497 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002498
Johann Baudy69e3c752009-05-18 22:11:22 -07002499 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002500 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002501 __u32 ts;
2502
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002503 ph = skb_zcopy_get_nouarg(skb);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002504 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002505
2506 ts = __packet_set_timestamp(po, ph, skb);
2507 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Neil Horman89ed5b52019-06-25 17:57:49 -04002508
2509 if (!packet_read_pending(&po->tx_ring))
2510 complete(&po->skb_completion);
Johann Baudy69e3c752009-05-18 22:11:22 -07002511 }
2512
2513 sock_wfree(skb);
2514}
2515
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002516static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
2517{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002518 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2519 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2520 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
2521 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
2522 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
2523 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2524 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
2525
2526 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
2527 return -EINVAL;
2528
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002529 return 0;
2530}
2531
2532static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
2533 struct virtio_net_hdr *vnet_hdr)
2534{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002535 if (*len < sizeof(*vnet_hdr))
2536 return -EINVAL;
2537 *len -= sizeof(*vnet_hdr);
2538
Al Virocbbd26b2016-11-01 22:09:04 -04002539 if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002540 return -EFAULT;
2541
2542 return __packet_snd_vnet_parse(vnet_hdr, *len);
2543}
2544
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002545static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002546 void *frame, struct net_device *dev, void *data, int tp_len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002547 __be16 proto, unsigned char *addr, int hlen, int copylen,
2548 const struct sockcm_cookie *sockc)
Johann Baudy69e3c752009-05-18 22:11:22 -07002549{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002550 union tpacket_uhdr ph;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002551 int to_write, offset, len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002552 struct socket *sock = po->sk.sk_socket;
2553 struct page *page;
Johann Baudy69e3c752009-05-18 22:11:22 -07002554 int err;
2555
2556 ph.raw = frame;
2557
2558 skb->protocol = proto;
2559 skb->dev = dev;
2560 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002561 skb->mark = po->sk.sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002562 skb->tstamp = sockc->transmit_time;
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002563 skb_setup_tx_timestamp(skb, sockc->tsflags);
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002564 skb_zcopy_set_nouarg(skb, ph.raw);
Johann Baudy69e3c752009-05-18 22:11:22 -07002565
Herbert Xuae641942011-11-18 02:20:04 +00002566 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002567 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002568
Johann Baudy69e3c752009-05-18 22:11:22 -07002569 to_write = tp_len;
2570
2571 if (sock->type == SOCK_DGRAM) {
2572 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2573 NULL, tp_len);
2574 if (unlikely(err < 0))
2575 return -EINVAL;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002576 } else if (copylen) {
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002577 int hdrlen = min_t(int, copylen, tp_len);
2578
Johann Baudy69e3c752009-05-18 22:11:22 -07002579 skb_push(skb, dev->hard_header_len);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002580 skb_put(skb, copylen - dev->hard_header_len);
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002581 err = skb_store_bits(skb, 0, data, hdrlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002582 if (unlikely(err))
2583 return err;
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002584 if (!dev_validate_header(dev, skb->data, hdrlen))
2585 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002586
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002587 data += hdrlen;
2588 to_write -= hdrlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002589 }
2590
Johann Baudy69e3c752009-05-18 22:11:22 -07002591 offset = offset_in_page(data);
2592 len_max = PAGE_SIZE - offset;
2593 len = ((to_write > len_max) ? len_max : to_write);
2594
2595 skb->data_len = to_write;
2596 skb->len += to_write;
2597 skb->truesize += to_write;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002598 refcount_add(to_write, &po->sk.sk_wmem_alloc);
Johann Baudy69e3c752009-05-18 22:11:22 -07002599
2600 while (likely(to_write)) {
2601 nr_frags = skb_shinfo(skb)->nr_frags;
2602
2603 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002604 pr_err("Packet exceed the number of skb frags(%lu)\n",
2605 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002606 return -EFAULT;
2607 }
2608
Changli Gao0af55bb2010-12-01 02:52:20 +00002609 page = pgv_to_page(data);
2610 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002611 flush_dcache_page(page);
2612 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002613 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002614 to_write -= len;
2615 offset = 0;
2616 len_max = PAGE_SIZE;
2617 len = ((to_write > len_max) ? len_max : to_write);
2618 }
2619
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002620 packet_parse_headers(skb, sock);
Daniel Borkmannefdfa2f2015-11-11 23:25:40 +01002621
Johann Baudy69e3c752009-05-18 22:11:22 -07002622 return tp_len;
2623}
2624
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002625static int tpacket_parse_header(struct packet_sock *po, void *frame,
2626 int size_max, void **data)
2627{
2628 union tpacket_uhdr ph;
2629 int tp_len, off;
2630
2631 ph.raw = frame;
2632
2633 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002634 case TPACKET_V3:
2635 if (ph.h3->tp_next_offset != 0) {
2636 pr_warn_once("variable sized slot not supported");
2637 return -EINVAL;
2638 }
2639 tp_len = ph.h3->tp_len;
2640 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002641 case TPACKET_V2:
2642 tp_len = ph.h2->tp_len;
2643 break;
2644 default:
2645 tp_len = ph.h1->tp_len;
2646 break;
2647 }
2648 if (unlikely(tp_len > size_max)) {
2649 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2650 return -EMSGSIZE;
2651 }
2652
2653 if (unlikely(po->tp_tx_has_off)) {
2654 int off_min, off_max;
2655
2656 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2657 off_max = po->tx_ring.frame_size - tp_len;
2658 if (po->sk.sk_type == SOCK_DGRAM) {
2659 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002660 case TPACKET_V3:
2661 off = ph.h3->tp_net;
2662 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002663 case TPACKET_V2:
2664 off = ph.h2->tp_net;
2665 break;
2666 default:
2667 off = ph.h1->tp_net;
2668 break;
2669 }
2670 } else {
2671 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002672 case TPACKET_V3:
2673 off = ph.h3->tp_mac;
2674 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002675 case TPACKET_V2:
2676 off = ph.h2->tp_mac;
2677 break;
2678 default:
2679 off = ph.h1->tp_mac;
2680 break;
2681 }
2682 }
2683 if (unlikely((off < off_min) || (off_max < off)))
2684 return -EINVAL;
2685 } else {
2686 off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2687 }
2688
2689 *data = frame + off;
2690 return tp_len;
2691}
2692
Johann Baudy69e3c752009-05-18 22:11:22 -07002693static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2694{
Neil Horman89ed5b52019-06-25 17:57:49 -04002695 struct sk_buff *skb = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002696 struct net_device *dev;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002697 struct virtio_net_hdr *vnet_hdr = NULL;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002698 struct sockcm_cookie sockc;
Johann Baudy69e3c752009-05-18 22:11:22 -07002699 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002700 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002701 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002702 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002703 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002704 unsigned char *addr = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002705 int tp_len, size_max;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002706 void *data;
Johann Baudy69e3c752009-05-18 22:11:22 -07002707 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002708 int status = TP_STATUS_AVAILABLE;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002709 int hlen, tlen, copylen = 0;
Neil Horman89ed5b52019-06-25 17:57:49 -04002710 long timeo = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002711
Johann Baudy69e3c752009-05-18 22:11:22 -07002712 mutex_lock(&po->pg_vec_lock);
2713
Eric Dumazet32d31822019-08-14 02:11:57 -07002714 /* packet_sendmsg() check on tx_ring.pg_vec was lockless,
2715 * we need to confirm it under protection of pg_vec_lock.
2716 */
2717 if (unlikely(!po->tx_ring.pg_vec)) {
2718 err = -EBUSY;
2719 goto out;
2720 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002721 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002722 dev = packet_cached_dev_get(po);
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07002723 proto = READ_ONCE(po->num);
Johann Baudy69e3c752009-05-18 22:11:22 -07002724 } else {
2725 err = -EINVAL;
2726 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2727 goto out;
2728 if (msg->msg_namelen < (saddr->sll_halen
2729 + offsetof(struct sockaddr_ll,
2730 sll_addr)))
2731 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002732 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002733 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002734 if (po->sk.sk_socket->type == SOCK_DGRAM) {
2735 if (dev && msg->msg_namelen < dev->addr_len +
2736 offsetof(struct sockaddr_ll, sll_addr))
2737 goto out_put;
2738 addr = saddr->sll_addr;
2739 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002740 }
2741
Johann Baudy69e3c752009-05-18 22:11:22 -07002742 err = -ENXIO;
2743 if (unlikely(dev == NULL))
2744 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002745 err = -ENETDOWN;
2746 if (unlikely(!(dev->flags & IFF_UP)))
2747 goto out_put;
2748
Willem de Bruijn657a0662018-07-06 10:12:56 -04002749 sockcm_init(&sockc, &po->sk);
Douglas Caetano dos Santosd19b1832017-05-12 15:19:15 -03002750 if (msg->msg_controllen) {
2751 err = sock_cmsg_send(&po->sk, msg, &sockc);
2752 if (unlikely(err))
2753 goto out_put;
2754 }
2755
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002756 if (po->sk.sk_socket->type == SOCK_RAW)
2757 reserve = dev->hard_header_len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002758 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002759 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002760
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002761 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002762 size_max = dev->mtu + reserve + VLAN_HLEN;
David S. Miller09effa62013-08-07 17:11:00 -07002763
Neil Horman89ed5b52019-06-25 17:57:49 -04002764 reinit_completion(&po->skb_completion);
2765
Johann Baudy69e3c752009-05-18 22:11:22 -07002766 do {
2767 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002768 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002769 if (unlikely(ph == NULL)) {
Neil Horman89ed5b52019-06-25 17:57:49 -04002770 if (need_wait && skb) {
2771 timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
2772 timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
2773 if (timeo <= 0) {
2774 err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
2775 goto out_put;
2776 }
2777 }
2778 /* check for additional frames */
Johann Baudy69e3c752009-05-18 22:11:22 -07002779 continue;
2780 }
2781
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002782 skb = NULL;
2783 tp_len = tpacket_parse_header(po, ph, size_max, &data);
2784 if (tp_len < 0)
2785 goto tpacket_error;
2786
Johann Baudy69e3c752009-05-18 22:11:22 -07002787 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002788 hlen = LL_RESERVED_SPACE(dev);
2789 tlen = dev->needed_tailroom;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002790 if (po->has_vnet_hdr) {
2791 vnet_hdr = data;
2792 data += sizeof(*vnet_hdr);
2793 tp_len -= sizeof(*vnet_hdr);
2794 if (tp_len < 0 ||
2795 __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
2796 tp_len = -EINVAL;
2797 goto tpacket_error;
2798 }
2799 copylen = __virtio16_to_cpu(vio_le(),
2800 vnet_hdr->hdr_len);
2801 }
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002802 copylen = max_t(int, copylen, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002803 skb = sock_alloc_send_skb(&po->sk,
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002804 hlen + tlen + sizeof(struct sockaddr_ll) +
2805 (copylen - dev->hard_header_len),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002806 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002807
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002808 if (unlikely(skb == NULL)) {
2809 /* we assume the socket was initially writeable ... */
2810 if (likely(len_sum > 0))
2811 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002812 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002813 }
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002814 tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002815 addr, hlen, copylen, &sockc);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002816 if (likely(tp_len >= 0) &&
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002817 tp_len > dev->mtu + reserve &&
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002818 !po->has_vnet_hdr &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002819 !packet_extra_vlan_len_allowed(dev, skb))
2820 tp_len = -EMSGSIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002821
2822 if (unlikely(tp_len < 0)) {
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002823tpacket_error:
Johann Baudy69e3c752009-05-18 22:11:22 -07002824 if (po->tp_loss) {
2825 __packet_set_status(po, ph,
2826 TP_STATUS_AVAILABLE);
2827 packet_increment_head(&po->tx_ring);
2828 kfree_skb(skb);
2829 continue;
2830 } else {
2831 status = TP_STATUS_WRONG_FORMAT;
2832 err = tp_len;
2833 goto out_status;
2834 }
2835 }
2836
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002837 if (po->has_vnet_hdr) {
2838 if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
2839 tp_len = -EINVAL;
2840 goto tpacket_error;
2841 }
2842 virtio_net_hdr_set_proto(skb, vnet_hdr);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002843 }
2844
Johann Baudy69e3c752009-05-18 22:11:22 -07002845 skb->destructor = tpacket_destruct_skb;
2846 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002847 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002848
2849 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002850 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002851 if (unlikely(err > 0)) {
2852 err = net_xmit_errno(err);
2853 if (err && __packet_get_status(po, ph) ==
2854 TP_STATUS_AVAILABLE) {
2855 /* skb was destructed already */
2856 skb = NULL;
2857 goto out_status;
2858 }
2859 /*
2860 * skb was dropped but not destructed yet;
2861 * let's treat it like congestion or err < 0
2862 */
2863 err = 0;
2864 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002865 packet_increment_head(&po->tx_ring);
2866 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002867 } while (likely((ph != NULL) ||
2868 /* Note: packet_read_pending() might be slow if we have
2869 * to call it as it's per_cpu variable, but in fast-path
2870 * we already short-circuit the loop with the first
2871 * condition, and luckily don't have to go that path
2872 * anyway.
2873 */
2874 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002875
2876 err = len_sum;
2877 goto out_put;
2878
Johann Baudy69e3c752009-05-18 22:11:22 -07002879out_status:
2880 __packet_set_status(po, ph, status);
2881 kfree_skb(skb);
2882out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002883 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002884out:
2885 mutex_unlock(&po->pg_vec_lock);
2886 return err;
2887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002889static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2890 size_t reserve, size_t len,
2891 size_t linear, int noblock,
2892 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002893{
2894 struct sk_buff *skb;
2895
2896 /* Under a page? Don't bother with paged skb. */
2897 if (prepad + len < PAGE_SIZE || !linear)
2898 linear = len;
2899
2900 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002901 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002902 if (!skb)
2903 return NULL;
2904
2905 skb_reserve(skb, reserve);
2906 skb_put(skb, linear);
2907 skb->data_len = len - linear;
2908 skb->len += len - linear;
2909
2910 return skb;
2911}
2912
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002913static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
2915 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002916 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 struct sk_buff *skb;
2918 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002919 __be16 proto;
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002920 unsigned char *addr = NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002921 int err, reserve = 0;
Edward Jeec7d39e32015-10-08 14:56:49 -07002922 struct sockcm_cookie sockc;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002923 struct virtio_net_hdr vnet_hdr = { 0 };
2924 int offset = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002925 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002926 bool has_vnet_hdr = false;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002927 int hlen, tlen, linear;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002928 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002931 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002933
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002934 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002935 dev = packet_cached_dev_get(po);
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07002936 proto = READ_ONCE(po->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 } else {
2938 err = -EINVAL;
2939 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2940 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002941 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2942 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002944 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Willem de Bruijn486efdc2019-04-29 11:53:18 -04002945 if (sock->type == SOCK_DGRAM) {
2946 if (dev && msg->msg_namelen < dev->addr_len +
2947 offsetof(struct sockaddr_ll, sll_addr))
2948 goto out_unlock;
2949 addr = saddr->sll_addr;
2950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002954 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002956 err = -ENETDOWN;
2957 if (unlikely(!(dev->flags & IFF_UP)))
2958 goto out_unlock;
2959
Willem de Bruijn657a0662018-07-06 10:12:56 -04002960 sockcm_init(&sockc, sk);
Edward Jeec7d39e32015-10-08 14:56:49 -07002961 sockc.mark = sk->sk_mark;
2962 if (msg->msg_controllen) {
2963 err = sock_cmsg_send(sk, msg, &sockc);
2964 if (unlikely(err))
2965 goto out_unlock;
2966 }
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (sock->type == SOCK_RAW)
2969 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002970 if (po->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002971 err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
2972 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002973 goto out_unlock;
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002974 has_vnet_hdr = true;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002975 }
2976
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002977 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2978 if (!netif_supports_nofcs(dev)) {
2979 err = -EPROTONOSUPPORT;
2980 goto out_unlock;
2981 }
2982 extra_len = 4; /* We're doing our own CRC */
2983 }
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 err = -EMSGSIZE;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002986 if (!vnet_hdr.gso_type &&
2987 (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 goto out_unlock;
2989
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002990 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002991 hlen = LL_RESERVED_SPACE(dev);
2992 tlen = dev->needed_tailroom;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002993 linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
2994 linear = max(linear, min_t(int, len, dev->hard_header_len));
2995 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002996 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002997 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 goto out_unlock;
2999
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04003000 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07003002 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05003003 if (sock->type == SOCK_DGRAM) {
3004 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05003005 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05003006 goto out_free;
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04003007 } else if (reserve) {
Willem de Bruijn9aad13b2018-05-24 18:10:30 -04003008 skb_reserve(skb, -reserve);
Nicolas Dichtel88a81212019-01-17 11:27:22 +01003009 if (len < reserve + sizeof(struct ipv6hdr) &&
3010 dev->min_header_len != dev->hard_header_len)
Willem de Bruijn993675a2018-07-11 12:00:45 -04003011 skb_reset_network_header(skb);
Willem de Bruijn9c707762014-11-19 13:10:16 -05003012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05003015 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (err)
3017 goto out_free;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00003018
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05003019 if (sock->type == SOCK_RAW &&
3020 !dev_validate_header(dev, skb->data, len)) {
3021 err = -EINVAL;
3022 goto out_free;
3023 }
3024
Willem de Bruijn8f932f72018-12-17 12:24:00 -05003025 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003027 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01003028 !packet_extra_vlan_len_allowed(dev, skb)) {
3029 err = -EMSGSIZE;
3030 goto out_free;
Ben Greear57f89bf2011-02-11 09:35:18 +00003031 }
3032
David S. Miller09effa62013-08-07 17:11:00 -07003033 skb->protocol = proto;
3034 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 skb->priority = sk->sk_priority;
Edward Jeec7d39e32015-10-08 14:56:49 -07003036 skb->mark = sockc.mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07003037 skb->tstamp = sockc.transmit_time;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01003038
Willem de Bruijnda7c9562017-09-26 12:20:17 -04003039 if (has_vnet_hdr) {
Jarno Rajahalmedb60eb5f2016-11-18 15:40:41 -08003040 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003041 if (err)
3042 goto out_free;
3043 len += sizeof(vnet_hdr);
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00003044 virtio_net_hdr_set_proto(skb, &vnet_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003045 }
3046
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00003047 packet_parse_headers(skb, sock);
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01003048
Ben Greear3bdc0eb2012-02-11 15:39:30 +00003049 if (unlikely(extra_len == 4))
3050 skb->no_fcs = 1;
3051
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003052 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (err > 0 && (err = net_xmit_errno(err)) != 0)
3054 goto out_unlock;
3055
Daniel Borkmanne40526c2013-11-21 16:50:58 +01003056 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003058 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060out_free:
3061 kfree_skb(skb);
3062out_unlock:
Yajun Deng1160dfa2021-08-05 19:55:27 +08003063 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064out:
3065 return err;
3066}
3067
Ying Xue1b784142015-03-02 15:37:48 +08003068static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07003069{
Johann Baudy69e3c752009-05-18 22:11:22 -07003070 struct sock *sk = sock->sk;
3071 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003072
Eric Dumazetd1b5bee2021-06-10 09:00:12 -07003073 /* Reading tx_ring.pg_vec without holding pg_vec_lock is racy.
3074 * tpacket_snd() will redo the check safely.
3075 */
3076 if (data_race(po->tx_ring.pg_vec))
Johann Baudy69e3c752009-05-18 22:11:22 -07003077 return tpacket_snd(po, msg);
Eric Dumazetd1b5bee2021-06-10 09:00:12 -07003078
3079 return packet_snd(sock, msg, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07003080}
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082/*
3083 * Close a PACKET socket. This is fairly simple. We immediately go
3084 * to 'closed' state and remove our protocol entry in the device list.
3085 */
3086
3087static int packet_release(struct socket *sock)
3088{
3089 struct sock *sk = sock->sk;
3090 struct packet_sock *po;
Anoob Soman2bd624b2017-02-15 20:25:39 +00003091 struct packet_fanout *f;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003092 struct net *net;
chetan lokef6fb8f12011-08-19 10:18:16 +00003093 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 if (!sk)
3096 return 0;
3097
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003098 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 po = pkt_sk(sk);
3100
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003101 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00003102 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003103 mutex_unlock(&net->packet.sklist_lock);
3104
Eric Dumazet920de802008-11-24 00:09:29 -08003105 sock_prot_inuse_add(net, sk->sk_prot, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
stephen hemminger808f5112010-02-22 07:57:18 +00003107 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003108 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003109 packet_cached_dev_reset(po);
3110
Ben Greear160ff182011-06-01 07:18:52 +00003111 if (po->prot_hook.dev) {
Eric Dumazetf1d92682021-12-14 07:09:33 -08003112 dev_put_track(po->prot_hook.dev, &po->prot_hook.dev_tracker);
Ben Greear160ff182011-06-01 07:18:52 +00003113 po->prot_hook.dev = NULL;
3114 }
stephen hemminger808f5112010-02-22 07:57:18 +00003115 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Eric Dumazet5171b372018-04-15 17:52:04 -07003119 lock_sock(sk);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003120 if (po->rx_ring.pg_vec) {
3121 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003122 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003123 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003124
Phil Sutter9665d5d2013-02-01 07:21:41 +00003125 if (po->tx_ring.pg_vec) {
3126 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003127 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003128 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003129 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Anoob Soman2bd624b2017-02-15 20:25:39 +00003131 f = fanout_release(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07003132
stephen hemminger808f5112010-02-22 07:57:18 +00003133 synchronize_net();
Anoob Soman2bd624b2017-02-15 20:25:39 +00003134
Willem de Bruijnafa09252019-05-31 12:37:23 -04003135 kfree(po->rollover);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003136 if (f) {
3137 fanout_release_data(f);
Tanner Love9c661b02020-11-06 13:07:40 -05003138 kvfree(f);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /*
3141 * Now the socket is dead. No more input will appear.
3142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 sock_orphan(sk);
3144 sock->sk = NULL;
3145
3146 /* Purge queues */
3147
3148 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01003149 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003150 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 sock_put(sk);
3153 return 0;
3154}
3155
3156/*
3157 * Attach a packet hook.
3158 */
3159
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003160static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
3161 __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003164 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003165 __be16 proto_curr;
3166 bool need_rehook;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003167 struct net_device *dev = NULL;
3168 int ret = 0;
3169 bool unlisted = false;
David S. Millerdc99f602011-07-05 01:45:05 -07003170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 spin_lock(&po->bind_lock);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003173 rcu_read_lock();
3174
Willem de Bruijn49716132017-09-26 12:19:37 -04003175 if (po->fanout) {
3176 ret = -EINVAL;
3177 goto out_unlock;
3178 }
3179
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003180 if (name) {
3181 dev = dev_get_by_name_rcu(sock_net(sk), name);
3182 if (!dev) {
3183 ret = -ENODEV;
3184 goto out_unlock;
3185 }
3186 } else if (ifindex) {
3187 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
3188 if (!dev) {
3189 ret = -ENODEV;
3190 goto out_unlock;
3191 }
3192 }
3193
Yajun Deng1160dfa2021-08-05 19:55:27 +08003194 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003195
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003196 proto_curr = po->prot_hook.type;
3197 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003199 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003201 if (need_rehook) {
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003202 if (po->running) {
3203 rcu_read_unlock();
Eric Dumazet15fe0762017-11-28 08:03:30 -08003204 /* prevents packet_notifier() from calling
3205 * register_prot_hook()
3206 */
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07003207 WRITE_ONCE(po->num, 0);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003208 __unregister_prot_hook(sk, true);
3209 rcu_read_lock();
3210 dev_curr = po->prot_hook.dev;
3211 if (dev)
3212 unlisted = !dev_get_by_index_rcu(sock_net(sk),
3213 dev->ifindex);
3214 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003215
Eric Dumazet15fe0762017-11-28 08:03:30 -08003216 BUG_ON(po->running);
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07003217 WRITE_ONCE(po->num, proto);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003218 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003219
Eric Dumazetf1d92682021-12-14 07:09:33 -08003220 dev_put_track(dev_curr, &po->prot_hook.dev_tracker);
3221 dev_curr = NULL;
3222
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003223 if (unlikely(unlisted)) {
3224 dev_put(dev);
3225 po->prot_hook.dev = NULL;
Eric Dumazete032f7c2021-06-16 06:42:02 -07003226 WRITE_ONCE(po->ifindex, -1);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003227 packet_cached_dev_reset(po);
3228 } else {
Eric Dumazetf1d92682021-12-14 07:09:33 -08003229 if (dev)
3230 netdev_tracker_alloc(dev,
3231 &po->prot_hook.dev_tracker,
3232 GFP_ATOMIC);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003233 po->prot_hook.dev = dev;
Eric Dumazete032f7c2021-06-16 06:42:02 -07003234 WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003235 packet_cached_dev_assign(po, dev);
3236 }
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003237 }
Eric Dumazetf1d92682021-12-14 07:09:33 -08003238 dev_put_track(dev_curr, &po->prot_hook.dev_tracker);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003239
3240 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto out_unlock;
3242
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003243 if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
David S. Millerce06b032011-07-04 01:44:29 -07003244 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08003245 } else {
3246 sk->sk_err = ENETDOWN;
3247 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04003248 sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
3250
3251out_unlock:
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003252 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 spin_unlock(&po->bind_lock);
3254 release_sock(sk);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258/*
3259 * Bind a packet socket to a device
3260 */
3261
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003262static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
3263 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003265 struct sock *sk = sock->sk;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003266 char name[sizeof(uaddr->sa_data) + 1];
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 /*
3269 * Check legality
3270 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003271
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003272 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return -EINVAL;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003274 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
3275 * zero-terminated.
3276 */
3277 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
3278 name[sizeof(uaddr->sa_data)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003280 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3284{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003285 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
3286 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /*
3289 * Check legality
3290 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (addr_len < sizeof(struct sockaddr_ll))
3293 return -EINVAL;
3294 if (sll->sll_family != AF_PACKET)
3295 return -EINVAL;
3296
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003297 return packet_do_bind(sk, NULL, sll->sll_ifindex,
3298 sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
3301static struct proto packet_proto = {
3302 .name = "PACKET",
3303 .owner = THIS_MODULE,
3304 .obj_size = sizeof(struct packet_sock),
3305};
3306
3307/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003308 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 */
3310
Eric Paris3f378b62009-11-05 22:18:14 -08003311static int packet_create(struct net *net, struct socket *sock, int protocol,
3312 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
3314 struct sock *sk;
3315 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003316 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 int err;
3318
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003319 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003321 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3322 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return -ESOCKTNOSUPPORT;
3324
3325 sock->state = SS_UNCONNECTED;
3326
3327 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003328 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (sk == NULL)
3330 goto out;
3331
3332 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 if (sock->type == SOCK_PACKET)
3334 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 sock_init_data(sock, sk);
3337
3338 po = pkt_sk(sk);
Neil Horman89ed5b52019-06-25 17:57:49 -04003339 init_completion(&po->skb_completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003341 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003342 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003343
Daniel Borkmannb0138402014-01-15 16:25:36 +01003344 err = packet_alloc_pending(po);
3345 if (err)
3346 goto out2;
3347
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003348 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003351 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
3353 /*
3354 * Attach a protocol block
3355 */
3356
3357 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003358 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003359 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (sock->type == SOCK_PACKET)
3363 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 po->prot_hook.af_packet_priv = sk;
3366
Al Viro0e11c912006-11-08 00:26:29 -08003367 if (proto) {
3368 po->prot_hook.type = proto;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003369 __register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003372 mutex_lock(&net->packet.sklist_lock);
Maxime Chevalliera4dc6a42019-03-16 14:41:30 +01003373 sk_add_node_tail_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003374 mutex_unlock(&net->packet.sklist_lock);
3375
Eric Dumazet36804532008-11-19 14:25:35 -08003376 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00003377
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003378 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003379out2:
3380 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381out:
3382 return err;
3383}
3384
3385/*
3386 * Pull a packet from our receive queue and hand it to the user.
3387 * If necessary we block.
3388 */
3389
Ying Xue1b784142015-03-02 15:37:48 +08003390static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3391 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 struct sock *sk = sock->sk;
3394 struct sk_buff *skb;
3395 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003396 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003397 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003400 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 goto out;
3402
3403#if 0
3404 /* What error should we return now? EUNATTACH? */
3405 if (pkt_sk(sk)->ifindex < 0)
3406 return -ENODEV;
3407#endif
3408
Richard Cochraned85b562010-04-07 22:41:28 +00003409 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003410 err = sock_recv_errqueue(sk, msg, len,
3411 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003412 goto out;
3413 }
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 * Call the generic datagram receiver. This handles all sorts
3417 * of horrible races and re-entrancy so we can forget about it
3418 * in the protocol layers.
3419 *
3420 * Now it will return ENETDOWN, if device have just gone down,
3421 * but then it will block.
3422 */
3423
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003424 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003427 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 * handles the blocking we don't see and worry about blocking
3429 * retries.
3430 */
3431
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003432 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 goto out;
3434
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07003435 packet_rcv_try_clear_pressure(pkt_sk(sk));
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003436
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003437 if (pkt_sk(sk)->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003438 err = packet_rcv_vnet(msg, skb, &len);
3439 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003440 goto out_free;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003441 vnet_hdr_len = sizeof(struct virtio_net_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003442 }
3443
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003444 /* You lose any data beyond the buffer you gave. If it worries
3445 * a user program they can ask the device for its MTU
3446 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003449 if (copied > len) {
3450 copied = len;
3451 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
3453
David S. Miller51f3d022014-11-05 16:46:40 -05003454 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (err)
3456 goto out_free;
3457
Eyal Birger2472d762015-03-01 14:58:28 +02003458 if (sock->type != SOCK_PACKET) {
3459 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3460
3461 /* Original length was stored in sockaddr_ll fields */
3462 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3463 sll->sll_family = AF_PACKET;
3464 sll->sll_protocol = skb->protocol;
3465 }
3466
Neil Horman3b885782009-10-12 13:26:31 -07003467 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003469 if (msg->msg_name) {
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003470 int copy_len;
3471
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003472 /* If the address length field is there to be filled
3473 * in, we fill it in now.
3474 */
3475 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003476 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003477 msg->msg_namelen = sizeof(struct sockaddr_pkt);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003478 copy_len = msg->msg_namelen;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003479 } else {
3480 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003481
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003482 msg->msg_namelen = sll->sll_halen +
3483 offsetof(struct sockaddr_ll, sll_addr);
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003484 copy_len = msg->msg_namelen;
3485 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
3486 memset(msg->msg_name +
3487 offsetof(struct sockaddr_ll, sll_addr),
3488 0, sizeof(sll->sll_addr));
3489 msg->msg_namelen = sizeof(struct sockaddr_ll);
3490 }
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003491 }
Willem de Bruijnb2cf86e2019-04-29 11:46:55 -04003492 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Herbert Xu8dc41942007-02-04 23:31:32 -08003495 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003496 struct tpacket_auxdata aux;
3497
3498 aux.tp_status = TP_STATUS_USER;
3499 if (skb->ip_summed == CHECKSUM_PARTIAL)
3500 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003501 else if (skb->pkt_type != PACKET_OUTGOING &&
3502 (skb->ip_summed == CHECKSUM_COMPLETE ||
3503 skb_csum_unnecessary(skb)))
3504 aux.tp_status |= TP_STATUS_CSUM_VALID;
3505
Eyal Birger2472d762015-03-01 14:58:28 +02003506 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003507 aux.tp_snaplen = skb->len;
3508 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003509 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003510 if (skb_vlan_tag_present(skb)) {
3511 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003512 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3513 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003514 } else {
3515 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003516 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003517 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003518 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003519 }
3520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /*
3522 * Free or return the buffer as appropriate. Again this
3523 * hides all the races and re-entrancy issues from us.
3524 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003525 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527out_free:
3528 skb_free_datagram(sk, skb);
3529out:
3530 return err;
3531}
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003534 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 struct net_device *dev;
3537 struct sock *sk = sock->sk;
3538
3539 if (peer)
3540 return -EOPNOTSUPP;
3541
3542 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003543 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003544 rcu_read_lock();
Eric Dumazete032f7c2021-06-16 06:42:02 -07003545 dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003546 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003547 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003548 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003550 return sizeof(*uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003554 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
3556 struct net_device *dev;
3557 struct sock *sk = sock->sk;
3558 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003559 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Eric Dumazete032f7c2021-06-16 06:42:02 -07003560 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 if (peer)
3563 return -EOPNOTSUPP;
3564
Eric Dumazete032f7c2021-06-16 06:42:02 -07003565 ifindex = READ_ONCE(po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 sll->sll_family = AF_PACKET;
Eric Dumazete032f7c2021-06-16 06:42:02 -07003567 sll->sll_ifindex = ifindex;
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07003568 sll->sll_protocol = READ_ONCE(po->num);
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003569 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003570 rcu_read_lock();
Eric Dumazete032f7c2021-06-16 06:42:02 -07003571 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 if (dev) {
3573 sll->sll_hatype = dev->type;
3574 sll->sll_halen = dev->addr_len;
3575 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 } else {
3577 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3578 sll->sll_halen = 0;
3579 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003580 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003582 return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Wang Chen2aeb0b82008-07-14 20:49:46 -07003585static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3586 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 switch (i->type) {
3589 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003590 if (i->alen != dev->addr_len)
3591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003593 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003595 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 break;
3597 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003598 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003600 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003601 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003602 if (i->alen != dev->addr_len)
3603 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003604 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003605 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003606 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003607 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003608 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003609 default:
3610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003615static void packet_dev_mclist_delete(struct net_device *dev,
3616 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003618 struct packet_mclist *ml;
3619
3620 while ((ml = *mlp) != NULL) {
3621 if (ml->ifindex == dev->ifindex) {
3622 packet_dev_mc(dev, ml, -1);
3623 *mlp = ml->next;
3624 kfree(ml);
3625 } else
3626 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
3628}
3629
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003630static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 struct packet_sock *po = pkt_sk(sk);
3633 struct packet_mclist *ml, *i;
3634 struct net_device *dev;
3635 int err;
3636
3637 rtnl_lock();
3638
3639 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003640 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if (!dev)
3642 goto done;
3643
3644 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003645 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 goto done;
3647
3648 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003649 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if (i == NULL)
3651 goto done;
3652
3653 err = 0;
3654 for (ml = po->mclist; ml; ml = ml->next) {
3655 if (ml->ifindex == mreq->mr_ifindex &&
3656 ml->type == mreq->mr_type &&
3657 ml->alen == mreq->mr_alen &&
3658 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3659 ml->count++;
3660 /* Free the new element ... */
3661 kfree(i);
3662 goto done;
3663 }
3664 }
3665
3666 i->type = mreq->mr_type;
3667 i->ifindex = mreq->mr_ifindex;
3668 i->alen = mreq->mr_alen;
3669 memcpy(i->addr, mreq->mr_address, i->alen);
Mathias Krause309cf372016-04-10 12:52:28 +02003670 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 i->count = 1;
3672 i->next = po->mclist;
3673 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003674 err = packet_dev_mc(dev, i, 1);
3675 if (err) {
3676 po->mclist = i->next;
3677 kfree(i);
3678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
3680done:
3681 rtnl_unlock();
3682 return err;
3683}
3684
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003685static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 struct packet_mclist *ml, **mlp;
3688
3689 rtnl_lock();
3690
3691 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3692 if (ml->ifindex == mreq->mr_ifindex &&
3693 ml->type == mreq->mr_type &&
3694 ml->alen == mreq->mr_alen &&
3695 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3696 if (--ml->count == 0) {
3697 struct net_device *dev;
3698 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003699 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3700 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 kfree(ml);
3703 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
3706 }
3707 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
3710
3711static void packet_flush_mclist(struct sock *sk)
3712{
3713 struct packet_sock *po = pkt_sk(sk);
3714 struct packet_mclist *ml;
3715
3716 if (!po->mclist)
3717 return;
3718
3719 rtnl_lock();
3720 while ((ml = po->mclist) != NULL) {
3721 struct net_device *dev;
3722
3723 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003724 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3725 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 kfree(ml);
3728 }
3729 rtnl_unlock();
3730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
3732static int
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003733packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
3734 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
3736 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003737 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 int ret;
3739
3740 if (level != SOL_PACKET)
3741 return -ENOPROTOOPT;
3742
Johann Baudy69e3c752009-05-18 22:11:22 -07003743 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003744 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 case PACKET_DROP_MEMBERSHIP:
3746 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003747 struct packet_mreq_max mreq;
3748 int len = optlen;
3749 memset(&mreq, 0, sizeof(mreq));
3750 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003752 if (len > sizeof(mreq))
3753 len = sizeof(mreq);
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003754 if (copy_from_sockptr(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003756 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 if (optname == PACKET_ADD_MEMBERSHIP)
3759 ret = packet_mc_add(sk, &mreq);
3760 else
3761 ret = packet_mc_drop(sk, &mreq);
3762 return ret;
3763 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003766 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 {
chetan lokef6fb8f12011-08-19 10:18:16 +00003768 union tpacket_req_u req_u;
3769 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Eric Dumazet5171b372018-04-15 17:52:04 -07003771 lock_sock(sk);
chetan lokef6fb8f12011-08-19 10:18:16 +00003772 switch (po->tp_version) {
3773 case TPACKET_V1:
3774 case TPACKET_V2:
3775 len = sizeof(req_u.req);
3776 break;
3777 case TPACKET_V3:
3778 default:
3779 len = sizeof(req_u.req3);
3780 break;
3781 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003782 if (optlen < len) {
3783 ret = -EINVAL;
3784 } else {
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003785 if (copy_from_sockptr(&req_u.req, optval, len))
Eric Dumazet5171b372018-04-15 17:52:04 -07003786 ret = -EFAULT;
3787 else
3788 ret = packet_set_ring(sk, &req_u, 0,
3789 optname == PACKET_TX_RING);
3790 }
3791 release_sock(sk);
3792 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 }
3794 case PACKET_COPY_THRESH:
3795 {
3796 int val;
3797
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003798 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003800 if (copy_from_sockptr(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return -EFAULT;
3802
3803 pkt_sk(sk)->copy_thresh = val;
3804 return 0;
3805 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003806 case PACKET_VERSION:
3807 {
3808 int val;
3809
3810 if (optlen != sizeof(val))
3811 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003812 if (copy_from_sockptr(&val, optval, sizeof(val)))
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003813 return -EFAULT;
3814 switch (val) {
3815 case TPACKET_V1:
3816 case TPACKET_V2:
chetan lokef6fb8f12011-08-19 10:18:16 +00003817 case TPACKET_V3:
Philip Pettersson84ac7262016-11-30 14:55:36 -08003818 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003819 default:
3820 return -EINVAL;
3821 }
Philip Pettersson84ac7262016-11-30 14:55:36 -08003822 lock_sock(sk);
3823 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3824 ret = -EBUSY;
3825 } else {
3826 po->tp_version = val;
3827 ret = 0;
3828 }
3829 release_sock(sk);
3830 return ret;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003831 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003832 case PACKET_RESERVE:
3833 {
3834 unsigned 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)))
Patrick McHardy8913336a2008-07-18 18:05:19 -07003839 return -EFAULT;
Andrey Konovalovbcc53642017-03-29 16:11:22 +02003840 if (val > INT_MAX)
3841 return -EINVAL;
Willem de Bruijnc27927e2017-08-10 12:41:58 -04003842 lock_sock(sk);
3843 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3844 ret = -EBUSY;
3845 } else {
3846 po->tp_reserve = val;
3847 ret = 0;
3848 }
3849 release_sock(sk);
3850 return ret;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003851 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003852 case PACKET_LOSS:
3853 {
3854 unsigned int val;
3855
3856 if (optlen != sizeof(val))
3857 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003858 if (copy_from_sockptr(&val, optval, sizeof(val)))
Johann Baudy69e3c752009-05-18 22:11:22 -07003859 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003860
3861 lock_sock(sk);
3862 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3863 ret = -EBUSY;
3864 } else {
3865 po->tp_loss = !!val;
3866 ret = 0;
3867 }
3868 release_sock(sk);
3869 return ret;
Johann Baudy69e3c752009-05-18 22:11:22 -07003870 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003871 case PACKET_AUXDATA:
3872 {
3873 int val;
3874
3875 if (optlen < sizeof(val))
3876 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003877 if (copy_from_sockptr(&val, optval, sizeof(val)))
Herbert Xu8dc41942007-02-04 23:31:32 -08003878 return -EFAULT;
3879
Willem de Bruijna6361f02018-04-23 17:37:03 -04003880 lock_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003881 po->auxdata = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003882 release_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003883 return 0;
3884 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003885 case PACKET_ORIGDEV:
3886 {
3887 int val;
3888
3889 if (optlen < sizeof(val))
3890 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003891 if (copy_from_sockptr(&val, optval, sizeof(val)))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003892 return -EFAULT;
3893
Willem de Bruijna6361f02018-04-23 17:37:03 -04003894 lock_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003895 po->origdev = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003896 release_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003897 return 0;
3898 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003899 case PACKET_VNET_HDR:
3900 {
3901 int val;
3902
3903 if (sock->type != SOCK_RAW)
3904 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003905 if (optlen < sizeof(val))
3906 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003907 if (copy_from_sockptr(&val, optval, sizeof(val)))
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003908 return -EFAULT;
3909
Willem de Bruijna6361f02018-04-23 17:37:03 -04003910 lock_sock(sk);
3911 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3912 ret = -EBUSY;
3913 } else {
3914 po->has_vnet_hdr = !!val;
3915 ret = 0;
3916 }
3917 release_sock(sk);
3918 return ret;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003919 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003920 case PACKET_TIMESTAMP:
3921 {
3922 int val;
3923
3924 if (optlen != sizeof(val))
3925 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003926 if (copy_from_sockptr(&val, optval, sizeof(val)))
Scott McMillan614f60f2010-06-02 05:53:56 -07003927 return -EFAULT;
3928
3929 po->tp_tstamp = val;
3930 return 0;
3931 }
David S. Millerdc99f602011-07-05 01:45:05 -07003932 case PACKET_FANOUT:
3933 {
Tanner Love9c661b02020-11-06 13:07:40 -05003934 struct fanout_args args = { 0 };
David S. Millerdc99f602011-07-05 01:45:05 -07003935
Tanner Love9c661b02020-11-06 13:07:40 -05003936 if (optlen != sizeof(int) && optlen != sizeof(args))
David S. Millerdc99f602011-07-05 01:45:05 -07003937 return -EINVAL;
Tanner Love9c661b02020-11-06 13:07:40 -05003938 if (copy_from_sockptr(&args, optval, optlen))
David S. Millerdc99f602011-07-05 01:45:05 -07003939 return -EFAULT;
3940
Tanner Love9c661b02020-11-06 13:07:40 -05003941 return fanout_add(sk, &args);
David S. Millerdc99f602011-07-05 01:45:05 -07003942 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003943 case PACKET_FANOUT_DATA:
3944 {
3945 if (!po->fanout)
3946 return -EINVAL;
3947
3948 return fanout_set_data(po, optval, optlen);
3949 }
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003950 case PACKET_IGNORE_OUTGOING:
3951 {
3952 int val;
3953
3954 if (optlen != sizeof(val))
3955 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003956 if (copy_from_sockptr(&val, optval, sizeof(val)))
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003957 return -EFAULT;
3958 if (val < 0 || val > 1)
3959 return -EINVAL;
3960
3961 po->prot_hook.ignore_outgoing = !!val;
3962 return 0;
3963 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003964 case PACKET_TX_HAS_OFF:
3965 {
3966 unsigned int val;
3967
3968 if (optlen != sizeof(val))
3969 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003970 if (copy_from_sockptr(&val, optval, sizeof(val)))
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003971 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003972
3973 lock_sock(sk);
Jiapeng Chong25c55b32021-05-17 18:15:25 +08003974 if (!po->rx_ring.pg_vec && !po->tx_ring.pg_vec)
Willem de Bruijna6361f02018-04-23 17:37:03 -04003975 po->tp_tx_has_off = !!val;
Jiapeng Chong25c55b32021-05-17 18:15:25 +08003976
Willem de Bruijna6361f02018-04-23 17:37:03 -04003977 release_sock(sk);
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003978 return 0;
3979 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003980 case PACKET_QDISC_BYPASS:
3981 {
3982 int val;
3983
3984 if (optlen != sizeof(val))
3985 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003986 if (copy_from_sockptr(&val, optval, sizeof(val)))
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003987 return -EFAULT;
3988
3989 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3990 return 0;
3991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 default:
3993 return -ENOPROTOOPT;
3994 }
3995}
3996
3997static int packet_getsockopt(struct socket *sock, int level, int optname,
3998 char __user *optval, int __user *optlen)
3999{
4000 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00004001 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 struct sock *sk = sock->sk;
4003 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00004004 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004005 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04004006 struct tpacket_rollover_stats rstats;
Eric Dumazet8e8e2952019-06-12 09:52:30 -07004007 int drops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
4009 if (level != SOL_PACKET)
4010 return -ENOPROTOOPT;
4011
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004012 if (get_user(len, optlen))
4013 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 if (len < 0)
4016 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004017
Johann Baudy69e3c752009-05-18 22:11:22 -07004018 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004021 memcpy(&st, &po->stats, sizeof(st));
4022 memset(&po->stats, 0, sizeof(po->stats));
4023 spin_unlock_bh(&sk->sk_receive_queue.lock);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07004024 drops = atomic_xchg(&po->tp_drops, 0);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004025
chetan lokef6fb8f12011-08-19 10:18:16 +00004026 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00004027 lv = sizeof(struct tpacket_stats_v3);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07004028 st.stats3.tp_drops = drops;
4029 st.stats3.tp_packets += drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004030 data = &st.stats3;
chetan lokef6fb8f12011-08-19 10:18:16 +00004031 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00004032 lv = sizeof(struct tpacket_stats);
Eric Dumazet8e8e2952019-06-12 09:52:30 -07004033 st.stats1.tp_drops = drops;
4034 st.stats1.tp_packets += drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004035 data = &st.stats1;
chetan lokef6fb8f12011-08-19 10:18:16 +00004036 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00004037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08004039 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08004040 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08004041 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07004042 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07004043 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07004044 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08004045 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08004046 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08004047 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004048 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004049 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004050 break;
4051 case PACKET_HDRLEN:
4052 if (len > sizeof(int))
4053 len = sizeof(int);
Alexander Potapenkofd2c83b2017-04-25 18:51:46 +02004054 if (len < sizeof(int))
4055 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004056 if (copy_from_user(&val, optval, len))
4057 return -EFAULT;
4058 switch (val) {
4059 case TPACKET_V1:
4060 val = sizeof(struct tpacket_hdr);
4061 break;
4062 case TPACKET_V2:
4063 val = sizeof(struct tpacket2_hdr);
4064 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004065 case TPACKET_V3:
4066 val = sizeof(struct tpacket3_hdr);
4067 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004068 default:
4069 return -EINVAL;
4070 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004071 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004072 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07004073 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004074 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07004075 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07004076 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07004077 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07004078 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07004079 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07004080 break;
David S. Millerdc99f602011-07-05 01:45:05 -07004081 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07004082 val = (po->fanout ?
4083 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00004084 ((u32)po->fanout->type << 16) |
4085 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07004086 0);
David S. Millerdc99f602011-07-05 01:45:05 -07004087 break;
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02004088 case PACKET_IGNORE_OUTGOING:
4089 val = po->prot_hook.ignore_outgoing;
4090 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04004091 case PACKET_ROLLOVER_STATS:
Mike Maloney57f015f2017-11-28 10:44:29 -05004092 if (!po->rollover)
Willem de Bruijna9b63912015-05-12 11:56:50 -04004093 return -EINVAL;
Mike Maloney57f015f2017-11-28 10:44:29 -05004094 rstats.tp_all = atomic_long_read(&po->rollover->num);
4095 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
4096 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
4097 data = &rstats;
4098 lv = sizeof(rstats);
Willem de Bruijna9b63912015-05-12 11:56:50 -04004099 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00004100 case PACKET_TX_HAS_OFF:
4101 val = po->tp_tx_has_off;
4102 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01004103 case PACKET_QDISC_BYPASS:
4104 val = packet_use_direct_xmit(po);
4105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 default:
4107 return -ENOPROTOOPT;
4108 }
4109
Eric Dumazetc06fff62012-04-19 21:56:11 +00004110 if (len > lv)
4111 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004112 if (put_user(len, optlen))
4113 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08004114 if (copy_to_user(optval, data, len))
4115 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
Jiri Pirko351638e2013-05-28 01:30:21 +00004119static int packet_notifier(struct notifier_block *this,
4120 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
4122 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00004123 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004124 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
stephen hemminger808f5112010-02-22 07:57:18 +00004126 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004127 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 struct packet_sock *po = pkt_sk(sk);
4129
4130 switch (msg) {
4131 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07004133 packet_dev_mclist_delete(dev, &po->mclist);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004134 fallthrough;
David S. Millera2efcfa2007-05-29 13:12:50 -07004135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 case NETDEV_DOWN:
4137 if (dev->ifindex == po->ifindex) {
4138 spin_lock(&po->bind_lock);
4139 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07004140 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 sk->sk_err = ENETDOWN;
4142 if (!sock_flag(sk, SOCK_DEAD))
Alexander Aringe3ae2362021-06-27 18:48:21 -04004143 sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 }
4145 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01004146 packet_cached_dev_reset(po);
Eric Dumazete032f7c2021-06-16 06:42:02 -07004147 WRITE_ONCE(po->ifindex, -1);
Eric Dumazetf1d92682021-12-14 07:09:33 -08004148 dev_put_track(po->prot_hook.dev,
4149 &po->prot_hook.dev_tracker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 po->prot_hook.dev = NULL;
4151 }
4152 spin_unlock(&po->bind_lock);
4153 }
4154 break;
4155 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00004156 if (dev->ifindex == po->ifindex) {
4157 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004158 if (po->num)
4159 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00004160 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 break;
4163 }
4164 }
stephen hemminger808f5112010-02-22 07:57:18 +00004165 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 return NOTIFY_DONE;
4167}
4168
4169
4170static int packet_ioctl(struct socket *sock, unsigned int cmd,
4171 unsigned long arg)
4172{
4173 struct sock *sk = sock->sk;
4174
Johann Baudy69e3c752009-05-18 22:11:22 -07004175 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004176 case SIOCOUTQ:
4177 {
4178 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07004179
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004180 return put_user(amount, (int __user *)arg);
4181 }
4182 case SIOCINQ:
4183 {
4184 struct sk_buff *skb;
4185 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004187 spin_lock_bh(&sk->sk_receive_queue.lock);
4188 skb = skb_peek(&sk->sk_receive_queue);
4189 if (skb)
4190 amount = skb->len;
4191 spin_unlock_bh(&sk->sk_receive_queue.lock);
4192 return put_user(amount, (int __user *)arg);
4193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004195 case SIOCADDRT:
4196 case SIOCDELRT:
4197 case SIOCDARP:
4198 case SIOCGARP:
4199 case SIOCSARP:
4200 case SIOCGIFADDR:
4201 case SIOCSIFADDR:
4202 case SIOCGIFBRDADDR:
4203 case SIOCSIFBRDADDR:
4204 case SIOCGIFNETMASK:
4205 case SIOCSIFNETMASK:
4206 case SIOCGIFDSTADDR:
4207 case SIOCSIFDSTADDR:
4208 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004209 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210#endif
4211
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004212 default:
4213 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
4215 return 0;
4216}
4217
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004218static __poll_t packet_poll(struct file *file, struct socket *sock,
4219 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
4221 struct sock *sk = sock->sk;
4222 struct packet_sock *po = pkt_sk(sk);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004223 __poll_t mask = datagram_poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004226 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004227 if (!packet_previous_rx_frame(po, &po->rx_ring,
4228 TP_STATUS_KERNEL))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004229 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
Eric Dumazet9bb6cd62019-06-12 09:52:33 -07004231 packet_rcv_try_clear_pressure(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004233 spin_lock_bh(&sk->sk_write_queue.lock);
4234 if (po->tx_ring.pg_vec) {
4235 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004236 mask |= EPOLLOUT | EPOLLWRNORM;
Johann Baudy69e3c752009-05-18 22:11:22 -07004237 }
4238 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return mask;
4240}
4241
4242
4243/* Dirty? Well, I still did not learn better way to account
4244 * for user mmaps.
4245 */
4246
4247static void packet_mm_open(struct vm_area_struct *vma)
4248{
4249 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004250 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (sk)
4254 atomic_inc(&pkt_sk(sk)->mapped);
4255}
4256
4257static void packet_mm_close(struct vm_area_struct *vma)
4258{
4259 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004260 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 if (sk)
4264 atomic_dec(&pkt_sk(sk)->mapped);
4265}
4266
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004267static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004268 .open = packet_mm_open,
4269 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270};
4271
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004272static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
4273 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
4275 int i;
4276
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004277 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004278 if (likely(pg_vec[i].buffer)) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004279 if (is_vmalloc_addr(pg_vec[i].buffer))
4280 vfree(pg_vec[i].buffer);
4281 else
4282 free_pages((unsigned long)pg_vec[i].buffer,
4283 order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004284 pg_vec[i].buffer = NULL;
4285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
4287 kfree(pg_vec);
4288}
4289
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004290static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004291{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004292 char *buffer;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004293 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
4294 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07004295
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004296 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004297 if (buffer)
4298 return buffer;
4299
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004300 /* __get_free_pages failed, fall back to vmalloc */
4301 buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
4302 if (buffer)
4303 return buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08004304
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004305 /* vmalloc failed, lets dig into swap here */
4306 gfp_flags &= ~__GFP_NORETRY;
4307 buffer = (char *) __get_free_pages(gfp_flags, order);
4308 if (buffer)
4309 return buffer;
4310
4311 /* complete and utter failure */
4312 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004313}
4314
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004315static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004316{
4317 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004318 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004319 int i;
4320
Christoph Paasch398f0132019-03-18 23:14:52 -07004321 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004322 if (unlikely(!pg_vec))
4323 goto out;
4324
4325 for (i = 0; i < block_nr; i++) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004326 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004327 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004328 goto out_free_pgvec;
4329 }
4330
4331out:
4332 return pg_vec;
4333
4334out_free_pgvec:
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004335 free_pg_vec(pg_vec, order, block_nr);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004336 pg_vec = NULL;
4337 goto out;
4338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
chetan lokef6fb8f12011-08-19 10:18:16 +00004340static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004341 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Neil Horman0e3125c2010-11-16 10:26:47 -08004343 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijn61fad682020-03-13 12:18:09 -04004345 unsigned long *rx_owner_map = NULL;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004346 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004347 struct packet_ring_buffer *rb;
4348 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004349 __be16 num;
Colin Ian King2a6d6c32020-07-01 16:04:33 +01004350 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +00004351 /* Added to avoid minimal code churn */
4352 struct tpacket_req *req = &req_u->req;
4353
Johann Baudy69e3c752009-05-18 22:11:22 -07004354 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4355 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4356
4357 err = -EBUSY;
4358 if (!closing) {
4359 if (atomic_read(&po->mapped))
4360 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004361 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004362 goto out;
4363 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 if (req->tp_block_nr) {
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004366 unsigned int min_frame_size;
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004369 err = -EBUSY;
4370 if (unlikely(rb->pg_vec))
4371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004373 switch (po->tp_version) {
4374 case TPACKET_V1:
4375 po->tp_hdrlen = TPACKET_HDRLEN;
4376 break;
4377 case TPACKET_V2:
4378 po->tp_hdrlen = TPACKET2_HDRLEN;
4379 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004380 case TPACKET_V3:
4381 po->tp_hdrlen = TPACKET3_HDRLEN;
4382 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004383 }
4384
Johann Baudy69e3c752009-05-18 22:11:22 -07004385 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004386 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004387 goto out;
Tobias Klauser90836b62015-11-17 10:40:21 +01004388 if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004389 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004390 min_frame_size = po->tp_hdrlen + po->tp_reserve;
Eric Dumazetdc808112014-08-15 09:16:04 -07004391 if (po->tp_version >= TPACKET_V3 &&
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004392 req->tp_block_size <
4393 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
Eric Dumazetdc808112014-08-15 09:16:04 -07004394 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004395 if (unlikely(req->tp_frame_size < min_frame_size))
Johann Baudy69e3c752009-05-18 22:11:22 -07004396 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004397 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Tobias Klauser4194b492015-11-17 10:38:36 +01004400 rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
4401 if (unlikely(rb->frames_per_block == 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004402 goto out;
Kal Conleyfc628142019-02-10 09:57:11 +01004403 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
Andrey Konovalov8f8d28e2017-03-29 16:11:21 +02004404 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004405 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4406 req->tp_frame_nr))
4407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 err = -ENOMEM;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004410 order = get_order(req->tp_block_size);
4411 pg_vec = alloc_pg_vec(req, order);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004412 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 goto out;
chetan lokef6fb8f12011-08-19 10:18:16 +00004414 switch (po->tp_version) {
4415 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004416 /* Block transmit is not supported yet */
4417 if (!tx_ring) {
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304418 init_prb_bdqc(po, rb, pg_vec, req_u);
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004419 } else {
4420 struct tpacket_req3 *req3 = &req_u->req3;
4421
4422 if (req3->tp_retire_blk_tov ||
4423 req3->tp_sizeof_priv ||
4424 req3->tp_feature_req_word) {
4425 err = -EINVAL;
Eric Dumazet55655e32019-06-24 02:38:20 -07004426 goto out_free_pg_vec;
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004427 }
4428 }
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004429 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004430 default:
Willem de Bruijn61fad682020-03-13 12:18:09 -04004431 if (!tx_ring) {
4432 rx_owner_map = bitmap_alloc(req->tp_frame_nr,
4433 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
4434 if (!rx_owner_map)
4435 goto out_free_pg_vec;
4436 }
chetan lokef6fb8f12011-08-19 10:18:16 +00004437 break;
4438 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004439 }
4440 /* Done */
4441 else {
4442 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004443 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 /* Detach socket from network */
4449 spin_lock(&po->bind_lock);
4450 was_running = po->running;
4451 num = po->num;
4452 if (was_running) {
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07004453 WRITE_ONCE(po->num, 0);
David S. Millerce06b032011-07-04 01:44:29 -07004454 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
4456 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 synchronize_net();
4459
4460 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004461 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (closing || atomic_read(&po->mapped) == 0) {
4463 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004464 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004465 swap(rb->pg_vec, pg_vec);
Willem de Bruijn61fad682020-03-13 12:18:09 -04004466 if (po->tp_version <= TPACKET_V2)
4467 swap(rb->rx_owner_map, rx_owner_map);
Johann Baudy69e3c752009-05-18 22:11:22 -07004468 rb->frame_max = (req->tp_frame_nr - 1);
4469 rb->head = 0;
4470 rb->frame_size = req->tp_frame_size;
4471 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004473 swap(rb->pg_vec_order, order);
Changli Gaoc053fd92010-12-10 16:02:20 -08004474 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Johann Baudy69e3c752009-05-18 22:11:22 -07004476 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4477 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4478 tpacket_rcv : packet_rcv;
4479 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004481 pr_err("packet_mmap: vma is busy: %d\n",
4482 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 }
Herbert Xu905db442009-01-30 14:12:06 -08004484 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004487 if (was_running) {
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07004488 WRITE_ONCE(po->num, num);
David S. Millerce06b032011-07-04 01:44:29 -07004489 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
4491 spin_unlock(&po->bind_lock);
WANG Congc800aaf2017-07-24 10:07:32 -07004492 if (pg_vec && (po->tp_version > TPACKET_V2)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004493 /* Because we don't support block-based V3 on tx-ring */
4494 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004495 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f12011-08-19 10:18:16 +00004496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Eric Dumazet55655e32019-06-24 02:38:20 -07004498out_free_pg_vec:
Willem de Bruijn61fad682020-03-13 12:18:09 -04004499 bitmap_free(rx_owner_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (pg_vec)
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004501 free_pg_vec(pg_vec, order, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502out:
4503 return err;
4504}
4505
Johann Baudy69e3c752009-05-18 22:11:22 -07004506static int packet_mmap(struct file *file, struct socket *sock,
4507 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508{
4509 struct sock *sk = sock->sk;
4510 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004511 unsigned long size, expected_size;
4512 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 unsigned long start;
4514 int err = -EINVAL;
4515 int i;
4516
4517 if (vma->vm_pgoff)
4518 return -EINVAL;
4519
Herbert Xu905db442009-01-30 14:12:06 -08004520 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004521
4522 expected_size = 0;
4523 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4524 if (rb->pg_vec) {
4525 expected_size += rb->pg_vec_len
4526 * rb->pg_vec_pages
4527 * PAGE_SIZE;
4528 }
4529 }
4530
4531 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004533
4534 size = vma->vm_end - vma->vm_start;
4535 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 goto out;
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004539 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4540 if (rb->pg_vec == NULL)
4541 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004542
Johann Baudy69e3c752009-05-18 22:11:22 -07004543 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004544 struct page *page;
4545 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004546 int pg_num;
4547
Changli Gaoc56b4d92010-12-01 02:52:57 +00004548 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4549 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004550 err = vm_insert_page(vma, start, page);
4551 if (unlikely(err))
4552 goto out;
4553 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004554 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004555 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004558
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004559 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 vma->vm_ops = &packet_mmap_ops;
4561 err = 0;
4562
4563out:
Herbert Xu905db442009-01-30 14:12:06 -08004564 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 return err;
4566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004568static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 .family = PF_PACKET,
4570 .owner = THIS_MODULE,
4571 .release = packet_release,
4572 .bind = packet_bind_spkt,
4573 .connect = sock_no_connect,
4574 .socketpair = sock_no_socketpair,
4575 .accept = sock_no_accept,
4576 .getname = packet_getname_spkt,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004577 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004579 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 .listen = sock_no_listen,
4581 .shutdown = sock_no_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 .sendmsg = packet_sendmsg_spkt,
4583 .recvmsg = packet_recvmsg,
4584 .mmap = sock_no_mmap,
4585 .sendpage = sock_no_sendpage,
4586};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004588static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 .family = PF_PACKET,
4590 .owner = THIS_MODULE,
4591 .release = packet_release,
4592 .bind = packet_bind,
4593 .connect = sock_no_connect,
4594 .socketpair = sock_no_socketpair,
4595 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004596 .getname = packet_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004597 .poll = packet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 .ioctl = packet_ioctl,
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +02004599 .gettstamp = sock_gettstamp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 .listen = sock_no_listen,
4601 .shutdown = sock_no_shutdown,
4602 .setsockopt = packet_setsockopt,
4603 .getsockopt = packet_getsockopt,
4604 .sendmsg = packet_sendmsg,
4605 .recvmsg = packet_recvmsg,
4606 .mmap = packet_mmap,
4607 .sendpage = sock_no_sendpage,
4608};
4609
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004610static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 .family = PF_PACKET,
4612 .create = packet_create,
4613 .owner = THIS_MODULE,
4614};
4615
4616static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004617 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618};
4619
4620#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004623 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Denis V. Luneve372c412007-11-19 22:31:54 -08004625 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004626
4627 rcu_read_lock();
4628 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629}
4630
4631static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4632{
Herbert Xu1bf40952007-12-16 14:04:02 -08004633 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004634 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
4637static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004638 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
stephen hemminger808f5112010-02-22 07:57:18 +00004640 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004643static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
4645 if (v == SEQ_START_TOKEN)
Baruch Siachabdcd062020-12-16 09:28:04 +02004646 seq_printf(seq,
4647 "%*sRefCnt Type Proto Iface R Rmem User Inode\n",
4648 IS_ENABLED(CONFIG_64BIT) ? -17 : -9, "sk");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004650 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 const struct packet_sock *po = pkt_sk(s);
4652
4653 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004654 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004656 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 s->sk_type,
Eric Dumazetc7d2ef52021-06-16 06:42:01 -07004658 ntohs(READ_ONCE(po->num)),
Eric Dumazete032f7c2021-06-16 06:42:02 -07004659 READ_ONCE(po->ifindex),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 po->running,
4661 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004662 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004663 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665
4666 return 0;
4667}
4668
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004669static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 .start = packet_seq_start,
4671 .next = packet_seq_next,
4672 .stop = packet_seq_stop,
4673 .show = packet_seq_show,
4674};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675#endif
4676
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004677static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004678{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004679 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004680 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004681
Yonatan Linika268e0f2020-12-14 22:25:50 +02004682#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02004683 if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
4684 sizeof(struct seq_net_private)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004685 return -ENOMEM;
Yonatan Linika268e0f2020-12-14 22:25:50 +02004686#endif /* CONFIG_PROC_FS */
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004687
4688 return 0;
4689}
4690
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004691static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004692{
Gao fengece31ff2013-02-18 01:34:56 +00004693 remove_proc_entry("packet", net->proc_net);
Vasily Averin669f8f12017-11-12 22:27:49 +03004694 WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004695}
4696
4697static struct pernet_operations packet_net_ops = {
4698 .init = packet_net_init,
4699 .exit = packet_net_exit,
4700};
4701
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703static void __exit packet_exit(void)
4704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004706 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 sock_unregister(PF_PACKET);
4708 proto_unregister(&packet_proto);
4709}
4710
4711static int __init packet_init(void)
4712{
YueHaibing36096f22019-05-09 22:52:20 +08004713 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
YueHaibing36096f22019-05-09 22:52:20 +08004715 rc = proto_register(&packet_proto, 0);
4716 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 goto out;
YueHaibing36096f22019-05-09 22:52:20 +08004718 rc = sock_register(&packet_family_ops);
4719 if (rc)
4720 goto out_proto;
4721 rc = register_pernet_subsys(&packet_net_ops);
4722 if (rc)
4723 goto out_sock;
4724 rc = register_netdevice_notifier(&packet_netdev_notifier);
4725 if (rc)
4726 goto out_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
YueHaibing36096f22019-05-09 22:52:20 +08004728 return 0;
4729
4730out_pernet:
4731 unregister_pernet_subsys(&packet_net_ops);
4732out_sock:
4733 sock_unregister(PF_PACKET);
4734out_proto:
4735 proto_unregister(&packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736out:
4737 return rc;
4738}
4739
4740module_init(packet_init);
4741module_exit(packet_exit);
4742MODULE_LICENSE("GPL");
4743MODULE_ALIAS_NETPROTO(PF_PACKET);