packet.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package iputil
  2. import (
  3. "encoding/binary"
  4. "golang.org/x/net/ipv4"
  5. )
  6. const (
  7. // Need 96 bytes for the largest reject packet:
  8. // - 20 byte ipv4 header
  9. // - 8 byte icmpv4 header
  10. // - 68 byte body (60 byte max orig ipv4 header + 8 byte orig icmpv4 header)
  11. MaxRejectPacketSize = ipv4.HeaderLen + 8 + 60 + 8
  12. )
  13. func CreateRejectPacket(packet []byte, out []byte) []byte {
  14. if len(packet) < ipv4.HeaderLen || int(packet[0]>>4) != ipv4.Version {
  15. return nil
  16. }
  17. switch packet[9] {
  18. case 6: // tcp
  19. return ipv4CreateRejectTCPPacket(packet, out)
  20. default:
  21. return ipv4CreateRejectICMPPacket(packet, out)
  22. }
  23. }
  24. func ipv4CreateRejectICMPPacket(packet []byte, out []byte) []byte {
  25. ihl := int(packet[0]&0x0f) << 2
  26. if len(packet) < ihl {
  27. // We need at least this many bytes for this to be a valid packet
  28. return nil
  29. }
  30. // ICMP reply includes original header and first 8 bytes of the packet
  31. packetLen := len(packet)
  32. if packetLen > ihl+8 {
  33. packetLen = ihl + 8
  34. }
  35. outLen := ipv4.HeaderLen + 8 + packetLen
  36. if outLen > cap(out) {
  37. return nil
  38. }
  39. out = out[:outLen]
  40. ipHdr := out[0:ipv4.HeaderLen]
  41. ipHdr[0] = ipv4.Version<<4 | (ipv4.HeaderLen >> 2) // version, ihl
  42. ipHdr[1] = 0 // DSCP, ECN
  43. binary.BigEndian.PutUint16(ipHdr[2:], uint16(outLen)) // Total Length
  44. ipHdr[4] = 0 // id
  45. ipHdr[5] = 0 // .
  46. ipHdr[6] = 0 // flags, fragment offset
  47. ipHdr[7] = 0 // .
  48. ipHdr[8] = 64 // TTL
  49. ipHdr[9] = 1 // protocol (icmp)
  50. ipHdr[10] = 0 // checksum
  51. ipHdr[11] = 0 // .
  52. // Swap dest / src IPs
  53. copy(ipHdr[12:16], packet[16:20])
  54. copy(ipHdr[16:20], packet[12:16])
  55. // Calculate checksum
  56. binary.BigEndian.PutUint16(ipHdr[10:], tcpipChecksum(ipHdr, 0))
  57. // ICMP Destination Unreachable
  58. icmpOut := out[ipv4.HeaderLen:]
  59. icmpOut[0] = 3 // type (Destination unreachable)
  60. icmpOut[1] = 3 // code (Port unreachable error)
  61. icmpOut[2] = 0 // checksum
  62. icmpOut[3] = 0 // .
  63. icmpOut[4] = 0 // unused
  64. icmpOut[5] = 0 // .
  65. icmpOut[6] = 0 // .
  66. icmpOut[7] = 0 // .
  67. // Copy original IP header and first 8 bytes as body
  68. copy(icmpOut[8:], packet[:packetLen])
  69. // Calculate checksum
  70. binary.BigEndian.PutUint16(icmpOut[2:], tcpipChecksum(icmpOut, 0))
  71. return out
  72. }
  73. func ipv4CreateRejectTCPPacket(packet []byte, out []byte) []byte {
  74. const tcpLen = 20
  75. ihl := int(packet[0]&0x0f) << 2
  76. outLen := ipv4.HeaderLen + tcpLen
  77. if len(packet) < ihl+tcpLen {
  78. // We need at least this many bytes for this to be a valid packet
  79. return nil
  80. }
  81. if outLen > cap(out) {
  82. return nil
  83. }
  84. out = out[:outLen]
  85. ipHdr := out[0:ipv4.HeaderLen]
  86. ipHdr[0] = ipv4.Version<<4 | (ipv4.HeaderLen >> 2) // version, ihl
  87. ipHdr[1] = 0 // DSCP, ECN
  88. binary.BigEndian.PutUint16(ipHdr[2:], uint16(outLen)) // Total Length
  89. ipHdr[4] = 0 // id
  90. ipHdr[5] = 0 // .
  91. ipHdr[6] = 0 // flags, fragment offset
  92. ipHdr[7] = 0 // .
  93. ipHdr[8] = 64 // TTL
  94. ipHdr[9] = 6 // protocol (tcp)
  95. ipHdr[10] = 0 // checksum
  96. ipHdr[11] = 0 // .
  97. // Swap dest / src IPs
  98. copy(ipHdr[12:16], packet[16:20])
  99. copy(ipHdr[16:20], packet[12:16])
  100. // Calculate checksum
  101. binary.BigEndian.PutUint16(ipHdr[10:], tcpipChecksum(ipHdr, 0))
  102. // TCP RST
  103. tcpIn := packet[ihl:]
  104. var ackSeq, seq uint32
  105. outFlags := byte(0b00000100) // RST
  106. // Set seq and ackSeq based on how iptables/netfilter does it in Linux:
  107. // - https://github.com/torvalds/linux/blob/v5.19/net/ipv4/netfilter/nf_reject_ipv4.c#L193-L221
  108. inAck := tcpIn[13]&0b00010000 != 0
  109. if inAck {
  110. seq = binary.BigEndian.Uint32(tcpIn[8:])
  111. } else {
  112. inSyn := uint32((tcpIn[13] & 0b00000010) >> 1)
  113. inFin := uint32(tcpIn[13] & 0b00000001)
  114. // seq from the packet + syn + fin + tcp segment length
  115. ackSeq = binary.BigEndian.Uint32(tcpIn[4:]) + inSyn + inFin + uint32(len(tcpIn)) - uint32(tcpIn[12]>>4)<<2
  116. outFlags |= 0b00010000 // ACK
  117. }
  118. tcpOut := out[ipv4.HeaderLen:]
  119. // Swap dest / src ports
  120. copy(tcpOut[0:2], tcpIn[2:4])
  121. copy(tcpOut[2:4], tcpIn[0:2])
  122. binary.BigEndian.PutUint32(tcpOut[4:], seq)
  123. binary.BigEndian.PutUint32(tcpOut[8:], ackSeq)
  124. tcpOut[12] = (tcpLen >> 2) << 4 // data offset, reserved, NS
  125. tcpOut[13] = outFlags // CWR, ECE, URG, ACK, PSH, RST, SYN, FIN
  126. tcpOut[14] = 0 // window size
  127. tcpOut[15] = 0 // .
  128. tcpOut[16] = 0 // checksum
  129. tcpOut[17] = 0 // .
  130. tcpOut[18] = 0 // URG Pointer
  131. tcpOut[19] = 0 // .
  132. // Calculate checksum
  133. csum := ipv4PseudoheaderChecksum(ipHdr[12:16], ipHdr[16:20], 6, tcpLen)
  134. binary.BigEndian.PutUint16(tcpOut[16:], tcpipChecksum(tcpOut, csum))
  135. return out
  136. }
  137. func CreateICMPEchoResponse(packet, out []byte) []byte {
  138. // Return early if this is not a simple ICMP Echo Request
  139. //TODO: make constants out of these
  140. if !(len(packet) >= 28 && len(packet) <= 9001 && packet[0] == 0x45 && packet[9] == 0x01 && packet[20] == 0x08) {
  141. return nil
  142. }
  143. // We don't support fragmented packets
  144. if packet[7] != 0 || (packet[6]&0x2F != 0) {
  145. return nil
  146. }
  147. out = out[:len(packet)]
  148. copy(out, packet)
  149. // Swap dest / src IPs and recalculate checksum
  150. ipv4 := out[0:20]
  151. copy(ipv4[12:16], packet[16:20])
  152. copy(ipv4[16:20], packet[12:16])
  153. ipv4[10] = 0
  154. ipv4[11] = 0
  155. binary.BigEndian.PutUint16(ipv4[10:], tcpipChecksum(ipv4, 0))
  156. // Change type to ICMP Echo Reply and recalculate checksum
  157. icmp := out[20:]
  158. icmp[0] = 0
  159. icmp[2] = 0
  160. icmp[3] = 0
  161. binary.BigEndian.PutUint16(icmp[2:], tcpipChecksum(icmp, 0))
  162. return out
  163. }
  164. // calculates the TCP/IP checksum defined in rfc1071. The passed-in
  165. // csum is any initial checksum data that's already been computed.
  166. //
  167. // based on:
  168. // - https://github.com/google/gopacket/blob/v1.1.19/layers/tcpip.go#L50-L70
  169. func tcpipChecksum(data []byte, csum uint32) uint16 {
  170. // to handle odd lengths, we loop to length - 1, incrementing by 2, then
  171. // handle the last byte specifically by checking against the original
  172. // length.
  173. length := len(data) - 1
  174. for i := 0; i < length; i += 2 {
  175. // For our test packet, doing this manually is about 25% faster
  176. // (740 ns vs. 1000ns) than doing it by calling binary.BigEndian.Uint16.
  177. csum += uint32(data[i]) << 8
  178. csum += uint32(data[i+1])
  179. }
  180. if len(data)%2 == 1 {
  181. csum += uint32(data[length]) << 8
  182. }
  183. for csum > 0xffff {
  184. csum = (csum >> 16) + (csum & 0xffff)
  185. }
  186. return ^uint16(csum)
  187. }
  188. // based on:
  189. // - https://github.com/google/gopacket/blob/v1.1.19/layers/tcpip.go#L26-L35
  190. func ipv4PseudoheaderChecksum(src, dst []byte, proto, length uint32) (csum uint32) {
  191. csum += (uint32(src[0]) + uint32(src[2])) << 8
  192. csum += uint32(src[1]) + uint32(src[3])
  193. csum += (uint32(dst[0]) + uint32(dst[2])) << 8
  194. csum += uint32(dst[1]) + uint32(dst[3])
  195. csum += proto
  196. csum += length & 0xffff
  197. csum += length >> 16
  198. return csum
  199. }