LCOV - code coverage report
Current view: top level - net/packet - af_packet.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 322 1694 19.0 %
Date: 2020-09-30 20:25:40 Functions: 22 97 22.7 %
Branches: 131 1206 10.9 %

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

Generated by: LCOV version 1.14