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 
13 #include <linux/netdevice.h>
14 #include "rmnet_config.h"
15 #include "rmnet_map.h"
16 #include "rmnet_private.h"
17 #include "rmnet_vnd.h"
18 
19 static u8 rmnet_map_do_flow_control(struct sk_buff *skb,
20 				    struct rmnet_real_dev_info *rdinfo,
21 				    int enable)
22 {
23 	struct rmnet_map_control_command *cmd;
24 	struct rmnet_endpoint *ep;
25 	struct net_device *vnd;
26 	u16 ip_family;
27 	u16 fc_seq;
28 	u32 qos_id;
29 	u8 mux_id;
30 	int r;
31 
32 	mux_id = RMNET_MAP_GET_MUX_ID(skb);
33 	cmd = RMNET_MAP_GET_CMD_START(skb);
34 
35 	if (mux_id >= RMNET_MAX_LOGICAL_EP) {
36 		kfree_skb(skb);
37 		return RX_HANDLER_CONSUMED;
38 	}
39 
40 	ep = &rdinfo->muxed_ep[mux_id];
41 	vnd = ep->egress_dev;
42 
43 	ip_family = cmd->flow_control.ip_family;
44 	fc_seq = ntohs(cmd->flow_control.flow_control_seq_num);
45 	qos_id = ntohl(cmd->flow_control.qos_id);
46 
47 	/* Ignore the ip family and pass the sequence number for both v4 and v6
48 	 * sequence. User space does not support creating dedicated flows for
49 	 * the 2 protocols
50 	 */
51 	r = rmnet_vnd_do_flow_control(vnd, enable);
52 	if (r) {
53 		kfree_skb(skb);
54 		return RMNET_MAP_COMMAND_UNSUPPORTED;
55 	} else {
56 		return RMNET_MAP_COMMAND_ACK;
57 	}
58 }
59 
60 static void rmnet_map_send_ack(struct sk_buff *skb,
61 			       unsigned char type,
62 			       struct rmnet_real_dev_info *rdinfo)
63 {
64 	struct rmnet_map_control_command *cmd;
65 	int xmit_status;
66 
67 	skb->protocol = htons(ETH_P_MAP);
68 
69 	cmd = RMNET_MAP_GET_CMD_START(skb);
70 	cmd->cmd_type = type & 0x03;
71 
72 	netif_tx_lock(skb->dev);
73 	xmit_status = skb->dev->netdev_ops->ndo_start_xmit(skb, skb->dev);
74 	netif_tx_unlock(skb->dev);
75 }
76 
77 /* Process MAP command frame and send N/ACK message as appropriate. Message cmd
78  * name is decoded here and appropriate handler is called.
79  */
80 rx_handler_result_t rmnet_map_command(struct sk_buff *skb,
81 				      struct rmnet_real_dev_info *rdinfo)
82 {
83 	struct rmnet_map_control_command *cmd;
84 	unsigned char command_name;
85 	unsigned char rc = 0;
86 
87 	cmd = RMNET_MAP_GET_CMD_START(skb);
88 	command_name = cmd->command_name;
89 
90 	switch (command_name) {
91 	case RMNET_MAP_COMMAND_FLOW_ENABLE:
92 		rc = rmnet_map_do_flow_control(skb, rdinfo, 1);
93 		break;
94 
95 	case RMNET_MAP_COMMAND_FLOW_DISABLE:
96 		rc = rmnet_map_do_flow_control(skb, rdinfo, 0);
97 		break;
98 
99 	default:
100 		rc = RMNET_MAP_COMMAND_UNSUPPORTED;
101 		kfree_skb(skb);
102 		break;
103 	}
104 	if (rc == RMNET_MAP_COMMAND_ACK)
105 		rmnet_map_send_ack(skb, rc, rdinfo);
106 	return RX_HANDLER_CONSUMED;
107 }
108