Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onlab.packet.dhcp; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import org.onlab.packet.DHCP6; |
| 22 | import org.onlab.packet.Deserializer; |
| 23 | import org.onlab.packet.DeserializationException; |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | import java.nio.ByteBuffer; |
| 28 | |
| 29 | /** |
| 30 | * Relay option for DHCPv6. |
| 31 | * Based on RFC-3315. |
| 32 | */ |
| 33 | public final class Dhcp6InterfaceIdOption extends Dhcp6Option { |
Kalhee Kim | a0b8f65 | 2017-10-18 22:00:30 +0000 | [diff] [blame] | 34 | private static final short VLAN_LEN = 2; |
| 35 | private static final short SEPARATOR_LEN = 1; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 36 | private MacAddress peerMacAddr; |
| 37 | private byte[] inPort; |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 38 | private short vlanId; |
| 39 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 40 | @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 Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 79 | |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 80 | /** |
| 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 Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 108 | * 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 Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 127 | * 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 Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 139 | if (optionData.length >= 31) { |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 140 | ByteBuffer bb = ByteBuffer.wrap(optionData); |
| 141 | |
| 142 | byte[] macAddr = new byte[MacAddress.MAC_ADDRESS_LENGTH]; |
Kalhee Kim | a0b8f65 | 2017-10-18 22:00:30 +0000 | [diff] [blame] | 143 | byte[] port = new byte[optionData.length - MacAddress.MAC_ADDRESS_LENGTH - |
| 144 | VLAN_LEN - SEPARATOR_LEN * 2]; |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 145 | short vlan; |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 146 | bb.get(macAddr); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 147 | bb.get(); // separator "-" |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 148 | bb.get(port); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 149 | bb.get(); // separator ":" |
| 150 | vlan = bb.getShort(); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 151 | interfaceIdOption.setMacAddress(MacAddress.valueOf(macAddr)); |
| 152 | interfaceIdOption.setInPort(port); |
Kalhee Kim | 0c0cb0b | 2017-09-15 17:43:27 +0000 | [diff] [blame] | 153 | interfaceIdOption.setVlanId(vlan > VlanId.MAX_VLAN ? VlanId.UNTAGGED : vlan); |
Kalhee Kim | 45fede4 | 2017-09-05 19:05:06 +0000 | [diff] [blame] | 154 | } |
| 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 | } |