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