1 /* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * RMNET Data ingress/egress handler
13  *
14  */
15 
16 #include <linux/netdevice.h>
17 #include <linux/netdev_features.h>
18 #include "rmnet_private.h"
19 #include "rmnet_config.h"
20 #include "rmnet_vnd.h"
21 #include "rmnet_map.h"
22 #include "rmnet_handlers.h"
23 
24 #define RMNET_IP_VERSION_4 0x40
25 #define RMNET_IP_VERSION_6 0x60
26 
27 /* Helper Functions */
28 
29 static void rmnet_set_skb_proto(struct sk_buff *skb)
30 {
31 	switch (skb->data[0] & 0xF0) {
32 	case RMNET_IP_VERSION_4:
33 		skb->protocol = htons(ETH_P_IP);
34 		break;
35 	case RMNET_IP_VERSION_6:
36 		skb->protocol = htons(ETH_P_IPV6);
37 		break;
38 	default:
39 		skb->protocol = htons(ETH_P_MAP);
40 		break;
41 	}
42 }
43 
44 /* Generic handler */
45 
46 static void
47 rmnet_deliver_skb(struct sk_buff *skb)
48 {
49 	struct rmnet_priv *priv = netdev_priv(skb->dev);
50 
51 	skb_reset_transport_header(skb);
52 	skb_reset_network_header(skb);
53 	rmnet_vnd_rx_fixup(skb, skb->dev);
54 
55 	skb->pkt_type = PACKET_HOST;
56 	skb_set_mac_header(skb, 0);
57 	gro_cells_receive(&priv->gro_cells, skb);
58 }
59 
60 /* MAP handler */
61 
62 static void
63 __rmnet_map_ingress_handler(struct sk_buff *skb,
64 			    struct rmnet_port *port)
65 {
66 	struct rmnet_endpoint *ep;
67 	u8 mux_id;
68 	u16 len;
69 
70 	if (RMNET_MAP_GET_CD_BIT(skb)) {
71 		if (port->ingress_data_format
72 		    & RMNET_INGRESS_FORMAT_MAP_COMMANDS)
73 			return rmnet_map_command(skb, port);
74 
75 		goto free_skb;
76 	}
77 
78 	mux_id = RMNET_MAP_GET_MUX_ID(skb);
79 	len = RMNET_MAP_GET_LENGTH(skb) - RMNET_MAP_GET_PAD(skb);
80 
81 	if (mux_id >= RMNET_MAX_LOGICAL_EP)
82 		goto free_skb;
83 
84 	ep = rmnet_get_endpoint(port, mux_id);
85 	if (!ep)
86 		goto free_skb;
87 
88 	skb->dev = ep->egress_dev;
89 
90 	/* Subtract MAP header */
91 	skb_pull(skb, sizeof(struct rmnet_map_header));
92 	skb_trim(skb, len);
93 	rmnet_set_skb_proto(skb);
94 	rmnet_deliver_skb(skb);
95 	return;
96 
97 free_skb:
98 	kfree_skb(skb);
99 }
100 
101 static void
102 rmnet_map_ingress_handler(struct sk_buff *skb,
103 			  struct rmnet_port *port)
104 {
105 	struct sk_buff *skbn;
106 
107 	if (port->ingress_data_format & RMNET_INGRESS_FORMAT_DEAGGREGATION) {
108 		while ((skbn = rmnet_map_deaggregate(skb)) != NULL)
109 			__rmnet_map_ingress_handler(skbn, port);
110 
111 		consume_skb(skb);
112 	} else {
113 		__rmnet_map_ingress_handler(skb, port);
114 	}
115 }
116 
117 static int rmnet_map_egress_handler(struct sk_buff *skb,
118 				    struct rmnet_port *port, u8 mux_id,
119 				    struct net_device *orig_dev)
120 {
121 	int required_headroom, additional_header_len;
122 	struct rmnet_map_header *map_header;
123 
124 	additional_header_len = 0;
125 	required_headroom = sizeof(struct rmnet_map_header);
126 
127 	if (skb_headroom(skb) < required_headroom) {
128 		if (pskb_expand_head(skb, required_headroom, 0, GFP_KERNEL))
129 			return RMNET_MAP_CONSUMED;
130 	}
131 
132 	map_header = rmnet_map_add_map_header(skb, additional_header_len, 0);
133 	if (!map_header)
134 		return RMNET_MAP_CONSUMED;
135 
136 	if (port->egress_data_format & RMNET_EGRESS_FORMAT_MUXING) {
137 		if (mux_id == 0xff)
138 			map_header->mux_id = 0;
139 		else
140 			map_header->mux_id = mux_id;
141 	}
142 
143 	skb->protocol = htons(ETH_P_MAP);
144 
145 	return RMNET_MAP_SUCCESS;
146 }
147 
148 static void
149 rmnet_bridge_handler(struct sk_buff *skb, struct net_device *bridge_dev)
150 {
151 	if (bridge_dev) {
152 		skb->dev = bridge_dev;
153 		dev_queue_xmit(skb);
154 	}
155 }
156 
157 /* Ingress / Egress Entry Points */
158 
159 /* Processes packet as per ingress data format for receiving device. Logical
160  * endpoint is determined from packet inspection. Packet is then sent to the
161  * egress device listed in the logical endpoint configuration.
162  */
163 rx_handler_result_t rmnet_rx_handler(struct sk_buff **pskb)
164 {
165 	struct sk_buff *skb = *pskb;
166 	struct rmnet_port *port;
167 	struct net_device *dev;
168 
169 	if (!skb)
170 		goto done;
171 
172 	dev = skb->dev;
173 	port = rmnet_get_port(dev);
174 
175 	switch (port->rmnet_mode) {
176 	case RMNET_EPMODE_VND:
177 		if (port->ingress_data_format & RMNET_INGRESS_FORMAT_MAP)
178 			rmnet_map_ingress_handler(skb, port);
179 		break;
180 	case RMNET_EPMODE_BRIDGE:
181 		rmnet_bridge_handler(skb, port->bridge_ep);
182 		break;
183 	}
184 
185 done:
186 	return RX_HANDLER_CONSUMED;
187 }
188 
189 /* Modifies packet as per logical endpoint configuration and egress data format
190  * for egress device configured in logical endpoint. Packet is then transmitted
191  * on the egress device.
192  */
193 void rmnet_egress_handler(struct sk_buff *skb)
194 {
195 	struct net_device *orig_dev;
196 	struct rmnet_port *port;
197 	struct rmnet_priv *priv;
198 	u8 mux_id;
199 
200 	orig_dev = skb->dev;
201 	priv = netdev_priv(orig_dev);
202 	skb->dev = priv->real_dev;
203 	mux_id = priv->mux_id;
204 
205 	port = rmnet_get_port(skb->dev);
206 	if (!port) {
207 		kfree_skb(skb);
208 		return;
209 	}
210 
211 	if (port->egress_data_format & RMNET_EGRESS_FORMAT_MAP) {
212 		switch (rmnet_map_egress_handler(skb, port, mux_id, orig_dev)) {
213 		case RMNET_MAP_CONSUMED:
214 			return;
215 
216 		case RMNET_MAP_SUCCESS:
217 			break;
218 
219 		default:
220 			kfree_skb(skb);
221 			return;
222 		}
223 	}
224 
225 	rmnet_vnd_tx_fixup(skb, orig_dev);
226 
227 	dev_queue_xmit(skb);
228 }
229