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