github.com/vpnishe/netstack@v1.10.6/tcpip/header/ndp_router_advert.go (about) 1 // Copyright 2019 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 "time" 20 ) 21 22 // NDPRouterAdvert is an NDP Router Advertisement message. It will only contain 23 // the body of an ICMPv6 packet. 24 // 25 // See RFC 4861 section 4.2 for more details. 26 type NDPRouterAdvert []byte 27 28 const ( 29 // NDPRAMinimumSize is the minimum size of a valid NDP Router 30 // Advertisement message (body of an ICMPv6 packet). 31 NDPRAMinimumSize = 12 32 33 // ndpRACurrHopLimitOffset is the byte of the Curr Hop Limit field 34 // within an NDPRouterAdvert. 35 ndpRACurrHopLimitOffset = 0 36 37 // ndpRAFlagsOffset is the byte with the NDP RA bit-fields/flags 38 // within an NDPRouterAdvert. 39 ndpRAFlagsOffset = 1 40 41 // ndpRAManagedAddrConfFlagMask is the mask of the Managed Address 42 // Configuration flag within the bit-field/flags byte of an 43 // NDPRouterAdvert. 44 ndpRAManagedAddrConfFlagMask = (1 << 7) 45 46 // ndpRAOtherConfFlagMask is the mask of the Other Configuration flag 47 // within the bit-field/flags byte of an NDPRouterAdvert. 48 ndpRAOtherConfFlagMask = (1 << 6) 49 50 // ndpRARouterLifetimeOffset is the start of the 2-byte Router Lifetime 51 // field within an NDPRouterAdvert. 52 ndpRARouterLifetimeOffset = 2 53 54 // ndpRAReachableTimeOffset is the start of the 4-byte Reachable Time 55 // field within an NDPRouterAdvert. 56 ndpRAReachableTimeOffset = 4 57 58 // ndpRARetransTimerOffset is the start of the 4-byte Retrans Timer 59 // field within an NDPRouterAdvert. 60 ndpRARetransTimerOffset = 8 61 62 // ndpRAOptionsOffset is the start of the NDP options in an 63 // NDPRouterAdvert. 64 ndpRAOptionsOffset = 12 65 ) 66 67 // CurrHopLimit returns the value of the Curr Hop Limit field. 68 func (b NDPRouterAdvert) CurrHopLimit() uint8 { 69 return b[ndpRACurrHopLimitOffset] 70 } 71 72 // ManagedAddrConfFlag returns the value of the Managed Address Configuration 73 // flag. 74 func (b NDPRouterAdvert) ManagedAddrConfFlag() bool { 75 return b[ndpRAFlagsOffset]&ndpRAManagedAddrConfFlagMask != 0 76 } 77 78 // OtherConfFlag returns the value of the Other Configuration flag. 79 func (b NDPRouterAdvert) OtherConfFlag() bool { 80 return b[ndpRAFlagsOffset]&ndpRAOtherConfFlagMask != 0 81 } 82 83 // RouterLifetime returns the lifetime associated with the default router. A 84 // value of 0 means the source of the Router Advertisement is not a default 85 // router and SHOULD NOT appear on the default router list. Note, a value of 0 86 // only means that the router should not be used as a default router, it does 87 // not apply to other information contained in the Router Advertisement. 88 func (b NDPRouterAdvert) RouterLifetime() time.Duration { 89 // The field is the time in seconds, as per RFC 4861 section 4.2. 90 return time.Second * time.Duration(binary.BigEndian.Uint16(b[ndpRARouterLifetimeOffset:])) 91 } 92 93 // ReachableTime returns the time that a node assumes a neighbor is reachable 94 // after having received a reachability confirmation. A value of 0 means 95 // that it is unspecified by the source of the Router Advertisement message. 96 func (b NDPRouterAdvert) ReachableTime() time.Duration { 97 // The field is the time in milliseconds, as per RFC 4861 section 4.2. 98 return time.Millisecond * time.Duration(binary.BigEndian.Uint32(b[ndpRAReachableTimeOffset:])) 99 } 100 101 // RetransTimer returns the time between retransmitted Neighbor Solicitation 102 // messages. A value of 0 means that it is unspecified by the source of the 103 // Router Advertisement message. 104 func (b NDPRouterAdvert) RetransTimer() time.Duration { 105 // The field is the time in milliseconds, as per RFC 4861 section 4.2. 106 return time.Millisecond * time.Duration(binary.BigEndian.Uint32(b[ndpRARetransTimerOffset:])) 107 } 108 109 // Options returns an NDPOptions of the the options body. 110 func (b NDPRouterAdvert) Options() NDPOptions { 111 return NDPOptions(b[ndpRAOptionsOffset:]) 112 }