Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/tag_trailer.c - Trailer tag format handling |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/etherdevice.h> |
| 12 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 14 | #include "dsa_priv.h" |
| 15 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 16 | static netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev) |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 17 | { |
| 18 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 19 | struct sk_buff *nskb; |
| 20 | int padlen; |
| 21 | u8 *trailer; |
| 22 | |
| 23 | dev->stats.tx_packets++; |
| 24 | dev->stats.tx_bytes += skb->len; |
| 25 | |
| 26 | /* |
| 27 | * We have to make sure that the trailer ends up as the very |
| 28 | * last 4 bytes of the packet. This means that we have to pad |
| 29 | * the packet to the minimum ethernet frame size, if necessary, |
| 30 | * before adding the trailer. |
| 31 | */ |
| 32 | padlen = 0; |
| 33 | if (skb->len < 60) |
| 34 | padlen = 60 - skb->len; |
| 35 | |
| 36 | nskb = alloc_skb(NET_IP_ALIGN + skb->len + padlen + 4, GFP_ATOMIC); |
| 37 | if (nskb == NULL) { |
| 38 | kfree_skb(skb); |
| 39 | return NETDEV_TX_OK; |
| 40 | } |
| 41 | skb_reserve(nskb, NET_IP_ALIGN); |
| 42 | |
| 43 | skb_reset_mac_header(nskb); |
| 44 | skb_set_network_header(nskb, skb_network_header(skb) - skb->head); |
| 45 | skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head); |
| 46 | skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len)); |
| 47 | kfree_skb(skb); |
| 48 | |
| 49 | if (padlen) { |
| 50 | u8 *pad = skb_put(nskb, padlen); |
| 51 | memset(pad, 0, padlen); |
| 52 | } |
| 53 | |
| 54 | trailer = skb_put(nskb, 4); |
| 55 | trailer[0] = 0x80; |
| 56 | trailer[1] = 1 << p->port; |
| 57 | trailer[2] = 0x10; |
| 58 | trailer[3] = 0x00; |
| 59 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 60 | nskb->dev = p->parent->dst->master_netdev; |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 61 | dev_queue_xmit(nskb); |
| 62 | |
| 63 | return NETDEV_TX_OK; |
| 64 | } |
| 65 | |
| 66 | static int trailer_rcv(struct sk_buff *skb, struct net_device *dev, |
| 67 | struct packet_type *pt, struct net_device *orig_dev) |
| 68 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 69 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 70 | struct dsa_switch *ds; |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 71 | u8 *trailer; |
| 72 | int source_port; |
| 73 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 74 | if (unlikely(dst == NULL)) |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 75 | goto out_drop; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 76 | ds = dst->ds[0]; |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 77 | |
| 78 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 79 | if (skb == NULL) |
| 80 | goto out; |
| 81 | |
| 82 | if (skb_linearize(skb)) |
| 83 | goto out_drop; |
| 84 | |
| 85 | trailer = skb_tail_pointer(skb) - 4; |
| 86 | if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 || |
| 87 | (trailer[3] & 0xef) != 0x00 || trailer[3] != 0x00) |
| 88 | goto out_drop; |
| 89 | |
| 90 | source_port = trailer[1] & 7; |
| 91 | if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL) |
| 92 | goto out_drop; |
| 93 | |
| 94 | pskb_trim_rcsum(skb, skb->len - 4); |
| 95 | |
| 96 | skb->dev = ds->ports[source_port]; |
| 97 | skb_push(skb, ETH_HLEN); |
Lennert Buytenhek | 14ee674 | 2008-11-10 21:52:42 -0800 | [diff] [blame] | 98 | skb->pkt_type = PACKET_HOST; |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 99 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 100 | |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 101 | skb->dev->stats.rx_packets++; |
| 102 | skb->dev->stats.rx_bytes += skb->len; |
| 103 | |
| 104 | netif_receive_skb(skb); |
| 105 | |
| 106 | return 0; |
| 107 | |
| 108 | out_drop: |
| 109 | kfree_skb(skb); |
| 110 | out: |
| 111 | return 0; |
| 112 | } |
| 113 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 114 | const struct dsa_device_ops trailer_netdev_ops = { |
| 115 | .xmit = trailer_xmit, |
| 116 | .rcv = trailer_rcv, |
Lennert Buytenhek | 396138f0 | 2008-10-07 13:46:07 +0000 | [diff] [blame] | 117 | }; |