blob: 13c0545548bb34aaf05293ff3bb02dab64336247 [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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
17package org.onlab.packet.ipv6;
18
19import org.onlab.packet.BasePacket;
20import org.onlab.packet.Data;
Jonathan Hart2a655752015-04-07 16:46:33 -070021import org.onlab.packet.Deserializer;
Charles M.C. Chan94f37372015-01-10 17:53:42 +080022import org.onlab.packet.IPacket;
23import org.onlab.packet.IPv6;
Jonathan Hart2a655752015-04-07 16:46:33 -070024
Charles M.C. Chan94f37372015-01-10 17:53:42 +080025import java.nio.ByteBuffer;
26
Jian Li5fc14292015-12-04 11:30:46 -080027import static com.google.common.base.MoreObjects.toStringHelper;
Jonathan Hart2a655752015-04-07 16:46:33 -070028import static org.onlab.packet.PacketUtils.checkInput;
29
Charles M.C. Chan94f37372015-01-10 17:53:42 +080030/**
31 * Implements IPv6 Encapsulating Security Payload (ESP) extension header format.
32 * (RFC 4303)
33 */
34public class EncapSecurityPayload extends BasePacket {
35 public static final byte HEADER_LENGTH = 8; // bytes
36
37 protected int securityParamIndex;
38 protected int sequence;
39 //
40 // NOTE: The remaining fields including payload data, padding length and
41 // next header are encrypted and all considered as a payload of ESP.
42 //
43
44 /**
45 * Gets the security parameter index of this header.
46 *
47 * @return the security parameter index
48 */
49 public int getSecurityParamIndex() {
50 return this.securityParamIndex;
51 }
52
53 /**
54 * Sets the security parameter index of this header.
55 *
56 * @param securityParamIndex the security parameter index to set
57 * @return this
58 */
59 public EncapSecurityPayload setSecurityParamIndex(final int securityParamIndex) {
60 this.securityParamIndex = securityParamIndex;
61 return this;
62 }
63
64 /**
65 * Gets the sequence number of this header.
66 *
67 * @return the sequence number
68 */
69 public int getSequence() {
70 return this.sequence;
71 }
72
73 /**
74 * Sets the sequence number of this header.
75 *
76 * @param sequence the sequence number to set
77 * @return this
78 */
79 public EncapSecurityPayload setSequence(final int sequence) {
80 this.sequence = sequence;
81 return this;
82 }
83
84 @Override
85 public byte[] serialize() {
86 byte[] payloadData = null;
87 if (this.payload != null) {
88 this.payload.setParent(this);
89 payloadData = this.payload.serialize();
90 }
91
92 int payloadLength = 0;
93 if (payloadData != null) {
94 payloadLength = payloadData.length;
95 }
96
97 final byte[] data = new byte[HEADER_LENGTH + payloadLength];
98 final ByteBuffer bb = ByteBuffer.wrap(data);
99
100 bb.putInt(this.securityParamIndex);
101 bb.putInt(this.sequence);
102
103 if (payloadData != null) {
104 bb.put(payloadData);
105 }
106
107 if (this.parent != null && this.parent instanceof IExtensionHeader) {
108 ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ESP);
109 }
110 return data;
111 }
112
113 @Override
114 public IPacket deserialize(byte[] data, int offset, int length) {
115 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
116 this.securityParamIndex = bb.getInt();
117 this.sequence = bb.getInt();
118
119 this.payload = new Data();
120 this.payload.deserialize(data, bb.position(),
Jonathan Hart2a655752015-04-07 16:46:33 -0700121 bb.limit() - bb.position());
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800122 this.payload.setParent(this);
123
124 return this;
125 }
126
127 /*
128 * (non-Javadoc)
129 *
130 * @see java.lang.Object#hashCode()
131 */
132 @Override
133 public int hashCode() {
134 final int prime = 5807;
135 int result = super.hashCode();
136 result = prime * result + this.securityParamIndex;
137 result = prime * result + this.sequence;
138 return result;
139 }
140
141 /*
142 * (non-Javadoc)
143 *
144 * @see java.lang.Object#equals(java.lang.Object)
145 */
146 @Override
147 public boolean equals(final Object obj) {
148 if (this == obj) {
149 return true;
150 }
151 if (!super.equals(obj)) {
152 return false;
153 }
154 if (!(obj instanceof EncapSecurityPayload)) {
155 return false;
156 }
157 final EncapSecurityPayload other = (EncapSecurityPayload) obj;
158 if (this.securityParamIndex != other.securityParamIndex) {
159 return false;
160 }
161 if (this.sequence != other.sequence) {
162 return false;
163 }
164 return true;
165 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700166
167 /**
168 * Deserializer function for encapsulated security payload headers.
169 *
170 * @return deserializer function
171 */
172 public static Deserializer<EncapSecurityPayload> deserializer() {
173 return (data, offset, length) -> {
174 checkInput(data, offset, length, HEADER_LENGTH);
175
176 EncapSecurityPayload encapSecurityPayload = new EncapSecurityPayload();
177
178 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
179 encapSecurityPayload.securityParamIndex = bb.getInt();
180 encapSecurityPayload.sequence = bb.getInt();
181
182 encapSecurityPayload.payload = Data.deserializer().deserialize(
183 data, bb.position(), bb.limit() - bb.position());
184 encapSecurityPayload.payload.setParent(encapSecurityPayload);
185
186 return encapSecurityPayload;
187 };
188 }
Jian Li5fc14292015-12-04 11:30:46 -0800189
190 @Override
191 public String toString() {
192 return toStringHelper(getClass())
193 .add("securityParamIndex", Integer.toString(securityParamIndex))
194 .add("sequence", Integer.toString(sequence))
195 .toString();
196 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800197}