summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/dhcpv6relay.go
blob: 5bca4314f5b56f1317ef62ed3d302ab221860b55 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package dhcpv6

import (
	"errors"
	"fmt"
	"net"

	"github.com/u-root/u-root/pkg/uio"
)

const RelayHeaderSize = 34

// RelayMessage is a DHCPv6 relay agent message as defined by RFC 3315 Section
// 7.
type RelayMessage struct {
	messageType MessageType
	hopCount    uint8
	linkAddr    net.IP
	peerAddr    net.IP
	options     Options
}

// Type is this relay message's types.
func (r *RelayMessage) Type() MessageType {
	return r.messageType
}

// String prints a short human-readable relay message.
func (r *RelayMessage) String() string {
	ret := fmt.Sprintf(
		"RelayMessage(messageType=%v hopcount=%v, linkaddr=%v, peeraddr=%v, %d options)",
		r.Type().String(), r.hopCount, r.linkAddr, r.peerAddr, len(r.options),
	)
	return ret
}

// Summary prints all options associated with this relay message.
func (r *RelayMessage) Summary() string {
	ret := fmt.Sprintf(
		"RelayMessage\n"+
			"  messageType=%v\n"+
			"  hopcount=%v\n"+
			"  linkaddr=%v\n"+
			"  peeraddr=%v\n"+
			"  options=%v\n",
		r.Type().String(),
		r.hopCount,
		r.linkAddr,
		r.peerAddr,
		r.options,
	)
	return ret
}

// ToBytes returns the serialized version of this relay message as defined by
// RFC 3315, Section 7.
func (r *RelayMessage) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(make([]byte, 0, RelayHeaderSize))
	buf.Write8(byte(r.messageType))
	buf.Write8(byte(r.hopCount))
	buf.WriteBytes(r.linkAddr.To16())
	buf.WriteBytes(r.peerAddr.To16())
	buf.WriteBytes(r.options.ToBytes())
	return buf.Data()
}

// SetMessageType sets the message type of this relay message.
func (r *RelayMessage) SetMessageType(messageType MessageType) {
	// not enforcing if message type is not a RELAY_FORW or a RELAY_REPL message
	r.messageType = messageType
}

// HopCount returns the hop count.
func (r *RelayMessage) HopCount() uint8 {
	return r.hopCount
}

// SetHopCount sets the hop count.
func (r *RelayMessage) SetHopCount(hopCount uint8) {
	r.hopCount = hopCount
}

// LinkAddr returns the link address for this relay message.
func (r *RelayMessage) LinkAddr() net.IP {
	return r.linkAddr
}

// SetLinkAddr sets the link address.
func (r *RelayMessage) SetLinkAddr(linkAddr net.IP) {
	r.linkAddr = linkAddr
}

// PeerAddr returns the peer address for this relay message.
func (r *RelayMessage) PeerAddr() net.IP {
	return r.peerAddr
}

// SetPeerAddr sets the peer address.
func (r *RelayMessage) SetPeerAddr(peerAddr net.IP) {
	r.peerAddr = peerAddr
}

// Options returns the current set of options associated with this message.
func (r *RelayMessage) Options() []Option {
	return r.options
}

// GetOption returns the options associated with the code.
func (r *RelayMessage) GetOption(code OptionCode) []Option {
	return r.options.Get(code)
}

// GetOneOption returns the first associated option with the code from this
// message.
func (r *RelayMessage) GetOneOption(code OptionCode) Option {
	return r.options.GetOne(code)
}

// SetOptions replaces this message's options.
func (r *RelayMessage) SetOptions(options []Option) {
	r.options = options
}

// AddOption adds an option to this message.
func (r *RelayMessage) AddOption(option Option) {
	r.options.Add(option)
}

// UpdateOption replaces the first option of the same type as the specified one.
func (r *RelayMessage) UpdateOption(option Option) {
	r.options.Update(option)
}

// IsRelay returns whether this is a relay message or not.
func (r *RelayMessage) IsRelay() bool {
	return true
}

// GetInnerMessage recurses into a relay message and extract and return the
// inner Message.  Return nil if none found (e.g. not a relay message).
func (r *RelayMessage) GetInnerMessage() (DHCPv6, error) {
	var (
		p   DHCPv6
		err error
	)
	p = r
	for {
		if !p.IsRelay() {
			return p, nil
		}
		p, err = DecapsulateRelay(p)
		if err != nil {
			return nil, err
		}
	}
}

// NewRelayReplFromRelayForw creates a MessageTypeRelayReply based on a
// MessageTypeRelayForward and replaces the inner message with the passed
// DHCPv6 message. It copies the OptionInterfaceID and OptionRemoteID if the
// options are present in the Relay packet.
func NewRelayReplFromRelayForw(relayForw, msg DHCPv6) (DHCPv6, error) {
	var (
		err                error
		linkAddr, peerAddr []net.IP
		optiid             []Option
		optrid             []Option
	)
	if relayForw == nil {
		return nil, errors.New("Relay message cannot be nil")
	}
	relay, ok := relayForw.(*RelayMessage)
	if !ok {
		return nil, errors.New("Not a RelayMessage")
	}
	if relay.Type() != MessageTypeRelayForward {
		return nil, errors.New("The passed packet is not of type MessageTypeRelayForward")
	}
	if msg == nil {
		return nil, errors.New("The passed message cannot be nil")
	}
	if msg.IsRelay() {
		return nil, errors.New("The passed message cannot be a relay")
	}
	for {
		linkAddr = append(linkAddr, relay.LinkAddr())
		peerAddr = append(peerAddr, relay.PeerAddr())
		optiid = append(optiid, relay.GetOneOption(OptionInterfaceID))
		optrid = append(optrid, relay.GetOneOption(OptionRemoteID))
		decap, err := DecapsulateRelay(relay)
		if err != nil {
			return nil, err
		}
		if decap.IsRelay() {
			relay = decap.(*RelayMessage)
		} else {
			break
		}
	}
	for i := len(linkAddr) - 1; i >= 0; i-- {
		msg, err = EncapsulateRelay(msg, MessageTypeRelayReply, linkAddr[i], peerAddr[i])
		if err != nil {
			return nil, err
		}
		if opt := optiid[i]; opt != nil {
			msg.AddOption(opt)
		}
		if opt := optrid[i]; opt != nil {
			msg.AddOption(opt)
		}
	}
	return msg, nil
}