blob: 2bd47e1717cd2b39061e3b769d3742a4cf6d990e [file] [log] [blame]
Kalhee Kim45fede42017-09-05 19:05:06 +00001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
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 */
17
18package org.onlab.packet.dhcp;
19import org.onlab.packet.MacAddress;
20import com.google.common.base.MoreObjects;
21import org.onlab.packet.DHCP6;
22import org.onlab.packet.Deserializer;
23import org.onlab.packet.DeserializationException;
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +000024import org.onlab.packet.VlanId;
Kalhee Kim45fede42017-09-05 19:05:06 +000025
26
27import java.nio.ByteBuffer;
28
29/**
30 * Relay option for DHCPv6.
31 * Based on RFC-3315.
32 */
33public final class Dhcp6InterfaceIdOption extends Dhcp6Option {
Kalhee Kima0b8f652017-10-18 22:00:30 +000034 private static final short VLAN_LEN = 2;
35 private static final short SEPARATOR_LEN = 1;
Kalhee Kim45fede42017-09-05 19:05:06 +000036 private MacAddress peerMacAddr;
37 private byte[] inPort;
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +000038 private short vlanId;
39
Kalhee Kim45fede42017-09-05 19:05:06 +000040 @Override
41 public short getCode() {
42 return DHCP6.OptionCode.INTERFACE_ID.value();
43 }
44
45 @Override
46 public short getLength() {
47 return (short) payload.serialize().length;
48 }
49
50 @Override
51 public byte[] getData() {
52 return this.payload.serialize();
53 }
54
55 /**
56 * Default constructor.
57 */
58 public Dhcp6InterfaceIdOption() {
59 }
60
61 /**
62 * Constructs a DHCPv6 relay option with DHCPv6 option.
63 *
64 * @param dhcp6Option the DHCPv6 option
65 */
66 public Dhcp6InterfaceIdOption(Dhcp6Option dhcp6Option) {
67 super(dhcp6Option);
68 }
69
70 /**
71 * Sets MacAddress address.
72 *
73 * @param macAddress the client peer MacAddress
74 */
75 public void setMacAddress(MacAddress macAddress) {
76 this.peerMacAddr = macAddress;
77 }
78
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +000079
Kalhee Kim45fede42017-09-05 19:05:06 +000080 /**
81 * Gets Mac address.
82 *
83 * @return the client peer mac address
84 */
85 public MacAddress getMacAddress() {
86 return peerMacAddr;
87 }
88
89 /**
90 * Sets inPort string.
91 *
92 * @param port the port from which client packet is received
93 */
94 public void setInPort(byte[] port) {
95 this.inPort = port;
96 }
97
98 /**
99 * Gets inPort string.
100 *
101 * @return the port from which client packet is received
102 */
103 public byte[] getInPort() {
104 return inPort;
105 }
106
107 /**
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000108 * Sets the vlan id of interface id.
109 *
110 * @param vlanId the vlanid of client packet
111 */
112 public void setVlanId(short vlanId) {
113 this.vlanId = vlanId;
114 }
115
116 /**
117 * Gets the vlan id of interface id.
118 *
119 * @return the vlan id
120 *
121 */
122 public short getVlanId() {
123 return vlanId;
124 }
125
126 /**
Kalhee Kim45fede42017-09-05 19:05:06 +0000127 * Gets deserializer for DHCPv6 relay option.
128 *
129 * @return the deserializer
130 */
131 public static Deserializer<Dhcp6Option> deserializer() {
132 return (data, offset, len) -> {
133 Dhcp6Option dhcp6Option = Dhcp6Option.deserializer().deserialize(data, offset, len);
134 if (dhcp6Option.getLength() < DEFAULT_LEN) {
135 throw new DeserializationException("Invalid InterfaceIoption data");
136 }
137 Dhcp6InterfaceIdOption interfaceIdOption = new Dhcp6InterfaceIdOption(dhcp6Option);
138 byte[] optionData = interfaceIdOption.getData();
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000139 if (optionData.length >= 31) {
Kalhee Kim45fede42017-09-05 19:05:06 +0000140 ByteBuffer bb = ByteBuffer.wrap(optionData);
141
142 byte[] macAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH];
Kalhee Kima0b8f652017-10-18 22:00:30 +0000143 byte[] port = new byte[optionData.length - MacAddress.MAC_ADDRESS_LENGTH -
144 VLAN_LEN - SEPARATOR_LEN * 2];
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000145 short vlan;
Kalhee Kim45fede42017-09-05 19:05:06 +0000146 bb.get(macAddr);
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000147 bb.get(); // separator "-"
Kalhee Kim45fede42017-09-05 19:05:06 +0000148 bb.get(port);
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000149 bb.get(); // separator ":"
150 vlan = bb.getShort();
Kalhee Kim45fede42017-09-05 19:05:06 +0000151 interfaceIdOption.setMacAddress(MacAddress.valueOf(macAddr));
152 interfaceIdOption.setInPort(port);
Kalhee Kim0c0cb0b2017-09-15 17:43:27 +0000153 interfaceIdOption.setVlanId(vlan > VlanId.MAX_VLAN ? VlanId.UNTAGGED : vlan);
Kalhee Kim45fede42017-09-05 19:05:06 +0000154 }
155 return interfaceIdOption;
156 };
157 }
158
159 @Override
160 public String toString() {
161 return MoreObjects.toStringHelper(getClass())
162 .add("code", getCode())
163 .add("length", getLength())
164 .add("data", payload.toString())
165 .toString();
166 }
167}