Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Laboratory |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package org.onlab.packet; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.LinkedHashMap; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * EAPOL MKA (EAPOL MAC Key Agreement Protocol) header. |
| 27 | */ |
| 28 | public class EAPOLMkpdu extends BasePacket { |
| 29 | |
| 30 | // Parameter Sets. |
Ray Milkey | fe6afd8 | 2018-11-26 14:03:20 -0800 | [diff] [blame] | 31 | private Map<Byte, IPacket> parameterSets = new LinkedHashMap<>(); |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Parameter Serialization Order. |
| 35 | * IEEE 802.1x Clause 11.11.3. |
| 36 | */ |
Ray Milkey | fe6afd8 | 2018-11-26 14:03:20 -0800 | [diff] [blame] | 37 | private static byte[] parametersetSerializerKeyList = new byte[]{ |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 38 | EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC, |
| 39 | EAPOLMkpduParameterSet.PARAMETERSET_TYPE_LIVE_PEER_LIST, |
| 40 | EAPOLMkpduParameterSet.PARAMETERSET_TYPE_POTENTIAL_PEER_LIST, |
| 41 | EAPOLMkpduParameterSet.PARAMETERSET_TYPE_MACSEC_SAK_USE, |
| 42 | EAPOLMkpduParameterSet.PARAMETERSET_TYPE_DISTRIBUTED_SAK, |
| 43 | // TODO: Fill other types. |
| 44 | EAPOLMkpduParameterSet.PARAMETERSET_TYPE_ICV_INDICATOR |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | // Various Parameter Set Deserializers. |
| 49 | |
Ray Milkey | fe6afd8 | 2018-11-26 14:03:20 -0800 | [diff] [blame] | 50 | private static final Map<Byte, Deserializer<? extends IPacket>> PARAMETERSET_DESERIALIZER_MAP = |
Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 51 | new LinkedHashMap<>(); |
| 52 | |
| 53 | static { |
| 54 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC, |
| 55 | EAPOLMkpduBasicParameterSet.deserializer()); |
| 56 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_LIVE_PEER_LIST, |
| 57 | EAPOLMkpduPeerListParameterSet.deserializer()); |
| 58 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_POTENTIAL_PEER_LIST, |
| 59 | EAPOLMkpduPeerListParameterSet.deserializer()); |
| 60 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_MACSEC_SAK_USE, |
| 61 | EAPOLMkpduMACSecUseParameterSet.deserializer()); |
| 62 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_DISTRIBUTED_SAK, |
| 63 | EAPOLMkpduDistributedSAKParameterSet.deserializer()); |
| 64 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_ICV_INDICATOR, |
| 65 | EAPOLMkpduICVIndicatorParameterSet.deserializer()); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public byte[] serialize() { |
| 70 | int payloadLength = packetLength(); |
| 71 | ByteBuffer payload = ByteBuffer.wrap(new byte[payloadLength]); |
| 72 | |
| 73 | |
| 74 | //Serialize Parameter Sets. |
| 75 | for (byte b : parametersetSerializerKeyList) { |
| 76 | IPacket packet = parameterSets.get(b); |
| 77 | if (packet != null) { |
| 78 | byte[] data = packet.serialize(); |
| 79 | if (data != null) { |
| 80 | payload.put(data); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return payload.array(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Static deserializer for EAPOL-MKA packets. |
| 89 | * |
| 90 | * @return deserializer function |
| 91 | */ |
| 92 | public static Deserializer<EAPOLMkpdu> deserializer() { |
| 93 | return (data, offset, length) -> { |
| 94 | byte parameterSetType; |
| 95 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 96 | EAPOLMkpdu mkpdu = new EAPOLMkpdu(); |
| 97 | |
| 98 | /* Extract Basic ParameterSet; |
| 99 | Special care needed, MKA Version & Peer Type difficult to distinguish. */ |
| 100 | Deserializer<? extends IPacket> psDeserializer = |
| 101 | EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.get(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC); |
| 102 | EAPOLMkpduParameterSet ps = (EAPOLMkpduParameterSet) (psDeserializer.deserialize(bb.array(), |
| 103 | bb.position(), bb.remaining())); |
| 104 | if (!mkpdu.addParameterSet(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC, ps)) { |
| 105 | throw new DeserializationException("Error in deserializing packets"); |
| 106 | } |
| 107 | // Update buffer position. |
| 108 | bb.position(bb.position() + ps.getTotalLength()); |
| 109 | |
| 110 | // Extract various remaining Parameter Sets. |
| 111 | while (bb.hasRemaining()) { |
| 112 | parameterSetType = bb.get(); |
| 113 | psDeserializer = EAPOLMkpdu.PARAMETERSET_DESERIALIZER_MAP.get(parameterSetType); |
| 114 | ps = (EAPOLMkpduParameterSet) (psDeserializer.deserialize(bb.array(), bb.position(), |
| 115 | bb.remaining())); |
| 116 | // Specially handle Peer List Parameter Sets . |
| 117 | if ((parameterSetType == EAPOLMkpduParameterSet.PARAMETERSET_TYPE_LIVE_PEER_LIST) || |
| 118 | (parameterSetType == EAPOLMkpduParameterSet.PARAMETERSET_TYPE_POTENTIAL_PEER_LIST)) { |
| 119 | EAPOLMkpduPeerListParameterSet peerList = |
| 120 | (EAPOLMkpduPeerListParameterSet) ps; |
| 121 | peerList.setPeerListType(parameterSetType); |
| 122 | } |
| 123 | if (!mkpdu.addParameterSet(parameterSetType, ps)) { |
| 124 | throw new DeserializationException("Error in deserializing packets"); |
| 125 | } |
| 126 | |
| 127 | // Update buffer. |
| 128 | short consumed = ps.getTotalLength(); |
| 129 | short remaining = (short) bb.remaining(); |
| 130 | // Already one byte shifted, only "consumed-1" is to be shifted. |
| 131 | bb.position(bb.position() + ((remaining > consumed) ? (consumed - 1) : remaining)); |
| 132 | } |
| 133 | return mkpdu; |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Populate various Parameter Sets to store. |
| 139 | * |
| 140 | * @param type short |
| 141 | * @param ps EAPOLMkpduParameterSet |
| 142 | * @return boolean |
| 143 | */ |
| 144 | public boolean addParameterSet(short type, EAPOLMkpduParameterSet ps) { |
| 145 | if (ps == null) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // Ensure type is valid. |
| 150 | if (!((EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC <= type && |
| 151 | type <= EAPOLMkpduParameterSet.PARAMETERSET_TYPE_DISTRIBUTED_SAK) || |
| 152 | (type == EAPOLMkpduParameterSet.PARAMETERSET_TYPE_ICV_INDICATOR))) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | // Update store. |
| 158 | parameterSets.put((byte) type, (IPacket) ps); |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Provide Basic Parameter Set details. |
| 165 | * |
| 166 | * @return EAPOLMkpduBasicParameterSet |
| 167 | */ |
| 168 | public EAPOLMkpduBasicParameterSet getBasicParameterSet() { |
| 169 | IPacket parameterSet = null; |
| 170 | if (parameterSets.containsKey(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC)) { |
| 171 | parameterSet = parameterSets.get(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_BASIC); |
| 172 | } |
| 173 | return (EAPOLMkpduBasicParameterSet) parameterSet; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Provide Live/Potential Peer List details. |
| 178 | * |
| 179 | * @return EAPOLMkpduPeerListParameterSet |
| 180 | */ |
| 181 | public EAPOLMkpduPeerListParameterSet getPeerListParameterSet() { |
| 182 | IPacket parameterSet; |
| 183 | if (parameterSets.containsKey(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_LIVE_PEER_LIST)) { |
| 184 | parameterSet = parameterSets.get(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_LIVE_PEER_LIST); |
| 185 | } else { |
| 186 | parameterSet = parameterSets.get(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_POTENTIAL_PEER_LIST); |
| 187 | } |
| 188 | return (EAPOLMkpduPeerListParameterSet) parameterSet; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Total EAPOL-MKPDU packet length. Cumulative length of Parameter Sets. |
| 193 | * |
| 194 | * @return length |
| 195 | */ |
| 196 | public short packetLength() { |
| 197 | short length = 0; |
| 198 | for (byte k : parameterSets.keySet()) { |
| 199 | EAPOLMkpduParameterSet p = (EAPOLMkpduParameterSet) parameterSets.get(k); |
| 200 | length += p.getTotalLength(); |
| 201 | } |
| 202 | return length; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Retrieve Parameter Set based on type. |
| 207 | * |
| 208 | * @param type byte |
| 209 | * @return EAPOLMkpduParameterSet |
| 210 | */ |
| 211 | public EAPOLMkpduParameterSet getParameterSet(byte type) { |
| 212 | EAPOLMkpduParameterSet ps = null; |
| 213 | Map.Entry<Byte, IPacket> entry = parameterSets.entrySet().stream() |
| 214 | .filter((i) -> { |
| 215 | return i.getKey().equals(new Byte(type)); |
| 216 | }) |
| 217 | .findFirst() |
| 218 | .orElse(null); |
| 219 | if (entry != null) { |
| 220 | ps = (EAPOLMkpduParameterSet) entry.getValue(); |
| 221 | } |
| 222 | return ps; |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | |