blob: a85c829853c00ec9e1ec4aeee2f05013d5c73c0b [file] [log] [blame]
Lennert Buytenhek396138f02008-10-07 13:46:07 +00001/*
2 * net/dsa/tag_trailer.c - Trailer tag format handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek396138f02008-10-07 13:46:07 +00004 *
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>
13#include <linux/netdevice.h>
14#include "dsa_priv.h"
15
Stephen Hemminger6fef4c02009-08-31 19:50:41 +000016netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
Lennert Buytenhek396138f02008-10-07 13:46:07 +000017{
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
60 nskb->protocol = htons(ETH_P_TRAILER);
61
Lennert Buytenheke84665c2009-03-20 09:52:09 +000062 nskb->dev = p->parent->dst->master_netdev;
Lennert Buytenhek396138f02008-10-07 13:46:07 +000063 dev_queue_xmit(nskb);
64
65 return NETDEV_TX_OK;
66}
67
68static int trailer_rcv(struct sk_buff *skb, struct net_device *dev,
69 struct packet_type *pt, struct net_device *orig_dev)
70{
Lennert Buytenheke84665c2009-03-20 09:52:09 +000071 struct dsa_switch_tree *dst = dev->dsa_ptr;
72 struct dsa_switch *ds;
Lennert Buytenhek396138f02008-10-07 13:46:07 +000073 u8 *trailer;
74 int source_port;
75
Lennert Buytenheke84665c2009-03-20 09:52:09 +000076 if (unlikely(dst == NULL))
Lennert Buytenhek396138f02008-10-07 13:46:07 +000077 goto out_drop;
Lennert Buytenheke84665c2009-03-20 09:52:09 +000078 ds = dst->ds[0];
Lennert Buytenhek396138f02008-10-07 13:46:07 +000079
80 skb = skb_unshare(skb, GFP_ATOMIC);
81 if (skb == NULL)
82 goto out;
83
84 if (skb_linearize(skb))
85 goto out_drop;
86
87 trailer = skb_tail_pointer(skb) - 4;
88 if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 ||
89 (trailer[3] & 0xef) != 0x00 || trailer[3] != 0x00)
90 goto out_drop;
91
92 source_port = trailer[1] & 7;
93 if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
94 goto out_drop;
95
96 pskb_trim_rcsum(skb, skb->len - 4);
97
98 skb->dev = ds->ports[source_port];
99 skb_push(skb, ETH_HLEN);
Lennert Buytenhek14ee6742008-11-10 21:52:42 -0800100 skb->pkt_type = PACKET_HOST;
Lennert Buytenhek396138f02008-10-07 13:46:07 +0000101 skb->protocol = eth_type_trans(skb, skb->dev);
102
Lennert Buytenhek396138f02008-10-07 13:46:07 +0000103 skb->dev->stats.rx_packets++;
104 skb->dev->stats.rx_bytes += skb->len;
105
106 netif_receive_skb(skb);
107
108 return 0;
109
110out_drop:
111 kfree_skb(skb);
112out:
113 return 0;
114}
115
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000116static struct packet_type trailer_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800117 .type = cpu_to_be16(ETH_P_TRAILER),
Lennert Buytenhek396138f02008-10-07 13:46:07 +0000118 .func = trailer_rcv,
119};
120
121static int __init trailer_init_module(void)
122{
123 dev_add_pack(&trailer_packet_type);
124 return 0;
125}
126module_init(trailer_init_module);
127
128static void __exit trailer_cleanup_module(void)
129{
130 dev_remove_pack(&trailer_packet_type);
131}
132module_exit(trailer_cleanup_module);