Subash Abhinov Kasiviswanathan | 2139ce8a | 2016-10-14 11:01:48 -0600 | [diff] [blame^] | 1 | /* Copyright (c) 2013-2016, 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/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/rmnet_data.h> |
| 18 | #include <linux/net_map.h> |
| 19 | #include <net/pkt_sched.h> |
| 20 | #include <net/rmnet_config.h> |
| 21 | #include "rmnet_data_config.h" |
| 22 | #include "rmnet_map.h" |
| 23 | #include "rmnet_data_private.h" |
| 24 | #include "rmnet_data_vnd.h" |
| 25 | #include "rmnet_data_stats.h" |
| 26 | |
| 27 | RMNET_LOG_MODULE(RMNET_DATA_LOGMASK_MAPC); |
| 28 | |
| 29 | unsigned long int rmnet_map_command_stats[RMNET_MAP_COMMAND_ENUM_LENGTH]; |
| 30 | module_param_array(rmnet_map_command_stats, ulong, 0, 0444); |
| 31 | MODULE_PARM_DESC(rmnet_map_command_stats, "MAP command statistics"); |
| 32 | |
| 33 | /* rmnet_map_do_flow_control() - Process MAP flow control command |
| 34 | * @skb: Socket buffer containing the MAP flow control message |
| 35 | * @config: Physical end-point configuration of ingress device |
| 36 | * @enable: boolean for enable/disable |
| 37 | * |
| 38 | * Process in-band MAP flow control messages. Assumes mux ID is mapped to a |
| 39 | * RmNet Data vitrual network device. |
| 40 | * |
| 41 | * Return: |
| 42 | * - RMNET_MAP_COMMAND_UNSUPPORTED on any error |
| 43 | * - RMNET_MAP_COMMAND_ACK on success |
| 44 | */ |
| 45 | static uint8_t rmnet_map_do_flow_control(struct sk_buff *skb, |
| 46 | struct rmnet_phys_ep_config *config, |
| 47 | int enable) |
| 48 | { |
| 49 | struct rmnet_map_control_command_s *cmd; |
| 50 | struct net_device *vnd; |
| 51 | struct rmnet_logical_ep_conf_s *ep; |
| 52 | u8 mux_id; |
| 53 | u16 ip_family; |
| 54 | u16 fc_seq; |
| 55 | u32 qos_id; |
| 56 | int r; |
| 57 | |
| 58 | if (unlikely(!skb || !config)) |
| 59 | return RX_HANDLER_CONSUMED; |
| 60 | |
| 61 | mux_id = RMNET_MAP_GET_MUX_ID(skb); |
| 62 | cmd = RMNET_MAP_GET_CMD_START(skb); |
| 63 | |
| 64 | if (mux_id >= RMNET_DATA_MAX_LOGICAL_EP) { |
| 65 | LOGD("Got packet on %s with bad mux id %d", |
| 66 | skb->dev->name, mux_id); |
| 67 | rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_BAD_MUX); |
| 68 | return RX_HANDLER_CONSUMED; |
| 69 | } |
| 70 | |
| 71 | ep = &config->muxed_ep[mux_id]; |
| 72 | |
| 73 | if (!ep->refcount) { |
| 74 | LOGD("Packet on %s:%d; has no logical endpoint config", |
| 75 | skb->dev->name, mux_id); |
| 76 | |
| 77 | rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_MUX_NO_EP); |
| 78 | return RX_HANDLER_CONSUMED; |
| 79 | } |
| 80 | |
| 81 | vnd = ep->egress_dev; |
| 82 | |
| 83 | ip_family = cmd->flow_control.ip_family; |
| 84 | fc_seq = ntohs(cmd->flow_control.flow_control_seq_num); |
| 85 | qos_id = ntohl(cmd->flow_control.qos_id); |
| 86 | |
| 87 | /* Ignore the ip family and pass the sequence number for both v4 and v6 |
| 88 | * sequence. User space does not support creating dedicated flows for |
| 89 | * the 2 protocols |
| 90 | */ |
| 91 | r = rmnet_vnd_do_flow_control(vnd, qos_id, fc_seq, fc_seq, enable); |
| 92 | LOGD("dev:%s, qos_id:0x%08X, ip_family:%hd, fc_seq %hd, en:%d", |
| 93 | skb->dev->name, qos_id, ip_family & 3, fc_seq, enable); |
| 94 | |
| 95 | if (r) { |
| 96 | rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_UNSUPPORTED); |
| 97 | return RMNET_MAP_COMMAND_UNSUPPORTED; |
| 98 | } else { |
| 99 | return RMNET_MAP_COMMAND_ACK; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* rmnet_map_send_ack() - Send N/ACK message for MAP commands |
| 104 | * @skb: Socket buffer containing the MAP command message |
| 105 | * @type: N/ACK message selector |
| 106 | * @config: Physical end-point configuration of ingress device |
| 107 | * |
| 108 | * skb is modified to contain the message type selector. The message is then |
| 109 | * transmitted on skb->dev. Note that this function grabs global Tx lock on |
| 110 | * skb->dev for latency reasons. |
| 111 | * |
| 112 | * Return: |
| 113 | * - void |
| 114 | */ |
| 115 | static void rmnet_map_send_ack(struct sk_buff *skb, |
| 116 | unsigned char type, |
| 117 | struct rmnet_phys_ep_config *config) |
| 118 | { |
| 119 | struct rmnet_map_control_command_s *cmd; |
| 120 | int xmit_status; |
| 121 | |
| 122 | if (unlikely(!skb)) |
| 123 | return; |
| 124 | |
| 125 | skb->protocol = htons(ETH_P_MAP); |
| 126 | |
| 127 | if ((config->ingress_data_format & RMNET_INGRESS_FORMAT_MAP_CKSUMV3) || |
| 128 | (config->ingress_data_format & RMNET_INGRESS_FORMAT_MAP_CKSUMV4)) { |
| 129 | if (unlikely(skb->len < (sizeof(struct rmnet_map_header_s) + |
| 130 | + RMNET_MAP_GET_LENGTH(skb) |
| 131 | + sizeof(struct rmnet_map_dl_checksum_trailer_s)))) { |
| 132 | rmnet_stats_dl_checksum( |
| 133 | RMNET_MAP_CHECKSUM_ERR_BAD_BUFFER); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | skb_trim(skb, skb->len - |
| 138 | sizeof(struct rmnet_map_dl_checksum_trailer_s)); |
| 139 | } |
| 140 | |
| 141 | cmd = RMNET_MAP_GET_CMD_START(skb); |
| 142 | cmd->cmd_type = type & 0x03; |
| 143 | |
| 144 | netif_tx_lock(skb->dev); |
| 145 | xmit_status = skb->dev->netdev_ops->ndo_start_xmit(skb, skb->dev); |
| 146 | netif_tx_unlock(skb->dev); |
| 147 | |
| 148 | LOGD("MAP command ACK=%hhu sent with rc: %d", type & 0x03, xmit_status); |
| 149 | } |
| 150 | |
| 151 | /* rmnet_map_command() - Entry point for handling MAP commands |
| 152 | * @skb: Socket buffer containing the MAP command message |
| 153 | * @config: Physical end-point configuration of ingress device |
| 154 | * |
| 155 | * Process MAP command frame and send N/ACK message as appropriate. Message cmd |
| 156 | * name is decoded here and appropriate handler is called. |
| 157 | * |
| 158 | * Return: |
| 159 | * - RX_HANDLER_CONSUMED. Command frames are always consumed. |
| 160 | */ |
| 161 | rx_handler_result_t rmnet_map_command(struct sk_buff *skb, |
| 162 | struct rmnet_phys_ep_config *config) |
| 163 | { |
| 164 | struct rmnet_map_control_command_s *cmd; |
| 165 | unsigned char command_name; |
| 166 | unsigned char rc = 0; |
| 167 | |
| 168 | if (unlikely(!skb)) |
| 169 | return RX_HANDLER_CONSUMED; |
| 170 | |
| 171 | cmd = RMNET_MAP_GET_CMD_START(skb); |
| 172 | command_name = cmd->command_name; |
| 173 | |
| 174 | if (command_name < RMNET_MAP_COMMAND_ENUM_LENGTH) |
| 175 | rmnet_map_command_stats[command_name]++; |
| 176 | |
| 177 | switch (command_name) { |
| 178 | case RMNET_MAP_COMMAND_FLOW_ENABLE: |
| 179 | rc = rmnet_map_do_flow_control(skb, config, 1); |
| 180 | break; |
| 181 | |
| 182 | case RMNET_MAP_COMMAND_FLOW_DISABLE: |
| 183 | rc = rmnet_map_do_flow_control(skb, config, 0); |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | rmnet_map_command_stats[RMNET_MAP_COMMAND_UNKNOWN]++; |
| 188 | LOGM("Uknown MAP command: %d", command_name); |
| 189 | rc = RMNET_MAP_COMMAND_UNSUPPORTED; |
| 190 | rmnet_kfree_skb(skb, RMNET_STATS_SKBFREE_MAPC_UNSUPPORTED); |
| 191 | break; |
| 192 | } |
| 193 | if (rc == RMNET_MAP_COMMAND_ACK) |
| 194 | rmnet_map_send_ack(skb, rc, config); |
| 195 | return RX_HANDLER_CONSUMED; |
| 196 | } |