| 37 | #include "../utils/udp.h" |
| 38 | |
| 39 | void L4Checksum::ProcessBatch(Context *ctx, bess::PacketBatch *batch) { |
| 40 | using bess::utils::Ethernet; |
| 41 | using bess::utils::Ipv4; |
| 42 | using bess::utils::Tcp; |
| 43 | using bess::utils::Udp; |
| 44 | using bess::utils::be16_t; |
| 45 | |
| 46 | int cnt = batch->cnt(); |
| 47 | |
| 48 | for (int i = 0; i < cnt; i++) { |
| 49 | Ethernet *eth = batch->pkts()[i]->head_data<Ethernet *>(); |
| 50 | |
| 51 | // Calculate checksum only for IPv4 packets |
| 52 | if (eth->ether_type != be16_t(Ethernet::Type::kIpv4)) |
| 53 | continue; |
| 54 | |
| 55 | Ipv4 *ip = reinterpret_cast<Ipv4 *>(eth + 1); |
| 56 | |
| 57 | if (ip->protocol == Ipv4::Proto::kUdp) { |
| 58 | size_t ip_bytes = (ip->header_length) << 2; |
| 59 | Udp *udp = |
| 60 | reinterpret_cast<Udp *>(reinterpret_cast<uint8_t *>(ip) + ip_bytes); |
| 61 | udp->checksum = CalculateIpv4UdpChecksum(*ip, *udp); |
| 62 | } else if (ip->protocol == Ipv4::Proto::kTcp) { |
| 63 | size_t ip_bytes = (ip->header_length) << 2; |
| 64 | Tcp *tcp = |
| 65 | reinterpret_cast<Tcp *>(reinterpret_cast<uint8_t *>(ip) + ip_bytes); |
| 66 | tcp->checksum = CalculateIpv4TcpChecksum(*ip, *tcp); |
| 67 | } |
| 68 | |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | RunNextModule(ctx, batch); |
| 73 | } |
| 74 | |
| 75 | ADD_MODULE(L4Checksum, "l4_checksum", |
| 76 | "recomputes the TCP/Ipv4 and UDP/IPv4 checksum") |
nothing calls this directly
no test coverage detected