• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * net/dsa/tag_trailer.c - Trailer tag format handling
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6 
7 #include <linux/etherdevice.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 
11 #include "dsa_priv.h"
12 
trailer_xmit(struct sk_buff * skb,struct net_device * dev)13 static struct sk_buff *trailer_xmit(struct sk_buff *skb, struct net_device *dev)
14 {
15 	struct dsa_port *dp = dsa_slave_to_port(dev);
16 	struct sk_buff *nskb;
17 	int padlen;
18 	u8 *trailer;
19 
20 	/*
21 	 * We have to make sure that the trailer ends up as the very
22 	 * last 4 bytes of the packet.  This means that we have to pad
23 	 * the packet to the minimum ethernet frame size, if necessary,
24 	 * before adding the trailer.
25 	 */
26 	padlen = 0;
27 	if (skb->len < 60)
28 		padlen = 60 - skb->len;
29 
30 	nskb = alloc_skb(NET_IP_ALIGN + skb->len + padlen + 4, GFP_ATOMIC);
31 	if (!nskb)
32 		return NULL;
33 	skb_reserve(nskb, NET_IP_ALIGN);
34 
35 	skb_reset_mac_header(nskb);
36 	skb_set_network_header(nskb, skb_network_header(skb) - skb->head);
37 	skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head);
38 	skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
39 	consume_skb(skb);
40 
41 	if (padlen) {
42 		skb_put_zero(nskb, padlen);
43 	}
44 
45 	trailer = skb_put(nskb, 4);
46 	trailer[0] = 0x80;
47 	trailer[1] = 1 << dp->index;
48 	trailer[2] = 0x10;
49 	trailer[3] = 0x00;
50 
51 	return nskb;
52 }
53 
trailer_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt)54 static struct sk_buff *trailer_rcv(struct sk_buff *skb, struct net_device *dev,
55 				   struct packet_type *pt)
56 {
57 	u8 *trailer;
58 	int source_port;
59 
60 	if (skb_linearize(skb))
61 		return NULL;
62 
63 	trailer = skb_tail_pointer(skb) - 4;
64 	if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 ||
65 	    (trailer[2] & 0xef) != 0x00 || trailer[3] != 0x00)
66 		return NULL;
67 
68 	source_port = trailer[1] & 7;
69 
70 	skb->dev = dsa_master_find_slave(dev, 0, source_port);
71 	if (!skb->dev)
72 		return NULL;
73 
74 	if (pskb_trim_rcsum(skb, skb->len - 4))
75 		return NULL;
76 
77 	return skb;
78 }
79 
80 static const struct dsa_device_ops trailer_netdev_ops = {
81 	.name	= "trailer",
82 	.proto	= DSA_TAG_PROTO_TRAILER,
83 	.xmit	= trailer_xmit,
84 	.rcv	= trailer_rcv,
85 	.overhead = 4,
86 };
87 
88 MODULE_LICENSE("GPL");
89 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_TRAILER);
90 
91 module_dsa_tag_driver(trailer_netdev_ops);
92