packet.go 7.0 KB

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