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