Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 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 | */ |
| 17 | |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 18 | package org.opencord.aaa; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 19 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 25 | |
| 26 | import org.opencord.olt.AccessDeviceService; |
| 27 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 29 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 30 | import com.google.common.collect.Maps; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * AAA Finite State Machine. |
| 36 | */ |
| 37 | |
| 38 | class StateMachine { |
| 39 | //INDEX to identify the state in the transition table |
| 40 | static final int STATE_IDLE = 0; |
| 41 | static final int STATE_STARTED = 1; |
| 42 | static final int STATE_PENDING = 2; |
| 43 | static final int STATE_AUTHORIZED = 3; |
| 44 | static final int STATE_UNAUTHORIZED = 4; |
| 45 | |
| 46 | //INDEX to identify the transition in the transition table |
| 47 | static final int TRANSITION_START = 0; // --> started |
| 48 | static final int TRANSITION_REQUEST_ACCESS = 1; |
| 49 | static final int TRANSITION_AUTHORIZE_ACCESS = 2; |
| 50 | static final int TRANSITION_DENY_ACCESS = 3; |
| 51 | static final int TRANSITION_LOGOFF = 4; |
| 52 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 53 | private static AccessDeviceService accessDeviceService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 54 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 55 | private static int identifier = -1; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 56 | private byte challengeIdentifier; |
| 57 | private byte[] challengeState; |
| 58 | private byte[] username; |
| 59 | private byte[] requestAuthenticator; |
| 60 | |
| 61 | // Supplicant connectivity info |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 62 | private ConnectPoint supplicantConnectpoint; |
| 63 | private MacAddress supplicantAddress; |
| 64 | private short vlanId; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 65 | private VlanId ctag; |
| 66 | private byte priorityCode; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 67 | |
| 68 | private String sessionId = null; |
| 69 | |
| 70 | private final Logger log = getLogger(getClass()); |
| 71 | |
| 72 | |
| 73 | private State[] states = { |
| 74 | new Idle(), new Started(), new Pending(), new Authorized(), new Unauthorized() |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | //State transition table |
| 79 | /* |
| 80 | |
| 81 | state IDLE | STARTED | PENDING | AUTHORIZED | UNAUTHORIZED |
| 82 | //// |
| 83 | input |
| 84 | ---------------------------------------------------------------------------------------------------- |
| 85 | |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 86 | START STARTED | _ | _ | STARTED | STARTED |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 87 | |
| 88 | REQUEST_ACCESS _ | PENDING | _ | _ | _ |
| 89 | |
| 90 | AUTHORIZE_ACCESS _ | _ | AUTHORIZED | _ | _ |
| 91 | |
| 92 | DENY_ACCESS _ | - | UNAUTHORIZED | _ | _ |
| 93 | |
| 94 | LOGOFF _ | _ | _ | IDLE | IDLE |
| 95 | */ |
| 96 | |
| 97 | private int[] idleTransition = |
| 98 | {STATE_STARTED, STATE_IDLE, STATE_IDLE, STATE_IDLE, STATE_IDLE}; |
| 99 | private int[] startedTransition = |
| 100 | {STATE_STARTED, STATE_PENDING, STATE_STARTED, STATE_STARTED, STATE_STARTED}; |
| 101 | private int[] pendingTransition = |
| 102 | {STATE_PENDING, STATE_PENDING, STATE_AUTHORIZED, STATE_UNAUTHORIZED, STATE_PENDING}; |
| 103 | private int[] authorizedTransition = |
Qianqian Hu | 0c34981 | 2016-02-15 17:25:22 +0800 | [diff] [blame] | 104 | {STATE_STARTED, STATE_AUTHORIZED, STATE_AUTHORIZED, STATE_AUTHORIZED, STATE_IDLE}; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 105 | private int[] unauthorizedTransition = |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 106 | {STATE_STARTED, STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_IDLE}; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 107 | |
| 108 | //THE TRANSITION TABLE |
| 109 | private int[][] transition = |
| 110 | {idleTransition, startedTransition, pendingTransition, authorizedTransition, |
| 111 | unauthorizedTransition}; |
| 112 | |
| 113 | private int currentState = STATE_IDLE; |
| 114 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 115 | // Maps of state machines. Each state machine is represented by an |
| 116 | // unique identifier on the switch: dpid + port number |
| 117 | private static Map<String, StateMachine> sessionIdMap; |
| 118 | private static Map<Integer, StateMachine> identifierMap; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 119 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 120 | public static void initializeMaps() { |
| 121 | sessionIdMap = Maps.newConcurrentMap(); |
| 122 | identifierMap = Maps.newConcurrentMap(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 123 | identifier = -1; |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public static void destroyMaps() { |
| 127 | sessionIdMap = null; |
| 128 | identifierMap = null; |
| 129 | } |
| 130 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 131 | public static void setAccessDeviceService(AccessDeviceService service) { |
| 132 | accessDeviceService = service; |
| 133 | } |
| 134 | |
Qianqian Hu | 61a6a40 | 2016-02-16 15:18:05 +0800 | [diff] [blame] | 135 | public static Map<String, StateMachine> sessionIdMap() { |
| 136 | return sessionIdMap; |
| 137 | } |
| 138 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 139 | public static StateMachine lookupStateMachineById(byte identifier) { |
| 140 | return identifierMap.get((int) identifier); |
| 141 | } |
| 142 | |
| 143 | public static StateMachine lookupStateMachineBySessionId(String sessionId) { |
| 144 | return sessionIdMap.get(sessionId); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public static void deleteStateMachineMapping(StateMachine machine) { |
| 148 | identifierMap.entrySet().removeIf(e -> e.getValue().equals(machine)); |
| 149 | } |
| 150 | |
| 151 | /** |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 152 | * Deletes authentication state machine records for a given MAC address. |
| 153 | * @param mac mac address of the suppliant who's state machine should be removed |
| 154 | */ |
| 155 | public static void deleteByMac(MacAddress mac) { |
| 156 | |
| 157 | // Walk the map from session IDs to state machines looking for a MAC match |
| 158 | for (Map.Entry<String, StateMachine> e : sessionIdMap.entrySet()) { |
| 159 | |
| 160 | // If a MAC match is found then delete the entry from the session ID |
| 161 | // and identifier map as well as call delete identifier to clean up |
| 162 | // the identifier bit set. |
| 163 | if (e.getValue() != null && e.getValue().supplicantAddress != null && |
| 164 | e.getValue().supplicantAddress.equals(mac)) { |
| 165 | sessionIdMap.remove(e.getValue().sessionId); |
| 166 | if (e.getValue().identifier != -1) { |
| 167 | deleteStateMachineMapping(e.getValue()); |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 175 | * State Machine Constructor. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 176 | * |
| 177 | * @param sessionId session Id represented by the switch dpid + port number |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 178 | * @param ctag C-TAG for this subscriber |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 179 | */ |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 180 | public StateMachine(String sessionId, VlanId ctag) { |
| 181 | log.info("Creating a new state machine for {} C-TAG {}", sessionId, |
| 182 | ctag); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 183 | this.sessionId = sessionId; |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 184 | sessionIdMap.put(sessionId, this); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 185 | this.ctag = ctag; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 189 | * Gets the connect point for the supplicant side. |
| 190 | * |
| 191 | * @return supplicant connect point |
| 192 | */ |
| 193 | public ConnectPoint supplicantConnectpoint() { |
| 194 | return supplicantConnectpoint; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sets the supplicant side connect point. |
| 199 | * |
| 200 | * @param supplicantConnectpoint supplicant select point. |
| 201 | */ |
| 202 | public void setSupplicantConnectpoint(ConnectPoint supplicantConnectpoint) { |
| 203 | this.supplicantConnectpoint = supplicantConnectpoint; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Gets the MAC address of the supplicant. |
| 208 | * |
| 209 | * @return supplicant MAC address |
| 210 | */ |
| 211 | public MacAddress supplicantAddress() { |
| 212 | return supplicantAddress; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Sets the supplicant MAC address. |
| 217 | * |
| 218 | * @param supplicantAddress new supplicant MAC address |
| 219 | */ |
| 220 | public void setSupplicantAddress(MacAddress supplicantAddress) { |
| 221 | this.supplicantAddress = supplicantAddress; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Gets the client's Vlan ID. |
| 226 | * |
| 227 | * @return client vlan ID |
| 228 | */ |
| 229 | public short vlanId() { |
| 230 | return vlanId; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Sets the client's vlan ID. |
| 235 | * |
| 236 | * @param vlanId new client vlan ID |
| 237 | */ |
| 238 | public void setVlanId(short vlanId) { |
| 239 | this.vlanId = vlanId; |
| 240 | } |
| 241 | |
| 242 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 243 | * Gets the client's priority Code. |
| 244 | * |
| 245 | * @return client Priority code |
| 246 | */ |
| 247 | public byte priorityCode() { |
| 248 | return priorityCode; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Sets the client's priority Code. |
| 253 | * |
| 254 | * @param priorityCode new client priority Code |
| 255 | */ |
| 256 | public void setPriorityCode(byte priorityCode) { |
| 257 | this.priorityCode = priorityCode; |
| 258 | } |
| 259 | |
| 260 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 261 | * Gets the client id that is requesting for access. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 262 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 263 | * @return The client id. |
| 264 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 265 | public String sessionId() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 266 | return this.sessionId; |
| 267 | } |
| 268 | |
| 269 | /** |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 270 | * Set the challenge identifier and the state issued by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 271 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 272 | * @param challengeIdentifier The challenge identifier set into the EAP packet from the RADIUS message. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 273 | * @param challengeState The challenge state from the RADIUS. |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 274 | */ |
| 275 | protected void setChallengeInfo(byte challengeIdentifier, byte[] challengeState) { |
| 276 | this.challengeIdentifier = challengeIdentifier; |
| 277 | this.challengeState = challengeState; |
| 278 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 279 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Set the challenge identifier issued by the RADIUS on the access challenge request. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 282 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 283 | * @param challengeIdentifier The challenge identifier set into the EAP packet from the RADIUS message. |
| 284 | */ |
| 285 | protected void setChallengeIdentifier(byte challengeIdentifier) { |
| 286 | log.info("Set Challenge Identifier to {}", challengeIdentifier); |
| 287 | this.challengeIdentifier = challengeIdentifier; |
| 288 | } |
| 289 | |
| 290 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 291 | * Gets the challenge EAP identifier set by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 292 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 293 | * @return The challenge EAP identifier. |
| 294 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 295 | protected byte challengeIdentifier() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 296 | return this.challengeIdentifier; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * Set the challenge state info issued by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 302 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 303 | * @param challengeState The challenge state from the RADIUS. |
| 304 | */ |
| 305 | protected void setChallengeState(byte[] challengeState) { |
| 306 | log.info("Set Challenge State"); |
| 307 | this.challengeState = challengeState; |
| 308 | } |
| 309 | |
| 310 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 311 | * Gets the challenge state set by the RADIUS. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 312 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 313 | * @return The challenge state. |
| 314 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 315 | protected byte[] challengeState() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 316 | return this.challengeState; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Set the username. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 321 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 322 | * @param username The username sent to the RADIUS upon access request. |
| 323 | */ |
| 324 | protected void setUsername(byte[] username) { |
| 325 | this.username = username; |
| 326 | } |
| 327 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 328 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 329 | * Gets the username. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 330 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 331 | * @return The requestAuthenticator. |
| 332 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 333 | protected byte[] requestAuthenticator() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 334 | return this.requestAuthenticator; |
| 335 | } |
| 336 | |
| 337 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 338 | * Sets the authenticator. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 339 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 340 | * @param authenticator The username sent to the RADIUS upon access request. |
| 341 | */ |
| 342 | protected void setRequestAuthenticator(byte[] authenticator) { |
| 343 | this.requestAuthenticator = authenticator; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 348 | * Gets the username. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 349 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 350 | * @return The username. |
| 351 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 352 | protected byte[] username() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 353 | return this.username; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Return the identifier of the state machine. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 358 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 359 | * @return The state machine identifier. |
| 360 | */ |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 361 | public synchronized byte identifier() { |
| 362 | identifier = (identifier + 1) % 255; |
| 363 | identifierMap.put(identifier, this); |
| 364 | return (byte) identifier; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 367 | /** |
| 368 | * Move to the next state. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 369 | * |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 370 | * @param msg message |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 371 | */ |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 372 | private void next(int msg) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 373 | currentState = transition[currentState][msg]; |
| 374 | log.info("Current State " + currentState); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Client has requested the start action to allow network access. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 379 | * |
| 380 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 381 | */ |
| 382 | public void start() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 383 | states[currentState].start(); |
| 384 | //move to the next state |
| 385 | next(TRANSITION_START); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 386 | identifier = this.identifier(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
| 390 | * An Identification information has been sent by the supplicant. |
| 391 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 392 | * |
| 393 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 394 | */ |
| 395 | public void requestAccess() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 396 | states[currentState].requestAccess(); |
| 397 | //move to the next state |
| 398 | next(TRANSITION_REQUEST_ACCESS); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
| 402 | * RADIUS has accepted the identification. |
| 403 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 404 | * |
| 405 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 406 | */ |
| 407 | public void authorizeAccess() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 408 | states[currentState].radiusAccepted(); |
| 409 | //move to the next state |
| 410 | next(TRANSITION_AUTHORIZE_ACCESS); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 411 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 412 | if (accessDeviceService != null) { |
| 413 | log.info("Provisioning subscriber at {} with C-TAG {}", |
| 414 | supplicantConnectpoint(), ctag); |
| 415 | accessDeviceService.provisionSubscriber(supplicantConnectpoint(), |
| 416 | ctag); |
| 417 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 418 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 419 | // Clear mapping |
| 420 | deleteStateMachineMapping(this); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /** |
| 424 | * RADIUS has denied the identification. |
| 425 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 426 | * |
| 427 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 428 | */ |
| 429 | public void denyAccess() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 430 | states[currentState].radiusDenied(); |
| 431 | //move to the next state |
| 432 | next(TRANSITION_DENY_ACCESS); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 433 | // Clear mappings |
| 434 | deleteStateMachineMapping(this); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Logoff request has been requested. |
| 439 | * Move to the next state if possible. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 440 | * |
| 441 | * @throws StateMachineException if authentication protocol is violated |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 442 | */ |
| 443 | public void logoff() throws StateMachineException { |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 444 | states[currentState].logoff(); |
| 445 | //move to the next state |
| 446 | next(TRANSITION_LOGOFF); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 450 | * Gets the current state. |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 451 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 452 | * @return The current state. Could be STATE_IDLE, STATE_STARTED, STATE_PENDING, STATE_AUTHORIZED, |
| 453 | * STATE_UNAUTHORIZED. |
| 454 | */ |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 455 | public int state() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 456 | return currentState; |
| 457 | } |
| 458 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 459 | @Override |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 460 | public String toString() { |
| 461 | return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" + |
| 462 | ("state: " + this.currentState); |
| 463 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 464 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 465 | abstract class State { |
| 466 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 467 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 468 | private String name = "State"; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 469 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 470 | public void start() throws StateMachineInvalidTransitionException { |
| 471 | log.warn("START transition from this state is not allowed."); |
| 472 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 473 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 474 | public void requestAccess() throws StateMachineInvalidTransitionException { |
| 475 | log.warn("REQUEST ACCESS transition from this state is not allowed."); |
| 476 | } |
| 477 | |
| 478 | public void radiusAccepted() throws StateMachineInvalidTransitionException { |
| 479 | log.warn("AUTHORIZE ACCESS transition from this state is not allowed."); |
| 480 | } |
| 481 | |
| 482 | public void radiusDenied() throws StateMachineInvalidTransitionException { |
| 483 | log.warn("DENY ACCESS transition from this state is not allowed."); |
| 484 | } |
| 485 | |
| 486 | public void logoff() throws StateMachineInvalidTransitionException { |
| 487 | log.warn("LOGOFF transition from this state is not allowed."); |
| 488 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 489 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 490 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Idle state: supplicant is logged of from the network. |
| 493 | */ |
| 494 | class Idle extends State { |
| 495 | private final Logger log = getLogger(getClass()); |
| 496 | private String name = "IDLE_STATE"; |
| 497 | |
| 498 | public void start() { |
| 499 | log.info("Moving from IDLE state to STARTED state."); |
| 500 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 501 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 502 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 503 | /** |
| 504 | * Started state: supplicant has entered the network and informed the authenticator. |
| 505 | */ |
| 506 | class Started extends State { |
| 507 | private final Logger log = getLogger(getClass()); |
| 508 | private String name = "STARTED_STATE"; |
| 509 | |
| 510 | public void requestAccess() { |
| 511 | log.info("Moving from STARTED state to PENDING state."); |
| 512 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 513 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 514 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 515 | /** |
| 516 | * Pending state: supplicant has been identified by the authenticator but has not access yet. |
| 517 | */ |
| 518 | class Pending extends State { |
| 519 | private final Logger log = getLogger(getClass()); |
| 520 | private String name = "PENDING_STATE"; |
| 521 | |
| 522 | public void radiusAccepted() { |
| 523 | log.info("Moving from PENDING state to AUTHORIZED state."); |
| 524 | } |
| 525 | |
| 526 | public void radiusDenied() { |
| 527 | log.info("Moving from PENDING state to UNAUTHORIZED state."); |
| 528 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 529 | } |
Thomas Vachuska | e989420 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 530 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 531 | /** |
| 532 | * Authorized state: supplicant port has been accepted, access is granted. |
| 533 | */ |
| 534 | class Authorized extends State { |
| 535 | private final Logger log = getLogger(getClass()); |
| 536 | private String name = "AUTHORIZED_STATE"; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 537 | |
Qianqian Hu | 0c34981 | 2016-02-15 17:25:22 +0800 | [diff] [blame] | 538 | public void start() { |
| 539 | log.info("Moving from AUTHORIZED state to STARTED state."); |
| 540 | } |
| 541 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 542 | public void logoff() { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 543 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 544 | log.info("Moving from AUTHORIZED state to IDLE state."); |
| 545 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 548 | /** |
| 549 | * Unauthorized state: supplicant port has been rejected, access is denied. |
| 550 | */ |
| 551 | class Unauthorized extends State { |
| 552 | private final Logger log = getLogger(getClass()); |
| 553 | private String name = "UNAUTHORIZED_STATE"; |
| 554 | |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 555 | public void start() { |
| 556 | log.info("Moving from UNAUTHORIZED state to STARTED state."); |
| 557 | } |
| 558 | |
Ray Milkey | 78e95a4 | 2015-09-24 08:36:45 -0700 | [diff] [blame] | 559 | public void logoff() { |
| 560 | log.info("Moving from UNAUTHORIZED state to IDLE state."); |
| 561 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 562 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 563 | |
| 564 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 565 | } |