rmnet_descriptor.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  1. /* Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  2. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * RMNET Packet Descriptor Framework
  14. *
  15. */
  16. #include <linux/ip.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/inet.h>
  19. #include <net/ipv6.h>
  20. #include <net/ip6_checksum.h>
  21. #include "rmnet_config.h"
  22. #include "rmnet_descriptor.h"
  23. #include "rmnet_handlers.h"
  24. #include "rmnet_private.h"
  25. #include "rmnet_vnd.h"
  26. #include "rmnet_qmi.h"
  27. #include "rmnet_trace.h"
  28. #include "qmi_rmnet.h"
  29. #define RMNET_FRAG_DESCRIPTOR_POOL_SIZE 64
  30. #define RMNET_DL_IND_HDR_SIZE (sizeof(struct rmnet_map_dl_ind_hdr) + \
  31. sizeof(struct rmnet_map_header) + \
  32. sizeof(struct rmnet_map_control_command_header))
  33. #define RMNET_DL_IND_TRL_SIZE (sizeof(struct rmnet_map_dl_ind_trl) + \
  34. sizeof(struct rmnet_map_header) + \
  35. sizeof(struct rmnet_map_control_command_header))
  36. #define RMNET_PB_IND_HDR_SIZE (sizeof(struct rmnet_map_pb_ind_hdr) + \
  37. sizeof(struct rmnet_map_header) + \
  38. sizeof(struct rmnet_map_control_command_header))
  39. #define rmnet_descriptor_for_each_frag(p, desc) \
  40. list_for_each_entry(p, &desc->frags, list)
  41. #define rmnet_descriptor_for_each_frag_safe(p, tmp, desc) \
  42. list_for_each_entry_safe(p, tmp, &desc->frags, list)
  43. #define rmnet_descriptor_for_each_frag_safe_reverse(p, tmp, desc) \
  44. list_for_each_entry_safe_reverse(p, tmp, &desc->frags, list)
  45. typedef void (*rmnet_perf_desc_hook_t)(struct rmnet_frag_descriptor *frag_desc,
  46. struct rmnet_port *port);
  47. typedef void (*rmnet_perf_chain_hook_t)(void);
  48. typedef void (*rmnet_perf_tether_ingress_hook_t)(struct tcphdr *tp, struct sk_buff *skb);
  49. rmnet_perf_tether_ingress_hook_t rmnet_perf_tether_ingress_hook __rcu __read_mostly;
  50. EXPORT_SYMBOL(rmnet_perf_tether_ingress_hook);
  51. struct rmnet_frag_descriptor *
  52. rmnet_get_frag_descriptor(struct rmnet_port *port)
  53. {
  54. struct rmnet_frag_descriptor_pool *pool = port->frag_desc_pool;
  55. struct rmnet_frag_descriptor *frag_desc;
  56. unsigned long flags;
  57. spin_lock_irqsave(&port->desc_pool_lock, flags);
  58. if (!list_empty(&pool->free_list)) {
  59. frag_desc = list_first_entry(&pool->free_list,
  60. struct rmnet_frag_descriptor,
  61. list);
  62. list_del_init(&frag_desc->list);
  63. } else {
  64. frag_desc = kzalloc(sizeof(*frag_desc), GFP_ATOMIC);
  65. if (!frag_desc)
  66. goto out;
  67. INIT_LIST_HEAD(&frag_desc->list);
  68. INIT_LIST_HEAD(&frag_desc->frags);
  69. pool->pool_size++;
  70. }
  71. out:
  72. spin_unlock_irqrestore(&port->desc_pool_lock, flags);
  73. return frag_desc;
  74. }
  75. EXPORT_SYMBOL(rmnet_get_frag_descriptor);
  76. void rmnet_recycle_frag_descriptor(struct rmnet_frag_descriptor *frag_desc,
  77. struct rmnet_port *port)
  78. {
  79. struct rmnet_frag_descriptor_pool *pool = port->frag_desc_pool;
  80. struct rmnet_fragment *frag, *tmp;
  81. unsigned long flags;
  82. list_del(&frag_desc->list);
  83. rmnet_descriptor_for_each_frag_safe(frag, tmp, frag_desc) {
  84. struct page *page = skb_frag_page(&frag->frag);
  85. if (page)
  86. put_page(page);
  87. list_del(&frag->list);
  88. kfree(frag);
  89. }
  90. memset(frag_desc, 0, sizeof(*frag_desc));
  91. INIT_LIST_HEAD(&frag_desc->list);
  92. INIT_LIST_HEAD(&frag_desc->frags);
  93. spin_lock_irqsave(&port->desc_pool_lock, flags);
  94. list_add_tail(&frag_desc->list, &pool->free_list);
  95. spin_unlock_irqrestore(&port->desc_pool_lock, flags);
  96. }
  97. EXPORT_SYMBOL(rmnet_recycle_frag_descriptor);
  98. void *rmnet_frag_pull(struct rmnet_frag_descriptor *frag_desc,
  99. struct rmnet_port *port, unsigned int size)
  100. {
  101. struct rmnet_fragment *frag, *tmp;
  102. if (size >= frag_desc->len) {
  103. pr_info("%s(): Pulling %u bytes from %u byte pkt. Dropping\n",
  104. __func__, size, frag_desc->len);
  105. rmnet_recycle_frag_descriptor(frag_desc, port);
  106. return NULL;
  107. }
  108. rmnet_descriptor_for_each_frag_safe(frag, tmp, frag_desc) {
  109. u32 frag_size = skb_frag_size(&frag->frag);
  110. if (!size)
  111. break;
  112. if (size >= frag_size) {
  113. /* Remove the whole frag */
  114. struct page *page = skb_frag_page(&frag->frag);
  115. if (page)
  116. put_page(page);
  117. list_del(&frag->list);
  118. size -= frag_size;
  119. frag_desc->len -= frag_size;
  120. kfree(frag);
  121. continue;
  122. }
  123. /* Pull off 'size' bytes */
  124. skb_frag_off_add(&frag->frag, size);
  125. skb_frag_size_sub(&frag->frag, size);
  126. frag_desc->len -= size;
  127. break;
  128. }
  129. return rmnet_frag_data_ptr(frag_desc);
  130. }
  131. EXPORT_SYMBOL(rmnet_frag_pull);
  132. void *rmnet_frag_trim(struct rmnet_frag_descriptor *frag_desc,
  133. struct rmnet_port *port, unsigned int size)
  134. {
  135. struct rmnet_fragment *frag, *tmp;
  136. unsigned int eat;
  137. if (!size) {
  138. pr_info("%s(): Trimming %u byte pkt to 0. Dropping\n",
  139. __func__, frag_desc->len);
  140. rmnet_recycle_frag_descriptor(frag_desc, port);
  141. return NULL;
  142. }
  143. /* Growing bigger doesn't make sense */
  144. if (size >= frag_desc->len)
  145. goto out;
  146. /* Compute number of bytes to remove from the end */
  147. eat = frag_desc->len - size;
  148. rmnet_descriptor_for_each_frag_safe_reverse(frag, tmp, frag_desc) {
  149. u32 frag_size = skb_frag_size(&frag->frag);
  150. if (!eat)
  151. goto out;
  152. if (eat >= frag_size) {
  153. /* Remove the whole frag */
  154. struct page *page = skb_frag_page(&frag->frag);
  155. if (page)
  156. put_page(page);
  157. list_del(&frag->list);
  158. eat -= frag_size;
  159. frag_desc->len -= frag_size;
  160. kfree(frag);
  161. continue;
  162. }
  163. /* Chop off 'eat' bytes from the end */
  164. skb_frag_size_sub(&frag->frag, eat);
  165. frag_desc->len -= eat;
  166. goto out;
  167. }
  168. out:
  169. return rmnet_frag_data_ptr(frag_desc);
  170. }
  171. EXPORT_SYMBOL(rmnet_frag_trim);
  172. static int rmnet_frag_copy_data(struct rmnet_frag_descriptor *frag_desc,
  173. u32 off, u32 len, void *buf)
  174. {
  175. struct rmnet_fragment *frag;
  176. u32 frag_size, copy_len;
  177. u32 buf_offset = 0;
  178. /* Don't make me do something we'd both regret */
  179. if (off > frag_desc->len || len > frag_desc->len ||
  180. off + len > frag_desc->len)
  181. return -EINVAL;
  182. /* Copy 'len' bytes into the bufer starting from 'off' */
  183. rmnet_descriptor_for_each_frag(frag, frag_desc) {
  184. if (!len)
  185. break;
  186. frag_size = skb_frag_size(&frag->frag);
  187. if (off < frag_size) {
  188. copy_len = min_t(u32, len, frag_size - off);
  189. memcpy(buf + buf_offset,
  190. skb_frag_address(&frag->frag) + off,
  191. copy_len);
  192. buf_offset += copy_len;
  193. len -= copy_len;
  194. off = 0;
  195. } else {
  196. off -= frag_size;
  197. }
  198. }
  199. return 0;
  200. }
  201. void *rmnet_frag_header_ptr(struct rmnet_frag_descriptor *frag_desc, u32 off,
  202. u32 len, void *buf)
  203. {
  204. struct rmnet_fragment *frag;
  205. u8 *start;
  206. u32 frag_size, offset;
  207. /* Don't take a long pointer off a short frag */
  208. if (off > frag_desc->len || len > frag_desc->len ||
  209. off + len > frag_desc->len)
  210. return NULL;
  211. /* Find the starting fragment */
  212. offset = off;
  213. rmnet_descriptor_for_each_frag(frag, frag_desc) {
  214. frag_size = skb_frag_size(&frag->frag);
  215. if (off < frag_size) {
  216. start = skb_frag_address(&frag->frag) + off;
  217. /* If the header is entirely on this frag, just return
  218. * a pointer to it.
  219. */
  220. if (off + len <= frag_size)
  221. return start;
  222. /* Otherwise, we need to copy the data into a linear
  223. * buffer.
  224. */
  225. break;
  226. }
  227. off -= frag_size;
  228. }
  229. if (rmnet_frag_copy_data(frag_desc, offset, len, buf) < 0)
  230. return NULL;
  231. return buf;
  232. }
  233. EXPORT_SYMBOL(rmnet_frag_header_ptr);
  234. int rmnet_frag_descriptor_add_frag(struct rmnet_frag_descriptor *frag_desc,
  235. struct page *p, u32 page_offset, u32 len)
  236. {
  237. struct rmnet_fragment *frag;
  238. frag = kzalloc(sizeof(*frag), GFP_ATOMIC);
  239. if (!frag)
  240. return -ENOMEM;
  241. INIT_LIST_HEAD(&frag->list);
  242. get_page(p);
  243. __skb_frag_set_page(&frag->frag, p);
  244. skb_frag_size_set(&frag->frag, len);
  245. skb_frag_off_set(&frag->frag, page_offset);
  246. list_add_tail(&frag->list, &frag_desc->frags);
  247. frag_desc->len += len;
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(rmnet_frag_descriptor_add_frag);
  251. int rmnet_frag_descriptor_add_frags_from(struct rmnet_frag_descriptor *to,
  252. struct rmnet_frag_descriptor *from,
  253. u32 off, u32 len)
  254. {
  255. struct rmnet_fragment *frag;
  256. int rc;
  257. /* Sanity check the lengths */
  258. if (off > from->len || len > from->len || off + len > from->len)
  259. return -EINVAL;
  260. rmnet_descriptor_for_each_frag(frag, from) {
  261. u32 frag_size;
  262. if (!len)
  263. break;
  264. frag_size = skb_frag_size(&frag->frag);
  265. if (off < frag_size) {
  266. struct page *p = skb_frag_page(&frag->frag);
  267. u32 page_off = skb_frag_off(&frag->frag);
  268. u32 copy_len = min_t(u32, len, frag_size - off);
  269. rc = rmnet_frag_descriptor_add_frag(to, p,
  270. page_off + off,
  271. copy_len);
  272. if (rc < 0)
  273. return rc;
  274. len -= copy_len;
  275. off = 0;
  276. } else {
  277. off -= frag_size;
  278. }
  279. }
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(rmnet_frag_descriptor_add_frags_from);
  283. int rmnet_frag_ipv6_skip_exthdr(struct rmnet_frag_descriptor *frag_desc,
  284. int start, u8 *nexthdrp, __be16 *frag_offp,
  285. bool *frag_hdrp)
  286. {
  287. u8 nexthdr = *nexthdrp;
  288. *frag_offp = 0;
  289. *frag_hdrp = false;
  290. while (ipv6_ext_hdr(nexthdr)) {
  291. struct ipv6_opt_hdr *hp, __hp;
  292. int hdrlen;
  293. if (nexthdr == NEXTHDR_NONE)
  294. return -EINVAL;
  295. hp = rmnet_frag_header_ptr(frag_desc, (u32)start, sizeof(*hp),
  296. &__hp);
  297. if (!hp)
  298. return -EINVAL;
  299. if (nexthdr == NEXTHDR_FRAGMENT) {
  300. u32 off = offsetof(struct frag_hdr, frag_off);
  301. __be16 *fp, __fp;
  302. fp = rmnet_frag_header_ptr(frag_desc, (u32)start + off,
  303. sizeof(*fp), &__fp);
  304. if (!fp)
  305. return -EINVAL;
  306. *frag_offp = *fp;
  307. *frag_hdrp = true;
  308. if (ntohs(*frag_offp) & ~0x7)
  309. break;
  310. hdrlen = 8;
  311. } else if (nexthdr == NEXTHDR_AUTH) {
  312. hdrlen = (hp->hdrlen + 2) << 2;
  313. } else {
  314. hdrlen = ipv6_optlen(hp);
  315. }
  316. nexthdr = hp->nexthdr;
  317. start += hdrlen;
  318. }
  319. *nexthdrp = nexthdr;
  320. return start;
  321. }
  322. EXPORT_SYMBOL(rmnet_frag_ipv6_skip_exthdr);
  323. static u8 rmnet_frag_do_flow_control(struct rmnet_map_header *qmap,
  324. struct rmnet_map_control_command *cmd,
  325. struct rmnet_port *port,
  326. int enable)
  327. {
  328. struct rmnet_endpoint *ep;
  329. struct net_device *vnd;
  330. u16 ip_family;
  331. u16 fc_seq;
  332. u32 qos_id;
  333. u8 mux_id;
  334. int r;
  335. mux_id = qmap->mux_id;
  336. if (mux_id >= RMNET_MAX_LOGICAL_EP)
  337. return RX_HANDLER_CONSUMED;
  338. ep = rmnet_get_endpoint(port, mux_id);
  339. if (!ep)
  340. return RX_HANDLER_CONSUMED;
  341. vnd = ep->egress_dev;
  342. ip_family = cmd->flow_control.ip_family;
  343. fc_seq = ntohs(cmd->flow_control.flow_control_seq_num);
  344. qos_id = ntohl(cmd->flow_control.qos_id);
  345. /* Ignore the ip family and pass the sequence number for both v4 and v6
  346. * sequence. User space does not support creating dedicated flows for
  347. * the 2 protocols
  348. */
  349. r = rmnet_vnd_do_flow_control(vnd, enable);
  350. if (r)
  351. return RMNET_MAP_COMMAND_UNSUPPORTED;
  352. else
  353. return RMNET_MAP_COMMAND_ACK;
  354. }
  355. static void rmnet_frag_send_ack(struct rmnet_map_header *qmap,
  356. unsigned char type,
  357. struct rmnet_port *port)
  358. {
  359. struct rmnet_map_control_command *cmd;
  360. struct net_device *dev = port->dev;
  361. struct sk_buff *skb;
  362. u16 alloc_len = ntohs(qmap->pkt_len) + sizeof(*qmap);
  363. skb = alloc_skb(alloc_len, GFP_ATOMIC);
  364. if (!skb)
  365. return;
  366. skb->protocol = htons(ETH_P_MAP);
  367. skb->dev = dev;
  368. cmd = rmnet_map_get_cmd_start(skb);
  369. cmd->cmd_type = type & 0x03;
  370. netif_tx_lock(dev);
  371. dev->netdev_ops->ndo_start_xmit(skb, dev);
  372. netif_tx_unlock(dev);
  373. }
  374. static void
  375. rmnet_frag_process_pb_ind(struct rmnet_frag_descriptor *frag_desc,
  376. struct rmnet_map_control_command_header *cmd,
  377. struct rmnet_port *port,
  378. u16 cmd_len)
  379. {
  380. struct rmnet_map_pb_ind_hdr *pbhdr, __pbhdr;
  381. u32 offset = sizeof(struct rmnet_map_header);
  382. u32 data_format;
  383. bool is_dl_mark_v2;
  384. if (cmd_len + offset < RMNET_PB_IND_HDR_SIZE)
  385. return;
  386. data_format = port->data_format;
  387. is_dl_mark_v2 = data_format & RMNET_INGRESS_FORMAT_DL_MARKER_V2;
  388. pbhdr = rmnet_frag_header_ptr(frag_desc, offset + sizeof(*cmd),
  389. sizeof(*pbhdr), &__pbhdr);
  390. if (!pbhdr)
  391. return;
  392. port->stats.pb_marker_count++;
  393. /* If a target is taking frag path, we can assume DL marker v2 is in
  394. * play
  395. */
  396. if (is_dl_mark_v2)
  397. rmnet_map_pb_ind_notify(port, pbhdr);
  398. }
  399. static void
  400. rmnet_frag_process_flow_start(struct rmnet_frag_descriptor *frag_desc,
  401. struct rmnet_map_control_command_header *cmd,
  402. struct rmnet_port *port,
  403. u16 cmd_len)
  404. {
  405. struct rmnet_map_dl_ind_hdr *dlhdr, __dlhdr;
  406. u32 offset = sizeof(struct rmnet_map_header);
  407. u32 data_format;
  408. bool is_dl_mark_v2;
  409. if (cmd_len + offset < RMNET_DL_IND_HDR_SIZE)
  410. return;
  411. data_format = port->data_format;
  412. is_dl_mark_v2 = data_format & RMNET_INGRESS_FORMAT_DL_MARKER_V2;
  413. dlhdr = rmnet_frag_header_ptr(frag_desc, offset + sizeof(*cmd),
  414. sizeof(*dlhdr), &__dlhdr);
  415. if (!dlhdr)
  416. return;
  417. port->stats.dl_hdr_last_ep_id = cmd->source_id;
  418. port->stats.dl_hdr_last_qmap_vers = cmd->reserved;
  419. port->stats.dl_hdr_last_trans_id = cmd->transaction_id;
  420. port->stats.dl_hdr_last_seq = dlhdr->le.seq;
  421. port->stats.dl_hdr_last_bytes = dlhdr->le.bytes;
  422. port->stats.dl_hdr_last_pkts = dlhdr->le.pkts;
  423. port->stats.dl_hdr_last_flows = dlhdr->le.flows;
  424. port->stats.dl_hdr_total_bytes += port->stats.dl_hdr_last_bytes;
  425. port->stats.dl_hdr_total_pkts += port->stats.dl_hdr_last_pkts;
  426. port->stats.dl_hdr_count++;
  427. /* If a target is taking frag path, we can assume DL marker v2 is in
  428. * play
  429. */
  430. if (is_dl_mark_v2)
  431. rmnet_map_dl_hdr_notify_v2(port, dlhdr, cmd);
  432. }
  433. static void
  434. rmnet_frag_process_flow_end(struct rmnet_frag_descriptor *frag_desc,
  435. struct rmnet_map_control_command_header *cmd,
  436. struct rmnet_port *port, u16 cmd_len)
  437. {
  438. struct rmnet_map_dl_ind_trl *dltrl, __dltrl;
  439. u32 offset = sizeof(struct rmnet_map_header);
  440. u32 data_format;
  441. bool is_dl_mark_v2;
  442. if (cmd_len + offset < RMNET_DL_IND_TRL_SIZE)
  443. return;
  444. data_format = port->data_format;
  445. is_dl_mark_v2 = data_format & RMNET_INGRESS_FORMAT_DL_MARKER_V2;
  446. dltrl = rmnet_frag_header_ptr(frag_desc, offset + sizeof(*cmd),
  447. sizeof(*dltrl), &__dltrl);
  448. if (!dltrl)
  449. return;
  450. port->stats.dl_trl_last_seq = dltrl->seq_le;
  451. port->stats.dl_trl_count++;
  452. /* If a target is taking frag path, we can assume DL marker v2 is in
  453. * play
  454. */
  455. if (is_dl_mark_v2)
  456. rmnet_map_dl_trl_notify_v2(port, dltrl, cmd);
  457. }
  458. /* Process MAP command frame and send N/ACK message as appropriate. Message cmd
  459. * name is decoded here and appropriate handler is called.
  460. */
  461. void rmnet_frag_command(struct rmnet_frag_descriptor *frag_desc,
  462. struct rmnet_map_header *qmap, struct rmnet_port *port)
  463. {
  464. struct rmnet_map_control_command *cmd, __cmd;
  465. unsigned char rc = 0;
  466. cmd = rmnet_frag_header_ptr(frag_desc, sizeof(*qmap), sizeof(*cmd),
  467. &__cmd);
  468. if (!cmd)
  469. return;
  470. switch (cmd->command_name) {
  471. case RMNET_MAP_COMMAND_FLOW_ENABLE:
  472. rc = rmnet_frag_do_flow_control(qmap, cmd, port, 1);
  473. break;
  474. case RMNET_MAP_COMMAND_FLOW_DISABLE:
  475. rc = rmnet_frag_do_flow_control(qmap, cmd, port, 0);
  476. break;
  477. default:
  478. rc = RMNET_MAP_COMMAND_UNSUPPORTED;
  479. break;
  480. }
  481. if (rc == RMNET_MAP_COMMAND_ACK)
  482. rmnet_frag_send_ack(qmap, rc, port);
  483. }
  484. int rmnet_frag_flow_command(struct rmnet_frag_descriptor *frag_desc,
  485. struct rmnet_port *port, u16 pkt_len)
  486. {
  487. struct rmnet_map_control_command_header *cmd, __cmd;
  488. cmd = rmnet_frag_header_ptr(frag_desc, sizeof(struct rmnet_map_header),
  489. sizeof(*cmd), &__cmd);
  490. if (!cmd)
  491. return -1;
  492. /* Silently discard any marksers recived over the LL channel */
  493. if (frag_desc->priority == 0xda1a &&
  494. (cmd->command_name == RMNET_MAP_COMMAND_FLOW_START ||
  495. cmd->command_name == RMNET_MAP_COMMAND_FLOW_END))
  496. return 0;
  497. switch (cmd->command_name) {
  498. case RMNET_MAP_COMMAND_FLOW_START:
  499. rmnet_frag_process_flow_start(frag_desc, cmd, port, pkt_len);
  500. break;
  501. case RMNET_MAP_COMMAND_FLOW_END:
  502. rmnet_frag_process_flow_end(frag_desc, cmd, port, pkt_len);
  503. break;
  504. case RMNET_MAP_COMMAND_PB_BYTES:
  505. rmnet_frag_process_pb_ind(frag_desc, cmd, port, pkt_len);
  506. break;
  507. default:
  508. return 1;
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL(rmnet_frag_flow_command);
  513. static int rmnet_frag_deaggregate_one(struct sk_buff *skb,
  514. struct rmnet_port *port,
  515. struct list_head *list,
  516. u32 start, u32 priority)
  517. {
  518. struct skb_shared_info *shinfo = skb_shinfo(skb);
  519. struct rmnet_frag_descriptor *frag_desc;
  520. struct rmnet_map_header *maph, __maph;
  521. skb_frag_t *frag;
  522. u32 start_frag, offset, i;
  523. u32 start_frag_size, start_frag_off;
  524. u32 pkt_len, copy_len = 0;
  525. int rc;
  526. for (start_frag = 0, offset = 0; start_frag < shinfo->nr_frags;
  527. start_frag++) {
  528. frag = &shinfo->frags[start_frag];
  529. if (start < skb_frag_size(frag) + offset)
  530. break;
  531. offset += skb_frag_size(frag);
  532. }
  533. if (start_frag == shinfo->nr_frags)
  534. return -1;
  535. /* start - offset is the additional offset into the page to account
  536. * for any data on it we've already used.
  537. */
  538. start_frag_size = skb_frag_size(frag) - (start - offset);
  539. start_frag_off = skb_frag_off(frag) + (start - offset);
  540. /* Grab the QMAP header. Careful, as there's no guarantee that it's
  541. * continugous!
  542. */
  543. if (likely(start_frag_size >= sizeof(*maph))) {
  544. maph = skb_frag_address(frag) + (start - offset);
  545. } else {
  546. /* The header's split across pages. We can rebuild it.
  547. * Probably not faster or stronger than before. But certainly
  548. * more linear.
  549. */
  550. if (skb_copy_bits(skb, start, &__maph, sizeof(__maph)) < 0)
  551. return -1;
  552. maph = &__maph;
  553. }
  554. pkt_len = ntohs(maph->pkt_len);
  555. /* Catch empty frames */
  556. if (!pkt_len)
  557. return -1;
  558. frag_desc = rmnet_get_frag_descriptor(port);
  559. if (!frag_desc)
  560. return -1;
  561. frag_desc->priority = priority;
  562. pkt_len += sizeof(*maph);
  563. if (port->data_format & RMNET_FLAGS_INGRESS_MAP_CKSUMV4) {
  564. pkt_len += sizeof(struct rmnet_map_dl_csum_trailer);
  565. } else if ((port->data_format & (RMNET_PRIV_FLAGS_INGRESS_MAP_CKSUMV5 |
  566. RMNET_FLAGS_INGRESS_COALESCE)) &&
  567. !maph->cd_bit) {
  568. u32 hsize = 0;
  569. u8 type;
  570. /* Check the type. This seems like should be overkill for less
  571. * than a single byte, doesn't it?
  572. */
  573. if (likely(start_frag_size >= sizeof(*maph) + 1)) {
  574. type = *((u8 *)maph + sizeof(*maph));
  575. } else {
  576. if (skb_copy_bits(skb, start + sizeof(*maph), &type,
  577. sizeof(type)) < 0)
  578. return -1;
  579. }
  580. /* Type only uses the first 7 bits */
  581. switch ((type & 0xFE) >> 1) {
  582. case RMNET_MAP_HEADER_TYPE_COALESCING:
  583. hsize = sizeof(struct rmnet_map_v5_coal_header);
  584. break;
  585. case RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD:
  586. hsize = sizeof(struct rmnet_map_v5_csum_header);
  587. break;
  588. }
  589. pkt_len += hsize;
  590. }
  591. /* Add all frags containing the packet data to the descriptor */
  592. for (i = start_frag; pkt_len > 0 && i < shinfo->nr_frags; ) {
  593. u32 size, off;
  594. u32 copy;
  595. frag = &shinfo->frags[i];
  596. size = skb_frag_size(frag);
  597. off = skb_frag_off(frag);
  598. if (i == start_frag) {
  599. /* These are different for the first one to account for
  600. * the starting offset.
  601. */
  602. size = start_frag_size;
  603. off = start_frag_off;
  604. }
  605. copy = min_t(u32, size, pkt_len);
  606. rc = rmnet_frag_descriptor_add_frag(frag_desc,
  607. skb_frag_page(frag), off,
  608. copy);
  609. if (rc < 0) {
  610. rmnet_recycle_frag_descriptor(frag_desc, port);
  611. return -1;
  612. }
  613. pkt_len -= copy;
  614. copy_len += copy;
  615. /* If the fragment is exhausted, we can move to the next one */
  616. if (!(size - copy_len)) {
  617. i++;
  618. copy_len = 0;
  619. }
  620. }
  621. if (pkt_len) {
  622. /* Packet length is larger than the amount of data we have */
  623. rmnet_recycle_frag_descriptor(frag_desc, port);
  624. return -1;
  625. }
  626. list_add_tail(&frag_desc->list, list);
  627. return (int)frag_desc->len;
  628. }
  629. void rmnet_frag_deaggregate(struct sk_buff *skb, struct rmnet_port *port,
  630. struct list_head *list, u32 priority)
  631. {
  632. u32 start = 0;
  633. int rc;
  634. while (start < skb->len) {
  635. rc = rmnet_frag_deaggregate_one(skb, port, list, start,
  636. priority);
  637. if (rc < 0)
  638. return;
  639. start += (u32)rc;
  640. }
  641. }
  642. /* Fill in GSO metadata to allow the SKB to be segmented by the NW stack
  643. * if needed (i.e. forwarding, UDP GRO)
  644. */
  645. static void rmnet_frag_gso_stamp(struct sk_buff *skb,
  646. struct rmnet_frag_descriptor *frag_desc)
  647. {
  648. struct skb_shared_info *shinfo = skb_shinfo(skb);
  649. if (frag_desc->trans_proto == IPPROTO_TCP)
  650. shinfo->gso_type = (frag_desc->ip_proto == 4) ?
  651. SKB_GSO_TCPV4 : SKB_GSO_TCPV6;
  652. else
  653. shinfo->gso_type = SKB_GSO_UDP_L4;
  654. shinfo->gso_size = frag_desc->gso_size;
  655. shinfo->gso_segs = frag_desc->gso_segs;
  656. }
  657. /* Set the partial checksum information. Sets the transport checksum to the
  658. * pseudoheader checksum and sets the offload metadata.
  659. */
  660. static void rmnet_frag_partial_csum(struct sk_buff *skb,
  661. struct rmnet_frag_descriptor *frag_desc)
  662. {
  663. rmnet_perf_tether_ingress_hook_t rmnet_perf_tether_ingress;
  664. struct iphdr *iph = (struct iphdr *)skb->data;
  665. __sum16 pseudo;
  666. u16 pkt_len = skb->len - frag_desc->ip_len;
  667. if (frag_desc->ip_proto == 4) {
  668. iph->tot_len = htons(skb->len);
  669. iph->check = 0;
  670. iph->check = ip_fast_csum(iph, iph->ihl);
  671. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  672. pkt_len, frag_desc->trans_proto,
  673. 0);
  674. } else {
  675. struct ipv6hdr *ip6h = (struct ipv6hdr *)iph;
  676. /* Payload length includes any extension headers */
  677. ip6h->payload_len = htons(skb->len - sizeof(*ip6h));
  678. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  679. pkt_len, frag_desc->trans_proto, 0);
  680. }
  681. if (frag_desc->trans_proto == IPPROTO_TCP) {
  682. struct tcphdr *tp = (struct tcphdr *)
  683. ((u8 *)iph + frag_desc->ip_len);
  684. tp->check = pseudo;
  685. skb->csum_offset = offsetof(struct tcphdr, check);
  686. rmnet_perf_tether_ingress = rcu_dereference(rmnet_perf_tether_ingress_hook);
  687. if (rmnet_perf_tether_ingress)
  688. rmnet_perf_tether_ingress(tp, skb);
  689. } else {
  690. struct udphdr *up = (struct udphdr *)
  691. ((u8 *)iph + frag_desc->ip_len);
  692. up->len = htons(pkt_len);
  693. up->check = pseudo;
  694. skb->csum_offset = offsetof(struct udphdr, check);
  695. }
  696. skb->ip_summed = CHECKSUM_PARTIAL;
  697. skb->csum_start = (u8 *)iph + frag_desc->ip_len - skb->head;
  698. }
  699. #define PFN_ENTRY_MAX (128)
  700. #define PFNI (count++ % PFN_ENTRY_MAX)
  701. static void rmnet_descriptor_trace_pfn(struct sk_buff *skb)
  702. {
  703. struct skb_shared_info *shinfo;
  704. struct sk_buff *frag_iter;
  705. unsigned long rpfn[PFN_ENTRY_MAX];
  706. int i, count;
  707. if (!trace_print_pfn_enabled())
  708. return;
  709. shinfo = skb_shinfo(skb);
  710. memset(rpfn, 0, sizeof(rpfn));
  711. count = 0;
  712. for (i = 0; i < shinfo->nr_frags; i++)
  713. rpfn[PFNI] = page_to_pfn(skb_frag_page(&shinfo->frags[i]));
  714. skb_walk_frags(skb, frag_iter) {
  715. shinfo = skb_shinfo(frag_iter);
  716. for (i = 0; i < shinfo->nr_frags; i++)
  717. rpfn[PFNI] = page_to_pfn(skb_frag_page(&shinfo->frags[i]));
  718. }
  719. trace_print_pfn(skb, rpfn, count);
  720. }
  721. /* Allocate and populate an skb to contain the packet represented by the
  722. * frag descriptor.
  723. */
  724. static struct sk_buff *rmnet_alloc_skb(struct rmnet_frag_descriptor *frag_desc,
  725. struct rmnet_port *port)
  726. {
  727. struct sk_buff *head_skb, *current_skb, *skb;
  728. struct skb_shared_info *shinfo;
  729. struct rmnet_fragment *frag, *tmp;
  730. struct rmnet_skb_cb *cb;
  731. /* Use the exact sizes if we know them (i.e. RSB/RSC, rmnet_perf) */
  732. if (frag_desc->hdrs_valid) {
  733. u16 hdr_len = frag_desc->ip_len + frag_desc->trans_len;
  734. head_skb = alloc_skb(hdr_len + RMNET_MAP_DEAGGR_HEADROOM,
  735. GFP_ATOMIC);
  736. if (!head_skb)
  737. return NULL;
  738. skb_reserve(head_skb, RMNET_MAP_DEAGGR_HEADROOM);
  739. rmnet_frag_copy_data(frag_desc, 0, hdr_len,
  740. skb_put(head_skb, hdr_len));
  741. skb_reset_network_header(head_skb);
  742. if (frag_desc->trans_len)
  743. skb_set_transport_header(head_skb, frag_desc->ip_len);
  744. /* Pull the headers off carefully */
  745. if (hdr_len == frag_desc->len)
  746. /* Fast forward "header only" packets */
  747. goto skip_frags;
  748. if (!rmnet_frag_pull(frag_desc, port, hdr_len)) {
  749. kfree(head_skb);
  750. return NULL;
  751. }
  752. } else {
  753. /* Allocate enough space to avoid penalties in the stack
  754. * from __pskb_pull_tail()
  755. */
  756. head_skb = alloc_skb(256 + RMNET_MAP_DEAGGR_HEADROOM,
  757. GFP_ATOMIC);
  758. if (!head_skb)
  759. return NULL;
  760. skb_reserve(head_skb, RMNET_MAP_DEAGGR_HEADROOM);
  761. }
  762. shinfo = skb_shinfo(head_skb);
  763. current_skb = head_skb;
  764. /* Add in the page fragments */
  765. rmnet_descriptor_for_each_frag_safe(frag, tmp, frag_desc) {
  766. struct page *p = skb_frag_page(&frag->frag);
  767. u32 frag_size = skb_frag_size(&frag->frag);
  768. add_frag:
  769. if (shinfo->nr_frags < MAX_SKB_FRAGS) {
  770. get_page(p);
  771. skb_add_rx_frag(current_skb, shinfo->nr_frags, p,
  772. skb_frag_off(&frag->frag), frag_size,
  773. frag_size);
  774. if (current_skb != head_skb) {
  775. head_skb->len += frag_size;
  776. head_skb->data_len += frag_size;
  777. }
  778. } else {
  779. /* Alloc a new skb and try again */
  780. skb = alloc_skb(0, GFP_ATOMIC);
  781. if (!skb)
  782. break;
  783. if (current_skb == head_skb)
  784. shinfo->frag_list = skb;
  785. else
  786. current_skb->next = skb;
  787. current_skb = skb;
  788. shinfo = skb_shinfo(current_skb);
  789. goto add_frag;
  790. }
  791. }
  792. skip_frags:
  793. head_skb->dev = frag_desc->dev;
  794. rmnet_set_skb_proto(head_skb);
  795. cb = RMNET_SKB_CB(head_skb);
  796. cb->coal_bytes = frag_desc->coal_bytes;
  797. cb->coal_bufsize = frag_desc->coal_bufsize;
  798. /* Handle any header metadata that needs to be updated after RSB/RSC
  799. * segmentation
  800. */
  801. if (frag_desc->ip_id_set) {
  802. struct iphdr *iph;
  803. iph = (struct iphdr *)rmnet_map_data_ptr(head_skb);
  804. csum_replace2(&iph->check, iph->id, frag_desc->ip_id);
  805. iph->id = frag_desc->ip_id;
  806. }
  807. if (frag_desc->tcp_seq_set) {
  808. struct tcphdr *th;
  809. th = (struct tcphdr *)
  810. (rmnet_map_data_ptr(head_skb) + frag_desc->ip_len);
  811. th->seq = frag_desc->tcp_seq;
  812. }
  813. if (frag_desc->tcp_flags_set) {
  814. struct tcphdr *th;
  815. __be16 *flags;
  816. th = (struct tcphdr *)
  817. (rmnet_map_data_ptr(head_skb) + frag_desc->ip_len);
  818. flags = (__be16 *)&tcp_flag_word(th);
  819. *flags = frag_desc->tcp_flags;
  820. }
  821. /* Handle csum offloading */
  822. if (frag_desc->csum_valid && frag_desc->hdrs_valid) {
  823. /* Set the partial checksum information */
  824. rmnet_frag_partial_csum(head_skb, frag_desc);
  825. } else if (frag_desc->csum_valid) {
  826. /* Non-RSB/RSC/perf packet. The current checksum is fine */
  827. head_skb->ip_summed = CHECKSUM_UNNECESSARY;
  828. } else if (frag_desc->hdrs_valid &&
  829. (frag_desc->trans_proto == IPPROTO_TCP ||
  830. frag_desc->trans_proto == IPPROTO_UDP)) {
  831. /* Unfortunately, we have to fake a bad checksum here, since
  832. * the original bad value is lost by the hardware. The only
  833. * reliable way to do it is to calculate the actual checksum
  834. * and corrupt it.
  835. */
  836. __sum16 *check;
  837. __wsum csum;
  838. unsigned int offset = skb_transport_offset(head_skb);
  839. __sum16 pseudo;
  840. /* Calculate pseudo header and update header fields */
  841. if (frag_desc->ip_proto == 4) {
  842. struct iphdr *iph = ip_hdr(head_skb);
  843. __be16 tot_len = htons(head_skb->len);
  844. csum_replace2(&iph->check, iph->tot_len, tot_len);
  845. iph->tot_len = tot_len;
  846. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  847. head_skb->len -
  848. frag_desc->ip_len,
  849. frag_desc->trans_proto, 0);
  850. } else {
  851. struct ipv6hdr *ip6h = ipv6_hdr(head_skb);
  852. ip6h->payload_len = htons(head_skb->len -
  853. sizeof(*ip6h));
  854. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  855. head_skb->len -
  856. frag_desc->ip_len,
  857. frag_desc->trans_proto, 0);
  858. }
  859. if (frag_desc->trans_proto == IPPROTO_TCP) {
  860. check = &tcp_hdr(head_skb)->check;
  861. } else {
  862. udp_hdr(head_skb)->len = htons(head_skb->len -
  863. frag_desc->ip_len);
  864. check = &udp_hdr(head_skb)->check;
  865. }
  866. *check = pseudo;
  867. csum = skb_checksum(head_skb, offset, head_skb->len - offset,
  868. 0);
  869. /* Add 1 to corrupt. This cannot produce a final value of 0
  870. * since csum_fold() can't return a value of 0xFFFF
  871. */
  872. *check = csum16_add(csum_fold(csum), htons(1));
  873. head_skb->ip_summed = CHECKSUM_NONE;
  874. }
  875. /* Handle any rmnet_perf metadata */
  876. if (frag_desc->hash) {
  877. head_skb->hash = frag_desc->hash;
  878. head_skb->sw_hash = 1;
  879. }
  880. if (frag_desc->flush_shs)
  881. cb->flush_shs = 1;
  882. /* Handle coalesced packets */
  883. if (frag_desc->gso_segs > 1)
  884. rmnet_frag_gso_stamp(head_skb, frag_desc);
  885. /* Propagate original priority value */
  886. head_skb->priority = frag_desc->priority;
  887. if (trace_print_tcp_rx_enabled()) {
  888. char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN];
  889. if (!frag_desc->hdrs_valid && !frag_desc->trans_len)
  890. goto skip_trace_print_tcp_rx;
  891. memset(saddr, 0, INET6_ADDRSTRLEN);
  892. memset(daddr, 0, INET6_ADDRSTRLEN);
  893. if (head_skb->protocol == htons(ETH_P_IP)) {
  894. if (ip_hdr(head_skb)->protocol != IPPROTO_TCP)
  895. goto skip_trace_print_tcp_rx;
  896. snprintf(saddr, INET6_ADDRSTRLEN, "%pI4", &ip_hdr(head_skb)->saddr);
  897. snprintf(daddr, INET6_ADDRSTRLEN, "%pI4", &ip_hdr(head_skb)->daddr);
  898. }
  899. if (head_skb->protocol == htons(ETH_P_IPV6)) {
  900. if (ipv6_hdr(head_skb)->nexthdr != IPPROTO_TCP)
  901. goto skip_trace_print_tcp_rx;
  902. snprintf(saddr, INET6_ADDRSTRLEN, "%pI6", &ipv6_hdr(head_skb)->saddr);
  903. snprintf(daddr, INET6_ADDRSTRLEN, "%pI6", &ipv6_hdr(head_skb)->daddr);
  904. }
  905. trace_print_tcp_rx(head_skb, saddr, daddr, tcp_hdr(head_skb));
  906. rmnet_descriptor_trace_pfn(head_skb);
  907. }
  908. skip_trace_print_tcp_rx:
  909. if (trace_print_udp_rx_enabled()) {
  910. char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN];
  911. u16 ip_id = 0;
  912. if (!frag_desc->hdrs_valid && !frag_desc->trans_len)
  913. goto skip_trace_print_udp_rx;
  914. memset(saddr, 0, INET6_ADDRSTRLEN);
  915. memset(daddr, 0, INET6_ADDRSTRLEN);
  916. if (head_skb->protocol == htons(ETH_P_IP)) {
  917. if (ip_hdr(head_skb)->protocol != IPPROTO_UDP)
  918. goto skip_trace_print_udp_rx;
  919. snprintf(saddr, INET6_ADDRSTRLEN, "%pI4", &ip_hdr(head_skb)->saddr);
  920. snprintf(daddr, INET6_ADDRSTRLEN, "%pI4", &ip_hdr(head_skb)->daddr);
  921. ip_id = ntohs(ip_hdr(head_skb)->id);
  922. }
  923. if (head_skb->protocol == htons(ETH_P_IPV6)) {
  924. if (ipv6_hdr(head_skb)->nexthdr != IPPROTO_UDP)
  925. goto skip_trace_print_udp_rx;
  926. snprintf(saddr, INET6_ADDRSTRLEN, "%pI6", &ipv6_hdr(head_skb)->saddr);
  927. snprintf(daddr, INET6_ADDRSTRLEN, "%pI6", &ipv6_hdr(head_skb)->daddr);
  928. }
  929. trace_print_udp_rx(head_skb, saddr, daddr, udp_hdr(head_skb), ip_id);
  930. rmnet_descriptor_trace_pfn(head_skb);
  931. }
  932. skip_trace_print_udp_rx:
  933. return head_skb;
  934. }
  935. /* Deliver the packets contained within a frag descriptor */
  936. void rmnet_frag_deliver(struct rmnet_frag_descriptor *frag_desc,
  937. struct rmnet_port *port)
  938. {
  939. struct sk_buff *skb;
  940. skb = rmnet_alloc_skb(frag_desc, port);
  941. if (skb)
  942. rmnet_deliver_skb(skb, port);
  943. rmnet_recycle_frag_descriptor(frag_desc, port);
  944. }
  945. EXPORT_SYMBOL(rmnet_frag_deliver);
  946. static void __rmnet_frag_segment_data(struct rmnet_frag_descriptor *coal_desc,
  947. struct rmnet_port *port,
  948. struct list_head *list, u8 pkt_id,
  949. bool csum_valid)
  950. {
  951. struct rmnet_priv *priv = netdev_priv(coal_desc->dev);
  952. struct rmnet_frag_descriptor *new_desc;
  953. u32 dlen = coal_desc->gso_size * coal_desc->gso_segs;
  954. u32 hlen = coal_desc->ip_len + coal_desc->trans_len;
  955. u32 offset = hlen + coal_desc->data_offset;
  956. int rc;
  957. new_desc = rmnet_get_frag_descriptor(port);
  958. if (!new_desc)
  959. return;
  960. /* Header information and most metadata is the same as the original */
  961. memcpy(new_desc, coal_desc, sizeof(*coal_desc));
  962. INIT_LIST_HEAD(&new_desc->list);
  963. INIT_LIST_HEAD(&new_desc->frags);
  964. new_desc->len = 0;
  965. /* Add the header fragments */
  966. rc = rmnet_frag_descriptor_add_frags_from(new_desc, coal_desc, 0,
  967. hlen);
  968. if (rc < 0)
  969. goto recycle;
  970. /* Add in the data fragments */
  971. rc = rmnet_frag_descriptor_add_frags_from(new_desc, coal_desc, offset,
  972. dlen);
  973. if (rc < 0)
  974. goto recycle;
  975. /* Update protocol-specific metadata */
  976. if (coal_desc->trans_proto == IPPROTO_TCP) {
  977. struct tcphdr *th, __th;
  978. th = rmnet_frag_header_ptr(coal_desc, coal_desc->ip_len,
  979. sizeof(*th), &__th);
  980. if (!th)
  981. goto recycle;
  982. new_desc->tcp_seq_set = 1;
  983. new_desc->tcp_seq = htonl(ntohl(th->seq) +
  984. coal_desc->data_offset);
  985. /* Don't allow any dangerous flags to appear in any segments
  986. * other than the last.
  987. */
  988. if (th->fin || th->psh) {
  989. if (offset + dlen < coal_desc->len) {
  990. __be32 flag_word = tcp_flag_word(th);
  991. /* Clear the FIN and PSH flags from this
  992. * segment.
  993. */
  994. flag_word &= ~TCP_FLAG_FIN;
  995. flag_word &= ~TCP_FLAG_PSH;
  996. new_desc->tcp_flags_set = 1;
  997. new_desc->tcp_flags = *((__be16 *)&flag_word);
  998. }
  999. }
  1000. } else if (coal_desc->trans_proto == IPPROTO_UDP) {
  1001. struct udphdr *uh, __uh;
  1002. uh = rmnet_frag_header_ptr(coal_desc, coal_desc->ip_len,
  1003. sizeof(*uh), &__uh);
  1004. if (!uh)
  1005. goto recycle;
  1006. if (coal_desc->ip_proto == 4 && !uh->check)
  1007. csum_valid = true;
  1008. }
  1009. if (coal_desc->ip_proto == 4) {
  1010. struct iphdr *iph, __iph;
  1011. iph = rmnet_frag_header_ptr(coal_desc, 0, sizeof(*iph),
  1012. &__iph);
  1013. if (!iph)
  1014. goto recycle;
  1015. new_desc->ip_id_set = 1;
  1016. new_desc->ip_id = htons(ntohs(iph->id) + coal_desc->pkt_id);
  1017. }
  1018. new_desc->csum_valid = csum_valid;
  1019. priv->stats.coal.coal_reconstruct++;
  1020. /* Update meta information to move past the data we just segmented */
  1021. coal_desc->data_offset += dlen;
  1022. coal_desc->pkt_id = pkt_id + 1;
  1023. coal_desc->gso_segs = 0;
  1024. /* Only relevant for the first segment to avoid overcoutning */
  1025. coal_desc->coal_bytes = 0;
  1026. coal_desc->coal_bufsize = 0;
  1027. list_add_tail(&new_desc->list, list);
  1028. return;
  1029. recycle:
  1030. rmnet_recycle_frag_descriptor(new_desc, port);
  1031. }
  1032. static bool rmnet_frag_validate_csum(struct rmnet_frag_descriptor *frag_desc)
  1033. {
  1034. u8 *data = rmnet_frag_data_ptr(frag_desc);
  1035. unsigned int datagram_len;
  1036. __wsum csum;
  1037. __sum16 pseudo;
  1038. /* Keep analysis tools happy, since they will see that
  1039. * rmnet_frag_data_ptr() could return NULL. It can't in this case,
  1040. * since we can't get this far otherwise...
  1041. */
  1042. if (unlikely(!data))
  1043. return false;
  1044. datagram_len = frag_desc->len - frag_desc->ip_len;
  1045. if (frag_desc->ip_proto == 4) {
  1046. struct iphdr *iph = (struct iphdr *)data;
  1047. pseudo = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1048. datagram_len,
  1049. frag_desc->trans_proto, 0);
  1050. } else {
  1051. struct ipv6hdr *ip6h = (struct ipv6hdr *)data;
  1052. pseudo = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  1053. datagram_len, frag_desc->trans_proto,
  1054. 0);
  1055. }
  1056. csum = csum_partial(data + frag_desc->ip_len, datagram_len,
  1057. csum_unfold(pseudo));
  1058. return !csum_fold(csum);
  1059. }
  1060. /* Converts the coalesced frame into a list of descriptors */
  1061. static void
  1062. rmnet_frag_segment_coal_data(struct rmnet_frag_descriptor *coal_desc,
  1063. u64 nlo_err_mask, struct rmnet_port *port,
  1064. struct list_head *list)
  1065. {
  1066. struct rmnet_priv *priv = netdev_priv(coal_desc->dev);
  1067. struct rmnet_map_v5_coal_header coal_hdr;
  1068. struct rmnet_fragment *frag;
  1069. u8 *version;
  1070. u16 pkt_len;
  1071. u8 pkt, total_pkt = 0;
  1072. u8 nlo;
  1073. bool gro = coal_desc->dev->features & NETIF_F_GRO_HW;
  1074. bool zero_csum = false;
  1075. /* Copy the coal header into our local storage before pulling it. It's
  1076. * possible that this header (or part of it) is the last port of a page
  1077. * a pulling it off would cause it to be freed. Referring back to the
  1078. * header would be invalid in that case.
  1079. */
  1080. if (rmnet_frag_copy_data(coal_desc, sizeof(struct rmnet_map_header),
  1081. sizeof(coal_hdr), &coal_hdr) < 0)
  1082. return;
  1083. /* Pull off the headers we no longer need */
  1084. if (!rmnet_frag_pull(coal_desc, port, sizeof(struct rmnet_map_header) +
  1085. sizeof(coal_hdr)))
  1086. return;
  1087. /* By definition, this byte is linear, and the first byte on the
  1088. * first fragment. ;) Hence why no header_ptr() call is needed
  1089. * for it.
  1090. */
  1091. version = rmnet_frag_data_ptr(coal_desc);
  1092. if (unlikely(!version))
  1093. return;
  1094. if ((*version & 0xF0) == 0x40) {
  1095. struct iphdr *iph, __iph;
  1096. iph = rmnet_frag_header_ptr(coal_desc, 0, sizeof(*iph),
  1097. &__iph);
  1098. if (!iph)
  1099. return;
  1100. coal_desc->ip_proto = 4;
  1101. coal_desc->ip_len = iph->ihl * 4;
  1102. coal_desc->trans_proto = iph->protocol;
  1103. /* Don't allow coalescing of any packets with IP options */
  1104. if (iph->ihl != 5)
  1105. gro = false;
  1106. } else if ((*version & 0xF0) == 0x60) {
  1107. struct ipv6hdr *ip6h, __ip6h;
  1108. int ip_len;
  1109. __be16 frag_off;
  1110. bool frag_hdr;
  1111. u8 protocol;
  1112. ip6h = rmnet_frag_header_ptr(coal_desc, 0, sizeof(*ip6h),
  1113. &__ip6h);
  1114. if (!ip6h)
  1115. return;
  1116. coal_desc->ip_proto = 6;
  1117. protocol = ip6h->nexthdr;
  1118. ip_len = rmnet_frag_ipv6_skip_exthdr(coal_desc,
  1119. sizeof(*ip6h),
  1120. &protocol,
  1121. &frag_off,
  1122. &frag_hdr);
  1123. coal_desc->trans_proto = protocol;
  1124. /* If we run into a problem, or this is fragmented packet
  1125. * (which should technically not be possible, if the HW
  1126. * works as intended...), bail.
  1127. */
  1128. if (ip_len < 0 || frag_off) {
  1129. priv->stats.coal.coal_ip_invalid++;
  1130. return;
  1131. }
  1132. if (frag_hdr) {
  1133. /* There is a fragment header, but this is not a
  1134. * fragmented packet. We can handle this, but it
  1135. * cannot be coalesced because of kernel limitations.
  1136. */
  1137. gro = false;
  1138. }
  1139. coal_desc->ip_len = (u16)ip_len;
  1140. if (coal_desc->ip_len > sizeof(*ip6h)) {
  1141. /* Don't allow coalescing of any packets with IPv6
  1142. * extension headers.
  1143. */
  1144. gro = false;
  1145. }
  1146. } else {
  1147. priv->stats.coal.coal_ip_invalid++;
  1148. return;
  1149. }
  1150. if (coal_desc->trans_proto == IPPROTO_TCP) {
  1151. struct tcphdr *th, __th;
  1152. th = rmnet_frag_header_ptr(coal_desc,
  1153. coal_desc->ip_len, sizeof(*th),
  1154. &__th);
  1155. if (!th)
  1156. return;
  1157. coal_desc->trans_len = th->doff * 4;
  1158. priv->stats.coal.coal_tcp++;
  1159. priv->stats.coal.coal_tcp_bytes += coal_desc->len;
  1160. } else if (coal_desc->trans_proto == IPPROTO_UDP) {
  1161. struct udphdr *uh, __uh;
  1162. uh = rmnet_frag_header_ptr(coal_desc,
  1163. coal_desc->ip_len, sizeof(*uh),
  1164. &__uh);
  1165. if (!uh)
  1166. return;
  1167. coal_desc->trans_len = sizeof(*uh);
  1168. priv->stats.coal.coal_udp++;
  1169. priv->stats.coal.coal_udp_bytes += coal_desc->len;
  1170. if (coal_desc->ip_proto == 4 && !uh->check)
  1171. zero_csum = true;
  1172. } else {
  1173. priv->stats.coal.coal_trans_invalid++;
  1174. return;
  1175. }
  1176. coal_desc->hdrs_valid = 1;
  1177. coal_desc->coal_bytes = coal_desc->len;
  1178. rmnet_descriptor_for_each_frag(frag, coal_desc)
  1179. coal_desc->coal_bufsize +=
  1180. page_size(skb_frag_page(&frag->frag));
  1181. if (rmnet_map_v5_csum_buggy(&coal_hdr) && !zero_csum) {
  1182. /* Mark the checksum as valid if it checks out */
  1183. if (rmnet_frag_validate_csum(coal_desc))
  1184. coal_desc->csum_valid = true;
  1185. coal_desc->gso_size = ntohs(coal_hdr.nl_pairs[0].pkt_len);
  1186. coal_desc->gso_size -= coal_desc->ip_len + coal_desc->trans_len;
  1187. coal_desc->gso_segs = coal_hdr.nl_pairs[0].num_packets;
  1188. list_add_tail(&coal_desc->list, list);
  1189. return;
  1190. }
  1191. /* Fast-forward the case where we have 1 NLO (i.e. 1 packet length),
  1192. * no checksum errors, and are allowing GRO. We can just reuse this
  1193. * descriptor unchanged.
  1194. */
  1195. if (gro && coal_hdr.num_nlos == 1 && coal_hdr.csum_valid) {
  1196. coal_desc->csum_valid = true;
  1197. coal_desc->gso_size = ntohs(coal_hdr.nl_pairs[0].pkt_len);
  1198. coal_desc->gso_size -= coal_desc->ip_len + coal_desc->trans_len;
  1199. coal_desc->gso_segs = coal_hdr.nl_pairs[0].num_packets;
  1200. list_add_tail(&coal_desc->list, list);
  1201. return;
  1202. }
  1203. /* Segment the coalesced descriptor into new packets */
  1204. for (nlo = 0; nlo < coal_hdr.num_nlos; nlo++) {
  1205. pkt_len = ntohs(coal_hdr.nl_pairs[nlo].pkt_len);
  1206. pkt_len -= coal_desc->ip_len + coal_desc->trans_len;
  1207. coal_desc->gso_size = pkt_len;
  1208. for (pkt = 0; pkt < coal_hdr.nl_pairs[nlo].num_packets;
  1209. pkt++, total_pkt++, nlo_err_mask >>= 1) {
  1210. bool csum_err = nlo_err_mask & 1;
  1211. /* Segment the packet if we're not sending the larger
  1212. * packet up the stack.
  1213. */
  1214. if (!gro) {
  1215. coal_desc->gso_segs = 1;
  1216. if (csum_err)
  1217. priv->stats.coal.coal_csum_err++;
  1218. __rmnet_frag_segment_data(coal_desc, port,
  1219. list, total_pkt,
  1220. !csum_err);
  1221. continue;
  1222. }
  1223. if (csum_err) {
  1224. priv->stats.coal.coal_csum_err++;
  1225. /* Segment out the good data */
  1226. if (coal_desc->gso_segs)
  1227. __rmnet_frag_segment_data(coal_desc,
  1228. port,
  1229. list,
  1230. total_pkt,
  1231. true);
  1232. /* Segment out the bad checksum */
  1233. coal_desc->gso_segs = 1;
  1234. __rmnet_frag_segment_data(coal_desc, port,
  1235. list, total_pkt,
  1236. false);
  1237. } else {
  1238. coal_desc->gso_segs++;
  1239. }
  1240. }
  1241. /* If we're switching NLOs, we need to send out everything from
  1242. * the previous one, if we haven't done so. NLOs only switch
  1243. * when the packet length changes.
  1244. */
  1245. if (coal_desc->gso_segs)
  1246. __rmnet_frag_segment_data(coal_desc, port, list,
  1247. total_pkt, true);
  1248. }
  1249. }
  1250. /* Record reason for coalescing pipe closure */
  1251. static void rmnet_frag_data_log_close_stats(struct rmnet_priv *priv, u8 type,
  1252. u8 code)
  1253. {
  1254. struct rmnet_coal_close_stats *stats = &priv->stats.coal.close;
  1255. switch (type) {
  1256. case RMNET_MAP_COAL_CLOSE_NON_COAL:
  1257. stats->non_coal++;
  1258. break;
  1259. case RMNET_MAP_COAL_CLOSE_IP_MISS:
  1260. stats->ip_miss++;
  1261. break;
  1262. case RMNET_MAP_COAL_CLOSE_TRANS_MISS:
  1263. stats->trans_miss++;
  1264. break;
  1265. case RMNET_MAP_COAL_CLOSE_HW:
  1266. switch (code) {
  1267. case RMNET_MAP_COAL_CLOSE_HW_NL:
  1268. stats->hw_nl++;
  1269. break;
  1270. case RMNET_MAP_COAL_CLOSE_HW_PKT:
  1271. stats->hw_pkt++;
  1272. break;
  1273. case RMNET_MAP_COAL_CLOSE_HW_BYTE:
  1274. stats->hw_byte++;
  1275. break;
  1276. case RMNET_MAP_COAL_CLOSE_HW_TIME:
  1277. stats->hw_time++;
  1278. break;
  1279. case RMNET_MAP_COAL_CLOSE_HW_EVICT:
  1280. stats->hw_evict++;
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. break;
  1286. case RMNET_MAP_COAL_CLOSE_COAL:
  1287. stats->coal++;
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. }
  1293. /* Check if the coalesced header has any incorrect values, in which case, the
  1294. * entire coalesced frame must be dropped. Then check if there are any
  1295. * checksum issues
  1296. */
  1297. static int
  1298. rmnet_frag_data_check_coal_header(struct rmnet_frag_descriptor *frag_desc,
  1299. u64 *nlo_err_mask)
  1300. {
  1301. struct rmnet_map_v5_coal_header *coal_hdr, __coal_hdr;
  1302. struct rmnet_priv *priv = netdev_priv(frag_desc->dev);
  1303. u64 mask = 0;
  1304. int i;
  1305. u8 veid, pkts = 0;
  1306. coal_hdr = rmnet_frag_header_ptr(frag_desc,
  1307. sizeof(struct rmnet_map_header),
  1308. sizeof(*coal_hdr), &__coal_hdr);
  1309. if (!coal_hdr)
  1310. return -EINVAL;
  1311. veid = coal_hdr->virtual_channel_id;
  1312. if (coal_hdr->num_nlos == 0 ||
  1313. coal_hdr->num_nlos > RMNET_MAP_V5_MAX_NLOS) {
  1314. priv->stats.coal.coal_hdr_nlo_err++;
  1315. return -EINVAL;
  1316. }
  1317. for (i = 0; i < RMNET_MAP_V5_MAX_NLOS; i++) {
  1318. /* If there is a checksum issue, we need to split
  1319. * up the skb. Rebuild the full csum error field
  1320. */
  1321. u8 err = coal_hdr->nl_pairs[i].csum_error_bitmap;
  1322. u8 pkt = coal_hdr->nl_pairs[i].num_packets;
  1323. mask |= ((u64)err) << (8 * i);
  1324. /* Track total packets in frame */
  1325. pkts += pkt;
  1326. if (pkts > RMNET_MAP_V5_MAX_PACKETS) {
  1327. priv->stats.coal.coal_hdr_pkt_err++;
  1328. return -EINVAL;
  1329. }
  1330. }
  1331. /* Track number of packets we get inside of coalesced frames */
  1332. priv->stats.coal.coal_pkts += pkts;
  1333. /* Update ethtool stats */
  1334. rmnet_frag_data_log_close_stats(priv,
  1335. coal_hdr->close_type,
  1336. coal_hdr->close_value);
  1337. if (veid < RMNET_MAX_VEID)
  1338. priv->stats.coal.coal_veid[veid]++;
  1339. *nlo_err_mask = mask;
  1340. return 0;
  1341. }
  1342. static int rmnet_frag_checksum_pkt(struct rmnet_frag_descriptor *frag_desc)
  1343. {
  1344. struct rmnet_priv *priv = netdev_priv(frag_desc->dev);
  1345. struct rmnet_fragment *frag;
  1346. int offset = sizeof(struct rmnet_map_header) +
  1347. sizeof(struct rmnet_map_v5_csum_header);
  1348. u8 *version, __version;
  1349. __wsum csum;
  1350. u16 csum_len;
  1351. version = rmnet_frag_header_ptr(frag_desc, offset, sizeof(*version),
  1352. &__version);
  1353. if (!version)
  1354. return -EINVAL;
  1355. if ((*version & 0xF0) == 0x40) {
  1356. struct iphdr *iph;
  1357. u8 __iph[60]; /* Max IP header size (0xF * 4) */
  1358. /* We need to access the entire IP header including options
  1359. * to validate its checksum. Fortunately, the version byte
  1360. * also will tell us the length, so we only need to pull
  1361. * once ;)
  1362. */
  1363. frag_desc->ip_len = (*version & 0xF) * 4;
  1364. iph = rmnet_frag_header_ptr(frag_desc, offset,
  1365. frag_desc->ip_len,
  1366. __iph);
  1367. if (!iph || ip_is_fragment(iph))
  1368. return -EINVAL;
  1369. /* Length needs to be sensible */
  1370. csum_len = ntohs(iph->tot_len);
  1371. if (csum_len > frag_desc->len - offset)
  1372. return -EINVAL;
  1373. csum_len -= frag_desc->ip_len;
  1374. /* IPv4 checksum must be valid */
  1375. if (ip_fast_csum((u8 *)iph, iph->ihl)) {
  1376. priv->stats.csum_sw++;
  1377. return 0;
  1378. }
  1379. frag_desc->ip_proto = 4;
  1380. frag_desc->trans_proto = iph->protocol;
  1381. csum = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1382. csum_len,
  1383. frag_desc->trans_proto, 0);
  1384. } else if ((*version & 0xF0) == 0x60) {
  1385. struct ipv6hdr *ip6h, __ip6h;
  1386. int ip_len;
  1387. __be16 frag_off;
  1388. bool frag_hdr;
  1389. u8 protocol;
  1390. ip6h = rmnet_frag_header_ptr(frag_desc, offset, sizeof(*ip6h),
  1391. &__ip6h);
  1392. if (!ip6h)
  1393. return -EINVAL;
  1394. frag_desc->ip_proto = 6;
  1395. protocol = ip6h->nexthdr;
  1396. ip_len = rmnet_frag_ipv6_skip_exthdr(frag_desc,
  1397. offset + sizeof(*ip6h),
  1398. &protocol, &frag_off,
  1399. &frag_hdr);
  1400. if (ip_len < 0 || frag_off)
  1401. return -EINVAL;
  1402. /* Length needs to be sensible */
  1403. frag_desc->ip_len = (u16)ip_len;
  1404. csum_len = ntohs(ip6h->payload_len);
  1405. if (csum_len + frag_desc->ip_len > frag_desc->len - offset)
  1406. return -EINVAL;
  1407. frag_desc->trans_proto = protocol;
  1408. csum = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  1409. csum_len,
  1410. frag_desc->trans_proto, 0);
  1411. } else {
  1412. /* Not checksumable */
  1413. return -EINVAL;
  1414. }
  1415. /* Protocol check */
  1416. if (frag_desc->trans_proto != IPPROTO_TCP &&
  1417. frag_desc->trans_proto != IPPROTO_UDP)
  1418. return -EINVAL;
  1419. offset += frag_desc->ip_len;
  1420. /* Check for UDP zero csum packets */
  1421. if (frag_desc->trans_proto == IPPROTO_UDP) {
  1422. struct udphdr *uh, __uh;
  1423. uh = rmnet_frag_header_ptr(frag_desc, offset, sizeof(*uh),
  1424. &__uh);
  1425. if (!uh)
  1426. return -EINVAL;
  1427. if (!uh->check) {
  1428. if (frag_desc->ip_proto == 4) {
  1429. /* Zero checksum is valid */
  1430. priv->stats.csum_sw++;
  1431. return 1;
  1432. }
  1433. /* Not valid in IPv6 */
  1434. priv->stats.csum_sw++;
  1435. return 0;
  1436. }
  1437. }
  1438. /* Walk the frags and checksum each chunk */
  1439. list_for_each_entry(frag, &frag_desc->frags, list) {
  1440. u32 frag_size = skb_frag_size(&frag->frag);
  1441. if (!csum_len)
  1442. break;
  1443. if (offset < frag_size) {
  1444. void *addr = skb_frag_address(&frag->frag) + offset;
  1445. u32 len = min_t(u32, csum_len, frag_size - offset);
  1446. /* Checksum 'len' bytes and add them in */
  1447. csum = csum_partial(addr, len, csum);
  1448. csum_len -= len;
  1449. offset = 0;
  1450. } else {
  1451. offset -= frag_size;
  1452. }
  1453. }
  1454. priv->stats.csum_sw++;
  1455. return !csum_fold(csum);
  1456. }
  1457. /* Process a QMAPv5 packet header */
  1458. int rmnet_frag_process_next_hdr_packet(struct rmnet_frag_descriptor *frag_desc,
  1459. struct rmnet_port *port,
  1460. struct list_head *list,
  1461. u16 len)
  1462. {
  1463. struct rmnet_map_v5_csum_header *csum_hdr, __csum_hdr;
  1464. struct rmnet_priv *priv = netdev_priv(frag_desc->dev);
  1465. u64 nlo_err_mask;
  1466. u32 offset = sizeof(struct rmnet_map_header);
  1467. int rc = 0;
  1468. /* Grab the header type. It's easier to grab enough for a full csum
  1469. * offload header here since it's only 8 bytes and then check the
  1470. * header type using that. This also doubles as a check to make sure
  1471. * there's enough data after the QMAP header to ensure that another
  1472. * header is present.
  1473. */
  1474. csum_hdr = rmnet_frag_header_ptr(frag_desc, offset, sizeof(*csum_hdr),
  1475. &__csum_hdr);
  1476. if (!csum_hdr)
  1477. return -EINVAL;
  1478. switch (csum_hdr->header_type) {
  1479. case RMNET_MAP_HEADER_TYPE_COALESCING:
  1480. priv->stats.coal.coal_rx++;
  1481. rc = rmnet_frag_data_check_coal_header(frag_desc,
  1482. &nlo_err_mask);
  1483. if (rc)
  1484. return rc;
  1485. rmnet_frag_segment_coal_data(frag_desc, nlo_err_mask, port,
  1486. list);
  1487. if (list_first_entry(list, struct rmnet_frag_descriptor,
  1488. list) != frag_desc)
  1489. rmnet_recycle_frag_descriptor(frag_desc, port);
  1490. break;
  1491. case RMNET_MAP_HEADER_TYPE_CSUM_OFFLOAD:
  1492. if (unlikely(!(frag_desc->dev->features & NETIF_F_RXCSUM))) {
  1493. priv->stats.csum_sw++;
  1494. } else if (csum_hdr->csum_valid_required) {
  1495. priv->stats.csum_ok++;
  1496. frag_desc->csum_valid = true;
  1497. } else {
  1498. int valid = rmnet_frag_checksum_pkt(frag_desc);
  1499. if (valid < 0) {
  1500. priv->stats.csum_validation_failed++;
  1501. } else if (valid) {
  1502. /* All's good */
  1503. priv->stats.csum_ok++;
  1504. frag_desc->csum_valid = true;
  1505. } else {
  1506. /* Checksum is actually bad */
  1507. priv->stats.csum_valid_unset++;
  1508. }
  1509. }
  1510. if (!rmnet_frag_pull(frag_desc, port,
  1511. offset + sizeof(*csum_hdr))) {
  1512. rc = -EINVAL;
  1513. break;
  1514. }
  1515. /* Remove padding only for csum offload packets.
  1516. * Coalesced packets should never have padding.
  1517. */
  1518. if (!rmnet_frag_trim(frag_desc, port, len)) {
  1519. rc = -EINVAL;
  1520. break;
  1521. }
  1522. list_del_init(&frag_desc->list);
  1523. list_add_tail(&frag_desc->list, list);
  1524. break;
  1525. default:
  1526. rc = -EINVAL;
  1527. break;
  1528. }
  1529. return rc;
  1530. }
  1531. /* Perf hook handler */
  1532. rmnet_perf_desc_hook_t rmnet_perf_desc_entry __rcu __read_mostly;
  1533. EXPORT_SYMBOL(rmnet_perf_desc_entry);
  1534. static void
  1535. __rmnet_frag_ingress_handler(struct rmnet_frag_descriptor *frag_desc,
  1536. struct rmnet_port *port)
  1537. {
  1538. rmnet_perf_desc_hook_t rmnet_perf_ingress;
  1539. struct rmnet_map_header *qmap, __qmap;
  1540. struct rmnet_endpoint *ep;
  1541. struct rmnet_frag_descriptor *frag, *tmp;
  1542. LIST_HEAD(segs);
  1543. u16 len, pad;
  1544. u8 mux_id;
  1545. bool skip_perf = (frag_desc->priority == 0xda1a);
  1546. qmap = rmnet_frag_header_ptr(frag_desc, 0, sizeof(*qmap), &__qmap);
  1547. if (!qmap)
  1548. goto recycle;
  1549. mux_id = qmap->mux_id;
  1550. pad = qmap->pad_len;
  1551. len = ntohs(qmap->pkt_len) - pad;
  1552. if (qmap->cd_bit) {
  1553. qmi_rmnet_set_dl_msg_active(port);
  1554. if (port->data_format & RMNET_INGRESS_FORMAT_DL_MARKER) {
  1555. rmnet_frag_flow_command(frag_desc, port, len);
  1556. goto recycle;
  1557. }
  1558. if (port->data_format & RMNET_FLAGS_INGRESS_MAP_COMMANDS)
  1559. rmnet_frag_command(frag_desc, qmap, port);
  1560. goto recycle;
  1561. }
  1562. if (mux_id >= RMNET_MAX_LOGICAL_EP)
  1563. goto recycle;
  1564. ep = rmnet_get_endpoint(port, mux_id);
  1565. if (!ep)
  1566. goto recycle;
  1567. frag_desc->dev = ep->egress_dev;
  1568. /* Handle QMAPv5 packet */
  1569. if (qmap->next_hdr &&
  1570. (port->data_format & (RMNET_FLAGS_INGRESS_COALESCE |
  1571. RMNET_PRIV_FLAGS_INGRESS_MAP_CKSUMV5))) {
  1572. if (rmnet_frag_process_next_hdr_packet(frag_desc, port, &segs,
  1573. len))
  1574. goto recycle;
  1575. } else {
  1576. /* We only have the main QMAP header to worry about */
  1577. if (!rmnet_frag_pull(frag_desc, port, sizeof(*qmap)))
  1578. return;
  1579. if (!rmnet_frag_trim(frag_desc, port, len))
  1580. return;
  1581. list_add_tail(&frag_desc->list, &segs);
  1582. }
  1583. if (port->data_format & RMNET_INGRESS_FORMAT_PS)
  1584. qmi_rmnet_work_maybe_restart(port,
  1585. list_first_entry_or_null(&segs,
  1586. struct rmnet_frag_descriptor,
  1587. list),
  1588. NULL);
  1589. if (skip_perf)
  1590. goto no_perf;
  1591. rcu_read_lock();
  1592. rmnet_perf_ingress = rcu_dereference(rmnet_perf_desc_entry);
  1593. if (rmnet_perf_ingress) {
  1594. list_for_each_entry_safe(frag, tmp, &segs, list) {
  1595. list_del_init(&frag->list);
  1596. rmnet_perf_ingress(frag, port);
  1597. }
  1598. rcu_read_unlock();
  1599. return;
  1600. }
  1601. rcu_read_unlock();
  1602. no_perf:
  1603. list_for_each_entry_safe(frag, tmp, &segs, list) {
  1604. list_del_init(&frag->list);
  1605. rmnet_frag_deliver(frag, port);
  1606. }
  1607. return;
  1608. recycle:
  1609. rmnet_recycle_frag_descriptor(frag_desc, port);
  1610. }
  1611. /* Notify perf at the end of SKB chain */
  1612. rmnet_perf_chain_hook_t rmnet_perf_chain_end __rcu __read_mostly;
  1613. EXPORT_SYMBOL(rmnet_perf_chain_end);
  1614. void rmnet_descriptor_classify_chain_count(u64 chain_count,
  1615. struct rmnet_port *port)
  1616. {
  1617. u64 index;
  1618. if (chain_count >= 60) {
  1619. port->stats.dl_chain_stat[6] += chain_count;
  1620. return;
  1621. }
  1622. index = chain_count;
  1623. do_div(index, 10);
  1624. port->stats.dl_chain_stat[index] += chain_count;
  1625. }
  1626. void rmnet_descriptor_classify_frag_count(u64 frag_count,
  1627. struct rmnet_port *port)
  1628. {
  1629. u64 index;
  1630. if (frag_count <= 1) {
  1631. port->stats.dl_frag_stat_1 += frag_count;
  1632. return;
  1633. }
  1634. if (frag_count >= 16) {
  1635. port->stats.dl_frag_stat[4] += frag_count;
  1636. return;
  1637. }
  1638. index = frag_count;
  1639. do_div(index, 4);
  1640. port->stats.dl_frag_stat[index] += frag_count;
  1641. }
  1642. void rmnet_frag_ingress_handler(struct sk_buff *skb,
  1643. struct rmnet_port *port)
  1644. {
  1645. rmnet_perf_chain_hook_t rmnet_perf_opt_chain_end;
  1646. LIST_HEAD(desc_list);
  1647. bool skip_perf = (skb->priority == 0xda1a);
  1648. u64 chain_count = 0;
  1649. struct sk_buff *head = skb;
  1650. /* Deaggregation and freeing of HW originating
  1651. * buffers is done within here
  1652. */
  1653. while (skb) {
  1654. struct sk_buff *skb_frag;
  1655. chain_count++;
  1656. rmnet_descriptor_classify_frag_count(skb_shinfo(skb)->nr_frags,
  1657. port);
  1658. rmnet_frag_deaggregate(skb, port, &desc_list, skb->priority);
  1659. if (!list_empty(&desc_list)) {
  1660. struct rmnet_frag_descriptor *frag_desc, *tmp;
  1661. list_for_each_entry_safe(frag_desc, tmp, &desc_list,
  1662. list) {
  1663. list_del_init(&frag_desc->list);
  1664. __rmnet_frag_ingress_handler(frag_desc, port);
  1665. }
  1666. }
  1667. if (skb == head) {
  1668. skb_frag = skb_shinfo(skb)->frag_list;
  1669. skb_shinfo(skb)->frag_list = NULL;
  1670. } else {
  1671. skb_frag = skb->next;
  1672. skb->next = NULL;
  1673. }
  1674. consume_skb(skb);
  1675. skb = skb_frag;
  1676. }
  1677. rmnet_descriptor_classify_chain_count(chain_count, port);
  1678. if (skip_perf)
  1679. return;
  1680. rcu_read_lock();
  1681. rmnet_perf_opt_chain_end = rcu_dereference(rmnet_perf_chain_end);
  1682. if (rmnet_perf_opt_chain_end)
  1683. rmnet_perf_opt_chain_end();
  1684. rcu_read_unlock();
  1685. }
  1686. void rmnet_descriptor_deinit(struct rmnet_port *port)
  1687. {
  1688. struct rmnet_frag_descriptor_pool *pool;
  1689. struct rmnet_frag_descriptor *frag_desc, *tmp;
  1690. pool = port->frag_desc_pool;
  1691. if (pool) {
  1692. list_for_each_entry_safe(frag_desc, tmp, &pool->free_list, list) {
  1693. kfree(frag_desc);
  1694. pool->pool_size--;
  1695. }
  1696. kfree(pool);
  1697. port->frag_desc_pool = NULL;
  1698. }
  1699. }
  1700. int rmnet_descriptor_init(struct rmnet_port *port)
  1701. {
  1702. struct rmnet_frag_descriptor_pool *pool;
  1703. int i;
  1704. spin_lock_init(&port->desc_pool_lock);
  1705. pool = kzalloc(sizeof(*pool), GFP_ATOMIC);
  1706. if (!pool)
  1707. return -ENOMEM;
  1708. INIT_LIST_HEAD(&pool->free_list);
  1709. port->frag_desc_pool = pool;
  1710. for (i = 0; i < RMNET_FRAG_DESCRIPTOR_POOL_SIZE; i++) {
  1711. struct rmnet_frag_descriptor *frag_desc;
  1712. frag_desc = kzalloc(sizeof(*frag_desc), GFP_ATOMIC);
  1713. if (!frag_desc)
  1714. return -ENOMEM;
  1715. INIT_LIST_HEAD(&frag_desc->list);
  1716. INIT_LIST_HEAD(&frag_desc->frags);
  1717. list_add_tail(&frag_desc->list, &pool->free_list);
  1718. pool->pool_size++;
  1719. }
  1720. return 0;
  1721. }