rmnet_descriptor.c 50 KB

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