net_rx_pkt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * QEMU RX packets abstractions
  3. *
  4. * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
  5. *
  6. * Developed by Daynix Computing LTD (http://www.daynix.com)
  7. *
  8. * Authors:
  9. * Dmitry Fleytman <dmitry@daynix.com>
  10. * Tamir Shomer <tamirs@daynix.com>
  11. * Yan Vugenfirer <yan@daynix.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  14. * See the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include "qemu/osdep.h"
  18. #include "trace.h"
  19. #include "net_rx_pkt.h"
  20. #include "net/checksum.h"
  21. #include "net/tap.h"
  22. struct NetRxPkt {
  23. struct virtio_net_hdr virt_hdr;
  24. uint8_t ehdr_buf[sizeof(struct eth_header) + sizeof(struct vlan_header)];
  25. struct iovec *vec;
  26. uint16_t vec_len_total;
  27. uint16_t vec_len;
  28. uint32_t tot_len;
  29. uint16_t tci;
  30. size_t ehdr_buf_len;
  31. bool has_virt_hdr;
  32. eth_pkt_types_e packet_type;
  33. /* Analysis results */
  34. bool isip4;
  35. bool isip6;
  36. bool isudp;
  37. bool istcp;
  38. size_t l3hdr_off;
  39. size_t l4hdr_off;
  40. size_t l5hdr_off;
  41. eth_ip6_hdr_info ip6hdr_info;
  42. eth_ip4_hdr_info ip4hdr_info;
  43. eth_l4_hdr_info l4hdr_info;
  44. };
  45. void net_rx_pkt_init(struct NetRxPkt **pkt, bool has_virt_hdr)
  46. {
  47. struct NetRxPkt *p = g_malloc0(sizeof *p);
  48. p->has_virt_hdr = has_virt_hdr;
  49. p->vec = NULL;
  50. p->vec_len_total = 0;
  51. *pkt = p;
  52. }
  53. void net_rx_pkt_uninit(struct NetRxPkt *pkt)
  54. {
  55. if (pkt->vec_len_total != 0) {
  56. g_free(pkt->vec);
  57. }
  58. g_free(pkt);
  59. }
  60. struct virtio_net_hdr *net_rx_pkt_get_vhdr(struct NetRxPkt *pkt)
  61. {
  62. assert(pkt);
  63. return &pkt->virt_hdr;
  64. }
  65. static inline void
  66. net_rx_pkt_iovec_realloc(struct NetRxPkt *pkt,
  67. int new_iov_len)
  68. {
  69. if (pkt->vec_len_total < new_iov_len) {
  70. g_free(pkt->vec);
  71. pkt->vec = g_malloc(sizeof(*pkt->vec) * new_iov_len);
  72. pkt->vec_len_total = new_iov_len;
  73. }
  74. }
  75. static void
  76. net_rx_pkt_pull_data(struct NetRxPkt *pkt,
  77. const struct iovec *iov, int iovcnt,
  78. size_t ploff)
  79. {
  80. uint32_t pllen = iov_size(iov, iovcnt) - ploff;
  81. if (pkt->ehdr_buf_len) {
  82. net_rx_pkt_iovec_realloc(pkt, iovcnt + 1);
  83. pkt->vec[0].iov_base = pkt->ehdr_buf;
  84. pkt->vec[0].iov_len = pkt->ehdr_buf_len;
  85. pkt->tot_len = pllen + pkt->ehdr_buf_len;
  86. pkt->vec_len = iov_copy(pkt->vec + 1, pkt->vec_len_total - 1,
  87. iov, iovcnt, ploff, pllen) + 1;
  88. } else {
  89. net_rx_pkt_iovec_realloc(pkt, iovcnt);
  90. pkt->tot_len = pllen;
  91. pkt->vec_len = iov_copy(pkt->vec, pkt->vec_len_total,
  92. iov, iovcnt, ploff, pkt->tot_len);
  93. }
  94. eth_get_protocols(pkt->vec, pkt->vec_len, &pkt->isip4, &pkt->isip6,
  95. &pkt->isudp, &pkt->istcp,
  96. &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
  97. &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
  98. trace_net_rx_pkt_parsed(pkt->isip4, pkt->isip6, pkt->isudp, pkt->istcp,
  99. pkt->l3hdr_off, pkt->l4hdr_off, pkt->l5hdr_off);
  100. }
  101. void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
  102. const struct iovec *iov, int iovcnt,
  103. size_t iovoff, bool strip_vlan)
  104. {
  105. uint16_t tci = 0;
  106. uint16_t ploff = iovoff;
  107. assert(pkt);
  108. if (strip_vlan) {
  109. pkt->ehdr_buf_len = eth_strip_vlan(iov, iovcnt, iovoff, pkt->ehdr_buf,
  110. &ploff, &tci);
  111. } else {
  112. pkt->ehdr_buf_len = 0;
  113. }
  114. pkt->tci = tci;
  115. net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
  116. }
  117. void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
  118. const struct iovec *iov, int iovcnt,
  119. size_t iovoff, bool strip_vlan,
  120. uint16_t vet)
  121. {
  122. uint16_t tci = 0;
  123. uint16_t ploff = iovoff;
  124. assert(pkt);
  125. if (strip_vlan) {
  126. pkt->ehdr_buf_len = eth_strip_vlan_ex(iov, iovcnt, iovoff, vet,
  127. pkt->ehdr_buf,
  128. &ploff, &tci);
  129. } else {
  130. pkt->ehdr_buf_len = 0;
  131. }
  132. pkt->tci = tci;
  133. net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
  134. }
  135. void net_rx_pkt_dump(struct NetRxPkt *pkt)
  136. {
  137. #ifdef NET_RX_PKT_DEBUG
  138. assert(pkt);
  139. printf("RX PKT: tot_len: %d, ehdr_buf_len: %lu, vlan_tag: %d\n",
  140. pkt->tot_len, pkt->ehdr_buf_len, pkt->tci);
  141. #endif
  142. }
  143. void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
  144. eth_pkt_types_e packet_type)
  145. {
  146. assert(pkt);
  147. pkt->packet_type = packet_type;
  148. }
  149. eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt)
  150. {
  151. assert(pkt);
  152. return pkt->packet_type;
  153. }
  154. size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt)
  155. {
  156. assert(pkt);
  157. return pkt->tot_len;
  158. }
  159. void net_rx_pkt_set_protocols(struct NetRxPkt *pkt, const void *data,
  160. size_t len)
  161. {
  162. const struct iovec iov = {
  163. .iov_base = (void *)data,
  164. .iov_len = len
  165. };
  166. assert(pkt);
  167. eth_get_protocols(&iov, 1, &pkt->isip4, &pkt->isip6,
  168. &pkt->isudp, &pkt->istcp,
  169. &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
  170. &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
  171. }
  172. void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
  173. bool *isip4, bool *isip6,
  174. bool *isudp, bool *istcp)
  175. {
  176. assert(pkt);
  177. *isip4 = pkt->isip4;
  178. *isip6 = pkt->isip6;
  179. *isudp = pkt->isudp;
  180. *istcp = pkt->istcp;
  181. }
  182. size_t net_rx_pkt_get_l3_hdr_offset(struct NetRxPkt *pkt)
  183. {
  184. assert(pkt);
  185. return pkt->l3hdr_off;
  186. }
  187. size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt)
  188. {
  189. assert(pkt);
  190. return pkt->l4hdr_off;
  191. }
  192. size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt)
  193. {
  194. assert(pkt);
  195. return pkt->l5hdr_off;
  196. }
  197. eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt)
  198. {
  199. return &pkt->ip6hdr_info;
  200. }
  201. eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt)
  202. {
  203. return &pkt->ip4hdr_info;
  204. }
  205. eth_l4_hdr_info *net_rx_pkt_get_l4_info(struct NetRxPkt *pkt)
  206. {
  207. return &pkt->l4hdr_info;
  208. }
  209. static inline void
  210. _net_rx_rss_add_chunk(uint8_t *rss_input, size_t *bytes_written,
  211. void *ptr, size_t size)
  212. {
  213. memcpy(&rss_input[*bytes_written], ptr, size);
  214. trace_net_rx_pkt_rss_add_chunk(ptr, size, *bytes_written);
  215. *bytes_written += size;
  216. }
  217. static inline void
  218. _net_rx_rss_prepare_ip4(uint8_t *rss_input,
  219. struct NetRxPkt *pkt,
  220. size_t *bytes_written)
  221. {
  222. struct ip_header *ip4_hdr = &pkt->ip4hdr_info.ip4_hdr;
  223. _net_rx_rss_add_chunk(rss_input, bytes_written,
  224. &ip4_hdr->ip_src, sizeof(uint32_t));
  225. _net_rx_rss_add_chunk(rss_input, bytes_written,
  226. &ip4_hdr->ip_dst, sizeof(uint32_t));
  227. }
  228. static inline void
  229. _net_rx_rss_prepare_ip6(uint8_t *rss_input,
  230. struct NetRxPkt *pkt,
  231. bool ipv6ex, size_t *bytes_written)
  232. {
  233. eth_ip6_hdr_info *ip6info = &pkt->ip6hdr_info;
  234. _net_rx_rss_add_chunk(rss_input, bytes_written,
  235. (ipv6ex && ip6info->rss_ex_src_valid) ? &ip6info->rss_ex_src
  236. : &ip6info->ip6_hdr.ip6_src,
  237. sizeof(struct in6_address));
  238. _net_rx_rss_add_chunk(rss_input, bytes_written,
  239. (ipv6ex && ip6info->rss_ex_dst_valid) ? &ip6info->rss_ex_dst
  240. : &ip6info->ip6_hdr.ip6_dst,
  241. sizeof(struct in6_address));
  242. }
  243. static inline void
  244. _net_rx_rss_prepare_tcp(uint8_t *rss_input,
  245. struct NetRxPkt *pkt,
  246. size_t *bytes_written)
  247. {
  248. struct tcp_header *tcphdr = &pkt->l4hdr_info.hdr.tcp;
  249. _net_rx_rss_add_chunk(rss_input, bytes_written,
  250. &tcphdr->th_sport, sizeof(uint16_t));
  251. _net_rx_rss_add_chunk(rss_input, bytes_written,
  252. &tcphdr->th_dport, sizeof(uint16_t));
  253. }
  254. static inline void
  255. _net_rx_rss_prepare_udp(uint8_t *rss_input,
  256. struct NetRxPkt *pkt,
  257. size_t *bytes_written)
  258. {
  259. struct udp_header *udphdr = &pkt->l4hdr_info.hdr.udp;
  260. _net_rx_rss_add_chunk(rss_input, bytes_written,
  261. &udphdr->uh_sport, sizeof(uint16_t));
  262. _net_rx_rss_add_chunk(rss_input, bytes_written,
  263. &udphdr->uh_dport, sizeof(uint16_t));
  264. }
  265. uint32_t
  266. net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
  267. NetRxPktRssType type,
  268. uint8_t *key)
  269. {
  270. uint8_t rss_input[36];
  271. size_t rss_length = 0;
  272. uint32_t rss_hash = 0;
  273. net_toeplitz_key key_data;
  274. switch (type) {
  275. case NetPktRssIpV4:
  276. assert(pkt->isip4);
  277. trace_net_rx_pkt_rss_ip4();
  278. _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
  279. break;
  280. case NetPktRssIpV4Tcp:
  281. assert(pkt->isip4);
  282. assert(pkt->istcp);
  283. trace_net_rx_pkt_rss_ip4_tcp();
  284. _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
  285. _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
  286. break;
  287. case NetPktRssIpV6Tcp:
  288. assert(pkt->isip6);
  289. assert(pkt->istcp);
  290. trace_net_rx_pkt_rss_ip6_tcp();
  291. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
  292. _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
  293. break;
  294. case NetPktRssIpV6:
  295. assert(pkt->isip6);
  296. trace_net_rx_pkt_rss_ip6();
  297. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
  298. break;
  299. case NetPktRssIpV6Ex:
  300. assert(pkt->isip6);
  301. trace_net_rx_pkt_rss_ip6_ex();
  302. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
  303. break;
  304. case NetPktRssIpV6TcpEx:
  305. assert(pkt->isip6);
  306. assert(pkt->istcp);
  307. trace_net_rx_pkt_rss_ip6_ex_tcp();
  308. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
  309. _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
  310. break;
  311. case NetPktRssIpV4Udp:
  312. assert(pkt->isip4);
  313. assert(pkt->isudp);
  314. trace_net_rx_pkt_rss_ip4_udp();
  315. _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
  316. _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
  317. break;
  318. case NetPktRssIpV6Udp:
  319. assert(pkt->isip6);
  320. assert(pkt->isudp);
  321. trace_net_rx_pkt_rss_ip6_udp();
  322. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
  323. _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
  324. break;
  325. case NetPktRssIpV6UdpEx:
  326. assert(pkt->isip6);
  327. assert(pkt->isudp);
  328. trace_net_rx_pkt_rss_ip6_ex_udp();
  329. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
  330. _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
  331. break;
  332. default:
  333. assert(false);
  334. break;
  335. }
  336. net_toeplitz_key_init(&key_data, key);
  337. net_toeplitz_add(&rss_hash, rss_input, rss_length, &key_data);
  338. trace_net_rx_pkt_rss_hash(rss_length, rss_hash);
  339. return rss_hash;
  340. }
  341. uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt)
  342. {
  343. assert(pkt);
  344. if (pkt->isip4) {
  345. return be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_id);
  346. }
  347. return 0;
  348. }
  349. bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt)
  350. {
  351. assert(pkt);
  352. if (pkt->istcp) {
  353. return TCP_HEADER_FLAGS(&pkt->l4hdr_info.hdr.tcp) & TCP_FLAG_ACK;
  354. }
  355. return false;
  356. }
  357. bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt)
  358. {
  359. assert(pkt);
  360. if (pkt->istcp) {
  361. return pkt->l4hdr_info.has_tcp_data;
  362. }
  363. return false;
  364. }
  365. struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt)
  366. {
  367. assert(pkt);
  368. return pkt->vec;
  369. }
  370. uint16_t net_rx_pkt_get_iovec_len(struct NetRxPkt *pkt)
  371. {
  372. assert(pkt);
  373. return pkt->vec_len;
  374. }
  375. void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
  376. struct virtio_net_hdr *vhdr)
  377. {
  378. assert(pkt);
  379. memcpy(&pkt->virt_hdr, vhdr, sizeof pkt->virt_hdr);
  380. }
  381. void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
  382. const struct iovec *iov, int iovcnt)
  383. {
  384. assert(pkt);
  385. iov_to_buf(iov, iovcnt, 0, &pkt->virt_hdr, sizeof pkt->virt_hdr);
  386. }
  387. bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt)
  388. {
  389. assert(pkt);
  390. return pkt->ehdr_buf_len ? true : false;
  391. }
  392. bool net_rx_pkt_has_virt_hdr(struct NetRxPkt *pkt)
  393. {
  394. assert(pkt);
  395. return pkt->has_virt_hdr;
  396. }
  397. uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt)
  398. {
  399. assert(pkt);
  400. return pkt->tci;
  401. }
  402. bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid)
  403. {
  404. uint32_t cntr;
  405. uint16_t csum;
  406. uint32_t csl;
  407. trace_net_rx_pkt_l3_csum_validate_entry();
  408. if (!pkt->isip4) {
  409. trace_net_rx_pkt_l3_csum_validate_not_ip4();
  410. return false;
  411. }
  412. csl = pkt->l4hdr_off - pkt->l3hdr_off;
  413. cntr = net_checksum_add_iov(pkt->vec, pkt->vec_len,
  414. pkt->l3hdr_off,
  415. csl, 0);
  416. csum = net_checksum_finish(cntr);
  417. *csum_valid = (csum == 0);
  418. trace_net_rx_pkt_l3_csum_validate_csum(pkt->l3hdr_off, csl,
  419. cntr, csum, *csum_valid);
  420. return true;
  421. }
  422. static uint16_t
  423. _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt)
  424. {
  425. uint32_t cntr;
  426. uint16_t csum;
  427. uint16_t csl;
  428. uint32_t cso;
  429. trace_net_rx_pkt_l4_csum_calc_entry();
  430. if (pkt->isip4) {
  431. if (pkt->isudp) {
  432. csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
  433. trace_net_rx_pkt_l4_csum_calc_ip4_udp();
  434. } else {
  435. csl = be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_len) -
  436. IP_HDR_GET_LEN(&pkt->ip4hdr_info.ip4_hdr);
  437. trace_net_rx_pkt_l4_csum_calc_ip4_tcp();
  438. }
  439. cntr = eth_calc_ip4_pseudo_hdr_csum(&pkt->ip4hdr_info.ip4_hdr,
  440. csl, &cso);
  441. trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
  442. } else {
  443. if (pkt->isudp) {
  444. csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
  445. trace_net_rx_pkt_l4_csum_calc_ip6_udp();
  446. } else {
  447. struct ip6_header *ip6hdr = &pkt->ip6hdr_info.ip6_hdr;
  448. size_t full_ip6hdr_len = pkt->l4hdr_off - pkt->l3hdr_off;
  449. size_t ip6opts_len = full_ip6hdr_len - sizeof(struct ip6_header);
  450. csl = be16_to_cpu(ip6hdr->ip6_ctlun.ip6_un1.ip6_un1_plen) -
  451. ip6opts_len;
  452. trace_net_rx_pkt_l4_csum_calc_ip6_tcp();
  453. }
  454. cntr = eth_calc_ip6_pseudo_hdr_csum(&pkt->ip6hdr_info.ip6_hdr, csl,
  455. pkt->ip6hdr_info.l4proto, &cso);
  456. trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
  457. }
  458. cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len,
  459. pkt->l4hdr_off, csl, cso);
  460. csum = net_checksum_finish_nozero(cntr);
  461. trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum);
  462. return csum;
  463. }
  464. bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid)
  465. {
  466. uint16_t csum;
  467. trace_net_rx_pkt_l4_csum_validate_entry();
  468. if (!pkt->istcp && !pkt->isudp) {
  469. trace_net_rx_pkt_l4_csum_validate_not_xxp();
  470. return false;
  471. }
  472. if (pkt->isudp && (pkt->l4hdr_info.hdr.udp.uh_sum == 0)) {
  473. trace_net_rx_pkt_l4_csum_validate_udp_with_no_checksum();
  474. return false;
  475. }
  476. if (pkt->isip4 && pkt->ip4hdr_info.fragment) {
  477. trace_net_rx_pkt_l4_csum_validate_ip4_fragment();
  478. return false;
  479. }
  480. csum = _net_rx_pkt_calc_l4_csum(pkt);
  481. *csum_valid = ((csum == 0) || (csum == 0xFFFF));
  482. trace_net_rx_pkt_l4_csum_validate_csum(*csum_valid);
  483. return true;
  484. }
  485. bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt)
  486. {
  487. uint16_t csum = 0;
  488. uint32_t l4_cso;
  489. trace_net_rx_pkt_l4_csum_fix_entry();
  490. if (pkt->istcp) {
  491. l4_cso = offsetof(struct tcp_header, th_sum);
  492. trace_net_rx_pkt_l4_csum_fix_tcp(l4_cso);
  493. } else if (pkt->isudp) {
  494. if (pkt->l4hdr_info.hdr.udp.uh_sum == 0) {
  495. trace_net_rx_pkt_l4_csum_fix_udp_with_no_checksum();
  496. return false;
  497. }
  498. l4_cso = offsetof(struct udp_header, uh_sum);
  499. trace_net_rx_pkt_l4_csum_fix_udp(l4_cso);
  500. } else {
  501. trace_net_rx_pkt_l4_csum_fix_not_xxp();
  502. return false;
  503. }
  504. if (pkt->isip4 && pkt->ip4hdr_info.fragment) {
  505. trace_net_rx_pkt_l4_csum_fix_ip4_fragment();
  506. return false;
  507. }
  508. /* Set zero to checksum word */
  509. iov_from_buf(pkt->vec, pkt->vec_len,
  510. pkt->l4hdr_off + l4_cso,
  511. &csum, sizeof(csum));
  512. /* Calculate L4 checksum */
  513. csum = cpu_to_be16(_net_rx_pkt_calc_l4_csum(pkt));
  514. /* Set calculated checksum to checksum word */
  515. iov_from_buf(pkt->vec, pkt->vec_len,
  516. pkt->l4hdr_off + l4_cso,
  517. &csum, sizeof(csum));
  518. trace_net_rx_pkt_l4_csum_fix_csum(pkt->l4hdr_off + l4_cso, csum);
  519. return true;
  520. }