2
0

net_rx_pkt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 "qemu/crc32c.h"
  19. #include "trace.h"
  20. #include "net_rx_pkt.h"
  21. #include "net/checksum.h"
  22. #include "net/tap.h"
  23. struct NetRxPkt {
  24. struct virtio_net_hdr virt_hdr;
  25. struct {
  26. struct eth_header eth;
  27. struct vlan_header vlan;
  28. } ehdr_buf;
  29. struct iovec *vec;
  30. uint16_t vec_len_total;
  31. uint16_t vec_len;
  32. uint32_t tot_len;
  33. uint16_t tci;
  34. size_t ehdr_buf_len;
  35. eth_pkt_types_e packet_type;
  36. /* Analysis results */
  37. bool hasip4;
  38. bool hasip6;
  39. size_t l3hdr_off;
  40. size_t l4hdr_off;
  41. size_t l5hdr_off;
  42. eth_ip6_hdr_info ip6hdr_info;
  43. eth_ip4_hdr_info ip4hdr_info;
  44. eth_l4_hdr_info l4hdr_info;
  45. };
  46. void net_rx_pkt_init(struct NetRxPkt **pkt)
  47. {
  48. struct NetRxPkt *p = g_malloc0(sizeof *p);
  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, 0, &pkt->hasip4, &pkt->hasip6,
  95. &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
  96. &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
  97. trace_net_rx_pkt_parsed(pkt->hasip4, pkt->hasip6, pkt->l4hdr_info.proto,
  98. pkt->l3hdr_off, pkt->l4hdr_off, pkt->l5hdr_off);
  99. }
  100. void net_rx_pkt_attach_iovec(struct NetRxPkt *pkt,
  101. const struct iovec *iov, int iovcnt,
  102. size_t iovoff, bool strip_vlan)
  103. {
  104. uint16_t tci = 0;
  105. uint16_t ploff = iovoff;
  106. assert(pkt);
  107. if (strip_vlan) {
  108. pkt->ehdr_buf_len = eth_strip_vlan(iov, iovcnt, iovoff, &pkt->ehdr_buf,
  109. &ploff, &tci);
  110. } else {
  111. pkt->ehdr_buf_len = 0;
  112. }
  113. pkt->tci = tci;
  114. net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
  115. }
  116. void net_rx_pkt_attach_iovec_ex(struct NetRxPkt *pkt,
  117. const struct iovec *iov, int iovcnt,
  118. size_t iovoff, int strip_vlan_index,
  119. uint16_t vet, uint16_t vet_ext)
  120. {
  121. uint16_t tci = 0;
  122. uint16_t ploff = iovoff;
  123. assert(pkt);
  124. pkt->ehdr_buf_len = eth_strip_vlan_ex(iov, iovcnt, iovoff,
  125. strip_vlan_index, vet, vet_ext,
  126. &pkt->ehdr_buf,
  127. &ploff, &tci);
  128. pkt->tci = tci;
  129. net_rx_pkt_pull_data(pkt, iov, iovcnt, ploff);
  130. }
  131. void net_rx_pkt_dump(struct NetRxPkt *pkt)
  132. {
  133. #ifdef NET_RX_PKT_DEBUG
  134. assert(pkt);
  135. printf("RX PKT: tot_len: %d, ehdr_buf_len: %lu, vlan_tag: %d\n",
  136. pkt->tot_len, pkt->ehdr_buf_len, pkt->tci);
  137. #endif
  138. }
  139. void net_rx_pkt_set_packet_type(struct NetRxPkt *pkt,
  140. eth_pkt_types_e packet_type)
  141. {
  142. assert(pkt);
  143. pkt->packet_type = packet_type;
  144. }
  145. eth_pkt_types_e net_rx_pkt_get_packet_type(struct NetRxPkt *pkt)
  146. {
  147. assert(pkt);
  148. return pkt->packet_type;
  149. }
  150. size_t net_rx_pkt_get_total_len(struct NetRxPkt *pkt)
  151. {
  152. assert(pkt);
  153. return pkt->tot_len;
  154. }
  155. void net_rx_pkt_set_protocols(struct NetRxPkt *pkt,
  156. const struct iovec *iov, size_t iovcnt,
  157. size_t iovoff)
  158. {
  159. assert(pkt);
  160. eth_get_protocols(iov, iovcnt, iovoff, &pkt->hasip4, &pkt->hasip6,
  161. &pkt->l3hdr_off, &pkt->l4hdr_off, &pkt->l5hdr_off,
  162. &pkt->ip6hdr_info, &pkt->ip4hdr_info, &pkt->l4hdr_info);
  163. }
  164. void net_rx_pkt_get_protocols(struct NetRxPkt *pkt,
  165. bool *hasip4, bool *hasip6,
  166. EthL4HdrProto *l4hdr_proto)
  167. {
  168. assert(pkt);
  169. *hasip4 = pkt->hasip4;
  170. *hasip6 = pkt->hasip6;
  171. *l4hdr_proto = pkt->l4hdr_info.proto;
  172. }
  173. size_t net_rx_pkt_get_l4_hdr_offset(struct NetRxPkt *pkt)
  174. {
  175. assert(pkt);
  176. return pkt->l4hdr_off;
  177. }
  178. size_t net_rx_pkt_get_l5_hdr_offset(struct NetRxPkt *pkt)
  179. {
  180. assert(pkt);
  181. return pkt->l5hdr_off;
  182. }
  183. eth_ip6_hdr_info *net_rx_pkt_get_ip6_info(struct NetRxPkt *pkt)
  184. {
  185. return &pkt->ip6hdr_info;
  186. }
  187. eth_ip4_hdr_info *net_rx_pkt_get_ip4_info(struct NetRxPkt *pkt)
  188. {
  189. return &pkt->ip4hdr_info;
  190. }
  191. static inline void
  192. _net_rx_rss_add_chunk(uint8_t *rss_input, size_t *bytes_written,
  193. void *ptr, size_t size)
  194. {
  195. memcpy(&rss_input[*bytes_written], ptr, size);
  196. trace_net_rx_pkt_rss_add_chunk(ptr, size, *bytes_written);
  197. *bytes_written += size;
  198. }
  199. static inline void
  200. _net_rx_rss_prepare_ip4(uint8_t *rss_input,
  201. struct NetRxPkt *pkt,
  202. size_t *bytes_written)
  203. {
  204. struct ip_header *ip4_hdr = &pkt->ip4hdr_info.ip4_hdr;
  205. _net_rx_rss_add_chunk(rss_input, bytes_written,
  206. &ip4_hdr->ip_src, sizeof(uint32_t));
  207. _net_rx_rss_add_chunk(rss_input, bytes_written,
  208. &ip4_hdr->ip_dst, sizeof(uint32_t));
  209. }
  210. static inline void
  211. _net_rx_rss_prepare_ip6(uint8_t *rss_input,
  212. struct NetRxPkt *pkt,
  213. bool ipv6ex, size_t *bytes_written)
  214. {
  215. eth_ip6_hdr_info *ip6info = &pkt->ip6hdr_info;
  216. _net_rx_rss_add_chunk(rss_input, bytes_written,
  217. (ipv6ex && ip6info->rss_ex_src_valid) ? &ip6info->rss_ex_src
  218. : &ip6info->ip6_hdr.ip6_src,
  219. sizeof(struct in6_address));
  220. _net_rx_rss_add_chunk(rss_input, bytes_written,
  221. (ipv6ex && ip6info->rss_ex_dst_valid) ? &ip6info->rss_ex_dst
  222. : &ip6info->ip6_hdr.ip6_dst,
  223. sizeof(struct in6_address));
  224. }
  225. static inline void
  226. _net_rx_rss_prepare_tcp(uint8_t *rss_input,
  227. struct NetRxPkt *pkt,
  228. size_t *bytes_written)
  229. {
  230. struct tcp_header *tcphdr = &pkt->l4hdr_info.hdr.tcp;
  231. _net_rx_rss_add_chunk(rss_input, bytes_written,
  232. &tcphdr->th_sport, sizeof(uint16_t));
  233. _net_rx_rss_add_chunk(rss_input, bytes_written,
  234. &tcphdr->th_dport, sizeof(uint16_t));
  235. }
  236. static inline void
  237. _net_rx_rss_prepare_udp(uint8_t *rss_input,
  238. struct NetRxPkt *pkt,
  239. size_t *bytes_written)
  240. {
  241. struct udp_header *udphdr = &pkt->l4hdr_info.hdr.udp;
  242. _net_rx_rss_add_chunk(rss_input, bytes_written,
  243. &udphdr->uh_sport, sizeof(uint16_t));
  244. _net_rx_rss_add_chunk(rss_input, bytes_written,
  245. &udphdr->uh_dport, sizeof(uint16_t));
  246. }
  247. uint32_t
  248. net_rx_pkt_calc_rss_hash(struct NetRxPkt *pkt,
  249. NetRxPktRssType type,
  250. uint8_t *key)
  251. {
  252. uint8_t rss_input[36];
  253. size_t rss_length = 0;
  254. uint32_t rss_hash = 0;
  255. net_toeplitz_key key_data;
  256. switch (type) {
  257. case NetPktRssIpV4:
  258. assert(pkt->hasip4);
  259. trace_net_rx_pkt_rss_ip4();
  260. _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
  261. break;
  262. case NetPktRssIpV4Tcp:
  263. assert(pkt->hasip4);
  264. assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP);
  265. trace_net_rx_pkt_rss_ip4_tcp();
  266. _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
  267. _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
  268. break;
  269. case NetPktRssIpV6Tcp:
  270. assert(pkt->hasip6);
  271. assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP);
  272. trace_net_rx_pkt_rss_ip6_tcp();
  273. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
  274. _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
  275. break;
  276. case NetPktRssIpV6:
  277. assert(pkt->hasip6);
  278. trace_net_rx_pkt_rss_ip6();
  279. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
  280. break;
  281. case NetPktRssIpV6Ex:
  282. assert(pkt->hasip6);
  283. trace_net_rx_pkt_rss_ip6_ex();
  284. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
  285. break;
  286. case NetPktRssIpV6TcpEx:
  287. assert(pkt->hasip6);
  288. assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP);
  289. trace_net_rx_pkt_rss_ip6_ex_tcp();
  290. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
  291. _net_rx_rss_prepare_tcp(&rss_input[0], pkt, &rss_length);
  292. break;
  293. case NetPktRssIpV4Udp:
  294. assert(pkt->hasip4);
  295. assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP);
  296. trace_net_rx_pkt_rss_ip4_udp();
  297. _net_rx_rss_prepare_ip4(&rss_input[0], pkt, &rss_length);
  298. _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
  299. break;
  300. case NetPktRssIpV6Udp:
  301. assert(pkt->hasip6);
  302. assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP);
  303. trace_net_rx_pkt_rss_ip6_udp();
  304. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, false, &rss_length);
  305. _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
  306. break;
  307. case NetPktRssIpV6UdpEx:
  308. assert(pkt->hasip6);
  309. assert(pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP);
  310. trace_net_rx_pkt_rss_ip6_ex_udp();
  311. _net_rx_rss_prepare_ip6(&rss_input[0], pkt, true, &rss_length);
  312. _net_rx_rss_prepare_udp(&rss_input[0], pkt, &rss_length);
  313. break;
  314. default:
  315. g_assert_not_reached();
  316. }
  317. net_toeplitz_key_init(&key_data, key);
  318. net_toeplitz_add(&rss_hash, rss_input, rss_length, &key_data);
  319. trace_net_rx_pkt_rss_hash(rss_length, rss_hash);
  320. return rss_hash;
  321. }
  322. uint16_t net_rx_pkt_get_ip_id(struct NetRxPkt *pkt)
  323. {
  324. assert(pkt);
  325. if (pkt->hasip4) {
  326. return be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_id);
  327. }
  328. return 0;
  329. }
  330. bool net_rx_pkt_is_tcp_ack(struct NetRxPkt *pkt)
  331. {
  332. assert(pkt);
  333. if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP) {
  334. return TCP_HEADER_FLAGS(&pkt->l4hdr_info.hdr.tcp) & TCP_FLAG_ACK;
  335. }
  336. return false;
  337. }
  338. bool net_rx_pkt_has_tcp_data(struct NetRxPkt *pkt)
  339. {
  340. assert(pkt);
  341. if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_TCP) {
  342. return pkt->l4hdr_info.has_tcp_data;
  343. }
  344. return false;
  345. }
  346. struct iovec *net_rx_pkt_get_iovec(struct NetRxPkt *pkt)
  347. {
  348. assert(pkt);
  349. return pkt->vec;
  350. }
  351. void net_rx_pkt_set_vhdr(struct NetRxPkt *pkt,
  352. struct virtio_net_hdr *vhdr)
  353. {
  354. assert(pkt);
  355. memcpy(&pkt->virt_hdr, vhdr, sizeof pkt->virt_hdr);
  356. }
  357. void net_rx_pkt_set_vhdr_iovec(struct NetRxPkt *pkt,
  358. const struct iovec *iov, int iovcnt)
  359. {
  360. assert(pkt);
  361. iov_to_buf(iov, iovcnt, 0, &pkt->virt_hdr, sizeof pkt->virt_hdr);
  362. }
  363. void net_rx_pkt_unset_vhdr(struct NetRxPkt *pkt)
  364. {
  365. assert(pkt);
  366. memset(&pkt->virt_hdr, 0, sizeof(pkt->virt_hdr));
  367. }
  368. bool net_rx_pkt_is_vlan_stripped(struct NetRxPkt *pkt)
  369. {
  370. assert(pkt);
  371. return pkt->ehdr_buf_len ? true : false;
  372. }
  373. uint16_t net_rx_pkt_get_vlan_tag(struct NetRxPkt *pkt)
  374. {
  375. assert(pkt);
  376. return pkt->tci;
  377. }
  378. bool net_rx_pkt_validate_l3_csum(struct NetRxPkt *pkt, bool *csum_valid)
  379. {
  380. uint32_t cntr;
  381. uint16_t csum;
  382. uint32_t csl;
  383. trace_net_rx_pkt_l3_csum_validate_entry();
  384. if (!pkt->hasip4) {
  385. trace_net_rx_pkt_l3_csum_validate_not_ip4();
  386. return false;
  387. }
  388. csl = pkt->l4hdr_off - pkt->l3hdr_off;
  389. cntr = net_checksum_add_iov(pkt->vec, pkt->vec_len,
  390. pkt->l3hdr_off,
  391. csl, 0);
  392. csum = net_checksum_finish(cntr);
  393. *csum_valid = (csum == 0);
  394. trace_net_rx_pkt_l3_csum_validate_csum(pkt->l3hdr_off, csl,
  395. cntr, csum, *csum_valid);
  396. return true;
  397. }
  398. static uint16_t
  399. _net_rx_pkt_calc_l4_csum(struct NetRxPkt *pkt)
  400. {
  401. uint32_t cntr;
  402. uint16_t csum;
  403. uint16_t csl;
  404. uint32_t cso;
  405. trace_net_rx_pkt_l4_csum_calc_entry();
  406. if (pkt->hasip4) {
  407. if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP) {
  408. csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
  409. trace_net_rx_pkt_l4_csum_calc_ip4_udp();
  410. } else {
  411. csl = be16_to_cpu(pkt->ip4hdr_info.ip4_hdr.ip_len) -
  412. IP_HDR_GET_LEN(&pkt->ip4hdr_info.ip4_hdr);
  413. trace_net_rx_pkt_l4_csum_calc_ip4_tcp();
  414. }
  415. cntr = eth_calc_ip4_pseudo_hdr_csum(&pkt->ip4hdr_info.ip4_hdr,
  416. csl, &cso);
  417. trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
  418. } else {
  419. if (pkt->l4hdr_info.proto == ETH_L4_HDR_PROTO_UDP) {
  420. csl = be16_to_cpu(pkt->l4hdr_info.hdr.udp.uh_ulen);
  421. trace_net_rx_pkt_l4_csum_calc_ip6_udp();
  422. } else {
  423. struct ip6_header *ip6hdr = &pkt->ip6hdr_info.ip6_hdr;
  424. size_t full_ip6hdr_len = pkt->l4hdr_off - pkt->l3hdr_off;
  425. size_t ip6opts_len = full_ip6hdr_len - sizeof(struct ip6_header);
  426. csl = be16_to_cpu(ip6hdr->ip6_ctlun.ip6_un1.ip6_un1_plen) -
  427. ip6opts_len;
  428. trace_net_rx_pkt_l4_csum_calc_ip6_tcp();
  429. }
  430. cntr = eth_calc_ip6_pseudo_hdr_csum(&pkt->ip6hdr_info.ip6_hdr, csl,
  431. pkt->ip6hdr_info.l4proto, &cso);
  432. trace_net_rx_pkt_l4_csum_calc_ph_csum(cntr, csl);
  433. }
  434. cntr += net_checksum_add_iov(pkt->vec, pkt->vec_len,
  435. pkt->l4hdr_off, csl, cso);
  436. csum = net_checksum_finish_nozero(cntr);
  437. trace_net_rx_pkt_l4_csum_calc_csum(pkt->l4hdr_off, csl, cntr, csum);
  438. return csum;
  439. }
  440. static bool
  441. _net_rx_pkt_validate_sctp_sum(struct NetRxPkt *pkt)
  442. {
  443. size_t csum_off;
  444. size_t off = pkt->l4hdr_off;
  445. size_t vec_len = pkt->vec_len;
  446. struct iovec *vec;
  447. uint32_t calculated = 0;
  448. uint32_t original;
  449. bool valid;
  450. for (vec = pkt->vec; vec->iov_len < off; vec++) {
  451. off -= vec->iov_len;
  452. vec_len--;
  453. }
  454. csum_off = off + 8;
  455. if (!iov_to_buf(vec, vec_len, csum_off, &original, sizeof(original))) {
  456. return false;
  457. }
  458. if (!iov_from_buf(vec, vec_len, csum_off,
  459. &calculated, sizeof(calculated))) {
  460. return false;
  461. }
  462. calculated = crc32c(0xffffffff,
  463. (uint8_t *)vec->iov_base + off, vec->iov_len - off);
  464. calculated = iov_crc32c(calculated ^ 0xffffffff, vec + 1, vec_len - 1);
  465. valid = calculated == le32_to_cpu(original);
  466. iov_from_buf(vec, vec_len, csum_off, &original, sizeof(original));
  467. return valid;
  468. }
  469. bool net_rx_pkt_validate_l4_csum(struct NetRxPkt *pkt, bool *csum_valid)
  470. {
  471. uint32_t csum;
  472. trace_net_rx_pkt_l4_csum_validate_entry();
  473. if (pkt->hasip4 && pkt->ip4hdr_info.fragment) {
  474. trace_net_rx_pkt_l4_csum_validate_ip4_fragment();
  475. return false;
  476. }
  477. switch (pkt->l4hdr_info.proto) {
  478. case ETH_L4_HDR_PROTO_UDP:
  479. if (pkt->l4hdr_info.hdr.udp.uh_sum == 0) {
  480. trace_net_rx_pkt_l4_csum_validate_udp_with_no_checksum();
  481. return false;
  482. }
  483. /* fall through */
  484. case ETH_L4_HDR_PROTO_TCP:
  485. csum = _net_rx_pkt_calc_l4_csum(pkt);
  486. *csum_valid = ((csum == 0) || (csum == 0xFFFF));
  487. break;
  488. case ETH_L4_HDR_PROTO_SCTP:
  489. *csum_valid = _net_rx_pkt_validate_sctp_sum(pkt);
  490. break;
  491. default:
  492. trace_net_rx_pkt_l4_csum_validate_not_xxp();
  493. return false;
  494. }
  495. trace_net_rx_pkt_l4_csum_validate_csum(*csum_valid);
  496. return true;
  497. }
  498. bool net_rx_pkt_fix_l4_csum(struct NetRxPkt *pkt)
  499. {
  500. uint16_t csum = 0;
  501. uint32_t l4_cso;
  502. trace_net_rx_pkt_l4_csum_fix_entry();
  503. switch (pkt->l4hdr_info.proto) {
  504. case ETH_L4_HDR_PROTO_TCP:
  505. l4_cso = offsetof(struct tcp_header, th_sum);
  506. trace_net_rx_pkt_l4_csum_fix_tcp(l4_cso);
  507. break;
  508. case ETH_L4_HDR_PROTO_UDP:
  509. if (pkt->l4hdr_info.hdr.udp.uh_sum == 0) {
  510. trace_net_rx_pkt_l4_csum_fix_udp_with_no_checksum();
  511. return false;
  512. }
  513. l4_cso = offsetof(struct udp_header, uh_sum);
  514. trace_net_rx_pkt_l4_csum_fix_udp(l4_cso);
  515. break;
  516. default:
  517. trace_net_rx_pkt_l4_csum_fix_not_xxp();
  518. return false;
  519. }
  520. if (pkt->hasip4 && pkt->ip4hdr_info.fragment) {
  521. trace_net_rx_pkt_l4_csum_fix_ip4_fragment();
  522. return false;
  523. }
  524. /* Set zero to checksum word */
  525. iov_from_buf(pkt->vec, pkt->vec_len,
  526. pkt->l4hdr_off + l4_cso,
  527. &csum, sizeof(csum));
  528. /* Calculate L4 checksum */
  529. csum = cpu_to_be16(_net_rx_pkt_calc_l4_csum(pkt));
  530. /* Set calculated checksum to checksum word */
  531. iov_from_buf(pkt->vec, pkt->vec_len,
  532. pkt->l4hdr_off + l4_cso,
  533. &csum, sizeof(csum));
  534. trace_net_rx_pkt_l4_csum_fix_csum(pkt->l4hdr_off + l4_cso, csum);
  535. return true;
  536. }