rmnet_map_data.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Copyright (c) 2013-2018, 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 MAP protocol
  13. *
  14. */
  15. #include <linux/netdevice.h>
  16. #include <linux/ip.h>
  17. #include <linux/ipv6.h>
  18. #include <net/ip6_checksum.h>
  19. #include "rmnet_config.h"
  20. #include "rmnet_map.h"
  21. #include "rmnet_private.h"
  22. #define RMNET_MAP_DEAGGR_SPACING 64
  23. #define RMNET_MAP_DEAGGR_HEADROOM (RMNET_MAP_DEAGGR_SPACING / 2)
  24. static __sum16 *rmnet_map_get_csum_field(unsigned char protocol,
  25. const void *txporthdr)
  26. {
  27. __sum16 *check = NULL;
  28. switch (protocol) {
  29. case IPPROTO_TCP:
  30. check = &(((struct tcphdr *)txporthdr)->check);
  31. break;
  32. case IPPROTO_UDP:
  33. check = &(((struct udphdr *)txporthdr)->check);
  34. break;
  35. default:
  36. check = NULL;
  37. break;
  38. }
  39. return check;
  40. }
  41. static int
  42. rmnet_map_ipv4_dl_csum_trailer(struct sk_buff *skb,
  43. struct rmnet_map_dl_csum_trailer *csum_trailer,
  44. struct rmnet_priv *priv)
  45. {
  46. __sum16 *csum_field, csum_temp, pseudo_csum, hdr_csum, ip_payload_csum;
  47. u16 csum_value, csum_value_final;
  48. struct iphdr *ip4h;
  49. void *txporthdr;
  50. __be16 addend;
  51. ip4h = (struct iphdr *)(skb->data);
  52. if ((ntohs(ip4h->frag_off) & IP_MF) ||
  53. ((ntohs(ip4h->frag_off) & IP_OFFSET) > 0)) {
  54. priv->stats.csum_fragmented_pkt++;
  55. return -EOPNOTSUPP;
  56. }
  57. txporthdr = skb->data + ip4h->ihl * 4;
  58. csum_field = rmnet_map_get_csum_field(ip4h->protocol, txporthdr);
  59. if (!csum_field) {
  60. priv->stats.csum_err_invalid_transport++;
  61. return -EPROTONOSUPPORT;
  62. }
  63. /* RFC 768 - Skip IPv4 UDP packets where sender checksum field is 0 */
  64. if (*csum_field == 0 && ip4h->protocol == IPPROTO_UDP) {
  65. priv->stats.csum_skipped++;
  66. return 0;
  67. }
  68. csum_value = ~ntohs(csum_trailer->csum_value);
  69. hdr_csum = ~ip_fast_csum(ip4h, (int)ip4h->ihl);
  70. ip_payload_csum = csum16_sub((__force __sum16)csum_value,
  71. (__force __be16)hdr_csum);
  72. pseudo_csum = ~csum_tcpudp_magic(ip4h->saddr, ip4h->daddr,
  73. ntohs(ip4h->tot_len) - ip4h->ihl * 4,
  74. ip4h->protocol, 0);
  75. addend = (__force __be16)ntohs((__force __be16)pseudo_csum);
  76. pseudo_csum = csum16_add(ip_payload_csum, addend);
  77. addend = (__force __be16)ntohs((__force __be16)*csum_field);
  78. csum_temp = ~csum16_sub(pseudo_csum, addend);
  79. csum_value_final = (__force u16)csum_temp;
  80. if (unlikely(csum_value_final == 0)) {
  81. switch (ip4h->protocol) {
  82. case IPPROTO_UDP:
  83. /* RFC 768 - DL4 1's complement rule for UDP csum 0 */
  84. csum_value_final = ~csum_value_final;
  85. break;
  86. case IPPROTO_TCP:
  87. /* DL4 Non-RFC compliant TCP checksum found */
  88. if (*csum_field == (__force __sum16)0xFFFF)
  89. csum_value_final = ~csum_value_final;
  90. break;
  91. }
  92. }
  93. if (csum_value_final == ntohs((__force __be16)*csum_field)) {
  94. priv->stats.csum_ok++;
  95. return 0;
  96. } else {
  97. priv->stats.csum_validation_failed++;
  98. return -EINVAL;
  99. }
  100. }
  101. #if IS_ENABLED(CONFIG_IPV6)
  102. static int
  103. rmnet_map_ipv6_dl_csum_trailer(struct sk_buff *skb,
  104. struct rmnet_map_dl_csum_trailer *csum_trailer,
  105. struct rmnet_priv *priv)
  106. {
  107. __sum16 *csum_field, ip6_payload_csum, pseudo_csum, csum_temp;
  108. u16 csum_value, csum_value_final;
  109. __be16 ip6_hdr_csum, addend;
  110. struct ipv6hdr *ip6h;
  111. void *txporthdr;
  112. u32 length;
  113. ip6h = (struct ipv6hdr *)(skb->data);
  114. txporthdr = skb->data + sizeof(struct ipv6hdr);
  115. csum_field = rmnet_map_get_csum_field(ip6h->nexthdr, txporthdr);
  116. if (!csum_field) {
  117. priv->stats.csum_err_invalid_transport++;
  118. return -EPROTONOSUPPORT;
  119. }
  120. csum_value = ~ntohs(csum_trailer->csum_value);
  121. ip6_hdr_csum = (__force __be16)
  122. ~ntohs((__force __be16)ip_compute_csum(ip6h,
  123. (int)(txporthdr - (void *)(skb->data))));
  124. ip6_payload_csum = csum16_sub((__force __sum16)csum_value,
  125. ip6_hdr_csum);
  126. length = (ip6h->nexthdr == IPPROTO_UDP) ?
  127. ntohs(((struct udphdr *)txporthdr)->len) :
  128. ntohs(ip6h->payload_len);
  129. pseudo_csum = ~(csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  130. length, ip6h->nexthdr, 0));
  131. addend = (__force __be16)ntohs((__force __be16)pseudo_csum);
  132. pseudo_csum = csum16_add(ip6_payload_csum, addend);
  133. addend = (__force __be16)ntohs((__force __be16)*csum_field);
  134. csum_temp = ~csum16_sub(pseudo_csum, addend);
  135. csum_value_final = (__force u16)csum_temp;
  136. if (unlikely(csum_value_final == 0)) {
  137. switch (ip6h->nexthdr) {
  138. case IPPROTO_UDP:
  139. /* RFC 2460 section 8.1
  140. * DL6 One's complement rule for UDP checksum 0
  141. */
  142. csum_value_final = ~csum_value_final;
  143. break;
  144. case IPPROTO_TCP:
  145. /* DL6 Non-RFC compliant TCP checksum found */
  146. if (*csum_field == (__force __sum16)0xFFFF)
  147. csum_value_final = ~csum_value_final;
  148. break;
  149. }
  150. }
  151. if (csum_value_final == ntohs((__force __be16)*csum_field)) {
  152. priv->stats.csum_ok++;
  153. return 0;
  154. } else {
  155. priv->stats.csum_validation_failed++;
  156. return -EINVAL;
  157. }
  158. }
  159. #endif
  160. static void rmnet_map_complement_ipv4_txporthdr_csum_field(void *iphdr)
  161. {
  162. struct iphdr *ip4h = (struct iphdr *)iphdr;
  163. void *txphdr;
  164. u16 *csum;
  165. txphdr = iphdr + ip4h->ihl * 4;
  166. if (ip4h->protocol == IPPROTO_TCP || ip4h->protocol == IPPROTO_UDP) {
  167. csum = (u16 *)rmnet_map_get_csum_field(ip4h->protocol, txphdr);
  168. *csum = ~(*csum);
  169. }
  170. }
  171. static void
  172. rmnet_map_ipv4_ul_csum_header(void *iphdr,
  173. struct rmnet_map_ul_csum_header *ul_header,
  174. struct sk_buff *skb)
  175. {
  176. struct iphdr *ip4h = (struct iphdr *)iphdr;
  177. __be16 *hdr = (__be16 *)ul_header, offset;
  178. offset = htons((__force u16)(skb_transport_header(skb) -
  179. (unsigned char *)iphdr));
  180. ul_header->csum_start_offset = offset;
  181. ul_header->csum_insert_offset = skb->csum_offset;
  182. ul_header->csum_enabled = 1;
  183. if (ip4h->protocol == IPPROTO_UDP)
  184. ul_header->udp_ind = 1;
  185. else
  186. ul_header->udp_ind = 0;
  187. /* Changing remaining fields to network order */
  188. hdr++;
  189. *hdr = htons((__force u16)*hdr);
  190. skb->ip_summed = CHECKSUM_NONE;
  191. rmnet_map_complement_ipv4_txporthdr_csum_field(iphdr);
  192. }
  193. #if IS_ENABLED(CONFIG_IPV6)
  194. static void rmnet_map_complement_ipv6_txporthdr_csum_field(void *ip6hdr)
  195. {
  196. struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
  197. void *txphdr;
  198. u16 *csum;
  199. txphdr = ip6hdr + sizeof(struct ipv6hdr);
  200. if (ip6h->nexthdr == IPPROTO_TCP || ip6h->nexthdr == IPPROTO_UDP) {
  201. csum = (u16 *)rmnet_map_get_csum_field(ip6h->nexthdr, txphdr);
  202. *csum = ~(*csum);
  203. }
  204. }
  205. static void
  206. rmnet_map_ipv6_ul_csum_header(void *ip6hdr,
  207. struct rmnet_map_ul_csum_header *ul_header,
  208. struct sk_buff *skb)
  209. {
  210. struct ipv6hdr *ip6h = (struct ipv6hdr *)ip6hdr;
  211. __be16 *hdr = (__be16 *)ul_header, offset;
  212. offset = htons((__force u16)(skb_transport_header(skb) -
  213. (unsigned char *)ip6hdr));
  214. ul_header->csum_start_offset = offset;
  215. ul_header->csum_insert_offset = skb->csum_offset;
  216. ul_header->csum_enabled = 1;
  217. if (ip6h->nexthdr == IPPROTO_UDP)
  218. ul_header->udp_ind = 1;
  219. else
  220. ul_header->udp_ind = 0;
  221. /* Changing remaining fields to network order */
  222. hdr++;
  223. *hdr = htons((__force u16)*hdr);
  224. skb->ip_summed = CHECKSUM_NONE;
  225. rmnet_map_complement_ipv6_txporthdr_csum_field(ip6hdr);
  226. }
  227. #endif
  228. /* Adds MAP header to front of skb->data
  229. * Padding is calculated and set appropriately in MAP header. Mux ID is
  230. * initialized to 0.
  231. */
  232. struct rmnet_map_header *rmnet_map_add_map_header(struct sk_buff *skb,
  233. int hdrlen, int pad)
  234. {
  235. struct rmnet_map_header *map_header;
  236. u32 padding, map_datalen;
  237. u8 *padbytes;
  238. map_datalen = skb->len - hdrlen;
  239. map_header = (struct rmnet_map_header *)
  240. skb_push(skb, sizeof(struct rmnet_map_header));
  241. memset(map_header, 0, sizeof(struct rmnet_map_header));
  242. if (pad == RMNET_MAP_NO_PAD_BYTES) {
  243. map_header->pkt_len = htons(map_datalen);
  244. return map_header;
  245. }
  246. padding = ALIGN(map_datalen, 4) - map_datalen;
  247. if (padding == 0)
  248. goto done;
  249. if (skb_tailroom(skb) < padding)
  250. return NULL;
  251. padbytes = (u8 *)skb_put(skb, padding);
  252. memset(padbytes, 0, padding);
  253. done:
  254. map_header->pkt_len = htons(map_datalen + padding);
  255. map_header->pad_len = padding & 0x3F;
  256. return map_header;
  257. }
  258. /* Deaggregates a single packet
  259. * A whole new buffer is allocated for each portion of an aggregated frame.
  260. * Caller should keep calling deaggregate() on the source skb until 0 is
  261. * returned, indicating that there are no more packets to deaggregate. Caller
  262. * is responsible for freeing the original skb.
  263. */
  264. struct sk_buff *rmnet_map_deaggregate(struct sk_buff *skb,
  265. struct rmnet_port *port)
  266. {
  267. struct rmnet_map_header *maph;
  268. struct sk_buff *skbn;
  269. u32 packet_len;
  270. if (skb->len == 0)
  271. return NULL;
  272. maph = (struct rmnet_map_header *)skb->data;
  273. packet_len = ntohs(maph->pkt_len) + sizeof(struct rmnet_map_header);
  274. if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4)
  275. packet_len += sizeof(struct rmnet_map_dl_csum_trailer);
  276. if (((int)skb->len - (int)packet_len) < 0)
  277. return NULL;
  278. /* Some hardware can send us empty frames. Catch them */
  279. if (ntohs(maph->pkt_len) == 0)
  280. return NULL;
  281. skbn = alloc_skb(packet_len + RMNET_MAP_DEAGGR_SPACING, GFP_ATOMIC);
  282. if (!skbn)
  283. return NULL;
  284. skb_reserve(skbn, RMNET_MAP_DEAGGR_HEADROOM);
  285. skb_put(skbn, packet_len);
  286. memcpy(skbn->data, skb->data, packet_len);
  287. skb_pull(skb, packet_len);
  288. return skbn;
  289. }
  290. /* Validates packet checksums. Function takes a pointer to
  291. * the beginning of a buffer which contains the IP payload +
  292. * padding + checksum trailer.
  293. * Only IPv4 and IPv6 are supported along with TCP & UDP.
  294. * Fragmented or tunneled packets are not supported.
  295. */
  296. int rmnet_map_checksum_downlink_packet(struct sk_buff *skb, u16 len)
  297. {
  298. struct rmnet_priv *priv = netdev_priv(skb->dev);
  299. struct rmnet_map_dl_csum_trailer *csum_trailer;
  300. if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) {
  301. priv->stats.csum_sw++;
  302. return -EOPNOTSUPP;
  303. }
  304. csum_trailer = (struct rmnet_map_dl_csum_trailer *)(skb->data + len);
  305. if (!csum_trailer->valid) {
  306. priv->stats.csum_valid_unset++;
  307. return -EINVAL;
  308. }
  309. if (skb->protocol == htons(ETH_P_IP)) {
  310. return rmnet_map_ipv4_dl_csum_trailer(skb, csum_trailer, priv);
  311. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  312. #if IS_ENABLED(CONFIG_IPV6)
  313. return rmnet_map_ipv6_dl_csum_trailer(skb, csum_trailer, priv);
  314. #else
  315. priv->stats.csum_err_invalid_ip_version++;
  316. return -EPROTONOSUPPORT;
  317. #endif
  318. } else {
  319. priv->stats.csum_err_invalid_ip_version++;
  320. return -EPROTONOSUPPORT;
  321. }
  322. return 0;
  323. }
  324. /* Generates UL checksum meta info header for IPv4 and IPv6 over TCP and UDP
  325. * packets that are supported for UL checksum offload.
  326. */
  327. void rmnet_map_checksum_uplink_packet(struct sk_buff *skb,
  328. struct net_device *orig_dev)
  329. {
  330. struct rmnet_priv *priv = netdev_priv(orig_dev);
  331. struct rmnet_map_ul_csum_header *ul_header;
  332. void *iphdr;
  333. ul_header = (struct rmnet_map_ul_csum_header *)
  334. skb_push(skb, sizeof(struct rmnet_map_ul_csum_header));
  335. if (unlikely(!(orig_dev->features &
  336. (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))))
  337. goto sw_csum;
  338. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  339. iphdr = (char *)ul_header +
  340. sizeof(struct rmnet_map_ul_csum_header);
  341. if (skb->protocol == htons(ETH_P_IP)) {
  342. rmnet_map_ipv4_ul_csum_header(iphdr, ul_header, skb);
  343. return;
  344. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  345. #if IS_ENABLED(CONFIG_IPV6)
  346. rmnet_map_ipv6_ul_csum_header(iphdr, ul_header, skb);
  347. return;
  348. #else
  349. priv->stats.csum_err_invalid_ip_version++;
  350. goto sw_csum;
  351. #endif
  352. } else {
  353. priv->stats.csum_err_invalid_ip_version++;
  354. }
  355. }
  356. sw_csum:
  357. ul_header->csum_start_offset = 0;
  358. ul_header->csum_insert_offset = 0;
  359. ul_header->csum_enabled = 0;
  360. ul_header->udp_ind = 0;
  361. priv->stats.csum_sw++;
  362. }