blob: 741953b42f44c505a38dc22e63b7299e74f9513c [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 lokef6fb8f12011-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 Torvalds7c0f6ba2016-12-24 11:46:01 -080076#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#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 lokef6fb8f12011-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 lokef6fb8f12011-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 lokef6fb8f12011-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 lokef6fb8f12011-08-19 10:18:16 +0000180#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
181#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
182#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
183#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
184#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
185#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
186#define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
187
Johann Baudy69e3c752009-05-18 22:11:22 -0700188struct packet_sock;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000189static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
190 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
chetan lokef6fb8f12011-08-19 10:18:16 +0000192static void *packet_previous_frame(struct packet_sock *po,
193 struct packet_ring_buffer *rb,
194 int status);
195static void packet_increment_head(struct packet_ring_buffer *buff);
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300196static int prb_curr_blk_in_use(struct tpacket_block_desc *);
chetan lokebc59ba32011-08-25 10:43:30 +0000197static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000198 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000199static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f12011-08-19 10:18:16 +0000200 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000201static int prb_queue_frozen(struct tpacket_kbdq_core *);
202static void prb_open_block(struct tpacket_kbdq_core *,
203 struct tpacket_block_desc *);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700204static void prb_retire_rx_blk_timer_expired(struct timer_list *);
chetan lokebc59ba32011-08-25 10:43:30 +0000205static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
chetan lokebc59ba32011-08-25 10:43:30 +0000206static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
207static void prb_clear_rxhash(struct tpacket_kbdq_core *,
208 struct tpacket3_hdr *);
209static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
210 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static void packet_flush_mclist(struct sock *sk);
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200212static u16 packet_pick_tx_queue(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Herbert Xuffbc6112007-02-04 23:33:10 -0800214struct packet_skb_cb {
Herbert Xuffbc6112007-02-04 23:33:10 -0800215 union {
216 struct sockaddr_pkt pkt;
Eyal Birger2472d762015-03-01 14:58:28 +0200217 union {
218 /* Trick: alias skb original length with
219 * ll.sll_family and ll.protocol in order
220 * to save room.
221 */
222 unsigned int origlen;
223 struct sockaddr_ll ll;
224 };
Herbert Xuffbc6112007-02-04 23:33:10 -0800225 } sa;
226};
227
David Woodhoused3869ef2015-09-23 19:45:08 +0100228#define vio_le() virtio_legacy_is_little_endian()
229
Herbert Xuffbc6112007-02-04 23:33:10 -0800230#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800231
chetan lokebc59ba32011-08-25 10:43:30 +0000232#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f12011-08-19 10:18:16 +0000233#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000234 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000235#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000236 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f12011-08-19 10:18:16 +0000237#define GET_NEXT_PRB_BLK_NUM(x) \
238 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
239 ((x)->kactive_blk_num+1) : 0)
240
David S. Millerdc99f602011-07-05 01:45:05 -0700241static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
242static void __fanout_link(struct sock *sk, struct packet_sock *po);
243
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100244static int packet_direct_xmit(struct sk_buff *skb)
245{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200246 return dev_direct_xmit(skb, packet_pick_tx_queue(skb));
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100247}
248
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100249static struct net_device *packet_cached_dev_get(struct packet_sock *po)
250{
251 struct net_device *dev;
252
253 rcu_read_lock();
254 dev = rcu_dereference(po->cached_dev);
255 if (likely(dev))
256 dev_hold(dev);
257 rcu_read_unlock();
258
259 return dev;
260}
261
262static void packet_cached_dev_assign(struct packet_sock *po,
263 struct net_device *dev)
264{
265 rcu_assign_pointer(po->cached_dev, dev);
266}
267
268static void packet_cached_dev_reset(struct packet_sock *po)
269{
270 RCU_INIT_POINTER(po->cached_dev, NULL);
271}
272
Daniel Borkmannd346a3f2013-12-06 11:36:17 +0100273static bool packet_use_direct_xmit(const struct packet_sock *po)
274{
275 return po->xmit == packet_direct_xmit;
276}
277
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200278static u16 packet_pick_tx_queue(struct sk_buff *skb)
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100279{
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200280 struct net_device *dev = skb->dev;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100281 const struct net_device_ops *ops = dev->netdev_ops;
Paolo Abenib71b5832019-03-20 11:02:05 +0100282 int cpu = raw_smp_processor_id();
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100283 u16 queue_index;
284
Paolo Abenib71b5832019-03-20 11:02:05 +0100285#ifdef CONFIG_XPS
286 skb->sender_cpu = cpu + 1;
287#endif
288 skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100289 if (ops->ndo_select_queue) {
Paolo Abenia350ecc2019-03-20 11:02:06 +0100290 queue_index = ops->ndo_select_queue(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100291 queue_index = netdev_cap_txqueue(dev, queue_index);
292 } else {
Paolo Abenib71b5832019-03-20 11:02:05 +0100293 queue_index = netdev_pick_tx(dev, skb, NULL);
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100294 }
295
Magnus Karlsson865b03f2018-05-02 13:01:33 +0200296 return queue_index;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +0100297}
298
Willem de Bruijna6361f02018-04-23 17:37:03 -0400299/* __register_prot_hook must be invoked through register_prot_hook
David S. Millerce06b032011-07-04 01:44:29 -0700300 * or from a context in which asynchronous accesses to the packet
301 * socket is not possible (packet_create()).
302 */
Willem de Bruijna6361f02018-04-23 17:37:03 -0400303static void __register_prot_hook(struct sock *sk)
David S. Millerce06b032011-07-04 01:44:29 -0700304{
305 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100306
David S. Millerce06b032011-07-04 01:44:29 -0700307 if (!po->running) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100308 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700309 __fanout_link(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100310 else
David S. Millerdc99f602011-07-05 01:45:05 -0700311 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100312
David S. Millerce06b032011-07-04 01:44:29 -0700313 sock_hold(sk);
314 po->running = 1;
315 }
316}
317
Willem de Bruijna6361f02018-04-23 17:37:03 -0400318static void register_prot_hook(struct sock *sk)
319{
320 lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
321 __register_prot_hook(sk);
322}
323
324/* If the sync parameter is true, we will temporarily drop
David S. Millerce06b032011-07-04 01:44:29 -0700325 * the po->bind_lock and do a synchronize_net to make sure no
326 * asynchronous packet processing paths still refer to the elements
327 * of po->prot_hook. If the sync parameter is false, it is the
328 * callers responsibility to take care of this.
329 */
330static void __unregister_prot_hook(struct sock *sk, bool sync)
331{
332 struct packet_sock *po = pkt_sk(sk);
333
Willem de Bruijna6361f02018-04-23 17:37:03 -0400334 lockdep_assert_held_once(&po->bind_lock);
335
David S. Millerce06b032011-07-04 01:44:29 -0700336 po->running = 0;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100337
338 if (po->fanout)
David S. Millerdc99f602011-07-05 01:45:05 -0700339 __fanout_unlink(sk, po);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +0100340 else
David S. Millerdc99f602011-07-05 01:45:05 -0700341 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100342
David S. Millerce06b032011-07-04 01:44:29 -0700343 __sock_put(sk);
344
345 if (sync) {
346 spin_unlock(&po->bind_lock);
347 synchronize_net();
348 spin_lock(&po->bind_lock);
349 }
350}
351
352static void unregister_prot_hook(struct sock *sk, bool sync)
353{
354 struct packet_sock *po = pkt_sk(sk);
355
356 if (po->running)
357 __unregister_prot_hook(sk, sync);
358}
359
Michael S. Tsirkin6e580402014-11-24 13:32:16 +0200360static inline struct page * __pure pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000361{
362 if (is_vmalloc_addr(addr))
363 return vmalloc_to_page(addr);
364 return virt_to_page(addr);
365}
366
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700367static void __packet_set_status(struct packet_sock *po, void *frame, int status)
368{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000369 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700370
371 h.raw = frame;
372 switch (po->tp_version) {
373 case TPACKET_V1:
374 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000375 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700376 break;
377 case TPACKET_V2:
378 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000379 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700380 break;
chetan lokef6fb8f12011-08-19 10:18:16 +0000381 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800382 h.h3->tp_status = status;
383 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
384 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700385 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000386 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700387 BUG();
388 }
389
390 smp_wmb();
391}
392
393static int __packet_get_status(struct packet_sock *po, void *frame)
394{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000395 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700396
397 smp_rmb();
398
399 h.raw = frame;
400 switch (po->tp_version) {
401 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000402 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700403 return h.h1->tp_status;
404 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000405 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700406 return h.h2->tp_status;
chetan lokef6fb8f12011-08-19 10:18:16 +0000407 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -0800408 flush_dcache_page(pgv_to_page(&h.h3->tp_status));
409 return h.h3->tp_status;
Johann Baudy69e3c752009-05-18 22:11:22 -0700410 default:
chetan lokef6fb8f12011-08-19 10:18:16 +0000411 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700412 BUG();
413 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Johann Baudy69e3c752009-05-18 22:11:22 -0700416
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000417static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
418 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000419{
420 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
421
Willem de Bruijn68a360e2014-07-25 18:01:31 -0400422 if (shhwtstamps &&
423 (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
424 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
425 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000426
427 if (ktime_to_timespec_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000428 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000429
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000430 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000431}
432
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000433static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
434 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000435{
436 union tpacket_uhdr h;
437 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000438 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000439
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000440 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
441 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000442
443 h.raw = frame;
444 switch (po->tp_version) {
445 case TPACKET_V1:
446 h.h1->tp_sec = ts.tv_sec;
447 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
448 break;
449 case TPACKET_V2:
450 h.h2->tp_sec = ts.tv_sec;
451 h.h2->tp_nsec = ts.tv_nsec;
452 break;
453 case TPACKET_V3:
Daniel Borkmann57ea8842017-01-05 02:34:28 +0100454 h.h3->tp_sec = ts.tv_sec;
455 h.h3->tp_nsec = ts.tv_nsec;
456 break;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000457 default:
458 WARN(1, "TPACKET version not supported.\n");
459 BUG();
460 }
461
462 /* one flush is safe, as both fields always lie on the same cacheline */
463 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
464 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000465
466 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000467}
468
Johann Baudy69e3c752009-05-18 22:11:22 -0700469static void *packet_lookup_frame(struct packet_sock *po,
470 struct packet_ring_buffer *rb,
471 unsigned int position,
472 int status)
473{
474 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000475 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700476
477 pg_vec_pos = position / rb->frames_per_block;
478 frame_offset = position % rb->frames_per_block;
479
Neil Horman0e3125c2010-11-16 10:26:47 -0800480 h.raw = rb->pg_vec[pg_vec_pos].buffer +
481 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700482
483 if (status != __packet_get_status(po, h.raw))
484 return NULL;
485
486 return h.raw;
487}
488
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000489static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700490 struct packet_ring_buffer *rb,
491 int status)
492{
493 return packet_lookup_frame(po, rb, rb->head, status);
494}
495
chetan lokebc59ba32011-08-25 10:43:30 +0000496static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000497{
498 del_timer_sync(&pkc->retire_blk_timer);
499}
500
501static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +0000502 struct sk_buff_head *rb_queue)
503{
chetan lokebc59ba32011-08-25 10:43:30 +0000504 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000505
Tobias Klauser73d0fcf2015-07-28 14:21:26 +0200506 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000507
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100508 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000509 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100510 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f12011-08-19 10:18:16 +0000511
512 prb_del_retire_blk_timer(pkc);
513}
514
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530515static void prb_setup_retire_blk_timer(struct packet_sock *po)
chetan lokef6fb8f12011-08-19 10:18:16 +0000516{
chetan lokebc59ba32011-08-25 10:43:30 +0000517 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f12011-08-19 10:18:16 +0000518
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530519 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
Kees Cook17bfd8c2017-10-24 01:46:26 -0700520 timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired,
521 0);
522 pkc->retire_blk_timer.expires = jiffies;
chetan lokef6fb8f12011-08-19 10:18:16 +0000523}
524
525static int prb_calc_retire_blk_tmo(struct packet_sock *po,
526 int blk_size_in_bytes)
527{
528 struct net_device *dev;
529 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800530 struct ethtool_link_ksettings ecmd;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000531 int err;
chetan lokef6fb8f12011-08-19 10:18:16 +0000532
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000533 rtnl_lock();
534 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
535 if (unlikely(!dev)) {
536 rtnl_unlock();
chetan lokef6fb8f12011-08-19 10:18:16 +0000537 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000538 }
David Decotigny7cad1ba2016-02-24 10:58:10 -0800539 err = __ethtool_get_link_ksettings(dev, &ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000540 rtnl_unlock();
541 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000542 /*
543 * If the link speed is so slow you don't really
544 * need to worry about perf anyways
545 */
David Decotigny7cad1ba2016-02-24 10:58:10 -0800546 if (ecmd.base.speed < SPEED_1000 ||
547 ecmd.base.speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000548 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000549 } else {
550 msec = 1;
David Decotigny7cad1ba2016-02-24 10:58:10 -0800551 div = ecmd.base.speed / 1000;
chetan lokef6fb8f12011-08-19 10:18:16 +0000552 }
553 }
554
555 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
556
557 if (div)
558 mbits /= div;
559
560 tmo = mbits * msec;
561
562 if (div)
563 return tmo+1;
564 return tmo;
565}
566
chetan lokebc59ba32011-08-25 10:43:30 +0000567static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000568 union tpacket_req_u *req_u)
569{
570 p1->feature_req_word = req_u->req3.tp_feature_req_word;
571}
572
573static void init_prb_bdqc(struct packet_sock *po,
574 struct packet_ring_buffer *rb,
575 struct pgv *pg_vec,
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530576 union tpacket_req_u *req_u)
chetan lokef6fb8f12011-08-19 10:18:16 +0000577{
Duan Jiong22781a52013-12-06 13:29:36 +0800578 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000579 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000580
581 memset(p1, 0x0, sizeof(*p1));
582
583 p1->knxt_seq_num = 1;
584 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000585 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000586 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f12011-08-19 10:18:16 +0000587 p1->kblk_size = req_u->req3.tp_block_size;
588 p1->knum_blocks = req_u->req3.tp_block_nr;
589 p1->hdrlen = po->tp_hdrlen;
590 p1->version = po->tp_version;
591 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000592 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000593 if (req_u->req3.tp_retire_blk_tov)
594 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
595 else
596 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
597 req_u->req3.tp_block_size);
598 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
599 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
600
Eric Dumazetdc808112014-08-15 09:16:04 -0700601 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000602 prb_init_ft_ops(p1, req_u);
Maninder Singhe8e85cc2015-06-22 12:39:16 +0530603 prb_setup_retire_blk_timer(po);
chetan lokef6fb8f12011-08-19 10:18:16 +0000604 prb_open_block(p1, pbd);
605}
606
607/* Do NOT update the last_blk_num first.
608 * Assumes sk_buff_head lock is held.
609 */
chetan lokebc59ba32011-08-25 10:43:30 +0000610static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000611{
612 mod_timer(&pkc->retire_blk_timer,
613 jiffies + pkc->tov_in_jiffies);
614 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
615}
616
617/*
618 * Timer logic:
619 * 1) We refresh the timer only when we open a block.
620 * By doing this we don't waste cycles refreshing the timer
621 * on packet-by-packet basis.
622 *
623 * With a 1MB block-size, on a 1Gbps line, it will take
624 * i) ~8 ms to fill a block + ii) memcpy etc.
625 * In this cut we are not accounting for the memcpy time.
626 *
627 * So, if the user sets the 'tmo' to 10ms then the timer
628 * will never fire while the block is still getting filled
629 * (which is what we want). However, the user could choose
630 * to close a block early and that's fine.
631 *
632 * But when the timer does fire, we check whether or not to refresh it.
633 * Since the tmo granularity is in msecs, it is not too expensive
634 * to refresh the timer, lets say every '8' msecs.
635 * Either the user can set the 'tmo' or we can derive it based on
636 * a) line-speed and b) block-size.
637 * prb_calc_retire_blk_tmo() calculates the tmo.
638 *
639 */
Kees Cook17bfd8c2017-10-24 01:46:26 -0700640static void prb_retire_rx_blk_timer_expired(struct timer_list *t)
chetan lokef6fb8f12011-08-19 10:18:16 +0000641{
Kees Cook17bfd8c2017-10-24 01:46:26 -0700642 struct packet_sock *po =
643 from_timer(po, t, rx_ring.prb_bdqc.retire_blk_timer);
Duan Jiong22781a52013-12-06 13:29:36 +0800644 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +0000645 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000646 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000647
648 spin_lock(&po->sk.sk_receive_queue.lock);
649
650 frozen = prb_queue_frozen(pkc);
651 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
652
653 if (unlikely(pkc->delete_blk_timer))
654 goto out;
655
656 /* We only need to plug the race when the block is partially filled.
657 * tpacket_rcv:
658 * lock(); increment BLOCK_NUM_PKTS; unlock()
659 * copy_bits() is in progress ...
660 * timer fires on other cpu:
661 * we can't retire the current block because copy_bits
662 * is in progress.
663 *
664 */
665 if (BLOCK_NUM_PKTS(pbd)) {
666 while (atomic_read(&pkc->blk_fill_in_prog)) {
667 /* Waiting for skb_copy_bits to finish... */
668 cpu_relax();
669 }
670 }
671
672 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
673 if (!frozen) {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300674 if (!BLOCK_NUM_PKTS(pbd)) {
675 /* An empty block. Just refresh the timer. */
676 goto refresh_timer;
677 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000678 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
679 if (!prb_dispatch_next_block(pkc, po))
680 goto refresh_timer;
681 else
682 goto out;
683 } else {
684 /* Case 1. Queue was frozen because user-space was
685 * lagging behind.
686 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300687 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +0000688 /*
689 * Ok, user-space is still behind.
690 * So just refresh the timer.
691 */
692 goto refresh_timer;
693 } else {
694 /* Case 2. queue was frozen,user-space caught up,
695 * now the link went idle && the timer fired.
696 * We don't have a block to close.So we open this
697 * block and restart the timer.
698 * opening a block thaws the queue,restarts timer
699 * Thawing/timer-refresh is a side effect.
700 */
701 prb_open_block(pkc, pbd);
702 goto out;
703 }
704 }
705 }
706
707refresh_timer:
708 _prb_refresh_rx_retire_blk_timer(pkc);
709
710out:
711 spin_unlock(&po->sk.sk_receive_queue.lock);
712}
713
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000714static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000715 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f12011-08-19 10:18:16 +0000716{
717 /* Flush everything minus the block header */
718
719#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
720 u8 *start, *end;
721
722 start = (u8 *)pbd1;
723
724 /* Skip the block header(we know header WILL fit in 4K) */
725 start += PAGE_SIZE;
726
727 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
728 for (; start < end; start += PAGE_SIZE)
729 flush_dcache_page(pgv_to_page(start));
730
731 smp_wmb();
732#endif
733
734 /* Now update the block status. */
735
736 BLOCK_STATUS(pbd1) = status;
737
738 /* Flush the block header */
739
740#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
741 start = (u8 *)pbd1;
742 flush_dcache_page(pgv_to_page(start));
743
744 smp_wmb();
745#endif
746}
747
748/*
749 * Side effect:
750 *
751 * 1) flush the block
752 * 2) Increment active_blk_num
753 *
754 * Note:We DONT refresh the timer on purpose.
755 * Because almost always the next block will be opened.
756 */
chetan lokebc59ba32011-08-25 10:43:30 +0000757static void prb_close_block(struct tpacket_kbdq_core *pkc1,
758 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f12011-08-19 10:18:16 +0000759 struct packet_sock *po, unsigned int stat)
760{
761 __u32 status = TP_STATUS_USER | stat;
762
763 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000764 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
Dan Collinsda413ee2014-12-19 16:49:25 +1300765 struct sock *sk = &po->sk;
chetan lokef6fb8f12011-08-19 10:18:16 +0000766
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000767 if (po->stats.stats3.tp_drops)
chetan lokef6fb8f12011-08-19 10:18:16 +0000768 status |= TP_STATUS_LOSING;
769
770 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
771 last_pkt->tp_next_offset = 0;
772
773 /* Get the ts of the last pkt */
774 if (BLOCK_NUM_PKTS(pbd1)) {
775 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
776 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
777 } else {
Alexander Drozdov41a50d62015-02-24 08:18:28 +0300778 /* Ok, we tmo'd - so get the current time.
779 *
780 * It shouldn't really happen as we don't close empty
781 * blocks. See prb_retire_rx_blk_timer_expired().
782 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000783 struct timespec ts;
784 getnstimeofday(&ts);
785 h1->ts_last_pkt.ts_sec = ts.tv_sec;
786 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
787 }
788
789 smp_wmb();
790
791 /* Flush the block */
792 prb_flush_block(pkc1, pbd1, status);
793
Dan Collinsda413ee2014-12-19 16:49:25 +1300794 sk->sk_data_ready(sk);
795
chetan lokef6fb8f12011-08-19 10:18:16 +0000796 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
797}
798
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000799static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000800{
801 pkc->reset_pending_on_curr_blk = 0;
802}
803
804/*
805 * Side effect of opening a block:
806 *
807 * 1) prb_queue is thawed.
808 * 2) retire_blk_timer is refreshed.
809 *
810 */
chetan lokebc59ba32011-08-25 10:43:30 +0000811static void prb_open_block(struct tpacket_kbdq_core *pkc1,
812 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f12011-08-19 10:18:16 +0000813{
814 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000815 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f12011-08-19 10:18:16 +0000816
817 smp_rmb();
818
Daniel Borkmann8da30562013-05-03 02:57:00 +0000819 /* We could have just memset this but we will lose the
820 * flexibility of making the priv area sticky
821 */
chetan lokef6fb8f12011-08-19 10:18:16 +0000822
Daniel Borkmann8da30562013-05-03 02:57:00 +0000823 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
824 BLOCK_NUM_PKTS(pbd1) = 0;
825 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f12011-08-19 10:18:16 +0000826
Daniel Borkmann8da30562013-05-03 02:57:00 +0000827 getnstimeofday(&ts);
chetan lokef6fb8f12011-08-19 10:18:16 +0000828
Daniel Borkmann8da30562013-05-03 02:57:00 +0000829 h1->ts_first_pkt.ts_sec = ts.tv_sec;
830 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f12011-08-19 10:18:16 +0000831
Daniel Borkmann8da30562013-05-03 02:57:00 +0000832 pkc1->pkblk_start = (char *)pbd1;
833 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
834
835 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
836 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
837
838 pbd1->version = pkc1->version;
839 pkc1->prev = pkc1->nxt_offset;
840 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
841
842 prb_thaw_queue(pkc1);
843 _prb_refresh_rx_retire_blk_timer(pkc1);
844
845 smp_wmb();
chetan lokef6fb8f12011-08-19 10:18:16 +0000846}
847
848/*
849 * Queue freeze logic:
850 * 1) Assume tp_block_nr = 8 blocks.
851 * 2) At time 't0', user opens Rx ring.
852 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
853 * 4) user-space is either sleeping or processing block '0'.
854 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
855 * it will close block-7,loop around and try to fill block '0'.
856 * call-flow:
857 * __packet_lookup_frame_in_block
858 * prb_retire_current_block()
859 * prb_dispatch_next_block()
860 * |->(BLOCK_STATUS == USER) evaluates to true
861 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
862 * 6) Now there are two cases:
863 * 6.1) Link goes idle right after the queue is frozen.
864 * But remember, the last open_block() refreshed the timer.
865 * When this timer expires,it will refresh itself so that we can
866 * re-open block-0 in near future.
867 * 6.2) Link is busy and keeps on receiving packets. This is a simple
868 * case and __packet_lookup_frame_in_block will check if block-0
869 * is free and can now be re-used.
870 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000871static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000872 struct packet_sock *po)
873{
874 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000875 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f12011-08-19 10:18:16 +0000876}
877
878#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
879
880/*
881 * If the next block is free then we will dispatch it
882 * and return a good offset.
883 * Else, we will freeze the queue.
884 * So, caller must check the return value.
885 */
chetan lokebc59ba32011-08-25 10:43:30 +0000886static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000887 struct packet_sock *po)
888{
chetan lokebc59ba32011-08-25 10:43:30 +0000889 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +0000890
891 smp_rmb();
892
893 /* 1. Get current block num */
894 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
895
896 /* 2. If this block is currently in_use then freeze the queue */
897 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
898 prb_freeze_queue(pkc, po);
899 return NULL;
900 }
901
902 /*
903 * 3.
904 * open this block and return the offset where the first packet
905 * needs to get stored.
906 */
907 prb_open_block(pkc, pbd);
908 return (void *)pkc->nxt_offset;
909}
910
chetan lokebc59ba32011-08-25 10:43:30 +0000911static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000912 struct packet_sock *po, unsigned int status)
913{
chetan lokebc59ba32011-08-25 10:43:30 +0000914 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f12011-08-19 10:18:16 +0000915
916 /* retire/close the current block */
917 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
918 /*
919 * Plug the case where copy_bits() is in progress on
920 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
921 * have space to copy the pkt in the current block and
922 * called prb_retire_current_block()
923 *
924 * We don't need to worry about the TMO case because
925 * the timer-handler already handled this case.
926 */
927 if (!(status & TP_STATUS_BLK_TMO)) {
928 while (atomic_read(&pkc->blk_fill_in_prog)) {
929 /* Waiting for skb_copy_bits to finish... */
930 cpu_relax();
931 }
932 }
933 prb_close_block(pkc, pbd, po, status);
934 return;
935 }
chetan lokef6fb8f12011-08-19 10:18:16 +0000936}
937
Rosen, Rami878cd3b2017-05-24 18:34:11 +0300938static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
chetan lokef6fb8f12011-08-19 10:18:16 +0000939{
940 return TP_STATUS_USER & BLOCK_STATUS(pbd);
941}
942
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000943static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f12011-08-19 10:18:16 +0000944{
945 return pkc->reset_pending_on_curr_blk;
946}
947
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000948static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +0000949{
chetan lokebc59ba32011-08-25 10:43:30 +0000950 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000951 atomic_dec(&pkc->blk_fill_in_prog);
952}
953
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000954static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000955 struct tpacket3_hdr *ppd)
956{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800957 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
chetan lokef6fb8f12011-08-19 10:18:16 +0000958}
959
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000960static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000961 struct tpacket3_hdr *ppd)
962{
963 ppd->hv1.tp_rxhash = 0;
964}
965
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000966static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000967 struct tpacket3_hdr *ppd)
968{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100969 if (skb_vlan_tag_present(pkc->skb)) {
970 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900971 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
972 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
chetan lokef6fb8f12011-08-19 10:18:16 +0000973 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000974 ppd->hv1.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900975 ppd->hv1.tp_vlan_tpid = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000976 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f12011-08-19 10:18:16 +0000977 }
978}
979
chetan lokebc59ba32011-08-25 10:43:30 +0000980static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f12011-08-19 10:18:16 +0000981 struct tpacket3_hdr *ppd)
982{
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +0900983 ppd->hv1.tp_padding = 0;
chetan lokef6fb8f12011-08-19 10:18:16 +0000984 prb_fill_vlan_info(pkc, ppd);
985
986 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
987 prb_fill_rxhash(pkc, ppd);
988 else
989 prb_clear_rxhash(pkc, ppd);
990}
991
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000992static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000993 struct tpacket_kbdq_core *pkc,
994 struct tpacket_block_desc *pbd,
chetan lokef6fb8f12011-08-19 10:18:16 +0000995 unsigned int len)
996{
997 struct tpacket3_hdr *ppd;
998
999 ppd = (struct tpacket3_hdr *)curr;
1000 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
1001 pkc->prev = curr;
1002 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
1003 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
1004 BLOCK_NUM_PKTS(pbd) += 1;
1005 atomic_inc(&pkc->blk_fill_in_prog);
1006 prb_run_all_ft_ops(pkc, ppd);
1007}
1008
1009/* Assumes caller has the sk->rx_queue.lock */
1010static void *__packet_lookup_frame_in_block(struct packet_sock *po,
1011 struct sk_buff *skb,
1012 int status,
1013 unsigned int len
1014 )
1015{
chetan lokebc59ba32011-08-25 10:43:30 +00001016 struct tpacket_kbdq_core *pkc;
1017 struct tpacket_block_desc *pbd;
chetan lokef6fb8f12011-08-19 10:18:16 +00001018 char *curr, *end;
1019
Joe Perchese3192692012-06-03 17:41:40 +00001020 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f12011-08-19 10:18:16 +00001021 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1022
1023 /* Queue is frozen when user space is lagging behind */
1024 if (prb_queue_frozen(pkc)) {
1025 /*
1026 * Check if that last block which caused the queue to freeze,
1027 * is still in_use by user-space.
1028 */
Rosen, Rami878cd3b2017-05-24 18:34:11 +03001029 if (prb_curr_blk_in_use(pbd)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00001030 /* Can't record this packet */
1031 return NULL;
1032 } else {
1033 /*
1034 * Ok, the block was released by user-space.
1035 * Now let's open that block.
1036 * opening a block also thaws the queue.
1037 * Thawing is a side effect.
1038 */
1039 prb_open_block(pkc, pbd);
1040 }
1041 }
1042
1043 smp_mb();
1044 curr = pkc->nxt_offset;
1045 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +00001046 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f12011-08-19 10:18:16 +00001047
1048 /* first try the current block */
1049 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
1050 prb_fill_curr_block(curr, pkc, pbd, len);
1051 return (void *)curr;
1052 }
1053
1054 /* Ok, close the current block */
1055 prb_retire_current_block(pkc, po, 0);
1056
1057 /* Now, try to dispatch the next block */
1058 curr = (char *)prb_dispatch_next_block(pkc, po);
1059 if (curr) {
1060 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
1061 prb_fill_curr_block(curr, pkc, pbd, len);
1062 return (void *)curr;
1063 }
1064
1065 /*
1066 * No free blocks are available.user_space hasn't caught up yet.
1067 * Queue was just frozen and now this packet will get dropped.
1068 */
1069 return NULL;
1070}
1071
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001072static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001073 struct sk_buff *skb,
1074 int status, unsigned int len)
1075{
1076 char *curr = NULL;
1077 switch (po->tp_version) {
1078 case TPACKET_V1:
1079 case TPACKET_V2:
1080 curr = packet_lookup_frame(po, &po->rx_ring,
1081 po->rx_ring.head, status);
1082 return curr;
1083 case TPACKET_V3:
1084 return __packet_lookup_frame_in_block(po, skb, status, len);
1085 default:
1086 WARN(1, "TPACKET version not supported\n");
1087 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001088 return NULL;
chetan lokef6fb8f12011-08-19 10:18:16 +00001089 }
1090}
1091
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001092static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001093 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001094 unsigned int idx,
chetan lokef6fb8f12011-08-19 10:18:16 +00001095 int status)
1096{
chetan lokebc59ba32011-08-25 10:43:30 +00001097 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001098 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f12011-08-19 10:18:16 +00001099
1100 if (status != BLOCK_STATUS(pbd))
1101 return NULL;
1102 return pbd;
1103}
1104
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001105static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f12011-08-19 10:18:16 +00001106{
1107 unsigned int prev;
1108 if (rb->prb_bdqc.kactive_blk_num)
1109 prev = rb->prb_bdqc.kactive_blk_num-1;
1110 else
1111 prev = rb->prb_bdqc.knum_blocks-1;
1112 return prev;
1113}
1114
1115/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001116static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001117 struct packet_ring_buffer *rb,
1118 int status)
1119{
1120 unsigned int previous = prb_previous_blk_num(rb);
1121 return prb_lookup_block(po, rb, previous, status);
1122}
1123
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001124static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001125 struct packet_ring_buffer *rb,
1126 int status)
1127{
1128 if (po->tp_version <= TPACKET_V2)
1129 return packet_previous_frame(po, rb, status);
1130
1131 return __prb_previous_block(po, rb, status);
1132}
1133
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001134static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f12011-08-19 10:18:16 +00001135 struct packet_ring_buffer *rb)
1136{
1137 switch (po->tp_version) {
1138 case TPACKET_V1:
1139 case TPACKET_V2:
1140 return packet_increment_head(rb);
1141 case TPACKET_V3:
1142 default:
1143 WARN(1, "TPACKET version not supported.\n");
1144 BUG();
1145 return;
1146 }
1147}
1148
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001149static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001150 struct packet_ring_buffer *rb,
1151 int status)
1152{
1153 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1154 return packet_lookup_frame(po, rb, previous, status);
1155}
1156
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001157static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001158{
1159 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1160}
1161
Daniel Borkmannb0138402014-01-15 16:25:36 +01001162static void packet_inc_pending(struct packet_ring_buffer *rb)
1163{
1164 this_cpu_inc(*rb->pending_refcnt);
1165}
1166
1167static void packet_dec_pending(struct packet_ring_buffer *rb)
1168{
1169 this_cpu_dec(*rb->pending_refcnt);
1170}
1171
1172static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
1173{
1174 unsigned int refcnt = 0;
1175 int cpu;
1176
1177 /* We don't use pending refcount in rx_ring. */
1178 if (rb->pending_refcnt == NULL)
1179 return 0;
1180
1181 for_each_possible_cpu(cpu)
1182 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
1183
1184 return refcnt;
1185}
1186
1187static int packet_alloc_pending(struct packet_sock *po)
1188{
1189 po->rx_ring.pending_refcnt = NULL;
1190
1191 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
1192 if (unlikely(po->tx_ring.pending_refcnt == NULL))
1193 return -ENOBUFS;
1194
1195 return 0;
1196}
1197
1198static void packet_free_pending(struct packet_sock *po)
1199{
1200 free_percpu(po->tx_ring.pending_refcnt);
1201}
1202
Willem de Bruijn99547292015-05-12 11:56:47 -04001203#define ROOM_POW_OFF 2
1204#define ROOM_NONE 0x0
1205#define ROOM_LOW 0x1
1206#define ROOM_NORMAL 0x2
1207
1208static bool __tpacket_has_room(struct packet_sock *po, int pow_off)
1209{
1210 int idx, len;
1211
1212 len = po->rx_ring.frame_max + 1;
1213 idx = po->rx_ring.head;
1214 if (pow_off)
1215 idx += len >> pow_off;
1216 if (idx >= len)
1217 idx -= len;
1218 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1219}
1220
1221static bool __tpacket_v3_has_room(struct packet_sock *po, int pow_off)
1222{
1223 int idx, len;
1224
1225 len = po->rx_ring.prb_bdqc.knum_blocks;
1226 idx = po->rx_ring.prb_bdqc.kactive_blk_num;
1227 if (pow_off)
1228 idx += len >> pow_off;
1229 if (idx >= len)
1230 idx -= len;
1231 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
1232}
1233
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001234static int __packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001235{
1236 struct sock *sk = &po->sk;
Willem de Bruijn99547292015-05-12 11:56:47 -04001237 int ret = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001238
Willem de Bruijn99547292015-05-12 11:56:47 -04001239 if (po->prot_hook.func != tpacket_rcv) {
1240 int avail = sk->sk_rcvbuf - atomic_read(&sk->sk_rmem_alloc)
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001241 - (skb ? skb->truesize : 0);
Willem de Bruijn99547292015-05-12 11:56:47 -04001242 if (avail > (sk->sk_rcvbuf >> ROOM_POW_OFF))
1243 return ROOM_NORMAL;
1244 else if (avail > 0)
1245 return ROOM_LOW;
1246 else
1247 return ROOM_NONE;
1248 }
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001249
Willem de Bruijn99547292015-05-12 11:56:47 -04001250 if (po->tp_version == TPACKET_V3) {
1251 if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
1252 ret = ROOM_NORMAL;
1253 else if (__tpacket_v3_has_room(po, 0))
1254 ret = ROOM_LOW;
1255 } else {
1256 if (__tpacket_has_room(po, ROOM_POW_OFF))
1257 ret = ROOM_NORMAL;
1258 else if (__tpacket_has_room(po, 0))
1259 ret = ROOM_LOW;
1260 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001261
1262 return ret;
1263}
1264
1265static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1266{
1267 int ret;
1268 bool has_room;
1269
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001270 spin_lock_bh(&po->sk.sk_receive_queue.lock);
1271 ret = __packet_rcv_has_room(po, skb);
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001272 has_room = ret == ROOM_NORMAL;
1273 if (po->pressure == has_room)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04001274 po->pressure = !has_room;
1275 spin_unlock_bh(&po->sk.sk_receive_queue.lock);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001276
Willem de Bruijn99547292015-05-12 11:56:47 -04001277 return ret;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static void packet_sock_destruct(struct sock *sk)
1281{
Richard Cochraned85b562010-04-07 22:41:28 +00001282 skb_queue_purge(&sk->sk_error_queue);
1283
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001284 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
Reshetova, Elena14afee42017-06-30 13:08:00 +03001285 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001288 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return;
1290 }
1291
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001292 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001295static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
1296{
1297 u32 rxhash;
1298 int i, count = 0;
1299
1300 rxhash = skb_get_hash(skb);
1301 for (i = 0; i < ROLLOVER_HLEN; i++)
1302 if (po->rollover->history[i] == rxhash)
1303 count++;
1304
1305 po->rollover->history[prandom_u32() % ROLLOVER_HLEN] = rxhash;
1306 return count > (ROLLOVER_HLEN >> 1);
1307}
1308
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001309static unsigned int fanout_demux_hash(struct packet_fanout *f,
1310 struct sk_buff *skb,
1311 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001312{
David S. Millereb70db82016-07-01 16:07:50 -04001313 return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
David S. Millerdc99f602011-07-05 01:45:05 -07001314}
1315
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001316static unsigned int fanout_demux_lb(struct packet_fanout *f,
1317 struct sk_buff *skb,
1318 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001319{
Willem de Bruijn468479e2015-06-17 15:59:34 -04001320 unsigned int val = atomic_inc_return(&f->rr_cur);
David S. Millerdc99f602011-07-05 01:45:05 -07001321
Willem de Bruijn468479e2015-06-17 15:59:34 -04001322 return val % num;
David S. Millerdc99f602011-07-05 01:45:05 -07001323}
1324
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001325static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1326 struct sk_buff *skb,
1327 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001328{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001329 return smp_processor_id() % num;
1330}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001331
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001332static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1333 struct sk_buff *skb,
1334 unsigned int num)
1335{
Daniel Borkmannf337db62014-01-22 02:29:39 +01001336 return prandom_u32_max(num);
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001337}
1338
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001339static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1340 struct sk_buff *skb,
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001341 unsigned int idx, bool try_self,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001342 unsigned int num)
1343{
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001344 struct packet_sock *po, *po_next, *po_skip = NULL;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001345 unsigned int i, j, room = ROOM_NONE;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001346
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001347 po = pkt_sk(f->arr[idx]);
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001348
1349 if (try_self) {
1350 room = packet_rcv_has_room(po, skb);
1351 if (room == ROOM_NORMAL ||
1352 (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
1353 return idx;
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001354 po_skip = po;
Willem de Bruijn3b3a5b02015-05-12 11:56:49 -04001355 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001356
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001357 i = j = min_t(int, po->rollover->sock, num - 1);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001358 do {
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001359 po_next = pkt_sk(f->arr[i]);
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001360 if (po_next != po_skip && !po_next->pressure &&
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04001361 packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001362 if (i != j)
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001363 po->rollover->sock = i;
Willem de Bruijna9b63912015-05-12 11:56:50 -04001364 atomic_long_inc(&po->rollover->num);
1365 if (room == ROOM_LOW)
1366 atomic_long_inc(&po->rollover->num_huge);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001367 return i;
1368 }
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001369
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001370 if (++i == num)
1371 i = 0;
1372 } while (i != j);
1373
Willem de Bruijna9b63912015-05-12 11:56:50 -04001374 atomic_long_inc(&po->rollover->num_failed);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001375 return idx;
1376}
1377
Neil Horman2d360972014-01-22 16:01:44 -05001378static unsigned int fanout_demux_qm(struct packet_fanout *f,
1379 struct sk_buff *skb,
1380 unsigned int num)
1381{
1382 return skb_get_queue_mapping(skb) % num;
1383}
1384
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001385static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1386 struct sk_buff *skb,
1387 unsigned int num)
1388{
1389 struct bpf_prog *prog;
1390 unsigned int ret = 0;
1391
1392 rcu_read_lock();
1393 prog = rcu_dereference(f->bpf_prog);
1394 if (prog)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001395 ret = bpf_prog_run_clear_cb(prog, skb) % num;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001396 rcu_read_unlock();
1397
1398 return ret;
1399}
1400
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001401static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1402{
1403 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001404}
1405
David S. Miller95ec3eb2011-07-06 01:56:38 -07001406static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1407 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001408{
1409 struct packet_fanout *f = pt->af_packet_priv;
Eric Dumazetf98f4512015-06-16 07:59:11 -07001410 unsigned int num = READ_ONCE(f->num_members);
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001411 struct net *net = read_pnet(&f->net);
David S. Millerdc99f602011-07-05 01:45:05 -07001412 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001413 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001414
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001415 if (!net_eq(dev_net(dev), net) || !num) {
David S. Millerdc99f602011-07-05 01:45:05 -07001416 kfree_skb(skb);
1417 return 0;
1418 }
1419
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001420 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -05001421 skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
Alexander Drozdov3f34b242015-02-20 08:24:27 +03001422 if (!skb)
1423 return 0;
1424 }
David S. Miller95ec3eb2011-07-06 01:56:38 -07001425 switch (f->type) {
1426 case PACKET_FANOUT_HASH:
1427 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001428 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001429 break;
1430 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001431 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001432 break;
1433 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001434 idx = fanout_demux_cpu(f, skb, num);
1435 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001436 case PACKET_FANOUT_RND:
1437 idx = fanout_demux_rnd(f, skb, num);
1438 break;
Neil Horman2d360972014-01-22 16:01:44 -05001439 case PACKET_FANOUT_QM:
1440 idx = fanout_demux_qm(f, skb, num);
1441 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001442 case PACKET_FANOUT_ROLLOVER:
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001443 idx = fanout_demux_rollover(f, skb, 0, false, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001444 break;
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001445 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001446 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001447 idx = fanout_demux_bpf(f, skb, num);
1448 break;
David S. Miller7736d332011-07-05 01:43:20 -07001449 }
1450
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001451 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
1452 idx = fanout_demux_rollover(f, skb, idx, true, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001453
Willem de Bruijnad377ca2015-05-12 11:56:45 -04001454 po = pkt_sk(f->arr[idx]);
David S. Millerdc99f602011-07-05 01:45:05 -07001455 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1456}
1457
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001458DEFINE_MUTEX(fanout_mutex);
1459EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001460static LIST_HEAD(fanout_list);
Mike Maloney4a69a862017-04-21 10:56:11 -04001461static u16 fanout_next_id;
David S. Millerdc99f602011-07-05 01:45:05 -07001462
1463static void __fanout_link(struct sock *sk, struct packet_sock *po)
1464{
1465 struct packet_fanout *f = po->fanout;
1466
1467 spin_lock(&f->lock);
1468 f->arr[f->num_members] = sk;
1469 smp_wmb();
1470 f->num_members++;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001471 if (f->num_members == 1)
1472 dev_add_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001473 spin_unlock(&f->lock);
1474}
1475
1476static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1477{
1478 struct packet_fanout *f = po->fanout;
1479 int i;
1480
1481 spin_lock(&f->lock);
1482 for (i = 0; i < f->num_members; i++) {
1483 if (f->arr[i] == sk)
1484 break;
1485 }
1486 BUG_ON(i >= f->num_members);
1487 f->arr[i] = f->arr[f->num_members - 1];
1488 f->num_members--;
Anoob Soman2bd624b2017-02-15 20:25:39 +00001489 if (f->num_members == 0)
1490 __dev_remove_pack(&f->prot_hook);
David S. Millerdc99f602011-07-05 01:45:05 -07001491 spin_unlock(&f->lock);
1492}
1493
Weilong Chend4dd8ae2013-12-23 11:31:38 +08001494static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001495{
Eric Dumazet161642e2015-10-09 11:29:32 -07001496 if (sk->sk_family != PF_PACKET)
1497 return false;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001498
Eric Dumazet161642e2015-10-09 11:29:32 -07001499 return ptype->af_packet_priv == pkt_sk(sk)->fanout;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001500}
1501
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001502static void fanout_init_data(struct packet_fanout *f)
1503{
1504 switch (f->type) {
1505 case PACKET_FANOUT_LB:
1506 atomic_set(&f->rr_cur, 0);
1507 break;
1508 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001509 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001510 RCU_INIT_POINTER(f->bpf_prog, NULL);
1511 break;
1512 }
1513}
1514
1515static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1516{
1517 struct bpf_prog *old;
1518
1519 spin_lock(&f->lock);
1520 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1521 rcu_assign_pointer(f->bpf_prog, new);
1522 spin_unlock(&f->lock);
1523
1524 if (old) {
1525 synchronize_net();
1526 bpf_prog_destroy(old);
1527 }
1528}
1529
1530static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1531 unsigned int len)
1532{
1533 struct bpf_prog *new;
1534 struct sock_fprog fprog;
1535 int ret;
1536
1537 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1538 return -EPERM;
1539 if (len != sizeof(fprog))
1540 return -EINVAL;
1541 if (copy_from_user(&fprog, data, len))
1542 return -EFAULT;
1543
Daniel Borkmannbab18992015-10-02 15:17:33 +02001544 ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001545 if (ret)
1546 return ret;
1547
1548 __fanout_set_data_bpf(po->fanout, new);
1549 return 0;
1550}
1551
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001552static int fanout_set_data_ebpf(struct packet_sock *po, char __user *data,
1553 unsigned int len)
1554{
1555 struct bpf_prog *new;
1556 u32 fd;
1557
1558 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1559 return -EPERM;
1560 if (len != sizeof(fd))
1561 return -EINVAL;
1562 if (copy_from_user(&fd, data, len))
1563 return -EFAULT;
1564
Daniel Borkmann113214b2016-06-30 17:24:44 +02001565 new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001566 if (IS_ERR(new))
1567 return PTR_ERR(new);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001568
1569 __fanout_set_data_bpf(po->fanout, new);
1570 return 0;
1571}
1572
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001573static int fanout_set_data(struct packet_sock *po, char __user *data,
1574 unsigned int len)
1575{
1576 switch (po->fanout->type) {
1577 case PACKET_FANOUT_CBPF:
1578 return fanout_set_data_cbpf(po, data, len);
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001579 case PACKET_FANOUT_EBPF:
1580 return fanout_set_data_ebpf(po, data, len);
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001581 default:
1582 return -EINVAL;
zhong jiang07d53ae2018-08-04 19:41:41 +08001583 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001584}
1585
1586static void fanout_release_data(struct packet_fanout *f)
1587{
1588 switch (f->type) {
1589 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001590 case PACKET_FANOUT_EBPF:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001591 __fanout_set_data_bpf(f, NULL);
zhong jiang07d53ae2018-08-04 19:41:41 +08001592 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001593}
1594
Mike Maloney4a69a862017-04-21 10:56:11 -04001595static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
1596{
1597 struct packet_fanout *f;
1598
1599 list_for_each_entry(f, &fanout_list, list) {
1600 if (f->id == candidate_id &&
1601 read_pnet(&f->net) == sock_net(sk)) {
1602 return false;
1603 }
1604 }
1605 return true;
1606}
1607
1608static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
1609{
1610 u16 id = fanout_next_id;
1611
1612 do {
1613 if (__fanout_id_is_free(sk, id)) {
1614 *new_id = id;
1615 fanout_next_id = id + 1;
1616 return true;
1617 }
1618
1619 id++;
1620 } while (id != fanout_next_id);
1621
1622 return false;
1623}
1624
David S. Miller7736d332011-07-05 01:43:20 -07001625static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001626{
Eric Dumazetd199fab2017-02-14 09:03:51 -08001627 struct packet_rollover *rollover = NULL;
David S. Millerdc99f602011-07-05 01:45:05 -07001628 struct packet_sock *po = pkt_sk(sk);
1629 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001630 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001631 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001632 int err;
1633
1634 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001635 case PACKET_FANOUT_ROLLOVER:
1636 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1637 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001638 case PACKET_FANOUT_HASH:
1639 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001640 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001641 case PACKET_FANOUT_RND:
Neil Horman2d360972014-01-22 16:01:44 -05001642 case PACKET_FANOUT_QM:
Willem de Bruijn47dceb82015-08-14 22:31:34 -04001643 case PACKET_FANOUT_CBPF:
Willem de Bruijnf2e52092015-08-14 22:31:35 -04001644 case PACKET_FANOUT_EBPF:
David S. Millerdc99f602011-07-05 01:45:05 -07001645 break;
1646 default:
1647 return -EINVAL;
1648 }
1649
Eric Dumazetd199fab2017-02-14 09:03:51 -08001650 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001651
Eric Dumazetd199fab2017-02-14 09:03:51 -08001652 err = -EALREADY;
David S. Millerdc99f602011-07-05 01:45:05 -07001653 if (po->fanout)
Eric Dumazetd199fab2017-02-14 09:03:51 -08001654 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001655
Willem de Bruijn4633c9e2015-05-17 19:44:02 -04001656 if (type == PACKET_FANOUT_ROLLOVER ||
1657 (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
Eric Dumazetd199fab2017-02-14 09:03:51 -08001658 err = -ENOMEM;
1659 rollover = kzalloc(sizeof(*rollover), GFP_KERNEL);
1660 if (!rollover)
1661 goto out;
1662 atomic_long_set(&rollover->num, 0);
1663 atomic_long_set(&rollover->num_huge, 0);
1664 atomic_long_set(&rollover->num_failed, 0);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04001665 }
1666
Mike Maloney4a69a862017-04-21 10:56:11 -04001667 if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
1668 if (id != 0) {
1669 err = -EINVAL;
1670 goto out;
1671 }
1672 if (!fanout_find_new_id(sk, &id)) {
1673 err = -ENOMEM;
1674 goto out;
1675 }
1676 /* ephemeral flag for the first socket in the group: drop it */
1677 flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
1678 }
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, Elenafb5c2c12017-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 Bruijn008ba2a2017-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, Elenafb5c2c12017-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 Maloney57f015f2017-11-28 10:44:29 -05001722 po->rollover = rollover;
1723 rollover = NULL;
Reshetova, Elenafb5c2c12017-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 Bruijn008ba2a2017-09-14 17:14:41 -04001729 spin_unlock(&po->bind_lock);
1730
1731 if (err && !refcount_read(&match->sk_ref)) {
1732 list_del(&match->list);
1733 kfree(match);
1734 }
1735
Eric Dumazetafe62c62011-07-07 06:41:29 -07001736out:
Mike Maloney57f015f2017-11-28 10:44:29 -05001737 kfree(rollover);
Eric Dumazetd199fab2017-02-14 09:03:51 -08001738 mutex_unlock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001739 return err;
1740}
1741
Anoob Soman2bd624b2017-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 Dumazetd199fab2017-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, Elenafb5c2c12017-06-30 13:08:10 +03001757 if (refcount_dec_and_test(&f->sk_ref))
Eric Dumazetd199fab2017-02-14 09:03:51 -08001758 list_del(&f->list);
Anoob Soman2bd624b2017-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 Soman2bd624b2017-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
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001851static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
1852{
Yoshiki Komachi18bed892019-03-18 14:39:52 +09001853 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
1854 sock->type == SOCK_RAW) {
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001855 skb_reset_mac_header(skb);
1856 skb->protocol = dev_parse_header_protocol(skb);
1857 }
1858
1859 skb_probe_transport_header(skb);
1860}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862/*
1863 * Output a raw packet to a device layer. This bypasses all the other
1864 * protocol layers and you must therefore supply it with a complete frame
1865 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001866
Ying Xue1b784142015-03-02 15:37:48 +08001867static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1868 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001871 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001872 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct net_device *dev;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001874 struct sockcm_cookie sockc;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001875 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001877 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001880 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
1882
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001883 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001885 return -EINVAL;
1886 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1887 proto = saddr->spkt_protocol;
1888 } else
1889 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001892 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
1894
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001895 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001896retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001897 rcu_read_lock();
1898 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 err = -ENODEV;
1900 if (dev == NULL)
1901 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001902
David S. Millerd5e76b02007-01-25 19:30:36 -08001903 err = -ENETDOWN;
1904 if (!(dev->flags & IFF_UP))
1905 goto out_unlock;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001908 * You may not queue a frame bigger than the mtu. This is the lowest level
1909 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001911
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001912 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1913 if (!netif_supports_nofcs(dev)) {
1914 err = -EPROTONOSUPPORT;
1915 goto out_unlock;
1916 }
1917 extra_len = 4; /* We're doing our own CRC */
1918 }
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001921 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 goto out_unlock;
1923
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001924 if (!skb) {
1925 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001926 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001927 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001929 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001930 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001931 if (skb == NULL)
1932 return -ENOBUFS;
1933 /* FIXME: Save some space for broken drivers that write a hard
1934 * header at transmission time by themselves. PPP is the notable
1935 * one here. This should really be fixed at the driver level.
1936 */
1937 skb_reserve(skb, reserved);
1938 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001939
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001940 /* Try to align data part correctly */
1941 if (hhlen) {
1942 skb->data -= hhlen;
1943 skb->tail -= hhlen;
1944 if (len < hhlen)
1945 skb_reset_network_header(skb);
1946 }
Al Viro6ce8e9c2014-04-06 21:25:44 -04001947 err = memcpy_from_msg(skb_put(skb, len), msg, len);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001948 if (err)
1949 goto out_free;
1950 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05001953 if (!dev_validate_header(dev, skb->data, len)) {
1954 err = -EINVAL;
1955 goto out_unlock;
1956 }
Daniel Borkmann3c70c132015-11-11 23:25:42 +01001957 if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
1958 !packet_extra_vlan_len_allowed(dev, skb)) {
1959 err = -EMSGSIZE;
1960 goto out_unlock;
Ben Greear57f89bf2011-02-11 09:35:18 +00001961 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001962
Willem de Bruijn657a0662018-07-06 10:12:56 -04001963 sockcm_init(&sockc, sk);
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001964 if (msg->msg_controllen) {
1965 err = sock_cmsg_send(sk, msg, &sockc);
Soheil Hassas Yeganehf8e77182016-07-20 18:01:18 -04001966 if (unlikely(err))
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001967 goto out_unlock;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001968 }
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 skb->protocol = proto;
1971 skb->dev = dev;
1972 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001973 skb->mark = sk->sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07001974 skb->tstamp = sockc.transmit_time;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00001975
Willem de Bruijn8f932f72018-12-17 12:24:00 -05001976 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001978 if (unlikely(extra_len == 4))
1979 skb->no_fcs = 1;
1980
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00001981 packet_parse_headers(skb, sock);
Jason Wangc1aad272013-03-25 20:19:57 +00001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001984 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001985 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001988 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001989out_free:
1990 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return err;
1992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001994static unsigned int run_filter(struct sk_buff *skb,
1995 const struct sock *sk,
1996 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct sk_filter *filter;
1999
Eric Dumazet80f8f102011-01-18 07:46:52 +00002000 rcu_read_lock();
2001 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08002002 if (filter != NULL)
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002003 res = bpf_prog_run_clear_cb(filter->prog, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00002004 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
David S. Millerdbcb5852007-01-24 15:21:02 -08002006 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002009static 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
Willem de Bruijnfd3a8862018-06-06 11:23:01 -04002018 if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002019 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 lokef6fb8f12011-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 Poirieredbd58b2017-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 Poirieredbd58b2017-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 Poirieredbd58b2017-08-28 14:29:41 -04002222 do_vnet = true;
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 macoff = netoff - maclen;
2225 }
chetan lokef6fb8f12011-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 lokef6fb8f12011-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 lokef6fb8f12011-08-19 10:18:16 +00002239 snaplen = po->rx_ring.frame_size - macoff;
Benjamin Poirieredbd58b2017-08-28 14:29:41 -04002240 if ((int)snaplen < 0) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002241 snaplen = 0;
Benjamin Poirieredbd58b2017-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 Poirieredbd58b2017-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 lokef6fb8f12011-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 lokef6fb8f12011-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 lokef6fb8f12011-08-19 10:18:16 +00002273 status |= TP_STATUS_LOSING;
2274 }
Eric Dumazet945d0152018-06-21 14:16:02 -07002275
2276 if (do_vnet &&
2277 virtio_net_hdr_from_skb(skb, h.raw + macoff -
2278 sizeof(struct virtio_net_hdr),
2279 vio_le(), true, 0))
2280 goto drop_n_account;
2281
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002282 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (copy_skb) {
2284 status |= TP_STATUS_COPY;
2285 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 spin_unlock(&sk->sk_receive_queue.lock);
2288
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002289 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002290
2291 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Daniel Borkmann7a513842013-04-23 00:39:29 +00002292 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002294 status |= ts_status;
2295
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002296 switch (po->tp_version) {
2297 case TPACKET_V1:
2298 h.h1->tp_len = skb->len;
2299 h.h1->tp_snaplen = snaplen;
2300 h.h1->tp_mac = macoff;
2301 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00002302 h.h1->tp_sec = ts.tv_sec;
2303 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002304 hdrlen = sizeof(*h.h1);
2305 break;
2306 case TPACKET_V2:
2307 h.h2->tp_len = skb->len;
2308 h.h2->tp_snaplen = snaplen;
2309 h.h2->tp_mac = macoff;
2310 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002311 h.h2->tp_sec = ts.tv_sec;
2312 h.h2->tp_nsec = ts.tv_nsec;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002313 if (skb_vlan_tag_present(skb)) {
2314 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002315 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
2316 status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00002317 } else {
2318 h.h2->tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09002319 h.h2->tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00002320 }
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002321 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002322 hdrlen = sizeof(*h.h2);
2323 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00002324 case TPACKET_V3:
2325 /* tp_nxt_offset,vlan are already populated above.
2326 * So DONT clear those fields here
2327 */
2328 h.h3->tp_status |= status;
2329 h.h3->tp_len = skb->len;
2330 h.h3->tp_snaplen = snaplen;
2331 h.h3->tp_mac = macoff;
2332 h.h3->tp_net = netoff;
chetan lokef6fb8f12011-08-19 10:18:16 +00002333 h.h3->tp_sec = ts.tv_sec;
2334 h.h3->tp_nsec = ts.tv_nsec;
Atzm Watanabee4d26f42013-12-17 22:53:36 +09002335 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
chetan lokef6fb8f12011-08-19 10:18:16 +00002336 hdrlen = sizeof(*h.h3);
2337 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002338 default:
2339 BUG();
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002342 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07002343 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 sll->sll_family = AF_PACKET;
2345 sll->sll_hatype = dev->type;
2346 sll->sll_protocol = skb->protocol;
2347 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08002348 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002349 sll->sll_ifindex = orig_dev->ifindex;
2350 else
2351 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Ralf Baechlee16aa202006-12-07 00:11:33 -08002353 smp_mb();
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002354
Changli Gaof6dafa92010-12-07 04:26:16 +00002355#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002356 if (po->tp_version <= TPACKET_V2) {
Changli Gao0af55bb2010-12-01 02:52:20 +00002357 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002359 end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
2360 macoff + snaplen);
2361
2362 for (start = h.raw; start < end; start += PAGE_SIZE)
2363 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002365 smp_wmb();
Changli Gaof6dafa92010-12-07 04:26:16 +00002366#endif
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002367
Dan Collinsda413ee2014-12-19 16:49:25 +13002368 if (po->tp_version <= TPACKET_V2) {
chetan lokef6fb8f12011-08-19 10:18:16 +00002369 __packet_set_status(po, h.raw, status);
Dan Collinsda413ee2014-12-19 16:49:25 +13002370 sk->sk_data_ready(sk);
2371 } else {
chetan lokef6fb8f12011-08-19 10:18:16 +00002372 prb_clear_blk_fill_status(&po->rx_ring);
Dan Collinsda413ee2014-12-19 16:49:25 +13002373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375drop_n_restore:
2376 if (skb_head != skb->data && skb_shared(skb)) {
2377 skb->data = skb_head;
2378 skb->len = skb_len;
2379 }
2380drop:
Weongyo Jeongda378452016-04-14 14:10:04 -07002381 if (!is_drop_n_account)
2382 consume_skb(skb);
2383 else
2384 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return 0;
2386
Willem de Bruijn58d19b12016-02-03 18:02:15 -05002387drop_n_account:
Weongyo Jeongda378452016-04-14 14:10:04 -07002388 is_drop_n_account = true;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00002389 po->stats.stats1.tp_drops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 spin_unlock(&sk->sk_receive_queue.lock);
2391
David S. Miller676d2362014-04-11 16:15:36 -04002392 sk->sk_data_ready(sk);
Wei Yongjunacb5d752009-02-25 00:36:42 +00002393 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto drop_n_restore;
2395}
2396
Johann Baudy69e3c752009-05-18 22:11:22 -07002397static void tpacket_destruct_skb(struct sk_buff *skb)
2398{
2399 struct packet_sock *po = pkt_sk(skb->sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002400
Johann Baudy69e3c752009-05-18 22:11:22 -07002401 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01002402 void *ph;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002403 __u32 ts;
2404
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002405 ph = skb_zcopy_get_nouarg(skb);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002406 packet_dec_pending(&po->tx_ring);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00002407
2408 ts = __packet_set_timestamp(po, ph, skb);
2409 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Johann Baudy69e3c752009-05-18 22:11:22 -07002410 }
2411
2412 sock_wfree(skb);
2413}
2414
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002415static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
2416{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002417 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2418 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2419 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
2420 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
2421 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
2422 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
2423 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
2424
2425 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
2426 return -EINVAL;
2427
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002428 return 0;
2429}
2430
2431static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
2432 struct virtio_net_hdr *vnet_hdr)
2433{
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002434 if (*len < sizeof(*vnet_hdr))
2435 return -EINVAL;
2436 *len -= sizeof(*vnet_hdr);
2437
Al Virocbbd26b2016-11-01 22:09:04 -04002438 if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002439 return -EFAULT;
2440
2441 return __packet_snd_vnet_parse(vnet_hdr, *len);
2442}
2443
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002444static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002445 void *frame, struct net_device *dev, void *data, int tp_len,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002446 __be16 proto, unsigned char *addr, int hlen, int copylen,
2447 const struct sockcm_cookie *sockc)
Johann Baudy69e3c752009-05-18 22:11:22 -07002448{
Daniel Borkmann184f4892013-04-16 01:57:46 +00002449 union tpacket_uhdr ph;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002450 int to_write, offset, len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07002451 struct socket *sock = po->sk.sk_socket;
2452 struct page *page;
Johann Baudy69e3c752009-05-18 22:11:22 -07002453 int err;
2454
2455 ph.raw = frame;
2456
2457 skb->protocol = proto;
2458 skb->dev = dev;
2459 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002460 skb->mark = po->sk.sk_mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002461 skb->tstamp = sockc->transmit_time;
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002462 skb_setup_tx_timestamp(skb, sockc->tsflags);
Willem de Bruijn5cd8d462018-11-20 13:00:18 -05002463 skb_zcopy_set_nouarg(skb, ph.raw);
Johann Baudy69e3c752009-05-18 22:11:22 -07002464
Herbert Xuae641942011-11-18 02:20:04 +00002465 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002466 skb_reset_network_header(skb);
Jason Wangc1aad272013-03-25 20:19:57 +00002467
Johann Baudy69e3c752009-05-18 22:11:22 -07002468 to_write = tp_len;
2469
2470 if (sock->type == SOCK_DGRAM) {
2471 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2472 NULL, tp_len);
2473 if (unlikely(err < 0))
2474 return -EINVAL;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002475 } else if (copylen) {
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002476 int hdrlen = min_t(int, copylen, tp_len);
2477
Johann Baudy69e3c752009-05-18 22:11:22 -07002478 skb_push(skb, dev->hard_header_len);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002479 skb_put(skb, copylen - dev->hard_header_len);
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002480 err = skb_store_bits(skb, 0, data, hdrlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002481 if (unlikely(err))
2482 return err;
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002483 if (!dev_validate_header(dev, skb->data, hdrlen))
2484 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002485
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002486 data += hdrlen;
2487 to_write -= hdrlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002488 }
2489
Johann Baudy69e3c752009-05-18 22:11:22 -07002490 offset = offset_in_page(data);
2491 len_max = PAGE_SIZE - offset;
2492 len = ((to_write > len_max) ? len_max : to_write);
2493
2494 skb->data_len = to_write;
2495 skb->len += to_write;
2496 skb->truesize += to_write;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002497 refcount_add(to_write, &po->sk.sk_wmem_alloc);
Johann Baudy69e3c752009-05-18 22:11:22 -07002498
2499 while (likely(to_write)) {
2500 nr_frags = skb_shinfo(skb)->nr_frags;
2501
2502 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002503 pr_err("Packet exceed the number of skb frags(%lu)\n",
2504 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002505 return -EFAULT;
2506 }
2507
Changli Gao0af55bb2010-12-01 02:52:20 +00002508 page = pgv_to_page(data);
2509 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002510 flush_dcache_page(page);
2511 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002512 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002513 to_write -= len;
2514 offset = 0;
2515 len_max = PAGE_SIZE;
2516 len = ((to_write > len_max) ? len_max : to_write);
2517 }
2518
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002519 packet_parse_headers(skb, sock);
Daniel Borkmannefdfa2f2015-11-11 23:25:40 +01002520
Johann Baudy69e3c752009-05-18 22:11:22 -07002521 return tp_len;
2522}
2523
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002524static int tpacket_parse_header(struct packet_sock *po, void *frame,
2525 int size_max, void **data)
2526{
2527 union tpacket_uhdr ph;
2528 int tp_len, off;
2529
2530 ph.raw = frame;
2531
2532 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002533 case TPACKET_V3:
2534 if (ph.h3->tp_next_offset != 0) {
2535 pr_warn_once("variable sized slot not supported");
2536 return -EINVAL;
2537 }
2538 tp_len = ph.h3->tp_len;
2539 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002540 case TPACKET_V2:
2541 tp_len = ph.h2->tp_len;
2542 break;
2543 default:
2544 tp_len = ph.h1->tp_len;
2545 break;
2546 }
2547 if (unlikely(tp_len > size_max)) {
2548 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
2549 return -EMSGSIZE;
2550 }
2551
2552 if (unlikely(po->tp_tx_has_off)) {
2553 int off_min, off_max;
2554
2555 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2556 off_max = po->tx_ring.frame_size - tp_len;
2557 if (po->sk.sk_type == SOCK_DGRAM) {
2558 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002559 case TPACKET_V3:
2560 off = ph.h3->tp_net;
2561 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002562 case TPACKET_V2:
2563 off = ph.h2->tp_net;
2564 break;
2565 default:
2566 off = ph.h1->tp_net;
2567 break;
2568 }
2569 } else {
2570 switch (po->tp_version) {
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08002571 case TPACKET_V3:
2572 off = ph.h3->tp_mac;
2573 break;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002574 case TPACKET_V2:
2575 off = ph.h2->tp_mac;
2576 break;
2577 default:
2578 off = ph.h1->tp_mac;
2579 break;
2580 }
2581 }
2582 if (unlikely((off < off_min) || (off_max < off)))
2583 return -EINVAL;
2584 } else {
2585 off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
2586 }
2587
2588 *data = frame + off;
2589 return tp_len;
2590}
2591
Johann Baudy69e3c752009-05-18 22:11:22 -07002592static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2593{
Johann Baudy69e3c752009-05-18 22:11:22 -07002594 struct sk_buff *skb;
2595 struct net_device *dev;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002596 struct virtio_net_hdr *vnet_hdr = NULL;
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002597 struct sockcm_cookie sockc;
Johann Baudy69e3c752009-05-18 22:11:22 -07002598 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002599 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002600 void *ph;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002601 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002602 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
Johann Baudy69e3c752009-05-18 22:11:22 -07002603 int tp_len, size_max;
2604 unsigned char *addr;
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002605 void *data;
Johann Baudy69e3c752009-05-18 22:11:22 -07002606 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002607 int status = TP_STATUS_AVAILABLE;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002608 int hlen, tlen, copylen = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002609
Johann Baudy69e3c752009-05-18 22:11:22 -07002610 mutex_lock(&po->pg_vec_lock);
2611
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002612 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002613 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002614 proto = po->num;
2615 addr = NULL;
2616 } else {
2617 err = -EINVAL;
2618 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2619 goto out;
2620 if (msg->msg_namelen < (saddr->sll_halen
2621 + offsetof(struct sockaddr_ll,
2622 sll_addr)))
2623 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002624 proto = saddr->sll_protocol;
Willem de Bruijn6b8d95f2018-12-22 16:53:45 -05002625 addr = saddr->sll_halen ? saddr->sll_addr : NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002626 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Willem de Bruijn99137b72018-12-21 12:06:59 -05002627 if (addr && dev && saddr->sll_halen < dev->addr_len)
Jason Gunthorped972f3d2019-01-08 23:27:06 +00002628 goto out_put;
Johann Baudy69e3c752009-05-18 22:11:22 -07002629 }
2630
Johann Baudy69e3c752009-05-18 22:11:22 -07002631 err = -ENXIO;
2632 if (unlikely(dev == NULL))
2633 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002634 err = -ENETDOWN;
2635 if (unlikely(!(dev->flags & IFF_UP)))
2636 goto out_put;
2637
Willem de Bruijn657a0662018-07-06 10:12:56 -04002638 sockcm_init(&sockc, &po->sk);
Douglas Caetano dos Santosd19b1832017-05-12 15:19:15 -03002639 if (msg->msg_controllen) {
2640 err = sock_cmsg_send(&po->sk, msg, &sockc);
2641 if (unlikely(err))
2642 goto out_put;
2643 }
2644
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002645 if (po->sk.sk_socket->type == SOCK_RAW)
2646 reserve = dev->hard_header_len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002647 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002648 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002649
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002650 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002651 size_max = dev->mtu + reserve + VLAN_HLEN;
David S. Miller09effa62013-08-07 17:11:00 -07002652
Johann Baudy69e3c752009-05-18 22:11:22 -07002653 do {
2654 ph = packet_current_frame(po, &po->tx_ring,
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002655 TP_STATUS_SEND_REQUEST);
Johann Baudy69e3c752009-05-18 22:11:22 -07002656 if (unlikely(ph == NULL)) {
Daniel Borkmann87a2fd22014-01-15 16:25:35 +01002657 if (need_wait && need_resched())
2658 schedule();
Johann Baudy69e3c752009-05-18 22:11:22 -07002659 continue;
2660 }
2661
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002662 skb = NULL;
2663 tp_len = tpacket_parse_header(po, ph, size_max, &data);
2664 if (tp_len < 0)
2665 goto tpacket_error;
2666
Johann Baudy69e3c752009-05-18 22:11:22 -07002667 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002668 hlen = LL_RESERVED_SPACE(dev);
2669 tlen = dev->needed_tailroom;
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002670 if (po->has_vnet_hdr) {
2671 vnet_hdr = data;
2672 data += sizeof(*vnet_hdr);
2673 tp_len -= sizeof(*vnet_hdr);
2674 if (tp_len < 0 ||
2675 __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
2676 tp_len = -EINVAL;
2677 goto tpacket_error;
2678 }
2679 copylen = __virtio16_to_cpu(vio_le(),
2680 vnet_hdr->hdr_len);
2681 }
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002682 copylen = max_t(int, copylen, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002683 skb = sock_alloc_send_skb(&po->sk,
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002684 hlen + tlen + sizeof(struct sockaddr_ll) +
2685 (copylen - dev->hard_header_len),
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002686 !need_wait, &err);
Johann Baudy69e3c752009-05-18 22:11:22 -07002687
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002688 if (unlikely(skb == NULL)) {
2689 /* we assume the socket was initially writeable ... */
2690 if (likely(len_sum > 0))
2691 err = len_sum;
Johann Baudy69e3c752009-05-18 22:11:22 -07002692 goto out_status;
Kretschmer, Mathiasfbf33a22015-05-08 15:44:37 +02002693 }
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002694 tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04002695 addr, hlen, copylen, &sockc);
Alexander Drozdovdbd46ab2015-07-28 13:57:01 +03002696 if (likely(tp_len >= 0) &&
Daniel Borkmann5cfb4c82015-11-11 23:25:44 +01002697 tp_len > dev->mtu + reserve &&
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002698 !po->has_vnet_hdr &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002699 !packet_extra_vlan_len_allowed(dev, skb))
2700 tp_len = -EMSGSIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002701
2702 if (unlikely(tp_len < 0)) {
Willem de Bruijn8d39b4a2016-02-03 18:02:16 -05002703tpacket_error:
Johann Baudy69e3c752009-05-18 22:11:22 -07002704 if (po->tp_loss) {
2705 __packet_set_status(po, ph,
2706 TP_STATUS_AVAILABLE);
2707 packet_increment_head(&po->tx_ring);
2708 kfree_skb(skb);
2709 continue;
2710 } else {
2711 status = TP_STATUS_WRONG_FORMAT;
2712 err = tp_len;
2713 goto out_status;
2714 }
2715 }
2716
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002717 if (po->has_vnet_hdr) {
2718 if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
2719 tp_len = -EINVAL;
2720 goto tpacket_error;
2721 }
2722 virtio_net_hdr_set_proto(skb, vnet_hdr);
Willem de Bruijn1d036d22016-02-03 18:02:17 -05002723 }
2724
Johann Baudy69e3c752009-05-18 22:11:22 -07002725 skb->destructor = tpacket_destruct_skb;
2726 __packet_set_status(po, ph, TP_STATUS_SENDING);
Daniel Borkmannb0138402014-01-15 16:25:36 +01002727 packet_inc_pending(&po->tx_ring);
Johann Baudy69e3c752009-05-18 22:11:22 -07002728
2729 status = TP_STATUS_SEND_REQUEST;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002730 err = po->xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002731 if (unlikely(err > 0)) {
2732 err = net_xmit_errno(err);
2733 if (err && __packet_get_status(po, ph) ==
2734 TP_STATUS_AVAILABLE) {
2735 /* skb was destructed already */
2736 skb = NULL;
2737 goto out_status;
2738 }
2739 /*
2740 * skb was dropped but not destructed yet;
2741 * let's treat it like congestion or err < 0
2742 */
2743 err = 0;
2744 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002745 packet_increment_head(&po->tx_ring);
2746 len_sum += tp_len;
Daniel Borkmannb0138402014-01-15 16:25:36 +01002747 } while (likely((ph != NULL) ||
2748 /* Note: packet_read_pending() might be slow if we have
2749 * to call it as it's per_cpu variable, but in fast-path
2750 * we already short-circuit the loop with the first
2751 * condition, and luckily don't have to go that path
2752 * anyway.
2753 */
2754 (need_wait && packet_read_pending(&po->tx_ring))));
Johann Baudy69e3c752009-05-18 22:11:22 -07002755
2756 err = len_sum;
2757 goto out_put;
2758
Johann Baudy69e3c752009-05-18 22:11:22 -07002759out_status:
2760 __packet_set_status(po, ph, status);
2761 kfree_skb(skb);
2762out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002763 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002764out:
2765 mutex_unlock(&po->pg_vec_lock);
2766 return err;
2767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002769static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2770 size_t reserve, size_t len,
2771 size_t linear, int noblock,
2772 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002773{
2774 struct sk_buff *skb;
2775
2776 /* Under a page? Don't bother with paged skb. */
2777 if (prepad + len < PAGE_SIZE || !linear)
2778 linear = len;
2779
2780 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002781 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002782 if (!skb)
2783 return NULL;
2784
2785 skb_reserve(skb, reserve);
2786 skb_put(skb, linear);
2787 skb->data_len = len - linear;
2788 skb->len += len - linear;
2789
2790 return skb;
2791}
2792
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002793static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 struct sock *sk = sock->sk;
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002796 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 struct sk_buff *skb;
2798 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002799 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00002801 int err, reserve = 0;
Edward Jeec7d39e32015-10-08 14:56:49 -07002802 struct sockcm_cookie sockc;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002803 struct virtio_net_hdr vnet_hdr = { 0 };
2804 int offset = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002805 struct packet_sock *po = pkt_sk(sk);
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002806 bool has_vnet_hdr = false;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002807 int hlen, tlen, linear;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002808 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002811 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002813
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002814 if (likely(saddr == NULL)) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002815 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 proto = po->num;
2817 addr = NULL;
2818 } else {
2819 err = -EINVAL;
2820 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2821 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002822 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 proto = saddr->sll_protocol;
Willem de Bruijn6b8d95f2018-12-22 16:53:45 -05002825 addr = saddr->sll_halen ? saddr->sll_addr : NULL;
Ben Greear827d9782011-06-01 07:18:53 +00002826 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Willem de Bruijn99137b72018-12-21 12:06:59 -05002827 if (addr && dev && saddr->sll_halen < dev->addr_len)
Jason Gunthorped972f3d2019-01-08 23:27:06 +00002828 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002832 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002834 err = -ENETDOWN;
2835 if (unlikely(!(dev->flags & IFF_UP)))
2836 goto out_unlock;
2837
Willem de Bruijn657a0662018-07-06 10:12:56 -04002838 sockcm_init(&sockc, sk);
Edward Jeec7d39e32015-10-08 14:56:49 -07002839 sockc.mark = sk->sk_mark;
2840 if (msg->msg_controllen) {
2841 err = sock_cmsg_send(sk, msg, &sockc);
2842 if (unlikely(err))
2843 goto out_unlock;
2844 }
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (sock->type == SOCK_RAW)
2847 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002848 if (po->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002849 err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
2850 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002851 goto out_unlock;
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002852 has_vnet_hdr = true;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002853 }
2854
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002855 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2856 if (!netif_supports_nofcs(dev)) {
2857 err = -EPROTONOSUPPORT;
2858 goto out_unlock;
2859 }
2860 extra_len = 4; /* We're doing our own CRC */
2861 }
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 err = -EMSGSIZE;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002864 if (!vnet_hdr.gso_type &&
2865 (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 goto out_unlock;
2867
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002868 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002869 hlen = LL_RESERVED_SPACE(dev);
2870 tlen = dev->needed_tailroom;
Willem de Bruijn57031eb2017-02-07 15:57:21 -05002871 linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
2872 linear = max(linear, min_t(int, len, dev->hard_header_len));
2873 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002874 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002875 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 goto out_unlock;
2877
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002878 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002880 err = -EINVAL;
Willem de Bruijn9c707762014-11-19 13:10:16 -05002881 if (sock->type == SOCK_DGRAM) {
2882 offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
Christoph Jaeger46d2cfb2015-01-11 13:01:16 -05002883 if (unlikely(offset < 0))
Willem de Bruijn9c707762014-11-19 13:10:16 -05002884 goto out_free;
Willem de Bruijnb84bbaf2018-05-11 13:24:25 -04002885 } else if (reserve) {
Willem de Bruijn9aad13b2018-05-24 18:10:30 -04002886 skb_reserve(skb, -reserve);
Nicolas Dichtel88a81212019-01-17 11:27:22 +01002887 if (len < reserve + sizeof(struct ipv6hdr) &&
2888 dev->min_header_len != dev->hard_header_len)
Willem de Bruijn993675a2018-07-11 12:00:45 -04002889 skb_reset_network_header(skb);
Willem de Bruijn9c707762014-11-19 13:10:16 -05002890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /* Returns -EFAULT on error */
Al Viroc0371da2014-11-24 10:42:55 -05002893 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (err)
2895 goto out_free;
Daniel Borkmannbf84a0102013-04-14 08:08:13 +00002896
Willem de Bruijn9ed988c2016-03-09 21:58:34 -05002897 if (sock->type == SOCK_RAW &&
2898 !dev_validate_header(dev, skb->data, len)) {
2899 err = -EINVAL;
2900 goto out_free;
2901 }
2902
Willem de Bruijn8f932f72018-12-17 12:24:00 -05002903 skb_setup_tx_timestamp(skb, sockc.tsflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002905 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
Daniel Borkmann3c70c132015-11-11 23:25:42 +01002906 !packet_extra_vlan_len_allowed(dev, skb)) {
2907 err = -EMSGSIZE;
2908 goto out_free;
Ben Greear57f89bf2011-02-11 09:35:18 +00002909 }
2910
David S. Miller09effa62013-08-07 17:11:00 -07002911 skb->protocol = proto;
2912 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 skb->priority = sk->sk_priority;
Edward Jeec7d39e32015-10-08 14:56:49 -07002914 skb->mark = sockc.mark;
Richard Cochran3d0ba8c2018-07-03 15:42:51 -07002915 skb->tstamp = sockc.transmit_time;
Daniel Borkmann0fd5d572014-02-16 15:55:22 +01002916
Willem de Bruijnda7c9562017-09-26 12:20:17 -04002917 if (has_vnet_hdr) {
Jarno Rajahalmedb60eb5f2016-11-18 15:40:41 -08002918 err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
Willem de Bruijn16cc1402016-02-03 18:02:14 -05002919 if (err)
2920 goto out_free;
2921 len += sizeof(vnet_hdr);
Jianfeng Tan9d2f67e2018-09-29 15:41:27 +00002922 virtio_net_hdr_set_proto(skb, &vnet_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002923 }
2924
Maxim Mikityanskiy75c65772019-02-21 12:40:01 +00002925 packet_parse_headers(skb, sock);
Daniel Borkmann8fd6c802015-11-11 23:25:41 +01002926
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002927 if (unlikely(extra_len == 4))
2928 skb->no_fcs = 1;
2929
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002930 err = po->xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2932 goto out_unlock;
2933
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002934 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002936 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938out_free:
2939 kfree_skb(skb);
2940out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002941 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 dev_put(dev);
2943out:
2944 return err;
2945}
2946
Ying Xue1b784142015-03-02 15:37:48 +08002947static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Johann Baudy69e3c752009-05-18 22:11:22 -07002948{
Johann Baudy69e3c752009-05-18 22:11:22 -07002949 struct sock *sk = sock->sk;
2950 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01002951
Johann Baudy69e3c752009-05-18 22:11:22 -07002952 if (po->tx_ring.pg_vec)
2953 return tpacket_snd(po, msg);
2954 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002955 return packet_snd(sock, msg, len);
2956}
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958/*
2959 * Close a PACKET socket. This is fairly simple. We immediately go
2960 * to 'closed' state and remove our protocol entry in the device list.
2961 */
2962
2963static int packet_release(struct socket *sock)
2964{
2965 struct sock *sk = sock->sk;
2966 struct packet_sock *po;
Anoob Soman2bd624b2017-02-15 20:25:39 +00002967 struct packet_fanout *f;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002968 struct net *net;
chetan lokef6fb8f12011-08-19 10:18:16 +00002969 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 if (!sk)
2972 return 0;
2973
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002974 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 po = pkt_sk(sk);
2976
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002977 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002978 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002979 mutex_unlock(&net->packet.sklist_lock);
2980
2981 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08002982 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002983 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
stephen hemminger808f5112010-02-22 07:57:18 +00002985 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002986 unregister_prot_hook(sk, false);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01002987 packet_cached_dev_reset(po);
2988
Ben Greear160ff182011-06-01 07:18:52 +00002989 if (po->prot_hook.dev) {
2990 dev_put(po->prot_hook.dev);
2991 po->prot_hook.dev = NULL;
2992 }
stephen hemminger808f5112010-02-22 07:57:18 +00002993 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Eric Dumazet5171b372018-04-15 17:52:04 -07002997 lock_sock(sk);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002998 if (po->rx_ring.pg_vec) {
2999 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003000 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003001 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003002
Phil Sutter9665d5d2013-02-01 07:21:41 +00003003 if (po->tx_ring.pg_vec) {
3004 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f12011-08-19 10:18:16 +00003005 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00003006 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003007 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Anoob Soman2bd624b2017-02-15 20:25:39 +00003009 f = fanout_release(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07003010
stephen hemminger808f5112010-02-22 07:57:18 +00003011 synchronize_net();
Anoob Soman2bd624b2017-02-15 20:25:39 +00003012
3013 if (f) {
Mike Maloney57f015f2017-11-28 10:44:29 -05003014 kfree(po->rollover);
Anoob Soman2bd624b2017-02-15 20:25:39 +00003015 fanout_release_data(f);
3016 kfree(f);
3017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 /*
3019 * Now the socket is dead. No more input will appear.
3020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 sock_orphan(sk);
3022 sock->sk = NULL;
3023
3024 /* Purge queues */
3025
3026 skb_queue_purge(&sk->sk_receive_queue);
Daniel Borkmannb0138402014-01-15 16:25:36 +01003027 packet_free_pending(po);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003028 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 sock_put(sk);
3031 return 0;
3032}
3033
3034/*
3035 * Attach a packet hook.
3036 */
3037
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003038static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
3039 __be16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 struct packet_sock *po = pkt_sk(sk);
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003042 struct net_device *dev_curr;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003043 __be16 proto_curr;
3044 bool need_rehook;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003045 struct net_device *dev = NULL;
3046 int ret = 0;
3047 bool unlisted = false;
David S. Millerdc99f602011-07-05 01:45:05 -07003048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 spin_lock(&po->bind_lock);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003051 rcu_read_lock();
3052
Willem de Bruijn49716132017-09-26 12:19:37 -04003053 if (po->fanout) {
3054 ret = -EINVAL;
3055 goto out_unlock;
3056 }
3057
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003058 if (name) {
3059 dev = dev_get_by_name_rcu(sock_net(sk), name);
3060 if (!dev) {
3061 ret = -ENODEV;
3062 goto out_unlock;
3063 }
3064 } else if (ifindex) {
3065 dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
3066 if (!dev) {
3067 ret = -ENODEV;
3068 goto out_unlock;
3069 }
3070 }
3071
3072 if (dev)
3073 dev_hold(dev);
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003074
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003075 proto_curr = po->prot_hook.type;
3076 dev_curr = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003078 need_rehook = proto_curr != proto || dev_curr != dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003080 if (need_rehook) {
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003081 if (po->running) {
3082 rcu_read_unlock();
Eric Dumazet15fe0762017-11-28 08:03:30 -08003083 /* prevents packet_notifier() from calling
3084 * register_prot_hook()
3085 */
3086 po->num = 0;
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003087 __unregister_prot_hook(sk, true);
3088 rcu_read_lock();
3089 dev_curr = po->prot_hook.dev;
3090 if (dev)
3091 unlisted = !dev_get_by_index_rcu(sock_net(sk),
3092 dev->ifindex);
3093 }
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003094
Eric Dumazet15fe0762017-11-28 08:03:30 -08003095 BUG_ON(po->running);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003096 po->num = proto;
3097 po->prot_hook.type = proto;
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003098
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003099 if (unlikely(unlisted)) {
3100 dev_put(dev);
3101 po->prot_hook.dev = NULL;
3102 po->ifindex = -1;
3103 packet_cached_dev_reset(po);
3104 } else {
3105 po->prot_hook.dev = dev;
3106 po->ifindex = dev ? dev->ifindex : 0;
3107 packet_cached_dev_assign(po, dev);
3108 }
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003109 }
Lars Westerhoff158cd4a2015-07-28 01:32:21 +03003110 if (dev_curr)
3111 dev_put(dev_curr);
Daniel Borkmann902fefb2014-01-15 16:25:34 +01003112
3113 if (proto == 0 || !need_rehook)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 goto out_unlock;
3115
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003116 if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
David S. Millerce06b032011-07-04 01:44:29 -07003117 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08003118 } else {
3119 sk->sk_err = ENETDOWN;
3120 if (!sock_flag(sk, SOCK_DEAD))
3121 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123
3124out_unlock:
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003125 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 spin_unlock(&po->bind_lock);
3127 release_sock(sk);
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003128 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
3131/*
3132 * Bind a packet socket to a device
3133 */
3134
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003135static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
3136 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003138 struct sock *sk = sock->sk;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003139 char name[sizeof(uaddr->sa_data) + 1];
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 /*
3142 * Check legality
3143 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003144
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003145 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return -EINVAL;
Alexander Potapenko540e2892017-03-01 12:57:20 +01003147 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
3148 * zero-terminated.
3149 */
3150 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data));
3151 name[sizeof(uaddr->sa_data)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003153 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
3157{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003158 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
3159 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /*
3162 * Check legality
3163 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (addr_len < sizeof(struct sockaddr_ll))
3166 return -EINVAL;
3167 if (sll->sll_family != AF_PACKET)
3168 return -EINVAL;
3169
Francesco Ruggeri30f7ea12015-11-05 08:16:14 -08003170 return packet_do_bind(sk, NULL, sll->sll_ifindex,
3171 sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
3174static struct proto packet_proto = {
3175 .name = "PACKET",
3176 .owner = THIS_MODULE,
3177 .obj_size = sizeof(struct packet_sock),
3178};
3179
3180/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003181 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 */
3183
Eric Paris3f378b62009-11-05 22:18:14 -08003184static int packet_create(struct net *net, struct socket *sock, int protocol,
3185 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 struct sock *sk;
3188 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08003189 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 int err;
3191
Eric W. Biedermandf008c92012-11-16 03:03:07 +00003192 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07003194 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
3195 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return -ESOCKTNOSUPPORT;
3197
3198 sock->state = SS_UNCONNECTED;
3199
3200 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -05003201 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (sk == NULL)
3203 goto out;
3204
3205 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (sock->type == SOCK_PACKET)
3207 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 sock_init_data(sock, sk);
3210
3211 po = pkt_sk(sk);
3212 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08003213 po->num = proto;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003214 po->xmit = dev_queue_xmit;
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003215
Daniel Borkmannb0138402014-01-15 16:25:36 +01003216 err = packet_alloc_pending(po);
3217 if (err)
3218 goto out2;
3219
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01003220 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08003223 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 /*
3226 * Attach a protocol block
3227 */
3228
3229 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08003230 mutex_init(&po->pg_vec_lock);
Willem de Bruijn0648ab72015-05-12 11:56:46 -04003231 po->rollover = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (sock->type == SOCK_PACKET)
3235 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 po->prot_hook.af_packet_priv = sk;
3238
Al Viro0e11c912006-11-08 00:26:29 -08003239 if (proto) {
3240 po->prot_hook.type = proto;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003241 __register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003244 mutex_lock(&net->packet.sklist_lock);
Maxime Chevalliera4dc6a42019-03-16 14:41:30 +01003245 sk_add_node_tail_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003246 mutex_unlock(&net->packet.sklist_lock);
3247
3248 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08003249 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003250 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00003251
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003252 return 0;
Daniel Borkmannb0138402014-01-15 16:25:36 +01003253out2:
3254 sk_free(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255out:
3256 return err;
3257}
3258
3259/*
3260 * Pull a packet from our receive queue and hand it to the user.
3261 * If necessary we block.
3262 */
3263
Ying Xue1b784142015-03-02 15:37:48 +08003264static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3265 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 struct sock *sk = sock->sk;
3268 struct sk_buff *skb;
3269 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003270 int vnet_hdr_len = 0;
Eyal Birger2472d762015-03-01 14:58:28 +02003271 unsigned int origlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00003274 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 goto out;
3276
3277#if 0
3278 /* What error should we return now? EUNATTACH? */
3279 if (pkt_sk(sk)->ifindex < 0)
3280 return -ENODEV;
3281#endif
3282
Richard Cochraned85b562010-04-07 22:41:28 +00003283 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02003284 err = sock_recv_errqueue(sk, msg, len,
3285 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00003286 goto out;
3287 }
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 * Call the generic datagram receiver. This handles all sorts
3291 * of horrible races and re-entrancy so we can forget about it
3292 * in the protocol layers.
3293 *
3294 * Now it will return ENETDOWN, if device have just gone down,
3295 * but then it will block.
3296 */
3297
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003298 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003301 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 * handles the blocking we don't see and worry about blocking
3303 * retries.
3304 */
3305
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003306 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 goto out;
3308
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04003309 if (pkt_sk(sk)->pressure)
3310 packet_rcv_has_room(pkt_sk(sk), NULL);
3311
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003312 if (pkt_sk(sk)->has_vnet_hdr) {
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003313 err = packet_rcv_vnet(msg, skb, &len);
3314 if (err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003315 goto out_free;
Willem de Bruijn16cc1402016-02-03 18:02:14 -05003316 vnet_hdr_len = sizeof(struct virtio_net_hdr);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003317 }
3318
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003319 /* You lose any data beyond the buffer you gave. If it worries
3320 * a user program they can ask the device for its MTU
3321 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003324 if (copied > len) {
3325 copied = len;
3326 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328
David S. Miller51f3d022014-11-05 16:46:40 -05003329 err = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (err)
3331 goto out_free;
3332
Eyal Birger2472d762015-03-01 14:58:28 +02003333 if (sock->type != SOCK_PACKET) {
3334 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3335
3336 /* Original length was stored in sockaddr_ll fields */
3337 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3338 sll->sll_family = AF_PACKET;
3339 sll->sll_protocol = skb->protocol;
3340 }
3341
Neil Horman3b885782009-10-12 13:26:31 -07003342 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003344 if (msg->msg_name) {
3345 /* If the address length field is there to be filled
3346 * in, we fill it in now.
3347 */
3348 if (sock->type == SOCK_PACKET) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01003349 __sockaddr_check_size(sizeof(struct sockaddr_pkt));
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003350 msg->msg_namelen = sizeof(struct sockaddr_pkt);
3351 } else {
3352 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
Eyal Birger2472d762015-03-01 14:58:28 +02003353
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003354 msg->msg_namelen = sll->sll_halen +
3355 offsetof(struct sockaddr_ll, sll_addr);
3356 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003357 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
3358 msg->msg_namelen);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01003359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Herbert Xu8dc41942007-02-04 23:31:32 -08003361 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08003362 struct tpacket_auxdata aux;
3363
3364 aux.tp_status = TP_STATUS_USER;
3365 if (skb->ip_summed == CHECKSUM_PARTIAL)
3366 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
Alexander Drozdov682f0482015-03-23 09:11:13 +03003367 else if (skb->pkt_type != PACKET_OUTGOING &&
3368 (skb->ip_summed == CHECKSUM_COMPLETE ||
3369 skb_csum_unnecessary(skb)))
3370 aux.tp_status |= TP_STATUS_CSUM_VALID;
3371
Eyal Birger2472d762015-03-01 14:58:28 +02003372 aux.tp_len = origlen;
Herbert Xuffbc6112007-02-04 23:33:10 -08003373 aux.tp_snaplen = skb->len;
3374 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003375 aux.tp_net = skb_network_offset(skb);
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01003376 if (skb_vlan_tag_present(skb)) {
3377 aux.tp_vlan_tci = skb_vlan_tag_get(skb);
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003378 aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
3379 aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
Ben Greeara3bcc232011-06-01 06:49:10 +00003380 } else {
3381 aux.tp_vlan_tci = 0;
Atzm Watanabea0cdfcf2013-12-17 22:53:40 +09003382 aux.tp_vlan_tpid = 0;
Ben Greeara3bcc232011-06-01 06:49:10 +00003383 }
Herbert Xuffbc6112007-02-04 23:33:10 -08003384 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08003385 }
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /*
3388 * Free or return the buffer as appropriate. Again this
3389 * hides all the races and re-entrancy issues from us.
3390 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003391 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393out_free:
3394 skb_free_datagram(sk, skb);
3395out:
3396 return err;
3397}
3398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003400 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402 struct net_device *dev;
3403 struct sock *sk = sock->sk;
3404
3405 if (peer)
3406 return -EOPNOTSUPP;
3407
3408 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003409 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003410 rcu_read_lock();
3411 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3412 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02003413 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01003414 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003416 return sizeof(*uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003420 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 struct net_device *dev;
3423 struct sock *sk = sock->sk;
3424 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00003425 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 if (peer)
3428 return -EOPNOTSUPP;
3429
3430 sll->sll_family = AF_PACKET;
3431 sll->sll_ifindex = po->ifindex;
3432 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08003433 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01003434 rcu_read_lock();
3435 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 if (dev) {
3437 sll->sll_hatype = dev->type;
3438 sll->sll_halen = dev->addr_len;
3439 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 } else {
3441 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
3442 sll->sll_halen = 0;
3443 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01003444 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01003446 return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
Wang Chen2aeb0b82008-07-14 20:49:46 -07003449static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
3450 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
3452 switch (i->type) {
3453 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003454 if (i->alen != dev->addr_len)
3455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00003457 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003459 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 break;
3461 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003462 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07003464 return dev_set_allmulti(dev, what);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003465 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00003466 if (i->alen != dev->addr_len)
3467 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003468 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003469 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003470 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00003471 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00003472 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003473 default:
3474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07003476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003479static void packet_dev_mclist_delete(struct net_device *dev,
3480 struct packet_mclist **mlp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003482 struct packet_mclist *ml;
3483
3484 while ((ml = *mlp) != NULL) {
3485 if (ml->ifindex == dev->ifindex) {
3486 packet_dev_mc(dev, ml, -1);
3487 *mlp = ml->next;
3488 kfree(ml);
3489 } else
3490 mlp = &ml->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 }
3492}
3493
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003494static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
3496 struct packet_sock *po = pkt_sk(sk);
3497 struct packet_mclist *ml, *i;
3498 struct net_device *dev;
3499 int err;
3500
3501 rtnl_lock();
3502
3503 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003504 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 if (!dev)
3506 goto done;
3507
3508 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00003509 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 goto done;
3511
3512 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08003513 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (i == NULL)
3515 goto done;
3516
3517 err = 0;
3518 for (ml = po->mclist; ml; ml = ml->next) {
3519 if (ml->ifindex == mreq->mr_ifindex &&
3520 ml->type == mreq->mr_type &&
3521 ml->alen == mreq->mr_alen &&
3522 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3523 ml->count++;
3524 /* Free the new element ... */
3525 kfree(i);
3526 goto done;
3527 }
3528 }
3529
3530 i->type = mreq->mr_type;
3531 i->ifindex = mreq->mr_ifindex;
3532 i->alen = mreq->mr_alen;
3533 memcpy(i->addr, mreq->mr_address, i->alen);
Mathias Krause309cf372016-04-10 12:52:28 +02003534 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 i->count = 1;
3536 i->next = po->mclist;
3537 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07003538 err = packet_dev_mc(dev, i, 1);
3539 if (err) {
3540 po->mclist = i->next;
3541 kfree(i);
3542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544done:
3545 rtnl_unlock();
3546 return err;
3547}
3548
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003549static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 struct packet_mclist *ml, **mlp;
3552
3553 rtnl_lock();
3554
3555 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
3556 if (ml->ifindex == mreq->mr_ifindex &&
3557 ml->type == mreq->mr_type &&
3558 ml->alen == mreq->mr_alen &&
3559 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
3560 if (--ml->count == 0) {
3561 struct net_device *dev;
3562 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003563 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3564 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 kfree(ml);
3567 }
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570 }
3571 rtnl_unlock();
Francesco Ruggeri82f17092015-03-09 11:51:04 -07003572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
3575static void packet_flush_mclist(struct sock *sk)
3576{
3577 struct packet_sock *po = pkt_sk(sk);
3578 struct packet_mclist *ml;
3579
3580 if (!po->mclist)
3581 return;
3582
3583 rtnl_lock();
3584 while ((ml = po->mclist) != NULL) {
3585 struct net_device *dev;
3586
3587 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003588 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3589 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 kfree(ml);
3592 }
3593 rtnl_unlock();
3594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596static int
David S. Millerb7058842009-09-30 16:12:20 -07003597packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003600 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 int ret;
3602
3603 if (level != SOL_PACKET)
3604 return -ENOPROTOOPT;
3605
Johann Baudy69e3c752009-05-18 22:11:22 -07003606 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003607 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 case PACKET_DROP_MEMBERSHIP:
3609 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003610 struct packet_mreq_max mreq;
3611 int len = optlen;
3612 memset(&mreq, 0, sizeof(mreq));
3613 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003615 if (len > sizeof(mreq))
3616 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003617 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003619 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3620 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 if (optname == PACKET_ADD_MEMBERSHIP)
3622 ret = packet_mc_add(sk, &mreq);
3623 else
3624 ret = packet_mc_drop(sk, &mreq);
3625 return ret;
3626 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003629 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 {
chetan lokef6fb8f12011-08-19 10:18:16 +00003631 union tpacket_req_u req_u;
3632 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Eric Dumazet5171b372018-04-15 17:52:04 -07003634 lock_sock(sk);
chetan lokef6fb8f12011-08-19 10:18:16 +00003635 switch (po->tp_version) {
3636 case TPACKET_V1:
3637 case TPACKET_V2:
3638 len = sizeof(req_u.req);
3639 break;
3640 case TPACKET_V3:
3641 default:
3642 len = sizeof(req_u.req3);
3643 break;
3644 }
Eric Dumazet5171b372018-04-15 17:52:04 -07003645 if (optlen < len) {
3646 ret = -EINVAL;
3647 } else {
3648 if (copy_from_user(&req_u.req, optval, len))
3649 ret = -EFAULT;
3650 else
3651 ret = packet_set_ring(sk, &req_u, 0,
3652 optname == PACKET_TX_RING);
3653 }
3654 release_sock(sk);
3655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657 case PACKET_COPY_THRESH:
3658 {
3659 int val;
3660
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003661 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003663 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return -EFAULT;
3665
3666 pkt_sk(sk)->copy_thresh = val;
3667 return 0;
3668 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003669 case PACKET_VERSION:
3670 {
3671 int val;
3672
3673 if (optlen != sizeof(val))
3674 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003675 if (copy_from_user(&val, optval, sizeof(val)))
3676 return -EFAULT;
3677 switch (val) {
3678 case TPACKET_V1:
3679 case TPACKET_V2:
chetan lokef6fb8f12011-08-19 10:18:16 +00003680 case TPACKET_V3:
Philip Pettersson84ac7262016-11-30 14:55:36 -08003681 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003682 default:
3683 return -EINVAL;
3684 }
Philip Pettersson84ac7262016-11-30 14:55:36 -08003685 lock_sock(sk);
3686 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3687 ret = -EBUSY;
3688 } else {
3689 po->tp_version = val;
3690 ret = 0;
3691 }
3692 release_sock(sk);
3693 return ret;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003694 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003695 case PACKET_RESERVE:
3696 {
3697 unsigned int val;
3698
3699 if (optlen != sizeof(val))
3700 return -EINVAL;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003701 if (copy_from_user(&val, optval, sizeof(val)))
3702 return -EFAULT;
Andrey Konovalovbcc53642017-03-29 16:11:22 +02003703 if (val > INT_MAX)
3704 return -EINVAL;
Willem de Bruijnc27927e2017-08-10 12:41:58 -04003705 lock_sock(sk);
3706 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3707 ret = -EBUSY;
3708 } else {
3709 po->tp_reserve = val;
3710 ret = 0;
3711 }
3712 release_sock(sk);
3713 return ret;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003714 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003715 case PACKET_LOSS:
3716 {
3717 unsigned int val;
3718
3719 if (optlen != sizeof(val))
3720 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003721 if (copy_from_user(&val, optval, sizeof(val)))
3722 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003723
3724 lock_sock(sk);
3725 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3726 ret = -EBUSY;
3727 } else {
3728 po->tp_loss = !!val;
3729 ret = 0;
3730 }
3731 release_sock(sk);
3732 return ret;
Johann Baudy69e3c752009-05-18 22:11:22 -07003733 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003734 case PACKET_AUXDATA:
3735 {
3736 int val;
3737
3738 if (optlen < sizeof(val))
3739 return -EINVAL;
3740 if (copy_from_user(&val, optval, sizeof(val)))
3741 return -EFAULT;
3742
Willem de Bruijna6361f02018-04-23 17:37:03 -04003743 lock_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003744 po->auxdata = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003745 release_sock(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08003746 return 0;
3747 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003748 case PACKET_ORIGDEV:
3749 {
3750 int val;
3751
3752 if (optlen < sizeof(val))
3753 return -EINVAL;
3754 if (copy_from_user(&val, optval, sizeof(val)))
3755 return -EFAULT;
3756
Willem de Bruijna6361f02018-04-23 17:37:03 -04003757 lock_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003758 po->origdev = !!val;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003759 release_sock(sk);
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003760 return 0;
3761 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003762 case PACKET_VNET_HDR:
3763 {
3764 int val;
3765
3766 if (sock->type != SOCK_RAW)
3767 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003768 if (optlen < sizeof(val))
3769 return -EINVAL;
3770 if (copy_from_user(&val, optval, sizeof(val)))
3771 return -EFAULT;
3772
Willem de Bruijna6361f02018-04-23 17:37:03 -04003773 lock_sock(sk);
3774 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3775 ret = -EBUSY;
3776 } else {
3777 po->has_vnet_hdr = !!val;
3778 ret = 0;
3779 }
3780 release_sock(sk);
3781 return ret;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003782 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003783 case PACKET_TIMESTAMP:
3784 {
3785 int val;
3786
3787 if (optlen != sizeof(val))
3788 return -EINVAL;
3789 if (copy_from_user(&val, optval, sizeof(val)))
3790 return -EFAULT;
3791
3792 po->tp_tstamp = val;
3793 return 0;
3794 }
David S. Millerdc99f602011-07-05 01:45:05 -07003795 case PACKET_FANOUT:
3796 {
3797 int val;
3798
3799 if (optlen != sizeof(val))
3800 return -EINVAL;
3801 if (copy_from_user(&val, optval, sizeof(val)))
3802 return -EFAULT;
3803
3804 return fanout_add(sk, val & 0xffff, val >> 16);
3805 }
Willem de Bruijn47dceb82015-08-14 22:31:34 -04003806 case PACKET_FANOUT_DATA:
3807 {
3808 if (!po->fanout)
3809 return -EINVAL;
3810
3811 return fanout_set_data(po, optval, optlen);
3812 }
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003813 case PACKET_IGNORE_OUTGOING:
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 if (val < 0 || val > 1)
3822 return -EINVAL;
3823
3824 po->prot_hook.ignore_outgoing = !!val;
3825 return 0;
3826 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003827 case PACKET_TX_HAS_OFF:
3828 {
3829 unsigned int val;
3830
3831 if (optlen != sizeof(val))
3832 return -EINVAL;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003833 if (copy_from_user(&val, optval, sizeof(val)))
3834 return -EFAULT;
Willem de Bruijna6361f02018-04-23 17:37:03 -04003835
3836 lock_sock(sk);
3837 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
3838 ret = -EBUSY;
3839 } else {
3840 po->tp_tx_has_off = !!val;
3841 ret = 0;
3842 }
3843 release_sock(sk);
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003844 return 0;
3845 }
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003846 case PACKET_QDISC_BYPASS:
3847 {
3848 int val;
3849
3850 if (optlen != sizeof(val))
3851 return -EINVAL;
3852 if (copy_from_user(&val, optval, sizeof(val)))
3853 return -EFAULT;
3854
3855 po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
3856 return 0;
3857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 default:
3859 return -ENOPROTOOPT;
3860 }
3861}
3862
3863static int packet_getsockopt(struct socket *sock, int level, int optname,
3864 char __user *optval, int __user *optlen)
3865{
3866 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003867 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 struct sock *sk = sock->sk;
3869 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003870 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003871 union tpacket_stats_u st;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003872 struct tpacket_rollover_stats rstats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
3874 if (level != SOL_PACKET)
3875 return -ENOPROTOOPT;
3876
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003877 if (get_user(len, optlen))
3878 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
3880 if (len < 0)
3881 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003882
Johann Baudy69e3c752009-05-18 22:11:22 -07003883 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003886 memcpy(&st, &po->stats, sizeof(st));
3887 memset(&po->stats, 0, sizeof(po->stats));
3888 spin_unlock_bh(&sk->sk_receive_queue.lock);
3889
chetan lokef6fb8f12011-08-19 10:18:16 +00003890 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003891 lv = sizeof(struct tpacket_stats_v3);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003892 st.stats3.tp_packets += st.stats3.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003893 data = &st.stats3;
chetan lokef6fb8f12011-08-19 10:18:16 +00003894 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003895 lv = sizeof(struct tpacket_stats);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003896 st.stats1.tp_packets += st.stats1.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003897 data = &st.stats1;
chetan lokef6fb8f12011-08-19 10:18:16 +00003898 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003901 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003902 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003903 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003904 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003905 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003906 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003907 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003908 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003909 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003910 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003911 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003912 break;
3913 case PACKET_HDRLEN:
3914 if (len > sizeof(int))
3915 len = sizeof(int);
Alexander Potapenkofd2c83b2017-04-25 18:51:46 +02003916 if (len < sizeof(int))
3917 return -EINVAL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003918 if (copy_from_user(&val, optval, len))
3919 return -EFAULT;
3920 switch (val) {
3921 case TPACKET_V1:
3922 val = sizeof(struct tpacket_hdr);
3923 break;
3924 case TPACKET_V2:
3925 val = sizeof(struct tpacket2_hdr);
3926 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00003927 case TPACKET_V3:
3928 val = sizeof(struct tpacket3_hdr);
3929 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003930 default:
3931 return -EINVAL;
3932 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003933 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003934 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003935 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003936 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003937 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003938 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003939 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003940 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003941 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003942 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003943 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003944 val = (po->fanout ?
3945 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003946 ((u32)po->fanout->type << 16) |
3947 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003948 0);
David S. Millerdc99f602011-07-05 01:45:05 -07003949 break;
Vincent Whitchurchfa788d92018-09-03 16:23:36 +02003950 case PACKET_IGNORE_OUTGOING:
3951 val = po->prot_hook.ignore_outgoing;
3952 break;
Willem de Bruijna9b63912015-05-12 11:56:50 -04003953 case PACKET_ROLLOVER_STATS:
Mike Maloney57f015f2017-11-28 10:44:29 -05003954 if (!po->rollover)
Willem de Bruijna9b63912015-05-12 11:56:50 -04003955 return -EINVAL;
Mike Maloney57f015f2017-11-28 10:44:29 -05003956 rstats.tp_all = atomic_long_read(&po->rollover->num);
3957 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
3958 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
3959 data = &rstats;
3960 lv = sizeof(rstats);
Willem de Bruijna9b63912015-05-12 11:56:50 -04003961 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003962 case PACKET_TX_HAS_OFF:
3963 val = po->tp_tx_has_off;
3964 break;
Daniel Borkmannd346a3f2013-12-06 11:36:17 +01003965 case PACKET_QDISC_BYPASS:
3966 val = packet_use_direct_xmit(po);
3967 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 default:
3969 return -ENOPROTOOPT;
3970 }
3971
Eric Dumazetc06fff62012-04-19 21:56:11 +00003972 if (len > lv)
3973 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003974 if (put_user(len, optlen))
3975 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08003976 if (copy_to_user(optval, data, len))
3977 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979}
3980
3981
Willem de Bruijn719c44d2016-06-07 12:06:34 -04003982#ifdef CONFIG_COMPAT
3983static int compat_packet_setsockopt(struct socket *sock, int level, int optname,
3984 char __user *optval, unsigned int optlen)
3985{
3986 struct packet_sock *po = pkt_sk(sock->sk);
3987
3988 if (level != SOL_PACKET)
3989 return -ENOPROTOOPT;
3990
3991 if (optname == PACKET_FANOUT_DATA &&
3992 po->fanout && po->fanout->type == PACKET_FANOUT_CBPF) {
3993 optval = (char __user *)get_compat_bpf_fprog(optval);
3994 if (!optval)
3995 return -EFAULT;
3996 optlen = sizeof(struct sock_fprog);
3997 }
3998
3999 return packet_setsockopt(sock, level, optname, optval, optlen);
4000}
4001#endif
4002
Jiri Pirko351638e2013-05-28 01:30:21 +00004003static int packet_notifier(struct notifier_block *this,
4004 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00004007 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004008 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
stephen hemminger808f5112010-02-22 07:57:18 +00004010 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004011 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 struct packet_sock *po = pkt_sk(sk);
4013
4014 switch (msg) {
4015 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (po->mclist)
Francesco Ruggeri82f17092015-03-09 11:51:04 -07004017 packet_dev_mclist_delete(dev, &po->mclist);
David S. Millera2efcfa2007-05-29 13:12:50 -07004018 /* fallthrough */
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 case NETDEV_DOWN:
4021 if (dev->ifindex == po->ifindex) {
4022 spin_lock(&po->bind_lock);
4023 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07004024 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 sk->sk_err = ENETDOWN;
4026 if (!sock_flag(sk, SOCK_DEAD))
4027 sk->sk_error_report(sk);
4028 }
4029 if (msg == NETDEV_UNREGISTER) {
Daniel Borkmann66e56cd2013-12-06 11:36:15 +01004030 packet_cached_dev_reset(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00004032 if (po->prot_hook.dev)
4033 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 po->prot_hook.dev = NULL;
4035 }
4036 spin_unlock(&po->bind_lock);
4037 }
4038 break;
4039 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00004040 if (dev->ifindex == po->ifindex) {
4041 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004042 if (po->num)
4043 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00004044 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 break;
4047 }
4048 }
stephen hemminger808f5112010-02-22 07:57:18 +00004049 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return NOTIFY_DONE;
4051}
4052
4053
4054static int packet_ioctl(struct socket *sock, unsigned int cmd,
4055 unsigned long arg)
4056{
4057 struct sock *sk = sock->sk;
4058
Johann Baudy69e3c752009-05-18 22:11:22 -07004059 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004060 case SIOCOUTQ:
4061 {
4062 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07004063
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004064 return put_user(amount, (int __user *)arg);
4065 }
4066 case SIOCINQ:
4067 {
4068 struct sk_buff *skb;
4069 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004071 spin_lock_bh(&sk->sk_receive_queue.lock);
4072 skb = skb_peek(&sk->sk_receive_queue);
4073 if (skb)
4074 amount = skb->len;
4075 spin_unlock_bh(&sk->sk_receive_queue.lock);
4076 return put_user(amount, (int __user *)arg);
4077 }
4078 case SIOCGSTAMP:
4079 return sock_get_timestamp(sk, (struct timeval __user *)arg);
4080 case SIOCGSTAMPNS:
4081 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004084 case SIOCADDRT:
4085 case SIOCDELRT:
4086 case SIOCDARP:
4087 case SIOCGARP:
4088 case SIOCSARP:
4089 case SIOCGIFADDR:
4090 case SIOCSIFADDR:
4091 case SIOCGIFBRDADDR:
4092 case SIOCSIFBRDADDR:
4093 case SIOCGIFNETMASK:
4094 case SIOCSIFNETMASK:
4095 case SIOCGIFDSTADDR:
4096 case SIOCSIFDSTADDR:
4097 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004098 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099#endif
4100
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004101 default:
4102 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 }
4104 return 0;
4105}
4106
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004107static __poll_t packet_poll(struct file *file, struct socket *sock,
4108 poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 struct sock *sk = sock->sk;
4111 struct packet_sock *po = pkt_sk(sk);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004112 __poll_t mask = datagram_poll(file, sock, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
4114 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004115 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004116 if (!packet_previous_rx_frame(po, &po->rx_ring,
4117 TP_STATUS_KERNEL))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004118 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
Willem de Bruijn2ccdbaa2015-05-12 11:56:48 -04004120 if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
Willem de Bruijn54d7c012015-05-14 15:25:02 -04004121 po->pressure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004123 spin_lock_bh(&sk->sk_write_queue.lock);
4124 if (po->tx_ring.pg_vec) {
4125 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08004126 mask |= EPOLLOUT | EPOLLWRNORM;
Johann Baudy69e3c752009-05-18 22:11:22 -07004127 }
4128 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 return mask;
4130}
4131
4132
4133/* Dirty? Well, I still did not learn better way to account
4134 * for user mmaps.
4135 */
4136
4137static void packet_mm_open(struct vm_area_struct *vma)
4138{
4139 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004140 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 if (sk)
4144 atomic_inc(&pkt_sk(sk)->mapped);
4145}
4146
4147static void packet_mm_close(struct vm_area_struct *vma)
4148{
4149 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004150 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 if (sk)
4154 atomic_dec(&pkt_sk(sk)->mapped);
4155}
4156
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004157static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004158 .open = packet_mm_open,
4159 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160};
4161
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004162static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
4163 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
4165 int i;
4166
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004167 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004168 if (likely(pg_vec[i].buffer)) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004169 if (is_vmalloc_addr(pg_vec[i].buffer))
4170 vfree(pg_vec[i].buffer);
4171 else
4172 free_pages((unsigned long)pg_vec[i].buffer,
4173 order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004174 pg_vec[i].buffer = NULL;
4175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
4177 kfree(pg_vec);
4178}
4179
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004180static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004181{
Daniel Borkmannf0d4eb22014-01-19 11:46:53 +01004182 char *buffer;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004183 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
4184 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07004185
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004186 buffer = (char *) __get_free_pages(gfp_flags, order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004187 if (buffer)
4188 return buffer;
4189
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004190 /* __get_free_pages failed, fall back to vmalloc */
4191 buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
4192 if (buffer)
4193 return buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -08004194
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004195 /* vmalloc failed, lets dig into swap here */
4196 gfp_flags &= ~__GFP_NORETRY;
4197 buffer = (char *) __get_free_pages(gfp_flags, order);
4198 if (buffer)
4199 return buffer;
4200
4201 /* complete and utter failure */
4202 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004203}
4204
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004205static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004206{
4207 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08004208 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004209 int i;
4210
Neil Horman0e3125c2010-11-16 10:26:47 -08004211 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004212 if (unlikely(!pg_vec))
4213 goto out;
4214
4215 for (i = 0; i < block_nr; i++) {
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004216 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08004217 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004218 goto out_free_pgvec;
4219 }
4220
4221out:
4222 return pg_vec;
4223
4224out_free_pgvec:
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004225 free_pg_vec(pg_vec, order, block_nr);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004226 pg_vec = NULL;
4227 goto out;
4228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
chetan lokef6fb8f12011-08-19 10:18:16 +00004230static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07004231 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
Neil Horman0e3125c2010-11-16 10:26:47 -08004233 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 struct packet_sock *po = pkt_sk(sk);
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004235 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004236 struct packet_ring_buffer *rb;
4237 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08004238 __be16 num;
chetan lokef6fb8f12011-08-19 10:18:16 +00004239 int err = -EINVAL;
4240 /* Added to avoid minimal code churn */
4241 struct tpacket_req *req = &req_u->req;
4242
Johann Baudy69e3c752009-05-18 22:11:22 -07004243 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
4244 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
4245
4246 err = -EBUSY;
4247 if (!closing) {
4248 if (atomic_read(&po->mapped))
4249 goto out;
Daniel Borkmannb0138402014-01-15 16:25:36 +01004250 if (packet_read_pending(rb))
Johann Baudy69e3c752009-05-18 22:11:22 -07004251 goto out;
4252 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 if (req->tp_block_nr) {
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004255 unsigned int min_frame_size;
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07004258 err = -EBUSY;
4259 if (unlikely(rb->pg_vec))
4260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004262 switch (po->tp_version) {
4263 case TPACKET_V1:
4264 po->tp_hdrlen = TPACKET_HDRLEN;
4265 break;
4266 case TPACKET_V2:
4267 po->tp_hdrlen = TPACKET2_HDRLEN;
4268 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004269 case TPACKET_V3:
4270 po->tp_hdrlen = TPACKET3_HDRLEN;
4271 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07004272 }
4273
Johann Baudy69e3c752009-05-18 22:11:22 -07004274 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004275 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004276 goto out;
Tobias Klauser90836b62015-11-17 10:40:21 +01004277 if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004278 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004279 min_frame_size = po->tp_hdrlen + po->tp_reserve;
Eric Dumazetdc808112014-08-15 09:16:04 -07004280 if (po->tp_version >= TPACKET_V3 &&
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004281 req->tp_block_size <
4282 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
Eric Dumazetdc808112014-08-15 09:16:04 -07004283 goto out;
Willem de Bruijn4576cd42018-08-06 10:38:34 -04004284 if (unlikely(req->tp_frame_size < min_frame_size))
Johann Baudy69e3c752009-05-18 22:11:22 -07004285 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004286 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07004287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Tobias Klauser4194b492015-11-17 10:38:36 +01004289 rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
4290 if (unlikely(rb->frames_per_block == 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07004291 goto out;
Kal Conleyfc628142019-02-10 09:57:11 +01004292 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
Andrey Konovalov8f8d28e2017-03-29 16:11:21 +02004293 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004294 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
4295 req->tp_frame_nr))
4296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298 err = -ENOMEM;
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004299 order = get_order(req->tp_block_size);
4300 pg_vec = alloc_pg_vec(req, order);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004301 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 goto out;
chetan lokef6fb8f12011-08-19 10:18:16 +00004303 switch (po->tp_version) {
4304 case TPACKET_V3:
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004305 /* Block transmit is not supported yet */
4306 if (!tx_ring) {
Maninder Singhe8e85cc2015-06-22 12:39:16 +05304307 init_prb_bdqc(po, rb, pg_vec, req_u);
Sowmini Varadhan7f953ab2017-01-03 06:31:47 -08004308 } else {
4309 struct tpacket_req3 *req3 = &req_u->req3;
4310
4311 if (req3->tp_retire_blk_tov ||
4312 req3->tp_sizeof_priv ||
4313 req3->tp_feature_req_word) {
4314 err = -EINVAL;
4315 goto out;
4316 }
4317 }
Dan Carpenterd7cf0c32014-02-18 15:20:51 +03004318 break;
chetan lokef6fb8f12011-08-19 10:18:16 +00004319 default:
4320 break;
4321 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004322 }
4323 /* Done */
4324 else {
4325 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004326 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07004327 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 /* Detach socket from network */
4332 spin_lock(&po->bind_lock);
4333 was_running = po->running;
4334 num = po->num;
4335 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07004337 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
4339 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 synchronize_net();
4342
4343 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08004344 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (closing || atomic_read(&po->mapped) == 0) {
4346 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07004347 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08004348 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07004349 rb->frame_max = (req->tp_frame_nr - 1);
4350 rb->head = 0;
4351 rb->frame_size = req->tp_frame_size;
4352 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004354 swap(rb->pg_vec_order, order);
Changli Gaoc053fd92010-12-10 16:02:20 -08004355 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Johann Baudy69e3c752009-05-18 22:11:22 -07004357 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
4358 po->prot_hook.func = (po->rx_ring.pg_vec) ?
4359 tpacket_rcv : packet_rcv;
4360 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004362 pr_err("packet_mmap: vma is busy: %d\n",
4363 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 }
Herbert Xu905db442009-01-30 14:12:06 -08004365 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
4367 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07004368 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07004370 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
4372 spin_unlock(&po->bind_lock);
WANG Congc800aaf2017-07-24 10:07:32 -07004373 if (pg_vec && (po->tp_version > TPACKET_V2)) {
chetan lokef6fb8f12011-08-19 10:18:16 +00004374 /* Because we don't support block-based V3 on tx-ring */
4375 if (!tx_ring)
Tobias Klauser73d0fcf2015-07-28 14:21:26 +02004376 prb_shutdown_retire_blk_timer(po, rb_queue);
chetan lokef6fb8f12011-08-19 10:18:16 +00004377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (pg_vec)
Eric Dumazet3a7ad062018-08-29 11:50:12 -07004380 free_pg_vec(pg_vec, order, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381out:
4382 return err;
4383}
4384
Johann Baudy69e3c752009-05-18 22:11:22 -07004385static int packet_mmap(struct file *file, struct socket *sock,
4386 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
4388 struct sock *sk = sock->sk;
4389 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07004390 unsigned long size, expected_size;
4391 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 unsigned long start;
4393 int err = -EINVAL;
4394 int i;
4395
4396 if (vma->vm_pgoff)
4397 return -EINVAL;
4398
Herbert Xu905db442009-01-30 14:12:06 -08004399 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07004400
4401 expected_size = 0;
4402 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4403 if (rb->pg_vec) {
4404 expected_size += rb->pg_vec_len
4405 * rb->pg_vec_pages
4406 * PAGE_SIZE;
4407 }
4408 }
4409
4410 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07004412
4413 size = vma->vm_end - vma->vm_start;
4414 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 goto out;
4416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07004418 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
4419 if (rb->pg_vec == NULL)
4420 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004421
Johann Baudy69e3c752009-05-18 22:11:22 -07004422 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08004423 struct page *page;
4424 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07004425 int pg_num;
4426
Changli Gaoc56b4d92010-12-01 02:52:57 +00004427 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
4428 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07004429 err = vm_insert_page(vma, start, page);
4430 if (unlikely(err))
4431 goto out;
4432 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08004433 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07004434 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
Johann Baudy69e3c752009-05-18 22:11:22 -07004437
David S. Miller4ebf0ae2005-12-06 16:38:35 -08004438 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 vma->vm_ops = &packet_mmap_ops;
4440 err = 0;
4441
4442out:
Herbert Xu905db442009-01-30 14:12:06 -08004443 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 return err;
4445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004447static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 .family = PF_PACKET,
4449 .owner = THIS_MODULE,
4450 .release = packet_release,
4451 .bind = packet_bind_spkt,
4452 .connect = sock_no_connect,
4453 .socketpair = sock_no_socketpair,
4454 .accept = sock_no_accept,
4455 .getname = packet_getname_spkt,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004456 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 .ioctl = packet_ioctl,
4458 .listen = sock_no_listen,
4459 .shutdown = sock_no_shutdown,
4460 .setsockopt = sock_no_setsockopt,
4461 .getsockopt = sock_no_getsockopt,
4462 .sendmsg = packet_sendmsg_spkt,
4463 .recvmsg = packet_recvmsg,
4464 .mmap = sock_no_mmap,
4465 .sendpage = sock_no_sendpage,
4466};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08004468static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 .family = PF_PACKET,
4470 .owner = THIS_MODULE,
4471 .release = packet_release,
4472 .bind = packet_bind,
4473 .connect = sock_no_connect,
4474 .socketpair = sock_no_socketpair,
4475 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004476 .getname = packet_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07004477 .poll = packet_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 .ioctl = packet_ioctl,
4479 .listen = sock_no_listen,
4480 .shutdown = sock_no_shutdown,
4481 .setsockopt = packet_setsockopt,
4482 .getsockopt = packet_getsockopt,
Willem de Bruijn719c44d2016-06-07 12:06:34 -04004483#ifdef CONFIG_COMPAT
4484 .compat_setsockopt = compat_packet_setsockopt,
4485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 .sendmsg = packet_sendmsg,
4487 .recvmsg = packet_recvmsg,
4488 .mmap = packet_mmap,
4489 .sendpage = sock_no_sendpage,
4490};
4491
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00004492static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 .family = PF_PACKET,
4494 .create = packet_create,
4495 .owner = THIS_MODULE,
4496};
4497
4498static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004499 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500};
4501
4502#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00004505 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
Denis V. Luneve372c412007-11-19 22:31:54 -08004507 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004508
4509 rcu_read_lock();
4510 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
4513static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4514{
Herbert Xu1bf40952007-12-16 14:04:02 -08004515 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00004516 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
4519static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00004520 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
stephen hemminger808f5112010-02-22 07:57:18 +00004522 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523}
4524
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09004525static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 if (v == SEQ_START_TOKEN)
4528 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
4529 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00004530 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 const struct packet_sock *po = pkt_sk(s);
4532
4533 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00004534 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03004536 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 s->sk_type,
4538 ntohs(po->num),
4539 po->ifindex,
4540 po->running,
4541 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06004542 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00004543 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 }
4545
4546 return 0;
4547}
4548
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004549static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 .start = packet_seq_start,
4551 .next = packet_seq_next,
4552 .stop = packet_seq_stop,
4553 .show = packet_seq_show,
4554};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555#endif
4556
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004557static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004558{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00004559 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08004560 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004561
Christoph Hellwigc3506372018-04-10 19:42:55 +02004562 if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
4563 sizeof(struct seq_net_private)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004564 return -ENOMEM;
4565
4566 return 0;
4567}
4568
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004569static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004570{
Gao fengece31ff2013-02-18 01:34:56 +00004571 remove_proc_entry("packet", net->proc_net);
Vasily Averin669f8f12017-11-12 22:27:49 +03004572 WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004573}
4574
4575static struct pernet_operations packet_net_ops = {
4576 .init = packet_net_init,
4577 .exit = packet_net_exit,
4578};
4579
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581static void __exit packet_exit(void)
4582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004584 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 sock_unregister(PF_PACKET);
4586 proto_unregister(&packet_proto);
4587}
4588
4589static int __init packet_init(void)
4590{
4591 int rc = proto_register(&packet_proto, 0);
4592
4593 if (rc != 0)
4594 goto out;
4595
4596 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08004597 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599out:
4600 return rc;
4601}
4602
4603module_init(packet_init);
4604module_exit(packet_exit);
4605MODULE_LICENSE("GPL");
4606MODULE_ALIAS_NETPROTO(PF_PACKET);