header.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ipv4
  5. import (
  6. "errors"
  7. "fmt"
  8. "net"
  9. "runtime"
  10. "syscall"
  11. "unsafe"
  12. )
  13. var (
  14. errMissingAddress = errors.New("missing address")
  15. errMissingHeader = errors.New("missing header")
  16. errHeaderTooShort = errors.New("header too short")
  17. errBufferTooShort = errors.New("buffer too short")
  18. errInvalidConnType = errors.New("invalid conn type")
  19. )
  20. // References:
  21. //
  22. // RFC 791 Internet Protocol
  23. // http://tools.ietf.org/html/rfc791
  24. // RFC 1112 Host Extensions for IP Multicasting
  25. // http://tools.ietf.org/html/rfc1112
  26. // RFC 1122 Requirements for Internet Hosts
  27. // http://tools.ietf.org/html/rfc1122
  28. // RFC 2474 Definition of the Differentiated Services Field (DS Field) in the IPv4 and IPv6 Headers
  29. // http://tools.ietf.org/html/rfc2474
  30. // RFC 2475 An Architecture for Differentiated Services
  31. // http://tools.ietf.org/html/rfc2475
  32. // RFC 2597 Assured Forwarding PHB Group
  33. // http://tools.ietf.org/html/rfc2597
  34. // RFC 2598 An Expedited Forwarding PHB
  35. // http://tools.ietf.org/html/rfc2598
  36. // RFC 3168 The Addition of Explicit Congestion Notification (ECN) to IP
  37. // http://tools.ietf.org/html/rfc3168
  38. // RFC 3260 New Terminology and Clarifications for Diffserv
  39. // http://tools.ietf.org/html/rfc3260
  40. const (
  41. Version = 4 // protocol version
  42. HeaderLen = 20 // header length without extension headers
  43. maxHeaderLen = 60 // sensible default, revisit if later RFCs define new usage of version and header length fields
  44. )
  45. const (
  46. // DiffServ class selector codepoints in RFC 2474.
  47. DSCP_CS0 = 0x00 // best effort
  48. DSCP_CS1 = 0x20 // class 1
  49. DSCP_CS2 = 0x40 // class 2
  50. DSCP_CS3 = 0x60 // class 3
  51. DSCP_CS4 = 0x80 // class 4
  52. DSCP_CS5 = 0xa0 // expedited forwarding
  53. DSCP_CS6 = 0xc0 // subsume deprecated IP precedence, internet control (routing information update)
  54. DSCP_CS7 = 0xe0 // subsume deprecated IP precedence, network control (link, neighbor liveliness check)
  55. // DiffServ assured forwarding codepoints in RFC 2474, 2475, 2597 and 3260.
  56. DSCP_AF11 = 0x28 // class 1 low drop precedence
  57. DSCP_AF12 = 0x30 // class 1 medium drop precedence
  58. DSCP_AF13 = 0x38 // class 1 high drop precedence
  59. DSCP_AF21 = 0x48 // class 2 low drop precedence
  60. DSCP_AF22 = 0x50 // class 2 medium drop precedence
  61. DSCP_AF23 = 0x58 // class 2 high drop precedence
  62. DSCP_AF31 = 0x68 // class 3 low drop precedence
  63. DSCP_AF32 = 0x70 // class 3 medium drop precedence
  64. DSCP_AF33 = 0x78 // class 3 high drop precedence
  65. DSCP_AF41 = 0x88 // class 4 low drop precedence
  66. DSCP_AF42 = 0x90 // class 4 medium drop precedence
  67. DSCP_AF43 = 0x98 // class 4 high drop precedence
  68. DSCP_EF = 0xb8 // expedited forwarding
  69. // ECN codepoints in RFC 3168.
  70. ECN_NOTECT = 0x00 // not ECN-capable transport
  71. ECN_ECT1 = 0x01 // ECN-capable transport, ECT(1)
  72. ECN_ECT0 = 0x02 // ECN-capable transport, ECT(0)
  73. ECN_CE = 0x03 // congestion experienced
  74. )
  75. type headerField int
  76. const (
  77. posTOS headerField = 1 // type-of-service
  78. posTotalLen = 2 // packet total length
  79. posID = 4 // identification
  80. posFragOff = 6 // fragment offset
  81. posTTL = 8 // time-to-live
  82. posProtocol = 9 // next protocol
  83. posChecksum = 10 // checksum
  84. posSrc = 12 // source address
  85. posDst = 16 // destination address
  86. )
  87. // A Header represents an IPv4 header.
  88. type Header struct {
  89. Version int // protocol version
  90. Len int // header length
  91. TOS int // type-of-service
  92. TotalLen int // packet total length
  93. ID int // identification
  94. FragOff int // fragment offset
  95. TTL int // time-to-live
  96. Protocol int // next protocol
  97. Checksum int // checksum
  98. Src net.IP // source address
  99. Dst net.IP // destination address
  100. Options []byte // options, extension headers
  101. }
  102. func (h *Header) String() string {
  103. if h == nil {
  104. return "<nil>"
  105. }
  106. return fmt.Sprintf("ver: %v, hdrlen: %v, tos: %#x, totallen: %v, id: %#x, fragoff: %#x, ttl: %v, proto: %v, cksum: %#x, src: %v, dst: %v", h.Version, h.Len, h.TOS, h.TotalLen, h.ID, h.FragOff, h.TTL, h.Protocol, h.Checksum, h.Src, h.Dst)
  107. }
  108. // Please refer to the online manual; IP(4) on Darwin, FreeBSD and
  109. // OpenBSD. IP(7) on Linux.
  110. var supportsNewIPInput = runtime.GOOS == "linux" || runtime.GOOS == "openbsd"
  111. // Marshal returns the binary encoding of the IPv4 header h.
  112. func (h *Header) Marshal() ([]byte, error) {
  113. if h == nil {
  114. return nil, syscall.EINVAL
  115. }
  116. if h.Len < HeaderLen {
  117. return nil, errHeaderTooShort
  118. }
  119. hdrlen := HeaderLen + len(h.Options)
  120. b := make([]byte, hdrlen)
  121. b[0] = byte(Version<<4 | (hdrlen >> 2 & 0x0f))
  122. b[posTOS] = byte(h.TOS)
  123. if supportsNewIPInput {
  124. b[posTotalLen], b[posTotalLen+1] = byte(h.TotalLen>>8), byte(h.TotalLen)
  125. b[posFragOff], b[posFragOff+1] = byte(h.FragOff>>8), byte(h.FragOff)
  126. } else {
  127. *(*uint16)(unsafe.Pointer(&b[posTotalLen : posTotalLen+1][0])) = uint16(h.TotalLen)
  128. *(*uint16)(unsafe.Pointer(&b[posFragOff : posFragOff+1][0])) = uint16(h.FragOff)
  129. }
  130. b[posID], b[posID+1] = byte(h.ID>>8), byte(h.ID)
  131. b[posTTL] = byte(h.TTL)
  132. b[posProtocol] = byte(h.Protocol)
  133. b[posChecksum], b[posChecksum+1] = byte(h.Checksum>>8), byte(h.Checksum)
  134. if ip := h.Src.To4(); ip != nil {
  135. copy(b[posSrc:posSrc+net.IPv4len], ip[0:net.IPv4len])
  136. }
  137. if ip := h.Dst.To4(); ip != nil {
  138. copy(b[posDst:posDst+net.IPv4len], ip[0:net.IPv4len])
  139. } else {
  140. return nil, errMissingAddress
  141. }
  142. if len(h.Options) > 0 {
  143. copy(b[HeaderLen:], h.Options)
  144. }
  145. return b, nil
  146. }
  147. // ParseHeader parses b as an IPv4 header.
  148. func ParseHeader(b []byte) (*Header, error) {
  149. if len(b) < HeaderLen {
  150. return nil, errHeaderTooShort
  151. }
  152. hdrlen := (int(b[0]) & 0x0f) << 2
  153. if hdrlen > len(b) {
  154. return nil, errBufferTooShort
  155. }
  156. h := &Header{}
  157. h.Version = int(b[0] >> 4)
  158. h.Len = hdrlen
  159. h.TOS = int(b[posTOS])
  160. if supportsNewIPInput {
  161. h.TotalLen = int(b[posTotalLen])<<8 | int(b[posTotalLen+1])
  162. h.FragOff = int(b[posFragOff])<<8 | int(b[posFragOff+1])
  163. } else {
  164. h.TotalLen = int(*(*uint16)(unsafe.Pointer(&b[posTotalLen : posTotalLen+1][0])))
  165. h.TotalLen += hdrlen
  166. h.FragOff = int(*(*uint16)(unsafe.Pointer(&b[posFragOff : posFragOff+1][0])))
  167. }
  168. h.ID = int(b[posID])<<8 | int(b[posID+1])
  169. h.TTL = int(b[posTTL])
  170. h.Protocol = int(b[posProtocol])
  171. h.Checksum = int(b[posChecksum])<<8 | int(b[posChecksum+1])
  172. h.Src = net.IPv4(b[posSrc], b[posSrc+1], b[posSrc+2], b[posSrc+3])
  173. h.Dst = net.IPv4(b[posDst], b[posDst+1], b[posDst+2], b[posDst+3])
  174. if hdrlen-HeaderLen > 0 {
  175. h.Options = make([]byte, hdrlen-HeaderLen)
  176. copy(h.Options, b[HeaderLen:])
  177. }
  178. return h, nil
  179. }