1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
3  *
4  * RMNET Data ingress/egress handler
5  */
6 
7 #include <linux/netdevice.h>
8 #include <linux/netdev_features.h>
9 #include <linux/if_arp.h>
10 #include <net/sock.h>
11 #include "rmnet_private.h"
12 #include "rmnet_config.h"
13 #include "rmnet_vnd.h"
14 #include "rmnet_map.h"
15 #include "rmnet_handlers.h"
16 
17 #define RMNET_IP_VERSION_4 0x40
18 #define RMNET_IP_VERSION_6 0x60
19 
20 /* Helper Functions */
21 
22 static void rmnet_set_skb_proto(struct sk_buff *skb)
23 {
24 	switch (skb->data[0] & 0xF0) {
25 	case RMNET_IP_VERSION_4:
26 		skb->protocol = htons(ETH_P_IP);
27 		break;
28 	case RMNET_IP_VERSION_6:
29 		skb->protocol = htons(ETH_P_IPV6);
30 		break;
31 	default:
32 		skb->protocol = htons(ETH_P_MAP);
33 		break;
34 	}
35 }
36 
37 /* Generic handler */
38 
39 static void
40 rmnet_deliver_skb(struct sk_buff *skb)
41 {
42 	struct rmnet_priv *priv = netdev_priv(skb->dev);
43 
44 	skb_reset_transport_header(skb);
45 	skb_reset_network_header(skb);
46 	rmnet_vnd_rx_fixup(skb, skb->dev);
47 
48 	skb->pkt_type = PACKET_HOST;
49 	skb_set_mac_header(skb, 0);
50 	gro_cells_receive(&priv->gro_cells, skb);
51 }
52 
53 /* MAP handler */
54 
55 static void
56 __rmnet_map_ingress_handler(struct sk_buff *skb,
57 			    struct rmnet_port *port)
58 {
59 	struct rmnet_map_header *map_header = (void *)skb->data;
60 	struct rmnet_endpoint *ep;
61 	u16 len, pad;
62 	u8 mux_id;
63 
64 	if (map_header->flags & MAP_CMD_FLAG) {
65 		/* Packet contains a MAP command (not data) */
66 		if (port->data_format & RMNET_FLAGS_INGRESS_MAP_COMMANDS)
67 			return rmnet_map_command(skb, port);
68 
69 		goto free_skb;
70 	}
71 
72 	mux_id = map_header->mux_id;
73 	pad = map_header->flags & MAP_PAD_LEN_MASK;
74 	len = ntohs(map_header->pkt_len) - pad;
75 
76 	if (mux_id >= RMNET_MAX_LOGICAL_EP)
77 		goto free_skb;
78 
79 	ep = rmnet_get_endpoint(port, mux_id);
80 	if (!ep)
81 		goto free_skb;
82 
83 	skb->dev = ep->egress_dev;
84 
85 	/* Subtract MAP header */
86 	skb_pull(skb, sizeof(struct rmnet_map_header));
87 	rmnet_set_skb_proto(skb);
88 
89 	if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
90 		if (!rmnet_map_checksum_downlink_packet(skb, len + pad))
91 			skb->ip_summed = CHECKSUM_UNNECESSARY;
92 	}
93 
94 	skb_trim(skb, len);
95 	rmnet_deliver_skb(skb);
96 	return;
97 
98 free_skb:
99 	kfree_skb(skb);
100 }
101 
102 static void
103 rmnet_map_ingress_handler(struct sk_buff *skb,
104 			  struct rmnet_port *port)
105 {
106 	struct sk_buff *skbn;
107 
108 	if (skb->dev->type == ARPHRD_ETHER) {
109 		if (pskb_expand_head(skb, ETH_HLEN, 0, GFP_ATOMIC)) {
110 			kfree_skb(skb);
111 			return;
112 		}
113 
114 		skb_push(skb, ETH_HLEN);
115 	}
116 
117 	if (port->data_format & RMNET_FLAGS_INGRESS_DEAGGREGATION) {
118 		while ((skbn = rmnet_map_deaggregate(skb, port)) != NULL)
119 			__rmnet_map_ingress_handler(skbn, port);
120 
121 		consume_skb(skb);
122 	} else {
123 		__rmnet_map_ingress_handler(skb, port);
124 	}
125 }
126 
127 static int rmnet_map_egress_handler(struct sk_buff *skb,
128 				    struct rmnet_port *port, u8 mux_id,
129 				    struct net_device *orig_dev)
130 {
131 	int required_headroom, additional_header_len;
132 	struct rmnet_map_header *map_header;
133 
134 	additional_header_len = 0;
135 	required_headroom = sizeof(struct rmnet_map_header);
136 
137 	if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV4) {
138 		additional_header_len = sizeof(struct rmnet_map_ul_csum_header);
139 		required_headroom += additional_header_len;
140 	}
141 
142 	if (skb_headroom(skb) < required_headroom) {
143 		if (pskb_expand_head(skb, required_headroom, 0, GFP_ATOMIC))
144 			return -ENOMEM;
145 	}
146 
147 	if (port->data_format & RMNET_FLAGS_EGRESS_MAP_CKSUMV4)
148 		rmnet_map_checksum_uplink_packet(skb, orig_dev);
149 
150 	map_header = rmnet_map_add_map_header(skb, additional_header_len, 0);
151 	if (!map_header)
152 		return -ENOMEM;
153 
154 	map_header->mux_id = mux_id;
155 
156 	skb->protocol = htons(ETH_P_MAP);
157 
158 	return 0;
159 }
160 
161 static void
162 rmnet_bridge_handler(struct sk_buff *skb, struct net_device *bridge_dev)
163 {
164 	if (skb_mac_header_was_set(skb))
165 		skb_push(skb, skb->mac_len);
166 
167 	if (bridge_dev) {
168 		skb->dev = bridge_dev;
169 		dev_queue_xmit(skb);
170 	}
171 }
172 
173 /* Ingress / Egress Entry Points */
174 
175 /* Processes packet as per ingress data format for receiving device. Logical
176  * endpoint is determined from packet inspection. Packet is then sent to the
177  * egress device listed in the logical endpoint configuration.
178  */
179 rx_handler_result_t rmnet_rx_handler(struct sk_buff **pskb)
180 {
181 	struct sk_buff *skb = *pskb;
182 	struct rmnet_port *port;
183 	struct net_device *dev;
184 
185 	if (!skb)
186 		goto done;
187 
188 	if (skb_linearize(skb)) {
189 		kfree_skb(skb);
190 		goto done;
191 	}
192 
193 	if (skb->pkt_type == PACKET_LOOPBACK)
194 		return RX_HANDLER_PASS;
195 
196 	dev = skb->dev;
197 	port = rmnet_get_port_rcu(dev);
198 	if (unlikely(!port)) {
199 		atomic_long_inc(&skb->dev->rx_nohandler);
200 		kfree_skb(skb);
201 		goto done;
202 	}
203 
204 	switch (port->rmnet_mode) {
205 	case RMNET_EPMODE_VND:
206 		rmnet_map_ingress_handler(skb, port);
207 		break;
208 	case RMNET_EPMODE_BRIDGE:
209 		rmnet_bridge_handler(skb, port->bridge_ep);
210 		break;
211 	}
212 
213 done:
214 	return RX_HANDLER_CONSUMED;
215 }
216 
217 /* Modifies packet as per logical endpoint configuration and egress data format
218  * for egress device configured in logical endpoint. Packet is then transmitted
219  * on the egress device.
220  */
221 void rmnet_egress_handler(struct sk_buff *skb)
222 {
223 	struct net_device *orig_dev;
224 	struct rmnet_port *port;
225 	struct rmnet_priv *priv;
226 	u8 mux_id;
227 
228 	sk_pacing_shift_update(skb->sk, 8);
229 
230 	orig_dev = skb->dev;
231 	priv = netdev_priv(orig_dev);
232 	skb->dev = priv->real_dev;
233 	mux_id = priv->mux_id;
234 
235 	port = rmnet_get_port_rcu(skb->dev);
236 	if (!port)
237 		goto drop;
238 
239 	if (rmnet_map_egress_handler(skb, port, mux_id, orig_dev))
240 		goto drop;
241 
242 	rmnet_vnd_tx_fixup(skb, orig_dev);
243 
244 	dev_queue_xmit(skb);
245 	return;
246 
247 drop:
248 	this_cpu_inc(priv->pcpu_stats->stats.tx_drops);
249 	kfree_skb(skb);
250 }
251