Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 4 | * operating system. INET is implemented using the BSD Socket |
| 5 | * interface as the means of communication with the user level. |
| 6 | * |
| 7 | * PACKET - implements raw packet sockets. |
| 8 | * |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 9 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 11 | * Alan Cox, <gw4pts@gw4pts.ampr.org> |
| 12 | * |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 13 | * Fixes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Alan Cox : verify_area() now used correctly |
| 15 | * Alan Cox : new skbuff lists, look ma no backlogs! |
| 16 | * Alan Cox : tidied skbuff lists. |
| 17 | * Alan Cox : Now uses generic datagram routines I |
| 18 | * added. Also fixed the peek/read crash |
| 19 | * from all old Linux datagram code. |
| 20 | * Alan Cox : Uses the improved datagram code. |
| 21 | * Alan Cox : Added NULL's for socket options. |
| 22 | * Alan Cox : Re-commented the code. |
| 23 | * Alan Cox : Use new kernel side addressing |
| 24 | * Rob Janssen : Correct MTU usage. |
| 25 | * Dave Platt : Counter leaks caused by incorrect |
| 26 | * interrupt locking and some slightly |
| 27 | * dubious gcc output. Can you read |
| 28 | * compiler: it said _VOLATILE_ |
| 29 | * Richard Kooijman : Timestamp fixes. |
| 30 | * Alan Cox : New buffers. Use sk->mac.raw. |
| 31 | * Alan Cox : sendmsg/recvmsg support. |
| 32 | * Alan Cox : Protocol setting support |
| 33 | * Alexey Kuznetsov : Untied from IPv4 stack. |
| 34 | * Cyrus Durgin : Fixed kerneld for kmod. |
| 35 | * Michal Ostrowski : Module initialization cleanup. |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 36 | * Ulises Alonso : Frame number limit removal and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * packet_set_ring memory leak. |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 38 | * Eric Biederman : Allow for > 8 byte hardware addresses. |
| 39 | * The convention is that longer addresses |
| 40 | * will simply extend the hardware address |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 41 | * byte arrays at the end of sockaddr_ll |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 42 | * and packet_mreq. |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 43 | * Johann Baudy : Added TX RING. |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 44 | * Chetan Loke : Implemented TPACKET_V3 block abstraction |
| 45 | * layer. |
| 46 | * Copyright (C) 2011, <lokec@ccs.neu.edu> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 48 | |
Baruch Siach | dc41c4a | 2021-09-09 20:49:47 +0300 | [diff] [blame] | 49 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 50 | |
Jakub Kicinski | cc69837 | 2020-11-20 14:50:52 -0800 | [diff] [blame] | 51 | #include <linux/ethtool.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <linux/mm.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 54 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/fcntl.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <linux/in.h> |
| 58 | #include <linux/inet.h> |
| 59 | #include <linux/netdevice.h> |
| 60 | #include <linux/if_packet.h> |
| 61 | #include <linux/wireless.h> |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 62 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/kmod.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 64 | #include <linux/slab.h> |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 65 | #include <linux/vmalloc.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 66 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <net/ip.h> |
| 68 | #include <net/protocol.h> |
| 69 | #include <linux/skbuff.h> |
| 70 | #include <net/sock.h> |
| 71 | #include <linux/errno.h> |
| 72 | #include <linux/timer.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 73 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include <asm/ioctls.h> |
| 75 | #include <asm/page.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 76 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #include <asm/io.h> |
| 78 | #include <linux/proc_fs.h> |
| 79 | #include <linux/seq_file.h> |
| 80 | #include <linux/poll.h> |
| 81 | #include <linux/module.h> |
| 82 | #include <linux/init.h> |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 83 | #include <linux/mutex.h> |
Eric Dumazet | 05423b2 | 2009-10-26 18:40:35 -0700 | [diff] [blame] | 84 | #include <linux/if_vlan.h> |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 85 | #include <linux/virtio_net.h> |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 86 | #include <linux/errqueue.h> |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 87 | #include <linux/net_tstamp.h> |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 88 | #include <linux/percpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #ifdef CONFIG_INET |
| 90 | #include <net/inet_common.h> |
| 91 | #endif |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 92 | #include <linux/bpf.h> |
Willem de Bruijn | 719c44d | 2016-06-07 12:06:34 -0400 | [diff] [blame] | 93 | #include <net/compat.h> |
Pablo Neira Ayuso | 0d7308c | 2021-10-08 22:06:04 +0200 | [diff] [blame] | 94 | #include <linux/netfilter_netdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Pavel Emelyanov | 2787b04 | 2012-08-13 05:49:39 +0000 | [diff] [blame] | 96 | #include "internal.h" |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | Assumptions: |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 100 | - If the device has no dev->header_ops->create, there is no LL header |
| 101 | visible above the device. In this case, its hard_header_len should be 0. |
Xie He | b4c5881 | 2020-09-14 00:41:54 -0700 | [diff] [blame] | 102 | The device may prepend its own header internally. In this case, its |
| 103 | needed_headroom should be set to the space needed for it to add its |
| 104 | internal header. |
| 105 | For example, a WiFi driver pretending to be an Ethernet driver should |
| 106 | set its hard_header_len to be the Ethernet header length, and set its |
| 107 | needed_headroom to be (the real WiFi header length - the fake Ethernet |
| 108 | header length). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | - packet socket receives packets with pulled ll header, |
| 110 | so that SOCK_RAW should push it back. |
| 111 | |
| 112 | On receive: |
| 113 | ----------- |
| 114 | |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 115 | Incoming, dev_has_header(dev) == true |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 116 | mac_header -> ll header |
| 117 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 119 | Outgoing, dev_has_header(dev) == true |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 120 | mac_header -> ll header |
| 121 | data -> ll header |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 123 | Incoming, dev_has_header(dev) == false |
Xie He | b79a80b | 2020-09-16 05:23:08 -0700 | [diff] [blame] | 124 | mac_header -> data |
| 125 | However drivers often make it point to the ll header. |
| 126 | This is incorrect because the ll header should be invisible to us. |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 127 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 129 | Outgoing, dev_has_header(dev) == false |
Xie He | b79a80b | 2020-09-16 05:23:08 -0700 | [diff] [blame] | 130 | mac_header -> data. ll header is invisible to us. |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 131 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | Resume |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 134 | If dev_has_header(dev) == false we are unable to restore the ll header, |
Xie He | b79a80b | 2020-09-16 05:23:08 -0700 | [diff] [blame] | 135 | because it is invisible to us. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | |
| 138 | On transmit: |
| 139 | ------------ |
| 140 | |
Xie He | 21c85974 | 2021-02-05 14:41:24 -0800 | [diff] [blame] | 141 | dev_has_header(dev) == true |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 142 | mac_header -> ll header |
| 143 | data -> ll header |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Xie He | 21c85974 | 2021-02-05 14:41:24 -0800 | [diff] [blame] | 145 | dev_has_header(dev) == false (ll header is invisible to us) |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 146 | mac_header -> data |
| 147 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Xie He | 0959972 | 2020-09-18 06:56:16 -0700 | [diff] [blame] | 149 | We should set network_header on output to the correct position, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | packet classifier depends on it. |
| 151 | */ |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* Private packet socket structures. */ |
| 154 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 155 | /* identical to struct packet_mreq except it has |
| 156 | * a longer address field. |
| 157 | */ |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 158 | struct packet_mreq_max { |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 159 | int mr_ifindex; |
| 160 | unsigned short mr_type; |
| 161 | unsigned short mr_alen; |
| 162 | unsigned char mr_address[MAX_ADDR_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | }; |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 164 | |
Daniel Borkmann | 184f489 | 2013-04-16 01:57:46 +0000 | [diff] [blame] | 165 | union tpacket_uhdr { |
| 166 | struct tpacket_hdr *h1; |
| 167 | struct tpacket2_hdr *h2; |
| 168 | struct tpacket3_hdr *h3; |
| 169 | void *raw; |
| 170 | }; |
| 171 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 172 | static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 173 | int closing, int tx_ring); |
| 174 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 175 | #define V3_ALIGNMENT (8) |
| 176 | |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 177 | #define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT)) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 178 | |
| 179 | #define BLK_PLUS_PRIV(sz_of_priv) \ |
| 180 | (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT)) |
| 181 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 182 | #define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status) |
| 183 | #define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts) |
| 184 | #define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt) |
| 185 | #define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len) |
| 186 | #define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num) |
| 187 | #define BLOCK_O2PRIV(x) ((x)->offset_to_priv) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 188 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 189 | struct packet_sock; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 190 | static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, |
| 191 | struct packet_type *pt, struct net_device *orig_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 193 | static void *packet_previous_frame(struct packet_sock *po, |
| 194 | struct packet_ring_buffer *rb, |
| 195 | int status); |
| 196 | static void packet_increment_head(struct packet_ring_buffer *buff); |
Rosen, Rami | 878cd3b | 2017-05-24 18:34:11 +0300 | [diff] [blame] | 197 | static int prb_curr_blk_in_use(struct tpacket_block_desc *); |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 198 | static void *prb_dispatch_next_block(struct tpacket_kbdq_core *, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 199 | struct packet_sock *); |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 200 | static void prb_retire_current_block(struct tpacket_kbdq_core *, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 201 | struct packet_sock *, unsigned int status); |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 202 | static int prb_queue_frozen(struct tpacket_kbdq_core *); |
| 203 | static void prb_open_block(struct tpacket_kbdq_core *, |
| 204 | struct tpacket_block_desc *); |
Kees Cook | 17bfd8c | 2017-10-24 01:46:26 -0700 | [diff] [blame] | 205 | static void prb_retire_rx_blk_timer_expired(struct timer_list *); |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 206 | static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *); |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 207 | static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *); |
| 208 | static void prb_clear_rxhash(struct tpacket_kbdq_core *, |
| 209 | struct tpacket3_hdr *); |
| 210 | static void prb_fill_vlan_info(struct tpacket_kbdq_core *, |
| 211 | struct tpacket3_hdr *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static void packet_flush_mclist(struct sock *sk); |
Magnus Karlsson | 865b03f | 2018-05-02 13:01:33 +0200 | [diff] [blame] | 213 | static u16 packet_pick_tx_queue(struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 215 | struct packet_skb_cb { |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 216 | union { |
| 217 | struct sockaddr_pkt pkt; |
Eyal Birger | 2472d76 | 2015-03-01 14:58:28 +0200 | [diff] [blame] | 218 | union { |
| 219 | /* Trick: alias skb original length with |
| 220 | * ll.sll_family and ll.protocol in order |
| 221 | * to save room. |
| 222 | */ |
| 223 | unsigned int origlen; |
| 224 | struct sockaddr_ll ll; |
| 225 | }; |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 226 | } sa; |
| 227 | }; |
| 228 | |
David Woodhouse | d3869ef | 2015-09-23 19:45:08 +0100 | [diff] [blame] | 229 | #define vio_le() virtio_legacy_is_little_endian() |
| 230 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 231 | #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb)) |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 232 | |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 233 | #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc)) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 234 | #define GET_PBLOCK_DESC(x, bid) \ |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 235 | ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer)) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 236 | #define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \ |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 237 | ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer)) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 238 | #define GET_NEXT_PRB_BLK_NUM(x) \ |
| 239 | (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \ |
| 240 | ((x)->kactive_blk_num+1) : 0) |
| 241 | |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 242 | static void __fanout_unlink(struct sock *sk, struct packet_sock *po); |
| 243 | static void __fanout_link(struct sock *sk, struct packet_sock *po); |
| 244 | |
Pablo Neira Ayuso | 0d7308c | 2021-10-08 22:06:04 +0200 | [diff] [blame] | 245 | #ifdef CONFIG_NETFILTER_EGRESS |
| 246 | static noinline struct sk_buff *nf_hook_direct_egress(struct sk_buff *skb) |
| 247 | { |
| 248 | struct sk_buff *next, *head = NULL, *tail; |
| 249 | int rc; |
| 250 | |
| 251 | rcu_read_lock(); |
| 252 | for (; skb != NULL; skb = next) { |
| 253 | next = skb->next; |
| 254 | skb_mark_not_on_list(skb); |
| 255 | |
| 256 | if (!nf_hook_egress(skb, &rc, skb->dev)) |
| 257 | continue; |
| 258 | |
| 259 | if (!head) |
| 260 | head = skb; |
| 261 | else |
| 262 | tail->next = skb; |
| 263 | |
| 264 | tail = skb; |
| 265 | } |
| 266 | rcu_read_unlock(); |
| 267 | |
| 268 | return head; |
| 269 | } |
| 270 | #endif |
| 271 | |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 272 | static int packet_direct_xmit(struct sk_buff *skb) |
| 273 | { |
Pablo Neira Ayuso | 0d7308c | 2021-10-08 22:06:04 +0200 | [diff] [blame] | 274 | #ifdef CONFIG_NETFILTER_EGRESS |
| 275 | if (nf_hook_egress_active()) { |
| 276 | skb = nf_hook_direct_egress(skb); |
| 277 | if (!skb) |
| 278 | return NET_XMIT_DROP; |
| 279 | } |
| 280 | #endif |
Magnus Karlsson | 865b03f | 2018-05-02 13:01:33 +0200 | [diff] [blame] | 281 | return dev_direct_xmit(skb, packet_pick_tx_queue(skb)); |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 284 | static struct net_device *packet_cached_dev_get(struct packet_sock *po) |
| 285 | { |
| 286 | struct net_device *dev; |
| 287 | |
| 288 | rcu_read_lock(); |
| 289 | dev = rcu_dereference(po->cached_dev); |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 290 | dev_hold(dev); |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 291 | rcu_read_unlock(); |
| 292 | |
| 293 | return dev; |
| 294 | } |
| 295 | |
| 296 | static void packet_cached_dev_assign(struct packet_sock *po, |
| 297 | struct net_device *dev) |
| 298 | { |
| 299 | rcu_assign_pointer(po->cached_dev, dev); |
| 300 | } |
| 301 | |
| 302 | static void packet_cached_dev_reset(struct packet_sock *po) |
| 303 | { |
| 304 | RCU_INIT_POINTER(po->cached_dev, NULL); |
| 305 | } |
| 306 | |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 307 | static bool packet_use_direct_xmit(const struct packet_sock *po) |
| 308 | { |
| 309 | return po->xmit == packet_direct_xmit; |
| 310 | } |
| 311 | |
Magnus Karlsson | 865b03f | 2018-05-02 13:01:33 +0200 | [diff] [blame] | 312 | static u16 packet_pick_tx_queue(struct sk_buff *skb) |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 313 | { |
Magnus Karlsson | 865b03f | 2018-05-02 13:01:33 +0200 | [diff] [blame] | 314 | struct net_device *dev = skb->dev; |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 315 | const struct net_device_ops *ops = dev->netdev_ops; |
Paolo Abeni | b71b583 | 2019-03-20 11:02:05 +0100 | [diff] [blame] | 316 | int cpu = raw_smp_processor_id(); |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 317 | u16 queue_index; |
| 318 | |
Paolo Abeni | b71b583 | 2019-03-20 11:02:05 +0100 | [diff] [blame] | 319 | #ifdef CONFIG_XPS |
| 320 | skb->sender_cpu = cpu + 1; |
| 321 | #endif |
| 322 | skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues); |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 323 | if (ops->ndo_select_queue) { |
Paolo Abeni | a350ecc | 2019-03-20 11:02:06 +0100 | [diff] [blame] | 324 | queue_index = ops->ndo_select_queue(dev, skb, NULL); |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 325 | queue_index = netdev_cap_txqueue(dev, queue_index); |
| 326 | } else { |
Paolo Abeni | b71b583 | 2019-03-20 11:02:05 +0100 | [diff] [blame] | 327 | queue_index = netdev_pick_tx(dev, skb, NULL); |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Magnus Karlsson | 865b03f | 2018-05-02 13:01:33 +0200 | [diff] [blame] | 330 | return queue_index; |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 333 | /* __register_prot_hook must be invoked through register_prot_hook |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 334 | * or from a context in which asynchronous accesses to the packet |
| 335 | * socket is not possible (packet_create()). |
| 336 | */ |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 337 | static void __register_prot_hook(struct sock *sk) |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 338 | { |
| 339 | struct packet_sock *po = pkt_sk(sk); |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 340 | |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 341 | if (!po->running) { |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 342 | if (po->fanout) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 343 | __fanout_link(sk, po); |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 344 | else |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 345 | dev_add_pack(&po->prot_hook); |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 346 | |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 347 | sock_hold(sk); |
| 348 | po->running = 1; |
| 349 | } |
| 350 | } |
| 351 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 352 | static void register_prot_hook(struct sock *sk) |
| 353 | { |
| 354 | lockdep_assert_held_once(&pkt_sk(sk)->bind_lock); |
| 355 | __register_prot_hook(sk); |
| 356 | } |
| 357 | |
| 358 | /* If the sync parameter is true, we will temporarily drop |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 359 | * the po->bind_lock and do a synchronize_net to make sure no |
| 360 | * asynchronous packet processing paths still refer to the elements |
| 361 | * of po->prot_hook. If the sync parameter is false, it is the |
| 362 | * callers responsibility to take care of this. |
| 363 | */ |
| 364 | static void __unregister_prot_hook(struct sock *sk, bool sync) |
| 365 | { |
| 366 | struct packet_sock *po = pkt_sk(sk); |
| 367 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 368 | lockdep_assert_held_once(&po->bind_lock); |
| 369 | |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 370 | po->running = 0; |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 371 | |
| 372 | if (po->fanout) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 373 | __fanout_unlink(sk, po); |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 374 | else |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 375 | __dev_remove_pack(&po->prot_hook); |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 376 | |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 377 | __sock_put(sk); |
| 378 | |
| 379 | if (sync) { |
| 380 | spin_unlock(&po->bind_lock); |
| 381 | synchronize_net(); |
| 382 | spin_lock(&po->bind_lock); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static void unregister_prot_hook(struct sock *sk, bool sync) |
| 387 | { |
| 388 | struct packet_sock *po = pkt_sk(sk); |
| 389 | |
| 390 | if (po->running) |
| 391 | __unregister_prot_hook(sk, sync); |
| 392 | } |
| 393 | |
Michael S. Tsirkin | 6e58040 | 2014-11-24 13:32:16 +0200 | [diff] [blame] | 394 | static inline struct page * __pure pgv_to_page(void *addr) |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 395 | { |
| 396 | if (is_vmalloc_addr(addr)) |
| 397 | return vmalloc_to_page(addr); |
| 398 | return virt_to_page(addr); |
| 399 | } |
| 400 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 401 | static void __packet_set_status(struct packet_sock *po, void *frame, int status) |
| 402 | { |
Daniel Borkmann | 184f489 | 2013-04-16 01:57:46 +0000 | [diff] [blame] | 403 | union tpacket_uhdr h; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 404 | |
| 405 | h.raw = frame; |
| 406 | switch (po->tp_version) { |
| 407 | case TPACKET_V1: |
| 408 | h.h1->tp_status = status; |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 409 | flush_dcache_page(pgv_to_page(&h.h1->tp_status)); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 410 | break; |
| 411 | case TPACKET_V2: |
| 412 | h.h2->tp_status = status; |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 413 | flush_dcache_page(pgv_to_page(&h.h2->tp_status)); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 414 | break; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 415 | case TPACKET_V3: |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 416 | h.h3->tp_status = status; |
| 417 | flush_dcache_page(pgv_to_page(&h.h3->tp_status)); |
| 418 | break; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 419 | default: |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 420 | WARN(1, "TPACKET version not supported.\n"); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 421 | BUG(); |
| 422 | } |
| 423 | |
| 424 | smp_wmb(); |
| 425 | } |
| 426 | |
Eric Dumazet | 96f657e | 2019-06-12 09:52:26 -0700 | [diff] [blame] | 427 | static int __packet_get_status(const struct packet_sock *po, void *frame) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 428 | { |
Daniel Borkmann | 184f489 | 2013-04-16 01:57:46 +0000 | [diff] [blame] | 429 | union tpacket_uhdr h; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 430 | |
| 431 | smp_rmb(); |
| 432 | |
| 433 | h.raw = frame; |
| 434 | switch (po->tp_version) { |
| 435 | case TPACKET_V1: |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 436 | flush_dcache_page(pgv_to_page(&h.h1->tp_status)); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 437 | return h.h1->tp_status; |
| 438 | case TPACKET_V2: |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 439 | flush_dcache_page(pgv_to_page(&h.h2->tp_status)); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 440 | return h.h2->tp_status; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 441 | case TPACKET_V3: |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 442 | flush_dcache_page(pgv_to_page(&h.h3->tp_status)); |
| 443 | return h.h3->tp_status; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 444 | default: |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 445 | WARN(1, "TPACKET version not supported.\n"); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 446 | BUG(); |
| 447 | return 0; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 450 | |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 451 | static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts, |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 452 | unsigned int flags) |
Daniel Borkmann | 7a51384 | 2013-04-23 00:39:29 +0000 | [diff] [blame] | 453 | { |
| 454 | struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); |
| 455 | |
Willem de Bruijn | 68a360e | 2014-07-25 18:01:31 -0400 | [diff] [blame] | 456 | if (shhwtstamps && |
| 457 | (flags & SOF_TIMESTAMPING_RAW_HARDWARE) && |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 458 | ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts)) |
Willem de Bruijn | 68a360e | 2014-07-25 18:01:31 -0400 | [diff] [blame] | 459 | return TP_STATUS_TS_RAW_HARDWARE; |
Daniel Borkmann | 7a51384 | 2013-04-23 00:39:29 +0000 | [diff] [blame] | 460 | |
Richard Sanger | 171c3b1 | 2021-05-12 13:31:22 +1200 | [diff] [blame] | 461 | if ((flags & SOF_TIMESTAMPING_SOFTWARE) && |
| 462 | ktime_to_timespec64_cond(skb->tstamp, ts)) |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 463 | return TP_STATUS_TS_SOFTWARE; |
Daniel Borkmann | 7a51384 | 2013-04-23 00:39:29 +0000 | [diff] [blame] | 464 | |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 465 | return 0; |
Daniel Borkmann | 7a51384 | 2013-04-23 00:39:29 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 468 | static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame, |
| 469 | struct sk_buff *skb) |
Willem de Bruijn | 2e31396 | 2013-04-23 00:39:28 +0000 | [diff] [blame] | 470 | { |
| 471 | union tpacket_uhdr h; |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 472 | struct timespec64 ts; |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 473 | __u32 ts_status; |
Willem de Bruijn | 2e31396 | 2013-04-23 00:39:28 +0000 | [diff] [blame] | 474 | |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 475 | if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp))) |
| 476 | return 0; |
Willem de Bruijn | 2e31396 | 2013-04-23 00:39:28 +0000 | [diff] [blame] | 477 | |
| 478 | h.raw = frame; |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 479 | /* |
| 480 | * versions 1 through 3 overflow the timestamps in y2106, since they |
| 481 | * all store the seconds in a 32-bit unsigned integer. |
| 482 | * If we create a version 4, that should have a 64-bit timestamp, |
| 483 | * either 64-bit seconds + 32-bit nanoseconds, or just 64-bit |
| 484 | * nanoseconds. |
| 485 | */ |
Willem de Bruijn | 2e31396 | 2013-04-23 00:39:28 +0000 | [diff] [blame] | 486 | switch (po->tp_version) { |
| 487 | case TPACKET_V1: |
| 488 | h.h1->tp_sec = ts.tv_sec; |
| 489 | h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 490 | break; |
| 491 | case TPACKET_V2: |
| 492 | h.h2->tp_sec = ts.tv_sec; |
| 493 | h.h2->tp_nsec = ts.tv_nsec; |
| 494 | break; |
| 495 | case TPACKET_V3: |
Daniel Borkmann | 57ea884 | 2017-01-05 02:34:28 +0100 | [diff] [blame] | 496 | h.h3->tp_sec = ts.tv_sec; |
| 497 | h.h3->tp_nsec = ts.tv_nsec; |
| 498 | break; |
Willem de Bruijn | 2e31396 | 2013-04-23 00:39:28 +0000 | [diff] [blame] | 499 | default: |
| 500 | WARN(1, "TPACKET version not supported.\n"); |
| 501 | BUG(); |
| 502 | } |
| 503 | |
| 504 | /* one flush is safe, as both fields always lie on the same cacheline */ |
| 505 | flush_dcache_page(pgv_to_page(&h.h1->tp_sec)); |
| 506 | smp_wmb(); |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 507 | |
| 508 | return ts_status; |
Willem de Bruijn | 2e31396 | 2013-04-23 00:39:28 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Eric Dumazet | d4b5bd9 | 2019-06-12 09:52:27 -0700 | [diff] [blame] | 511 | static void *packet_lookup_frame(const struct packet_sock *po, |
| 512 | const struct packet_ring_buffer *rb, |
| 513 | unsigned int position, |
| 514 | int status) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 515 | { |
| 516 | unsigned int pg_vec_pos, frame_offset; |
Daniel Borkmann | 184f489 | 2013-04-16 01:57:46 +0000 | [diff] [blame] | 517 | union tpacket_uhdr h; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 518 | |
| 519 | pg_vec_pos = position / rb->frames_per_block; |
| 520 | frame_offset = position % rb->frames_per_block; |
| 521 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 522 | h.raw = rb->pg_vec[pg_vec_pos].buffer + |
| 523 | (frame_offset * rb->frame_size); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 524 | |
| 525 | if (status != __packet_get_status(po, h.raw)) |
| 526 | return NULL; |
| 527 | |
| 528 | return h.raw; |
| 529 | } |
| 530 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 531 | static void *packet_current_frame(struct packet_sock *po, |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 532 | struct packet_ring_buffer *rb, |
| 533 | int status) |
| 534 | { |
| 535 | return packet_lookup_frame(po, rb, rb->head, status); |
| 536 | } |
| 537 | |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 538 | static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 539 | { |
| 540 | del_timer_sync(&pkc->retire_blk_timer); |
| 541 | } |
| 542 | |
| 543 | static void prb_shutdown_retire_blk_timer(struct packet_sock *po, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 544 | struct sk_buff_head *rb_queue) |
| 545 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 546 | struct tpacket_kbdq_core *pkc; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 547 | |
Tobias Klauser | 73d0fcf | 2015-07-28 14:21:26 +0200 | [diff] [blame] | 548 | pkc = GET_PBDQC_FROM_RB(&po->rx_ring); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 549 | |
Veaceslav Falico | ec6f809 | 2013-11-29 09:53:23 +0100 | [diff] [blame] | 550 | spin_lock_bh(&rb_queue->lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 551 | pkc->delete_blk_timer = 1; |
Veaceslav Falico | ec6f809 | 2013-11-29 09:53:23 +0100 | [diff] [blame] | 552 | spin_unlock_bh(&rb_queue->lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 553 | |
| 554 | prb_del_retire_blk_timer(pkc); |
| 555 | } |
| 556 | |
Maninder Singh | e8e85cc | 2015-06-22 12:39:16 +0530 | [diff] [blame] | 557 | static void prb_setup_retire_blk_timer(struct packet_sock *po) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 558 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 559 | struct tpacket_kbdq_core *pkc; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 560 | |
Maninder Singh | e8e85cc | 2015-06-22 12:39:16 +0530 | [diff] [blame] | 561 | pkc = GET_PBDQC_FROM_RB(&po->rx_ring); |
Kees Cook | 17bfd8c | 2017-10-24 01:46:26 -0700 | [diff] [blame] | 562 | timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired, |
| 563 | 0); |
| 564 | pkc->retire_blk_timer.expires = jiffies; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static int prb_calc_retire_blk_tmo(struct packet_sock *po, |
| 568 | int blk_size_in_bytes) |
| 569 | { |
| 570 | struct net_device *dev; |
Mao Wenan | 0914d2b | 2019-12-23 18:42:57 +0800 | [diff] [blame] | 571 | unsigned int mbits, div; |
David Decotigny | 7cad1ba | 2016-02-24 10:58:10 -0800 | [diff] [blame] | 572 | struct ethtool_link_ksettings ecmd; |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 573 | int err; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 574 | |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 575 | rtnl_lock(); |
| 576 | dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex); |
| 577 | if (unlikely(!dev)) { |
| 578 | rtnl_unlock(); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 579 | return DEFAULT_PRB_RETIRE_TOV; |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 580 | } |
David Decotigny | 7cad1ba | 2016-02-24 10:58:10 -0800 | [diff] [blame] | 581 | err = __ethtool_get_link_ksettings(dev, &ecmd); |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 582 | rtnl_unlock(); |
Mao Wenan | 0914d2b | 2019-12-23 18:42:57 +0800 | [diff] [blame] | 583 | if (err) |
Mao Wenan | b43d1f9 | 2019-12-09 21:31:25 +0800 | [diff] [blame] | 584 | return DEFAULT_PRB_RETIRE_TOV; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 585 | |
Mao Wenan | 0914d2b | 2019-12-23 18:42:57 +0800 | [diff] [blame] | 586 | /* If the link speed is so slow you don't really |
| 587 | * need to worry about perf anyways |
| 588 | */ |
| 589 | if (ecmd.base.speed < SPEED_1000 || |
| 590 | ecmd.base.speed == SPEED_UNKNOWN) |
| 591 | return DEFAULT_PRB_RETIRE_TOV; |
| 592 | |
| 593 | div = ecmd.base.speed / 1000; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 594 | mbits = (blk_size_in_bytes * 8) / (1024 * 1024); |
| 595 | |
| 596 | if (div) |
| 597 | mbits /= div; |
| 598 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 599 | if (div) |
Mao Wenan | 0914d2b | 2019-12-23 18:42:57 +0800 | [diff] [blame] | 600 | return mbits + 1; |
| 601 | return mbits; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 602 | } |
| 603 | |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 604 | static void prb_init_ft_ops(struct tpacket_kbdq_core *p1, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 605 | union tpacket_req_u *req_u) |
| 606 | { |
| 607 | p1->feature_req_word = req_u->req3.tp_feature_req_word; |
| 608 | } |
| 609 | |
| 610 | static void init_prb_bdqc(struct packet_sock *po, |
| 611 | struct packet_ring_buffer *rb, |
| 612 | struct pgv *pg_vec, |
Maninder Singh | e8e85cc | 2015-06-22 12:39:16 +0530 | [diff] [blame] | 613 | union tpacket_req_u *req_u) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 614 | { |
Duan Jiong | 22781a5 | 2013-12-06 13:29:36 +0800 | [diff] [blame] | 615 | struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb); |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 616 | struct tpacket_block_desc *pbd; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 617 | |
| 618 | memset(p1, 0x0, sizeof(*p1)); |
| 619 | |
| 620 | p1->knxt_seq_num = 1; |
| 621 | p1->pkbdq = pg_vec; |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 622 | pbd = (struct tpacket_block_desc *)pg_vec[0].buffer; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 623 | p1->pkblk_start = pg_vec[0].buffer; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 624 | p1->kblk_size = req_u->req3.tp_block_size; |
| 625 | p1->knum_blocks = req_u->req3.tp_block_nr; |
| 626 | p1->hdrlen = po->tp_hdrlen; |
| 627 | p1->version = po->tp_version; |
| 628 | p1->last_kactive_blk_num = 0; |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 629 | po->stats.stats3.tp_freeze_q_cnt = 0; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 630 | if (req_u->req3.tp_retire_blk_tov) |
| 631 | p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov; |
| 632 | else |
| 633 | p1->retire_blk_tov = prb_calc_retire_blk_tmo(po, |
| 634 | req_u->req3.tp_block_size); |
| 635 | p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov); |
| 636 | p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv; |
John Ogness | 632ca50 | 2020-07-07 17:28:04 +0206 | [diff] [blame] | 637 | rwlock_init(&p1->blk_fill_in_prog_lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 638 | |
Eric Dumazet | dc80811 | 2014-08-15 09:16:04 -0700 | [diff] [blame] | 639 | p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 640 | prb_init_ft_ops(p1, req_u); |
Maninder Singh | e8e85cc | 2015-06-22 12:39:16 +0530 | [diff] [blame] | 641 | prb_setup_retire_blk_timer(po); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 642 | prb_open_block(p1, pbd); |
| 643 | } |
| 644 | |
| 645 | /* Do NOT update the last_blk_num first. |
| 646 | * Assumes sk_buff_head lock is held. |
| 647 | */ |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 648 | static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 649 | { |
| 650 | mod_timer(&pkc->retire_blk_timer, |
| 651 | jiffies + pkc->tov_in_jiffies); |
| 652 | pkc->last_kactive_blk_num = pkc->kactive_blk_num; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Timer logic: |
| 657 | * 1) We refresh the timer only when we open a block. |
| 658 | * By doing this we don't waste cycles refreshing the timer |
| 659 | * on packet-by-packet basis. |
| 660 | * |
| 661 | * With a 1MB block-size, on a 1Gbps line, it will take |
| 662 | * i) ~8 ms to fill a block + ii) memcpy etc. |
| 663 | * In this cut we are not accounting for the memcpy time. |
| 664 | * |
| 665 | * So, if the user sets the 'tmo' to 10ms then the timer |
| 666 | * will never fire while the block is still getting filled |
| 667 | * (which is what we want). However, the user could choose |
| 668 | * to close a block early and that's fine. |
| 669 | * |
| 670 | * But when the timer does fire, we check whether or not to refresh it. |
| 671 | * Since the tmo granularity is in msecs, it is not too expensive |
| 672 | * to refresh the timer, lets say every '8' msecs. |
| 673 | * Either the user can set the 'tmo' or we can derive it based on |
| 674 | * a) line-speed and b) block-size. |
| 675 | * prb_calc_retire_blk_tmo() calculates the tmo. |
| 676 | * |
| 677 | */ |
Kees Cook | 17bfd8c | 2017-10-24 01:46:26 -0700 | [diff] [blame] | 678 | static void prb_retire_rx_blk_timer_expired(struct timer_list *t) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 679 | { |
Kees Cook | 17bfd8c | 2017-10-24 01:46:26 -0700 | [diff] [blame] | 680 | struct packet_sock *po = |
| 681 | from_timer(po, t, rx_ring.prb_bdqc.retire_blk_timer); |
Duan Jiong | 22781a5 | 2013-12-06 13:29:36 +0800 | [diff] [blame] | 682 | struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 683 | unsigned int frozen; |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 684 | struct tpacket_block_desc *pbd; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 685 | |
| 686 | spin_lock(&po->sk.sk_receive_queue.lock); |
| 687 | |
| 688 | frozen = prb_queue_frozen(pkc); |
| 689 | pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc); |
| 690 | |
| 691 | if (unlikely(pkc->delete_blk_timer)) |
| 692 | goto out; |
| 693 | |
| 694 | /* We only need to plug the race when the block is partially filled. |
| 695 | * tpacket_rcv: |
| 696 | * lock(); increment BLOCK_NUM_PKTS; unlock() |
| 697 | * copy_bits() is in progress ... |
| 698 | * timer fires on other cpu: |
| 699 | * we can't retire the current block because copy_bits |
| 700 | * is in progress. |
| 701 | * |
| 702 | */ |
| 703 | if (BLOCK_NUM_PKTS(pbd)) { |
John Ogness | 632ca50 | 2020-07-07 17:28:04 +0206 | [diff] [blame] | 704 | /* Waiting for skb_copy_bits to finish... */ |
| 705 | write_lock(&pkc->blk_fill_in_prog_lock); |
| 706 | write_unlock(&pkc->blk_fill_in_prog_lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) { |
| 710 | if (!frozen) { |
Alexander Drozdov | 41a50d6 | 2015-02-24 08:18:28 +0300 | [diff] [blame] | 711 | if (!BLOCK_NUM_PKTS(pbd)) { |
| 712 | /* An empty block. Just refresh the timer. */ |
| 713 | goto refresh_timer; |
| 714 | } |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 715 | prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO); |
| 716 | if (!prb_dispatch_next_block(pkc, po)) |
| 717 | goto refresh_timer; |
| 718 | else |
| 719 | goto out; |
| 720 | } else { |
| 721 | /* Case 1. Queue was frozen because user-space was |
| 722 | * lagging behind. |
| 723 | */ |
Rosen, Rami | 878cd3b | 2017-05-24 18:34:11 +0300 | [diff] [blame] | 724 | if (prb_curr_blk_in_use(pbd)) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 725 | /* |
| 726 | * Ok, user-space is still behind. |
| 727 | * So just refresh the timer. |
| 728 | */ |
| 729 | goto refresh_timer; |
| 730 | } else { |
| 731 | /* Case 2. queue was frozen,user-space caught up, |
| 732 | * now the link went idle && the timer fired. |
| 733 | * We don't have a block to close.So we open this |
| 734 | * block and restart the timer. |
| 735 | * opening a block thaws the queue,restarts timer |
| 736 | * Thawing/timer-refresh is a side effect. |
| 737 | */ |
| 738 | prb_open_block(pkc, pbd); |
| 739 | goto out; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | refresh_timer: |
| 745 | _prb_refresh_rx_retire_blk_timer(pkc); |
| 746 | |
| 747 | out: |
| 748 | spin_unlock(&po->sk.sk_receive_queue.lock); |
| 749 | } |
| 750 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 751 | static void prb_flush_block(struct tpacket_kbdq_core *pkc1, |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 752 | struct tpacket_block_desc *pbd1, __u32 status) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 753 | { |
| 754 | /* Flush everything minus the block header */ |
| 755 | |
| 756 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 |
| 757 | u8 *start, *end; |
| 758 | |
| 759 | start = (u8 *)pbd1; |
| 760 | |
| 761 | /* Skip the block header(we know header WILL fit in 4K) */ |
| 762 | start += PAGE_SIZE; |
| 763 | |
| 764 | end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end); |
| 765 | for (; start < end; start += PAGE_SIZE) |
| 766 | flush_dcache_page(pgv_to_page(start)); |
| 767 | |
| 768 | smp_wmb(); |
| 769 | #endif |
| 770 | |
| 771 | /* Now update the block status. */ |
| 772 | |
| 773 | BLOCK_STATUS(pbd1) = status; |
| 774 | |
| 775 | /* Flush the block header */ |
| 776 | |
| 777 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 |
| 778 | start = (u8 *)pbd1; |
| 779 | flush_dcache_page(pgv_to_page(start)); |
| 780 | |
| 781 | smp_wmb(); |
| 782 | #endif |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * Side effect: |
| 787 | * |
| 788 | * 1) flush the block |
| 789 | * 2) Increment active_blk_num |
| 790 | * |
| 791 | * Note:We DONT refresh the timer on purpose. |
| 792 | * Because almost always the next block will be opened. |
| 793 | */ |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 794 | static void prb_close_block(struct tpacket_kbdq_core *pkc1, |
| 795 | struct tpacket_block_desc *pbd1, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 796 | struct packet_sock *po, unsigned int stat) |
| 797 | { |
| 798 | __u32 status = TP_STATUS_USER | stat; |
| 799 | |
| 800 | struct tpacket3_hdr *last_pkt; |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 801 | struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1; |
Dan Collins | da413ee | 2014-12-19 16:49:25 +1300 | [diff] [blame] | 802 | struct sock *sk = &po->sk; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 803 | |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 804 | if (atomic_read(&po->tp_drops)) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 805 | status |= TP_STATUS_LOSING; |
| 806 | |
| 807 | last_pkt = (struct tpacket3_hdr *)pkc1->prev; |
| 808 | last_pkt->tp_next_offset = 0; |
| 809 | |
| 810 | /* Get the ts of the last pkt */ |
| 811 | if (BLOCK_NUM_PKTS(pbd1)) { |
| 812 | h1->ts_last_pkt.ts_sec = last_pkt->tp_sec; |
| 813 | h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec; |
| 814 | } else { |
Alexander Drozdov | 41a50d6 | 2015-02-24 08:18:28 +0300 | [diff] [blame] | 815 | /* Ok, we tmo'd - so get the current time. |
| 816 | * |
| 817 | * It shouldn't really happen as we don't close empty |
| 818 | * blocks. See prb_retire_rx_blk_timer_expired(). |
| 819 | */ |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 820 | struct timespec64 ts; |
| 821 | ktime_get_real_ts64(&ts); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 822 | h1->ts_last_pkt.ts_sec = ts.tv_sec; |
| 823 | h1->ts_last_pkt.ts_nsec = ts.tv_nsec; |
| 824 | } |
| 825 | |
| 826 | smp_wmb(); |
| 827 | |
| 828 | /* Flush the block */ |
| 829 | prb_flush_block(pkc1, pbd1, status); |
| 830 | |
Dan Collins | da413ee | 2014-12-19 16:49:25 +1300 | [diff] [blame] | 831 | sk->sk_data_ready(sk); |
| 832 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 833 | pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1); |
| 834 | } |
| 835 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 836 | static void prb_thaw_queue(struct tpacket_kbdq_core *pkc) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 837 | { |
| 838 | pkc->reset_pending_on_curr_blk = 0; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Side effect of opening a block: |
| 843 | * |
| 844 | * 1) prb_queue is thawed. |
| 845 | * 2) retire_blk_timer is refreshed. |
| 846 | * |
| 847 | */ |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 848 | static void prb_open_block(struct tpacket_kbdq_core *pkc1, |
| 849 | struct tpacket_block_desc *pbd1) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 850 | { |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 851 | struct timespec64 ts; |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 852 | struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 853 | |
| 854 | smp_rmb(); |
| 855 | |
Daniel Borkmann | 8da3056 | 2013-05-03 02:57:00 +0000 | [diff] [blame] | 856 | /* We could have just memset this but we will lose the |
| 857 | * flexibility of making the priv area sticky |
| 858 | */ |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 859 | |
Daniel Borkmann | 8da3056 | 2013-05-03 02:57:00 +0000 | [diff] [blame] | 860 | BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++; |
| 861 | BLOCK_NUM_PKTS(pbd1) = 0; |
| 862 | BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 863 | |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 864 | ktime_get_real_ts64(&ts); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 865 | |
Daniel Borkmann | 8da3056 | 2013-05-03 02:57:00 +0000 | [diff] [blame] | 866 | h1->ts_first_pkt.ts_sec = ts.tv_sec; |
| 867 | h1->ts_first_pkt.ts_nsec = ts.tv_nsec; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 868 | |
Daniel Borkmann | 8da3056 | 2013-05-03 02:57:00 +0000 | [diff] [blame] | 869 | pkc1->pkblk_start = (char *)pbd1; |
| 870 | pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv); |
| 871 | |
| 872 | BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv); |
| 873 | BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN; |
| 874 | |
| 875 | pbd1->version = pkc1->version; |
| 876 | pkc1->prev = pkc1->nxt_offset; |
| 877 | pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size; |
| 878 | |
| 879 | prb_thaw_queue(pkc1); |
| 880 | _prb_refresh_rx_retire_blk_timer(pkc1); |
| 881 | |
| 882 | smp_wmb(); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* |
| 886 | * Queue freeze logic: |
| 887 | * 1) Assume tp_block_nr = 8 blocks. |
| 888 | * 2) At time 't0', user opens Rx ring. |
| 889 | * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7 |
| 890 | * 4) user-space is either sleeping or processing block '0'. |
| 891 | * 5) tpacket_rcv is currently filling block '7', since there is no space left, |
| 892 | * it will close block-7,loop around and try to fill block '0'. |
| 893 | * call-flow: |
| 894 | * __packet_lookup_frame_in_block |
| 895 | * prb_retire_current_block() |
| 896 | * prb_dispatch_next_block() |
| 897 | * |->(BLOCK_STATUS == USER) evaluates to true |
| 898 | * 5.1) Since block-0 is currently in-use, we just freeze the queue. |
| 899 | * 6) Now there are two cases: |
| 900 | * 6.1) Link goes idle right after the queue is frozen. |
| 901 | * But remember, the last open_block() refreshed the timer. |
| 902 | * When this timer expires,it will refresh itself so that we can |
| 903 | * re-open block-0 in near future. |
| 904 | * 6.2) Link is busy and keeps on receiving packets. This is a simple |
| 905 | * case and __packet_lookup_frame_in_block will check if block-0 |
| 906 | * is free and can now be re-used. |
| 907 | */ |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 908 | static void prb_freeze_queue(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 909 | struct packet_sock *po) |
| 910 | { |
| 911 | pkc->reset_pending_on_curr_blk = 1; |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 912 | po->stats.stats3.tp_freeze_q_cnt++; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | #define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT)) |
| 916 | |
| 917 | /* |
| 918 | * If the next block is free then we will dispatch it |
| 919 | * and return a good offset. |
| 920 | * Else, we will freeze the queue. |
| 921 | * So, caller must check the return value. |
| 922 | */ |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 923 | static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 924 | struct packet_sock *po) |
| 925 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 926 | struct tpacket_block_desc *pbd; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 927 | |
| 928 | smp_rmb(); |
| 929 | |
| 930 | /* 1. Get current block num */ |
| 931 | pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc); |
| 932 | |
| 933 | /* 2. If this block is currently in_use then freeze the queue */ |
| 934 | if (TP_STATUS_USER & BLOCK_STATUS(pbd)) { |
| 935 | prb_freeze_queue(pkc, po); |
| 936 | return NULL; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * 3. |
| 941 | * open this block and return the offset where the first packet |
| 942 | * needs to get stored. |
| 943 | */ |
| 944 | prb_open_block(pkc, pbd); |
| 945 | return (void *)pkc->nxt_offset; |
| 946 | } |
| 947 | |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 948 | static void prb_retire_current_block(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 949 | struct packet_sock *po, unsigned int status) |
| 950 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 951 | struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 952 | |
| 953 | /* retire/close the current block */ |
| 954 | if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) { |
| 955 | /* |
| 956 | * Plug the case where copy_bits() is in progress on |
| 957 | * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't |
| 958 | * have space to copy the pkt in the current block and |
| 959 | * called prb_retire_current_block() |
| 960 | * |
| 961 | * We don't need to worry about the TMO case because |
| 962 | * the timer-handler already handled this case. |
| 963 | */ |
| 964 | if (!(status & TP_STATUS_BLK_TMO)) { |
John Ogness | 632ca50 | 2020-07-07 17:28:04 +0206 | [diff] [blame] | 965 | /* Waiting for skb_copy_bits to finish... */ |
| 966 | write_lock(&pkc->blk_fill_in_prog_lock); |
| 967 | write_unlock(&pkc->blk_fill_in_prog_lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 968 | } |
| 969 | prb_close_block(pkc, pbd, po, status); |
| 970 | return; |
| 971 | } |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Rosen, Rami | 878cd3b | 2017-05-24 18:34:11 +0300 | [diff] [blame] | 974 | static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 975 | { |
| 976 | return TP_STATUS_USER & BLOCK_STATUS(pbd); |
| 977 | } |
| 978 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 979 | static int prb_queue_frozen(struct tpacket_kbdq_core *pkc) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 980 | { |
| 981 | return pkc->reset_pending_on_curr_blk; |
| 982 | } |
| 983 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 984 | static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb) |
John Ogness | 88fd1cb | 2020-08-13 21:45:25 +0206 | [diff] [blame] | 985 | __releases(&pkc->blk_fill_in_prog_lock) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 986 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 987 | struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb); |
John Ogness | 632ca50 | 2020-07-07 17:28:04 +0206 | [diff] [blame] | 988 | |
| 989 | read_unlock(&pkc->blk_fill_in_prog_lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 992 | static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 993 | struct tpacket3_hdr *ppd) |
| 994 | { |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 995 | ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 998 | static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 999 | struct tpacket3_hdr *ppd) |
| 1000 | { |
| 1001 | ppd->hv1.tp_rxhash = 0; |
| 1002 | } |
| 1003 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1004 | static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1005 | struct tpacket3_hdr *ppd) |
| 1006 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 1007 | if (skb_vlan_tag_present(pkc->skb)) { |
| 1008 | ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb); |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 1009 | ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto); |
| 1010 | ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1011 | } else { |
danborkmann@iogearbox.net | 9e67030 | 2012-08-20 03:34:03 +0000 | [diff] [blame] | 1012 | ppd->hv1.tp_vlan_tci = 0; |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 1013 | ppd->hv1.tp_vlan_tpid = 0; |
danborkmann@iogearbox.net | 9e67030 | 2012-08-20 03:34:03 +0000 | [diff] [blame] | 1014 | ppd->tp_status = TP_STATUS_AVAILABLE; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 1018 | static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1019 | struct tpacket3_hdr *ppd) |
| 1020 | { |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 1021 | ppd->hv1.tp_padding = 0; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1022 | prb_fill_vlan_info(pkc, ppd); |
| 1023 | |
| 1024 | if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH) |
| 1025 | prb_fill_rxhash(pkc, ppd); |
| 1026 | else |
| 1027 | prb_clear_rxhash(pkc, ppd); |
| 1028 | } |
| 1029 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1030 | static void prb_fill_curr_block(char *curr, |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 1031 | struct tpacket_kbdq_core *pkc, |
| 1032 | struct tpacket_block_desc *pbd, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1033 | unsigned int len) |
John Ogness | 88fd1cb | 2020-08-13 21:45:25 +0206 | [diff] [blame] | 1034 | __acquires(&pkc->blk_fill_in_prog_lock) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1035 | { |
| 1036 | struct tpacket3_hdr *ppd; |
| 1037 | |
| 1038 | ppd = (struct tpacket3_hdr *)curr; |
| 1039 | ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len); |
| 1040 | pkc->prev = curr; |
| 1041 | pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len); |
| 1042 | BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len); |
| 1043 | BLOCK_NUM_PKTS(pbd) += 1; |
John Ogness | 632ca50 | 2020-07-07 17:28:04 +0206 | [diff] [blame] | 1044 | read_lock(&pkc->blk_fill_in_prog_lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1045 | prb_run_all_ft_ops(pkc, ppd); |
| 1046 | } |
| 1047 | |
| 1048 | /* Assumes caller has the sk->rx_queue.lock */ |
| 1049 | static void *__packet_lookup_frame_in_block(struct packet_sock *po, |
| 1050 | struct sk_buff *skb, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1051 | unsigned int len |
| 1052 | ) |
| 1053 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 1054 | struct tpacket_kbdq_core *pkc; |
| 1055 | struct tpacket_block_desc *pbd; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1056 | char *curr, *end; |
| 1057 | |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 1058 | pkc = GET_PBDQC_FROM_RB(&po->rx_ring); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1059 | pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc); |
| 1060 | |
| 1061 | /* Queue is frozen when user space is lagging behind */ |
| 1062 | if (prb_queue_frozen(pkc)) { |
| 1063 | /* |
| 1064 | * Check if that last block which caused the queue to freeze, |
| 1065 | * is still in_use by user-space. |
| 1066 | */ |
Rosen, Rami | 878cd3b | 2017-05-24 18:34:11 +0300 | [diff] [blame] | 1067 | if (prb_curr_blk_in_use(pbd)) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1068 | /* Can't record this packet */ |
| 1069 | return NULL; |
| 1070 | } else { |
| 1071 | /* |
| 1072 | * Ok, the block was released by user-space. |
| 1073 | * Now let's open that block. |
| 1074 | * opening a block also thaws the queue. |
| 1075 | * Thawing is a side effect. |
| 1076 | */ |
| 1077 | prb_open_block(pkc, pbd); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | smp_mb(); |
| 1082 | curr = pkc->nxt_offset; |
| 1083 | pkc->skb = skb; |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 1084 | end = (char *)pbd + pkc->kblk_size; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* first try the current block */ |
| 1087 | if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) { |
| 1088 | prb_fill_curr_block(curr, pkc, pbd, len); |
| 1089 | return (void *)curr; |
| 1090 | } |
| 1091 | |
| 1092 | /* Ok, close the current block */ |
| 1093 | prb_retire_current_block(pkc, po, 0); |
| 1094 | |
| 1095 | /* Now, try to dispatch the next block */ |
| 1096 | curr = (char *)prb_dispatch_next_block(pkc, po); |
| 1097 | if (curr) { |
| 1098 | pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc); |
| 1099 | prb_fill_curr_block(curr, pkc, pbd, len); |
| 1100 | return (void *)curr; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * No free blocks are available.user_space hasn't caught up yet. |
| 1105 | * Queue was just frozen and now this packet will get dropped. |
| 1106 | */ |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1110 | static void *packet_current_rx_frame(struct packet_sock *po, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1111 | struct sk_buff *skb, |
| 1112 | int status, unsigned int len) |
| 1113 | { |
| 1114 | char *curr = NULL; |
| 1115 | switch (po->tp_version) { |
| 1116 | case TPACKET_V1: |
| 1117 | case TPACKET_V2: |
| 1118 | curr = packet_lookup_frame(po, &po->rx_ring, |
| 1119 | po->rx_ring.head, status); |
| 1120 | return curr; |
| 1121 | case TPACKET_V3: |
Mao Wenan | 4608805 | 2019-06-11 09:32:13 +0800 | [diff] [blame] | 1122 | return __packet_lookup_frame_in_block(po, skb, len); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1123 | default: |
| 1124 | WARN(1, "TPACKET version not supported\n"); |
| 1125 | BUG(); |
Ying Xue | 99aa347 | 2012-08-06 16:27:10 +0000 | [diff] [blame] | 1126 | return NULL; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
Eric Dumazet | dcf70ce | 2019-06-12 09:52:28 -0700 | [diff] [blame] | 1130 | static void *prb_lookup_block(const struct packet_sock *po, |
| 1131 | const struct packet_ring_buffer *rb, |
| 1132 | unsigned int idx, |
| 1133 | int status) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1134 | { |
chetan loke | bc59ba3 | 2011-08-25 10:43:30 +0000 | [diff] [blame] | 1135 | struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb); |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1136 | struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (status != BLOCK_STATUS(pbd)) |
| 1139 | return NULL; |
| 1140 | return pbd; |
| 1141 | } |
| 1142 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1143 | static int prb_previous_blk_num(struct packet_ring_buffer *rb) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1144 | { |
| 1145 | unsigned int prev; |
| 1146 | if (rb->prb_bdqc.kactive_blk_num) |
| 1147 | prev = rb->prb_bdqc.kactive_blk_num-1; |
| 1148 | else |
| 1149 | prev = rb->prb_bdqc.knum_blocks-1; |
| 1150 | return prev; |
| 1151 | } |
| 1152 | |
| 1153 | /* Assumes caller has held the rx_queue.lock */ |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1154 | static void *__prb_previous_block(struct packet_sock *po, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1155 | struct packet_ring_buffer *rb, |
| 1156 | int status) |
| 1157 | { |
| 1158 | unsigned int previous = prb_previous_blk_num(rb); |
| 1159 | return prb_lookup_block(po, rb, previous, status); |
| 1160 | } |
| 1161 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1162 | static void *packet_previous_rx_frame(struct packet_sock *po, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1163 | struct packet_ring_buffer *rb, |
| 1164 | int status) |
| 1165 | { |
| 1166 | if (po->tp_version <= TPACKET_V2) |
| 1167 | return packet_previous_frame(po, rb, status); |
| 1168 | |
| 1169 | return __prb_previous_block(po, rb, status); |
| 1170 | } |
| 1171 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1172 | static void packet_increment_rx_head(struct packet_sock *po, |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 1173 | struct packet_ring_buffer *rb) |
| 1174 | { |
| 1175 | switch (po->tp_version) { |
| 1176 | case TPACKET_V1: |
| 1177 | case TPACKET_V2: |
| 1178 | return packet_increment_head(rb); |
| 1179 | case TPACKET_V3: |
| 1180 | default: |
| 1181 | WARN(1, "TPACKET version not supported.\n"); |
| 1182 | BUG(); |
| 1183 | return; |
| 1184 | } |
| 1185 | } |
| 1186 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1187 | static void *packet_previous_frame(struct packet_sock *po, |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1188 | struct packet_ring_buffer *rb, |
| 1189 | int status) |
| 1190 | { |
| 1191 | unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max; |
| 1192 | return packet_lookup_frame(po, rb, previous, status); |
| 1193 | } |
| 1194 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 1195 | static void packet_increment_head(struct packet_ring_buffer *buff) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1196 | { |
| 1197 | buff->head = buff->head != buff->frame_max ? buff->head+1 : 0; |
| 1198 | } |
| 1199 | |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 1200 | static void packet_inc_pending(struct packet_ring_buffer *rb) |
| 1201 | { |
| 1202 | this_cpu_inc(*rb->pending_refcnt); |
| 1203 | } |
| 1204 | |
| 1205 | static void packet_dec_pending(struct packet_ring_buffer *rb) |
| 1206 | { |
| 1207 | this_cpu_dec(*rb->pending_refcnt); |
| 1208 | } |
| 1209 | |
| 1210 | static unsigned int packet_read_pending(const struct packet_ring_buffer *rb) |
| 1211 | { |
| 1212 | unsigned int refcnt = 0; |
| 1213 | int cpu; |
| 1214 | |
| 1215 | /* We don't use pending refcount in rx_ring. */ |
| 1216 | if (rb->pending_refcnt == NULL) |
| 1217 | return 0; |
| 1218 | |
| 1219 | for_each_possible_cpu(cpu) |
| 1220 | refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu); |
| 1221 | |
| 1222 | return refcnt; |
| 1223 | } |
| 1224 | |
| 1225 | static int packet_alloc_pending(struct packet_sock *po) |
| 1226 | { |
| 1227 | po->rx_ring.pending_refcnt = NULL; |
| 1228 | |
| 1229 | po->tx_ring.pending_refcnt = alloc_percpu(unsigned int); |
| 1230 | if (unlikely(po->tx_ring.pending_refcnt == NULL)) |
| 1231 | return -ENOBUFS; |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | static void packet_free_pending(struct packet_sock *po) |
| 1237 | { |
| 1238 | free_percpu(po->tx_ring.pending_refcnt); |
| 1239 | } |
| 1240 | |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1241 | #define ROOM_POW_OFF 2 |
| 1242 | #define ROOM_NONE 0x0 |
| 1243 | #define ROOM_LOW 0x1 |
| 1244 | #define ROOM_NORMAL 0x2 |
| 1245 | |
Eric Dumazet | d4b5bd9 | 2019-06-12 09:52:27 -0700 | [diff] [blame] | 1246 | static bool __tpacket_has_room(const struct packet_sock *po, int pow_off) |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1247 | { |
| 1248 | int idx, len; |
| 1249 | |
Eric Dumazet | d4b5bd9 | 2019-06-12 09:52:27 -0700 | [diff] [blame] | 1250 | len = READ_ONCE(po->rx_ring.frame_max) + 1; |
| 1251 | idx = READ_ONCE(po->rx_ring.head); |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1252 | if (pow_off) |
| 1253 | idx += len >> pow_off; |
| 1254 | if (idx >= len) |
| 1255 | idx -= len; |
| 1256 | return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL); |
| 1257 | } |
| 1258 | |
Eric Dumazet | dcf70ce | 2019-06-12 09:52:28 -0700 | [diff] [blame] | 1259 | static bool __tpacket_v3_has_room(const struct packet_sock *po, int pow_off) |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1260 | { |
| 1261 | int idx, len; |
| 1262 | |
Eric Dumazet | dcf70ce | 2019-06-12 09:52:28 -0700 | [diff] [blame] | 1263 | len = READ_ONCE(po->rx_ring.prb_bdqc.knum_blocks); |
| 1264 | idx = READ_ONCE(po->rx_ring.prb_bdqc.kactive_blk_num); |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1265 | if (pow_off) |
| 1266 | idx += len >> pow_off; |
| 1267 | if (idx >= len) |
| 1268 | idx -= len; |
| 1269 | return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL); |
| 1270 | } |
| 1271 | |
Eric Dumazet | 0338a14 | 2019-06-12 09:52:29 -0700 | [diff] [blame] | 1272 | static int __packet_rcv_has_room(const struct packet_sock *po, |
| 1273 | const struct sk_buff *skb) |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1274 | { |
Eric Dumazet | 0338a14 | 2019-06-12 09:52:29 -0700 | [diff] [blame] | 1275 | const struct sock *sk = &po->sk; |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1276 | int ret = ROOM_NONE; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1277 | |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1278 | if (po->prot_hook.func != tpacket_rcv) { |
Eric Dumazet | 0338a14 | 2019-06-12 09:52:29 -0700 | [diff] [blame] | 1279 | int rcvbuf = READ_ONCE(sk->sk_rcvbuf); |
| 1280 | int avail = rcvbuf - atomic_read(&sk->sk_rmem_alloc) |
| 1281 | - (skb ? skb->truesize : 0); |
| 1282 | |
| 1283 | if (avail > (rcvbuf >> ROOM_POW_OFF)) |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1284 | return ROOM_NORMAL; |
| 1285 | else if (avail > 0) |
| 1286 | return ROOM_LOW; |
| 1287 | else |
| 1288 | return ROOM_NONE; |
| 1289 | } |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1290 | |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1291 | if (po->tp_version == TPACKET_V3) { |
| 1292 | if (__tpacket_v3_has_room(po, ROOM_POW_OFF)) |
| 1293 | ret = ROOM_NORMAL; |
| 1294 | else if (__tpacket_v3_has_room(po, 0)) |
| 1295 | ret = ROOM_LOW; |
| 1296 | } else { |
| 1297 | if (__tpacket_has_room(po, ROOM_POW_OFF)) |
| 1298 | ret = ROOM_NORMAL; |
| 1299 | else if (__tpacket_has_room(po, 0)) |
| 1300 | ret = ROOM_LOW; |
| 1301 | } |
Willem de Bruijn | 2ccdbaa | 2015-05-12 11:56:48 -0400 | [diff] [blame] | 1302 | |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb) |
| 1307 | { |
Eric Dumazet | 3a2bb84 | 2019-06-12 09:52:32 -0700 | [diff] [blame] | 1308 | int pressure, ret; |
Willem de Bruijn | 2ccdbaa | 2015-05-12 11:56:48 -0400 | [diff] [blame] | 1309 | |
Willem de Bruijn | 54d7c01 | 2015-05-14 15:25:02 -0400 | [diff] [blame] | 1310 | ret = __packet_rcv_has_room(po, skb); |
Eric Dumazet | 3a2bb84 | 2019-06-12 09:52:32 -0700 | [diff] [blame] | 1311 | pressure = ret != ROOM_NORMAL; |
| 1312 | |
| 1313 | if (READ_ONCE(po->pressure) != pressure) |
| 1314 | WRITE_ONCE(po->pressure, pressure); |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1315 | |
Willem de Bruijn | 9954729 | 2015-05-12 11:56:47 -0400 | [diff] [blame] | 1316 | return ret; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Eric Dumazet | 9bb6cd6 | 2019-06-12 09:52:33 -0700 | [diff] [blame] | 1319 | static void packet_rcv_try_clear_pressure(struct packet_sock *po) |
| 1320 | { |
| 1321 | if (READ_ONCE(po->pressure) && |
| 1322 | __packet_rcv_has_room(po, NULL) == ROOM_NORMAL) |
| 1323 | WRITE_ONCE(po->pressure, 0); |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | static void packet_sock_destruct(struct sock *sk) |
| 1327 | { |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 1328 | skb_queue_purge(&sk->sk_error_queue); |
| 1329 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 1330 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 1331 | WARN_ON(refcount_read(&sk->sk_wmem_alloc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| 1333 | if (!sock_flag(sk, SOCK_DEAD)) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1334 | pr_err("Attempt to release alive packet socket: %p\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | return; |
| 1336 | } |
| 1337 | |
Pavel Emelyanov | 17ab56a | 2007-11-10 21:38:48 -0800 | [diff] [blame] | 1338 | sk_refcnt_debug_dec(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Willem de Bruijn | 3b3a5b0 | 2015-05-12 11:56:49 -0400 | [diff] [blame] | 1341 | static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb) |
| 1342 | { |
Eric Dumazet | b756ad9 | 2019-11-08 05:07:46 -0800 | [diff] [blame] | 1343 | u32 *history = po->rollover->history; |
| 1344 | u32 victim, rxhash; |
Willem de Bruijn | 3b3a5b0 | 2015-05-12 11:56:49 -0400 | [diff] [blame] | 1345 | int i, count = 0; |
| 1346 | |
| 1347 | rxhash = skb_get_hash(skb); |
| 1348 | for (i = 0; i < ROLLOVER_HLEN; i++) |
Eric Dumazet | b756ad9 | 2019-11-08 05:07:46 -0800 | [diff] [blame] | 1349 | if (READ_ONCE(history[i]) == rxhash) |
Willem de Bruijn | 3b3a5b0 | 2015-05-12 11:56:49 -0400 | [diff] [blame] | 1350 | count++; |
| 1351 | |
Eric Dumazet | b756ad9 | 2019-11-08 05:07:46 -0800 | [diff] [blame] | 1352 | victim = prandom_u32() % ROLLOVER_HLEN; |
| 1353 | |
| 1354 | /* Avoid dirtying the cache line if possible */ |
| 1355 | if (READ_ONCE(history[victim]) != rxhash) |
| 1356 | WRITE_ONCE(history[victim], rxhash); |
| 1357 | |
Willem de Bruijn | 3b3a5b0 | 2015-05-12 11:56:49 -0400 | [diff] [blame] | 1358 | return count > (ROLLOVER_HLEN >> 1); |
| 1359 | } |
| 1360 | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1361 | static unsigned int fanout_demux_hash(struct packet_fanout *f, |
| 1362 | struct sk_buff *skb, |
| 1363 | unsigned int num) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1364 | { |
David S. Miller | eb70db8 | 2016-07-01 16:07:50 -0400 | [diff] [blame] | 1365 | return reciprocal_scale(__skb_get_hash_symmetric(skb), num); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1368 | static unsigned int fanout_demux_lb(struct packet_fanout *f, |
| 1369 | struct sk_buff *skb, |
| 1370 | unsigned int num) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1371 | { |
Willem de Bruijn | 468479e | 2015-06-17 15:59:34 -0400 | [diff] [blame] | 1372 | unsigned int val = atomic_inc_return(&f->rr_cur); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1373 | |
Willem de Bruijn | 468479e | 2015-06-17 15:59:34 -0400 | [diff] [blame] | 1374 | return val % num; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1377 | static unsigned int fanout_demux_cpu(struct packet_fanout *f, |
| 1378 | struct sk_buff *skb, |
| 1379 | unsigned int num) |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1380 | { |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1381 | return smp_processor_id() % num; |
| 1382 | } |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1383 | |
Daniel Borkmann | 5df0ddf | 2013-08-28 22:13:09 +0200 | [diff] [blame] | 1384 | static unsigned int fanout_demux_rnd(struct packet_fanout *f, |
| 1385 | struct sk_buff *skb, |
| 1386 | unsigned int num) |
| 1387 | { |
Daniel Borkmann | f337db6 | 2014-01-22 02:29:39 +0100 | [diff] [blame] | 1388 | return prandom_u32_max(num); |
Daniel Borkmann | 5df0ddf | 2013-08-28 22:13:09 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1391 | static unsigned int fanout_demux_rollover(struct packet_fanout *f, |
| 1392 | struct sk_buff *skb, |
Willem de Bruijn | ad377ca | 2015-05-12 11:56:45 -0400 | [diff] [blame] | 1393 | unsigned int idx, bool try_self, |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1394 | unsigned int num) |
| 1395 | { |
Willem de Bruijn | 4633c9e | 2015-05-17 19:44:02 -0400 | [diff] [blame] | 1396 | struct packet_sock *po, *po_next, *po_skip = NULL; |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 1397 | unsigned int i, j, room = ROOM_NONE; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1398 | |
Eric Dumazet | 94f633e | 2021-04-14 12:36:44 -0700 | [diff] [blame] | 1399 | po = pkt_sk(rcu_dereference(f->arr[idx])); |
Willem de Bruijn | 3b3a5b0 | 2015-05-12 11:56:49 -0400 | [diff] [blame] | 1400 | |
| 1401 | if (try_self) { |
| 1402 | room = packet_rcv_has_room(po, skb); |
| 1403 | if (room == ROOM_NORMAL || |
| 1404 | (room == ROOM_LOW && !fanout_flow_is_huge(po, skb))) |
| 1405 | return idx; |
Willem de Bruijn | 4633c9e | 2015-05-17 19:44:02 -0400 | [diff] [blame] | 1406 | po_skip = po; |
Willem de Bruijn | 3b3a5b0 | 2015-05-12 11:56:49 -0400 | [diff] [blame] | 1407 | } |
Willem de Bruijn | ad377ca | 2015-05-12 11:56:45 -0400 | [diff] [blame] | 1408 | |
Willem de Bruijn | 0648ab7 | 2015-05-12 11:56:46 -0400 | [diff] [blame] | 1409 | i = j = min_t(int, po->rollover->sock, num - 1); |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1410 | do { |
Eric Dumazet | 94f633e | 2021-04-14 12:36:44 -0700 | [diff] [blame] | 1411 | po_next = pkt_sk(rcu_dereference(f->arr[i])); |
Eric Dumazet | 3a2bb84 | 2019-06-12 09:52:32 -0700 | [diff] [blame] | 1412 | if (po_next != po_skip && !READ_ONCE(po_next->pressure) && |
Willem de Bruijn | 2ccdbaa | 2015-05-12 11:56:48 -0400 | [diff] [blame] | 1413 | packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) { |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1414 | if (i != j) |
Willem de Bruijn | 0648ab7 | 2015-05-12 11:56:46 -0400 | [diff] [blame] | 1415 | po->rollover->sock = i; |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 1416 | atomic_long_inc(&po->rollover->num); |
| 1417 | if (room == ROOM_LOW) |
| 1418 | atomic_long_inc(&po->rollover->num_huge); |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1419 | return i; |
| 1420 | } |
Willem de Bruijn | ad377ca | 2015-05-12 11:56:45 -0400 | [diff] [blame] | 1421 | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1422 | if (++i == num) |
| 1423 | i = 0; |
| 1424 | } while (i != j); |
| 1425 | |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 1426 | atomic_long_inc(&po->rollover->num_failed); |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1427 | return idx; |
| 1428 | } |
| 1429 | |
Neil Horman | 2d36097 | 2014-01-22 16:01:44 -0500 | [diff] [blame] | 1430 | static unsigned int fanout_demux_qm(struct packet_fanout *f, |
| 1431 | struct sk_buff *skb, |
| 1432 | unsigned int num) |
| 1433 | { |
| 1434 | return skb_get_queue_mapping(skb) % num; |
| 1435 | } |
| 1436 | |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1437 | static unsigned int fanout_demux_bpf(struct packet_fanout *f, |
| 1438 | struct sk_buff *skb, |
| 1439 | unsigned int num) |
| 1440 | { |
| 1441 | struct bpf_prog *prog; |
| 1442 | unsigned int ret = 0; |
| 1443 | |
| 1444 | rcu_read_lock(); |
| 1445 | prog = rcu_dereference(f->bpf_prog); |
| 1446 | if (prog) |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 1447 | ret = bpf_prog_run_clear_cb(prog, skb) % num; |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1448 | rcu_read_unlock(); |
| 1449 | |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1453 | static bool fanout_has_flag(struct packet_fanout *f, u16 flag) |
| 1454 | { |
| 1455 | return f->flags & (flag >> 8); |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1458 | static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev, |
| 1459 | struct packet_type *pt, struct net_device *orig_dev) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1460 | { |
| 1461 | struct packet_fanout *f = pt->af_packet_priv; |
Eric Dumazet | f98f451 | 2015-06-16 07:59:11 -0700 | [diff] [blame] | 1462 | unsigned int num = READ_ONCE(f->num_members); |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 1463 | struct net *net = read_pnet(&f->net); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1464 | struct packet_sock *po; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1465 | unsigned int idx; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1466 | |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 1467 | if (!net_eq(dev_net(dev), net) || !num) { |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1468 | kfree_skb(skb); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Alexander Drozdov | 3f34b24 | 2015-02-20 08:24:27 +0300 | [diff] [blame] | 1472 | if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) { |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 1473 | skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET); |
Alexander Drozdov | 3f34b24 | 2015-02-20 08:24:27 +0300 | [diff] [blame] | 1474 | if (!skb) |
| 1475 | return 0; |
| 1476 | } |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1477 | switch (f->type) { |
| 1478 | case PACKET_FANOUT_HASH: |
| 1479 | default: |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1480 | idx = fanout_demux_hash(f, skb, num); |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1481 | break; |
| 1482 | case PACKET_FANOUT_LB: |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1483 | idx = fanout_demux_lb(f, skb, num); |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1484 | break; |
| 1485 | case PACKET_FANOUT_CPU: |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1486 | idx = fanout_demux_cpu(f, skb, num); |
| 1487 | break; |
Daniel Borkmann | 5df0ddf | 2013-08-28 22:13:09 +0200 | [diff] [blame] | 1488 | case PACKET_FANOUT_RND: |
| 1489 | idx = fanout_demux_rnd(f, skb, num); |
| 1490 | break; |
Neil Horman | 2d36097 | 2014-01-22 16:01:44 -0500 | [diff] [blame] | 1491 | case PACKET_FANOUT_QM: |
| 1492 | idx = fanout_demux_qm(f, skb, num); |
| 1493 | break; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1494 | case PACKET_FANOUT_ROLLOVER: |
Willem de Bruijn | ad377ca | 2015-05-12 11:56:45 -0400 | [diff] [blame] | 1495 | idx = fanout_demux_rollover(f, skb, 0, false, num); |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1496 | break; |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1497 | case PACKET_FANOUT_CBPF: |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1498 | case PACKET_FANOUT_EBPF: |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1499 | idx = fanout_demux_bpf(f, skb, num); |
| 1500 | break; |
David S. Miller | 7736d33 | 2011-07-05 01:43:20 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Willem de Bruijn | ad377ca | 2015-05-12 11:56:45 -0400 | [diff] [blame] | 1503 | if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER)) |
| 1504 | idx = fanout_demux_rollover(f, skb, idx, true, num); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1505 | |
Eric Dumazet | 94f633e | 2021-04-14 12:36:44 -0700 | [diff] [blame] | 1506 | po = pkt_sk(rcu_dereference(f->arr[idx])); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1507 | return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev); |
| 1508 | } |
| 1509 | |
Pavel Emelyanov | fff3321 | 2012-08-16 05:36:48 +0000 | [diff] [blame] | 1510 | DEFINE_MUTEX(fanout_mutex); |
| 1511 | EXPORT_SYMBOL_GPL(fanout_mutex); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1512 | static LIST_HEAD(fanout_list); |
Mike Maloney | 4a69a86 | 2017-04-21 10:56:11 -0400 | [diff] [blame] | 1513 | static u16 fanout_next_id; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1514 | |
| 1515 | static void __fanout_link(struct sock *sk, struct packet_sock *po) |
| 1516 | { |
| 1517 | struct packet_fanout *f = po->fanout; |
| 1518 | |
| 1519 | spin_lock(&f->lock); |
Eric Dumazet | 94f633e | 2021-04-14 12:36:44 -0700 | [diff] [blame] | 1520 | rcu_assign_pointer(f->arr[f->num_members], sk); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1521 | smp_wmb(); |
| 1522 | f->num_members++; |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 1523 | if (f->num_members == 1) |
| 1524 | dev_add_pack(&f->prot_hook); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1525 | spin_unlock(&f->lock); |
| 1526 | } |
| 1527 | |
| 1528 | static void __fanout_unlink(struct sock *sk, struct packet_sock *po) |
| 1529 | { |
| 1530 | struct packet_fanout *f = po->fanout; |
| 1531 | int i; |
| 1532 | |
| 1533 | spin_lock(&f->lock); |
| 1534 | for (i = 0; i < f->num_members; i++) { |
Eric Dumazet | 94f633e | 2021-04-14 12:36:44 -0700 | [diff] [blame] | 1535 | if (rcu_dereference_protected(f->arr[i], |
| 1536 | lockdep_is_held(&f->lock)) == sk) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1537 | break; |
| 1538 | } |
| 1539 | BUG_ON(i >= f->num_members); |
Eric Dumazet | 94f633e | 2021-04-14 12:36:44 -0700 | [diff] [blame] | 1540 | rcu_assign_pointer(f->arr[i], |
| 1541 | rcu_dereference_protected(f->arr[f->num_members - 1], |
| 1542 | lockdep_is_held(&f->lock))); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1543 | f->num_members--; |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 1544 | if (f->num_members == 0) |
| 1545 | __dev_remove_pack(&f->prot_hook); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1546 | spin_unlock(&f->lock); |
| 1547 | } |
| 1548 | |
Weilong Chen | d4dd8ae | 2013-12-23 11:31:38 +0800 | [diff] [blame] | 1549 | static bool match_fanout_group(struct packet_type *ptype, struct sock *sk) |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1550 | { |
Eric Dumazet | 161642e | 2015-10-09 11:29:32 -0700 | [diff] [blame] | 1551 | if (sk->sk_family != PF_PACKET) |
| 1552 | return false; |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1553 | |
Eric Dumazet | 161642e | 2015-10-09 11:29:32 -0700 | [diff] [blame] | 1554 | return ptype->af_packet_priv == pkt_sk(sk)->fanout; |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1557 | static void fanout_init_data(struct packet_fanout *f) |
| 1558 | { |
| 1559 | switch (f->type) { |
| 1560 | case PACKET_FANOUT_LB: |
| 1561 | atomic_set(&f->rr_cur, 0); |
| 1562 | break; |
| 1563 | case PACKET_FANOUT_CBPF: |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1564 | case PACKET_FANOUT_EBPF: |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1565 | RCU_INIT_POINTER(f->bpf_prog, NULL); |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new) |
| 1571 | { |
| 1572 | struct bpf_prog *old; |
| 1573 | |
| 1574 | spin_lock(&f->lock); |
| 1575 | old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock)); |
| 1576 | rcu_assign_pointer(f->bpf_prog, new); |
| 1577 | spin_unlock(&f->lock); |
| 1578 | |
| 1579 | if (old) { |
| 1580 | synchronize_net(); |
| 1581 | bpf_prog_destroy(old); |
| 1582 | } |
| 1583 | } |
| 1584 | |
Christoph Hellwig | b1ea9ff | 2020-07-23 08:08:47 +0200 | [diff] [blame] | 1585 | static int fanout_set_data_cbpf(struct packet_sock *po, sockptr_t data, |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1586 | unsigned int len) |
| 1587 | { |
| 1588 | struct bpf_prog *new; |
| 1589 | struct sock_fprog fprog; |
| 1590 | int ret; |
| 1591 | |
| 1592 | if (sock_flag(&po->sk, SOCK_FILTER_LOCKED)) |
| 1593 | return -EPERM; |
Christoph Hellwig | 4d295e5 | 2020-07-17 08:23:13 +0200 | [diff] [blame] | 1594 | |
| 1595 | ret = copy_bpf_fprog_from_user(&fprog, data, len); |
| 1596 | if (ret) |
| 1597 | return ret; |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1598 | |
Daniel Borkmann | bab1899 | 2015-10-02 15:17:33 +0200 | [diff] [blame] | 1599 | ret = bpf_prog_create_from_user(&new, &fprog, NULL, false); |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1600 | if (ret) |
| 1601 | return ret; |
| 1602 | |
| 1603 | __fanout_set_data_bpf(po->fanout, new); |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1607 | static int fanout_set_data_ebpf(struct packet_sock *po, sockptr_t data, |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1608 | unsigned int len) |
| 1609 | { |
| 1610 | struct bpf_prog *new; |
| 1611 | u32 fd; |
| 1612 | |
| 1613 | if (sock_flag(&po->sk, SOCK_FILTER_LOCKED)) |
| 1614 | return -EPERM; |
| 1615 | if (len != sizeof(fd)) |
| 1616 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1617 | if (copy_from_sockptr(&fd, data, len)) |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1618 | return -EFAULT; |
| 1619 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1620 | new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1621 | if (IS_ERR(new)) |
| 1622 | return PTR_ERR(new); |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1623 | |
| 1624 | __fanout_set_data_bpf(po->fanout, new); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1628 | static int fanout_set_data(struct packet_sock *po, sockptr_t data, |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1629 | unsigned int len) |
| 1630 | { |
| 1631 | switch (po->fanout->type) { |
| 1632 | case PACKET_FANOUT_CBPF: |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1633 | return fanout_set_data_cbpf(po, data, len); |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1634 | case PACKET_FANOUT_EBPF: |
| 1635 | return fanout_set_data_ebpf(po, data, len); |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1636 | default: |
| 1637 | return -EINVAL; |
zhong jiang | 07d53ae | 2018-08-04 19:41:41 +0800 | [diff] [blame] | 1638 | } |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static void fanout_release_data(struct packet_fanout *f) |
| 1642 | { |
| 1643 | switch (f->type) { |
| 1644 | case PACKET_FANOUT_CBPF: |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1645 | case PACKET_FANOUT_EBPF: |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1646 | __fanout_set_data_bpf(f, NULL); |
zhong jiang | 07d53ae | 2018-08-04 19:41:41 +0800 | [diff] [blame] | 1647 | } |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1648 | } |
| 1649 | |
Mike Maloney | 4a69a86 | 2017-04-21 10:56:11 -0400 | [diff] [blame] | 1650 | static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id) |
| 1651 | { |
| 1652 | struct packet_fanout *f; |
| 1653 | |
| 1654 | list_for_each_entry(f, &fanout_list, list) { |
| 1655 | if (f->id == candidate_id && |
| 1656 | read_pnet(&f->net) == sock_net(sk)) { |
| 1657 | return false; |
| 1658 | } |
| 1659 | } |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| 1663 | static bool fanout_find_new_id(struct sock *sk, u16 *new_id) |
| 1664 | { |
| 1665 | u16 id = fanout_next_id; |
| 1666 | |
| 1667 | do { |
| 1668 | if (__fanout_id_is_free(sk, id)) { |
| 1669 | *new_id = id; |
| 1670 | fanout_next_id = id + 1; |
| 1671 | return true; |
| 1672 | } |
| 1673 | |
| 1674 | id++; |
| 1675 | } while (id != fanout_next_id); |
| 1676 | |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1680 | static int fanout_add(struct sock *sk, struct fanout_args *args) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1681 | { |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1682 | struct packet_rollover *rollover = NULL; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1683 | struct packet_sock *po = pkt_sk(sk); |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1684 | u16 type_flags = args->type_flags; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1685 | struct packet_fanout *f, *match; |
David S. Miller | 7736d33 | 2011-07-05 01:43:20 -0700 | [diff] [blame] | 1686 | u8 type = type_flags & 0xff; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1687 | u8 flags = type_flags >> 8; |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1688 | u16 id = args->id; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1689 | int err; |
| 1690 | |
| 1691 | switch (type) { |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1692 | case PACKET_FANOUT_ROLLOVER: |
| 1693 | if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER) |
| 1694 | return -EINVAL; |
Gustavo A. R. Silva | 5af5a02 | 2020-11-20 12:38:20 -0600 | [diff] [blame] | 1695 | break; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1696 | case PACKET_FANOUT_HASH: |
| 1697 | case PACKET_FANOUT_LB: |
David S. Miller | 95ec3eb | 2011-07-06 01:56:38 -0700 | [diff] [blame] | 1698 | case PACKET_FANOUT_CPU: |
Daniel Borkmann | 5df0ddf | 2013-08-28 22:13:09 +0200 | [diff] [blame] | 1699 | case PACKET_FANOUT_RND: |
Neil Horman | 2d36097 | 2014-01-22 16:01:44 -0500 | [diff] [blame] | 1700 | case PACKET_FANOUT_QM: |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1701 | case PACKET_FANOUT_CBPF: |
Willem de Bruijn | f2e5209 | 2015-08-14 22:31:35 -0400 | [diff] [blame] | 1702 | case PACKET_FANOUT_EBPF: |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1703 | break; |
| 1704 | default: |
| 1705 | return -EINVAL; |
| 1706 | } |
| 1707 | |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1708 | mutex_lock(&fanout_mutex); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1709 | |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1710 | err = -EALREADY; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1711 | if (po->fanout) |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1712 | goto out; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1713 | |
Willem de Bruijn | 4633c9e | 2015-05-17 19:44:02 -0400 | [diff] [blame] | 1714 | if (type == PACKET_FANOUT_ROLLOVER || |
| 1715 | (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) { |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1716 | err = -ENOMEM; |
| 1717 | rollover = kzalloc(sizeof(*rollover), GFP_KERNEL); |
| 1718 | if (!rollover) |
| 1719 | goto out; |
| 1720 | atomic_long_set(&rollover->num, 0); |
| 1721 | atomic_long_set(&rollover->num_huge, 0); |
| 1722 | atomic_long_set(&rollover->num_failed, 0); |
Willem de Bruijn | 0648ab7 | 2015-05-12 11:56:46 -0400 | [diff] [blame] | 1723 | } |
| 1724 | |
Mike Maloney | 4a69a86 | 2017-04-21 10:56:11 -0400 | [diff] [blame] | 1725 | if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) { |
| 1726 | if (id != 0) { |
| 1727 | err = -EINVAL; |
| 1728 | goto out; |
| 1729 | } |
| 1730 | if (!fanout_find_new_id(sk, &id)) { |
| 1731 | err = -ENOMEM; |
| 1732 | goto out; |
| 1733 | } |
| 1734 | /* ephemeral flag for the first socket in the group: drop it */ |
| 1735 | flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8); |
| 1736 | } |
| 1737 | |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1738 | match = NULL; |
| 1739 | list_for_each_entry(f, &fanout_list, list) { |
| 1740 | if (f->id == id && |
| 1741 | read_pnet(&f->net) == sock_net(sk)) { |
| 1742 | match = f; |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1746 | err = -EINVAL; |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1747 | if (match) { |
| 1748 | if (match->flags != flags) |
| 1749 | goto out; |
| 1750 | if (args->max_num_members && |
| 1751 | args->max_num_members != match->max_num_members) |
| 1752 | goto out; |
| 1753 | } else { |
| 1754 | if (args->max_num_members > PACKET_FANOUT_MAX) |
| 1755 | goto out; |
| 1756 | if (!args->max_num_members) |
| 1757 | /* legacy PACKET_FANOUT_MAX */ |
| 1758 | args->max_num_members = 256; |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1759 | err = -ENOMEM; |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1760 | match = kvzalloc(struct_size(match, arr, args->max_num_members), |
| 1761 | GFP_KERNEL); |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1762 | if (!match) |
| 1763 | goto out; |
| 1764 | write_pnet(&match->net, sock_net(sk)); |
| 1765 | match->id = id; |
| 1766 | match->type = type; |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 1767 | match->flags = flags; |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1768 | INIT_LIST_HEAD(&match->list); |
| 1769 | spin_lock_init(&match->lock); |
Reshetova, Elena | fb5c2c1 | 2017-06-30 13:08:10 +0300 | [diff] [blame] | 1770 | refcount_set(&match->sk_ref, 0); |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 1771 | fanout_init_data(match); |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1772 | match->prot_hook.type = po->prot_hook.type; |
| 1773 | match->prot_hook.dev = po->prot_hook.dev; |
| 1774 | match->prot_hook.func = packet_rcv_fanout; |
| 1775 | match->prot_hook.af_packet_priv = match; |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1776 | match->prot_hook.id_match = match_fanout_group; |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1777 | match->max_num_members = args->max_num_members; |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1778 | list_add(&match->list, &fanout_list); |
| 1779 | } |
| 1780 | err = -EINVAL; |
Willem de Bruijn | 008ba2a | 2017-09-14 17:14:41 -0400 | [diff] [blame] | 1781 | |
| 1782 | spin_lock(&po->bind_lock); |
| 1783 | if (po->running && |
| 1784 | match->type == type && |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1785 | match->prot_hook.type == po->prot_hook.type && |
| 1786 | match->prot_hook.dev == po->prot_hook.dev) { |
| 1787 | err = -ENOSPC; |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1788 | if (refcount_read(&match->sk_ref) < match->max_num_members) { |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1789 | __dev_remove_pack(&po->prot_hook); |
| 1790 | po->fanout = match; |
Mike Maloney | 57f015f | 2017-11-28 10:44:29 -0500 | [diff] [blame] | 1791 | po->rollover = rollover; |
| 1792 | rollover = NULL; |
Reshetova, Elena | fb5c2c1 | 2017-06-30 13:08:10 +0300 | [diff] [blame] | 1793 | refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1); |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1794 | __fanout_link(sk, po); |
| 1795 | err = 0; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1796 | } |
| 1797 | } |
Willem de Bruijn | 008ba2a | 2017-09-14 17:14:41 -0400 | [diff] [blame] | 1798 | spin_unlock(&po->bind_lock); |
| 1799 | |
| 1800 | if (err && !refcount_read(&match->sk_ref)) { |
| 1801 | list_del(&match->list); |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 1802 | kvfree(match); |
Willem de Bruijn | 008ba2a | 2017-09-14 17:14:41 -0400 | [diff] [blame] | 1803 | } |
| 1804 | |
Eric Dumazet | afe62c6 | 2011-07-07 06:41:29 -0700 | [diff] [blame] | 1805 | out: |
Mike Maloney | 57f015f | 2017-11-28 10:44:29 -0500 | [diff] [blame] | 1806 | kfree(rollover); |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1807 | mutex_unlock(&fanout_mutex); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1808 | return err; |
| 1809 | } |
| 1810 | |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 1811 | /* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes |
| 1812 | * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout. |
| 1813 | * It is the responsibility of the caller to call fanout_release_data() and |
| 1814 | * free the returned packet_fanout (after synchronize_net()) |
| 1815 | */ |
| 1816 | static struct packet_fanout *fanout_release(struct sock *sk) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1817 | { |
| 1818 | struct packet_sock *po = pkt_sk(sk); |
| 1819 | struct packet_fanout *f; |
| 1820 | |
Pavel Emelyanov | fff3321 | 2012-08-16 05:36:48 +0000 | [diff] [blame] | 1821 | mutex_lock(&fanout_mutex); |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1822 | f = po->fanout; |
| 1823 | if (f) { |
| 1824 | po->fanout = NULL; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1825 | |
Reshetova, Elena | fb5c2c1 | 2017-06-30 13:08:10 +0300 | [diff] [blame] | 1826 | if (refcount_dec_and_test(&f->sk_ref)) |
Eric Dumazet | d199fab | 2017-02-14 09:03:51 -0800 | [diff] [blame] | 1827 | list_del(&f->list); |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 1828 | else |
| 1829 | f = NULL; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1830 | } |
| 1831 | mutex_unlock(&fanout_mutex); |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 1832 | |
| 1833 | return f; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 1834 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
Daniel Borkmann | 3c70c13 | 2015-11-11 23:25:42 +0100 | [diff] [blame] | 1836 | static bool packet_extra_vlan_len_allowed(const struct net_device *dev, |
| 1837 | struct sk_buff *skb) |
| 1838 | { |
| 1839 | /* Earlier code assumed this would be a VLAN pkt, double-check |
| 1840 | * this now that we have the actual packet in hand. We can only |
| 1841 | * do this check on Ethernet devices. |
| 1842 | */ |
| 1843 | if (unlikely(dev->type != ARPHRD_ETHER)) |
| 1844 | return false; |
| 1845 | |
| 1846 | skb_reset_mac_header(skb); |
| 1847 | return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q)); |
| 1848 | } |
| 1849 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1850 | static const struct proto_ops packet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1852 | static const struct proto_ops packet_ops_spkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1854 | static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, |
| 1855 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | { |
| 1857 | struct sock *sk; |
| 1858 | struct sockaddr_pkt *spkt; |
| 1859 | |
| 1860 | /* |
| 1861 | * When we registered the protocol we saved the socket in the data |
| 1862 | * field for just this event. |
| 1863 | */ |
| 1864 | |
| 1865 | sk = pt->af_packet_priv; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | /* |
| 1868 | * Yank back the headers [hope the device set this |
| 1869 | * right or kerboom...] |
| 1870 | * |
| 1871 | * Incoming packets have ll header pulled, |
| 1872 | * push it back. |
| 1873 | * |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 1874 | * For outgoing ones skb->data == skb_mac_header(skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | * so that this procedure is noop. |
| 1876 | */ |
| 1877 | |
| 1878 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 1879 | goto out; |
| 1880 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1881 | if (!net_eq(dev_net(dev), sock_net(sk))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 1882 | goto out; |
| 1883 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1884 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 1885 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | goto oom; |
| 1887 | |
| 1888 | /* drop any routing info */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1889 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 1891 | /* drop conntrack reference */ |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 1892 | nf_reset_ct(skb); |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 1893 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1894 | spkt = &PACKET_SKB_CB(skb)->sa.pkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 1896 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
| 1898 | /* |
| 1899 | * The SOCK_PACKET socket receives _all_ frames. |
| 1900 | */ |
| 1901 | |
| 1902 | spkt->spkt_family = dev->type; |
| 1903 | strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device)); |
| 1904 | spkt->spkt_protocol = skb->protocol; |
| 1905 | |
| 1906 | /* |
| 1907 | * Charge the memory to the socket. This is done specifically |
| 1908 | * to prevent sockets using all the memory up. |
| 1909 | */ |
| 1910 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1911 | if (sock_queue_rcv_skb(sk, skb) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | return 0; |
| 1913 | |
| 1914 | out: |
| 1915 | kfree_skb(skb); |
| 1916 | oom: |
| 1917 | return 0; |
| 1918 | } |
| 1919 | |
Maxim Mikityanskiy | 75c6577 | 2019-02-21 12:40:01 +0000 | [diff] [blame] | 1920 | static void packet_parse_headers(struct sk_buff *skb, struct socket *sock) |
| 1921 | { |
Yoshiki Komachi | 18bed89 | 2019-03-18 14:39:52 +0900 | [diff] [blame] | 1922 | if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) && |
| 1923 | sock->type == SOCK_RAW) { |
Maxim Mikityanskiy | 75c6577 | 2019-02-21 12:40:01 +0000 | [diff] [blame] | 1924 | skb_reset_mac_header(skb); |
| 1925 | skb->protocol = dev_parse_header_protocol(skb); |
| 1926 | } |
| 1927 | |
| 1928 | skb_probe_transport_header(skb); |
| 1929 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | |
| 1931 | /* |
| 1932 | * Output a raw packet to a device layer. This bypasses all the other |
| 1933 | * protocol layers and you must therefore supply it with a complete frame |
| 1934 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1935 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1936 | static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, |
| 1937 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | { |
| 1939 | struct sock *sk = sock->sk; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1940 | DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name); |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 1941 | struct sk_buff *skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | struct net_device *dev; |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 1943 | struct sockcm_cookie sockc; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1944 | __be16 proto = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | int err; |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 1946 | int extra_len = 0; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1949 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | */ |
| 1951 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1952 | if (saddr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | if (msg->msg_namelen < sizeof(struct sockaddr)) |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1954 | return -EINVAL; |
| 1955 | if (msg->msg_namelen == sizeof(struct sockaddr_pkt)) |
| 1956 | proto = saddr->spkt_protocol; |
| 1957 | } else |
| 1958 | return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | |
| 1960 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1961 | * Find the device first to size check it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | */ |
| 1963 | |
danborkmann@iogearbox.net | de74e92 | 2012-06-10 08:59:28 +0000 | [diff] [blame] | 1964 | saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0; |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 1965 | retry: |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 1966 | rcu_read_lock(); |
| 1967 | dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | err = -ENODEV; |
| 1969 | if (dev == NULL) |
| 1970 | goto out_unlock; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1971 | |
David S. Miller | d5e76b0 | 2007-01-25 19:30:36 -0800 | [diff] [blame] | 1972 | err = -ENETDOWN; |
| 1973 | if (!(dev->flags & IFF_UP)) |
| 1974 | goto out_unlock; |
| 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | /* |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1977 | * You may not queue a frame bigger than the mtu. This is the lowest level |
| 1978 | * raw protocol and you must do your own fragmentation at this level. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1980 | |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 1981 | if (unlikely(sock_flag(sk, SOCK_NOFCS))) { |
| 1982 | if (!netif_supports_nofcs(dev)) { |
| 1983 | err = -EPROTONOSUPPORT; |
| 1984 | goto out_unlock; |
| 1985 | } |
| 1986 | extra_len = 4; /* We're doing our own CRC */ |
| 1987 | } |
| 1988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | err = -EMSGSIZE; |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 1990 | if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | goto out_unlock; |
| 1992 | |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 1993 | if (!skb) { |
| 1994 | size_t reserved = LL_RESERVED_SPACE(dev); |
Herbert Xu | 4ce4091 | 2011-11-18 02:20:05 +0000 | [diff] [blame] | 1995 | int tlen = dev->needed_tailroom; |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 1996 | unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 1998 | rcu_read_unlock(); |
Herbert Xu | 4ce4091 | 2011-11-18 02:20:05 +0000 | [diff] [blame] | 1999 | skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL); |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 2000 | if (skb == NULL) |
| 2001 | return -ENOBUFS; |
| 2002 | /* FIXME: Save some space for broken drivers that write a hard |
| 2003 | * header at transmission time by themselves. PPP is the notable |
| 2004 | * one here. This should really be fixed at the driver level. |
| 2005 | */ |
| 2006 | skb_reserve(skb, reserved); |
| 2007 | skb_reset_network_header(skb); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2008 | |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 2009 | /* Try to align data part correctly */ |
| 2010 | if (hhlen) { |
| 2011 | skb->data -= hhlen; |
| 2012 | skb->tail -= hhlen; |
| 2013 | if (len < hhlen) |
| 2014 | skb_reset_network_header(skb); |
| 2015 | } |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 2016 | err = memcpy_from_msg(skb_put(skb, len), msg, len); |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 2017 | if (err) |
| 2018 | goto out_free; |
| 2019 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 2022 | if (!dev_validate_header(dev, skb->data, len)) { |
| 2023 | err = -EINVAL; |
| 2024 | goto out_unlock; |
| 2025 | } |
Daniel Borkmann | 3c70c13 | 2015-11-11 23:25:42 +0100 | [diff] [blame] | 2026 | if (len > (dev->mtu + dev->hard_header_len + extra_len) && |
| 2027 | !packet_extra_vlan_len_allowed(dev, skb)) { |
| 2028 | err = -EMSGSIZE; |
| 2029 | goto out_unlock; |
Ben Greear | 57f89bf | 2011-02-11 09:35:18 +0000 | [diff] [blame] | 2030 | } |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 2031 | |
Willem de Bruijn | 657a066 | 2018-07-06 10:12:56 -0400 | [diff] [blame] | 2032 | sockcm_init(&sockc, sk); |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 2033 | if (msg->msg_controllen) { |
| 2034 | err = sock_cmsg_send(sk, msg, &sockc); |
Soheil Hassas Yeganeh | f8e7718 | 2016-07-20 18:01:18 -0400 | [diff] [blame] | 2035 | if (unlikely(err)) |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 2036 | goto out_unlock; |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 2037 | } |
| 2038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | skb->protocol = proto; |
| 2040 | skb->dev = dev; |
| 2041 | skb->priority = sk->sk_priority; |
Eric Dumazet | 2d37a18 | 2009-10-01 19:14:46 +0000 | [diff] [blame] | 2042 | skb->mark = sk->sk_mark; |
Richard Cochran | 3d0ba8c | 2018-07-03 15:42:51 -0700 | [diff] [blame] | 2043 | skb->tstamp = sockc.transmit_time; |
Daniel Borkmann | bf84a010 | 2013-04-14 08:08:13 +0000 | [diff] [blame] | 2044 | |
Willem de Bruijn | 8f932f7 | 2018-12-17 12:24:00 -0500 | [diff] [blame] | 2045 | skb_setup_tx_timestamp(skb, sockc.tsflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 2047 | if (unlikely(extra_len == 4)) |
| 2048 | skb->no_fcs = 1; |
| 2049 | |
Maxim Mikityanskiy | 75c6577 | 2019-02-21 12:40:01 +0000 | [diff] [blame] | 2050 | packet_parse_headers(skb, sock); |
Jason Wang | c1aad27 | 2013-03-25 20:19:57 +0000 | [diff] [blame] | 2051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | dev_queue_xmit(skb); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 2053 | rcu_read_unlock(); |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2054 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | out_unlock: |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 2057 | rcu_read_unlock(); |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 2058 | out_free: |
| 2059 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | return err; |
| 2061 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2063 | static unsigned int run_filter(struct sk_buff *skb, |
| 2064 | const struct sock *sk, |
| 2065 | unsigned int res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | { |
| 2067 | struct sk_filter *filter; |
| 2068 | |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 2069 | rcu_read_lock(); |
| 2070 | filter = rcu_dereference(sk->sk_filter); |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2071 | if (filter != NULL) |
Alexei Starovoitov | ff936a0 | 2015-10-07 10:55:41 -0700 | [diff] [blame] | 2072 | res = bpf_prog_run_clear_cb(filter->prog, skb); |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 2073 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2075 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2078 | static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb, |
| 2079 | size_t *len) |
| 2080 | { |
| 2081 | struct virtio_net_hdr vnet_hdr; |
| 2082 | |
| 2083 | if (*len < sizeof(vnet_hdr)) |
| 2084 | return -EINVAL; |
| 2085 | *len -= sizeof(vnet_hdr); |
| 2086 | |
Willem de Bruijn | fd3a886 | 2018-06-06 11:23:01 -0400 | [diff] [blame] | 2087 | if (virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0)) |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2088 | return -EINVAL; |
| 2089 | |
| 2090 | return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr)); |
| 2091 | } |
| 2092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | /* |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 2094 | * This function makes lazy skb cloning in hope that most of packets |
| 2095 | * are discarded by BPF. |
| 2096 | * |
| 2097 | * Note tricky part: we DO mangle shared skb! skb->data, skb->len |
| 2098 | * and skb->cb are mangled. It works because (and until) packets |
| 2099 | * falling here are owned by current CPU. Output packets are cloned |
| 2100 | * by dev_queue_xmit_nit(), input packets are processed by net_bh |
Wang Hai | 0e4161d | 2021-03-24 14:19:31 +0800 | [diff] [blame] | 2101 | * sequentially, so that if we return skb to original state on exit, |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 2102 | * we will not harm anyone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | */ |
| 2104 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2105 | static int packet_rcv(struct sk_buff *skb, struct net_device *dev, |
| 2106 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | { |
| 2108 | struct sock *sk; |
| 2109 | struct sockaddr_ll *sll; |
| 2110 | struct packet_sock *po; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2111 | u8 *skb_head = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | int skb_len = skb->len; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2113 | unsigned int snaplen, res; |
Weongyo Jeong | da37845 | 2016-04-14 14:10:04 -0700 | [diff] [blame] | 2114 | bool is_drop_n_account = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | |
| 2116 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 2117 | goto drop; |
| 2118 | |
| 2119 | sk = pt->af_packet_priv; |
| 2120 | po = pkt_sk(sk); |
| 2121 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 2122 | if (!net_eq(dev_net(dev), sock_net(sk))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2123 | goto drop; |
| 2124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | skb->dev = dev; |
| 2126 | |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 2127 | if (dev_has_header(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | /* The device has an explicit notion of ll header, |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 2129 | * exported to higher levels. |
| 2130 | * |
| 2131 | * Otherwise, the device hides details of its frame |
| 2132 | * structure, so that corresponding packet head is |
| 2133 | * never delivered to user. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | */ |
| 2135 | if (sk->sk_type != SOCK_DGRAM) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 2136 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | else if (skb->pkt_type == PACKET_OUTGOING) { |
| 2138 | /* Special case: outgoing packets have ll header at head */ |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 2139 | skb_pull(skb, skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | snaplen = skb->len; |
| 2144 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2145 | res = run_filter(skb, sk, snaplen); |
| 2146 | if (!res) |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 2147 | goto drop_n_restore; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2148 | if (snaplen > res) |
| 2149 | snaplen = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
Eric Dumazet | 0fd7bac | 2011-12-21 07:11:44 +0000 | [diff] [blame] | 2151 | if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | goto drop_n_acct; |
| 2153 | |
| 2154 | if (skb_shared(skb)) { |
| 2155 | struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); |
| 2156 | if (nskb == NULL) |
| 2157 | goto drop_n_acct; |
| 2158 | |
| 2159 | if (skb_head != skb->data) { |
| 2160 | skb->data = skb_head; |
| 2161 | skb->len = skb_len; |
| 2162 | } |
Eric Dumazet | abc4e4f | 2012-04-19 02:24:42 +0000 | [diff] [blame] | 2163 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | skb = nskb; |
| 2165 | } |
| 2166 | |
Eyal Birger | b4772ef | 2015-03-01 14:58:29 +0200 | [diff] [blame] | 2167 | sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8); |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 2168 | |
| 2169 | sll = &PACKET_SKB_CB(skb)->sa.ll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | sll->sll_hatype = dev->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | sll->sll_pkttype = skb->pkt_type; |
Peter P Waskiewicz Jr | 8032b46 | 2007-11-10 22:03:25 -0800 | [diff] [blame] | 2172 | if (unlikely(po->origdev)) |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 2173 | sll->sll_ifindex = orig_dev->ifindex; |
| 2174 | else |
| 2175 | sll->sll_ifindex = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 2177 | sll->sll_halen = dev_parse_header(skb, sll->sll_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | |
Eyal Birger | 2472d76 | 2015-03-01 14:58:28 +0200 | [diff] [blame] | 2179 | /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg(). |
| 2180 | * Use their space for storing the original skb length. |
| 2181 | */ |
| 2182 | PACKET_SKB_CB(skb)->sa.origlen = skb->len; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | if (pskb_trim(skb, snaplen)) |
| 2185 | goto drop_n_acct; |
| 2186 | |
| 2187 | skb_set_owner_r(skb, sk); |
| 2188 | skb->dev = NULL; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2189 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 2191 | /* drop conntrack reference */ |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 2192 | nf_reset_ct(skb); |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 2193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | spin_lock(&sk->sk_receive_queue.lock); |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 2195 | po->stats.stats1.tp_packets++; |
Eyal Birger | 3bc3b96 | 2015-03-01 14:58:30 +0200 | [diff] [blame] | 2196 | sock_skb_set_dropcount(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 2198 | spin_unlock(&sk->sk_receive_queue.lock); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 2199 | sk->sk_data_ready(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | return 0; |
| 2201 | |
| 2202 | drop_n_acct: |
Weongyo Jeong | da37845 | 2016-04-14 14:10:04 -0700 | [diff] [blame] | 2203 | is_drop_n_account = true; |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 2204 | atomic_inc(&po->tp_drops); |
Willem de Bruijn | 7091fbd | 2011-09-30 10:38:28 +0000 | [diff] [blame] | 2205 | atomic_inc(&sk->sk_drops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
| 2207 | drop_n_restore: |
| 2208 | if (skb_head != skb->data && skb_shared(skb)) { |
| 2209 | skb->data = skb_head; |
| 2210 | skb->len = skb_len; |
| 2211 | } |
| 2212 | drop: |
Weongyo Jeong | da37845 | 2016-04-14 14:10:04 -0700 | [diff] [blame] | 2213 | if (!is_drop_n_account) |
| 2214 | consume_skb(skb); |
| 2215 | else |
| 2216 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
| 2219 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2220 | static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, |
| 2221 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | { |
| 2223 | struct sock *sk; |
| 2224 | struct packet_sock *po; |
| 2225 | struct sockaddr_ll *sll; |
Daniel Borkmann | 184f489 | 2013-04-16 01:57:46 +0000 | [diff] [blame] | 2226 | union tpacket_uhdr h; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2227 | u8 *skb_head = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | int skb_len = skb->len; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2229 | unsigned int snaplen, res; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2230 | unsigned long status = TP_STATUS_USER; |
Or Cohen | acf69c9 | 2020-09-03 21:05:28 -0700 | [diff] [blame] | 2231 | unsigned short macoff, hdrlen; |
| 2232 | unsigned int netoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | struct sk_buff *copy_skb = NULL; |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 2234 | struct timespec64 ts; |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 2235 | __u32 ts_status; |
Weongyo Jeong | da37845 | 2016-04-14 14:10:04 -0700 | [diff] [blame] | 2236 | bool is_drop_n_account = false; |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 2237 | unsigned int slot_id = 0; |
Benjamin Poirier | edbd58b | 2017-08-28 14:29:41 -0400 | [diff] [blame] | 2238 | bool do_vnet = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
Atzm Watanabe | 5184635 | 2013-12-17 22:53:32 +0900 | [diff] [blame] | 2240 | /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT. |
| 2241 | * We may add members to them until current aligned size without forcing |
| 2242 | * userspace to call getsockopt(..., PACKET_HDRLEN, ...). |
| 2243 | */ |
| 2244 | BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32); |
| 2245 | BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48); |
| 2246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 2248 | goto drop; |
| 2249 | |
| 2250 | sk = pt->af_packet_priv; |
| 2251 | po = pkt_sk(sk); |
| 2252 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 2253 | if (!net_eq(dev_net(dev), sock_net(sk))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2254 | goto drop; |
| 2255 | |
Eyal Birger | d549699 | 2020-11-21 08:28:17 +0200 | [diff] [blame] | 2256 | if (dev_has_header(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | if (sk->sk_type != SOCK_DGRAM) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 2258 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | else if (skb->pkt_type == PACKET_OUTGOING) { |
| 2260 | /* Special case: outgoing packets have ll header at head */ |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 2261 | skb_pull(skb, skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | snaplen = skb->len; |
| 2266 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2267 | res = run_filter(skb, sk, snaplen); |
| 2268 | if (!res) |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 2269 | goto drop_n_restore; |
Alexander Drozdov | 68c2e5d | 2015-03-23 09:11:12 +0300 | [diff] [blame] | 2270 | |
Eric Dumazet | 2c51c62 | 2019-06-12 09:52:31 -0700 | [diff] [blame] | 2271 | /* If we are flooded, just give up */ |
| 2272 | if (__packet_rcv_has_room(po, skb) == ROOM_NONE) { |
| 2273 | atomic_inc(&po->tp_drops); |
| 2274 | goto drop_n_restore; |
| 2275 | } |
| 2276 | |
Alexander Drozdov | 68c2e5d | 2015-03-23 09:11:12 +0300 | [diff] [blame] | 2277 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 2278 | status |= TP_STATUS_CSUMNOTREADY; |
Alexander Drozdov | 682f048 | 2015-03-23 09:11:13 +0300 | [diff] [blame] | 2279 | else if (skb->pkt_type != PACKET_OUTGOING && |
| 2280 | (skb->ip_summed == CHECKSUM_COMPLETE || |
| 2281 | skb_csum_unnecessary(skb))) |
| 2282 | status |= TP_STATUS_CSUM_VALID; |
Alexander Drozdov | 68c2e5d | 2015-03-23 09:11:12 +0300 | [diff] [blame] | 2283 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 2284 | if (snaplen > res) |
| 2285 | snaplen = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
| 2287 | if (sk->sk_type == SOCK_DGRAM) { |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 2288 | macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 + |
| 2289 | po->tp_reserve; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } else { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 2291 | unsigned int maclen = skb_network_offset(skb); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2292 | netoff = TPACKET_ALIGN(po->tp_hdrlen + |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 2293 | (maclen < 16 ? 16 : maclen)) + |
Willem de Bruijn | 58d19b1 | 2016-02-03 18:02:15 -0500 | [diff] [blame] | 2294 | po->tp_reserve; |
Benjamin Poirier | edbd58b | 2017-08-28 14:29:41 -0400 | [diff] [blame] | 2295 | if (po->has_vnet_hdr) { |
Willem de Bruijn | 58d19b1 | 2016-02-03 18:02:15 -0500 | [diff] [blame] | 2296 | netoff += sizeof(struct virtio_net_hdr); |
Benjamin Poirier | edbd58b | 2017-08-28 14:29:41 -0400 | [diff] [blame] | 2297 | do_vnet = true; |
| 2298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | macoff = netoff - maclen; |
| 2300 | } |
Or Cohen | acf69c9 | 2020-09-03 21:05:28 -0700 | [diff] [blame] | 2301 | if (netoff > USHRT_MAX) { |
| 2302 | atomic_inc(&po->tp_drops); |
| 2303 | goto drop_n_restore; |
| 2304 | } |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2305 | if (po->tp_version <= TPACKET_V2) { |
| 2306 | if (macoff + snaplen > po->rx_ring.frame_size) { |
| 2307 | if (po->copy_thresh && |
Eric Dumazet | 0fd7bac | 2011-12-21 07:11:44 +0000 | [diff] [blame] | 2308 | atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2309 | if (skb_shared(skb)) { |
| 2310 | copy_skb = skb_clone(skb, GFP_ATOMIC); |
| 2311 | } else { |
| 2312 | copy_skb = skb_get(skb); |
| 2313 | skb_head = skb->data; |
| 2314 | } |
| 2315 | if (copy_skb) |
| 2316 | skb_set_owner_r(copy_skb, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | } |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2318 | snaplen = po->rx_ring.frame_size - macoff; |
Benjamin Poirier | edbd58b | 2017-08-28 14:29:41 -0400 | [diff] [blame] | 2319 | if ((int)snaplen < 0) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2320 | snaplen = 0; |
Benjamin Poirier | edbd58b | 2017-08-28 14:29:41 -0400 | [diff] [blame] | 2321 | do_vnet = false; |
| 2322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | } |
Eric Dumazet | dc80811 | 2014-08-15 09:16:04 -0700 | [diff] [blame] | 2324 | } else if (unlikely(macoff + snaplen > |
| 2325 | GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) { |
| 2326 | u32 nval; |
| 2327 | |
| 2328 | nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff; |
| 2329 | pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n", |
| 2330 | snaplen, nval, macoff); |
| 2331 | snaplen = nval; |
| 2332 | if (unlikely((int)snaplen < 0)) { |
| 2333 | snaplen = 0; |
| 2334 | macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len; |
Benjamin Poirier | edbd58b | 2017-08-28 14:29:41 -0400 | [diff] [blame] | 2335 | do_vnet = false; |
Eric Dumazet | dc80811 | 2014-08-15 09:16:04 -0700 | [diff] [blame] | 2336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | spin_lock(&sk->sk_receive_queue.lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2339 | h.raw = packet_current_rx_frame(po, skb, |
| 2340 | TP_STATUS_KERNEL, (macoff+snaplen)); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2341 | if (!h.raw) |
Willem de Bruijn | 58d19b1 | 2016-02-03 18:02:15 -0500 | [diff] [blame] | 2342 | goto drop_n_account; |
Willem de Bruijn | 46e4c42 | 2020-03-09 11:34:35 -0400 | [diff] [blame] | 2343 | |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 2344 | if (po->tp_version <= TPACKET_V2) { |
| 2345 | slot_id = po->rx_ring.head; |
| 2346 | if (test_bit(slot_id, po->rx_ring.rx_owner_map)) |
| 2347 | goto drop_n_account; |
| 2348 | __set_bit(slot_id, po->rx_ring.rx_owner_map); |
| 2349 | } |
| 2350 | |
Willem de Bruijn | 46e4c42 | 2020-03-09 11:34:35 -0400 | [diff] [blame] | 2351 | if (do_vnet && |
| 2352 | virtio_net_hdr_from_skb(skb, h.raw + macoff - |
| 2353 | sizeof(struct virtio_net_hdr), |
John Ogness | 88fd1cb | 2020-08-13 21:45:25 +0206 | [diff] [blame] | 2354 | vio_le(), true, 0)) { |
| 2355 | if (po->tp_version == TPACKET_V3) |
| 2356 | prb_clear_blk_fill_status(&po->rx_ring); |
Willem de Bruijn | 46e4c42 | 2020-03-09 11:34:35 -0400 | [diff] [blame] | 2357 | goto drop_n_account; |
John Ogness | 88fd1cb | 2020-08-13 21:45:25 +0206 | [diff] [blame] | 2358 | } |
Willem de Bruijn | 46e4c42 | 2020-03-09 11:34:35 -0400 | [diff] [blame] | 2359 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2360 | if (po->tp_version <= TPACKET_V2) { |
| 2361 | packet_increment_rx_head(po, &po->rx_ring); |
| 2362 | /* |
| 2363 | * LOSING will be reported till you read the stats, |
| 2364 | * because it's COR - Clear On Read. |
| 2365 | * Anyways, moving it for V1/V2 only as V3 doesn't need this |
| 2366 | * at packet level. |
| 2367 | */ |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 2368 | if (atomic_read(&po->tp_drops)) |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2369 | status |= TP_STATUS_LOSING; |
| 2370 | } |
Eric Dumazet | 945d015 | 2018-06-21 14:16:02 -0700 | [diff] [blame] | 2371 | |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 2372 | po->stats.stats1.tp_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | if (copy_skb) { |
| 2374 | status |= TP_STATUS_COPY; |
| 2375 | __skb_queue_tail(&sk->sk_receive_queue, copy_skb); |
| 2376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | spin_unlock(&sk->sk_receive_queue.lock); |
| 2378 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2379 | skb_copy_bits(skb, 0, h.raw + macoff, snaplen); |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 2380 | |
Richard Sanger | 171c3b1 | 2021-05-12 13:31:22 +1200 | [diff] [blame] | 2381 | /* Always timestamp; prefer an existing software timestamp taken |
| 2382 | * closer to the time of capture. |
| 2383 | */ |
| 2384 | ts_status = tpacket_get_timestamp(skb, &ts, |
| 2385 | po->tp_tstamp | SOF_TIMESTAMPING_SOFTWARE); |
| 2386 | if (!ts_status) |
Arnd Bergmann | d413fcb | 2017-11-27 10:09:24 +0100 | [diff] [blame] | 2387 | ktime_get_real_ts64(&ts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 2389 | status |= ts_status; |
| 2390 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2391 | switch (po->tp_version) { |
| 2392 | case TPACKET_V1: |
| 2393 | h.h1->tp_len = skb->len; |
| 2394 | h.h1->tp_snaplen = snaplen; |
| 2395 | h.h1->tp_mac = macoff; |
| 2396 | h.h1->tp_net = netoff; |
Daniel Borkmann | 4b457bd | 2013-04-16 01:29:11 +0000 | [diff] [blame] | 2397 | h.h1->tp_sec = ts.tv_sec; |
| 2398 | h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2399 | hdrlen = sizeof(*h.h1); |
| 2400 | break; |
| 2401 | case TPACKET_V2: |
| 2402 | h.h2->tp_len = skb->len; |
| 2403 | h.h2->tp_snaplen = snaplen; |
| 2404 | h.h2->tp_mac = macoff; |
| 2405 | h.h2->tp_net = netoff; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2406 | h.h2->tp_sec = ts.tv_sec; |
| 2407 | h.h2->tp_nsec = ts.tv_nsec; |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 2408 | if (skb_vlan_tag_present(skb)) { |
| 2409 | h.h2->tp_vlan_tci = skb_vlan_tag_get(skb); |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 2410 | h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto); |
| 2411 | status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; |
Ben Greear | a3bcc23 | 2011-06-01 06:49:10 +0000 | [diff] [blame] | 2412 | } else { |
| 2413 | h.h2->tp_vlan_tci = 0; |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 2414 | h.h2->tp_vlan_tpid = 0; |
Ben Greear | a3bcc23 | 2011-06-01 06:49:10 +0000 | [diff] [blame] | 2415 | } |
Atzm Watanabe | e4d26f4 | 2013-12-17 22:53:36 +0900 | [diff] [blame] | 2416 | memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding)); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2417 | hdrlen = sizeof(*h.h2); |
| 2418 | break; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2419 | case TPACKET_V3: |
| 2420 | /* tp_nxt_offset,vlan are already populated above. |
| 2421 | * So DONT clear those fields here |
| 2422 | */ |
| 2423 | h.h3->tp_status |= status; |
| 2424 | h.h3->tp_len = skb->len; |
| 2425 | h.h3->tp_snaplen = snaplen; |
| 2426 | h.h3->tp_mac = macoff; |
| 2427 | h.h3->tp_net = netoff; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2428 | h.h3->tp_sec = ts.tv_sec; |
| 2429 | h.h3->tp_nsec = ts.tv_nsec; |
Atzm Watanabe | e4d26f4 | 2013-12-17 22:53:36 +0900 | [diff] [blame] | 2430 | memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding)); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2431 | hdrlen = sizeof(*h.h3); |
| 2432 | break; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2433 | default: |
| 2434 | BUG(); |
| 2435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2437 | sll = h.raw + TPACKET_ALIGN(hdrlen); |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 2438 | sll->sll_halen = dev_parse_header(skb, sll->sll_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | sll->sll_family = AF_PACKET; |
| 2440 | sll->sll_hatype = dev->type; |
| 2441 | sll->sll_protocol = skb->protocol; |
| 2442 | sll->sll_pkttype = skb->pkt_type; |
Peter P Waskiewicz Jr | 8032b46 | 2007-11-10 22:03:25 -0800 | [diff] [blame] | 2443 | if (unlikely(po->origdev)) |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 2444 | sll->sll_ifindex = orig_dev->ifindex; |
| 2445 | else |
| 2446 | sll->sll_ifindex = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | |
Ralf Baechle | e16aa20 | 2006-12-07 00:11:33 -0800 | [diff] [blame] | 2448 | smp_mb(); |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 2449 | |
Changli Gao | f6dafa9 | 2010-12-07 04:26:16 +0000 | [diff] [blame] | 2450 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 2451 | if (po->tp_version <= TPACKET_V2) { |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 2452 | u8 *start, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 2454 | end = (u8 *) PAGE_ALIGN((unsigned long) h.raw + |
| 2455 | macoff + snaplen); |
| 2456 | |
| 2457 | for (start = h.raw; start < end; start += PAGE_SIZE) |
| 2458 | flush_dcache_page(pgv_to_page(start)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 2460 | smp_wmb(); |
Changli Gao | f6dafa9 | 2010-12-07 04:26:16 +0000 | [diff] [blame] | 2461 | #endif |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 2462 | |
Dan Collins | da413ee | 2014-12-19 16:49:25 +1300 | [diff] [blame] | 2463 | if (po->tp_version <= TPACKET_V2) { |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 2464 | spin_lock(&sk->sk_receive_queue.lock); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2465 | __packet_set_status(po, h.raw, status); |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 2466 | __clear_bit(slot_id, po->rx_ring.rx_owner_map); |
| 2467 | spin_unlock(&sk->sk_receive_queue.lock); |
Dan Collins | da413ee | 2014-12-19 16:49:25 +1300 | [diff] [blame] | 2468 | sk->sk_data_ready(sk); |
John Ogness | 88fd1cb | 2020-08-13 21:45:25 +0206 | [diff] [blame] | 2469 | } else if (po->tp_version == TPACKET_V3) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 2470 | prb_clear_blk_fill_status(&po->rx_ring); |
Dan Collins | da413ee | 2014-12-19 16:49:25 +1300 | [diff] [blame] | 2471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | |
| 2473 | drop_n_restore: |
| 2474 | if (skb_head != skb->data && skb_shared(skb)) { |
| 2475 | skb->data = skb_head; |
| 2476 | skb->len = skb_len; |
| 2477 | } |
| 2478 | drop: |
Weongyo Jeong | da37845 | 2016-04-14 14:10:04 -0700 | [diff] [blame] | 2479 | if (!is_drop_n_account) |
| 2480 | consume_skb(skb); |
| 2481 | else |
| 2482 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | return 0; |
| 2484 | |
Willem de Bruijn | 58d19b1 | 2016-02-03 18:02:15 -0500 | [diff] [blame] | 2485 | drop_n_account: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | spin_unlock(&sk->sk_receive_queue.lock); |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 2487 | atomic_inc(&po->tp_drops); |
| 2488 | is_drop_n_account = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 2490 | sk->sk_data_ready(sk); |
Wei Yongjun | acb5d75 | 2009-02-25 00:36:42 +0000 | [diff] [blame] | 2491 | kfree_skb(copy_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | goto drop_n_restore; |
| 2493 | } |
| 2494 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2495 | static void tpacket_destruct_skb(struct sk_buff *skb) |
| 2496 | { |
| 2497 | struct packet_sock *po = pkt_sk(skb->sk); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2498 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2499 | if (likely(po->tx_ring.pg_vec)) { |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 2500 | void *ph; |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 2501 | __u32 ts; |
| 2502 | |
Willem de Bruijn | 5cd8d46 | 2018-11-20 13:00:18 -0500 | [diff] [blame] | 2503 | ph = skb_zcopy_get_nouarg(skb); |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 2504 | packet_dec_pending(&po->tx_ring); |
Daniel Borkmann | b9c32fb | 2013-04-23 00:39:31 +0000 | [diff] [blame] | 2505 | |
| 2506 | ts = __packet_set_timestamp(po, ph, skb); |
| 2507 | __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts); |
Neil Horman | 89ed5b5 | 2019-06-25 17:57:49 -0400 | [diff] [blame] | 2508 | |
| 2509 | if (!packet_read_pending(&po->tx_ring)) |
| 2510 | complete(&po->skb_completion); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | sock_wfree(skb); |
| 2514 | } |
| 2515 | |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2516 | static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len) |
| 2517 | { |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2518 | if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
| 2519 | (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) + |
| 2520 | __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 > |
| 2521 | __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len))) |
| 2522 | vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(), |
| 2523 | __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) + |
| 2524 | __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2); |
| 2525 | |
| 2526 | if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len) |
| 2527 | return -EINVAL; |
| 2528 | |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2529 | return 0; |
| 2530 | } |
| 2531 | |
| 2532 | static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len, |
| 2533 | struct virtio_net_hdr *vnet_hdr) |
| 2534 | { |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2535 | if (*len < sizeof(*vnet_hdr)) |
| 2536 | return -EINVAL; |
| 2537 | *len -= sizeof(*vnet_hdr); |
| 2538 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 2539 | if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter)) |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2540 | return -EFAULT; |
| 2541 | |
| 2542 | return __packet_snd_vnet_parse(vnet_hdr, *len); |
| 2543 | } |
| 2544 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2545 | static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb, |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2546 | void *frame, struct net_device *dev, void *data, int tp_len, |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 2547 | __be16 proto, unsigned char *addr, int hlen, int copylen, |
| 2548 | const struct sockcm_cookie *sockc) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2549 | { |
Daniel Borkmann | 184f489 | 2013-04-16 01:57:46 +0000 | [diff] [blame] | 2550 | union tpacket_uhdr ph; |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2551 | int to_write, offset, len, nr_frags, len_max; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2552 | struct socket *sock = po->sk.sk_socket; |
| 2553 | struct page *page; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2554 | int err; |
| 2555 | |
| 2556 | ph.raw = frame; |
| 2557 | |
| 2558 | skb->protocol = proto; |
| 2559 | skb->dev = dev; |
| 2560 | skb->priority = po->sk.sk_priority; |
Eric Dumazet | 2d37a18 | 2009-10-01 19:14:46 +0000 | [diff] [blame] | 2561 | skb->mark = po->sk.sk_mark; |
Richard Cochran | 3d0ba8c | 2018-07-03 15:42:51 -0700 | [diff] [blame] | 2562 | skb->tstamp = sockc->transmit_time; |
Willem de Bruijn | 8f932f7 | 2018-12-17 12:24:00 -0500 | [diff] [blame] | 2563 | skb_setup_tx_timestamp(skb, sockc->tsflags); |
Willem de Bruijn | 5cd8d46 | 2018-11-20 13:00:18 -0500 | [diff] [blame] | 2564 | skb_zcopy_set_nouarg(skb, ph.raw); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2565 | |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 2566 | skb_reserve(skb, hlen); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2567 | skb_reset_network_header(skb); |
Jason Wang | c1aad27 | 2013-03-25 20:19:57 +0000 | [diff] [blame] | 2568 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2569 | to_write = tp_len; |
| 2570 | |
| 2571 | if (sock->type == SOCK_DGRAM) { |
| 2572 | err = dev_hard_header(skb, dev, ntohs(proto), addr, |
| 2573 | NULL, tp_len); |
| 2574 | if (unlikely(err < 0)) |
| 2575 | return -EINVAL; |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2576 | } else if (copylen) { |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 2577 | int hdrlen = min_t(int, copylen, tp_len); |
| 2578 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2579 | skb_push(skb, dev->hard_header_len); |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2580 | skb_put(skb, copylen - dev->hard_header_len); |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 2581 | err = skb_store_bits(skb, 0, data, hdrlen); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2582 | if (unlikely(err)) |
| 2583 | return err; |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 2584 | if (!dev_validate_header(dev, skb->data, hdrlen)) |
| 2585 | return -EINVAL; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2586 | |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 2587 | data += hdrlen; |
| 2588 | to_write -= hdrlen; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2591 | offset = offset_in_page(data); |
| 2592 | len_max = PAGE_SIZE - offset; |
| 2593 | len = ((to_write > len_max) ? len_max : to_write); |
| 2594 | |
| 2595 | skb->data_len = to_write; |
| 2596 | skb->len += to_write; |
| 2597 | skb->truesize += to_write; |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 2598 | refcount_add(to_write, &po->sk.sk_wmem_alloc); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2599 | |
| 2600 | while (likely(to_write)) { |
| 2601 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 2602 | |
| 2603 | if (unlikely(nr_frags >= MAX_SKB_FRAGS)) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2604 | pr_err("Packet exceed the number of skb frags(%lu)\n", |
| 2605 | MAX_SKB_FRAGS); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2606 | return -EFAULT; |
| 2607 | } |
| 2608 | |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 2609 | page = pgv_to_page(data); |
| 2610 | data += len; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2611 | flush_dcache_page(page); |
| 2612 | get_page(page); |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 2613 | skb_fill_page_desc(skb, nr_frags, page, offset, len); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2614 | to_write -= len; |
| 2615 | offset = 0; |
| 2616 | len_max = PAGE_SIZE; |
| 2617 | len = ((to_write > len_max) ? len_max : to_write); |
| 2618 | } |
| 2619 | |
Maxim Mikityanskiy | 75c6577 | 2019-02-21 12:40:01 +0000 | [diff] [blame] | 2620 | packet_parse_headers(skb, sock); |
Daniel Borkmann | efdfa2f | 2015-11-11 23:25:40 +0100 | [diff] [blame] | 2621 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2622 | return tp_len; |
| 2623 | } |
| 2624 | |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2625 | static int tpacket_parse_header(struct packet_sock *po, void *frame, |
| 2626 | int size_max, void **data) |
| 2627 | { |
| 2628 | union tpacket_uhdr ph; |
| 2629 | int tp_len, off; |
| 2630 | |
| 2631 | ph.raw = frame; |
| 2632 | |
| 2633 | switch (po->tp_version) { |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 2634 | case TPACKET_V3: |
| 2635 | if (ph.h3->tp_next_offset != 0) { |
| 2636 | pr_warn_once("variable sized slot not supported"); |
| 2637 | return -EINVAL; |
| 2638 | } |
| 2639 | tp_len = ph.h3->tp_len; |
| 2640 | break; |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2641 | case TPACKET_V2: |
| 2642 | tp_len = ph.h2->tp_len; |
| 2643 | break; |
| 2644 | default: |
| 2645 | tp_len = ph.h1->tp_len; |
| 2646 | break; |
| 2647 | } |
| 2648 | if (unlikely(tp_len > size_max)) { |
| 2649 | pr_err("packet size is too long (%d > %d)\n", tp_len, size_max); |
| 2650 | return -EMSGSIZE; |
| 2651 | } |
| 2652 | |
| 2653 | if (unlikely(po->tp_tx_has_off)) { |
| 2654 | int off_min, off_max; |
| 2655 | |
| 2656 | off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll); |
| 2657 | off_max = po->tx_ring.frame_size - tp_len; |
| 2658 | if (po->sk.sk_type == SOCK_DGRAM) { |
| 2659 | switch (po->tp_version) { |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 2660 | case TPACKET_V3: |
| 2661 | off = ph.h3->tp_net; |
| 2662 | break; |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2663 | case TPACKET_V2: |
| 2664 | off = ph.h2->tp_net; |
| 2665 | break; |
| 2666 | default: |
| 2667 | off = ph.h1->tp_net; |
| 2668 | break; |
| 2669 | } |
| 2670 | } else { |
| 2671 | switch (po->tp_version) { |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 2672 | case TPACKET_V3: |
| 2673 | off = ph.h3->tp_mac; |
| 2674 | break; |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2675 | case TPACKET_V2: |
| 2676 | off = ph.h2->tp_mac; |
| 2677 | break; |
| 2678 | default: |
| 2679 | off = ph.h1->tp_mac; |
| 2680 | break; |
| 2681 | } |
| 2682 | } |
| 2683 | if (unlikely((off < off_min) || (off_max < off))) |
| 2684 | return -EINVAL; |
| 2685 | } else { |
| 2686 | off = po->tp_hdrlen - sizeof(struct sockaddr_ll); |
| 2687 | } |
| 2688 | |
| 2689 | *data = frame + off; |
| 2690 | return tp_len; |
| 2691 | } |
| 2692 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2693 | static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) |
| 2694 | { |
Neil Horman | 89ed5b5 | 2019-06-25 17:57:49 -0400 | [diff] [blame] | 2695 | struct sk_buff *skb = NULL; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2696 | struct net_device *dev; |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2697 | struct virtio_net_hdr *vnet_hdr = NULL; |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 2698 | struct sockcm_cookie sockc; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2699 | __be16 proto; |
David S. Miller | 09effa6 | 2013-08-07 17:11:00 -0700 | [diff] [blame] | 2700 | int err, reserve = 0; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2701 | void *ph; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 2702 | DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name); |
Daniel Borkmann | 87a2fd2 | 2014-01-15 16:25:35 +0100 | [diff] [blame] | 2703 | bool need_wait = !(msg->msg_flags & MSG_DONTWAIT); |
Willem de Bruijn | 486efdc | 2019-04-29 11:53:18 -0400 | [diff] [blame] | 2704 | unsigned char *addr = NULL; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2705 | int tp_len, size_max; |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2706 | void *data; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2707 | int len_sum = 0; |
danborkmann@iogearbox.net | 9e67030 | 2012-08-20 03:34:03 +0000 | [diff] [blame] | 2708 | int status = TP_STATUS_AVAILABLE; |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2709 | int hlen, tlen, copylen = 0; |
Neil Horman | 89ed5b5 | 2019-06-25 17:57:49 -0400 | [diff] [blame] | 2710 | long timeo = 0; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2711 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2712 | mutex_lock(&po->pg_vec_lock); |
| 2713 | |
Eric Dumazet | 32d3182 | 2019-08-14 02:11:57 -0700 | [diff] [blame] | 2714 | /* packet_sendmsg() check on tx_ring.pg_vec was lockless, |
| 2715 | * we need to confirm it under protection of pg_vec_lock. |
| 2716 | */ |
| 2717 | if (unlikely(!po->tx_ring.pg_vec)) { |
| 2718 | err = -EBUSY; |
| 2719 | goto out; |
| 2720 | } |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 2721 | if (likely(saddr == NULL)) { |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 2722 | dev = packet_cached_dev_get(po); |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 2723 | proto = READ_ONCE(po->num); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2724 | } else { |
| 2725 | err = -EINVAL; |
| 2726 | if (msg->msg_namelen < sizeof(struct sockaddr_ll)) |
| 2727 | goto out; |
| 2728 | if (msg->msg_namelen < (saddr->sll_halen |
| 2729 | + offsetof(struct sockaddr_ll, |
| 2730 | sll_addr))) |
| 2731 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2732 | proto = saddr->sll_protocol; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 2733 | dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex); |
Willem de Bruijn | 486efdc | 2019-04-29 11:53:18 -0400 | [diff] [blame] | 2734 | if (po->sk.sk_socket->type == SOCK_DGRAM) { |
| 2735 | if (dev && msg->msg_namelen < dev->addr_len + |
| 2736 | offsetof(struct sockaddr_ll, sll_addr)) |
| 2737 | goto out_put; |
| 2738 | addr = saddr->sll_addr; |
| 2739 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2742 | err = -ENXIO; |
| 2743 | if (unlikely(dev == NULL)) |
| 2744 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2745 | err = -ENETDOWN; |
| 2746 | if (unlikely(!(dev->flags & IFF_UP))) |
| 2747 | goto out_put; |
| 2748 | |
Willem de Bruijn | 657a066 | 2018-07-06 10:12:56 -0400 | [diff] [blame] | 2749 | sockcm_init(&sockc, &po->sk); |
Douglas Caetano dos Santos | d19b183 | 2017-05-12 15:19:15 -0300 | [diff] [blame] | 2750 | if (msg->msg_controllen) { |
| 2751 | err = sock_cmsg_send(&po->sk, msg, &sockc); |
| 2752 | if (unlikely(err)) |
| 2753 | goto out_put; |
| 2754 | } |
| 2755 | |
Daniel Borkmann | 5cfb4c8 | 2015-11-11 23:25:44 +0100 | [diff] [blame] | 2756 | if (po->sk.sk_socket->type == SOCK_RAW) |
| 2757 | reserve = dev->hard_header_len; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2758 | size_max = po->tx_ring.frame_size |
Gabor Gombas | b5dd884 | 2009-10-29 03:19:11 -0700 | [diff] [blame] | 2759 | - (po->tp_hdrlen - sizeof(struct sockaddr_ll)); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2760 | |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2761 | if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr) |
Daniel Borkmann | 5cfb4c8 | 2015-11-11 23:25:44 +0100 | [diff] [blame] | 2762 | size_max = dev->mtu + reserve + VLAN_HLEN; |
David S. Miller | 09effa6 | 2013-08-07 17:11:00 -0700 | [diff] [blame] | 2763 | |
Neil Horman | 89ed5b5 | 2019-06-25 17:57:49 -0400 | [diff] [blame] | 2764 | reinit_completion(&po->skb_completion); |
| 2765 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2766 | do { |
| 2767 | ph = packet_current_frame(po, &po->tx_ring, |
Daniel Borkmann | 87a2fd2 | 2014-01-15 16:25:35 +0100 | [diff] [blame] | 2768 | TP_STATUS_SEND_REQUEST); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2769 | if (unlikely(ph == NULL)) { |
Neil Horman | 89ed5b5 | 2019-06-25 17:57:49 -0400 | [diff] [blame] | 2770 | if (need_wait && skb) { |
| 2771 | timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT); |
| 2772 | timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo); |
| 2773 | if (timeo <= 0) { |
| 2774 | err = !timeo ? -ETIMEDOUT : -ERESTARTSYS; |
| 2775 | goto out_put; |
| 2776 | } |
| 2777 | } |
| 2778 | /* check for additional frames */ |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2779 | continue; |
| 2780 | } |
| 2781 | |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2782 | skb = NULL; |
| 2783 | tp_len = tpacket_parse_header(po, ph, size_max, &data); |
| 2784 | if (tp_len < 0) |
| 2785 | goto tpacket_error; |
| 2786 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2787 | status = TP_STATUS_SEND_REQUEST; |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 2788 | hlen = LL_RESERVED_SPACE(dev); |
| 2789 | tlen = dev->needed_tailroom; |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2790 | if (po->has_vnet_hdr) { |
| 2791 | vnet_hdr = data; |
| 2792 | data += sizeof(*vnet_hdr); |
| 2793 | tp_len -= sizeof(*vnet_hdr); |
| 2794 | if (tp_len < 0 || |
| 2795 | __packet_snd_vnet_parse(vnet_hdr, tp_len)) { |
| 2796 | tp_len = -EINVAL; |
| 2797 | goto tpacket_error; |
| 2798 | } |
| 2799 | copylen = __virtio16_to_cpu(vio_le(), |
| 2800 | vnet_hdr->hdr_len); |
| 2801 | } |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 2802 | copylen = max_t(int, copylen, dev->hard_header_len); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2803 | skb = sock_alloc_send_skb(&po->sk, |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2804 | hlen + tlen + sizeof(struct sockaddr_ll) + |
| 2805 | (copylen - dev->hard_header_len), |
Kretschmer, Mathias | fbf33a2 | 2015-05-08 15:44:37 +0200 | [diff] [blame] | 2806 | !need_wait, &err); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2807 | |
Kretschmer, Mathias | fbf33a2 | 2015-05-08 15:44:37 +0200 | [diff] [blame] | 2808 | if (unlikely(skb == NULL)) { |
| 2809 | /* we assume the socket was initially writeable ... */ |
| 2810 | if (likely(len_sum > 0)) |
| 2811 | err = len_sum; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2812 | goto out_status; |
Kretschmer, Mathias | fbf33a2 | 2015-05-08 15:44:37 +0200 | [diff] [blame] | 2813 | } |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2814 | tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto, |
Soheil Hassas Yeganeh | c14ac94 | 2016-04-02 23:08:12 -0400 | [diff] [blame] | 2815 | addr, hlen, copylen, &sockc); |
Alexander Drozdov | dbd46ab | 2015-07-28 13:57:01 +0300 | [diff] [blame] | 2816 | if (likely(tp_len >= 0) && |
Daniel Borkmann | 5cfb4c8 | 2015-11-11 23:25:44 +0100 | [diff] [blame] | 2817 | tp_len > dev->mtu + reserve && |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2818 | !po->has_vnet_hdr && |
Daniel Borkmann | 3c70c13 | 2015-11-11 23:25:42 +0100 | [diff] [blame] | 2819 | !packet_extra_vlan_len_allowed(dev, skb)) |
| 2820 | tp_len = -EMSGSIZE; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2821 | |
| 2822 | if (unlikely(tp_len < 0)) { |
Willem de Bruijn | 8d39b4a | 2016-02-03 18:02:16 -0500 | [diff] [blame] | 2823 | tpacket_error: |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2824 | if (po->tp_loss) { |
| 2825 | __packet_set_status(po, ph, |
| 2826 | TP_STATUS_AVAILABLE); |
| 2827 | packet_increment_head(&po->tx_ring); |
| 2828 | kfree_skb(skb); |
| 2829 | continue; |
| 2830 | } else { |
| 2831 | status = TP_STATUS_WRONG_FORMAT; |
| 2832 | err = tp_len; |
| 2833 | goto out_status; |
| 2834 | } |
| 2835 | } |
| 2836 | |
Jianfeng Tan | 9d2f67e | 2018-09-29 15:41:27 +0000 | [diff] [blame] | 2837 | if (po->has_vnet_hdr) { |
| 2838 | if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) { |
| 2839 | tp_len = -EINVAL; |
| 2840 | goto tpacket_error; |
| 2841 | } |
| 2842 | virtio_net_hdr_set_proto(skb, vnet_hdr); |
Willem de Bruijn | 1d036d2 | 2016-02-03 18:02:17 -0500 | [diff] [blame] | 2843 | } |
| 2844 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2845 | skb->destructor = tpacket_destruct_skb; |
| 2846 | __packet_set_status(po, ph, TP_STATUS_SENDING); |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 2847 | packet_inc_pending(&po->tx_ring); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2848 | |
| 2849 | status = TP_STATUS_SEND_REQUEST; |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 2850 | err = po->xmit(skb); |
Jarek Poplawski | eb70df1 | 2010-01-10 22:04:19 +0000 | [diff] [blame] | 2851 | if (unlikely(err > 0)) { |
| 2852 | err = net_xmit_errno(err); |
| 2853 | if (err && __packet_get_status(po, ph) == |
| 2854 | TP_STATUS_AVAILABLE) { |
| 2855 | /* skb was destructed already */ |
| 2856 | skb = NULL; |
| 2857 | goto out_status; |
| 2858 | } |
| 2859 | /* |
| 2860 | * skb was dropped but not destructed yet; |
| 2861 | * let's treat it like congestion or err < 0 |
| 2862 | */ |
| 2863 | err = 0; |
| 2864 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2865 | packet_increment_head(&po->tx_ring); |
| 2866 | len_sum += tp_len; |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 2867 | } while (likely((ph != NULL) || |
| 2868 | /* Note: packet_read_pending() might be slow if we have |
| 2869 | * to call it as it's per_cpu variable, but in fast-path |
| 2870 | * we already short-circuit the loop with the first |
| 2871 | * condition, and luckily don't have to go that path |
| 2872 | * anyway. |
| 2873 | */ |
| 2874 | (need_wait && packet_read_pending(&po->tx_ring)))); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2875 | |
| 2876 | err = len_sum; |
| 2877 | goto out_put; |
| 2878 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2879 | out_status: |
| 2880 | __packet_set_status(po, ph, status); |
| 2881 | kfree_skb(skb); |
| 2882 | out_put: |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 2883 | dev_put(dev); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2884 | out: |
| 2885 | mutex_unlock(&po->pg_vec_lock); |
| 2886 | return err; |
| 2887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | |
Olof Johansson | eea49cc9 | 2011-11-02 11:00:49 +0000 | [diff] [blame] | 2889 | static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad, |
| 2890 | size_t reserve, size_t len, |
| 2891 | size_t linear, int noblock, |
| 2892 | int *err) |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2893 | { |
| 2894 | struct sk_buff *skb; |
| 2895 | |
| 2896 | /* Under a page? Don't bother with paged skb. */ |
| 2897 | if (prepad + len < PAGE_SIZE || !linear) |
| 2898 | linear = len; |
| 2899 | |
| 2900 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
Eric Dumazet | 28d6427 | 2013-08-08 14:38:47 -0700 | [diff] [blame] | 2901 | err, 0); |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2902 | if (!skb) |
| 2903 | return NULL; |
| 2904 | |
| 2905 | skb_reserve(skb, reserve); |
| 2906 | skb_put(skb, linear); |
| 2907 | skb->data_len = len - linear; |
| 2908 | skb->len += len - linear; |
| 2909 | |
| 2910 | return skb; |
| 2911 | } |
| 2912 | |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 2913 | static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | { |
| 2915 | struct sock *sk = sock->sk; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 2916 | DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | struct sk_buff *skb; |
| 2918 | struct net_device *dev; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 2919 | __be16 proto; |
Willem de Bruijn | 486efdc | 2019-04-29 11:53:18 -0400 | [diff] [blame] | 2920 | unsigned char *addr = NULL; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 2921 | int err, reserve = 0; |
Edward Jee | c7d39e3 | 2015-10-08 14:56:49 -0700 | [diff] [blame] | 2922 | struct sockcm_cookie sockc; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2923 | struct virtio_net_hdr vnet_hdr = { 0 }; |
| 2924 | int offset = 0; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2925 | struct packet_sock *po = pkt_sk(sk); |
Willem de Bruijn | da7c956 | 2017-09-26 12:20:17 -0400 | [diff] [blame] | 2926 | bool has_vnet_hdr = false; |
Willem de Bruijn | 57031eb | 2017-02-07 15:57:21 -0500 | [diff] [blame] | 2927 | int hlen, tlen, linear; |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 2928 | int extra_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | |
| 2930 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2931 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2933 | |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 2934 | if (likely(saddr == NULL)) { |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 2935 | dev = packet_cached_dev_get(po); |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 2936 | proto = READ_ONCE(po->num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | } else { |
| 2938 | err = -EINVAL; |
| 2939 | if (msg->msg_namelen < sizeof(struct sockaddr_ll)) |
| 2940 | goto out; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 2941 | if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) |
| 2942 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | proto = saddr->sll_protocol; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 2944 | dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex); |
Willem de Bruijn | 486efdc | 2019-04-29 11:53:18 -0400 | [diff] [blame] | 2945 | if (sock->type == SOCK_DGRAM) { |
| 2946 | if (dev && msg->msg_namelen < dev->addr_len + |
| 2947 | offsetof(struct sockaddr_ll, sll_addr)) |
| 2948 | goto out_unlock; |
| 2949 | addr = saddr->sll_addr; |
| 2950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | err = -ENXIO; |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 2954 | if (unlikely(dev == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | goto out_unlock; |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 2956 | err = -ENETDOWN; |
| 2957 | if (unlikely(!(dev->flags & IFF_UP))) |
| 2958 | goto out_unlock; |
| 2959 | |
Willem de Bruijn | 657a066 | 2018-07-06 10:12:56 -0400 | [diff] [blame] | 2960 | sockcm_init(&sockc, sk); |
Edward Jee | c7d39e3 | 2015-10-08 14:56:49 -0700 | [diff] [blame] | 2961 | sockc.mark = sk->sk_mark; |
| 2962 | if (msg->msg_controllen) { |
| 2963 | err = sock_cmsg_send(sk, msg, &sockc); |
| 2964 | if (unlikely(err)) |
| 2965 | goto out_unlock; |
| 2966 | } |
| 2967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | if (sock->type == SOCK_RAW) |
| 2969 | reserve = dev->hard_header_len; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2970 | if (po->has_vnet_hdr) { |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2971 | err = packet_snd_vnet_parse(msg, &len, &vnet_hdr); |
| 2972 | if (err) |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2973 | goto out_unlock; |
Willem de Bruijn | da7c956 | 2017-09-26 12:20:17 -0400 | [diff] [blame] | 2974 | has_vnet_hdr = true; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2975 | } |
| 2976 | |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 2977 | if (unlikely(sock_flag(sk, SOCK_NOFCS))) { |
| 2978 | if (!netif_supports_nofcs(dev)) { |
| 2979 | err = -EPROTONOSUPPORT; |
| 2980 | goto out_unlock; |
| 2981 | } |
| 2982 | extra_len = 4; /* We're doing our own CRC */ |
| 2983 | } |
| 2984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | err = -EMSGSIZE; |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 2986 | if (!vnet_hdr.gso_type && |
| 2987 | (len > dev->mtu + reserve + VLAN_HLEN + extra_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | goto out_unlock; |
| 2989 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2990 | err = -ENOBUFS; |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 2991 | hlen = LL_RESERVED_SPACE(dev); |
| 2992 | tlen = dev->needed_tailroom; |
Willem de Bruijn | 57031eb | 2017-02-07 15:57:21 -0500 | [diff] [blame] | 2993 | linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len); |
| 2994 | linear = max(linear, min_t(int, len, dev->hard_header_len)); |
| 2995 | skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear, |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2996 | msg->msg_flags & MSG_DONTWAIT, &err); |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2997 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | goto out_unlock; |
| 2999 | |
Willem de Bruijn | b84bbaf | 2018-05-11 13:24:25 -0400 | [diff] [blame] | 3000 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 3002 | err = -EINVAL; |
Willem de Bruijn | 9c70776 | 2014-11-19 13:10:16 -0500 | [diff] [blame] | 3003 | if (sock->type == SOCK_DGRAM) { |
| 3004 | offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len); |
Christoph Jaeger | 46d2cfb | 2015-01-11 13:01:16 -0500 | [diff] [blame] | 3005 | if (unlikely(offset < 0)) |
Willem de Bruijn | 9c70776 | 2014-11-19 13:10:16 -0500 | [diff] [blame] | 3006 | goto out_free; |
Willem de Bruijn | b84bbaf | 2018-05-11 13:24:25 -0400 | [diff] [blame] | 3007 | } else if (reserve) { |
Willem de Bruijn | 9aad13b | 2018-05-24 18:10:30 -0400 | [diff] [blame] | 3008 | skb_reserve(skb, -reserve); |
Nicolas Dichtel | 88a8121 | 2019-01-17 11:27:22 +0100 | [diff] [blame] | 3009 | if (len < reserve + sizeof(struct ipv6hdr) && |
| 3010 | dev->min_header_len != dev->hard_header_len) |
Willem de Bruijn | 993675a | 2018-07-11 12:00:45 -0400 | [diff] [blame] | 3011 | skb_reset_network_header(skb); |
Willem de Bruijn | 9c70776 | 2014-11-19 13:10:16 -0500 | [diff] [blame] | 3012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | |
| 3014 | /* Returns -EFAULT on error */ |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 3015 | err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | if (err) |
| 3017 | goto out_free; |
Daniel Borkmann | bf84a010 | 2013-04-14 08:08:13 +0000 | [diff] [blame] | 3018 | |
Willem de Bruijn | 9ed988c | 2016-03-09 21:58:34 -0500 | [diff] [blame] | 3019 | if (sock->type == SOCK_RAW && |
| 3020 | !dev_validate_header(dev, skb->data, len)) { |
| 3021 | err = -EINVAL; |
| 3022 | goto out_free; |
| 3023 | } |
| 3024 | |
Willem de Bruijn | 8f932f7 | 2018-12-17 12:24:00 -0500 | [diff] [blame] | 3025 | skb_setup_tx_timestamp(skb, sockc.tsflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 3027 | if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) && |
Daniel Borkmann | 3c70c13 | 2015-11-11 23:25:42 +0100 | [diff] [blame] | 3028 | !packet_extra_vlan_len_allowed(dev, skb)) { |
| 3029 | err = -EMSGSIZE; |
| 3030 | goto out_free; |
Ben Greear | 57f89bf | 2011-02-11 09:35:18 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
David S. Miller | 09effa6 | 2013-08-07 17:11:00 -0700 | [diff] [blame] | 3033 | skb->protocol = proto; |
| 3034 | skb->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | skb->priority = sk->sk_priority; |
Edward Jee | c7d39e3 | 2015-10-08 14:56:49 -0700 | [diff] [blame] | 3036 | skb->mark = sockc.mark; |
Richard Cochran | 3d0ba8c | 2018-07-03 15:42:51 -0700 | [diff] [blame] | 3037 | skb->tstamp = sockc.transmit_time; |
Daniel Borkmann | 0fd5d57 | 2014-02-16 15:55:22 +0100 | [diff] [blame] | 3038 | |
Willem de Bruijn | da7c956 | 2017-09-26 12:20:17 -0400 | [diff] [blame] | 3039 | if (has_vnet_hdr) { |
Jarno Rajahalme | db60eb5f | 2016-11-18 15:40:41 -0800 | [diff] [blame] | 3040 | err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le()); |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 3041 | if (err) |
| 3042 | goto out_free; |
| 3043 | len += sizeof(vnet_hdr); |
Jianfeng Tan | 9d2f67e | 2018-09-29 15:41:27 +0000 | [diff] [blame] | 3044 | virtio_net_hdr_set_proto(skb, &vnet_hdr); |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3045 | } |
| 3046 | |
Maxim Mikityanskiy | 75c6577 | 2019-02-21 12:40:01 +0000 | [diff] [blame] | 3047 | packet_parse_headers(skb, sock); |
Daniel Borkmann | 8fd6c80 | 2015-11-11 23:25:41 +0100 | [diff] [blame] | 3048 | |
Ben Greear | 3bdc0eb | 2012-02-11 15:39:30 +0000 | [diff] [blame] | 3049 | if (unlikely(extra_len == 4)) |
| 3050 | skb->no_fcs = 1; |
| 3051 | |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 3052 | err = po->xmit(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | if (err > 0 && (err = net_xmit_errno(err)) != 0) |
| 3054 | goto out_unlock; |
| 3055 | |
Daniel Borkmann | e40526c | 2013-11-21 16:50:58 +0100 | [diff] [blame] | 3056 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3058 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | |
| 3060 | out_free: |
| 3061 | kfree_skb(skb); |
| 3062 | out_unlock: |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 3063 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | out: |
| 3065 | return err; |
| 3066 | } |
| 3067 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 3068 | static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3069 | { |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3070 | struct sock *sk = sock->sk; |
| 3071 | struct packet_sock *po = pkt_sk(sk); |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 3072 | |
Eric Dumazet | d1b5bee | 2021-06-10 09:00:12 -0700 | [diff] [blame] | 3073 | /* Reading tx_ring.pg_vec without holding pg_vec_lock is racy. |
| 3074 | * tpacket_snd() will redo the check safely. |
| 3075 | */ |
| 3076 | if (data_race(po->tx_ring.pg_vec)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3077 | return tpacket_snd(po, msg); |
Eric Dumazet | d1b5bee | 2021-06-10 09:00:12 -0700 | [diff] [blame] | 3078 | |
| 3079 | return packet_snd(sock, msg, len); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | /* |
| 3083 | * Close a PACKET socket. This is fairly simple. We immediately go |
| 3084 | * to 'closed' state and remove our protocol entry in the device list. |
| 3085 | */ |
| 3086 | |
| 3087 | static int packet_release(struct socket *sock) |
| 3088 | { |
| 3089 | struct sock *sk = sock->sk; |
| 3090 | struct packet_sock *po; |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 3091 | struct packet_fanout *f; |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 3092 | struct net *net; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 3093 | union tpacket_req_u req_u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | |
| 3095 | if (!sk) |
| 3096 | return 0; |
| 3097 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3098 | net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | po = pkt_sk(sk); |
| 3100 | |
Pavel Emelyanov | 0fa7fa9 | 2012-08-21 01:06:47 +0000 | [diff] [blame] | 3101 | mutex_lock(&net->packet.sklist_lock); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 3102 | sk_del_node_init_rcu(sk); |
Pavel Emelyanov | 0fa7fa9 | 2012-08-21 01:06:47 +0000 | [diff] [blame] | 3103 | mutex_unlock(&net->packet.sklist_lock); |
| 3104 | |
Eric Dumazet | 920de80 | 2008-11-24 00:09:29 -0800 | [diff] [blame] | 3105 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 3107 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 3108 | unregister_prot_hook(sk, false); |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 3109 | packet_cached_dev_reset(po); |
| 3110 | |
Ben Greear | 160ff18 | 2011-06-01 07:18:52 +0000 | [diff] [blame] | 3111 | if (po->prot_hook.dev) { |
Eric Dumazet | f1d9268 | 2021-12-14 07:09:33 -0800 | [diff] [blame^] | 3112 | dev_put_track(po->prot_hook.dev, &po->prot_hook.dev_tracker); |
Ben Greear | 160ff18 | 2011-06-01 07:18:52 +0000 | [diff] [blame] | 3113 | po->prot_hook.dev = NULL; |
| 3114 | } |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 3115 | spin_unlock(&po->bind_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | packet_flush_mclist(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | |
Eric Dumazet | 5171b37 | 2018-04-15 17:52:04 -0700 | [diff] [blame] | 3119 | lock_sock(sk); |
Phil Sutter | 9665d5d | 2013-02-01 07:21:41 +0000 | [diff] [blame] | 3120 | if (po->rx_ring.pg_vec) { |
| 3121 | memset(&req_u, 0, sizeof(req_u)); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 3122 | packet_set_ring(sk, &req_u, 1, 0); |
Phil Sutter | 9665d5d | 2013-02-01 07:21:41 +0000 | [diff] [blame] | 3123 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3124 | |
Phil Sutter | 9665d5d | 2013-02-01 07:21:41 +0000 | [diff] [blame] | 3125 | if (po->tx_ring.pg_vec) { |
| 3126 | memset(&req_u, 0, sizeof(req_u)); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 3127 | packet_set_ring(sk, &req_u, 1, 1); |
Phil Sutter | 9665d5d | 2013-02-01 07:21:41 +0000 | [diff] [blame] | 3128 | } |
Eric Dumazet | 5171b37 | 2018-04-15 17:52:04 -0700 | [diff] [blame] | 3129 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 3131 | f = fanout_release(sk); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3132 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 3133 | synchronize_net(); |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 3134 | |
Willem de Bruijn | afa0925 | 2019-05-31 12:37:23 -0400 | [diff] [blame] | 3135 | kfree(po->rollover); |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 3136 | if (f) { |
| 3137 | fanout_release_data(f); |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 3138 | kvfree(f); |
Anoob Soman | 2bd624b | 2017-02-15 20:25:39 +0000 | [diff] [blame] | 3139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | /* |
| 3141 | * Now the socket is dead. No more input will appear. |
| 3142 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | sock_orphan(sk); |
| 3144 | sock->sk = NULL; |
| 3145 | |
| 3146 | /* Purge queues */ |
| 3147 | |
| 3148 | skb_queue_purge(&sk->sk_receive_queue); |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 3149 | packet_free_pending(po); |
Pavel Emelyanov | 17ab56a | 2007-11-10 21:38:48 -0800 | [diff] [blame] | 3150 | sk_refcnt_debug_release(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | |
| 3152 | sock_put(sk); |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Attach a packet hook. |
| 3158 | */ |
| 3159 | |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3160 | static int packet_do_bind(struct sock *sk, const char *name, int ifindex, |
| 3161 | __be16 proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | { |
| 3163 | struct packet_sock *po = pkt_sk(sk); |
Lars Westerhoff | 158cd4a | 2015-07-28 01:32:21 +0300 | [diff] [blame] | 3164 | struct net_device *dev_curr; |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3165 | __be16 proto_curr; |
| 3166 | bool need_rehook; |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3167 | struct net_device *dev = NULL; |
| 3168 | int ret = 0; |
| 3169 | bool unlisted = false; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | spin_lock(&po->bind_lock); |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3173 | rcu_read_lock(); |
| 3174 | |
Willem de Bruijn | 4971613 | 2017-09-26 12:19:37 -0400 | [diff] [blame] | 3175 | if (po->fanout) { |
| 3176 | ret = -EINVAL; |
| 3177 | goto out_unlock; |
| 3178 | } |
| 3179 | |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3180 | if (name) { |
| 3181 | dev = dev_get_by_name_rcu(sock_net(sk), name); |
| 3182 | if (!dev) { |
| 3183 | ret = -ENODEV; |
| 3184 | goto out_unlock; |
| 3185 | } |
| 3186 | } else if (ifindex) { |
| 3187 | dev = dev_get_by_index_rcu(sock_net(sk), ifindex); |
| 3188 | if (!dev) { |
| 3189 | ret = -ENODEV; |
| 3190 | goto out_unlock; |
| 3191 | } |
| 3192 | } |
| 3193 | |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 3194 | dev_hold(dev); |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 3195 | |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3196 | proto_curr = po->prot_hook.type; |
| 3197 | dev_curr = po->prot_hook.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3199 | need_rehook = proto_curr != proto || dev_curr != dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3201 | if (need_rehook) { |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3202 | if (po->running) { |
| 3203 | rcu_read_unlock(); |
Eric Dumazet | 15fe076 | 2017-11-28 08:03:30 -0800 | [diff] [blame] | 3204 | /* prevents packet_notifier() from calling |
| 3205 | * register_prot_hook() |
| 3206 | */ |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 3207 | WRITE_ONCE(po->num, 0); |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3208 | __unregister_prot_hook(sk, true); |
| 3209 | rcu_read_lock(); |
| 3210 | dev_curr = po->prot_hook.dev; |
| 3211 | if (dev) |
| 3212 | unlisted = !dev_get_by_index_rcu(sock_net(sk), |
| 3213 | dev->ifindex); |
| 3214 | } |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 3215 | |
Eric Dumazet | 15fe076 | 2017-11-28 08:03:30 -0800 | [diff] [blame] | 3216 | BUG_ON(po->running); |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 3217 | WRITE_ONCE(po->num, proto); |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3218 | po->prot_hook.type = proto; |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3219 | |
Eric Dumazet | f1d9268 | 2021-12-14 07:09:33 -0800 | [diff] [blame^] | 3220 | dev_put_track(dev_curr, &po->prot_hook.dev_tracker); |
| 3221 | dev_curr = NULL; |
| 3222 | |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3223 | if (unlikely(unlisted)) { |
| 3224 | dev_put(dev); |
| 3225 | po->prot_hook.dev = NULL; |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3226 | WRITE_ONCE(po->ifindex, -1); |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3227 | packet_cached_dev_reset(po); |
| 3228 | } else { |
Eric Dumazet | f1d9268 | 2021-12-14 07:09:33 -0800 | [diff] [blame^] | 3229 | if (dev) |
| 3230 | netdev_tracker_alloc(dev, |
| 3231 | &po->prot_hook.dev_tracker, |
| 3232 | GFP_ATOMIC); |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3233 | po->prot_hook.dev = dev; |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3234 | WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0); |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3235 | packet_cached_dev_assign(po, dev); |
| 3236 | } |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3237 | } |
Eric Dumazet | f1d9268 | 2021-12-14 07:09:33 -0800 | [diff] [blame^] | 3238 | dev_put_track(dev_curr, &po->prot_hook.dev_tracker); |
Daniel Borkmann | 902fefb | 2014-01-15 16:25:34 +0100 | [diff] [blame] | 3239 | |
| 3240 | if (proto == 0 || !need_rehook) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | goto out_unlock; |
| 3242 | |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3243 | if (!unlisted && (!dev || (dev->flags & IFF_UP))) { |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 3244 | register_prot_hook(sk); |
Urs Thuermann | be85d4a | 2007-11-12 21:05:20 -0800 | [diff] [blame] | 3245 | } else { |
| 3246 | sk->sk_err = ENETDOWN; |
| 3247 | if (!sock_flag(sk, SOCK_DEAD)) |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 3248 | sk_error_report(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | out_unlock: |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3252 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | spin_unlock(&po->bind_lock); |
| 3254 | release_sock(sk); |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3255 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | /* |
| 3259 | * Bind a packet socket to a device |
| 3260 | */ |
| 3261 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3262 | static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, |
| 3263 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3265 | struct sock *sk = sock->sk; |
Alexander Potapenko | 540e289 | 2017-03-01 12:57:20 +0100 | [diff] [blame] | 3266 | char name[sizeof(uaddr->sa_data) + 1]; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 3267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | /* |
| 3269 | * Check legality |
| 3270 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 3271 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 3272 | if (addr_len != sizeof(struct sockaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | return -EINVAL; |
Alexander Potapenko | 540e289 | 2017-03-01 12:57:20 +0100 | [diff] [blame] | 3274 | /* uaddr->sa_data comes from the userspace, it's not guaranteed to be |
| 3275 | * zero-terminated. |
| 3276 | */ |
| 3277 | memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data)); |
| 3278 | name[sizeof(uaddr->sa_data)] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3280 | return packet_do_bind(sk, name, 0, pkt_sk(sk)->num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | |
| 3283 | static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 3284 | { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3285 | struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr; |
| 3286 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
| 3288 | /* |
| 3289 | * Check legality |
| 3290 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 3291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | if (addr_len < sizeof(struct sockaddr_ll)) |
| 3293 | return -EINVAL; |
| 3294 | if (sll->sll_family != AF_PACKET) |
| 3295 | return -EINVAL; |
| 3296 | |
Francesco Ruggeri | 30f7ea1 | 2015-11-05 08:16:14 -0800 | [diff] [blame] | 3297 | return packet_do_bind(sk, NULL, sll->sll_ifindex, |
| 3298 | sll->sll_protocol ? : pkt_sk(sk)->num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | static struct proto packet_proto = { |
| 3302 | .name = "PACKET", |
| 3303 | .owner = THIS_MODULE, |
| 3304 | .obj_size = sizeof(struct packet_sock), |
| 3305 | }; |
| 3306 | |
| 3307 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 3308 | * Create a packet of type SOCK_PACKET. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | */ |
| 3310 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 3311 | static int packet_create(struct net *net, struct socket *sock, int protocol, |
| 3312 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | { |
| 3314 | struct sock *sk; |
| 3315 | struct packet_sock *po; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 3316 | __be16 proto = (__force __be16)protocol; /* weird, but documented */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | int err; |
| 3318 | |
Eric W. Biederman | df008c9 | 2012-11-16 03:03:07 +0000 | [diff] [blame] | 3319 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | return -EPERM; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 3321 | if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW && |
| 3322 | sock->type != SOCK_PACKET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | return -ESOCKTNOSUPPORT; |
| 3324 | |
| 3325 | sock->state = SS_UNCONNECTED; |
| 3326 | |
| 3327 | err = -ENOBUFS; |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 3328 | sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | if (sk == NULL) |
| 3330 | goto out; |
| 3331 | |
| 3332 | sock->ops = &packet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | if (sock->type == SOCK_PACKET) |
| 3334 | sock->ops = &packet_ops_spkt; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 3335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | sock_init_data(sock, sk); |
| 3337 | |
| 3338 | po = pkt_sk(sk); |
Neil Horman | 89ed5b5 | 2019-06-25 17:57:49 -0400 | [diff] [blame] | 3339 | init_completion(&po->skb_completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | sk->sk_family = PF_PACKET; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 3341 | po->num = proto; |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 3342 | po->xmit = dev_queue_xmit; |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 3343 | |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 3344 | err = packet_alloc_pending(po); |
| 3345 | if (err) |
| 3346 | goto out2; |
| 3347 | |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 3348 | packet_cached_dev_reset(po); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | |
| 3350 | sk->sk_destruct = packet_sock_destruct; |
Pavel Emelyanov | 17ab56a | 2007-11-10 21:38:48 -0800 | [diff] [blame] | 3351 | sk_refcnt_debug_inc(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | |
| 3353 | /* |
| 3354 | * Attach a protocol block |
| 3355 | */ |
| 3356 | |
| 3357 | spin_lock_init(&po->bind_lock); |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 3358 | mutex_init(&po->pg_vec_lock); |
Willem de Bruijn | 0648ab7 | 2015-05-12 11:56:46 -0400 | [diff] [blame] | 3359 | po->rollover = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | po->prot_hook.func = packet_rcv; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 3361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | if (sock->type == SOCK_PACKET) |
| 3363 | po->prot_hook.func = packet_rcv_spkt; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 3364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | po->prot_hook.af_packet_priv = sk; |
| 3366 | |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 3367 | if (proto) { |
| 3368 | po->prot_hook.type = proto; |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3369 | __register_prot_hook(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | } |
| 3371 | |
Pavel Emelyanov | 0fa7fa9 | 2012-08-21 01:06:47 +0000 | [diff] [blame] | 3372 | mutex_lock(&net->packet.sklist_lock); |
Maxime Chevallier | a4dc6a4 | 2019-03-16 14:41:30 +0100 | [diff] [blame] | 3373 | sk_add_node_tail_rcu(sk, &net->packet.sklist); |
Pavel Emelyanov | 0fa7fa9 | 2012-08-21 01:06:47 +0000 | [diff] [blame] | 3374 | mutex_unlock(&net->packet.sklist_lock); |
| 3375 | |
Eric Dumazet | 3680453 | 2008-11-19 14:25:35 -0800 | [diff] [blame] | 3376 | sock_prot_inuse_add(net, &packet_proto, 1); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 3377 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3378 | return 0; |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 3379 | out2: |
| 3380 | sk_free(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | out: |
| 3382 | return err; |
| 3383 | } |
| 3384 | |
| 3385 | /* |
| 3386 | * Pull a packet from our receive queue and hand it to the user. |
| 3387 | * If necessary we block. |
| 3388 | */ |
| 3389 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 3390 | static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 3391 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | { |
| 3393 | struct sock *sk = sock->sk; |
| 3394 | struct sk_buff *skb; |
| 3395 | int copied, err; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3396 | int vnet_hdr_len = 0; |
Eyal Birger | 2472d76 | 2015-03-01 14:58:28 +0200 | [diff] [blame] | 3397 | unsigned int origlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | |
| 3399 | err = -EINVAL; |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 3400 | if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3401 | goto out; |
| 3402 | |
| 3403 | #if 0 |
| 3404 | /* What error should we return now? EUNATTACH? */ |
| 3405 | if (pkt_sk(sk)->ifindex < 0) |
| 3406 | return -ENODEV; |
| 3407 | #endif |
| 3408 | |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 3409 | if (flags & MSG_ERRQUEUE) { |
Richard Cochran | cb820f8 | 2013-07-19 19:40:09 +0200 | [diff] [blame] | 3410 | err = sock_recv_errqueue(sk, msg, len, |
| 3411 | SOL_PACKET, PACKET_TX_TIMESTAMP); |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 3412 | goto out; |
| 3413 | } |
| 3414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | * Call the generic datagram receiver. This handles all sorts |
| 3417 | * of horrible races and re-entrancy so we can forget about it |
| 3418 | * in the protocol layers. |
| 3419 | * |
| 3420 | * Now it will return ENETDOWN, if device have just gone down, |
| 3421 | * but then it will block. |
| 3422 | */ |
| 3423 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3424 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | |
| 3426 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 3427 | * An error occurred so return it. Because skb_recv_datagram() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | * handles the blocking we don't see and worry about blocking |
| 3429 | * retries. |
| 3430 | */ |
| 3431 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 3432 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | goto out; |
| 3434 | |
Eric Dumazet | 9bb6cd6 | 2019-06-12 09:52:33 -0700 | [diff] [blame] | 3435 | packet_rcv_try_clear_pressure(pkt_sk(sk)); |
Willem de Bruijn | 2ccdbaa | 2015-05-12 11:56:48 -0400 | [diff] [blame] | 3436 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3437 | if (pkt_sk(sk)->has_vnet_hdr) { |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 3438 | err = packet_rcv_vnet(msg, skb, &len); |
| 3439 | if (err) |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3440 | goto out_free; |
Willem de Bruijn | 16cc140 | 2016-02-03 18:02:14 -0500 | [diff] [blame] | 3441 | vnet_hdr_len = sizeof(struct virtio_net_hdr); |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3442 | } |
| 3443 | |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3444 | /* You lose any data beyond the buffer you gave. If it worries |
| 3445 | * a user program they can ask the device for its MTU |
| 3446 | * anyway. |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 3447 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | copied = skb->len; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3449 | if (copied > len) { |
| 3450 | copied = len; |
| 3451 | msg->msg_flags |= MSG_TRUNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 3454 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3455 | if (err) |
| 3456 | goto out_free; |
| 3457 | |
Eyal Birger | 2472d76 | 2015-03-01 14:58:28 +0200 | [diff] [blame] | 3458 | if (sock->type != SOCK_PACKET) { |
| 3459 | struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll; |
| 3460 | |
| 3461 | /* Original length was stored in sockaddr_ll fields */ |
| 3462 | origlen = PACKET_SKB_CB(skb)->sa.origlen; |
| 3463 | sll->sll_family = AF_PACKET; |
| 3464 | sll->sll_protocol = skb->protocol; |
| 3465 | } |
| 3466 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 3467 | sock_recv_ts_and_drops(msg, sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3469 | if (msg->msg_name) { |
Willem de Bruijn | b2cf86e | 2019-04-29 11:46:55 -0400 | [diff] [blame] | 3470 | int copy_len; |
| 3471 | |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3472 | /* If the address length field is there to be filled |
| 3473 | * in, we fill it in now. |
| 3474 | */ |
| 3475 | if (sock->type == SOCK_PACKET) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 3476 | __sockaddr_check_size(sizeof(struct sockaddr_pkt)); |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3477 | msg->msg_namelen = sizeof(struct sockaddr_pkt); |
Willem de Bruijn | b2cf86e | 2019-04-29 11:46:55 -0400 | [diff] [blame] | 3478 | copy_len = msg->msg_namelen; |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3479 | } else { |
| 3480 | struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll; |
Eyal Birger | 2472d76 | 2015-03-01 14:58:28 +0200 | [diff] [blame] | 3481 | |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3482 | msg->msg_namelen = sll->sll_halen + |
| 3483 | offsetof(struct sockaddr_ll, sll_addr); |
Willem de Bruijn | b2cf86e | 2019-04-29 11:46:55 -0400 | [diff] [blame] | 3484 | copy_len = msg->msg_namelen; |
| 3485 | if (msg->msg_namelen < sizeof(struct sockaddr_ll)) { |
| 3486 | memset(msg->msg_name + |
| 3487 | offsetof(struct sockaddr_ll, sll_addr), |
| 3488 | 0, sizeof(sll->sll_addr)); |
| 3489 | msg->msg_namelen = sizeof(struct sockaddr_ll); |
| 3490 | } |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3491 | } |
Willem de Bruijn | b2cf86e | 2019-04-29 11:46:55 -0400 | [diff] [blame] | 3492 | memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len); |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 3493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3495 | if (pkt_sk(sk)->auxdata) { |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 3496 | struct tpacket_auxdata aux; |
| 3497 | |
| 3498 | aux.tp_status = TP_STATUS_USER; |
| 3499 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 3500 | aux.tp_status |= TP_STATUS_CSUMNOTREADY; |
Alexander Drozdov | 682f048 | 2015-03-23 09:11:13 +0300 | [diff] [blame] | 3501 | else if (skb->pkt_type != PACKET_OUTGOING && |
| 3502 | (skb->ip_summed == CHECKSUM_COMPLETE || |
| 3503 | skb_csum_unnecessary(skb))) |
| 3504 | aux.tp_status |= TP_STATUS_CSUM_VALID; |
| 3505 | |
Eyal Birger | 2472d76 | 2015-03-01 14:58:28 +0200 | [diff] [blame] | 3506 | aux.tp_len = origlen; |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 3507 | aux.tp_snaplen = skb->len; |
| 3508 | aux.tp_mac = 0; |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 3509 | aux.tp_net = skb_network_offset(skb); |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 3510 | if (skb_vlan_tag_present(skb)) { |
| 3511 | aux.tp_vlan_tci = skb_vlan_tag_get(skb); |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 3512 | aux.tp_vlan_tpid = ntohs(skb->vlan_proto); |
| 3513 | aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; |
Ben Greear | a3bcc23 | 2011-06-01 06:49:10 +0000 | [diff] [blame] | 3514 | } else { |
| 3515 | aux.tp_vlan_tci = 0; |
Atzm Watanabe | a0cdfcf | 2013-12-17 22:53:40 +0900 | [diff] [blame] | 3516 | aux.tp_vlan_tpid = 0; |
Ben Greear | a3bcc23 | 2011-06-01 06:49:10 +0000 | [diff] [blame] | 3517 | } |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 3518 | put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3519 | } |
| 3520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | /* |
| 3522 | * Free or return the buffer as appropriate. Again this |
| 3523 | * hides all the races and re-entrancy issues from us. |
| 3524 | */ |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3525 | err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | |
| 3527 | out_free: |
| 3528 | skb_free_datagram(sk, skb); |
| 3529 | out: |
| 3530 | return err; |
| 3531 | } |
| 3532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 3534 | int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | { |
| 3536 | struct net_device *dev; |
| 3537 | struct sock *sk = sock->sk; |
| 3538 | |
| 3539 | if (peer) |
| 3540 | return -EOPNOTSUPP; |
| 3541 | |
| 3542 | uaddr->sa_family = AF_PACKET; |
Daniel Borkmann | 2dc85bf | 2013-06-12 16:02:27 +0200 | [diff] [blame] | 3543 | memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data)); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 3544 | rcu_read_lock(); |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3545 | dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex)); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 3546 | if (dev) |
Daniel Borkmann | 2dc85bf | 2013-06-12 16:02:27 +0200 | [diff] [blame] | 3547 | strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data)); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 3548 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 3550 | return sizeof(*uaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | |
| 3553 | static int packet_getname(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 3554 | int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | { |
| 3556 | struct net_device *dev; |
| 3557 | struct sock *sk = sock->sk; |
| 3558 | struct packet_sock *po = pkt_sk(sk); |
Cyrill Gorcunov | 13cfa97 | 2009-11-08 05:51:19 +0000 | [diff] [blame] | 3559 | DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr); |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3560 | int ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | |
| 3562 | if (peer) |
| 3563 | return -EOPNOTSUPP; |
| 3564 | |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3565 | ifindex = READ_ONCE(po->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | sll->sll_family = AF_PACKET; |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3567 | sll->sll_ifindex = ifindex; |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 3568 | sll->sll_protocol = READ_ONCE(po->num); |
Vasiliy Kulikov | 6728664 | 2010-11-10 12:09:10 -0800 | [diff] [blame] | 3569 | sll->sll_pkttype = 0; |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 3570 | rcu_read_lock(); |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 3571 | dev = dev_get_by_index_rcu(sock_net(sk), ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | if (dev) { |
| 3573 | sll->sll_hatype = dev->type; |
| 3574 | sll->sll_halen = dev->addr_len; |
| 3575 | memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | } else { |
| 3577 | sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */ |
| 3578 | sll->sll_halen = 0; |
| 3579 | } |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 3580 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 3582 | return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 3585 | static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i, |
| 3586 | int what) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | { |
| 3588 | switch (i->type) { |
| 3589 | case PACKET_MR_MULTICAST: |
Jiri Pirko | 1162563 | 2010-03-02 20:40:01 +0000 | [diff] [blame] | 3590 | if (i->alen != dev->addr_len) |
| 3591 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | if (what > 0) |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 3593 | return dev_mc_add(dev, i->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | else |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 3595 | return dev_mc_del(dev, i->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | break; |
| 3597 | case PACKET_MR_PROMISC: |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 3598 | return dev_set_promiscuity(dev, what); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | case PACKET_MR_ALLMULTI: |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 3600 | return dev_set_allmulti(dev, what); |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 3601 | case PACKET_MR_UNICAST: |
Jiri Pirko | 1162563 | 2010-03-02 20:40:01 +0000 | [diff] [blame] | 3602 | if (i->alen != dev->addr_len) |
| 3603 | return -EINVAL; |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 3604 | if (what > 0) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 3605 | return dev_uc_add(dev, i->addr); |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 3606 | else |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 3607 | return dev_uc_del(dev, i->addr); |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 3608 | break; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3609 | default: |
| 3610 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | } |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 3612 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | } |
| 3614 | |
Francesco Ruggeri | 82f1709 | 2015-03-09 11:51:04 -0700 | [diff] [blame] | 3615 | static void packet_dev_mclist_delete(struct net_device *dev, |
| 3616 | struct packet_mclist **mlp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | { |
Francesco Ruggeri | 82f1709 | 2015-03-09 11:51:04 -0700 | [diff] [blame] | 3618 | struct packet_mclist *ml; |
| 3619 | |
| 3620 | while ((ml = *mlp) != NULL) { |
| 3621 | if (ml->ifindex == dev->ifindex) { |
| 3622 | packet_dev_mc(dev, ml, -1); |
| 3623 | *mlp = ml->next; |
| 3624 | kfree(ml); |
| 3625 | } else |
| 3626 | mlp = &ml->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | } |
| 3628 | } |
| 3629 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 3630 | static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | { |
| 3632 | struct packet_sock *po = pkt_sk(sk); |
| 3633 | struct packet_mclist *ml, *i; |
| 3634 | struct net_device *dev; |
| 3635 | int err; |
| 3636 | |
| 3637 | rtnl_lock(); |
| 3638 | |
| 3639 | err = -ENODEV; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 3640 | dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | if (!dev) |
| 3642 | goto done; |
| 3643 | |
| 3644 | err = -EINVAL; |
Jiri Pirko | 1162563 | 2010-03-02 20:40:01 +0000 | [diff] [blame] | 3645 | if (mreq->mr_alen > dev->addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | goto done; |
| 3647 | |
| 3648 | err = -ENOBUFS; |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 3649 | i = kmalloc(sizeof(*i), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | if (i == NULL) |
| 3651 | goto done; |
| 3652 | |
| 3653 | err = 0; |
| 3654 | for (ml = po->mclist; ml; ml = ml->next) { |
| 3655 | if (ml->ifindex == mreq->mr_ifindex && |
| 3656 | ml->type == mreq->mr_type && |
| 3657 | ml->alen == mreq->mr_alen && |
| 3658 | memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { |
| 3659 | ml->count++; |
| 3660 | /* Free the new element ... */ |
| 3661 | kfree(i); |
| 3662 | goto done; |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | i->type = mreq->mr_type; |
| 3667 | i->ifindex = mreq->mr_ifindex; |
| 3668 | i->alen = mreq->mr_alen; |
| 3669 | memcpy(i->addr, mreq->mr_address, i->alen); |
Mathias Krause | 309cf37 | 2016-04-10 12:52:28 +0200 | [diff] [blame] | 3670 | memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | i->count = 1; |
| 3672 | i->next = po->mclist; |
| 3673 | po->mclist = i; |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 3674 | err = packet_dev_mc(dev, i, 1); |
| 3675 | if (err) { |
| 3676 | po->mclist = i->next; |
| 3677 | kfree(i); |
| 3678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | |
| 3680 | done: |
| 3681 | rtnl_unlock(); |
| 3682 | return err; |
| 3683 | } |
| 3684 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 3685 | static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | { |
| 3687 | struct packet_mclist *ml, **mlp; |
| 3688 | |
| 3689 | rtnl_lock(); |
| 3690 | |
| 3691 | for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) { |
| 3692 | if (ml->ifindex == mreq->mr_ifindex && |
| 3693 | ml->type == mreq->mr_type && |
| 3694 | ml->alen == mreq->mr_alen && |
| 3695 | memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { |
| 3696 | if (--ml->count == 0) { |
| 3697 | struct net_device *dev; |
| 3698 | *mlp = ml->next; |
Eric Dumazet | ad959e7 | 2009-10-16 06:38:46 +0000 | [diff] [blame] | 3699 | dev = __dev_get_by_index(sock_net(sk), ml->ifindex); |
| 3700 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | packet_dev_mc(dev, ml, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | kfree(ml); |
| 3703 | } |
Francesco Ruggeri | 82f1709 | 2015-03-09 11:51:04 -0700 | [diff] [blame] | 3704 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | } |
| 3706 | } |
| 3707 | rtnl_unlock(); |
Francesco Ruggeri | 82f1709 | 2015-03-09 11:51:04 -0700 | [diff] [blame] | 3708 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | static void packet_flush_mclist(struct sock *sk) |
| 3712 | { |
| 3713 | struct packet_sock *po = pkt_sk(sk); |
| 3714 | struct packet_mclist *ml; |
| 3715 | |
| 3716 | if (!po->mclist) |
| 3717 | return; |
| 3718 | |
| 3719 | rtnl_lock(); |
| 3720 | while ((ml = po->mclist) != NULL) { |
| 3721 | struct net_device *dev; |
| 3722 | |
| 3723 | po->mclist = ml->next; |
Eric Dumazet | ad959e7 | 2009-10-16 06:38:46 +0000 | [diff] [blame] | 3724 | dev = __dev_get_by_index(sock_net(sk), ml->ifindex); |
| 3725 | if (dev != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | packet_dev_mc(dev, ml, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | kfree(ml); |
| 3728 | } |
| 3729 | rtnl_unlock(); |
| 3730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | |
| 3732 | static int |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3733 | packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval, |
| 3734 | unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | { |
| 3736 | struct sock *sk = sock->sk; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3737 | struct packet_sock *po = pkt_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | int ret; |
| 3739 | |
| 3740 | if (level != SOL_PACKET) |
| 3741 | return -ENOPROTOOPT; |
| 3742 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3743 | switch (optname) { |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 3744 | case PACKET_ADD_MEMBERSHIP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | case PACKET_DROP_MEMBERSHIP: |
| 3746 | { |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 3747 | struct packet_mreq_max mreq; |
| 3748 | int len = optlen; |
| 3749 | memset(&mreq, 0, sizeof(mreq)); |
| 3750 | if (len < sizeof(struct packet_mreq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | return -EINVAL; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 3752 | if (len > sizeof(mreq)) |
| 3753 | len = sizeof(mreq); |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3754 | if (copy_from_sockptr(&mreq, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | return -EFAULT; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 3756 | if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address))) |
| 3757 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | if (optname == PACKET_ADD_MEMBERSHIP) |
| 3759 | ret = packet_mc_add(sk, &mreq); |
| 3760 | else |
| 3761 | ret = packet_mc_drop(sk, &mreq); |
| 3762 | return ret; |
| 3763 | } |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 3764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | case PACKET_RX_RING: |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3766 | case PACKET_TX_RING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 3768 | union tpacket_req_u req_u; |
| 3769 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | |
Eric Dumazet | 5171b37 | 2018-04-15 17:52:04 -0700 | [diff] [blame] | 3771 | lock_sock(sk); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 3772 | switch (po->tp_version) { |
| 3773 | case TPACKET_V1: |
| 3774 | case TPACKET_V2: |
| 3775 | len = sizeof(req_u.req); |
| 3776 | break; |
| 3777 | case TPACKET_V3: |
| 3778 | default: |
| 3779 | len = sizeof(req_u.req3); |
| 3780 | break; |
| 3781 | } |
Eric Dumazet | 5171b37 | 2018-04-15 17:52:04 -0700 | [diff] [blame] | 3782 | if (optlen < len) { |
| 3783 | ret = -EINVAL; |
| 3784 | } else { |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3785 | if (copy_from_sockptr(&req_u.req, optval, len)) |
Eric Dumazet | 5171b37 | 2018-04-15 17:52:04 -0700 | [diff] [blame] | 3786 | ret = -EFAULT; |
| 3787 | else |
| 3788 | ret = packet_set_ring(sk, &req_u, 0, |
| 3789 | optname == PACKET_TX_RING); |
| 3790 | } |
| 3791 | release_sock(sk); |
| 3792 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | } |
| 3794 | case PACKET_COPY_THRESH: |
| 3795 | { |
| 3796 | int val; |
| 3797 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 3798 | if (optlen != sizeof(val)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3800 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | return -EFAULT; |
| 3802 | |
| 3803 | pkt_sk(sk)->copy_thresh = val; |
| 3804 | return 0; |
| 3805 | } |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 3806 | case PACKET_VERSION: |
| 3807 | { |
| 3808 | int val; |
| 3809 | |
| 3810 | if (optlen != sizeof(val)) |
| 3811 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3812 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 3813 | return -EFAULT; |
| 3814 | switch (val) { |
| 3815 | case TPACKET_V1: |
| 3816 | case TPACKET_V2: |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 3817 | case TPACKET_V3: |
Philip Pettersson | 84ac726 | 2016-11-30 14:55:36 -0800 | [diff] [blame] | 3818 | break; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 3819 | default: |
| 3820 | return -EINVAL; |
| 3821 | } |
Philip Pettersson | 84ac726 | 2016-11-30 14:55:36 -0800 | [diff] [blame] | 3822 | lock_sock(sk); |
| 3823 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { |
| 3824 | ret = -EBUSY; |
| 3825 | } else { |
| 3826 | po->tp_version = val; |
| 3827 | ret = 0; |
| 3828 | } |
| 3829 | release_sock(sk); |
| 3830 | return ret; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 3831 | } |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 3832 | case PACKET_RESERVE: |
| 3833 | { |
| 3834 | unsigned int val; |
| 3835 | |
| 3836 | if (optlen != sizeof(val)) |
| 3837 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3838 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 3839 | return -EFAULT; |
Andrey Konovalov | bcc5364 | 2017-03-29 16:11:22 +0200 | [diff] [blame] | 3840 | if (val > INT_MAX) |
| 3841 | return -EINVAL; |
Willem de Bruijn | c27927e | 2017-08-10 12:41:58 -0400 | [diff] [blame] | 3842 | lock_sock(sk); |
| 3843 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { |
| 3844 | ret = -EBUSY; |
| 3845 | } else { |
| 3846 | po->tp_reserve = val; |
| 3847 | ret = 0; |
| 3848 | } |
| 3849 | release_sock(sk); |
| 3850 | return ret; |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 3851 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3852 | case PACKET_LOSS: |
| 3853 | { |
| 3854 | unsigned int val; |
| 3855 | |
| 3856 | if (optlen != sizeof(val)) |
| 3857 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3858 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3859 | return -EFAULT; |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3860 | |
| 3861 | lock_sock(sk); |
| 3862 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { |
| 3863 | ret = -EBUSY; |
| 3864 | } else { |
| 3865 | po->tp_loss = !!val; |
| 3866 | ret = 0; |
| 3867 | } |
| 3868 | release_sock(sk); |
| 3869 | return ret; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 3870 | } |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3871 | case PACKET_AUXDATA: |
| 3872 | { |
| 3873 | int val; |
| 3874 | |
| 3875 | if (optlen < sizeof(val)) |
| 3876 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3877 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3878 | return -EFAULT; |
| 3879 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3880 | lock_sock(sk); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3881 | po->auxdata = !!val; |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3882 | release_sock(sk); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 3883 | return 0; |
| 3884 | } |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 3885 | case PACKET_ORIGDEV: |
| 3886 | { |
| 3887 | int val; |
| 3888 | |
| 3889 | if (optlen < sizeof(val)) |
| 3890 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3891 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 3892 | return -EFAULT; |
| 3893 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3894 | lock_sock(sk); |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 3895 | po->origdev = !!val; |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3896 | release_sock(sk); |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 3897 | return 0; |
| 3898 | } |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3899 | case PACKET_VNET_HDR: |
| 3900 | { |
| 3901 | int val; |
| 3902 | |
| 3903 | if (sock->type != SOCK_RAW) |
| 3904 | return -EINVAL; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3905 | if (optlen < sizeof(val)) |
| 3906 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3907 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3908 | return -EFAULT; |
| 3909 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3910 | lock_sock(sk); |
| 3911 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { |
| 3912 | ret = -EBUSY; |
| 3913 | } else { |
| 3914 | po->has_vnet_hdr = !!val; |
| 3915 | ret = 0; |
| 3916 | } |
| 3917 | release_sock(sk); |
| 3918 | return ret; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 3919 | } |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 3920 | case PACKET_TIMESTAMP: |
| 3921 | { |
| 3922 | int val; |
| 3923 | |
| 3924 | if (optlen != sizeof(val)) |
| 3925 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3926 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 3927 | return -EFAULT; |
| 3928 | |
| 3929 | po->tp_tstamp = val; |
| 3930 | return 0; |
| 3931 | } |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3932 | case PACKET_FANOUT: |
| 3933 | { |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 3934 | struct fanout_args args = { 0 }; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3935 | |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 3936 | if (optlen != sizeof(int) && optlen != sizeof(args)) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3937 | return -EINVAL; |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 3938 | if (copy_from_sockptr(&args, optval, optlen)) |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3939 | return -EFAULT; |
| 3940 | |
Tanner Love | 9c661b0 | 2020-11-06 13:07:40 -0500 | [diff] [blame] | 3941 | return fanout_add(sk, &args); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 3942 | } |
Willem de Bruijn | 47dceb8 | 2015-08-14 22:31:34 -0400 | [diff] [blame] | 3943 | case PACKET_FANOUT_DATA: |
| 3944 | { |
| 3945 | if (!po->fanout) |
| 3946 | return -EINVAL; |
| 3947 | |
| 3948 | return fanout_set_data(po, optval, optlen); |
| 3949 | } |
Vincent Whitchurch | fa788d9 | 2018-09-03 16:23:36 +0200 | [diff] [blame] | 3950 | case PACKET_IGNORE_OUTGOING: |
| 3951 | { |
| 3952 | int val; |
| 3953 | |
| 3954 | if (optlen != sizeof(val)) |
| 3955 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3956 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Vincent Whitchurch | fa788d9 | 2018-09-03 16:23:36 +0200 | [diff] [blame] | 3957 | return -EFAULT; |
| 3958 | if (val < 0 || val > 1) |
| 3959 | return -EINVAL; |
| 3960 | |
| 3961 | po->prot_hook.ignore_outgoing = !!val; |
| 3962 | return 0; |
| 3963 | } |
Paul Chavent | 5920cd3a | 2012-11-06 23:10:47 +0000 | [diff] [blame] | 3964 | case PACKET_TX_HAS_OFF: |
| 3965 | { |
| 3966 | unsigned int val; |
| 3967 | |
| 3968 | if (optlen != sizeof(val)) |
| 3969 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3970 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Paul Chavent | 5920cd3a | 2012-11-06 23:10:47 +0000 | [diff] [blame] | 3971 | return -EFAULT; |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3972 | |
| 3973 | lock_sock(sk); |
Jiapeng Chong | 25c55b3 | 2021-05-17 18:15:25 +0800 | [diff] [blame] | 3974 | if (!po->rx_ring.pg_vec && !po->tx_ring.pg_vec) |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3975 | po->tp_tx_has_off = !!val; |
Jiapeng Chong | 25c55b3 | 2021-05-17 18:15:25 +0800 | [diff] [blame] | 3976 | |
Willem de Bruijn | a6361f0 | 2018-04-23 17:37:03 -0400 | [diff] [blame] | 3977 | release_sock(sk); |
Paul Chavent | 5920cd3a | 2012-11-06 23:10:47 +0000 | [diff] [blame] | 3978 | return 0; |
| 3979 | } |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 3980 | case PACKET_QDISC_BYPASS: |
| 3981 | { |
| 3982 | int val; |
| 3983 | |
| 3984 | if (optlen != sizeof(val)) |
| 3985 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3986 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 3987 | return -EFAULT; |
| 3988 | |
| 3989 | po->xmit = val ? packet_direct_xmit : dev_queue_xmit; |
| 3990 | return 0; |
| 3991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | default: |
| 3993 | return -ENOPROTOOPT; |
| 3994 | } |
| 3995 | } |
| 3996 | |
| 3997 | static int packet_getsockopt(struct socket *sock, int level, int optname, |
| 3998 | char __user *optval, int __user *optlen) |
| 3999 | { |
| 4000 | int len; |
Eric Dumazet | c06fff6 | 2012-04-19 21:56:11 +0000 | [diff] [blame] | 4001 | int val, lv = sizeof(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | struct sock *sk = sock->sk; |
| 4003 | struct packet_sock *po = pkt_sk(sk); |
Eric Dumazet | c06fff6 | 2012-04-19 21:56:11 +0000 | [diff] [blame] | 4004 | void *data = &val; |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 4005 | union tpacket_stats_u st; |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 4006 | struct tpacket_rollover_stats rstats; |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 4007 | int drops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | |
| 4009 | if (level != SOL_PACKET) |
| 4010 | return -ENOPROTOOPT; |
| 4011 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 4012 | if (get_user(len, optlen)) |
| 4013 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | |
| 4015 | if (len < 0) |
| 4016 | return -EINVAL; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4017 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4018 | switch (optname) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | case PACKET_STATISTICS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | spin_lock_bh(&sk->sk_receive_queue.lock); |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 4021 | memcpy(&st, &po->stats, sizeof(st)); |
| 4022 | memset(&po->stats, 0, sizeof(po->stats)); |
| 4023 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 4024 | drops = atomic_xchg(&po->tp_drops, 0); |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 4025 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4026 | if (po->tp_version == TPACKET_V3) { |
Eric Dumazet | c06fff6 | 2012-04-19 21:56:11 +0000 | [diff] [blame] | 4027 | lv = sizeof(struct tpacket_stats_v3); |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 4028 | st.stats3.tp_drops = drops; |
| 4029 | st.stats3.tp_packets += drops; |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 4030 | data = &st.stats3; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4031 | } else { |
Eric Dumazet | c06fff6 | 2012-04-19 21:56:11 +0000 | [diff] [blame] | 4032 | lv = sizeof(struct tpacket_stats); |
Eric Dumazet | 8e8e295 | 2019-06-12 09:52:30 -0700 | [diff] [blame] | 4033 | st.stats1.tp_drops = drops; |
| 4034 | st.stats1.tp_packets += drops; |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 4035 | data = &st.stats1; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4036 | } |
Daniel Borkmann | ee80fbf | 2013-04-19 06:12:29 +0000 | [diff] [blame] | 4037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | break; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 4039 | case PACKET_AUXDATA: |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 4040 | val = po->auxdata; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 4041 | break; |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 4042 | case PACKET_ORIGDEV: |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 4043 | val = po->origdev; |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 4044 | break; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 4045 | case PACKET_VNET_HDR: |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 4046 | val = po->has_vnet_hdr; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 4047 | break; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4048 | case PACKET_VERSION: |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4049 | val = po->tp_version; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4050 | break; |
| 4051 | case PACKET_HDRLEN: |
| 4052 | if (len > sizeof(int)) |
| 4053 | len = sizeof(int); |
Alexander Potapenko | fd2c83b | 2017-04-25 18:51:46 +0200 | [diff] [blame] | 4054 | if (len < sizeof(int)) |
| 4055 | return -EINVAL; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4056 | if (copy_from_user(&val, optval, len)) |
| 4057 | return -EFAULT; |
| 4058 | switch (val) { |
| 4059 | case TPACKET_V1: |
| 4060 | val = sizeof(struct tpacket_hdr); |
| 4061 | break; |
| 4062 | case TPACKET_V2: |
| 4063 | val = sizeof(struct tpacket2_hdr); |
| 4064 | break; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4065 | case TPACKET_V3: |
| 4066 | val = sizeof(struct tpacket3_hdr); |
| 4067 | break; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4068 | default: |
| 4069 | return -EINVAL; |
| 4070 | } |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4071 | break; |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 4072 | case PACKET_RESERVE: |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 4073 | val = po->tp_reserve; |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 4074 | break; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4075 | case PACKET_LOSS: |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4076 | val = po->tp_loss; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4077 | break; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 4078 | case PACKET_TIMESTAMP: |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 4079 | val = po->tp_tstamp; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 4080 | break; |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 4081 | case PACKET_FANOUT: |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 4082 | val = (po->fanout ? |
| 4083 | ((u32)po->fanout->id | |
Willem de Bruijn | 77f65eb | 2013-03-19 10:18:11 +0000 | [diff] [blame] | 4084 | ((u32)po->fanout->type << 16) | |
| 4085 | ((u32)po->fanout->flags << 24)) : |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 4086 | 0); |
David S. Miller | dc99f60 | 2011-07-05 01:45:05 -0700 | [diff] [blame] | 4087 | break; |
Vincent Whitchurch | fa788d9 | 2018-09-03 16:23:36 +0200 | [diff] [blame] | 4088 | case PACKET_IGNORE_OUTGOING: |
| 4089 | val = po->prot_hook.ignore_outgoing; |
| 4090 | break; |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 4091 | case PACKET_ROLLOVER_STATS: |
Mike Maloney | 57f015f | 2017-11-28 10:44:29 -0500 | [diff] [blame] | 4092 | if (!po->rollover) |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 4093 | return -EINVAL; |
Mike Maloney | 57f015f | 2017-11-28 10:44:29 -0500 | [diff] [blame] | 4094 | rstats.tp_all = atomic_long_read(&po->rollover->num); |
| 4095 | rstats.tp_huge = atomic_long_read(&po->rollover->num_huge); |
| 4096 | rstats.tp_failed = atomic_long_read(&po->rollover->num_failed); |
| 4097 | data = &rstats; |
| 4098 | lv = sizeof(rstats); |
Willem de Bruijn | a9b6391 | 2015-05-12 11:56:50 -0400 | [diff] [blame] | 4099 | break; |
Paul Chavent | 5920cd3a | 2012-11-06 23:10:47 +0000 | [diff] [blame] | 4100 | case PACKET_TX_HAS_OFF: |
| 4101 | val = po->tp_tx_has_off; |
| 4102 | break; |
Daniel Borkmann | d346a3f | 2013-12-06 11:36:17 +0100 | [diff] [blame] | 4103 | case PACKET_QDISC_BYPASS: |
| 4104 | val = packet_use_direct_xmit(po); |
| 4105 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | default: |
| 4107 | return -ENOPROTOOPT; |
| 4108 | } |
| 4109 | |
Eric Dumazet | c06fff6 | 2012-04-19 21:56:11 +0000 | [diff] [blame] | 4110 | if (len > lv) |
| 4111 | len = lv; |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 4112 | if (put_user(len, optlen)) |
| 4113 | return -EFAULT; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 4114 | if (copy_to_user(optval, data, len)) |
| 4115 | return -EFAULT; |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 4116 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | } |
| 4118 | |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 4119 | static int packet_notifier(struct notifier_block *this, |
| 4120 | unsigned long msg, void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | { |
| 4122 | struct sock *sk; |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 4123 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 4124 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4126 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4127 | sk_for_each_rcu(sk, &net->packet.sklist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | struct packet_sock *po = pkt_sk(sk); |
| 4129 | |
| 4130 | switch (msg) { |
| 4131 | case NETDEV_UNREGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | if (po->mclist) |
Francesco Ruggeri | 82f1709 | 2015-03-09 11:51:04 -0700 | [diff] [blame] | 4133 | packet_dev_mclist_delete(dev, &po->mclist); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4134 | fallthrough; |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 4135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | case NETDEV_DOWN: |
| 4137 | if (dev->ifindex == po->ifindex) { |
| 4138 | spin_lock(&po->bind_lock); |
| 4139 | if (po->running) { |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 4140 | __unregister_prot_hook(sk, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4141 | sk->sk_err = ENETDOWN; |
| 4142 | if (!sock_flag(sk, SOCK_DEAD)) |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 4143 | sk_error_report(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | } |
| 4145 | if (msg == NETDEV_UNREGISTER) { |
Daniel Borkmann | 66e56cd | 2013-12-06 11:36:15 +0100 | [diff] [blame] | 4146 | packet_cached_dev_reset(po); |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 4147 | WRITE_ONCE(po->ifindex, -1); |
Eric Dumazet | f1d9268 | 2021-12-14 07:09:33 -0800 | [diff] [blame^] | 4148 | dev_put_track(po->prot_hook.dev, |
| 4149 | &po->prot_hook.dev_tracker); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | po->prot_hook.dev = NULL; |
| 4151 | } |
| 4152 | spin_unlock(&po->bind_lock); |
| 4153 | } |
| 4154 | break; |
| 4155 | case NETDEV_UP: |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4156 | if (dev->ifindex == po->ifindex) { |
| 4157 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 4158 | if (po->num) |
| 4159 | register_prot_hook(sk); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4160 | spin_unlock(&po->bind_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | break; |
| 4163 | } |
| 4164 | } |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4165 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | return NOTIFY_DONE; |
| 4167 | } |
| 4168 | |
| 4169 | |
| 4170 | static int packet_ioctl(struct socket *sock, unsigned int cmd, |
| 4171 | unsigned long arg) |
| 4172 | { |
| 4173 | struct sock *sk = sock->sk; |
| 4174 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4175 | switch (cmd) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4176 | case SIOCOUTQ: |
| 4177 | { |
| 4178 | int amount = sk_wmem_alloc_get(sk); |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 4179 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4180 | return put_user(amount, (int __user *)arg); |
| 4181 | } |
| 4182 | case SIOCINQ: |
| 4183 | { |
| 4184 | struct sk_buff *skb; |
| 4185 | int amount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4187 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 4188 | skb = skb_peek(&sk->sk_receive_queue); |
| 4189 | if (skb) |
| 4190 | amount = skb->len; |
| 4191 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 4192 | return put_user(amount, (int __user *)arg); |
| 4193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | #ifdef CONFIG_INET |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4195 | case SIOCADDRT: |
| 4196 | case SIOCDELRT: |
| 4197 | case SIOCDARP: |
| 4198 | case SIOCGARP: |
| 4199 | case SIOCSARP: |
| 4200 | case SIOCGIFADDR: |
| 4201 | case SIOCSIFADDR: |
| 4202 | case SIOCGIFBRDADDR: |
| 4203 | case SIOCSIFBRDADDR: |
| 4204 | case SIOCGIFNETMASK: |
| 4205 | case SIOCSIFNETMASK: |
| 4206 | case SIOCGIFDSTADDR: |
| 4207 | case SIOCSIFDSTADDR: |
| 4208 | case SIOCSIFFLAGS: |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4209 | return inet_dgram_ops.ioctl(sock, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | #endif |
| 4211 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4212 | default: |
| 4213 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | } |
| 4215 | return 0; |
| 4216 | } |
| 4217 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 4218 | static __poll_t packet_poll(struct file *file, struct socket *sock, |
| 4219 | poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | { |
| 4221 | struct sock *sk = sock->sk; |
| 4222 | struct packet_sock *po = pkt_sk(sk); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 4223 | __poll_t mask = datagram_poll(file, sock, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | |
| 4225 | spin_lock_bh(&sk->sk_receive_queue.lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4226 | if (po->rx_ring.pg_vec) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4227 | if (!packet_previous_rx_frame(po, &po->rx_ring, |
| 4228 | TP_STATUS_KERNEL)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4229 | mask |= EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | } |
Eric Dumazet | 9bb6cd6 | 2019-06-12 09:52:33 -0700 | [diff] [blame] | 4231 | packet_rcv_try_clear_pressure(po); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4233 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 4234 | if (po->tx_ring.pg_vec) { |
| 4235 | if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 4236 | mask |= EPOLLOUT | EPOLLWRNORM; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4237 | } |
| 4238 | spin_unlock_bh(&sk->sk_write_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | return mask; |
| 4240 | } |
| 4241 | |
| 4242 | |
| 4243 | /* Dirty? Well, I still did not learn better way to account |
| 4244 | * for user mmaps. |
| 4245 | */ |
| 4246 | |
| 4247 | static void packet_mm_open(struct vm_area_struct *vma) |
| 4248 | { |
| 4249 | struct file *file = vma->vm_file; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4250 | struct socket *sock = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | if (sk) |
| 4254 | atomic_inc(&pkt_sk(sk)->mapped); |
| 4255 | } |
| 4256 | |
| 4257 | static void packet_mm_close(struct vm_area_struct *vma) |
| 4258 | { |
| 4259 | struct file *file = vma->vm_file; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4260 | struct socket *sock = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | if (sk) |
| 4264 | atomic_dec(&pkt_sk(sk)->mapped); |
| 4265 | } |
| 4266 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 4267 | static const struct vm_operations_struct packet_mmap_ops = { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4268 | .open = packet_mm_open, |
| 4269 | .close = packet_mm_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | }; |
| 4271 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4272 | static void free_pg_vec(struct pgv *pg_vec, unsigned int order, |
| 4273 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | { |
| 4275 | int i; |
| 4276 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4277 | for (i = 0; i < len; i++) { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4278 | if (likely(pg_vec[i].buffer)) { |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4279 | if (is_vmalloc_addr(pg_vec[i].buffer)) |
| 4280 | vfree(pg_vec[i].buffer); |
| 4281 | else |
| 4282 | free_pages((unsigned long)pg_vec[i].buffer, |
| 4283 | order); |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4284 | pg_vec[i].buffer = NULL; |
| 4285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | } |
| 4287 | kfree(pg_vec); |
| 4288 | } |
| 4289 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4290 | static char *alloc_one_pg_vec_page(unsigned long order) |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4291 | { |
Daniel Borkmann | f0d4eb2 | 2014-01-19 11:46:53 +0100 | [diff] [blame] | 4292 | char *buffer; |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4293 | gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | |
| 4294 | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY; |
Eric Dumazet | 719bfea | 2009-04-15 03:39:52 -0700 | [diff] [blame] | 4295 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4296 | buffer = (char *) __get_free_pages(gfp_flags, order); |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4297 | if (buffer) |
| 4298 | return buffer; |
| 4299 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4300 | /* __get_free_pages failed, fall back to vmalloc */ |
| 4301 | buffer = vzalloc(array_size((1 << order), PAGE_SIZE)); |
| 4302 | if (buffer) |
| 4303 | return buffer; |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4304 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4305 | /* vmalloc failed, lets dig into swap here */ |
| 4306 | gfp_flags &= ~__GFP_NORETRY; |
| 4307 | buffer = (char *) __get_free_pages(gfp_flags, order); |
| 4308 | if (buffer) |
| 4309 | return buffer; |
| 4310 | |
| 4311 | /* complete and utter failure */ |
| 4312 | return NULL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4313 | } |
| 4314 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4315 | static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order) |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4316 | { |
| 4317 | unsigned int block_nr = req->tp_block_nr; |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4318 | struct pgv *pg_vec; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4319 | int i; |
| 4320 | |
Christoph Paasch | 398f013 | 2019-03-18 23:14:52 -0700 | [diff] [blame] | 4321 | pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN); |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4322 | if (unlikely(!pg_vec)) |
| 4323 | goto out; |
| 4324 | |
| 4325 | for (i = 0; i < block_nr; i++) { |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4326 | pg_vec[i].buffer = alloc_one_pg_vec_page(order); |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4327 | if (unlikely(!pg_vec[i].buffer)) |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4328 | goto out_free_pgvec; |
| 4329 | } |
| 4330 | |
| 4331 | out: |
| 4332 | return pg_vec; |
| 4333 | |
| 4334 | out_free_pgvec: |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4335 | free_pg_vec(pg_vec, order, block_nr); |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4336 | pg_vec = NULL; |
| 4337 | goto out; |
| 4338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4340 | static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4341 | int closing, int tx_ring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4343 | struct pgv *pg_vec = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | struct packet_sock *po = pkt_sk(sk); |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 4345 | unsigned long *rx_owner_map = NULL; |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4346 | int was_running, order = 0; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4347 | struct packet_ring_buffer *rb; |
| 4348 | struct sk_buff_head *rb_queue; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 4349 | __be16 num; |
Colin Ian King | 2a6d6c3 | 2020-07-01 16:04:33 +0100 | [diff] [blame] | 4350 | int err; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4351 | /* Added to avoid minimal code churn */ |
| 4352 | struct tpacket_req *req = &req_u->req; |
| 4353 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4354 | rb = tx_ring ? &po->tx_ring : &po->rx_ring; |
| 4355 | rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue; |
| 4356 | |
| 4357 | err = -EBUSY; |
| 4358 | if (!closing) { |
| 4359 | if (atomic_read(&po->mapped)) |
| 4360 | goto out; |
Daniel Borkmann | b013840 | 2014-01-15 16:25:36 +0100 | [diff] [blame] | 4361 | if (packet_read_pending(rb)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4362 | goto out; |
| 4363 | } |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4365 | if (req->tp_block_nr) { |
Willem de Bruijn | 4576cd4 | 2018-08-06 10:38:34 -0400 | [diff] [blame] | 4366 | unsigned int min_frame_size; |
| 4367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | /* Sanity tests and some calculations */ |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4369 | err = -EBUSY; |
| 4370 | if (unlikely(rb->pg_vec)) |
| 4371 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4373 | switch (po->tp_version) { |
| 4374 | case TPACKET_V1: |
| 4375 | po->tp_hdrlen = TPACKET_HDRLEN; |
| 4376 | break; |
| 4377 | case TPACKET_V2: |
| 4378 | po->tp_hdrlen = TPACKET2_HDRLEN; |
| 4379 | break; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4380 | case TPACKET_V3: |
| 4381 | po->tp_hdrlen = TPACKET3_HDRLEN; |
| 4382 | break; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 4383 | } |
| 4384 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4385 | err = -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4386 | if (unlikely((int)req->tp_block_size <= 0)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4387 | goto out; |
Tobias Klauser | 90836b6 | 2015-11-17 10:40:21 +0100 | [diff] [blame] | 4388 | if (unlikely(!PAGE_ALIGNED(req->tp_block_size))) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4389 | goto out; |
Willem de Bruijn | 4576cd4 | 2018-08-06 10:38:34 -0400 | [diff] [blame] | 4390 | min_frame_size = po->tp_hdrlen + po->tp_reserve; |
Eric Dumazet | dc80811 | 2014-08-15 09:16:04 -0700 | [diff] [blame] | 4391 | if (po->tp_version >= TPACKET_V3 && |
Willem de Bruijn | 4576cd4 | 2018-08-06 10:38:34 -0400 | [diff] [blame] | 4392 | req->tp_block_size < |
| 4393 | BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size) |
Eric Dumazet | dc80811 | 2014-08-15 09:16:04 -0700 | [diff] [blame] | 4394 | goto out; |
Willem de Bruijn | 4576cd4 | 2018-08-06 10:38:34 -0400 | [diff] [blame] | 4395 | if (unlikely(req->tp_frame_size < min_frame_size)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4396 | goto out; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4397 | if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1))) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4398 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | |
Tobias Klauser | 4194b49 | 2015-11-17 10:38:36 +0100 | [diff] [blame] | 4400 | rb->frames_per_block = req->tp_block_size / req->tp_frame_size; |
| 4401 | if (unlikely(rb->frames_per_block == 0)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4402 | goto out; |
Kal Conley | fc62814 | 2019-02-10 09:57:11 +0100 | [diff] [blame] | 4403 | if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr)) |
Andrey Konovalov | 8f8d28e | 2017-03-29 16:11:21 +0200 | [diff] [blame] | 4404 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4405 | if (unlikely((rb->frames_per_block * req->tp_block_nr) != |
| 4406 | req->tp_frame_nr)) |
| 4407 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | |
| 4409 | err = -ENOMEM; |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4410 | order = get_order(req->tp_block_size); |
| 4411 | pg_vec = alloc_pg_vec(req, order); |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4412 | if (unlikely(!pg_vec)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | goto out; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4414 | switch (po->tp_version) { |
| 4415 | case TPACKET_V3: |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 4416 | /* Block transmit is not supported yet */ |
| 4417 | if (!tx_ring) { |
Maninder Singh | e8e85cc | 2015-06-22 12:39:16 +0530 | [diff] [blame] | 4418 | init_prb_bdqc(po, rb, pg_vec, req_u); |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 4419 | } else { |
| 4420 | struct tpacket_req3 *req3 = &req_u->req3; |
| 4421 | |
| 4422 | if (req3->tp_retire_blk_tov || |
| 4423 | req3->tp_sizeof_priv || |
| 4424 | req3->tp_feature_req_word) { |
| 4425 | err = -EINVAL; |
Eric Dumazet | 55655e3 | 2019-06-24 02:38:20 -0700 | [diff] [blame] | 4426 | goto out_free_pg_vec; |
Sowmini Varadhan | 7f953ab | 2017-01-03 06:31:47 -0800 | [diff] [blame] | 4427 | } |
| 4428 | } |
Dan Carpenter | d7cf0c3 | 2014-02-18 15:20:51 +0300 | [diff] [blame] | 4429 | break; |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4430 | default: |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 4431 | if (!tx_ring) { |
| 4432 | rx_owner_map = bitmap_alloc(req->tp_frame_nr, |
| 4433 | GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); |
| 4434 | if (!rx_owner_map) |
| 4435 | goto out_free_pg_vec; |
| 4436 | } |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4437 | break; |
| 4438 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4439 | } |
| 4440 | /* Done */ |
| 4441 | else { |
| 4442 | err = -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4443 | if (unlikely(req->tp_frame_nr)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4444 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | |
| 4448 | /* Detach socket from network */ |
| 4449 | spin_lock(&po->bind_lock); |
| 4450 | was_running = po->running; |
| 4451 | num = po->num; |
| 4452 | if (was_running) { |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 4453 | WRITE_ONCE(po->num, 0); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 4454 | __unregister_prot_hook(sk, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | } |
| 4456 | spin_unlock(&po->bind_lock); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | synchronize_net(); |
| 4459 | |
| 4460 | err = -EBUSY; |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 4461 | mutex_lock(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | if (closing || atomic_read(&po->mapped) == 0) { |
| 4463 | err = 0; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4464 | spin_lock_bh(&rb_queue->lock); |
Changli Gao | c053fd9 | 2010-12-10 16:02:20 -0800 | [diff] [blame] | 4465 | swap(rb->pg_vec, pg_vec); |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 4466 | if (po->tp_version <= TPACKET_V2) |
| 4467 | swap(rb->rx_owner_map, rx_owner_map); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4468 | rb->frame_max = (req->tp_frame_nr - 1); |
| 4469 | rb->head = 0; |
| 4470 | rb->frame_size = req->tp_frame_size; |
| 4471 | spin_unlock_bh(&rb_queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4473 | swap(rb->pg_vec_order, order); |
Changli Gao | c053fd9 | 2010-12-10 16:02:20 -0800 | [diff] [blame] | 4474 | swap(rb->pg_vec_len, req->tp_block_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4476 | rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE; |
| 4477 | po->prot_hook.func = (po->rx_ring.pg_vec) ? |
| 4478 | tpacket_rcv : packet_rcv; |
| 4479 | skb_queue_purge(rb_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | if (atomic_read(&po->mapped)) |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4481 | pr_err("packet_mmap: vma is busy: %d\n", |
| 4482 | atomic_read(&po->mapped)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | } |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 4484 | mutex_unlock(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | |
| 4486 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 4487 | if (was_running) { |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 4488 | WRITE_ONCE(po->num, num); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame] | 4489 | register_prot_hook(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | } |
| 4491 | spin_unlock(&po->bind_lock); |
WANG Cong | c800aaf | 2017-07-24 10:07:32 -0700 | [diff] [blame] | 4492 | if (pg_vec && (po->tp_version > TPACKET_V2)) { |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4493 | /* Because we don't support block-based V3 on tx-ring */ |
| 4494 | if (!tx_ring) |
Tobias Klauser | 73d0fcf | 2015-07-28 14:21:26 +0200 | [diff] [blame] | 4495 | prb_shutdown_retire_blk_timer(po, rb_queue); |
chetan loke | f6fb8f1 | 2011-08-19 10:18:16 +0000 | [diff] [blame] | 4496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | |
Eric Dumazet | 55655e3 | 2019-06-24 02:38:20 -0700 | [diff] [blame] | 4498 | out_free_pg_vec: |
Willem de Bruijn | 61fad68 | 2020-03-13 12:18:09 -0400 | [diff] [blame] | 4499 | bitmap_free(rx_owner_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | if (pg_vec) |
Eric Dumazet | 3a7ad06 | 2018-08-29 11:50:12 -0700 | [diff] [blame] | 4501 | free_pg_vec(pg_vec, order, req->tp_block_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | out: |
| 4503 | return err; |
| 4504 | } |
| 4505 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4506 | static int packet_mmap(struct file *file, struct socket *sock, |
| 4507 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | { |
| 4509 | struct sock *sk = sock->sk; |
| 4510 | struct packet_sock *po = pkt_sk(sk); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4511 | unsigned long size, expected_size; |
| 4512 | struct packet_ring_buffer *rb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | unsigned long start; |
| 4514 | int err = -EINVAL; |
| 4515 | int i; |
| 4516 | |
| 4517 | if (vma->vm_pgoff) |
| 4518 | return -EINVAL; |
| 4519 | |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 4520 | mutex_lock(&po->pg_vec_lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4521 | |
| 4522 | expected_size = 0; |
| 4523 | for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) { |
| 4524 | if (rb->pg_vec) { |
| 4525 | expected_size += rb->pg_vec_len |
| 4526 | * rb->pg_vec_pages |
| 4527 | * PAGE_SIZE; |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | if (expected_size == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4533 | |
| 4534 | size = vma->vm_end - vma->vm_start; |
| 4535 | if (size != expected_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | goto out; |
| 4537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | start = vma->vm_start; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4539 | for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) { |
| 4540 | if (rb->pg_vec == NULL) |
| 4541 | continue; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4542 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4543 | for (i = 0; i < rb->pg_vec_len; i++) { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4544 | struct page *page; |
| 4545 | void *kaddr = rb->pg_vec[i].buffer; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4546 | int pg_num; |
| 4547 | |
Changli Gao | c56b4d9 | 2010-12-01 02:52:57 +0000 | [diff] [blame] | 4548 | for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) { |
| 4549 | page = pgv_to_page(kaddr); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4550 | err = vm_insert_page(vma, start, page); |
| 4551 | if (unlikely(err)) |
| 4552 | goto out; |
| 4553 | start += PAGE_SIZE; |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 4554 | kaddr += PAGE_SIZE; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4555 | } |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 4558 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 4559 | atomic_inc(&po->mapped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | vma->vm_ops = &packet_mmap_ops; |
| 4561 | err = 0; |
| 4562 | |
| 4563 | out: |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 4564 | mutex_unlock(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | return err; |
| 4566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 4568 | static const struct proto_ops packet_ops_spkt = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | .family = PF_PACKET, |
| 4570 | .owner = THIS_MODULE, |
| 4571 | .release = packet_release, |
| 4572 | .bind = packet_bind_spkt, |
| 4573 | .connect = sock_no_connect, |
| 4574 | .socketpair = sock_no_socketpair, |
| 4575 | .accept = sock_no_accept, |
| 4576 | .getname = packet_getname_spkt, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 4577 | .poll = datagram_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | .ioctl = packet_ioctl, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 4579 | .gettstamp = sock_gettstamp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | .listen = sock_no_listen, |
| 4581 | .shutdown = sock_no_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | .sendmsg = packet_sendmsg_spkt, |
| 4583 | .recvmsg = packet_recvmsg, |
| 4584 | .mmap = sock_no_mmap, |
| 4585 | .sendpage = sock_no_sendpage, |
| 4586 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 4588 | static const struct proto_ops packet_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | .family = PF_PACKET, |
| 4590 | .owner = THIS_MODULE, |
| 4591 | .release = packet_release, |
| 4592 | .bind = packet_bind, |
| 4593 | .connect = sock_no_connect, |
| 4594 | .socketpair = sock_no_socketpair, |
| 4595 | .accept = sock_no_accept, |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4596 | .getname = packet_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 4597 | .poll = packet_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | .ioctl = packet_ioctl, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 4599 | .gettstamp = sock_gettstamp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | .listen = sock_no_listen, |
| 4601 | .shutdown = sock_no_shutdown, |
| 4602 | .setsockopt = packet_setsockopt, |
| 4603 | .getsockopt = packet_getsockopt, |
| 4604 | .sendmsg = packet_sendmsg, |
| 4605 | .recvmsg = packet_recvmsg, |
| 4606 | .mmap = packet_mmap, |
| 4607 | .sendpage = sock_no_sendpage, |
| 4608 | }; |
| 4609 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 4610 | static const struct net_proto_family packet_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | .family = PF_PACKET, |
| 4612 | .create = packet_create, |
| 4613 | .owner = THIS_MODULE, |
| 4614 | }; |
| 4615 | |
| 4616 | static struct notifier_block packet_netdev_notifier = { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4617 | .notifier_call = packet_notifier, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | }; |
| 4619 | |
| 4620 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | |
| 4622 | static void *packet_seq_start(struct seq_file *seq, loff_t *pos) |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4623 | __acquires(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 4625 | struct net *net = seq_file_net(seq); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4626 | |
| 4627 | rcu_read_lock(); |
| 4628 | return seq_hlist_start_head_rcu(&net->packet.sklist, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 4632 | { |
Herbert Xu | 1bf4095 | 2007-12-16 14:04:02 -0800 | [diff] [blame] | 4633 | struct net *net = seq_file_net(seq); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4634 | return seq_hlist_next_rcu(v, &net->packet.sklist, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | static void packet_seq_stop(struct seq_file *seq, void *v) |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4638 | __releases(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | { |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 4640 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | } |
| 4642 | |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 4643 | static int packet_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | { |
| 4645 | if (v == SEQ_START_TOKEN) |
Baruch Siach | abdcd06 | 2020-12-16 09:28:04 +0200 | [diff] [blame] | 4646 | seq_printf(seq, |
| 4647 | "%*sRefCnt Type Proto Iface R Rmem User Inode\n", |
| 4648 | IS_ENABLED(CONFIG_64BIT) ? -17 : -9, "sk"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | else { |
Li Zefan | b7ceabd | 2010-02-08 23:19:29 +0000 | [diff] [blame] | 4650 | struct sock *s = sk_entry(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | const struct packet_sock *po = pkt_sk(s); |
| 4652 | |
| 4653 | seq_printf(seq, |
Dan Rosenberg | 71338aa | 2011-05-23 12:17:35 +0000 | [diff] [blame] | 4654 | "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | s, |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 4656 | refcount_read(&s->sk_refcnt), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | s->sk_type, |
Eric Dumazet | c7d2ef5 | 2021-06-16 06:42:01 -0700 | [diff] [blame] | 4658 | ntohs(READ_ONCE(po->num)), |
Eric Dumazet | e032f7c | 2021-06-16 06:42:02 -0700 | [diff] [blame] | 4659 | READ_ONCE(po->ifindex), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | po->running, |
| 4661 | atomic_read(&s->sk_rmem_alloc), |
Eric W. Biederman | a7cb5a4 | 2012-05-24 01:10:10 -0600 | [diff] [blame] | 4662 | from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)), |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 4663 | sock_i_ino(s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | return 0; |
| 4667 | } |
| 4668 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 4669 | static const struct seq_operations packet_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | .start = packet_seq_start, |
| 4671 | .next = packet_seq_next, |
| 4672 | .stop = packet_seq_stop, |
| 4673 | .show = packet_seq_show, |
| 4674 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | #endif |
| 4676 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4677 | static int __net_init packet_net_init(struct net *net) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4678 | { |
Pavel Emelyanov | 0fa7fa9 | 2012-08-21 01:06:47 +0000 | [diff] [blame] | 4679 | mutex_init(&net->packet.sklist_lock); |
Denis V. Lunev | 2aaef4e | 2007-12-11 04:19:54 -0800 | [diff] [blame] | 4680 | INIT_HLIST_HEAD(&net->packet.sklist); |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4681 | |
Yonatan Linik | a268e0f | 2020-12-14 22:25:50 +0200 | [diff] [blame] | 4682 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 4683 | if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops, |
| 4684 | sizeof(struct seq_net_private))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4685 | return -ENOMEM; |
Yonatan Linik | a268e0f | 2020-12-14 22:25:50 +0200 | [diff] [blame] | 4686 | #endif /* CONFIG_PROC_FS */ |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4687 | |
| 4688 | return 0; |
| 4689 | } |
| 4690 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 4691 | static void __net_exit packet_net_exit(struct net *net) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4692 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 4693 | remove_proc_entry("packet", net->proc_net); |
Vasily Averin | 669f8f1 | 2017-11-12 22:27:49 +0300 | [diff] [blame] | 4694 | WARN_ON_ONCE(!hlist_empty(&net->packet.sklist)); |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | static struct pernet_operations packet_net_ops = { |
| 4698 | .init = packet_net_init, |
| 4699 | .exit = packet_net_exit, |
| 4700 | }; |
| 4701 | |
| 4702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | static void __exit packet_exit(void) |
| 4704 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | unregister_netdevice_notifier(&packet_netdev_notifier); |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 4706 | unregister_pernet_subsys(&packet_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | sock_unregister(PF_PACKET); |
| 4708 | proto_unregister(&packet_proto); |
| 4709 | } |
| 4710 | |
| 4711 | static int __init packet_init(void) |
| 4712 | { |
YueHaibing | 36096f2 | 2019-05-09 22:52:20 +0800 | [diff] [blame] | 4713 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | |
YueHaibing | 36096f2 | 2019-05-09 22:52:20 +0800 | [diff] [blame] | 4715 | rc = proto_register(&packet_proto, 0); |
| 4716 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | goto out; |
YueHaibing | 36096f2 | 2019-05-09 22:52:20 +0800 | [diff] [blame] | 4718 | rc = sock_register(&packet_family_ops); |
| 4719 | if (rc) |
| 4720 | goto out_proto; |
| 4721 | rc = register_pernet_subsys(&packet_net_ops); |
| 4722 | if (rc) |
| 4723 | goto out_sock; |
| 4724 | rc = register_netdevice_notifier(&packet_netdev_notifier); |
| 4725 | if (rc) |
| 4726 | goto out_pernet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | |
YueHaibing | 36096f2 | 2019-05-09 22:52:20 +0800 | [diff] [blame] | 4728 | return 0; |
| 4729 | |
| 4730 | out_pernet: |
| 4731 | unregister_pernet_subsys(&packet_net_ops); |
| 4732 | out_sock: |
| 4733 | sock_unregister(PF_PACKET); |
| 4734 | out_proto: |
| 4735 | proto_unregister(&packet_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | out: |
| 4737 | return rc; |
| 4738 | } |
| 4739 | |
| 4740 | module_init(packet_init); |
| 4741 | module_exit(packet_exit); |
| 4742 | MODULE_LICENSE("GPL"); |
| 4743 | MODULE_ALIAS_NETPROTO(PF_PACKET); |