Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 17 | package org.onlab.packet; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 25 | import static org.onlab.packet.PacketUtils.*; |
| 26 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 27 | /** |
Thomas Vachuska | e4ed1a2 | 2015-08-26 15:19:05 -0700 | [diff] [blame] | 28 | * Representation of a UDP packet. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 29 | */ |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 30 | public class UDP extends BasePacket { |
Thomas Vachuska | e4ed1a2 | 2015-08-26 15:19:05 -0700 | [diff] [blame] | 31 | public static final Map<Integer, Deserializer<? extends IPacket>> PORT_DESERIALIZER_MAP = |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 32 | ImmutableMap.<Integer, Deserializer<? extends IPacket>>builder() |
Yi Tseng | 60bf35a | 2017-06-02 17:05:48 -0700 | [diff] [blame] | 33 | .put(UDP.DHCP_SERVER_PORT, DHCP.deserializer()) |
| 34 | .put(UDP.DHCP_CLIENT_PORT, DHCP.deserializer()) |
| 35 | .put(UDP.DHCP_V6_SERVER_PORT, DHCP6.deserializer()) |
| 36 | .put(UDP.DHCP_V6_CLIENT_PORT, DHCP6.deserializer()) |
jaegonkim | 1179d83 | 2017-06-13 19:30:55 +0900 | [diff] [blame] | 37 | .put(UDP.VXLAN_UDP_PORT, VXLAN.deserializer()) |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 38 | .put(UDP.RIP_PORT, RIP.deserializer()) |
| 39 | .put(UDP.RIPNG_PORT, RIPng.deserializer()) |
Yi Tseng | 60bf35a | 2017-06-02 17:05:48 -0700 | [diff] [blame] | 40 | .build(); |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 41 | |
Thomas Vachuska | e4ed1a2 | 2015-08-26 15:19:05 -0700 | [diff] [blame] | 42 | public static final int DHCP_SERVER_PORT = 67; |
| 43 | public static final int DHCP_CLIENT_PORT = 68; |
Yi Tseng | 60bf35a | 2017-06-02 17:05:48 -0700 | [diff] [blame] | 44 | public static final int DHCP_V6_SERVER_PORT = 547; |
| 45 | public static final int DHCP_V6_CLIENT_PORT = 546; |
jaegonkim | 1179d83 | 2017-06-13 19:30:55 +0900 | [diff] [blame] | 46 | public static final int VXLAN_UDP_PORT = 4789; |
Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 47 | public static final int RIP_PORT = 520; |
| 48 | public static final int RIPNG_PORT = 521; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 49 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 50 | private static final short UDP_HEADER_LENGTH = 8; |
| 51 | |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 52 | protected int sourcePort; |
| 53 | protected int destinationPort; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 54 | protected short length; |
| 55 | protected short checksum; |
| 56 | |
| 57 | /** |
| 58 | * @return the sourcePort |
| 59 | */ |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 60 | public int getSourcePort() { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 61 | return this.sourcePort; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param sourcePort |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 66 | * the sourcePort to set (16 bits unsigned integer) |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 67 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 68 | */ |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 69 | public UDP setSourcePort(final int sourcePort) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 70 | this.sourcePort = sourcePort; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return the destinationPort |
| 76 | */ |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 77 | public int getDestinationPort() { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 78 | return this.destinationPort; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param destinationPort |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 83 | * the destinationPort to set (16 bits unsigned integer) |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 84 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 85 | */ |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 86 | public UDP setDestinationPort(final int destinationPort) { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 87 | this.destinationPort = destinationPort; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return the length |
| 93 | */ |
| 94 | public short getLength() { |
| 95 | return this.length; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return the checksum |
| 100 | */ |
| 101 | public short getChecksum() { |
| 102 | return this.checksum; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param checksum |
| 107 | * the checksum to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 108 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 109 | */ |
| 110 | public UDP setChecksum(final short checksum) { |
| 111 | this.checksum = checksum; |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void resetChecksum() { |
| 117 | this.checksum = 0; |
| 118 | super.resetChecksum(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Serializes the packet. Will compute and set the following fields if they |
| 123 | * are set to specific values at the time serialize is called: -checksum : 0 |
| 124 | * -length : 0 |
| 125 | */ |
| 126 | @Override |
| 127 | public byte[] serialize() { |
| 128 | byte[] payloadData = null; |
| 129 | if (this.payload != null) { |
| 130 | this.payload.setParent(this); |
| 131 | payloadData = this.payload.serialize(); |
| 132 | } |
| 133 | |
| 134 | this.length = (short) (8 + (payloadData == null ? 0 |
| 135 | : payloadData.length)); |
| 136 | |
| 137 | final byte[] data = new byte[this.length]; |
| 138 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 139 | |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 140 | bb.putShort((short) (this.sourcePort & 0xffff)); |
| 141 | bb.putShort((short) (this.destinationPort & 0xffff)); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 142 | bb.putShort(this.length); |
| 143 | bb.putShort(this.checksum); |
| 144 | if (payloadData != null) { |
| 145 | bb.put(payloadData); |
| 146 | } |
| 147 | |
| 148 | if (this.parent != null && this.parent instanceof IPv4) { |
| 149 | ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_UDP); |
| 150 | } |
| 151 | |
| 152 | // compute checksum if needed |
| 153 | if (this.checksum == 0) { |
| 154 | bb.rewind(); |
| 155 | int accumulation = 0; |
| 156 | |
| 157 | // compute pseudo header mac |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 158 | if (this.parent != null) { |
| 159 | if (this.parent instanceof IPv4) { |
| 160 | final IPv4 ipv4 = (IPv4) this.parent; |
| 161 | accumulation += (ipv4.getSourceAddress() >> 16 & 0xffff) |
| 162 | + (ipv4.getSourceAddress() & 0xffff); |
| 163 | accumulation += (ipv4.getDestinationAddress() >> 16 & 0xffff) |
| 164 | + (ipv4.getDestinationAddress() & 0xffff); |
| 165 | accumulation += ipv4.getProtocol() & 0xff; |
| 166 | accumulation += length & 0xffff; |
| 167 | } else if (this.parent instanceof IPv6) { |
| 168 | final IPv6 ipv6 = (IPv6) this.parent; |
| 169 | final int bbLength = |
| 170 | Ip6Address.BYTE_LENGTH * 2 // IPv6 src, dst |
| 171 | + 2 // nextHeader (with padding) |
| 172 | + 4; // length |
| 173 | final ByteBuffer bbChecksum = ByteBuffer.allocate(bbLength); |
| 174 | bbChecksum.put(ipv6.getSourceAddress()); |
| 175 | bbChecksum.put(ipv6.getDestinationAddress()); |
| 176 | bbChecksum.put((byte) 0); // padding |
| 177 | bbChecksum.put(ipv6.getNextHeader()); |
| 178 | bbChecksum.putInt(length); |
| 179 | bbChecksum.rewind(); |
| 180 | for (int i = 0; i < bbLength / 2; ++i) { |
| 181 | accumulation += 0xffff & bbChecksum.getShort(); |
| 182 | } |
| 183 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | for (int i = 0; i < this.length / 2; ++i) { |
| 187 | accumulation += 0xffff & bb.getShort(); |
| 188 | } |
| 189 | // pad to an even number of shorts |
| 190 | if (this.length % 2 > 0) { |
| 191 | accumulation += (bb.get() & 0xff) << 8; |
| 192 | } |
| 193 | |
| 194 | accumulation = (accumulation >> 16 & 0xffff) |
| 195 | + (accumulation & 0xffff); |
| 196 | this.checksum = (short) (~accumulation & 0xffff); |
| 197 | bb.putShort(6, this.checksum); |
| 198 | } |
| 199 | return data; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * (non-Javadoc) |
| 204 | * |
| 205 | * @see java.lang.Object#hashCode() |
| 206 | */ |
| 207 | @Override |
| 208 | public int hashCode() { |
| 209 | final int prime = 5807; |
| 210 | int result = super.hashCode(); |
| 211 | result = prime * result + this.checksum; |
| 212 | result = prime * result + this.destinationPort; |
| 213 | result = prime * result + this.length; |
| 214 | result = prime * result + this.sourcePort; |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * (non-Javadoc) |
| 220 | * |
| 221 | * @see java.lang.Object#equals(java.lang.Object) |
| 222 | */ |
| 223 | @Override |
| 224 | public boolean equals(final Object obj) { |
| 225 | if (this == obj) { |
| 226 | return true; |
| 227 | } |
| 228 | if (!super.equals(obj)) { |
| 229 | return false; |
| 230 | } |
| 231 | if (!(obj instanceof UDP)) { |
| 232 | return false; |
| 233 | } |
| 234 | final UDP other = (UDP) obj; |
| 235 | if (this.checksum != other.checksum) { |
| 236 | return false; |
| 237 | } |
| 238 | if (this.destinationPort != other.destinationPort) { |
| 239 | return false; |
| 240 | } |
| 241 | if (this.length != other.length) { |
| 242 | return false; |
| 243 | } |
| 244 | if (this.sourcePort != other.sourcePort) { |
| 245 | return false; |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Deserializer function for UDP packets. |
| 252 | * |
| 253 | * @return deserializer function |
| 254 | */ |
| 255 | public static Deserializer<UDP> deserializer() { |
| 256 | return (data, offset, length) -> { |
| 257 | checkInput(data, offset, length, UDP_HEADER_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 258 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 259 | UDP udp = new UDP(); |
| 260 | |
| 261 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 262 | udp.sourcePort = (bb.getShort() & 0xffff); |
| 263 | udp.destinationPort = (bb.getShort() & 0xffff); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 264 | udp.length = bb.getShort(); |
| 265 | udp.checksum = bb.getShort(); |
| 266 | |
| 267 | Deserializer<? extends IPacket> deserializer; |
| 268 | if (UDP.PORT_DESERIALIZER_MAP.containsKey(udp.destinationPort)) { |
| 269 | deserializer = UDP.PORT_DESERIALIZER_MAP.get(udp.destinationPort); |
| 270 | } else if (UDP.PORT_DESERIALIZER_MAP.containsKey(udp.sourcePort)) { |
| 271 | deserializer = UDP.PORT_DESERIALIZER_MAP.get(udp.sourcePort); |
| 272 | } else { |
| 273 | deserializer = Data.deserializer(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 274 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 275 | |
| 276 | udp.payload = deserializer.deserialize(data, bb.position(), |
| 277 | bb.limit() - bb.position()); |
| 278 | udp.payload.setParent(udp); |
| 279 | return udp; |
| 280 | }; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 281 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 282 | |
| 283 | @Override |
| 284 | public String toString() { |
| 285 | return toStringHelper(getClass()) |
| 286 | .add("sourcePort", Integer.toString(sourcePort)) |
| 287 | .add("destinationPort", Integer.toString(destinationPort)) |
| 288 | .add("length", Short.toString(length)) |
| 289 | .add("checksum", Short.toString(checksum)) |
| 290 | .toString(); |
| 291 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 292 | } |