tcp.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use super::*;
  2. use crate::socket::tcp::Socket;
  3. impl InterfaceInner {
  4. pub(crate) fn process_tcp<'frame>(
  5. &mut self,
  6. sockets: &mut SocketSet,
  7. ip_repr: IpRepr,
  8. ip_payload: &'frame [u8],
  9. ) -> Option<Packet<'frame>> {
  10. let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
  11. let tcp_packet = check!(TcpPacket::new_checked(ip_payload));
  12. let tcp_repr = check!(TcpRepr::parse(
  13. &tcp_packet,
  14. &src_addr,
  15. &dst_addr,
  16. &self.caps.checksum
  17. ));
  18. for tcp_socket in sockets
  19. .items_mut()
  20. .filter_map(|i| Socket::downcast_mut(&mut i.socket))
  21. {
  22. if tcp_socket.accepts(self, &ip_repr, &tcp_repr) {
  23. return tcp_socket
  24. .process(self, &ip_repr, &tcp_repr)
  25. .map(|(ip, tcp)| Packet::new(ip, IpPayload::Tcp(tcp)));
  26. }
  27. }
  28. if tcp_repr.control == TcpControl::Rst
  29. || ip_repr.dst_addr().is_unspecified()
  30. || ip_repr.src_addr().is_unspecified()
  31. {
  32. // Never reply to a TCP RST packet with another TCP RST packet. We also never want to
  33. // send a TCP RST packet with unspecified addresses.
  34. None
  35. } else {
  36. // The packet wasn't handled by a socket, send a TCP RST packet.
  37. let (ip, tcp) = tcp::Socket::rst_reply(&ip_repr, &tcp_repr);
  38. Some(Packet::new(ip, IpPayload::Tcp(tcp)))
  39. }
  40. }
  41. }