blob: 802fe548d621ebf25a18792dde1efc9fcfcab747 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * PACKET - implements raw packet sockets.
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
11 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090012 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090035 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070037 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090040 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070041 * and packet_mreq.
Johann Baudy69e3c752009-05-18 22:11:22 -070042 * Johann Baudy : Added TX RING.
chetan lokef6fb8f102011-08-19 10:18:16 +000043 * Chetan Loke : Implemented TPACKET_V3 block abstraction
44 * layer.
45 * Copyright (C) 2011, <lokec@ccs.neu.edu>
46 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * This program is free software; you can redistribute it and/or
49 * modify it under the terms of the GNU General Public License
50 * as published by the Free Software Foundation; either version
51 * 2 of the License, or (at your option) any later version.
52 *
53 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/fcntl.h>
59#include <linux/socket.h>
60#include <linux/in.h>
61#include <linux/inet.h>
62#include <linux/netdevice.h>
63#include <linux/if_packet.h>
64#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080065#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080068#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/ip.h>
71#include <net/protocol.h>
72#include <linux/skbuff.h>
73#include <net/sock.h>
74#include <linux/errno.h>
75#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/uaccess.h>
77#include <asm/ioctls.h>
78#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <asm/io.h>
81#include <linux/proc_fs.h>
82#include <linux/seq_file.h>
83#include <linux/poll.h>
84#include <linux/module.h>
85#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080086#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070087#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080088#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000089#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070090#include <linux/net_tstamp.h>
Daniel Borkmannb0138402014-01-15 16:25:36 +010091#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_INET
93#include <net/inet_common.h>
94#endif
Willem de Bruijn47dceb82015-08-14 22:31:34 -040095#include <linux/bpf.h>
Willem de Bruijn719c44d2016-06-07 12:06:34 -040096#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Pavel Emelyanov2787b042012-08-13 05:49:39 +000098#include "internal.h"
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 Assumptions:
102 - if device has no dev->hard_header routine, it adds and removes ll header
103 inside itself. In this case ll header is invisible outside of device,
104 but higher levels still should reserve dev->hard_header_len.
105 Some devices are enough clever to reallocate skb, when header
106 will not fit to reserved space (tunnel), another ones are silly
107 (PPP).
108 - packet socket receives packets with pulled ll header,
109 so that SOCK_RAW should push it back.
110
111On receive:
112-----------
113
114Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700115 mac_header -> ll header
116 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700119 mac_header -> ll header
120 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700123 mac_header -> UNKNOWN position. It is very likely, that it points to ll
124 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900125 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700126 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700129 mac_header -> data. ll header is still not built!
130 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132Resume
133 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
134
135
136On transmit:
137------------
138
139dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700140 mac_header -> ll header
141 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700144 mac_header -> data
145 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 We should set nh.raw on output to correct posistion,
148 packet classifier depends on it.
149 */
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Private packet socket structures. */
152
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700153/* identical to struct packet_mreq except it has
154 * a longer address field.
155 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000156struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700157 int mr_ifindex;
158 unsigned short mr_type;
159 unsigned short mr_alen;
160 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
David S. Millera2efcfa2007-05-29 13:12:50 -0700162
Daniel Borkmann184f4892013-04-16 01:57:46 +0000163union tpacket_uhdr {
164 struct tpacket_hdr *h1;
165 struct tpacket2_hdr *h2;
166 struct tpacket3_hdr *h3;
167 void *raw;
168};
169
chetan lokef6fb8f102011-08-19 10:18:16 +0000170static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700171 int closing, int tx_ring);
172
chetan lokef6fb8f102011-08-19 10:18:16 +0000173#define V3_ALIGNMENT (8)
174
chetan lokebc59ba32011-08-25 10:43:30 +0000175#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f102011-08-19 10:18:16 +0000176
177#define BLK_PLUS_PRIV(sz_of_priv) \
178 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
179
chetan lokef6fb8f102011-08-19 10:18:16 +0000180#define PGV_FROM_VMALLOC 1
Johann Baudy69e3c752009-05-18 22:11:22 -0700181
chetan lokef6fb8f102011-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)
188#define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
189
Johann Baudy69e3c752009-05-18 22:11:22 -0700190struct packet_sock;
191static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000192static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
193 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
chetan lokef6fb8f102011-08-19 10:18:16 +0000195static void *packet_previous_frame(struct packet_sock *po,
196 struct packet_ring_buffer *rb,
197 int status);
198static void packet_increment_head(struct packet_ring_buffer *buff);
chetan lokebc59ba32011-08-25 10:43:30 +0000199static int prb_curr_blk_in_use(struct tpacket_kbdq_core *,
200 struct tpacket_block_desc *);
201static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f102011-08-19 10:18:16 +0000202 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000203static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f102011-08-19 10:18:16 +0000204 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000205static int prb_queue_frozen(struct tpacket_kbdq_core *);
206static void prb_open_block(struct tpacket_kbdq_core *,
207 struct tpacket_block_desc *);
chetan lokef6fb8f102011-08-19 10:18:16 +0000208static void prb_retire_rx_blk_timer_expired(unsigned long);
chetan lokebc59ba32011-08-25 10:43:30 +0000209static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
210static void prb_init_blk_timer(struct packet_sock *,
211 struct tpacket_kbdq_core *,
212 void (*func) (unsigned long));
213static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
214static void prb_clear_rxhash(struct tpacket_kbdq_core *,
215 struct tpacket3_hdr *);
216static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
217 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static void packet_flush_mclist(struct sock *sk);
219
Herbert Xuffbc6112007-02-04 23:33:10 -0800220struct packet_skb_cb {
Herbert Xuffbc6112007-02-04 23:33:10 -0800221 union {
222 struct sockaddr_pkt pkt;
Eyal Birger2472d762015-03-01 14:58:28 +0200223 union {
224 /* Trick: alias skb original length with
225 * ll.sll_family and ll.protocol in order
226 * to save room.
227 */
228 unsigned int origlen;
229 struct sockaddr_ll ll;
230 };
Herbert Xuffbc6112007-02-04 23:33:10 -0800231 } sa;
232};
233
David Woodhoused3869ef2015-09-23 19:45:08 +0100234#define vio_le() virtio_legacy_is_little_endian()
235
Herbert Xuffbc6112007-02-04 23:33:10 -0800236#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800237
chetan lokebc59ba32011-08-25 10:43:30 +0000238#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f102011-08-19 10:18:16 +0000239#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000240 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f102011-08-19 10:18:16 +0000241#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000242 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f102011-08-19 10:18:16 +0000243#define GET_NEXT_PRB_BLK_NUM(x) \
244 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
245 ((x)->kactive_blk_num+1) : 0)
246
David S. Millerdc99f602011-07-05 01:45:05 -0700247static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
248static void __fanout_link(struct sock *sk, struct packet_sock *po);
249
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100250static int packet_direct_xmit(struct sk_buff *skb)
251{
252 struct net_device *dev = skb->dev;
Willem de Bruijn104ba782016-10-26 11:23:07 -0400253 struct sk_buff *orig_skb = skb;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100254 struct netdev_queue *txq;
Daniel Borkmann43279502014-03-27 16:38:30 +0100255 int ret = NETDEV_TX_BUSY;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100256
257 if (unlikely(!netif_running(dev) ||
Daniel Borkmann43279502014-03-27 16:38:30 +0100258 !netif_carrier_ok(dev)))
259 goto drop;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100260
Willem de Bruijn104ba782016-10-26 11:23:07 -0400261 skb = validate_xmit_skb_list(skb, dev);
262 if (skb != orig_skb)
Daniel Borkmann43279502014-03-27 16:38:30 +0100263 goto drop;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100264
Daniel Borkmann10c51b56232014-08-27 11:11:27 +0200265 txq = skb_get_tx_queue(dev, skb);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100266
Daniel Borkmann43279502014-03-27 16:38:30 +0100267 local_bh_disable();
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100268
Daniel Borkmann43279502014-03-27 16:38:30 +0100269 HARD_TX_LOCK(dev, txq, smp_processor_id());
David S. Miller10b3ad82014-08-29 21:07:24 -0700270 if (!netif_xmit_frozen_or_drv_stopped(txq))
David S. Millerfa2dbdc2014-08-29 21:55:22 -0700271 ret = netdev_start_xmit(skb, dev, txq, false);
Daniel Borkmann43279502014-03-27 16:38:30 +0100272 HARD_TX_UNLOCK(dev, txq);
273
274 local_bh_enable();
275
276 if (!dev_xmit_complete(ret))
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100277 kfree_skb(skb);
Daniel Borkmann43279502014-03-27 16:38:30 +0100278
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100279 return ret;
Daniel Borkmann43279502014-03-27 16:38:30 +0100280drop:
Daniel Borkmann0f97ede2014-04-02 20:52:56 +0200281 atomic_long_inc(&dev->tx_dropped);
Willem de Bruijn104ba782016-10-26 11:23:07 -0400282 kfree_skb_list(skb);
Daniel Borkmann43279502014-03-27 16:38:30 +0100283 return NET_XMIT_DROP;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100284}
285
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100286static struct net_device *packet_cached_dev_get(struct packet_sock *po)
287{
288 struct net_device *dev;
289
290 rcu_read_lock();
291 dev = rcu_dereference(po->cached_dev);
292 if (likely(dev))
293 dev_hold(dev);
294 rcu_read_unlock();
295
296 return dev;
297}
298
299static void packet_cached_dev_assign(struct packet_sock *po,
300 struct net_device *dev)
301{
302 rcu_assign_pointer(po->cached_dev, dev);
303}
304
305static void packet_cached_dev_reset(struct packet_sock *po)
306{
307 RCU_INIT_POINTER(po->cached_dev, NULL);
308}
309
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100310static bool packet_use_direct_xmit(const struct packet_sock *po)
311{
312 return po->xmit == packet_direct_xmit;
313}
314
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100315static u16 __packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100316{
Li Zhong1cbac012013-12-12 22:39:55 +0100317 return (u16) raw_smp_processor_id() % dev->real_num_tx_queues;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100318}
319
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100320static void packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
321{
322 const struct net_device_ops *ops = dev->netdev_ops;
323 u16 queue_index;
324
325 if (ops->ndo_select_queue) {
326 queue_index = ops->ndo_select_queue(dev, skb, NULL,
327 __packet_pick_tx_queue);
328 queue_index = netdev_cap_txqueue(dev, queue_index);
329 } else {
330 queue_index = __packet_pick_tx_queue(dev, skb);
331 }
332
333 skb_set_queue_mapping(skb, queue_index);
334}
335
Willem de Bruijnb2a52072018-04-23 17:37:03 -0400336/* __register_prot_hook must be invoked through register_prot_hook
David S. Millerce06b032011-07-04 01:44:29 -0700337 * or from a context in which asynchronous accesses to the packet
338 * socket is not possible (packet_create()).
339 */
Willem de Bruijnb2a52072018-04-23 17:37:03 -0400340static void __register_prot_hook(struct sock *sk)
David S. Millerce06b032011-07-04 01:44:29 -0700341{
342 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100343
David S. Millerce06b032011-07-04 01:44:29 -0700344 if (!po->running) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100345 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700346 __fanout_link(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100347 else
David S. Millerdc99f602011-07-05 01:45:05 -0700348 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100349
David S. Millerce06b032011-07-04 01:44:29 -0700350 sock_hold(sk);
351 po->running = 1;
352 }
353}
354
Willem de Bruijnb2a52072018-04-23 17:37:03 -0400355static void register_prot_hook(struct sock *sk)
356{
357 lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
358 __register_prot_hook(sk);
359}
360
361/* If the sync parameter is true, we will temporarily drop
David S. Millerce06b032011-07-04 01:44:29 -0700362 * the po->bind_lock and do a synchronize_net to make sure no
363 * asynchronous packet processing paths still refer to the elements
364 * of po->prot_hook. If the sync parameter is false, it is the
365 * callers responsibility to take care of this.
366 */
367static void __unregister_prot_hook(struct sock *sk, bool sync)
368{
369 struct packet_sock *po = pkt_sk(sk);
370
Willem de Bruijnb2a52072018-04-23 17:37:03 -0400371 lockdep_assert_held_once(&po->bind_lock);
372
David S. Millerce06b032011-07-04 01:44:29 -0700373 po->running = 0;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100374
375 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700376 __fanout_unlink(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100377 else
David S. Millerdc99f602011-07-05 01:45:05 -0700378 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100379
David S. Millerce06b032011-07-04 01:44:29 -0700380 __sock_put(sk);
381
382 if (sync) {
383 spin_unlock(&po->bind_lock);
384 synchronize_net();
385 spin_lock(&po->bind_lock);
386 }
387}
388
389static void unregister_prot_hook(struct sock *sk, bool sync)
390{
391 struct packet_sock *po = pkt_sk(sk);
392
393 if (po->running)
394 __unregister_prot_hook(sk, sync);
395}
396
Michael S. Tsirkin6e580402014-11-24 13:32:16 +0200397static inline struct page * __pure pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000398{
399 if (is_vmalloc_addr(addr))
400 return vmalloc_to_page(addr);
401 return virt_to_page(addr);
402}
403
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700404static void __packet_set_status(struct packet_sock *po, void *frame, int status)
405{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000406 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700407
408 h.raw = frame;
409 switch (po->tp_version) {
410 case TPACKET_V1:
411 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000412 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700413 break;
414 case TPACKET_V2:
415 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000416 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700417 break;
chetan lokef6fb8f102011-08-19 10:18:16 +0000418 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700419 default:
chetan lokef6fb8f102011-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
427static int __packet_get_status(struct packet_sock *po, void *frame)
428{
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 lokef6fb8f102011-08-19 10:18:16 +0000441 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700442 default:
chetan lokef6fb8f102011-08-19 10:18:16 +0000443 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700444 BUG();
445 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
Johann Baudy69e3c752009-05-18 22:11:22 -0700448
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000449static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
450 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000451{
452 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
453
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400454 if (shhwtstamps &&
455 (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
456 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
457 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000458
459 if (ktime_to_timespec_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000460 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000461
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000462 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000463}
464
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000465static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
466 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000467{
468 union tpacket_uhdr h;
469 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000470 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000471
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000472 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
473 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000474
475 h.raw = frame;
476 switch (po->tp_version) {
477 case TPACKET_V1:
478 h.h1->tp_sec = ts.tv_sec;
479 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
480 break;
481 case TPACKET_V2:
482 h.h2->tp_sec = ts.tv_sec;
483 h.h2->tp_nsec = ts.tv_nsec;
484 break;
485 case TPACKET_V3:
486 default:
487 WARN(1, "TPACKET version not supported.\n");
488 BUG();
489 }
490
491 /* one flush is safe, as both fields always lie on the same cacheline */
492 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
493 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000494
495 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000496}
497
Johann Baudy69e3c752009-05-18 22:11:22 -0700498static void *packet_lookup_frame(struct packet_sock *po,
499 struct packet_ring_buffer *rb,
500 unsigned int position,
501 int status)
502{
503 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000504 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700505
506 pg_vec_pos = position / rb->frames_per_block;
507 frame_offset = position % rb->frames_per_block;
508
Neil Horman0e3125c2010-11-16 10:26:47 -0800509 h.raw = rb->pg_vec[pg_vec_pos].buffer +
510 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700511
512 if (status != __packet_get_status(po, h.raw))
513 return NULL;
514
515 return h.raw;
516}
517
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000518static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700519 struct packet_ring_buffer *rb,
520 int status)
521{
522 return packet_lookup_frame(po, rb, rb->head, status);
523}
524
chetan lokebc59ba32011-08-25 10:43:30 +0000525static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000526{
527 del_timer_sync(&pkc->retire_blk_timer);
528}
529
530static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +0000531 struct sk_buff_head *rb_queue)
532{
chetan lokebc59ba32011-08-25 10:43:30 +0000533 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f102011-08-19 10:18:16 +0000534
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200535 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000536
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100537 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f102011-08-19 10:18:16 +0000538 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100539 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f102011-08-19 10:18:16 +0000540
541 prb_del_retire_blk_timer(pkc);
542}
543
544static void prb_init_blk_timer(struct packet_sock *po,
chetan lokebc59ba32011-08-25 10:43:30 +0000545 struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000546 void (*func) (unsigned long))
547{
548 init_timer(&pkc->retire_blk_timer);
549 pkc->retire_blk_timer.data = (long)po;
550 pkc->retire_blk_timer.function = func;
551 pkc->retire_blk_timer.expires = jiffies;
552}
553
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530554static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f102011-08-19 10:18:16 +0000555{
chetan lokebc59ba32011-08-25 10:43:30 +0000556 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f102011-08-19 10:18:16 +0000557
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530558 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000559 prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
560}
561
562static int prb_calc_retire_blk_tmo(struct packet_sock *po,
563 int blk_size_in_bytes)
564{
565 struct net_device *dev;
566 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800567 struct ethtool_link_ksettings ecmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000568 int err;
chetan lokef6fb8f102011-08-19 10:18:16 +0000569
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000570 rtnl_lock();
571 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
572 if (unlikely(!dev)) {
573 rtnl_unlock();
chetan lokef6fb8f102011-08-19 10:18:16 +0000574 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000575 }
David Decotigny7cad1ba2016-02-24 10:58:10 -0800576 err = __ethtool_get_link_ksettings(dev, &ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000577 rtnl_unlock();
578 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000579 /*
580 * If the link speed is so slow you don't really
581 * need to worry about perf anyways
582 */
David Decotigny7cad1ba2016-02-24 10:58:10 -0800583 if (ecmd.base.speed < SPEED_1000 ||
584 ecmd.base.speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000585 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000586 } else {
587 msec = 1;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800588 div = ecmd.base.speed / 1000;
chetan lokef6fb8f102011-08-19 10:18:16 +0000589 }
Mao Wenan43c0e112019-12-09 21:31:25 +0800590 } else
591 return DEFAULT_PRB_RETIRE_TOV;
chetan lokef6fb8f102011-08-19 10:18:16 +0000592
593 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
594
595 if (div)
596 mbits /= div;
597
598 tmo = mbits * msec;
599
600 if (div)
601 return tmo+1;
602 return tmo;
603}
604
chetan lokebc59ba32011-08-25 10:43:30 +0000605static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f102011-08-19 10:18:16 +0000606 union tpacket_req_u *req_u)
607{
608 p1->feature_req_word = req_u->req3.tp_feature_req_word;
609}
610
611static void init_prb_bdqc(struct packet_sock *po,
612 struct packet_ring_buffer *rb,
613 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530614 union tpacket_req_u *req_u)
chetan lokef6fb8f102011-08-19 10:18:16 +0000615{
Duan Jiong22781a52013-12-06 13:29:36 +0800616 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000617 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000618
619 memset(p1, 0x0, sizeof(*p1));
620
621 p1->knxt_seq_num = 1;
622 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000623 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000624 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f102011-08-19 10:18:16 +0000625 p1->kblk_size = req_u->req3.tp_block_size;
626 p1->knum_blocks = req_u->req3.tp_block_nr;
627 p1->hdrlen = po->tp_hdrlen;
628 p1->version = po->tp_version;
629 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000630 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f102011-08-19 10:18:16 +0000631 if (req_u->req3.tp_retire_blk_tov)
632 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
633 else
634 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
635 req_u->req3.tp_block_size);
636 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
637 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
638
Eric Dumazetdc808112014-08-15 09:16:04 -0700639 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 */
678static void prb_retire_rx_blk_timer_expired(unsigned long data)
679{
680 struct packet_sock *po = (struct packet_sock *)data;
Duan Jiong22781a52013-12-06 13:29:36 +0800681 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000682 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000683 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000684
685 spin_lock(&po->sk.sk_receive_queue.lock);
686
687 frozen = prb_queue_frozen(pkc);
688 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
689
690 if (unlikely(pkc->delete_blk_timer))
691 goto out;
692
693 /* We only need to plug the race when the block is partially filled.
694 * tpacket_rcv:
695 * lock(); increment BLOCK_NUM_PKTS; unlock()
696 * copy_bits() is in progress ...
697 * timer fires on other cpu:
698 * we can't retire the current block because copy_bits
699 * is in progress.
700 *
701 */
702 if (BLOCK_NUM_PKTS(pbd)) {
703 while (atomic_read(&pkc->blk_fill_in_prog)) {
704 /* Waiting for skb_copy_bits to finish... */
705 cpu_relax();
706 }
707 }
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 lokef6fb8f102011-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 */
724 if (prb_curr_blk_in_use(pkc, pbd)) {
725 /*
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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-08-19 10:18:16 +0000803
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000804 if (po->stats.stats3.tp_drops)
chetan lokef6fb8f102011-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 */
chetan lokef6fb8f102011-08-19 10:18:16 +0000820 struct timespec ts;
821 getnstimeofday(&ts);
822 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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-08-19 10:18:16 +0000850{
851 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000852 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-08-19 10:18:16 +0000863
Daniel Borkmann8da30562013-05-03 02:57:00 +0000864 getnstimeofday(&ts);
chetan lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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)) {
965 while (atomic_read(&pkc->blk_fill_in_prog)) {
966 /* Waiting for skb_copy_bits to finish... */
967 cpu_relax();
968 }
969 }
970 prb_close_block(pkc, pbd, po, status);
971 return;
972 }
chetan lokef6fb8f102011-08-19 10:18:16 +0000973}
974
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000975static int prb_curr_blk_in_use(struct tpacket_kbdq_core *pkc,
chetan lokebc59ba32011-08-25 10:43:30 +0000976 struct tpacket_block_desc *pbd)
chetan lokef6fb8f102011-08-19 10:18:16 +0000977{
978 return TP_STATUS_USER & BLOCK_STATUS(pbd);
979}
980
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000981static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000982{
983 return pkc->reset_pending_on_curr_blk;
984}
985
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000986static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f102011-08-19 10:18:16 +0000987{
chetan lokebc59ba32011-08-25 10:43:30 +0000988 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f102011-08-19 10:18:16 +0000989 atomic_dec(&pkc->blk_fill_in_prog);
990}
991
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000992static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-08-19 10:18:16 +00001033 unsigned int len)
1034{
1035 struct tpacket3_hdr *ppd;
1036
1037 ppd = (struct tpacket3_hdr *)curr;
1038 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
1039 pkc->prev = curr;
1040 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
1041 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1042 BLOCK_NUM_PKTS(pbd) += 1;
1043 atomic_inc(&pkc->blk_fill_in_prog);
1044 prb_run_all_ft_ops(pkc, ppd);
1045}
1046
1047/* Assumes caller has the sk->rx_queue.lock */
1048static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1049 struct sk_buff *skb,
1050 int status,
1051 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 lokef6fb8f102011-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 lokef6fb8f102011-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 */
1067 if (prb_curr_blk_in_use(pkc, pbd)) {
1068 /* 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 lokef6fb8f102011-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 lokef6fb8f102011-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:
1122 return __packet_lookup_frame_in_block(po, skb, status, len);
1123 default:
1124 WARN(1, "TPACKET version not supported\n");
1125 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001126 return NULL;
chetan lokef6fb8f102011-08-19 10:18:16 +00001127 }
1128}
1129
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001130static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001131 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001132 unsigned int idx,
chetan lokef6fb8f102011-08-19 10:18:16 +00001133 int status)
1134{
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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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 lokef6fb8f102011-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
1246static bool __tpacket_has_room(struct packet_sock *po, int pow_off)
1247{
1248 int idx, len;
1249
1250 len = po->rx_ring.frame_max + 1;
1251 idx = po->rx_ring.head;
1252 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
1259static bool __tpacket_v3_has_room(struct packet_sock *po, int pow_off)
1260{
1261 int idx, len;
1262
1263 len = po->rx_ring.prb_bdqc.knum_blocks;
1264 idx = po->rx_ring.prb_bdqc.kactive_blk_num;
1265 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
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001272static int __packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001273{
1274 struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001275 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001276
Willem de Bruijn99547292015-05-12 11:56:47 -04001277 if (po->prot_hook.func != tpacket_rcv) {
1278 int avail = sk->sk_rcvbuf - atomic_read(&sk->sk_rmem_alloc)
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001279 - (skb ? skb->truesize : 0);
Willem de Bruijn99547292015-05-12 11:56:47 -04001280 if (avail > (sk->sk_rcvbuf >> ROOM_POW_OFF))
1281 return ROOM_NORMAL;
1282 else if (avail > 0)
1283 return ROOM_LOW;
1284 else
1285 return ROOM_NONE;
1286 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001287
Willem de Bruijn99547292015-05-12 11:56:47 -04001288 if (po->tp_version == TPACKET_V3) {
1289 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1290 ret = ROOM_NORMAL;
1291 else if (__tpacket_v3_has_room(po, 0))
1292 ret = ROOM_LOW;
1293 } else {
1294 if (__tpacket_has_room(po, ROOM_POW_OFF))
1295 ret = ROOM_NORMAL;
1296 else if (__tpacket_has_room(po, 0))
1297 ret = ROOM_LOW;
1298 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001299
1300 return ret;
1301}
1302
1303static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1304{
1305 int ret;
1306 bool has_room;
1307
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001308 spin_lock_bh(&po->sk.sk_receive_queue.lock);
1309 ret = __packet_rcv_has_room(po, skb);
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001310 has_room = ret == ROOM_NORMAL;
1311 if (po->pressure == has_room)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001312 po->pressure = !has_room;
1313 spin_unlock_bh(&po->sk.sk_receive_queue.lock);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001314
Willem de Bruijn99547292015-05-12 11:56:47 -04001315 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static void packet_sock_destruct(struct sock *sk)
1319{
Richard Cochraned85b562010-04-07 22:41:28 +00001320 skb_queue_purge(&sk->sk_error_queue);
1321
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001322 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
1323 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001326 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return;
1328 }
1329
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001330 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001333static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1334{
Eric Dumazet451081e2019-11-08 05:07:46 -08001335 u32 *history = po->rollover->history;
1336 u32 victim, rxhash;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001337 int i, count = 0;
1338
1339 rxhash = skb_get_hash(skb);
1340 for (i = 0; i < ROLLOVER_HLEN; i++)
Eric Dumazet451081e2019-11-08 05:07:46 -08001341 if (READ_ONCE(history[i]) == rxhash)
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001342 count++;
1343
Eric Dumazet451081e2019-11-08 05:07:46 -08001344 victim = prandom_u32() % ROLLOVER_HLEN;
1345
1346 /* Avoid dirtying the cache line if possible */
1347 if (READ_ONCE(history[victim]) != rxhash)
1348 WRITE_ONCE(history[victim], rxhash);
1349
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001350 return count > (ROLLOVER_HLEN >> 1);
1351}
1352
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001353static unsigned int fanout_demux_hash(struct packet_fanout *f,
1354 struct sk_buff *skb,
1355 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001356{
David S. Millereb70db82016-07-01 16:07:50 -04001357 return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001358}
1359
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001360static unsigned int fanout_demux_lb(struct packet_fanout *f,
1361 struct sk_buff *skb,
1362 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001363{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001364 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001365
Willem de Bruijn468479e2015-06-17 15:59:34 -04001366 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001367}
1368
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001369static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1370 struct sk_buff *skb,
1371 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001372{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001373 return smp_processor_id() % num;
1374}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001375
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001376static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1377 struct sk_buff *skb,
1378 unsigned int num)
1379{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001380 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001381}
1382
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001383static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1384 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001385 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001386 unsigned int num)
1387{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001388 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001389 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001390
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001391 po = pkt_sk(f->arr[idx]);
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001392
1393 if (try_self) {
1394 room = packet_rcv_has_room(po, skb);
1395 if (room == ROOM_NORMAL ||
1396 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1397 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001398 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001399 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001400
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001401 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001402 do {
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001403 po_next = pkt_sk(f->arr[i]);
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001404 if (po_next != po_skip && !po_next->pressure &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001405 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001406 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001407 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001408 atomic_long_inc(&po->rollover->num);
1409 if (room == ROOM_LOW)
1410 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001411 return i;
1412 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001413
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001414 if (++i == num)
1415 i = 0;
1416 } while (i != j);
1417
Willem de Bruijna9b63912015-05-12 11:56:50 -04001418 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001419 return idx;
1420}
1421
Neil Horman2d360972014-01-22 16:01:44 -05001422static unsigned int fanout_demux_qm(struct packet_fanout *f,
1423 struct sk_buff *skb,
1424 unsigned int num)
1425{
1426 return skb_get_queue_mapping(skb) % num;
1427}
1428
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001429static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1430 struct sk_buff *skb,
1431 unsigned int num)
1432{
1433 struct bpf_prog *prog;
1434 unsigned int ret = 0;
1435
1436 rcu_read_lock();
1437 prog = rcu_dereference(f->bpf_prog);
1438 if (prog)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001439 ret = bpf_prog_run_clear_cb(prog, skb) % num;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001440 rcu_read_unlock();
1441
1442 return ret;
1443}
1444
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001445static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1446{
1447 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001448}
1449
David S. Miller95ec3eb2011-07-06 01:56:38 -07001450static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1451 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001452{
1453 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001454 unsigned int num = READ_ONCE(f->num_members);
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001455 struct net *net = read_pnet(&f->net);
David S. Millerdc99f602011-07-05 01:45:05 -07001456 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001457 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001458
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001459 if (!net_eq(dev_net(dev), net) || !num) {
David S. Millerdc99f602011-07-05 01:45:05 -07001460 kfree_skb(skb);
1461 return 0;
1462 }
1463
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001464 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001465 skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001466 if (!skb)
1467 return 0;
1468 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001469 switch (f->type) {
1470 case PACKET_FANOUT_HASH:
1471 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001472 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001473 break;
1474 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001475 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001476 break;
1477 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001478 idx = fanout_demux_cpu(f, skb, num);
1479 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001480 case PACKET_FANOUT_RND:
1481 idx = fanout_demux_rnd(f, skb, num);
1482 break;
Neil Horman2d360972014-01-22 16:01:44 -05001483 case PACKET_FANOUT_QM:
1484 idx = fanout_demux_qm(f, skb, num);
1485 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001486 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001487 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001488 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001489 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001490 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001491 idx = fanout_demux_bpf(f, skb, num);
1492 break;
David S. Miller7736d332011-07-05 01:43:20 -07001493 }
1494
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001495 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1496 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001497
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001498 po = pkt_sk(f->arr[idx]);
David S. Millerdc99f602011-07-05 01:45:05 -07001499 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1500}
1501
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001502DEFINE_MUTEX(fanout_mutex);
1503EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001504static LIST_HEAD(fanout_list);
1505
1506static void __fanout_link(struct sock *sk, struct packet_sock *po)
1507{
1508 struct packet_fanout *f = po->fanout;
1509
1510 spin_lock(&f->lock);
1511 f->arr[f->num_members] = sk;
1512 smp_wmb();
1513 f->num_members++;
Anoob Soman9206efc2017-02-15 20:25:39 +00001514 if (f->num_members == 1)
1515 dev_add_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001516 spin_unlock(&f->lock);
1517}
1518
1519static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1520{
1521 struct packet_fanout *f = po->fanout;
1522 int i;
1523
1524 spin_lock(&f->lock);
1525 for (i = 0; i < f->num_members; i++) {
1526 if (f->arr[i] == sk)
1527 break;
1528 }
1529 BUG_ON(i >= f->num_members);
1530 f->arr[i] = f->arr[f->num_members - 1];
1531 f->num_members--;
Anoob Soman9206efc2017-02-15 20:25:39 +00001532 if (f->num_members == 0)
1533 __dev_remove_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001534 spin_unlock(&f->lock);
1535}
1536
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001537static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001538{
Eric Dumazet161642e2015-10-09 11:29:32 -07001539 if (sk->sk_family != PF_PACKET)
1540 return false;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001541
Eric Dumazet161642e2015-10-09 11:29:32 -07001542 return ptype->af_packet_priv == pkt_sk(sk)->fanout;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001543}
1544
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001545static void fanout_init_data(struct packet_fanout *f)
1546{
1547 switch (f->type) {
1548 case PACKET_FANOUT_LB:
1549 atomic_set(&f->rr_cur, 0);
1550 break;
1551 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001552 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001553 RCU_INIT_POINTER(f->bpf_prog, NULL);
1554 break;
1555 }
1556}
1557
1558static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1559{
1560 struct bpf_prog *old;
1561
1562 spin_lock(&f->lock);
1563 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1564 rcu_assign_pointer(f->bpf_prog, new);
1565 spin_unlock(&f->lock);
1566
1567 if (old) {
1568 synchronize_net();
1569 bpf_prog_destroy(old);
1570 }
1571}
1572
1573static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1574 unsigned int len)
1575{
1576 struct bpf_prog *new;
1577 struct sock_fprog fprog;
1578 int ret;
1579
1580 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1581 return -EPERM;
1582 if (len != sizeof(fprog))
1583 return -EINVAL;
1584 if (copy_from_user(&fprog, data, len))
1585 return -EFAULT;
1586
Daniel Borkmannbab18992015-10-02 15:17:33 +02001587 ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001588 if (ret)
1589 return ret;
1590
1591 __fanout_set_data_bpf(po->fanout, new);
1592 return 0;
1593}
1594
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001595static int fanout_set_data_ebpf(struct packet_sock *po, char __user *data,
1596 unsigned int len)
1597{
1598 struct bpf_prog *new;
1599 u32 fd;
1600
1601 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1602 return -EPERM;
1603 if (len != sizeof(fd))
1604 return -EINVAL;
1605 if (copy_from_user(&fd, data, len))
1606 return -EFAULT;
1607
Daniel Borkmann113214b2016-06-30 17:24:44 +02001608 new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001609 if (IS_ERR(new))
1610 return PTR_ERR(new);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001611
1612 __fanout_set_data_bpf(po->fanout, new);
1613 return 0;
1614}
1615
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001616static int fanout_set_data(struct packet_sock *po, char __user *data,
1617 unsigned int len)
1618{
1619 switch (po->fanout->type) {
1620 case PACKET_FANOUT_CBPF:
1621 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001622 case PACKET_FANOUT_EBPF:
1623 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001624 default:
1625 return -EINVAL;
1626 };
1627}
1628
1629static void fanout_release_data(struct packet_fanout *f)
1630{
1631 switch (f->type) {
1632 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001633 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001634 __fanout_set_data_bpf(f, NULL);
1635 };
1636}
1637
David S. Miller7736d332011-07-05 01:43:20 -07001638static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001639{
Eric Dumazet722737f2017-02-14 09:03:51 -08001640 struct packet_rollover *rollover = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001641 struct packet_sock *po = pkt_sk(sk);
1642 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001643 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001644 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001645 int err;
1646
1647 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001648 case PACKET_FANOUT_ROLLOVER:
1649 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1650 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001651 case PACKET_FANOUT_HASH:
1652 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001653 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001654 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001655 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001656 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001657 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001658 break;
1659 default:
1660 return -EINVAL;
1661 }
1662
Eric Dumazet722737f2017-02-14 09:03:51 -08001663 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001664
Eric Dumazet722737f2017-02-14 09:03:51 -08001665 err = -EALREADY;
David S. Millerdc99f602011-07-05 01:45:05 -07001666 if (po->fanout)
Eric Dumazet722737f2017-02-14 09:03:51 -08001667 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001668
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001669 if (type == PACKET_FANOUT_ROLLOVER ||
1670 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Eric Dumazet722737f2017-02-14 09:03:51 -08001671 err = -ENOMEM;
1672 rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
1673 if (!rollover)
1674 goto out;
1675 atomic_long_set(&rollover->num, 0);
1676 atomic_long_set(&rollover->num_huge, 0);
1677 atomic_long_set(&rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001678 }
1679
David S. Millerdc99f602011-07-05 01:45:05 -07001680 match = NULL;
1681 list_for_each_entry(f, &fanout_list, list) {
1682 if (f->id == id &&
1683 read_pnet(&f->net) == sock_net(sk)) {
1684 match = f;
1685 break;
1686 }
1687 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001688 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001689 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001690 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001691 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001692 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001693 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001694 if (!match)
1695 goto out;
1696 write_pnet(&match->net, sock_net(sk));
1697 match->id = id;
1698 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001699 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001700 INIT_LIST_HEAD(&match->list);
1701 spin_lock_init(&match->lock);
Reshetova, Elena35b2e8c2017-06-30 13:08:10 +03001702 refcount_set(&match->sk_ref, 0);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001703 fanout_init_data(match);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001704 match->prot_hook.type = po->prot_hook.type;
1705 match->prot_hook.dev = po->prot_hook.dev;
1706 match->prot_hook.func = packet_rcv_fanout;
1707 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001708 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001709 list_add(&match->list, &fanout_list);
1710 }
1711 err = -EINVAL;
Willem de Bruijn6f7cdd42017-09-14 17:14:41 -04001712
1713 spin_lock(&po->bind_lock);
1714 if (po->running &&
1715 match->type == type &&
Eric Dumazetafe62c62011-07-07 06:41:29 -07001716 match->prot_hook.type == po->prot_hook.type &&
1717 match->prot_hook.dev == po->prot_hook.dev) {
1718 err = -ENOSPC;
Reshetova, Elena35b2e8c2017-06-30 13:08:10 +03001719 if (refcount_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001720 __dev_remove_pack(&po->prot_hook);
1721 po->fanout = match;
Mike Maloney30c573a2017-11-28 10:44:29 -05001722 po->rollover = rollover;
1723 rollover = NULL;
Reshetova, Elena35b2e8c2017-06-30 13:08:10 +03001724 refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001725 __fanout_link(sk, po);
1726 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001727 }
1728 }
Willem de Bruijn6f7cdd42017-09-14 17:14:41 -04001729 spin_unlock(&po->bind_lock);
1730
Reshetova, Elena35b2e8c2017-06-30 13:08:10 +03001731 if (err && !refcount_read(&match->sk_ref)) {
Willem de Bruijn6f7cdd42017-09-14 17:14:41 -04001732 list_del(&match->list);
1733 kfree(match);
1734 }
1735
Eric Dumazetafe62c62011-07-07 06:41:29 -07001736out:
Mike Maloney30c573a2017-11-28 10:44:29 -05001737 kfree(rollover);
Eric Dumazet722737f2017-02-14 09:03:51 -08001738 mutex_unlock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001739 return err;
1740}
1741
Anoob Soman9206efc2017-02-15 20:25:39 +00001742/* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
1743 * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
1744 * It is the responsibility of the caller to call fanout_release_data() and
1745 * free the returned packet_fanout (after synchronize_net())
1746 */
1747static struct packet_fanout *fanout_release(struct sock *sk)
David S. Millerdc99f602011-07-05 01:45:05 -07001748{
1749 struct packet_sock *po = pkt_sk(sk);
1750 struct packet_fanout *f;
1751
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001752 mutex_lock(&fanout_mutex);
Eric Dumazet722737f2017-02-14 09:03:51 -08001753 f = po->fanout;
1754 if (f) {
1755 po->fanout = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001756
Reshetova, Elena35b2e8c2017-06-30 13:08:10 +03001757 if (refcount_dec_and_test(&f->sk_ref))
Eric Dumazet722737f2017-02-14 09:03:51 -08001758 list_del(&f->list);
Anoob Soman9206efc2017-02-15 20:25:39 +00001759 else
1760 f = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001761 }
1762 mutex_unlock(&fanout_mutex);
Anoob Soman9206efc2017-02-15 20:25:39 +00001763
1764 return f;
David S. Millerdc99f602011-07-05 01:45:05 -07001765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001767static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
1768 struct sk_buff *skb)
1769{
1770 /* Earlier code assumed this would be a VLAN pkt, double-check
1771 * this now that we have the actual packet in hand. We can only
1772 * do this check on Ethernet devices.
1773 */
1774 if (unlikely(dev->type != ARPHRD_ETHER))
1775 return false;
1776
1777 skb_reset_mac_header(skb);
1778 return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
1779}
1780
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001781static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001783static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001785static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1786 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct sock *sk;
1789 struct sockaddr_pkt *spkt;
1790
1791 /*
1792 * When we registered the protocol we saved the socket in the data
1793 * field for just this event.
1794 */
1795
1796 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /*
1799 * Yank back the headers [hope the device set this
1800 * right or kerboom...]
1801 *
1802 * Incoming packets have ll header pulled,
1803 * push it back.
1804 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001805 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * so that this procedure is noop.
1807 */
1808
1809 if (skb->pkt_type == PACKET_LOOPBACK)
1810 goto out;
1811
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001812 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001813 goto out;
1814
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001815 skb = skb_share_check(skb, GFP_ATOMIC);
1816 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto oom;
1818
1819 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001820 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Phil Oester84531c22005-07-12 11:57:52 -07001822 /* drop conntrack reference */
1823 nf_reset(skb);
1824
Herbert Xuffbc6112007-02-04 23:33:10 -08001825 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001827 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /*
1830 * The SOCK_PACKET socket receives _all_ frames.
1831 */
1832
1833 spkt->spkt_family = dev->type;
1834 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1835 spkt->spkt_protocol = skb->protocol;
1836
1837 /*
1838 * Charge the memory to the socket. This is done specifically
1839 * to prevent sockets using all the memory up.
1840 */
1841
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001842 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return 0;
1844
1845out:
1846 kfree_skb(skb);
1847oom:
1848 return 0;
1849}
1850
1851
1852/*
1853 * Output a raw packet to a device layer. This bypasses all the other
1854 * protocol layers and you must therefore supply it with a complete frame
1855 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001856
Ying Xue1b784142015-03-02 15:37:48 +08001857static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1858 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001861 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001862 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct net_device *dev;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001864 struct sockcm_cookie sockc;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001865 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001867 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001870 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
1872
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001873 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001875 return -EINVAL;
1876 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1877 proto = saddr->spkt_protocol;
1878 } else
1879 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001882 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
1884
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001885 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001886retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001887 rcu_read_lock();
1888 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 err = -ENODEV;
1890 if (dev == NULL)
1891 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001892
David S. Millerd5e76b02007-01-25 19:30:36 -08001893 err = -ENETDOWN;
1894 if (!(dev->flags & IFF_UP))
1895 goto out_unlock;
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001898 * You may not queue a frame bigger than the mtu. This is the lowest level
1899 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001901
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001902 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1903 if (!netif_supports_nofcs(dev)) {
1904 err = -EPROTONOSUPPORT;
1905 goto out_unlock;
1906 }
1907 extra_len = 4; /* We're doing our own CRC */
1908 }
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001911 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto out_unlock;
1913
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001914 if (!skb) {
1915 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001916 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001917 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001919 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001920 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001921 if (skb == NULL)
1922 return -ENOBUFS;
1923 /* FIXME: Save some space for broken drivers that write a hard
1924 * header at transmission time by themselves. PPP is the notable
1925 * one here. This should really be fixed at the driver level.
1926 */
1927 skb_reserve(skb, reserved);
1928 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001929
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001930 /* Try to align data part correctly */
1931 if (hhlen) {
1932 skb->data -= hhlen;
1933 skb->tail -= hhlen;
1934 if (len < hhlen)
1935 skb_reset_network_header(skb);
1936 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001937 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001938 if (err)
1939 goto out_free;
1940 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05001943 if (!dev_validate_header(dev, skb->data, len)) {
1944 err = -EINVAL;
1945 goto out_unlock;
1946 }
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001947 if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
1948 !packet_extra_vlan_len_allowed(dev, skb)) {
1949 err = -EMSGSIZE;
1950 goto out_unlock;
Ben Greear57f89bf2011-02-11 09:35:18 +00001951 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001952
Yoshihiro Shimodaedbe7742016-07-19 14:40:51 +09001953 sockc.tsflags = sk->sk_tsflags;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001954 if (msg->msg_controllen) {
1955 err = sock_cmsg_send(sk, msg, &sockc);
Soheil Hassas Yeganehf8e77182016-07-20 18:01:18 -04001956 if (unlikely(err))
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001957 goto out_unlock;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001958 }
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 skb->protocol = proto;
1961 skb->dev = dev;
1962 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001963 skb->mark = sk->sk_mark;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00001964
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001965 sock_tx_timestamp(sk, sockc.tsflags, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001967 if (unlikely(extra_len == 4))
1968 skb->no_fcs = 1;
1969
Jason Wang40893fd2013-03-26 23:11:22 +00001970 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001973 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001974 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001977 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001978out_free:
1979 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return err;
1981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001983static unsigned int run_filter(struct sk_buff *skb,
1984 const struct sock *sk,
1985 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
1987 struct sk_filter *filter;
1988
Eric Dumazet80f8f102011-01-18 07:46:52 +00001989 rcu_read_lock();
1990 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001991 if (filter != NULL)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001992 res = bpf_prog_run_clear_cb(filter->prog, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001993 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
David S. Millerdbcb5852007-01-24 15:21:02 -08001995 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Willem de Bruijn16cc1402016-02-03 18:02:14 -05001998static int __packet_rcv_vnet(const struct sk_buff *skb,
1999 struct virtio_net_hdr *vnet_hdr)
2000{
2001 *vnet_hdr = (const struct virtio_net_hdr) { 0 };
2002
Jason Wang1e7cbb42017-01-20 14:32:42 +08002003 if (virtio_net_hdr_from_skb(skb, vnet_hdr, vio_le(), true))
Mike Rapoport1276f242016-06-08 16:09:22 +03002004 BUG();
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002005
2006 return 0;
2007}
2008
2009static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
2010 size_t *len)
2011{
2012 struct virtio_net_hdr vnet_hdr;
2013
2014 if (*len < sizeof(vnet_hdr))
2015 return -EINVAL;
2016 *len -= sizeof(vnet_hdr);
2017
2018 if (__packet_rcv_vnet(skb, &vnet_hdr))
2019 return -EINVAL;
2020
2021 return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
2022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00002025 * This function makes lazy skb cloning in hope that most of packets
2026 * are discarded by BPF.
2027 *
2028 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
2029 * and skb->cb are mangled. It works because (and until) packets
2030 * falling here are owned by current CPU. Output packets are cloned
2031 * by dev_queue_xmit_nit(), input packets are processed by net_bh
2032 * sequencially, so that if we return skb to original state on exit,
2033 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
2035
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002036static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
2037 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 struct sock *sk;
2040 struct sockaddr_ll *sll;
2041 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002042 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002044 unsigned int snaplen, res;
Weongyo Jeongda378452016-04-14 14:10:04 -07002045 bool is_drop_n_account = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 if (skb->pkt_type == PACKET_LOOPBACK)
2048 goto drop;
2049
2050 sk = pt->af_packet_priv;
2051 po = pkt_sk(sk);
2052
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002053 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002054 goto drop;
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 skb->dev = dev;
2057
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002058 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00002060 * exported to higher levels.
2061 *
2062 * Otherwise, the device hides details of its frame
2063 * structure, so that corresponding packet head is
2064 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
2066 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002067 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 else if (skb->pkt_type == PACKET_OUTGOING) {
2069 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002070 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 }
2073
2074 snaplen = skb->len;
2075
David S. Millerdbcb5852007-01-24 15:21:02 -08002076 res = run_filter(skb, sk, snaplen);
2077 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002078 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08002079 if (snaplen > res)
2080 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002082 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 goto drop_n_acct;
2084
2085 if (skb_shared(skb)) {
2086 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2087 if (nskb == NULL)
2088 goto drop_n_acct;
2089
2090 if (skb_head != skb->data) {
2091 skb->data = skb_head;
2092 skb->len = skb_len;
2093 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00002094 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 skb = nskb;
2096 }
2097
Eyal Birgerb4772ef2015-03-01 14:58:29 +02002098 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
Herbert Xuffbc6112007-02-04 23:33:10 -08002099
2100 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 sll->sll_hatype = dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002103 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002104 sll->sll_ifindex = orig_dev->ifindex;
2105 else
2106 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002108 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Eyal Birger2472d762015-03-01 14:58:28 +02002110 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
2111 * Use their space for storing the original skb length.
2112 */
2113 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (pskb_trim(skb, snaplen))
2116 goto drop_n_acct;
2117
2118 skb_set_owner_r(skb, sk);
2119 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00002120 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Phil Oester84531c22005-07-12 11:57:52 -07002122 /* drop conntrack reference */
2123 nf_reset(skb);
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002126 po->stats.stats1.tp_packets++;
Eyal Birger3bc3b962015-03-01 14:58:30 +02002127 sock_skb_set_dropcount(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 __skb_queue_tail(&sk->sk_receive_queue, skb);
2129 spin_unlock(&sk->sk_receive_queue.lock);
David S. Miller676d2362014-04-11 16:15:36 -04002130 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return 0;
2132
2133drop_n_acct:
Weongyo Jeongda378452016-04-14 14:10:04 -07002134 is_drop_n_account = true;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002135 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002136 po->stats.stats1.tp_drops++;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00002137 atomic_inc(&sk->sk_drops);
2138 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140drop_n_restore:
2141 if (skb_head != skb->data && skb_shared(skb)) {
2142 skb->data = skb_head;
2143 skb->len = skb_len;
2144 }
2145drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002146 if (!is_drop_n_account)
2147 consume_skb(skb);
2148 else
2149 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return 0;
2151}
2152
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002153static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
2154 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 struct sock *sk;
2157 struct packet_sock *po;
2158 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00002159 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002160 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08002162 unsigned int snaplen, res;
chetan lokef6fb8f102011-08-19 10:18:16 +00002163 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002164 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002166 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002167 __u32 ts_status;
Weongyo Jeongda378452016-04-14 14:10:04 -07002168 bool is_drop_n_account = false;
Benjamin Poirier8c623e52017-08-28 14:29:41 -04002169 bool do_vnet = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Atzm Watanabe51846352013-12-17 22:53:32 +09002171 /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
2172 * We may add members to them until current aligned size without forcing
2173 * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
2174 */
2175 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
2176 BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (skb->pkt_type == PACKET_LOOPBACK)
2179 goto drop;
2180
2181 sk = pt->af_packet_priv;
2182 po = pkt_sk(sk);
2183
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002184 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002185 goto drop;
2186
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07002187 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002189 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 else if (skb->pkt_type == PACKET_OUTGOING) {
2191 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002192 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194 }
2195
2196 snaplen = skb->len;
2197
David S. Millerdbcb5852007-01-24 15:21:02 -08002198 res = run_filter(skb, sk, snaplen);
2199 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07002200 goto drop_n_restore;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002201
2202 if (skb->ip_summed == CHECKSUM_PARTIAL)
2203 status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03002204 else if (skb->pkt_type != PACKET_OUTGOING &&
2205 (skb->ip_summed == CHECKSUM_COMPLETE ||
2206 skb_csum_unnecessary(skb)))
2207 status |= TP_STATUS_CSUM_VALID;
Alexander Drozdov68c2e5d2015-03-23 09:11:12 +03002208
David S. Millerdbcb5852007-01-24 15:21:02 -08002209 if (snaplen > res)
2210 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07002213 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
2214 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00002216 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002217 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07002218 (maclen < 16 ? 16 : maclen)) +
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002219 po->tp_reserve;
Benjamin Poirier8c623e52017-08-28 14:29:41 -04002220 if (po->has_vnet_hdr) {
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002221 netoff += sizeof(struct virtio_net_hdr);
Benjamin Poirier8c623e52017-08-28 14:29:41 -04002222 do_vnet = true;
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 macoff = netoff - maclen;
2225 }
chetan lokef6fb8f102011-08-19 10:18:16 +00002226 if (po->tp_version <= TPACKET_V2) {
2227 if (macoff + snaplen > po->rx_ring.frame_size) {
2228 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00002229 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002230 if (skb_shared(skb)) {
2231 copy_skb = skb_clone(skb, GFP_ATOMIC);
2232 } else {
2233 copy_skb = skb_get(skb);
2234 skb_head = skb->data;
2235 }
2236 if (copy_skb)
2237 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
chetan lokef6fb8f102011-08-19 10:18:16 +00002239 snaplen = po->rx_ring.frame_size - macoff;
Benjamin Poirier8c623e52017-08-28 14:29:41 -04002240 if ((int)snaplen < 0) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002241 snaplen = 0;
Benjamin Poirier8c623e52017-08-28 14:29:41 -04002242 do_vnet = false;
2243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Eric Dumazetdc808112014-08-15 09:16:04 -07002245 } else if (unlikely(macoff + snaplen >
2246 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
2247 u32 nval;
2248
2249 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
2250 pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
2251 snaplen, nval, macoff);
2252 snaplen = nval;
2253 if (unlikely((int)snaplen < 0)) {
2254 snaplen = 0;
2255 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
Benjamin Poirier8c623e52017-08-28 14:29:41 -04002256 do_vnet = false;
Eric Dumazetdc808112014-08-15 09:16:04 -07002257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00002260 h.raw = packet_current_rx_frame(po, skb,
2261 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002262 if (!h.raw)
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002263 goto drop_n_account;
chetan lokef6fb8f102011-08-19 10:18:16 +00002264 if (po->tp_version <= TPACKET_V2) {
2265 packet_increment_rx_head(po, &po->rx_ring);
2266 /*
2267 * LOSING will be reported till you read the stats,
2268 * because it's COR - Clear On Read.
2269 * Anyways, moving it for V1/V2 only as V3 doesn't need this
2270 * at packet level.
2271 */
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002272 if (po->stats.stats1.tp_drops)
chetan lokef6fb8f102011-08-19 10:18:16 +00002273 status |= TP_STATUS_LOSING;
2274 }
Eric Dumazet9dc96f72018-06-21 14:16:02 -07002275
2276 if (do_vnet &&
2277 __packet_rcv_vnet(skb, h.raw + macoff -
2278 sizeof(struct virtio_net_hdr)))
2279 goto drop_n_account;
2280
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002281 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (copy_skb) {
2283 status |= TP_STATUS_COPY;
2284 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 spin_unlock(&sk->sk_receive_queue.lock);
2287
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002288 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002289
2290 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Daniel Borkmann7a513842013-04-23 00:39:29 +00002291 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002293 status |= ts_status;
2294
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002295 switch (po->tp_version) {
2296 case TPACKET_V1:
2297 h.h1->tp_len = skb->len;
2298 h.h1->tp_snaplen = snaplen;
2299 h.h1->tp_mac = macoff;
2300 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002301 h.h1->tp_sec = ts.tv_sec;
2302 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002303 hdrlen = sizeof(*h.h1);
2304 break;
2305 case TPACKET_V2:
2306 h.h2->tp_len = skb->len;
2307 h.h2->tp_snaplen = snaplen;
2308 h.h2->tp_mac = macoff;
2309 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002310 h.h2->tp_sec = ts.tv_sec;
2311 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002312 if (skb_vlan_tag_present(skb)) {
2313 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002314 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2315 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002316 } else {
2317 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002318 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002319 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002320 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002321 hdrlen = sizeof(*h.h2);
2322 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00002323 case TPACKET_V3:
2324 /* tp_nxt_offset,vlan are already populated above.
2325 * So DONT clear those fields here
2326 */
2327 h.h3->tp_status |= status;
2328 h.h3->tp_len = skb->len;
2329 h.h3->tp_snaplen = snaplen;
2330 h.h3->tp_mac = macoff;
2331 h.h3->tp_net = netoff;
chetan lokef6fb8f102011-08-19 10:18:16 +00002332 h.h3->tp_sec = ts.tv_sec;
2333 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002334 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f102011-08-19 10:18:16 +00002335 hdrlen = sizeof(*h.h3);
2336 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002337 default:
2338 BUG();
2339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002341 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002342 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 sll->sll_family = AF_PACKET;
2344 sll->sll_hatype = dev->type;
2345 sll->sll_protocol = skb->protocol;
2346 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002347 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002348 sll->sll_ifindex = orig_dev->ifindex;
2349 else
2350 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Ralf Baechlee16aa202006-12-07 00:11:33 -08002352 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002353
Changli Gaof6dafa92010-12-07 04:26:16 +00002354#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002355 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002356 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002358 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2359 macoff + snaplen);
2360
2361 for (start = h.raw; start < end; start += PAGE_SIZE)
2362 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002364 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002365#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002366
Dan Collinsda413ee2014-12-19 16:49:25 +13002367 if (po->tp_version <= TPACKET_V2) {
chetan lokef6fb8f102011-08-19 10:18:16 +00002368 __packet_set_status(po, h.raw, status);
Dan Collinsda413ee2014-12-19 16:49:25 +13002369 sk->sk_data_ready(sk);
2370 } else {
chetan lokef6fb8f102011-08-19 10:18:16 +00002371 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374drop_n_restore:
2375 if (skb_head != skb->data && skb_shared(skb)) {
2376 skb->data = skb_head;
2377 skb->len = skb_len;
2378 }
2379drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002380 if (!is_drop_n_account)
2381 consume_skb(skb);
2382 else
2383 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return 0;
2385
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002386drop_n_account:
Weongyo Jeongda378452016-04-14 14:10:04 -07002387 is_drop_n_account = true;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002388 po->stats.stats1.tp_drops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 spin_unlock(&sk->sk_receive_queue.lock);
2390
David S. Miller676d2362014-04-11 16:15:36 -04002391 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002392 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto drop_n_restore;
2394}
2395
Johann Baudy69e3c752009-05-18 22:11:22 -07002396static void tpacket_destruct_skb(struct sk_buff *skb)
2397{
2398 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002399
Johann Baudy69e3c752009-05-18 22:11:22 -07002400 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002401 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002402 __u32 ts;
2403
Johann Baudy69e3c752009-05-18 22:11:22 -07002404 ph = skb_shinfo(skb)->destructor_arg;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002405 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002406
2407 ts = __packet_set_timestamp(po, ph, skb);
2408 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Neil Horman0c76cea2019-06-25 17:57:49 -04002409
2410 if (!packet_read_pending(&po->tx_ring))
2411 complete(&po->skb_completion);
Johann Baudy69e3c752009-05-18 22:11:22 -07002412 }
2413
2414 sock_wfree(skb);
2415}
2416
Daniel Borkmannc72219b2015-11-11 23:25:43 +01002417static void tpacket_set_protocol(const struct net_device *dev,
2418 struct sk_buff *skb)
2419{
2420 if (dev->type == ARPHRD_ETHER) {
2421 skb_reset_mac_header(skb);
2422 skb->protocol = eth_hdr(skb)->h_proto;
2423 }
2424}
2425
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002426static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
2427{
2428 unsigned short gso_type = 0;
2429
2430 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2431 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2432 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
2433 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
2434 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
2435 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2436 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
2437
2438 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
2439 return -EINVAL;
2440
2441 if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2442 switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2443 case VIRTIO_NET_HDR_GSO_TCPV4:
2444 gso_type = SKB_GSO_TCPV4;
2445 break;
2446 case VIRTIO_NET_HDR_GSO_TCPV6:
2447 gso_type = SKB_GSO_TCPV6;
2448 break;
2449 case VIRTIO_NET_HDR_GSO_UDP:
2450 gso_type = SKB_GSO_UDP;
2451 break;
2452 default:
2453 return -EINVAL;
2454 }
2455
2456 if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
2457 gso_type |= SKB_GSO_TCP_ECN;
2458
2459 if (vnet_hdr->gso_size == 0)
2460 return -EINVAL;
2461 }
2462
2463 vnet_hdr->gso_type = gso_type; /* changes type, temporary storage */
2464 return 0;
2465}
2466
2467static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
2468 struct virtio_net_hdr *vnet_hdr)
2469{
2470 int n;
2471
2472 if (*len < sizeof(*vnet_hdr))
2473 return -EINVAL;
2474 *len -= sizeof(*vnet_hdr);
2475
2476 n = copy_from_iter(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter);
2477 if (n != sizeof(*vnet_hdr))
2478 return -EFAULT;
2479
2480 return __packet_snd_vnet_parse(vnet_hdr, *len);
2481}
2482
2483static int packet_snd_vnet_gso(struct sk_buff *skb,
2484 struct virtio_net_hdr *vnet_hdr)
2485{
2486 if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2487 u16 s = __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start);
2488 u16 o = __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset);
2489
2490 if (!skb_partial_csum_set(skb, s, o))
2491 return -EINVAL;
2492 }
2493
2494 skb_shinfo(skb)->gso_size =
2495 __virtio16_to_cpu(vio_le(), vnet_hdr->gso_size);
2496 skb_shinfo(skb)->gso_type = vnet_hdr->gso_type;
2497
2498 /* Header must be checked, and gso_segs computed. */
2499 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2500 skb_shinfo(skb)->gso_segs = 0;
2501 return 0;
2502}
2503
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002504static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002505 void *frame, struct net_device *dev, void *data, int tp_len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002506 __be16 proto, unsigned char *addr, int hlen, int copylen,
2507 const struct sockcm_cookie *sockc)
Johann Baudy69e3c752009-05-18 22:11:22 -07002508{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002509 union tpacket_uhdr ph;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002510 int to_write, offset, len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002511 struct socket *sock = po->sk.sk_socket;
2512 struct page *page;
Johann Baudy69e3c752009-05-18 22:11:22 -07002513 int err;
2514
2515 ph.raw = frame;
2516
2517 skb->protocol = proto;
2518 skb->dev = dev;
2519 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002520 skb->mark = po->sk.sk_mark;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002521 sock_tx_timestamp(&po->sk, sockc->tsflags, &skb_shinfo(skb)->tx_flags);
Johann Baudy69e3c752009-05-18 22:11:22 -07002522 skb_shinfo(skb)->destructor_arg = ph.raw;
2523
Herbert Xuae641942011-11-18 02:20:04 +00002524 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002525 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002526
Johann Baudy69e3c752009-05-18 22:11:22 -07002527 to_write = tp_len;
2528
2529 if (sock->type == SOCK_DGRAM) {
2530 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2531 NULL, tp_len);
2532 if (unlikely(err < 0))
2533 return -EINVAL;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002534 } else if (copylen) {
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002535 int hdrlen = min_t(int, copylen, tp_len);
2536
Johann Baudy69e3c752009-05-18 22:11:22 -07002537 skb_push(skb, dev->hard_header_len);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002538 skb_put(skb, copylen - dev->hard_header_len);
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002539 err = skb_store_bits(skb, 0, data, hdrlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002540 if (unlikely(err))
2541 return err;
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002542 if (!dev_validate_header(dev, skb->data, hdrlen))
2543 return -EINVAL;
Daniel Borkmannc72219b2015-11-11 23:25:43 +01002544 if (!skb->protocol)
2545 tpacket_set_protocol(dev, skb);
Johann Baudy69e3c752009-05-18 22:11:22 -07002546
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002547 data += hdrlen;
2548 to_write -= hdrlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002549 }
2550
Johann Baudy69e3c752009-05-18 22:11:22 -07002551 offset = offset_in_page(data);
2552 len_max = PAGE_SIZE - offset;
2553 len = ((to_write > len_max) ? len_max : to_write);
2554
2555 skb->data_len = to_write;
2556 skb->len += to_write;
2557 skb->truesize += to_write;
2558 atomic_add(to_write, &po->sk.sk_wmem_alloc);
2559
2560 while (likely(to_write)) {
2561 nr_frags = skb_shinfo(skb)->nr_frags;
2562
2563 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002564 pr_err("Packet exceed the number of skb frags(%lu)\n",
2565 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002566 return -EFAULT;
2567 }
2568
Changli Gao0af55bb2010-12-01 02:52:20 +00002569 page = pgv_to_page(data);
2570 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002571 flush_dcache_page(page);
2572 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002573 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002574 to_write -= len;
2575 offset = 0;
2576 len_max = PAGE_SIZE;
2577 len = ((to_write > len_max) ? len_max : to_write);
2578 }
2579
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01002580 skb_probe_transport_header(skb, 0);
Daniel Borkmannefdfa2f2015-11-11 23:25:40 +01002581
Johann Baudy69e3c752009-05-18 22:11:22 -07002582 return tp_len;
2583}
2584
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002585static int tpacket_parse_header(struct packet_sock *po, void *frame,
2586 int size_max, void **data)
2587{
2588 union tpacket_uhdr ph;
2589 int tp_len, off;
2590
2591 ph.raw = frame;
2592
2593 switch (po->tp_version) {
2594 case TPACKET_V2:
2595 tp_len = ph.h2->tp_len;
2596 break;
2597 default:
2598 tp_len = ph.h1->tp_len;
2599 break;
2600 }
2601 if (unlikely(tp_len > size_max)) {
2602 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2603 return -EMSGSIZE;
2604 }
2605
2606 if (unlikely(po->tp_tx_has_off)) {
2607 int off_min, off_max;
2608
2609 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2610 off_max = po->tx_ring.frame_size - tp_len;
2611 if (po->sk.sk_type == SOCK_DGRAM) {
2612 switch (po->tp_version) {
2613 case TPACKET_V2:
2614 off = ph.h2->tp_net;
2615 break;
2616 default:
2617 off = ph.h1->tp_net;
2618 break;
2619 }
2620 } else {
2621 switch (po->tp_version) {
2622 case TPACKET_V2:
2623 off = ph.h2->tp_mac;
2624 break;
2625 default:
2626 off = ph.h1->tp_mac;
2627 break;
2628 }
2629 }
2630 if (unlikely((off < off_min) || (off_max < off)))
2631 return -EINVAL;
2632 } else {
2633 off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2634 }
2635
2636 *data = frame + off;
2637 return tp_len;
2638}
2639
Johann Baudy69e3c752009-05-18 22:11:22 -07002640static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2641{
Neil Horman0c76cea2019-06-25 17:57:49 -04002642 struct sk_buff *skb = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002643 struct net_device *dev;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002644 struct virtio_net_hdr *vnet_hdr = NULL;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002645 struct sockcm_cookie sockc;
Johann Baudy69e3c752009-05-18 22:11:22 -07002646 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002647 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002648 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002649 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002650 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Willem de Bruijn71061f12019-04-29 11:53:18 -04002651 unsigned char *addr = NULL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002652 int tp_len, size_max;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002653 void *data;
Johann Baudy69e3c752009-05-18 22:11:22 -07002654 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002655 int status = TP_STATUS_AVAILABLE;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002656 int hlen, tlen, copylen = 0;
Neil Horman0c76cea2019-06-25 17:57:49 -04002657 long timeo = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002658
Johann Baudy69e3c752009-05-18 22:11:22 -07002659 mutex_lock(&po->pg_vec_lock);
2660
Eric Dumazet7876df052019-08-14 02:11:57 -07002661 /* packet_sendmsg() check on tx_ring.pg_vec was lockless,
2662 * we need to confirm it under protection of pg_vec_lock.
2663 */
2664 if (unlikely(!po->tx_ring.pg_vec)) {
2665 err = -EBUSY;
2666 goto out;
2667 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002668 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002669 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002670 proto = po->num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002671 } else {
2672 err = -EINVAL;
2673 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2674 goto out;
2675 if (msg->msg_namelen < (saddr->sll_halen
2676 + offsetof(struct sockaddr_ll,
2677 sll_addr)))
2678 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002679 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002680 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Willem de Bruijn71061f12019-04-29 11:53:18 -04002681 if (po->sk.sk_socket->type == SOCK_DGRAM) {
2682 if (dev && msg->msg_namelen < dev->addr_len +
2683 offsetof(struct sockaddr_ll, sll_addr))
2684 goto out_put;
2685 addr = saddr->sll_addr;
2686 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002687 }
2688
Johann Baudy69e3c752009-05-18 22:11:22 -07002689 err = -ENXIO;
2690 if (unlikely(dev == NULL))
2691 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002692 err = -ENETDOWN;
2693 if (unlikely(!(dev->flags & IFF_UP)))
2694 goto out_put;
2695
Douglas Caetano dos Santosf79d3302017-05-12 15:19:15 -03002696 sockc.tsflags = po->sk.sk_tsflags;
2697 if (msg->msg_controllen) {
2698 err = sock_cmsg_send(&po->sk, msg, &sockc);
2699 if (unlikely(err))
2700 goto out_put;
2701 }
2702
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002703 if (po->sk.sk_socket->type == SOCK_RAW)
2704 reserve = dev->hard_header_len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002705 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002706 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002707
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002708 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002709 size_max = dev->mtu + reserve + VLAN_HLEN;
David S. Miller09effa62013-08-07 17:11:00 -07002710
Neil Horman0c76cea2019-06-25 17:57:49 -04002711 reinit_completion(&po->skb_completion);
2712
Johann Baudy69e3c752009-05-18 22:11:22 -07002713 do {
2714 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002715 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002716 if (unlikely(ph == NULL)) {
Neil Horman0c76cea2019-06-25 17:57:49 -04002717 if (need_wait && skb) {
2718 timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
2719 timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
2720 if (timeo <= 0) {
2721 err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
2722 goto out_put;
2723 }
2724 }
2725 /* check for additional frames */
Johann Baudy69e3c752009-05-18 22:11:22 -07002726 continue;
2727 }
2728
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002729 skb = NULL;
2730 tp_len = tpacket_parse_header(po, ph, size_max, &data);
2731 if (tp_len < 0)
2732 goto tpacket_error;
2733
Johann Baudy69e3c752009-05-18 22:11:22 -07002734 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002735 hlen = LL_RESERVED_SPACE(dev);
2736 tlen = dev->needed_tailroom;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002737 if (po->has_vnet_hdr) {
2738 vnet_hdr = data;
2739 data += sizeof(*vnet_hdr);
2740 tp_len -= sizeof(*vnet_hdr);
2741 if (tp_len < 0 ||
2742 __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
2743 tp_len = -EINVAL;
2744 goto tpacket_error;
2745 }
2746 copylen = __virtio16_to_cpu(vio_le(),
2747 vnet_hdr->hdr_len);
2748 }
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002749 copylen = max_t(int, copylen, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002750 skb = sock_alloc_send_skb(&po->sk,
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002751 hlen + tlen + sizeof(struct sockaddr_ll) +
2752 (copylen - dev->hard_header_len),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002753 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002754
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002755 if (unlikely(skb == NULL)) {
2756 /* we assume the socket was initially writeable ... */
2757 if (likely(len_sum > 0))
2758 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002759 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002760 }
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002761 tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002762 addr, hlen, copylen, &sockc);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002763 if (likely(tp_len >= 0) &&
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002764 tp_len > dev->mtu + reserve &&
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002765 !po->has_vnet_hdr &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002766 !packet_extra_vlan_len_allowed(dev, skb))
2767 tp_len = -EMSGSIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002768
2769 if (unlikely(tp_len < 0)) {
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002770tpacket_error:
Johann Baudy69e3c752009-05-18 22:11:22 -07002771 if (po->tp_loss) {
2772 __packet_set_status(po, ph,
2773 TP_STATUS_AVAILABLE);
2774 packet_increment_head(&po->tx_ring);
2775 kfree_skb(skb);
2776 continue;
2777 } else {
2778 status = TP_STATUS_WRONG_FORMAT;
2779 err = tp_len;
2780 goto out_status;
2781 }
2782 }
2783
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002784 if (po->has_vnet_hdr && packet_snd_vnet_gso(skb, vnet_hdr)) {
2785 tp_len = -EINVAL;
2786 goto tpacket_error;
2787 }
2788
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002789 packet_pick_tx_queue(dev, skb);
2790
Johann Baudy69e3c752009-05-18 22:11:22 -07002791 skb->destructor = tpacket_destruct_skb;
2792 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002793 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002794
2795 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002796 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002797 if (unlikely(err > 0)) {
2798 err = net_xmit_errno(err);
2799 if (err && __packet_get_status(po, ph) ==
2800 TP_STATUS_AVAILABLE) {
2801 /* skb was destructed already */
2802 skb = NULL;
2803 goto out_status;
2804 }
2805 /*
2806 * skb was dropped but not destructed yet;
2807 * let's treat it like congestion or err < 0
2808 */
2809 err = 0;
2810 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002811 packet_increment_head(&po->tx_ring);
2812 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002813 } while (likely((ph != NULL) ||
2814 /* Note: packet_read_pending() might be slow if we have
2815 * to call it as it's per_cpu variable, but in fast-path
2816 * we already short-circuit the loop with the first
2817 * condition, and luckily don't have to go that path
2818 * anyway.
2819 */
2820 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002821
2822 err = len_sum;
2823 goto out_put;
2824
Johann Baudy69e3c752009-05-18 22:11:22 -07002825out_status:
2826 __packet_set_status(po, ph, status);
2827 kfree_skb(skb);
2828out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002829 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002830out:
2831 mutex_unlock(&po->pg_vec_lock);
2832 return err;
2833}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002835static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2836 size_t reserve, size_t len,
2837 size_t linear, int noblock,
2838 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002839{
2840 struct sk_buff *skb;
2841
2842 /* Under a page? Don't bother with paged skb. */
2843 if (prepad + len < PAGE_SIZE || !linear)
2844 linear = len;
2845
2846 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002847 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002848 if (!skb)
2849 return NULL;
2850
2851 skb_reserve(skb, reserve);
2852 skb_put(skb, linear);
2853 skb->data_len = len - linear;
2854 skb->len += len - linear;
2855
2856 return skb;
2857}
2858
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002859static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002862 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 struct sk_buff *skb;
2864 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002865 __be16 proto;
Willem de Bruijn71061f12019-04-29 11:53:18 -04002866 unsigned char *addr = NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002867 int err, reserve = 0;
Edward Jeec7d39e32015-10-08 14:56:49 -07002868 struct sockcm_cookie sockc;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002869 struct virtio_net_hdr vnet_hdr = { 0 };
2870 int offset = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002871 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijn24ee3942017-09-26 12:20:17 -04002872 bool has_vnet_hdr = false;
Willem de Bruijn82849542017-02-07 15:57:21 -05002873 int hlen, tlen, linear;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002874 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002877 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002879
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002880 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002881 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 proto = po->num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 } else {
2884 err = -EINVAL;
2885 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2886 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002887 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 proto = saddr->sll_protocol;
Ben Greear827d9782011-06-01 07:18:53 +00002890 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Willem de Bruijn71061f12019-04-29 11:53:18 -04002891 if (sock->type == SOCK_DGRAM) {
2892 if (dev && msg->msg_namelen < dev->addr_len +
2893 offsetof(struct sockaddr_ll, sll_addr))
2894 goto out_unlock;
2895 addr = saddr->sll_addr;
2896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002900 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002902 err = -ENETDOWN;
2903 if (unlikely(!(dev->flags & IFF_UP)))
2904 goto out_unlock;
2905
Yoshihiro Shimodaedbe7742016-07-19 14:40:51 +09002906 sockc.tsflags = sk->sk_tsflags;
Edward Jeec7d39e32015-10-08 14:56:49 -07002907 sockc.mark = sk->sk_mark;
2908 if (msg->msg_controllen) {
2909 err = sock_cmsg_send(sk, msg, &sockc);
2910 if (unlikely(err))
2911 goto out_unlock;
2912 }
2913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (sock->type == SOCK_RAW)
2915 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002916 if (po->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002917 err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
2918 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002919 goto out_unlock;
Willem de Bruijn24ee3942017-09-26 12:20:17 -04002920 has_vnet_hdr = true;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002921 }
2922
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002923 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2924 if (!netif_supports_nofcs(dev)) {
2925 err = -EPROTONOSUPPORT;
2926 goto out_unlock;
2927 }
2928 extra_len = 4; /* We're doing our own CRC */
2929 }
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 err = -EMSGSIZE;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002932 if (!vnet_hdr.gso_type &&
2933 (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 goto out_unlock;
2935
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002936 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002937 hlen = LL_RESERVED_SPACE(dev);
2938 tlen = dev->needed_tailroom;
Willem de Bruijn82849542017-02-07 15:57:21 -05002939 linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
2940 linear = max(linear, min_t(int, len, dev->hard_header_len));
2941 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002942 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002943 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 goto out_unlock;
2945
Willem de Bruijn6190cce2018-05-11 13:24:25 -04002946 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002948 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002949 if (sock->type == SOCK_DGRAM) {
2950 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002951 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002952 goto out_free;
Willem de Bruijn6190cce2018-05-11 13:24:25 -04002953 } else if (reserve) {
Willem de Bruijn1118c602018-05-24 18:10:30 -04002954 skb_reserve(skb, -reserve);
Willem de Bruijn4770fdc2018-07-11 12:00:45 -04002955 if (len < reserve)
2956 skb_reset_network_header(skb);
Willem de Bruijn9c707762014-11-19 13:10:16 -05002957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002960 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (err)
2962 goto out_free;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00002963
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002964 if (sock->type == SOCK_RAW &&
2965 !dev_validate_header(dev, skb->data, len)) {
2966 err = -EINVAL;
2967 goto out_free;
2968 }
2969
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002970 sock_tx_timestamp(sk, sockc.tsflags, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002972 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002973 !packet_extra_vlan_len_allowed(dev, skb)) {
2974 err = -EMSGSIZE;
2975 goto out_free;
Ben Greear57f89bf2011-02-11 09:35:18 +00002976 }
2977
David S. Miller09effa62013-08-07 17:11:00 -07002978 skb->protocol = proto;
2979 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 skb->priority = sk->sk_priority;
Edward Jeec7d39e32015-10-08 14:56:49 -07002981 skb->mark = sockc.mark;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002982
2983 packet_pick_tx_queue(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Willem de Bruijn24ee3942017-09-26 12:20:17 -04002985 if (has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002986 err = packet_snd_vnet_gso(skb, &vnet_hdr);
2987 if (err)
2988 goto out_free;
2989 len += sizeof(vnet_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002990 }
2991
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01002992 skb_probe_transport_header(skb, reserve);
2993
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002994 if (unlikely(extra_len == 4))
2995 skb->no_fcs = 1;
2996
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002997 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2999 goto out_unlock;
3000
Daniel Borkmanne40526c2013-11-21 16:50:58 +01003001 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003003 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005out_free:
3006 kfree_skb(skb);
3007out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01003008 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 dev_put(dev);
3010out:
3011 return err;
3012}
3013
Ying Xue1b784142015-03-02 15:37:48 +08003014static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07003015{
Johann Baudy69e3c752009-05-18 22:11:22 -07003016 struct sock *sk = sock->sk;
3017 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003018
Johann Baudy69e3c752009-05-18 22:11:22 -07003019 if (po->tx_ring.pg_vec)
3020 return tpacket_snd(po, msg);
3021 else
Johann Baudy69e3c752009-05-18 22:11:22 -07003022 return packet_snd(sock, msg, len);
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025/*
3026 * Close a PACKET socket. This is fairly simple. We immediately go
3027 * to 'closed' state and remove our protocol entry in the device list.
3028 */
3029
3030static int packet_release(struct socket *sock)
3031{
3032 struct sock *sk = sock->sk;
3033 struct packet_sock *po;
Anoob Soman9206efc2017-02-15 20:25:39 +00003034 struct packet_fanout *f;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003035 struct net *net;
chetan lokef6fb8f102011-08-19 10:18:16 +00003036 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 if (!sk)
3039 return 0;
3040
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003041 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 po = pkt_sk(sk);
3043
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003044 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00003045 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003046 mutex_unlock(&net->packet.sklist_lock);
3047
3048 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08003049 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003050 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
stephen hemminger808f5112010-02-22 07:57:18 +00003052 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003053 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003054 packet_cached_dev_reset(po);
3055
Ben Greear160ff182011-06-01 07:18:52 +00003056 if (po->prot_hook.dev) {
3057 dev_put(po->prot_hook.dev);
3058 po->prot_hook.dev = NULL;
3059 }
stephen hemminger808f5112010-02-22 07:57:18 +00003060 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Eric Dumazet55ca7b12018-04-15 17:52:04 -07003064 lock_sock(sk);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003065 if (po->rx_ring.pg_vec) {
3066 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00003067 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003068 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003069
Phil Sutter9665d5d2013-02-01 07:21:41 +00003070 if (po->tx_ring.pg_vec) {
3071 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00003072 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003073 }
Eric Dumazet55ca7b12018-04-15 17:52:04 -07003074 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Anoob Soman9206efc2017-02-15 20:25:39 +00003076 f = fanout_release(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07003077
stephen hemminger808f5112010-02-22 07:57:18 +00003078 synchronize_net();
Anoob Soman9206efc2017-02-15 20:25:39 +00003079
3080 if (f) {
Mike Maloney30c573a2017-11-28 10:44:29 -05003081 kfree(po->rollover);
Anoob Soman9206efc2017-02-15 20:25:39 +00003082 fanout_release_data(f);
3083 kfree(f);
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 /*
3086 * Now the socket is dead. No more input will appear.
3087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 sock_orphan(sk);
3089 sock->sk = NULL;
3090
3091 /* Purge queues */
3092
3093 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01003094 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003095 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
3097 sock_put(sk);
3098 return 0;
3099}
3100
3101/*
3102 * Attach a packet hook.
3103 */
3104
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003105static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
3106 __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003109 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003110 __be16 proto_curr;
3111 bool need_rehook;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003112 struct net_device *dev = NULL;
3113 int ret = 0;
3114 bool unlisted = false;
David S. Millerdc99f602011-07-05 01:45:05 -07003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 spin_lock(&po->bind_lock);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003118 rcu_read_lock();
3119
Willem de Bruijn0f221672017-09-26 12:19:37 -04003120 if (po->fanout) {
3121 ret = -EINVAL;
3122 goto out_unlock;
3123 }
3124
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003125 if (name) {
3126 dev = dev_get_by_name_rcu(sock_net(sk), name);
3127 if (!dev) {
3128 ret = -ENODEV;
3129 goto out_unlock;
3130 }
3131 } else if (ifindex) {
3132 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
3133 if (!dev) {
3134 ret = -ENODEV;
3135 goto out_unlock;
3136 }
3137 }
3138
3139 if (dev)
3140 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003141
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003142 proto_curr = po->prot_hook.type;
3143 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003145 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003147 if (need_rehook) {
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003148 if (po->running) {
3149 rcu_read_unlock();
Eric Dumazet5471afe2017-11-28 08:03:30 -08003150 /* prevents packet_notifier() from calling
3151 * register_prot_hook()
3152 */
3153 po->num = 0;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003154 __unregister_prot_hook(sk, true);
3155 rcu_read_lock();
3156 dev_curr = po->prot_hook.dev;
3157 if (dev)
3158 unlisted = !dev_get_by_index_rcu(sock_net(sk),
3159 dev->ifindex);
3160 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003161
Eric Dumazet5471afe2017-11-28 08:03:30 -08003162 BUG_ON(po->running);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003163 po->num = proto;
3164 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003165
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003166 if (unlikely(unlisted)) {
3167 dev_put(dev);
3168 po->prot_hook.dev = NULL;
3169 po->ifindex = -1;
3170 packet_cached_dev_reset(po);
3171 } else {
3172 po->prot_hook.dev = dev;
3173 po->ifindex = dev ? dev->ifindex : 0;
3174 packet_cached_dev_assign(po, dev);
3175 }
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003176 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003177 if (dev_curr)
3178 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003179
3180 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 goto out_unlock;
3182
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003183 if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
David S. Millerce06b032011-07-04 01:44:29 -07003184 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08003185 } else {
3186 sk->sk_err = ENETDOWN;
3187 if (!sock_flag(sk, SOCK_DEAD))
3188 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190
3191out_unlock:
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003192 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 spin_unlock(&po->bind_lock);
3194 release_sock(sk);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
3198/*
3199 * Bind a packet socket to a device
3200 */
3201
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003202static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
3203 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003205 struct sock *sk = sock->sk;
Alexander Potapenko62fe0522017-03-01 12:57:20 +01003206 char name[sizeof(uaddr->sa_data) + 1];
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 /*
3209 * Check legality
3210 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003211
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003212 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return -EINVAL;
Alexander Potapenko62fe0522017-03-01 12:57:20 +01003214 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
3215 * zero-terminated.
3216 */
3217 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
3218 name[sizeof(uaddr->sa_data)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003220 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3224{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003225 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
3226 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 /*
3229 * Check legality
3230 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (addr_len < sizeof(struct sockaddr_ll))
3233 return -EINVAL;
3234 if (sll->sll_family != AF_PACKET)
3235 return -EINVAL;
3236
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003237 return packet_do_bind(sk, NULL, sll->sll_ifindex,
3238 sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
3241static struct proto packet_proto = {
3242 .name = "PACKET",
3243 .owner = THIS_MODULE,
3244 .obj_size = sizeof(struct packet_sock),
3245};
3246
3247/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003248 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 */
3250
Eric Paris3f378b62009-11-05 22:18:14 -08003251static int packet_create(struct net *net, struct socket *sock, int protocol,
3252 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
3254 struct sock *sk;
3255 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003256 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 int err;
3258
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003259 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003261 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3262 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return -ESOCKTNOSUPPORT;
3264
3265 sock->state = SS_UNCONNECTED;
3266
3267 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003268 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 if (sk == NULL)
3270 goto out;
3271
3272 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (sock->type == SOCK_PACKET)
3274 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 sock_init_data(sock, sk);
3277
3278 po = pkt_sk(sk);
Neil Horman0c76cea2019-06-25 17:57:49 -04003279 init_completion(&po->skb_completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003281 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003282 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003283
Daniel Borkmannb0138402014-01-15 16:25:36 +01003284 err = packet_alloc_pending(po);
3285 if (err)
3286 goto out2;
3287
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003288 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003291 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 /*
3294 * Attach a protocol block
3295 */
3296
3297 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003298 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003299 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (sock->type == SOCK_PACKET)
3303 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 po->prot_hook.af_packet_priv = sk;
3306
Al Viro0e11c912006-11-08 00:26:29 -08003307 if (proto) {
3308 po->prot_hook.type = proto;
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003309 __register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
3311
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003312 mutex_lock(&net->packet.sklist_lock);
Maxime Chevallier936a9182019-03-16 14:41:30 +01003313 sk_add_node_tail_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003314 mutex_unlock(&net->packet.sklist_lock);
3315
3316 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003317 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003318 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003319
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003320 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003321out2:
3322 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323out:
3324 return err;
3325}
3326
3327/*
3328 * Pull a packet from our receive queue and hand it to the user.
3329 * If necessary we block.
3330 */
3331
Ying Xue1b784142015-03-02 15:37:48 +08003332static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3333 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 struct sock *sk = sock->sk;
3336 struct sk_buff *skb;
3337 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003338 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003339 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003342 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 goto out;
3344
3345#if 0
3346 /* What error should we return now? EUNATTACH? */
3347 if (pkt_sk(sk)->ifindex < 0)
3348 return -ENODEV;
3349#endif
3350
Richard Cochraned85b562010-04-07 22:41:28 +00003351 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003352 err = sock_recv_errqueue(sk, msg, len,
3353 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003354 goto out;
3355 }
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 * Call the generic datagram receiver. This handles all sorts
3359 * of horrible races and re-entrancy so we can forget about it
3360 * in the protocol layers.
3361 *
3362 * Now it will return ENETDOWN, if device have just gone down,
3363 * but then it will block.
3364 */
3365
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003366 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
3368 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003369 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 * handles the blocking we don't see and worry about blocking
3371 * retries.
3372 */
3373
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003374 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 goto out;
3376
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003377 if (pkt_sk(sk)->pressure)
3378 packet_rcv_has_room(pkt_sk(sk), NULL);
3379
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003380 if (pkt_sk(sk)->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003381 err = packet_rcv_vnet(msg, skb, &len);
3382 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003383 goto out_free;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003384 vnet_hdr_len = sizeof(struct virtio_net_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003385 }
3386
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003387 /* You lose any data beyond the buffer you gave. If it worries
3388 * a user program they can ask the device for its MTU
3389 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003392 if (copied > len) {
3393 copied = len;
3394 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
David S. Miller51f3d022014-11-05 16:46:40 -05003397 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 if (err)
3399 goto out_free;
3400
Eyal Birger2472d762015-03-01 14:58:28 +02003401 if (sock->type != SOCK_PACKET) {
3402 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3403
3404 /* Original length was stored in sockaddr_ll fields */
3405 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3406 sll->sll_family = AF_PACKET;
3407 sll->sll_protocol = skb->protocol;
3408 }
3409
Neil Horman3b885782009-10-12 13:26:31 -07003410 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003412 if (msg->msg_name) {
Willem de Bruijn0c169852019-04-29 11:46:55 -04003413 int copy_len;
3414
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003415 /* If the address length field is there to be filled
3416 * in, we fill it in now.
3417 */
3418 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003419 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003420 msg->msg_namelen = sizeof(struct sockaddr_pkt);
Willem de Bruijn0c169852019-04-29 11:46:55 -04003421 copy_len = msg->msg_namelen;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003422 } else {
3423 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003424
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003425 msg->msg_namelen = sll->sll_halen +
3426 offsetof(struct sockaddr_ll, sll_addr);
Willem de Bruijn0c169852019-04-29 11:46:55 -04003427 copy_len = msg->msg_namelen;
3428 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
3429 memset(msg->msg_name +
3430 offsetof(struct sockaddr_ll, sll_addr),
3431 0, sizeof(sll->sll_addr));
3432 msg->msg_namelen = sizeof(struct sockaddr_ll);
3433 }
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003434 }
Willem de Bruijn0c169852019-04-29 11:46:55 -04003435 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Herbert Xu8dc41942007-02-04 23:31:32 -08003438 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003439 struct tpacket_auxdata aux;
3440
3441 aux.tp_status = TP_STATUS_USER;
3442 if (skb->ip_summed == CHECKSUM_PARTIAL)
3443 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003444 else if (skb->pkt_type != PACKET_OUTGOING &&
3445 (skb->ip_summed == CHECKSUM_COMPLETE ||
3446 skb_csum_unnecessary(skb)))
3447 aux.tp_status |= TP_STATUS_CSUM_VALID;
3448
Eyal Birger2472d762015-03-01 14:58:28 +02003449 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003450 aux.tp_snaplen = skb->len;
3451 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003452 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003453 if (skb_vlan_tag_present(skb)) {
3454 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003455 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3456 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003457 } else {
3458 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003459 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003460 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003461 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003462 }
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 /*
3465 * Free or return the buffer as appropriate. Again this
3466 * hides all the races and re-entrancy issues from us.
3467 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003468 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470out_free:
3471 skb_free_datagram(sk, skb);
3472out:
3473 return err;
3474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
3477 int *uaddr_len, int peer)
3478{
3479 struct net_device *dev;
3480 struct sock *sk = sock->sk;
3481
3482 if (peer)
3483 return -EOPNOTSUPP;
3484
3485 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003486 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003487 rcu_read_lock();
3488 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3489 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003490 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003491 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 *uaddr_len = sizeof(*uaddr);
3493
3494 return 0;
3495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
3498 int *uaddr_len, int peer)
3499{
3500 struct net_device *dev;
3501 struct sock *sk = sock->sk;
3502 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003503 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 if (peer)
3506 return -EOPNOTSUPP;
3507
3508 sll->sll_family = AF_PACKET;
3509 sll->sll_ifindex = po->ifindex;
3510 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003511 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003512 rcu_read_lock();
3513 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (dev) {
3515 sll->sll_hatype = dev->type;
3516 sll->sll_halen = dev->addr_len;
3517 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 } else {
3519 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3520 sll->sll_halen = 0;
3521 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003522 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003523 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 return 0;
3526}
3527
Wang Chen2aeb0b82008-07-14 20:49:46 -07003528static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3529 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
3531 switch (i->type) {
3532 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003533 if (i->alen != dev->addr_len)
3534 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003536 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003538 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 break;
3540 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003541 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003543 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003544 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003545 if (i->alen != dev->addr_len)
3546 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003547 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003548 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003549 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003550 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003551 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003552 default:
3553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003558static void packet_dev_mclist_delete(struct net_device *dev,
3559 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003561 struct packet_mclist *ml;
3562
3563 while ((ml = *mlp) != NULL) {
3564 if (ml->ifindex == dev->ifindex) {
3565 packet_dev_mc(dev, ml, -1);
3566 *mlp = ml->next;
3567 kfree(ml);
3568 } else
3569 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571}
3572
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003573static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
3575 struct packet_sock *po = pkt_sk(sk);
3576 struct packet_mclist *ml, *i;
3577 struct net_device *dev;
3578 int err;
3579
3580 rtnl_lock();
3581
3582 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003583 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 if (!dev)
3585 goto done;
3586
3587 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003588 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 goto done;
3590
3591 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003592 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 if (i == NULL)
3594 goto done;
3595
3596 err = 0;
3597 for (ml = po->mclist; ml; ml = ml->next) {
3598 if (ml->ifindex == mreq->mr_ifindex &&
3599 ml->type == mreq->mr_type &&
3600 ml->alen == mreq->mr_alen &&
3601 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3602 ml->count++;
3603 /* Free the new element ... */
3604 kfree(i);
3605 goto done;
3606 }
3607 }
3608
3609 i->type = mreq->mr_type;
3610 i->ifindex = mreq->mr_ifindex;
3611 i->alen = mreq->mr_alen;
3612 memcpy(i->addr, mreq->mr_address, i->alen);
Mathias Krause309cf372016-04-10 12:52:28 +02003613 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 i->count = 1;
3615 i->next = po->mclist;
3616 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003617 err = packet_dev_mc(dev, i, 1);
3618 if (err) {
3619 po->mclist = i->next;
3620 kfree(i);
3621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623done:
3624 rtnl_unlock();
3625 return err;
3626}
3627
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003628static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct packet_mclist *ml, **mlp;
3631
3632 rtnl_lock();
3633
3634 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3635 if (ml->ifindex == mreq->mr_ifindex &&
3636 ml->type == mreq->mr_type &&
3637 ml->alen == mreq->mr_alen &&
3638 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3639 if (--ml->count == 0) {
3640 struct net_device *dev;
3641 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003642 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3643 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 kfree(ml);
3646 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649 }
3650 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
3654static void packet_flush_mclist(struct sock *sk)
3655{
3656 struct packet_sock *po = pkt_sk(sk);
3657 struct packet_mclist *ml;
3658
3659 if (!po->mclist)
3660 return;
3661
3662 rtnl_lock();
3663 while ((ml = po->mclist) != NULL) {
3664 struct net_device *dev;
3665
3666 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003667 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3668 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 kfree(ml);
3671 }
3672 rtnl_unlock();
3673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675static int
David S. Millerb7058842009-09-30 16:12:20 -07003676packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
3678 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003679 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 int ret;
3681
3682 if (level != SOL_PACKET)
3683 return -ENOPROTOOPT;
3684
Johann Baudy69e3c752009-05-18 22:11:22 -07003685 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003686 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 case PACKET_DROP_MEMBERSHIP:
3688 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003689 struct packet_mreq_max mreq;
3690 int len = optlen;
3691 memset(&mreq, 0, sizeof(mreq));
3692 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003694 if (len > sizeof(mreq))
3695 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003696 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003698 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3699 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (optname == PACKET_ADD_MEMBERSHIP)
3701 ret = packet_mc_add(sk, &mreq);
3702 else
3703 ret = packet_mc_drop(sk, &mreq);
3704 return ret;
3705 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003708 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 {
chetan lokef6fb8f102011-08-19 10:18:16 +00003710 union tpacket_req_u req_u;
3711 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Eric Dumazet55ca7b12018-04-15 17:52:04 -07003713 lock_sock(sk);
chetan lokef6fb8f102011-08-19 10:18:16 +00003714 switch (po->tp_version) {
3715 case TPACKET_V1:
3716 case TPACKET_V2:
3717 len = sizeof(req_u.req);
3718 break;
3719 case TPACKET_V3:
3720 default:
3721 len = sizeof(req_u.req3);
3722 break;
3723 }
Eric Dumazet55ca7b12018-04-15 17:52:04 -07003724 if (optlen < len) {
3725 ret = -EINVAL;
3726 } else {
3727 if (copy_from_user(&req_u.req, optval, len))
3728 ret = -EFAULT;
3729 else
3730 ret = packet_set_ring(sk, &req_u, 0,
3731 optname == PACKET_TX_RING);
3732 }
3733 release_sock(sk);
3734 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
3736 case PACKET_COPY_THRESH:
3737 {
3738 int val;
3739
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003740 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003742 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return -EFAULT;
3744
3745 pkt_sk(sk)->copy_thresh = val;
3746 return 0;
3747 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003748 case PACKET_VERSION:
3749 {
3750 int val;
3751
3752 if (optlen != sizeof(val))
3753 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003754 if (copy_from_user(&val, optval, sizeof(val)))
3755 return -EFAULT;
3756 switch (val) {
3757 case TPACKET_V1:
3758 case TPACKET_V2:
chetan lokef6fb8f102011-08-19 10:18:16 +00003759 case TPACKET_V3:
Philip Pettersson84ac7262016-11-30 14:55:36 -08003760 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003761 default:
3762 return -EINVAL;
3763 }
Philip Pettersson84ac7262016-11-30 14:55:36 -08003764 lock_sock(sk);
3765 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3766 ret = -EBUSY;
3767 } else {
3768 po->tp_version = val;
3769 ret = 0;
3770 }
3771 release_sock(sk);
3772 return ret;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003773 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003774 case PACKET_RESERVE:
3775 {
3776 unsigned int val;
3777
3778 if (optlen != sizeof(val))
3779 return -EINVAL;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003780 if (copy_from_user(&val, optval, sizeof(val)))
3781 return -EFAULT;
Andrey Konovalov1f49c8c2017-03-29 16:11:22 +02003782 if (val > INT_MAX)
3783 return -EINVAL;
Willem de Bruijne5841352017-08-10 12:41:58 -04003784 lock_sock(sk);
3785 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3786 ret = -EBUSY;
3787 } else {
3788 po->tp_reserve = val;
3789 ret = 0;
3790 }
3791 release_sock(sk);
3792 return ret;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003793 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003794 case PACKET_LOSS:
3795 {
3796 unsigned int val;
3797
3798 if (optlen != sizeof(val))
3799 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003800 if (copy_from_user(&val, optval, sizeof(val)))
3801 return -EFAULT;
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003802
3803 lock_sock(sk);
3804 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3805 ret = -EBUSY;
3806 } else {
3807 po->tp_loss = !!val;
3808 ret = 0;
3809 }
3810 release_sock(sk);
3811 return ret;
Johann Baudy69e3c752009-05-18 22:11:22 -07003812 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003813 case PACKET_AUXDATA:
3814 {
3815 int val;
3816
3817 if (optlen < sizeof(val))
3818 return -EINVAL;
3819 if (copy_from_user(&val, optval, sizeof(val)))
3820 return -EFAULT;
3821
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003822 lock_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003823 po->auxdata = !!val;
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003824 release_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003825 return 0;
3826 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003827 case PACKET_ORIGDEV:
3828 {
3829 int val;
3830
3831 if (optlen < sizeof(val))
3832 return -EINVAL;
3833 if (copy_from_user(&val, optval, sizeof(val)))
3834 return -EFAULT;
3835
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003836 lock_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003837 po->origdev = !!val;
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003838 release_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003839 return 0;
3840 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003841 case PACKET_VNET_HDR:
3842 {
3843 int val;
3844
3845 if (sock->type != SOCK_RAW)
3846 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003847 if (optlen < sizeof(val))
3848 return -EINVAL;
3849 if (copy_from_user(&val, optval, sizeof(val)))
3850 return -EFAULT;
3851
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003852 lock_sock(sk);
3853 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3854 ret = -EBUSY;
3855 } else {
3856 po->has_vnet_hdr = !!val;
3857 ret = 0;
3858 }
3859 release_sock(sk);
3860 return ret;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003861 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003862 case PACKET_TIMESTAMP:
3863 {
3864 int val;
3865
3866 if (optlen != sizeof(val))
3867 return -EINVAL;
3868 if (copy_from_user(&val, optval, sizeof(val)))
3869 return -EFAULT;
3870
3871 po->tp_tstamp = val;
3872 return 0;
3873 }
David S. Millerdc99f602011-07-05 01:45:05 -07003874 case PACKET_FANOUT:
3875 {
3876 int val;
3877
3878 if (optlen != sizeof(val))
3879 return -EINVAL;
3880 if (copy_from_user(&val, optval, sizeof(val)))
3881 return -EFAULT;
3882
3883 return fanout_add(sk, val & 0xffff, val >> 16);
3884 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003885 case PACKET_FANOUT_DATA:
3886 {
3887 if (!po->fanout)
3888 return -EINVAL;
3889
3890 return fanout_set_data(po, optval, optlen);
3891 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003892 case PACKET_TX_HAS_OFF:
3893 {
3894 unsigned int val;
3895
3896 if (optlen != sizeof(val))
3897 return -EINVAL;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003898 if (copy_from_user(&val, optval, sizeof(val)))
3899 return -EFAULT;
Willem de Bruijnb2a52072018-04-23 17:37:03 -04003900
3901 lock_sock(sk);
3902 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3903 ret = -EBUSY;
3904 } else {
3905 po->tp_tx_has_off = !!val;
3906 ret = 0;
3907 }
3908 release_sock(sk);
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003909 return 0;
3910 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003911 case PACKET_QDISC_BYPASS:
3912 {
3913 int val;
3914
3915 if (optlen != sizeof(val))
3916 return -EINVAL;
3917 if (copy_from_user(&val, optval, sizeof(val)))
3918 return -EFAULT;
3919
3920 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3921 return 0;
3922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 default:
3924 return -ENOPROTOOPT;
3925 }
3926}
3927
3928static int packet_getsockopt(struct socket *sock, int level, int optname,
3929 char __user *optval, int __user *optlen)
3930{
3931 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003932 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct sock *sk = sock->sk;
3934 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003935 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003936 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003937 struct tpacket_rollover_stats rstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 if (level != SOL_PACKET)
3940 return -ENOPROTOOPT;
3941
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003942 if (get_user(len, optlen))
3943 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 if (len < 0)
3946 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003947
Johann Baudy69e3c752009-05-18 22:11:22 -07003948 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003951 memcpy(&st, &po->stats, sizeof(st));
3952 memset(&po->stats, 0, sizeof(po->stats));
3953 spin_unlock_bh(&sk->sk_receive_queue.lock);
3954
chetan lokef6fb8f102011-08-19 10:18:16 +00003955 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003956 lv = sizeof(struct tpacket_stats_v3);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003957 st.stats3.tp_packets += st.stats3.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003958 data = &st.stats3;
chetan lokef6fb8f102011-08-19 10:18:16 +00003959 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003960 lv = sizeof(struct tpacket_stats);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003961 st.stats1.tp_packets += st.stats1.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003962 data = &st.stats1;
chetan lokef6fb8f102011-08-19 10:18:16 +00003963 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003966 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003967 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003968 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003969 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003970 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003971 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003972 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003973 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003974 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003975 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003976 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003977 break;
3978 case PACKET_HDRLEN:
3979 if (len > sizeof(int))
3980 len = sizeof(int);
Alexander Potapenko897e8c52017-04-25 18:51:46 +02003981 if (len < sizeof(int))
3982 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003983 if (copy_from_user(&val, optval, len))
3984 return -EFAULT;
3985 switch (val) {
3986 case TPACKET_V1:
3987 val = sizeof(struct tpacket_hdr);
3988 break;
3989 case TPACKET_V2:
3990 val = sizeof(struct tpacket2_hdr);
3991 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00003992 case TPACKET_V3:
3993 val = sizeof(struct tpacket3_hdr);
3994 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003995 default:
3996 return -EINVAL;
3997 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003998 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003999 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07004000 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07004001 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07004002 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07004003 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07004004 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07004005 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07004006 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07004007 break;
David S. Millerdc99f602011-07-05 01:45:05 -07004008 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07004009 val = (po->fanout ?
4010 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00004011 ((u32)po->fanout->type << 16) |
4012 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07004013 0);
David S. Millerdc99f602011-07-05 01:45:05 -07004014 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04004015 case PACKET_ROLLOVER_STATS:
Mike Maloney30c573a2017-11-28 10:44:29 -05004016 if (!po->rollover)
Willem de Bruijna9b63912015-05-12 11:56:50 -04004017 return -EINVAL;
Mike Maloney30c573a2017-11-28 10:44:29 -05004018 rstats.tp_all = atomic_long_read(&po->rollover->num);
4019 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
4020 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
4021 data = &rstats;
4022 lv = sizeof(rstats);
Willem de Bruijna9b63912015-05-12 11:56:50 -04004023 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00004024 case PACKET_TX_HAS_OFF:
4025 val = po->tp_tx_has_off;
4026 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01004027 case PACKET_QDISC_BYPASS:
4028 val = packet_use_direct_xmit(po);
4029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 default:
4031 return -ENOPROTOOPT;
4032 }
4033
Eric Dumazetc06fff62012-04-19 21:56:11 +00004034 if (len > lv)
4035 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004036 if (put_user(len, optlen))
4037 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08004038 if (copy_to_user(optval, data, len))
4039 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08004040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
4043
Willem de Bruijn719c44d2016-06-07 12:06:34 -04004044#ifdef CONFIG_COMPAT
4045static int compat_packet_setsockopt(struct socket *sock, int level, int optname,
4046 char __user *optval, unsigned int optlen)
4047{
4048 struct packet_sock *po = pkt_sk(sock->sk);
4049
4050 if (level != SOL_PACKET)
4051 return -ENOPROTOOPT;
4052
4053 if (optname == PACKET_FANOUT_DATA &&
4054 po->fanout && po->fanout->type == PACKET_FANOUT_CBPF) {
4055 optval = (char __user *)get_compat_bpf_fprog(optval);
4056 if (!optval)
4057 return -EFAULT;
4058 optlen = sizeof(struct sock_fprog);
4059 }
4060
4061 return packet_setsockopt(sock, level, optname, optval, optlen);
4062}
4063#endif
4064
Jiri Pirko351638e2013-05-28 01:30:21 +00004065static int packet_notifier(struct notifier_block *this,
4066 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00004069 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004070 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
stephen hemminger808f5112010-02-22 07:57:18 +00004072 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004073 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 struct packet_sock *po = pkt_sk(sk);
4075
4076 switch (msg) {
4077 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07004079 packet_dev_mclist_delete(dev, &po->mclist);
David S. Millera2efcfa2007-05-29 13:12:50 -07004080 /* fallthrough */
4081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 case NETDEV_DOWN:
4083 if (dev->ifindex == po->ifindex) {
4084 spin_lock(&po->bind_lock);
4085 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07004086 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 sk->sk_err = ENETDOWN;
4088 if (!sock_flag(sk, SOCK_DEAD))
4089 sk->sk_error_report(sk);
4090 }
4091 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01004092 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00004094 if (po->prot_hook.dev)
4095 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 po->prot_hook.dev = NULL;
4097 }
4098 spin_unlock(&po->bind_lock);
4099 }
4100 break;
4101 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00004102 if (dev->ifindex == po->ifindex) {
4103 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004104 if (po->num)
4105 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00004106 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 break;
4109 }
4110 }
stephen hemminger808f5112010-02-22 07:57:18 +00004111 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 return NOTIFY_DONE;
4113}
4114
4115
4116static int packet_ioctl(struct socket *sock, unsigned int cmd,
4117 unsigned long arg)
4118{
4119 struct sock *sk = sock->sk;
4120
Johann Baudy69e3c752009-05-18 22:11:22 -07004121 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004122 case SIOCOUTQ:
4123 {
4124 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07004125
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004126 return put_user(amount, (int __user *)arg);
4127 }
4128 case SIOCINQ:
4129 {
4130 struct sk_buff *skb;
4131 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004133 spin_lock_bh(&sk->sk_receive_queue.lock);
4134 skb = skb_peek(&sk->sk_receive_queue);
4135 if (skb)
4136 amount = skb->len;
4137 spin_unlock_bh(&sk->sk_receive_queue.lock);
4138 return put_user(amount, (int __user *)arg);
4139 }
4140 case SIOCGSTAMP:
4141 return sock_get_timestamp(sk, (struct timeval __user *)arg);
4142 case SIOCGSTAMPNS:
4143 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004146 case SIOCADDRT:
4147 case SIOCDELRT:
4148 case SIOCDARP:
4149 case SIOCGARP:
4150 case SIOCSARP:
4151 case SIOCGIFADDR:
4152 case SIOCSIFADDR:
4153 case SIOCGIFBRDADDR:
4154 case SIOCSIFBRDADDR:
4155 case SIOCGIFNETMASK:
4156 case SIOCSIFNETMASK:
4157 case SIOCGIFDSTADDR:
4158 case SIOCSIFDSTADDR:
4159 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004160 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161#endif
4162
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004163 default:
4164 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
4166 return 0;
4167}
4168
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004169static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 poll_table *wait)
4171{
4172 struct sock *sk = sock->sk;
4173 struct packet_sock *po = pkt_sk(sk);
4174 unsigned int mask = datagram_poll(file, sock, wait);
4175
4176 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004177 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f102011-08-19 10:18:16 +00004178 if (!packet_previous_rx_frame(po, &po->rx_ring,
4179 TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 mask |= POLLIN | POLLRDNORM;
4181 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04004182 if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04004183 po->pressure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004185 spin_lock_bh(&sk->sk_write_queue.lock);
4186 if (po->tx_ring.pg_vec) {
4187 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
4188 mask |= POLLOUT | POLLWRNORM;
4189 }
4190 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 return mask;
4192}
4193
4194
4195/* Dirty? Well, I still did not learn better way to account
4196 * for user mmaps.
4197 */
4198
4199static void packet_mm_open(struct vm_area_struct *vma)
4200{
4201 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004202 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004204
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 if (sk)
4206 atomic_inc(&pkt_sk(sk)->mapped);
4207}
4208
4209static void packet_mm_close(struct vm_area_struct *vma)
4210{
4211 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004212 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 if (sk)
4216 atomic_dec(&pkt_sk(sk)->mapped);
4217}
4218
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004219static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004220 .open = packet_mm_open,
4221 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222};
4223
Neil Horman0e3125c2010-11-16 10:26:47 -08004224static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
4225 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
4227 int i;
4228
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004229 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004230 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00004231 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08004232 vfree(pg_vec[i].buffer);
4233 else
4234 free_pages((unsigned long)pg_vec[i].buffer,
4235 order);
4236 pg_vec[i].buffer = NULL;
4237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
4239 kfree(pg_vec);
4240}
4241
Olof Johanssoneea49cc92011-11-02 11:00:49 +00004242static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004243{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004244 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08004245 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
4246 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07004247
Neil Horman0e3125c2010-11-16 10:26:47 -08004248 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004249 if (buffer)
4250 return buffer;
4251
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004252 /* __get_free_pages failed, fall back to vmalloc */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00004253 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08004254 if (buffer)
4255 return buffer;
4256
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004257 /* vmalloc failed, lets dig into swap here */
Neil Horman0e3125c2010-11-16 10:26:47 -08004258 gfp_flags &= ~__GFP_NORETRY;
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004259 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004260 if (buffer)
4261 return buffer;
4262
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004263 /* complete and utter failure */
Neil Horman0e3125c2010-11-16 10:26:47 -08004264 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004265}
4266
Neil Horman0e3125c2010-11-16 10:26:47 -08004267static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004268{
4269 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004270 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004271 int i;
4272
Christoph Paasch49190e42019-03-18 23:14:52 -07004273 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004274 if (unlikely(!pg_vec))
4275 goto out;
4276
4277 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00004278 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004279 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004280 goto out_free_pgvec;
4281 }
4282
4283out:
4284 return pg_vec;
4285
4286out_free_pgvec:
4287 free_pg_vec(pg_vec, order, block_nr);
4288 pg_vec = NULL;
4289 goto out;
4290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
chetan lokef6fb8f102011-08-19 10:18:16 +00004292static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004293 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Neil Horman0e3125c2010-11-16 10:26:47 -08004295 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08004297 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004298 struct packet_ring_buffer *rb;
4299 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004300 __be16 num;
chetan lokef6fb8f102011-08-19 10:18:16 +00004301 int err = -EINVAL;
4302 /* Added to avoid minimal code churn */
4303 struct tpacket_req *req = &req_u->req;
4304
4305 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
4306 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
Dave Jones6ae81ce2016-04-04 15:11:50 -04004307 net_warn_ratelimited("Tx-ring is not supported.\n");
chetan lokef6fb8f102011-08-19 10:18:16 +00004308 goto out;
4309 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004310
4311 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4312 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4313
4314 err = -EBUSY;
4315 if (!closing) {
4316 if (atomic_read(&po->mapped))
4317 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004318 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004319 goto out;
4320 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 if (req->tp_block_nr) {
Willem de Bruijn59629842018-08-06 10:38:34 -04004323 unsigned int min_frame_size;
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004326 err = -EBUSY;
4327 if (unlikely(rb->pg_vec))
4328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004330 switch (po->tp_version) {
4331 case TPACKET_V1:
4332 po->tp_hdrlen = TPACKET_HDRLEN;
4333 break;
4334 case TPACKET_V2:
4335 po->tp_hdrlen = TPACKET2_HDRLEN;
4336 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00004337 case TPACKET_V3:
4338 po->tp_hdrlen = TPACKET3_HDRLEN;
4339 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004340 }
4341
Johann Baudy69e3c752009-05-18 22:11:22 -07004342 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004343 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004344 goto out;
Tobias Klauser90836b62015-11-17 10:40:21 +01004345 if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004346 goto out;
Willem de Bruijn59629842018-08-06 10:38:34 -04004347 min_frame_size = po->tp_hdrlen + po->tp_reserve;
Eric Dumazetdc808112014-08-15 09:16:04 -07004348 if (po->tp_version >= TPACKET_V3 &&
Willem de Bruijn59629842018-08-06 10:38:34 -04004349 req->tp_block_size <
4350 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
Eric Dumazetdc808112014-08-15 09:16:04 -07004351 goto out;
Willem de Bruijn59629842018-08-06 10:38:34 -04004352 if (unlikely(req->tp_frame_size < min_frame_size))
Johann Baudy69e3c752009-05-18 22:11:22 -07004353 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004354 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Tobias Klauser4194b492015-11-17 10:38:36 +01004357 rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
4358 if (unlikely(rb->frames_per_block == 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004359 goto out;
Kal Conley3b703172019-02-10 09:57:11 +01004360 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
Andrey Konovalov10452122017-03-29 16:11:21 +02004361 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004362 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4363 req->tp_frame_nr))
4364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
4366 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004367 order = get_order(req->tp_block_size);
4368 pg_vec = alloc_pg_vec(req, order);
4369 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 goto out;
chetan lokef6fb8f102011-08-19 10:18:16 +00004371 switch (po->tp_version) {
4372 case TPACKET_V3:
4373 /* Transmit path is not supported. We checked
4374 * it above but just being paranoid
4375 */
4376 if (!tx_ring)
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304377 init_prb_bdqc(po, rb, pg_vec, req_u);
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004378 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00004379 default:
4380 break;
4381 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004382 }
4383 /* Done */
4384 else {
4385 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004386 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391 /* Detach socket from network */
4392 spin_lock(&po->bind_lock);
4393 was_running = po->running;
4394 num = po->num;
4395 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004397 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 }
4399 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 synchronize_net();
4402
4403 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004404 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 if (closing || atomic_read(&po->mapped) == 0) {
4406 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004407 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004408 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07004409 rb->frame_max = (req->tp_frame_nr - 1);
4410 rb->head = 0;
4411 rb->frame_size = req->tp_frame_size;
4412 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Changli Gaoc053fd92010-12-10 16:02:20 -08004414 swap(rb->pg_vec_order, order);
4415 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Johann Baudy69e3c752009-05-18 22:11:22 -07004417 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4418 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4419 tpacket_rcv : packet_rcv;
4420 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004422 pr_err("packet_mmap: vma is busy: %d\n",
4423 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 }
Herbert Xu905db442009-01-30 14:12:06 -08004425 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004428 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004430 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
4432 spin_unlock(&po->bind_lock);
WANG Cong47f3bf62017-07-24 10:07:32 -07004433 if (pg_vec && (po->tp_version > TPACKET_V2)) {
chetan lokef6fb8f102011-08-19 10:18:16 +00004434 /* Because we don't support block-based V3 on tx-ring */
4435 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004436 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f102011-08-19 10:18:16 +00004437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (pg_vec)
4440 free_pg_vec(pg_vec, order, req->tp_block_nr);
4441out:
4442 return err;
4443}
4444
Johann Baudy69e3c752009-05-18 22:11:22 -07004445static int packet_mmap(struct file *file, struct socket *sock,
4446 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
4448 struct sock *sk = sock->sk;
4449 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004450 unsigned long size, expected_size;
4451 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 unsigned long start;
4453 int err = -EINVAL;
4454 int i;
4455
4456 if (vma->vm_pgoff)
4457 return -EINVAL;
4458
Herbert Xu905db442009-01-30 14:12:06 -08004459 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004460
4461 expected_size = 0;
4462 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4463 if (rb->pg_vec) {
4464 expected_size += rb->pg_vec_len
4465 * rb->pg_vec_pages
4466 * PAGE_SIZE;
4467 }
4468 }
4469
4470 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004472
4473 size = vma->vm_end - vma->vm_start;
4474 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 goto out;
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004478 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4479 if (rb->pg_vec == NULL)
4480 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004481
Johann Baudy69e3c752009-05-18 22:11:22 -07004482 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004483 struct page *page;
4484 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004485 int pg_num;
4486
Changli Gaoc56b4d92010-12-01 02:52:57 +00004487 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4488 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004489 err = vm_insert_page(vma, start, page);
4490 if (unlikely(err))
4491 goto out;
4492 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004493 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004494 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004497
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004498 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 vma->vm_ops = &packet_mmap_ops;
4500 err = 0;
4501
4502out:
Herbert Xu905db442009-01-30 14:12:06 -08004503 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return err;
4505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004507static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 .family = PF_PACKET,
4509 .owner = THIS_MODULE,
4510 .release = packet_release,
4511 .bind = packet_bind_spkt,
4512 .connect = sock_no_connect,
4513 .socketpair = sock_no_socketpair,
4514 .accept = sock_no_accept,
4515 .getname = packet_getname_spkt,
4516 .poll = datagram_poll,
4517 .ioctl = packet_ioctl,
4518 .listen = sock_no_listen,
4519 .shutdown = sock_no_shutdown,
4520 .setsockopt = sock_no_setsockopt,
4521 .getsockopt = sock_no_getsockopt,
4522 .sendmsg = packet_sendmsg_spkt,
4523 .recvmsg = packet_recvmsg,
4524 .mmap = sock_no_mmap,
4525 .sendpage = sock_no_sendpage,
4526};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004528static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 .family = PF_PACKET,
4530 .owner = THIS_MODULE,
4531 .release = packet_release,
4532 .bind = packet_bind,
4533 .connect = sock_no_connect,
4534 .socketpair = sock_no_socketpair,
4535 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004536 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 .poll = packet_poll,
4538 .ioctl = packet_ioctl,
4539 .listen = sock_no_listen,
4540 .shutdown = sock_no_shutdown,
4541 .setsockopt = packet_setsockopt,
4542 .getsockopt = packet_getsockopt,
Willem de Bruijn719c44d2016-06-07 12:06:34 -04004543#ifdef CONFIG_COMPAT
4544 .compat_setsockopt = compat_packet_setsockopt,
4545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 .sendmsg = packet_sendmsg,
4547 .recvmsg = packet_recvmsg,
4548 .mmap = packet_mmap,
4549 .sendpage = sock_no_sendpage,
4550};
4551
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004552static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 .family = PF_PACKET,
4554 .create = packet_create,
4555 .owner = THIS_MODULE,
4556};
4557
4558static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004559 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560};
4561
4562#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004565 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Denis V. Luneve372c412007-11-19 22:31:54 -08004567 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004568
4569 rcu_read_lock();
4570 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
4573static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4574{
Herbert Xu1bf40952007-12-16 14:04:02 -08004575 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004576 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577}
4578
4579static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004580 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
stephen hemminger808f5112010-02-22 07:57:18 +00004582 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583}
4584
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004585static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
4587 if (v == SEQ_START_TOKEN)
4588 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
4589 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004590 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 const struct packet_sock *po = pkt_sk(s);
4592
4593 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004594 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 s,
4596 atomic_read(&s->sk_refcnt),
4597 s->sk_type,
4598 ntohs(po->num),
4599 po->ifindex,
4600 po->running,
4601 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004602 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004603 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
4605
4606 return 0;
4607}
4608
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004609static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 .start = packet_seq_start,
4611 .next = packet_seq_next,
4612 .stop = packet_seq_stop,
4613 .show = packet_seq_show,
4614};
4615
4616static int packet_seq_open(struct inode *inode, struct file *file)
4617{
Denis V. Luneve372c412007-11-19 22:31:54 -08004618 return seq_open_net(inode, file, &packet_seq_ops,
4619 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
4621
Arjan van de Venda7071d2007-02-12 00:55:36 -08004622static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 .owner = THIS_MODULE,
4624 .open = packet_seq_open,
4625 .read = seq_read,
4626 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08004627 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628};
4629
4630#endif
4631
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004632static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004633{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004634 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004635 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004636
Gao fengd4beaa62013-02-18 01:34:54 +00004637 if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004638 return -ENOMEM;
4639
4640 return 0;
4641}
4642
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004643static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004644{
Gao fengece31ff2013-02-18 01:34:56 +00004645 remove_proc_entry("packet", net->proc_net);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004646}
4647
4648static struct pernet_operations packet_net_ops = {
4649 .init = packet_net_init,
4650 .exit = packet_net_exit,
4651};
4652
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654static void __exit packet_exit(void)
4655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004657 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 sock_unregister(PF_PACKET);
4659 proto_unregister(&packet_proto);
4660}
4661
4662static int __init packet_init(void)
4663{
YueHaibing7b7b9da2019-05-09 22:52:20 +08004664 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
YueHaibing7b7b9da2019-05-09 22:52:20 +08004666 rc = proto_register(&packet_proto, 0);
4667 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 goto out;
YueHaibing7b7b9da2019-05-09 22:52:20 +08004669 rc = sock_register(&packet_family_ops);
4670 if (rc)
4671 goto out_proto;
4672 rc = register_pernet_subsys(&packet_net_ops);
4673 if (rc)
4674 goto out_sock;
4675 rc = register_netdevice_notifier(&packet_netdev_notifier);
4676 if (rc)
4677 goto out_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
YueHaibing7b7b9da2019-05-09 22:52:20 +08004679 return 0;
4680
4681out_pernet:
4682 unregister_pernet_subsys(&packet_net_ops);
4683out_sock:
4684 sock_unregister(PF_PACKET);
4685out_proto:
4686 proto_unregister(&packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687out:
4688 return rc;
4689}
4690
4691module_init(packet_init);
4692module_exit(packet_exit);
4693MODULE_LICENSE("GPL");
4694MODULE_ALIAS_NETPROTO(PF_PACKET);