kfree_skb assumes that an skb is dropped after an failure and notes that.
consume_skb should be used in non-failure situations. Such information is
important for dropmonitor netlink which tells how many packets were dropped
and where this drop happened.
Signed-off-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
 
        forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
        if (!forw_packet_aggr->skb) {
-               batadv_forw_packet_free(forw_packet_aggr);
+               batadv_forw_packet_free(forw_packet_aggr, true);
                return;
        }
 
        if (hardif_neigh)
                batadv_hardif_neigh_put(hardif_neigh);
 
-       kfree_skb(skb_priv);
+       consume_skb(skb_priv);
 }
 
 /**
        struct delayed_work *delayed_work;
        struct batadv_forw_packet *forw_packet;
        struct batadv_priv *bat_priv;
+       bool dropped = false;
 
        delayed_work = to_delayed_work(work);
        forw_packet = container_of(delayed_work, struct batadv_forw_packet,
        hlist_del(&forw_packet->list);
        spin_unlock_bh(&bat_priv->forw_bat_list_lock);
 
-       if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
+       if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
+               dropped = true;
                goto out;
+       }
 
        batadv_iv_ogm_emit(forw_packet);
 
                batadv_iv_ogm_schedule(forw_packet->if_incoming);
 
 out:
-       batadv_forw_packet_free(forw_packet);
+       batadv_forw_packet_free(forw_packet, dropped);
 }
 
 static int batadv_iv_ogm_receive(struct sk_buff *skb,
                ogm_packet = (struct batadv_ogm_packet *)packet_pos;
        }
 
-       kfree_skb(skb);
+       consume_skb(skb);
        return NET_RX_SUCCESS;
 }
 
 
 /**
  * batadv_frag_clear_chain - delete entries in the fragment buffer chain
  * @head: head of chain with entries.
+ * @dropped: whether the chain is cleared because all fragments are dropped
  *
  * Free fragments in the passed hlist. Should be called with appropriate lock.
  */
-static void batadv_frag_clear_chain(struct hlist_head *head)
+static void batadv_frag_clear_chain(struct hlist_head *head, bool dropped)
 {
        struct batadv_frag_list_entry *entry;
        struct hlist_node *node;
 
        hlist_for_each_entry_safe(entry, node, head, list) {
                hlist_del(&entry->list);
-               kfree_skb(entry->skb);
+
+               if (dropped)
+                       kfree_skb(entry->skb);
+               else
+                       consume_skb(entry->skb);
+
                kfree(entry);
        }
 }
                spin_lock_bh(&chain->lock);
 
                if (!check_cb || check_cb(chain)) {
-                       batadv_frag_clear_chain(&chain->fragment_list);
+                       batadv_frag_clear_chain(&chain->fragment_list, true);
                        chain->size = 0;
                }
 
                return false;
 
        if (!hlist_empty(&chain->fragment_list))
-               batadv_frag_clear_chain(&chain->fragment_list);
+               batadv_frag_clear_chain(&chain->fragment_list, true);
 
        chain->size = 0;
        chain->seqno = seqno;
                 * exceeds the maximum size of one merged packet. Don't allow
                 * packets to have different total_size.
                 */
-               batadv_frag_clear_chain(&chain->fragment_list);
+               batadv_frag_clear_chain(&chain->fragment_list, true);
                chain->size = 0;
        } else if (ntohs(frag_packet->total_size) == chain->size) {
                /* All fragments received. Hand over chain to caller. */
        struct batadv_frag_list_entry *entry;
        struct sk_buff *skb_out;
        int size, hdr_size = sizeof(struct batadv_frag_packet);
+       bool dropped = false;
 
        /* Remove first entry, as this is the destination for the rest of the
         * fragments.
        if (pskb_expand_head(skb_out, 0, size - skb_out->len, GFP_ATOMIC) < 0) {
                kfree_skb(skb_out);
                skb_out = NULL;
+               dropped = true;
                goto free;
        }
 
 
 free:
        /* Locking is not needed, because 'chain' is not part of any orig. */
-       batadv_frag_clear_chain(chain);
+       batadv_frag_clear_chain(chain, dropped);
        return skb_out;
 }
 
 
 /**
  * batadv_nc_packet_free - frees nc packet
  * @nc_packet: the nc packet to free
+ * @dropped: whether the packet is freed because is is dropped
  */
-static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
+static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet,
+                                 bool dropped)
 {
-       kfree_skb(nc_packet->skb);
+       if (dropped)
+               kfree_skb(nc_packet->skb);
+       else
+               consume_skb(nc_packet->skb);
+
        batadv_nc_path_put(nc_packet->nc_path);
        kfree(nc_packet);
 }
 {
        batadv_send_unicast_skb(nc_packet->skb, nc_packet->neigh_node);
        nc_packet->skb = NULL;
-       batadv_nc_packet_free(nc_packet);
+       batadv_nc_packet_free(nc_packet, false);
 }
 
 /**
 
        /* purge nc packet */
        list_del(&nc_packet->list);
-       batadv_nc_packet_free(nc_packet);
+       batadv_nc_packet_free(nc_packet, true);
 
        res = true;
 
        }
 
        /* skb_src is now coded into skb_dest, so free it */
-       kfree_skb(skb_src);
+       consume_skb(skb_src);
 
        /* avoid duplicate free of skb from nc_packet */
        nc_packet->skb = NULL;
-       batadv_nc_packet_free(nc_packet);
+       batadv_nc_packet_free(nc_packet, false);
 
        /* Send the coded packet and return true */
        batadv_send_unicast_skb(skb_dest, first_dest);
        /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
         * our ref
         */
-       kfree_skb(skb);
+       consume_skb(skb);
 }
 
 /**
        ether_addr_copy(unicast_packet->dest, orig_dest);
        unicast_packet->ttvn = ttvn;
 
-       batadv_nc_packet_free(nc_packet);
+       batadv_nc_packet_free(nc_packet, false);
        return unicast_packet;
 }
 
        return batadv_recv_unicast_packet(skb, recv_if);
 
 free_nc_packet:
-       batadv_nc_packet_free(nc_packet);
+       batadv_nc_packet_free(nc_packet, true);
        return NET_RX_DROP;
 }
 
 
 /**
  * batadv_forw_packet_free - free a forwarding packet
  * @forw_packet: The packet to free
+ * @dropped: whether the packet is freed because is is dropped
  *
  * This frees a forwarding packet and releases any resources it might
  * have claimed.
  */
-void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
+void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet,
+                            bool dropped)
 {
-       kfree_skb(forw_packet->skb);
+       if (dropped)
+               kfree_skb(forw_packet->skb);
+       else
+               consume_skb(forw_packet->skb);
+
        if (forw_packet->if_incoming)
                batadv_hardif_put(forw_packet->if_incoming);
        if (forw_packet->if_outgoing)
        return NETDEV_TX_OK;
 
 err_packet_free:
-       batadv_forw_packet_free(forw_packet);
+       batadv_forw_packet_free(forw_packet, true);
 err:
        return NETDEV_TX_BUSY;
 }
        struct sk_buff *skb1;
        struct net_device *soft_iface;
        struct batadv_priv *bat_priv;
+       bool dropped = false;
        u8 *neigh_addr;
        u8 *orig_neigh;
        int ret = 0;
        hlist_del(&forw_packet->list);
        spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
 
-       if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
+       if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
+               dropped = true;
                goto out;
+       }
 
-       if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
+       if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet)) {
+               dropped = true;
                goto out;
+       }
 
        bcast_packet = (struct batadv_bcast_packet *)forw_packet->skb->data;
 
        }
 
 out:
-       batadv_forw_packet_free(forw_packet);
+       batadv_forw_packet_free(forw_packet, dropped);
 }
 
 void
 
                if (pending) {
                        hlist_del(&forw_packet->list);
-                       batadv_forw_packet_free(forw_packet);
+                       batadv_forw_packet_free(forw_packet, true);
                }
        }
        spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
 
                if (pending) {
                        hlist_del(&forw_packet->list);
-                       batadv_forw_packet_free(forw_packet);
+                       batadv_forw_packet_free(forw_packet, true);
                }
        }
        spin_unlock_bh(&bat_priv->forw_bat_list_lock);
 
 
 struct sk_buff;
 
-void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet);
+void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet,
+                            bool dropped);
 struct batadv_forw_packet *
 batadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming,
                         struct batadv_hard_iface *if_outgoing,
 
                /* a copy is stored in the bcast list, therefore removing
                 * the original skb.
                 */
-               kfree_skb(skb);
+               consume_skb(skb);
 
        /* unicast packet */
        } else {