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