blob: a1e119550c69993b745ded07ee76688b7877036b [file] [log] [blame]
Charles M.C. Chan94f37372015-01-10 17:53:42 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Charles M.C. Chan94f37372015-01-10 17:53:42 +08003 *
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.IPv6;
Jonathan Hart2a655752015-04-07 16:46:33 -070023
Charles M.C. Chan94f37372015-01-10 17:53:42 +080024import java.nio.ByteBuffer;
25
Jian Li5fc14292015-12-04 11:30:46 -080026import static com.google.common.base.MoreObjects.toStringHelper;
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
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800112 /*
113 * (non-Javadoc)
114 *
115 * @see java.lang.Object#hashCode()
116 */
117 @Override
118 public int hashCode() {
119 final int prime = 5807;
120 int result = super.hashCode();
121 result = prime * result + this.securityParamIndex;
122 result = prime * result + this.sequence;
123 return result;
124 }
125
126 /*
127 * (non-Javadoc)
128 *
129 * @see java.lang.Object#equals(java.lang.Object)
130 */
131 @Override
132 public boolean equals(final Object obj) {
133 if (this == obj) {
134 return true;
135 }
136 if (!super.equals(obj)) {
137 return false;
138 }
139 if (!(obj instanceof EncapSecurityPayload)) {
140 return false;
141 }
142 final EncapSecurityPayload other = (EncapSecurityPayload) obj;
143 if (this.securityParamIndex != other.securityParamIndex) {
144 return false;
145 }
146 if (this.sequence != other.sequence) {
147 return false;
148 }
149 return true;
150 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700151
152 /**
153 * Deserializer function for encapsulated security payload headers.
154 *
155 * @return deserializer function
156 */
157 public static Deserializer<EncapSecurityPayload> deserializer() {
158 return (data, offset, length) -> {
159 checkInput(data, offset, length, HEADER_LENGTH);
160
161 EncapSecurityPayload encapSecurityPayload = new EncapSecurityPayload();
162
163 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
164 encapSecurityPayload.securityParamIndex = bb.getInt();
165 encapSecurityPayload.sequence = bb.getInt();
166
167 encapSecurityPayload.payload = Data.deserializer().deserialize(
168 data, bb.position(), bb.limit() - bb.position());
169 encapSecurityPayload.payload.setParent(encapSecurityPayload);
170
171 return encapSecurityPayload;
172 };
173 }
Jian Li5fc14292015-12-04 11:30:46 -0800174
175 @Override
176 public String toString() {
177 return toStringHelper(getClass())
178 .add("securityParamIndex", Integer.toString(securityParamIndex))
179 .add("sequence", Integer.toString(sequence))
180 .toString();
181 }
Charles M.C. Chan94f37372015-01-10 17:53:42 +0800182}