github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/tcpip/header/ndp_neighbor_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 "github.com/nicocha30/gvisor-ligolo/pkg/tcpip"
    18  
    19  // NDPNeighborAdvert is an NDP Neighbor Advertisement message. It will
    20  // only contain the body of an ICMPv6 packet.
    21  //
    22  // See RFC 4861 section 4.4 for more details.
    23  type NDPNeighborAdvert []byte
    24  
    25  const (
    26  	// NDPNAMinimumSize is the minimum size of a valid NDP Neighbor
    27  	// Advertisement message (body of an ICMPv6 packet).
    28  	NDPNAMinimumSize = 20
    29  
    30  	// ndpNATargetAddressOffset is the start of the Target Address
    31  	// field within an NDPNeighborAdvert.
    32  	ndpNATargetAddressOffset = 4
    33  
    34  	// ndpNAOptionsOffset is the start of the NDP options in an
    35  	// NDPNeighborAdvert.
    36  	ndpNAOptionsOffset = ndpNATargetAddressOffset + IPv6AddressSize
    37  
    38  	// ndpNAFlagsOffset is the offset of the flags within an
    39  	// NDPNeighborAdvert
    40  	ndpNAFlagsOffset = 0
    41  
    42  	// ndpNARouterFlagMask is the mask of the Router Flag field in
    43  	// the flags byte within in an NDPNeighborAdvert.
    44  	ndpNARouterFlagMask = (1 << 7)
    45  
    46  	// ndpNASolicitedFlagMask is the mask of the Solicited Flag field in
    47  	// the flags byte within in an NDPNeighborAdvert.
    48  	ndpNASolicitedFlagMask = (1 << 6)
    49  
    50  	// ndpNAOverrideFlagMask is the mask of the Override Flag field in
    51  	// the flags byte within in an NDPNeighborAdvert.
    52  	ndpNAOverrideFlagMask = (1 << 5)
    53  )
    54  
    55  // TargetAddress returns the value within the Target Address field.
    56  func (b NDPNeighborAdvert) TargetAddress() tcpip.Address {
    57  	return tcpip.AddrFrom16Slice(b[ndpNATargetAddressOffset:][:IPv6AddressSize])
    58  }
    59  
    60  // SetTargetAddress sets the value within the Target Address field.
    61  func (b NDPNeighborAdvert) SetTargetAddress(addr tcpip.Address) {
    62  	copy(b[ndpNATargetAddressOffset:][:IPv6AddressSize], addr.AsSlice())
    63  }
    64  
    65  // RouterFlag returns the value of the Router Flag field.
    66  func (b NDPNeighborAdvert) RouterFlag() bool {
    67  	return b[ndpNAFlagsOffset]&ndpNARouterFlagMask != 0
    68  }
    69  
    70  // SetRouterFlag sets the value in the Router Flag field.
    71  func (b NDPNeighborAdvert) SetRouterFlag(f bool) {
    72  	if f {
    73  		b[ndpNAFlagsOffset] |= ndpNARouterFlagMask
    74  	} else {
    75  		b[ndpNAFlagsOffset] &^= ndpNARouterFlagMask
    76  	}
    77  }
    78  
    79  // SolicitedFlag returns the value of the Solicited Flag field.
    80  func (b NDPNeighborAdvert) SolicitedFlag() bool {
    81  	return b[ndpNAFlagsOffset]&ndpNASolicitedFlagMask != 0
    82  }
    83  
    84  // SetSolicitedFlag sets the value in the Solicited Flag field.
    85  func (b NDPNeighborAdvert) SetSolicitedFlag(f bool) {
    86  	if f {
    87  		b[ndpNAFlagsOffset] |= ndpNASolicitedFlagMask
    88  	} else {
    89  		b[ndpNAFlagsOffset] &^= ndpNASolicitedFlagMask
    90  	}
    91  }
    92  
    93  // OverrideFlag returns the value of the Override Flag field.
    94  func (b NDPNeighborAdvert) OverrideFlag() bool {
    95  	return b[ndpNAFlagsOffset]&ndpNAOverrideFlagMask != 0
    96  }
    97  
    98  // SetOverrideFlag sets the value in the Override Flag field.
    99  func (b NDPNeighborAdvert) SetOverrideFlag(f bool) {
   100  	if f {
   101  		b[ndpNAFlagsOffset] |= ndpNAOverrideFlagMask
   102  	} else {
   103  		b[ndpNAFlagsOffset] &^= ndpNAOverrideFlagMask
   104  	}
   105  }
   106  
   107  // Options returns an NDPOptions of the the options body.
   108  func (b NDPNeighborAdvert) Options() NDPOptions {
   109  	return NDPOptions(b[ndpNAOptionsOffset:])
   110  }