Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 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 rx_handler_result_t |
Subash Abhinov Kasiviswanathan | d8bbb07 | 2017-10-11 18:43:52 -0600 | [diff] [blame] | 47 | rmnet_deliver_skb(struct sk_buff *skb) |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 48 | { |
Subash Abhinov Kasiviswanathan | d8bbb07 | 2017-10-11 18:43:52 -0600 | [diff] [blame] | 49 | skb_reset_transport_header(skb); |
| 50 | skb_reset_network_header(skb); |
| 51 | rmnet_vnd_rx_fixup(skb, skb->dev); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 52 | |
Subash Abhinov Kasiviswanathan | d8bbb07 | 2017-10-11 18:43:52 -0600 | [diff] [blame] | 53 | skb->pkt_type = PACKET_HOST; |
| 54 | skb_set_mac_header(skb, 0); |
| 55 | netif_receive_skb(skb); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 56 | return RX_HANDLER_CONSUMED; |
| 57 | } |
| 58 | |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 59 | /* MAP handler */ |
| 60 | |
| 61 | static rx_handler_result_t |
| 62 | __rmnet_map_ingress_handler(struct sk_buff *skb, |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 63 | struct rmnet_port *port) |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 64 | { |
| 65 | struct rmnet_endpoint *ep; |
| 66 | u8 mux_id; |
| 67 | u16 len; |
| 68 | |
| 69 | if (RMNET_MAP_GET_CD_BIT(skb)) { |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 70 | if (port->ingress_data_format |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 71 | & RMNET_INGRESS_FORMAT_MAP_COMMANDS) |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 72 | return rmnet_map_command(skb, port); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 73 | |
| 74 | kfree_skb(skb); |
| 75 | return RX_HANDLER_CONSUMED; |
| 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 | kfree_skb(skb); |
| 83 | return RX_HANDLER_CONSUMED; |
| 84 | } |
| 85 | |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 86 | ep = &port->muxed_ep[mux_id]; |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 87 | |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 88 | if (port->ingress_data_format & RMNET_INGRESS_FORMAT_DEMUXING) |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 89 | skb->dev = ep->egress_dev; |
| 90 | |
| 91 | /* Subtract MAP header */ |
| 92 | skb_pull(skb, sizeof(struct rmnet_map_header)); |
| 93 | skb_trim(skb, len); |
| 94 | rmnet_set_skb_proto(skb); |
Subash Abhinov Kasiviswanathan | d8bbb07 | 2017-10-11 18:43:52 -0600 | [diff] [blame] | 95 | return rmnet_deliver_skb(skb); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static rx_handler_result_t |
| 99 | rmnet_map_ingress_handler(struct sk_buff *skb, |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 100 | struct rmnet_port *port) |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 101 | { |
| 102 | struct sk_buff *skbn; |
| 103 | int rc; |
| 104 | |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 105 | if (port->ingress_data_format & RMNET_INGRESS_FORMAT_DEAGGREGATION) { |
| 106 | while ((skbn = rmnet_map_deaggregate(skb)) != NULL) |
| 107 | __rmnet_map_ingress_handler(skbn, port); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 108 | |
| 109 | consume_skb(skb); |
| 110 | rc = RX_HANDLER_CONSUMED; |
| 111 | } else { |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 112 | rc = __rmnet_map_ingress_handler(skb, port); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return rc; |
| 116 | } |
| 117 | |
| 118 | static int rmnet_map_egress_handler(struct sk_buff *skb, |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 119 | struct rmnet_port *port, u8 mux_id, |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 120 | struct net_device *orig_dev) |
| 121 | { |
| 122 | int required_headroom, additional_header_len; |
| 123 | struct rmnet_map_header *map_header; |
| 124 | |
| 125 | additional_header_len = 0; |
| 126 | required_headroom = sizeof(struct rmnet_map_header); |
| 127 | |
| 128 | if (skb_headroom(skb) < required_headroom) { |
| 129 | if (pskb_expand_head(skb, required_headroom, 0, GFP_KERNEL)) |
| 130 | return RMNET_MAP_CONSUMED; |
| 131 | } |
| 132 | |
| 133 | map_header = rmnet_map_add_map_header(skb, additional_header_len, 0); |
| 134 | if (!map_header) |
| 135 | return RMNET_MAP_CONSUMED; |
| 136 | |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 137 | if (port->egress_data_format & RMNET_EGRESS_FORMAT_MUXING) { |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 138 | if (mux_id == 0xff) |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 139 | map_header->mux_id = 0; |
| 140 | else |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 141 | map_header->mux_id = mux_id; |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | skb->protocol = htons(ETH_P_MAP); |
| 145 | |
| 146 | return RMNET_MAP_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | /* Ingress / Egress Entry Points */ |
| 150 | |
| 151 | /* Processes packet as per ingress data format for receiving device. Logical |
| 152 | * endpoint is determined from packet inspection. Packet is then sent to the |
| 153 | * egress device listed in the logical endpoint configuration. |
| 154 | */ |
| 155 | rx_handler_result_t rmnet_rx_handler(struct sk_buff **pskb) |
| 156 | { |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 157 | struct rmnet_port *port; |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 158 | struct sk_buff *skb = *pskb; |
| 159 | struct net_device *dev; |
| 160 | int rc; |
| 161 | |
| 162 | if (!skb) |
| 163 | return RX_HANDLER_CONSUMED; |
| 164 | |
| 165 | dev = skb->dev; |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 166 | port = rmnet_get_port(dev); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 167 | |
Subash Abhinov Kasiviswanathan | d8bbb07 | 2017-10-11 18:43:52 -0600 | [diff] [blame] | 168 | if (port->ingress_data_format & RMNET_INGRESS_FORMAT_MAP) |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 169 | rc = rmnet_map_ingress_handler(skb, port); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 170 | |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | /* Modifies packet as per logical endpoint configuration and egress data format |
| 175 | * for egress device configured in logical endpoint. Packet is then transmitted |
| 176 | * on the egress device. |
| 177 | */ |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 178 | void rmnet_egress_handler(struct sk_buff *skb) |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 179 | { |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 180 | struct net_device *orig_dev; |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 181 | struct rmnet_port *port; |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 182 | struct rmnet_priv *priv; |
| 183 | u8 mux_id; |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 184 | |
| 185 | orig_dev = skb->dev; |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 186 | priv = netdev_priv(orig_dev); |
| 187 | skb->dev = priv->real_dev; |
| 188 | mux_id = priv->mux_id; |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 189 | |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 190 | port = rmnet_get_port(skb->dev); |
| 191 | if (!port) { |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 192 | kfree_skb(skb); |
| 193 | return; |
| 194 | } |
| 195 | |
Subash Abhinov Kasiviswanathan | b665f4f | 2017-09-02 23:30:46 -0600 | [diff] [blame] | 196 | if (port->egress_data_format & RMNET_EGRESS_FORMAT_MAP) { |
Subash Abhinov Kasiviswanathan | 56470c9 | 2017-10-11 18:43:55 -0600 | [diff] [blame^] | 197 | switch (rmnet_map_egress_handler(skb, port, mux_id, orig_dev)) { |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 198 | case RMNET_MAP_CONSUMED: |
| 199 | return; |
| 200 | |
| 201 | case RMNET_MAP_SUCCESS: |
| 202 | break; |
| 203 | |
| 204 | default: |
| 205 | kfree_skb(skb); |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | |
Subash Abhinov Kasiviswanathan | 9148963 | 2017-10-11 18:43:54 -0600 | [diff] [blame] | 210 | rmnet_vnd_tx_fixup(skb, orig_dev); |
Subash Abhinov Kasiviswanathan | ceed73a | 2017-08-29 22:44:18 -0600 | [diff] [blame] | 211 | |
| 212 | dev_queue_xmit(skb); |
| 213 | } |