rmnet_handlers.c 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * RMNET Data ingress/egress handler
 *
 */

#include <linux/netdevice.h>
#include <linux/netdev_features.h>
18
#include <linux/if_arp.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "rmnet_private.h"
#include "rmnet_config.h"
#include "rmnet_vnd.h"
#include "rmnet_map.h"
#include "rmnet_handlers.h"

#define RMNET_IP_VERSION_4 0x40
#define RMNET_IP_VERSION_6 0x60

/* Helper Functions */

static void rmnet_set_skb_proto(struct sk_buff *skb)
{
	switch (skb->data[0] & 0xF0) {
	case RMNET_IP_VERSION_4:
		skb->protocol = htons(ETH_P_IP);
		break;
	case RMNET_IP_VERSION_6:
		skb->protocol = htons(ETH_P_IPV6);
		break;
	default:
		skb->protocol = htons(ETH_P_MAP);
		break;
	}
}

/* Generic handler */

47
static void
48
rmnet_deliver_skb(struct sk_buff *skb)
49
{
50 51
	struct rmnet_priv *priv = netdev_priv(skb->dev);

52 53 54
	skb_reset_transport_header(skb);
	skb_reset_network_header(skb);
	rmnet_vnd_rx_fixup(skb, skb->dev);
55

56 57
	skb->pkt_type = PACKET_HOST;
	skb_set_mac_header(skb, 0);
58
	gro_cells_receive(&priv->gro_cells, skb);
59 60 61 62
}

/* MAP handler */

63
static void
64
__rmnet_map_ingress_handler(struct sk_buff *skb,
65
			    struct rmnet_port *port)
66 67 68 69 70 71
{
	struct rmnet_endpoint *ep;
	u8 mux_id;
	u16 len;

	if (RMNET_MAP_GET_CD_BIT(skb)) {
72
		if (port->data_format & RMNET_INGRESS_FORMAT_MAP_COMMANDS)
73
			return rmnet_map_command(skb, port);
74

75
		goto free_skb;
76 77 78 79 80
	}

	mux_id = RMNET_MAP_GET_MUX_ID(skb);
	len = RMNET_MAP_GET_LENGTH(skb) - RMNET_MAP_GET_PAD(skb);

81 82
	if (mux_id >= RMNET_MAX_LOGICAL_EP)
		goto free_skb;
83

84 85 86
	ep = rmnet_get_endpoint(port, mux_id);
	if (!ep)
		goto free_skb;
87

88
	skb->dev = ep->egress_dev;
89 90 91 92 93

	/* Subtract MAP header */
	skb_pull(skb, sizeof(struct rmnet_map_header));
	skb_trim(skb, len);
	rmnet_set_skb_proto(skb);
94 95
	rmnet_deliver_skb(skb);
	return;
96 97 98

free_skb:
	kfree_skb(skb);
99 100
}

101
static void
102
rmnet_map_ingress_handler(struct sk_buff *skb,
103
			  struct rmnet_port *port)
104 105 106
{
	struct sk_buff *skbn;

107 108 109 110 111 112 113 114 115
	if (skb->dev->type == ARPHRD_ETHER) {
		if (pskb_expand_head(skb, ETH_HLEN, 0, GFP_KERNEL)) {
			kfree_skb(skb);
			return;
		}

		skb_push(skb, ETH_HLEN);
	}

116
	if (port->data_format & RMNET_INGRESS_FORMAT_DEAGGREGATION) {
117 118
		while ((skbn = rmnet_map_deaggregate(skb)) != NULL)
			__rmnet_map_ingress_handler(skbn, port);
119 120 121

		consume_skb(skb);
	} else {
122
		__rmnet_map_ingress_handler(skb, port);
123 124 125 126
	}
}

static int rmnet_map_egress_handler(struct sk_buff *skb,
127
				    struct rmnet_port *port, u8 mux_id,
128 129 130 131 132 133 134 135 136 137
				    struct net_device *orig_dev)
{
	int required_headroom, additional_header_len;
	struct rmnet_map_header *map_header;

	additional_header_len = 0;
	required_headroom = sizeof(struct rmnet_map_header);

	if (skb_headroom(skb) < required_headroom) {
		if (pskb_expand_head(skb, required_headroom, 0, GFP_KERNEL))
138
			goto fail;
139 140 141 142
	}

	map_header = rmnet_map_add_map_header(skb, additional_header_len, 0);
	if (!map_header)
143
		goto fail;
144

145
	map_header->mux_id = mux_id;
146 147 148

	skb->protocol = htons(ETH_P_MAP);

149
	return 0;
150 151 152

fail:
	kfree_skb(skb);
153
	return -ENOMEM;
154 155
}

156
static void
157 158 159 160 161 162 163 164
rmnet_bridge_handler(struct sk_buff *skb, struct net_device *bridge_dev)
{
	if (bridge_dev) {
		skb->dev = bridge_dev;
		dev_queue_xmit(skb);
	}
}

165 166 167 168 169 170 171 172 173
/* Ingress / Egress Entry Points */

/* Processes packet as per ingress data format for receiving device. Logical
 * endpoint is determined from packet inspection. Packet is then sent to the
 * egress device listed in the logical endpoint configuration.
 */
rx_handler_result_t rmnet_rx_handler(struct sk_buff **pskb)
{
	struct sk_buff *skb = *pskb;
174
	struct rmnet_port *port;
175 176 177
	struct net_device *dev;

	if (!skb)
178
		goto done;
179 180

	dev = skb->dev;
181
	port = rmnet_get_port(dev);
182

183 184
	switch (port->rmnet_mode) {
	case RMNET_EPMODE_VND:
185
		rmnet_map_ingress_handler(skb, port);
186 187
		break;
	case RMNET_EPMODE_BRIDGE:
188
		rmnet_bridge_handler(skb, port->bridge_ep);
189 190
		break;
	}
191

192 193
done:
	return RX_HANDLER_CONSUMED;
194 195 196 197 198 199
}

/* Modifies packet as per logical endpoint configuration and egress data format
 * for egress device configured in logical endpoint. Packet is then transmitted
 * on the egress device.
 */
200
void rmnet_egress_handler(struct sk_buff *skb)
201 202
{
	struct net_device *orig_dev;
203
	struct rmnet_port *port;
204 205
	struct rmnet_priv *priv;
	u8 mux_id;
206 207

	orig_dev = skb->dev;
208 209 210
	priv = netdev_priv(orig_dev);
	skb->dev = priv->real_dev;
	mux_id = priv->mux_id;
211

212 213
	port = rmnet_get_port(skb->dev);
	if (!port) {
214 215 216 217
		kfree_skb(skb);
		return;
	}

218 219
	if (rmnet_map_egress_handler(skb, port, mux_id, orig_dev))
		return;
220

221
	rmnet_vnd_tx_fixup(skb, orig_dev);
222 223 224

	dev_queue_xmit(skb);
}