github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/header/igmp.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package header 16 17 import ( 18 "encoding/binary" 19 "fmt" 20 "time" 21 22 "github.com/SagerNet/gvisor/pkg/tcpip" 23 ) 24 25 // IGMP represents an IGMP header stored in a byte array. 26 type IGMP []byte 27 28 // IGMP implements `Transport`. 29 var _ Transport = (*IGMP)(nil) 30 31 const ( 32 // IGMPMinimumSize is the minimum size of a valid IGMP packet in bytes, 33 // as per RFC 2236, Section 2, Page 2. 34 IGMPMinimumSize = 8 35 36 // IGMPQueryMinimumSize is the minimum size of a valid Membership Query 37 // Message in bytes, as per RFC 2236, Section 2, Page 2. 38 IGMPQueryMinimumSize = 8 39 40 // IGMPReportMinimumSize is the minimum size of a valid Report Message in 41 // bytes, as per RFC 2236, Section 2, Page 2. 42 IGMPReportMinimumSize = 8 43 44 // IGMPLeaveMessageMinimumSize is the minimum size of a valid Leave Message 45 // in bytes, as per RFC 2236, Section 2, Page 2. 46 IGMPLeaveMessageMinimumSize = 8 47 48 // IGMPTTL is the TTL for all IGMP messages, as per RFC 2236, Section 3, Page 49 // 3. 50 IGMPTTL = 1 51 52 // igmpTypeOffset defines the offset of the type field in an IGMP message. 53 igmpTypeOffset = 0 54 55 // igmpMaxRespTimeOffset defines the offset of the MaxRespTime field in an 56 // IGMP message. 57 igmpMaxRespTimeOffset = 1 58 59 // igmpChecksumOffset defines the offset of the checksum field in an IGMP 60 // message. 61 igmpChecksumOffset = 2 62 63 // igmpGroupAddressOffset defines the offset of the Group Address field in an 64 // IGMP message. 65 igmpGroupAddressOffset = 4 66 67 // IGMPProtocolNumber is IGMP's transport protocol number. 68 IGMPProtocolNumber tcpip.TransportProtocolNumber = 2 69 ) 70 71 // IGMPType is the IGMP type field as per RFC 2236. 72 type IGMPType byte 73 74 // Values for the IGMP Type described in RFC 2236 Section 2.1, Page 2. 75 // Descriptions below come from there. 76 const ( 77 // IGMPMembershipQuery indicates that the message type is Membership Query. 78 // "There are two sub-types of Membership Query messages: 79 // - General Query, used to learn which groups have members on an 80 // attached network. 81 // - Group-Specific Query, used to learn if a particular group 82 // has any members on an attached network. 83 // These two messages are differentiated by the Group Address, as 84 // described in section 1.4 ." 85 IGMPMembershipQuery IGMPType = 0x11 86 // IGMPv1MembershipReport indicates that the message is a Membership Report 87 // generated by a host using the IGMPv1 protocol: "an additional type of 88 // message, for backwards-compatibility with IGMPv1" 89 IGMPv1MembershipReport IGMPType = 0x12 90 // IGMPv2MembershipReport indicates that the Message type is a Membership 91 // Report generated by a host using the IGMPv2 protocol. 92 IGMPv2MembershipReport IGMPType = 0x16 93 // IGMPLeaveGroup indicates that the message type is a Leave Group 94 // notification message. 95 IGMPLeaveGroup IGMPType = 0x17 96 ) 97 98 // Type is the IGMP type field. 99 func (b IGMP) Type() IGMPType { return IGMPType(b[igmpTypeOffset]) } 100 101 // SetType sets the IGMP type field. 102 func (b IGMP) SetType(t IGMPType) { b[igmpTypeOffset] = byte(t) } 103 104 // MaxRespTime gets the MaxRespTimeField. This is meaningful only in Membership 105 // Query messages, in other cases it is set to 0 by the sender and ignored by 106 // the receiver. 107 func (b IGMP) MaxRespTime() time.Duration { 108 // As per RFC 2236 section 2.2, 109 // 110 // The Max Response Time field is meaningful only in Membership Query 111 // messages, and specifies the maximum allowed time before sending a 112 // responding report in units of 1/10 second. In all other messages, it 113 // is set to zero by the sender and ignored by receivers. 114 return DecisecondToDuration(b[igmpMaxRespTimeOffset]) 115 } 116 117 // SetMaxRespTime sets the MaxRespTimeField. 118 func (b IGMP) SetMaxRespTime(m byte) { b[igmpMaxRespTimeOffset] = m } 119 120 // Checksum is the IGMP checksum field. 121 func (b IGMP) Checksum() uint16 { 122 return binary.BigEndian.Uint16(b[igmpChecksumOffset:]) 123 } 124 125 // SetChecksum sets the IGMP checksum field. 126 func (b IGMP) SetChecksum(checksum uint16) { 127 binary.BigEndian.PutUint16(b[igmpChecksumOffset:], checksum) 128 } 129 130 // GroupAddress gets the Group Address field. 131 func (b IGMP) GroupAddress() tcpip.Address { 132 return tcpip.Address(b[igmpGroupAddressOffset:][:IPv4AddressSize]) 133 } 134 135 // SetGroupAddress sets the Group Address field. 136 func (b IGMP) SetGroupAddress(address tcpip.Address) { 137 if n := copy(b[igmpGroupAddressOffset:], address); n != IPv4AddressSize { 138 panic(fmt.Sprintf("copied %d bytes, expected %d", n, IPv4AddressSize)) 139 } 140 } 141 142 // SourcePort implements Transport.SourcePort. 143 func (IGMP) SourcePort() uint16 { 144 return 0 145 } 146 147 // DestinationPort implements Transport.DestinationPort. 148 func (IGMP) DestinationPort() uint16 { 149 return 0 150 } 151 152 // SetSourcePort implements Transport.SetSourcePort. 153 func (IGMP) SetSourcePort(uint16) { 154 } 155 156 // SetDestinationPort implements Transport.SetDestinationPort. 157 func (IGMP) SetDestinationPort(uint16) { 158 } 159 160 // Payload implements Transport.Payload. 161 func (IGMP) Payload() []byte { 162 return nil 163 } 164 165 // IGMPCalculateChecksum calculates the IGMP checksum over the provided IGMP 166 // header. 167 func IGMPCalculateChecksum(h IGMP) uint16 { 168 // The header contains a checksum itself, set it aside to avoid checksumming 169 // the checksum and replace it afterwards. 170 existingXsum := h.Checksum() 171 h.SetChecksum(0) 172 xsum := ^Checksum(h, 0) 173 h.SetChecksum(existingXsum) 174 return xsum 175 } 176 177 // DecisecondToDuration converts a value representing deci-seconds to a 178 // time.Duration. 179 func DecisecondToDuration(ds uint8) time.Duration { 180 return time.Duration(ds) * time.Second / 10 181 }