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