summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header/ndp_neighbor_advert.go
blob: 5c2b472c84ea0908e12e0e900d5265605cb9dcd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2019 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package header

import "gvisor.dev/gvisor/pkg/tcpip"

// NDPNeighborAdvert is an NDP Neighbor Advertisement message. It will
// only contain the body of an ICMPv6 packet.
type NDPNeighborAdvert []byte

const (
	// NDPNAMinimumSize is the minimum size of a valid NDP Neighbor
	// Advertisement message (body of an ICMPv6 packet).
	NDPNAMinimumSize = 20

	// ndpNATargetAddressOffset is the start of the Target Address
	// field within an NDPNeighborAdvert.
	ndpNATargetAddressOffset = 4

	// ndpNAOptionsOffset is the start of the NDP options in an
	// NDPNeighborAdvert.
	ndpNAOptionsOffset = ndpNATargetAddressOffset + IPv6AddressSize

	// ndpNAFlagsOffset is the offset of the flags within an
	// NDPNeighborAdvert
	ndpNAFlagsOffset = 0

	// ndpNARouterFlagMask is the mask of the Router Flag field in
	// the flags byte within in an NDPNeighborAdvert.
	ndpNARouterFlagMask = (1 << 7)

	// ndpNASolicitedFlagMask is the mask of the Solicited Flag field in
	// the flags byte within in an NDPNeighborAdvert.
	ndpNASolicitedFlagMask = (1 << 6)

	// ndpNAOverrideFlagMask is the mask of the Override Flag field in
	// the flags byte within in an NDPNeighborAdvert.
	ndpNAOverrideFlagMask = (1 << 5)
)

// TargetAddress returns the value within the Target Address field.
func (b NDPNeighborAdvert) TargetAddress() tcpip.Address {
	return tcpip.Address(b[ndpNATargetAddressOffset:][:IPv6AddressSize])
}

// SetTargetAddress sets the value within the Target Address field.
func (b NDPNeighborAdvert) SetTargetAddress(addr tcpip.Address) {
	copy(b[ndpNATargetAddressOffset:][:IPv6AddressSize], addr)
}

// RouterFlag returns the value of the Router Flag field.
func (b NDPNeighborAdvert) RouterFlag() bool {
	return b[ndpNAFlagsOffset]&ndpNARouterFlagMask != 0
}

// SetRouterFlag sets the value in the Router Flag field.
func (b NDPNeighborAdvert) SetRouterFlag(f bool) {
	if f {
		b[ndpNAFlagsOffset] |= ndpNARouterFlagMask
	} else {
		b[ndpNAFlagsOffset] &^= ndpNARouterFlagMask
	}
}

// SolicitedFlag returns the value of the Solicited Flag field.
func (b NDPNeighborAdvert) SolicitedFlag() bool {
	return b[ndpNAFlagsOffset]&ndpNASolicitedFlagMask != 0
}

// SetSolicitedFlag sets the value in the Solicited Flag field.
func (b NDPNeighborAdvert) SetSolicitedFlag(f bool) {
	if f {
		b[ndpNAFlagsOffset] |= ndpNASolicitedFlagMask
	} else {
		b[ndpNAFlagsOffset] &^= ndpNASolicitedFlagMask
	}
}

// OverrideFlag returns the value of the Override Flag field.
func (b NDPNeighborAdvert) OverrideFlag() bool {
	return b[ndpNAFlagsOffset]&ndpNAOverrideFlagMask != 0
}

// SetOverrideFlag sets the value in the Override Flag field.
func (b NDPNeighborAdvert) SetOverrideFlag(f bool) {
	if f {
		b[ndpNAFlagsOffset] |= ndpNAOverrideFlagMask
	} else {
		b[ndpNAFlagsOffset] &^= ndpNAOverrideFlagMask
	}
}

// Options returns an NDPOptions of the the options body.
func (b NDPNeighborAdvert) Options() NDPOptions {
	return NDPOptions(b[ndpNAOptionsOffset:])
}