blob: 25ef6a3b77e85777c4d6deff135fa898a6c80529 [file] [log] [blame]
Yi Tseng60bf35a2017-06-02 17:05:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tseng60bf35a2017-06-02 17:05:48 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.packet;
18
Yi Tsengca34e1d2017-07-18 16:16:25 -070019import com.google.common.collect.ImmutableMap;
Yi Tseng60bf35a2017-06-02 17:05:48 -070020import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Lists;
Yi Tsengca34e1d2017-07-18 16:16:25 -070022import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
23import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
24import org.onlab.packet.dhcp.Dhcp6IaNaOption;
25import org.onlab.packet.dhcp.Dhcp6IaTaOption;
Kalhee Kim45fede42017-09-05 19:05:06 +000026import org.onlab.packet.dhcp.Dhcp6IaPdOption;
Yi Tsengc7403c22017-06-19 16:23:22 -070027import org.onlab.packet.dhcp.Dhcp6Option;
Yi Tsengca34e1d2017-07-18 16:16:25 -070028import org.onlab.packet.dhcp.Dhcp6RelayOption;
Kalhee Kim45fede42017-09-05 19:05:06 +000029import org.onlab.packet.dhcp.Dhcp6InterfaceIdOption;
Yi Tseng60bf35a2017-06-02 17:05:48 -070030
31import java.nio.ByteBuffer;
32import java.util.List;
Yi Tsengca34e1d2017-07-18 16:16:25 -070033import java.util.Map;
Yi Tseng60bf35a2017-06-02 17:05:48 -070034import java.util.Set;
35
Yi Tsengca34e1d2017-07-18 16:16:25 -070036import static com.google.common.base.MoreObjects.toStringHelper;
Yi Tseng60bf35a2017-06-02 17:05:48 -070037import static com.google.common.base.Preconditions.checkNotNull;
38
39/**
40 * Representation of an DHCPv6 Packet.
41 * Base on RFC-3315.
42 */
43public class DHCP6 extends BasePacket {
Yi Tsengb4fdb042017-08-07 13:32:32 -070044 private static final int UNSIGNED_SHORT_MASK = 0xffff;
Yi Tseng60bf35a2017-06-02 17:05:48 -070045 // size of different field of option
46 private static final int OPT_CODE_SIZE = 2;
47 private static final int OPT_LEN_SIZE = 2;
48
49 // default size of DHCPv6 payload (without options)
50 private static final int DHCP6_DEFAULT_SIZE = 4;
51
52 // default size of DHCPv6 relay message payload (without options)
53 private static final int DHCP6_RELAY_MSG_SIZE = 34;
54 private static final int IPV6_ADDR_LEN = 16;
55
56 // masks & offsets for default DHCPv6 header
57 private static final int MSG_TYPE_OFFSET = 24;
58 private static final int TRANSACTION_ID_MASK = 0x00ffffff;
59
60 // Relay message types
Yi Tsengca34e1d2017-07-18 16:16:25 -070061 public static final Set<Byte> RELAY_MSG_TYPES =
Yi Tseng60bf35a2017-06-02 17:05:48 -070062 ImmutableSet.of(MsgType.RELAY_FORW.value,
63 MsgType.RELAY_REPL.value);
64
65 /**
66 * DHCPv6 message type.
67 */
68 public enum MsgType {
69 SOLICIT((byte) 1), ADVERTISE((byte) 2), REQUEST((byte) 3),
70 CONFIRM((byte) 4), RENEW((byte) 5), REBIND((byte) 6),
71 REPLY((byte) 7), RELEASE((byte) 8), DECLINE((byte) 9),
72 RECONFIGURE((byte) 10), INFORMATION_REQUEST((byte) 11),
73 RELAY_FORW((byte) 12), RELAY_REPL((byte) 13);
74
75 protected byte value;
76 MsgType(final byte value) {
77 this.value = value;
78 }
79 public byte value() {
80 return this.value;
81 }
Kalhee Kimea4b6c22017-11-09 14:38:37 +000082 public static MsgType getType(final int value) {
83 switch (value) {
84 case 1:
85 return SOLICIT;
86 case 2:
87 return ADVERTISE;
88 case 3:
89 return REQUEST;
90 case 4:
91 return CONFIRM;
92 case 5:
93 return RENEW;
94 case 6:
95 return REBIND;
96 case 7:
97 return REPLY;
98 case 8:
99 return RELEASE;
100 case 9:
101 return DECLINE;
102 case 10:
103 return RECONFIGURE;
104 case 11:
105 return INFORMATION_REQUEST;
106 case 12:
107 return RELAY_FORW;
108 case 13:
109 return RELAY_REPL;
110 default:
111 return null;
112 }
113 }
Yi Tseng60bf35a2017-06-02 17:05:48 -0700114 }
115
116 /**
117 * DHCPv6 option code.
118 */
119 public enum OptionCode {
120 CLIENTID((short) 1), SERVERID((short) 2), IA_NA((short) 3), IA_TA((short) 4),
121 IAADDR((short) 5), ORO((short) 6), PREFERENCE((short) 7), ELAPSED_TIME((short) 8),
122 RELAY_MSG((short) 9), AUTH((short) 11), UNICAST((short) 12),
123 STATUS_CODE((short) 13), RAPID_COMMIT((short) 14), USER_CLASS((short) 15),
124 VENDOR_CLASS((short) 16), VENDOR_OPTS((short) 17), INTERFACE_ID((short) 18),
Kalhee Kim45fede42017-09-05 19:05:06 +0000125 RECONF_MSG((short) 19), RECONF_ACCEPT((short) 20), IA_PD((short) 25), IAPREFIX((short) 26),
126 SUBSCRIBER_ID((short) 38);
Yi Tseng60bf35a2017-06-02 17:05:48 -0700127
128 protected short value;
129 OptionCode(final short value) {
130 this.value = value;
131 }
132 public short value() {
133 return this.value;
134 }
135 }
136
Yi Tsengca34e1d2017-07-18 16:16:25 -0700137 private static final Map<Short, Deserializer<Dhcp6Option>> OPT_DESERIALIZERS =
Kalhee Kim45fede42017-09-05 19:05:06 +0000138 ImmutableMap.<Short, Deserializer<Dhcp6Option>>builder()
139 .put(OptionCode.IA_NA.value, Dhcp6IaNaOption.deserializer())
140 .put(OptionCode.IA_TA.value, Dhcp6IaTaOption.deserializer())
141 .put(OptionCode.IAADDR.value, Dhcp6IaAddressOption.deserializer())
142 .put(OptionCode.RELAY_MSG.value, Dhcp6RelayOption.deserializer())
143 .put(OptionCode.CLIENTID.value, Dhcp6ClientIdOption.deserializer())
144 .put(OptionCode.IA_PD.value, Dhcp6IaPdOption.deserializer())
145 .put(OptionCode.INTERFACE_ID.value, Dhcp6InterfaceIdOption.deserializer())
146 .build();
Yi Tsengca34e1d2017-07-18 16:16:25 -0700147
Yi Tseng60bf35a2017-06-02 17:05:48 -0700148 // general field
149 private byte msgType; // 1 byte
Yi Tsengc7403c22017-06-19 16:23:22 -0700150 private List<Dhcp6Option> options;
Yi Tseng60bf35a2017-06-02 17:05:48 -0700151
152 // non-relay field
153 private int transactionId; // 3 bytes
154
155 // relay field
156 private byte hopCount; // 1 byte
157 private byte[] linkAddress; // 16 bytes
158 private byte[] peerAddress; // 16 bytes
159
160 /**
161 * Creates new DHCPv6 object.
162 */
163 public DHCP6() {
164 options = Lists.newArrayList();
165 }
166
167 @Override
168 public byte[] serialize() {
169 int payloadLength = options.stream()
Yi Tsengc7403c22017-06-19 16:23:22 -0700170 .mapToInt(Dhcp6Option::getLength)
Yi Tseng60bf35a2017-06-02 17:05:48 -0700171 .sum();
172
173 // 2 bytes code and 2 bytes length
174 payloadLength += options.size() * (OPT_CODE_SIZE + OPT_LEN_SIZE);
175
176 if (RELAY_MSG_TYPES.contains(msgType)) {
177 payloadLength += DHCP6_RELAY_MSG_SIZE;
178 } else {
179 payloadLength += DHCP6_DEFAULT_SIZE;
180 }
181
182 ByteBuffer bb = ByteBuffer.allocate(payloadLength);
183
184 if (RELAY_MSG_TYPES.contains(msgType)) {
185 bb.put(msgType);
186 bb.put(hopCount);
187 bb.put(linkAddress);
188 bb.put(peerAddress);
189 } else {
190 int defaultHeader = ((int) msgType) << MSG_TYPE_OFFSET | (transactionId & TRANSACTION_ID_MASK);
191 bb.putInt(defaultHeader);
192 }
193
194 // serialize options
195 options.forEach(option -> {
Yi Tsengca34e1d2017-07-18 16:16:25 -0700196 bb.put(option.serialize());
Yi Tseng60bf35a2017-06-02 17:05:48 -0700197 });
198
199 return bb.array();
200 }
201
202 /**
203 * Returns a deserializer for DHCPv6.
204 *
205 * @return the deserializer for DHCPv6
206 */
207 public static Deserializer<DHCP6> deserializer() {
208 return (data, offset, length) -> {
209 DHCP6 dhcp6 = new DHCP6();
210
211 checkNotNull(data);
212
213 if (offset < 0 || length < 0 ||
214 length > data.length || offset >= data.length ||
215 offset + length > data.length) {
216 throw new DeserializationException("Illegal offset or length");
217 }
218
219 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
220 if (bb.remaining() < DHCP6.DHCP6_DEFAULT_SIZE) {
221 throw new DeserializationException(
222 "Buffer underflow while reading DHCPv6 option");
223 }
224
225 // peek message type
Yi Tsengb4fdb042017-08-07 13:32:32 -0700226 dhcp6.msgType = bb.array()[offset];
Yi Tseng60bf35a2017-06-02 17:05:48 -0700227 if (RELAY_MSG_TYPES.contains(dhcp6.msgType)) {
228 bb.get(); // drop message type
229 dhcp6.hopCount = bb.get();
230 dhcp6.linkAddress = new byte[IPV6_ADDR_LEN];
231 dhcp6.peerAddress = new byte[IPV6_ADDR_LEN];
232
233 bb.get(dhcp6.linkAddress);
234 bb.get(dhcp6.peerAddress);
235 } else {
236 // get msg type + transaction id (1 + 3 bytes)
237 int defaultHeader = bb.getInt();
238 dhcp6.transactionId = defaultHeader & TRANSACTION_ID_MASK;
239 }
240
241 dhcp6.options = Lists.newArrayList();
Yi Tsengca34e1d2017-07-18 16:16:25 -0700242 while (bb.remaining() >= Dhcp6Option.DEFAULT_LEN) {
243 // create temporary byte buffer for reading code and length
244 ByteBuffer optByteBuffer =
Yi Tsengb4fdb042017-08-07 13:32:32 -0700245 ByteBuffer.wrap(data, bb.position(), bb.limit() - bb.position());
Yi Tsengca34e1d2017-07-18 16:16:25 -0700246 short code = optByteBuffer.getShort();
Yi Tsengb4fdb042017-08-07 13:32:32 -0700247 int optionLen = UNSIGNED_SHORT_MASK & optByteBuffer.getShort();
Yi Tsengca34e1d2017-07-18 16:16:25 -0700248 if (optByteBuffer.remaining() < optionLen) {
Yi Tseng60bf35a2017-06-02 17:05:48 -0700249 throw new DeserializationException(
250 "Buffer underflow while reading DHCPv6 option");
251 }
Yi Tsengca34e1d2017-07-18 16:16:25 -0700252 Dhcp6Option option;
253 byte[] optionData = new byte[Dhcp6Option.DEFAULT_LEN + optionLen];
Yi Tseng60bf35a2017-06-02 17:05:48 -0700254 bb.get(optionData);
Yi Tsengca34e1d2017-07-18 16:16:25 -0700255 if (OPT_DESERIALIZERS.containsKey(code)) {
256 option = OPT_DESERIALIZERS.get(code).deserialize(optionData, 0, optionData.length);
257 } else {
258 option = Dhcp6Option.deserializer().deserialize(optionData, 0, optionData.length);
259 }
260 option.setParent(dhcp6);
Yi Tseng60bf35a2017-06-02 17:05:48 -0700261 dhcp6.options.add(option);
262 }
263
264 return dhcp6;
265 };
266 }
267
Yi Tseng60bf35a2017-06-02 17:05:48 -0700268
269 /**
270 * Gets the message type of this DHCPv6 packet.
271 *
272 * @return the message type
273 */
274 public byte getMsgType() {
275 return msgType;
276 }
277
278 /**
279 * Gets options from this DHCPv6 packet.
280 *
281 * @return DHCPv6 options
282 */
Yi Tsengc7403c22017-06-19 16:23:22 -0700283 public List<Dhcp6Option> getOptions() {
Yi Tseng60bf35a2017-06-02 17:05:48 -0700284 return options;
285 }
286
287 /**
288 * Gets the transaction ID of this DHCPv6 packet.
289 *
290 * @return the transaction ID
291 */
292 public int getTransactionId() {
293 return transactionId;
294 }
295
296 /**
297 * Gets the hop count of this DHCPv6 relay message.
298 *
299 * @return the hop count
300 */
301 public byte getHopCount() {
302 return hopCount;
303 }
304
305 /**
306 * Gets the link address of this DHCPv6 relay message.
307 *
308 * @return the link address
309 */
310 public byte[] getLinkAddress() {
311 return linkAddress;
312 }
313
314 /**
Yi Tsengca34e1d2017-07-18 16:16:25 -0700315 * Gets IPv6 link address.
316 *
317 * @return the IPv6 link address
318 */
319 public Ip6Address getIp6LinkAddress() {
320 return linkAddress == null ? null : Ip6Address.valueOf(linkAddress);
321 }
322
323 /**
Yi Tseng60bf35a2017-06-02 17:05:48 -0700324 * Gets the peer address of this DHCPv6 relay message.
325 *
326 * @return the link address
327 */
328 public byte[] getPeerAddress() {
329 return peerAddress;
330 }
331
332 /**
Yi Tsengca34e1d2017-07-18 16:16:25 -0700333 * Gets IPv6 peer address.
334 *
335 * @return the IPv6 peer address
336 */
337 public Ip6Address getIp6PeerAddress() {
338 return peerAddress == null ? null : Ip6Address.valueOf(peerAddress);
339 }
340
341 /**
Yi Tseng60bf35a2017-06-02 17:05:48 -0700342 * Sets message type.
343 *
344 * @param msgType the message type
345 */
346 public void setMsgType(byte msgType) {
347 this.msgType = msgType;
348 }
349
350 /**
351 * Sets options.
352 *
353 * @param options the options
354 */
Yi Tsengc7403c22017-06-19 16:23:22 -0700355 public void setOptions(List<Dhcp6Option> options) {
Yi Tseng60bf35a2017-06-02 17:05:48 -0700356 this.options = options;
357 }
358
359 /**
360 * Sets transaction id.
361 *
362 * @param transactionId the transaction id
363 */
364 public void setTransactionId(int transactionId) {
365 this.transactionId = transactionId;
366 }
367
368 /**
369 * Sets hop count.
370 *
371 * @param hopCount the hop count
372 */
373 public void setHopCount(byte hopCount) {
374 this.hopCount = hopCount;
375 }
376
377 /**
378 * Sets link address.
379 *
380 * @param linkAddress the link address
381 */
382 public void setLinkAddress(byte[] linkAddress) {
383 this.linkAddress = linkAddress;
384 }
385
386 /**
387 * Sets peer address.
388 *
389 * @param peerAddress the peer address
390 */
391 public void setPeerAddress(byte[] peerAddress) {
392 this.peerAddress = peerAddress;
393 }
Yi Tsengca34e1d2017-07-18 16:16:25 -0700394
395 @Override
396 public String toString() {
397 if (RELAY_MSG_TYPES.contains(msgType)) {
398 // relay message
399 return toStringHelper(getClass())
400 .add("msgType", msgType)
401 .add("hopCount", hopCount)
402 .add("linkAddress", Ip6Address.valueOf(linkAddress))
403 .add("peerAddress", Ip6Address.valueOf(peerAddress))
404 .add("options", options)
405 .toString();
406 } else {
407 return toStringHelper(getClass())
408 .add("msgType", msgType)
409 .add("transactionId", transactionId)
410 .add("options", options)
411 .toString();
412 }
413 }
Yi Tseng60bf35a2017-06-02 17:05:48 -0700414}