StateMachine.java
15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
*
* Copyright 2015 AT&T Foundry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.aaa;
import java.util.BitSet;
import java.util.Map;
import org.onlab.packet.MacAddress;
import org.onosproject.net.ConnectPoint;
import org.slf4j.Logger;
import com.google.common.collect.Maps;
import static org.slf4j.LoggerFactory.getLogger;
/**
* AAA Finite State Machine.
*/
class StateMachine {
//INDEX to identify the state in the transition table
static final int STATE_IDLE = 0;
static final int STATE_STARTED = 1;
static final int STATE_PENDING = 2;
static final int STATE_AUTHORIZED = 3;
static final int STATE_UNAUTHORIZED = 4;
//INDEX to identify the transition in the transition table
static final int TRANSITION_START = 0; // --> started
static final int TRANSITION_REQUEST_ACCESS = 1;
static final int TRANSITION_AUTHORIZE_ACCESS = 2;
static final int TRANSITION_DENY_ACCESS = 3;
static final int TRANSITION_LOGOFF = 4;
//map of access identifiers (issued at EAPOL START)
static BitSet bitSet = new BitSet();
private int identifier = -1;
private byte challengeIdentifier;
private byte[] challengeState;
private byte[] username;
private byte[] requestAuthenticator;
// Supplicant connectivity info
private ConnectPoint supplicantConnectpoint;
private MacAddress supplicantAddress;
private short vlanId;
private String sessionId = null;
private final Logger log = getLogger(getClass());
private State[] states = {
new Idle(), new Started(), new Pending(), new Authorized(), new Unauthorized()
};
//State transition table
/*
state IDLE | STARTED | PENDING | AUTHORIZED | UNAUTHORIZED
////
input
----------------------------------------------------------------------------------------------------
START STARTED | _ | _ | STARTED | _
REQUEST_ACCESS _ | PENDING | _ | _ | _
AUTHORIZE_ACCESS _ | _ | AUTHORIZED | _ | _
DENY_ACCESS _ | - | UNAUTHORIZED | _ | _
LOGOFF _ | _ | _ | IDLE | IDLE
*/
private int[] idleTransition =
{STATE_STARTED, STATE_IDLE, STATE_IDLE, STATE_IDLE, STATE_IDLE};
private int[] startedTransition =
{STATE_STARTED, STATE_PENDING, STATE_STARTED, STATE_STARTED, STATE_STARTED};
private int[] pendingTransition =
{STATE_PENDING, STATE_PENDING, STATE_AUTHORIZED, STATE_UNAUTHORIZED, STATE_PENDING};
private int[] authorizedTransition =
{STATE_STARTED, STATE_AUTHORIZED, STATE_AUTHORIZED, STATE_AUTHORIZED, STATE_IDLE};
private int[] unauthorizedTransition =
{STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_UNAUTHORIZED, STATE_IDLE};
//THE TRANSITION TABLE
private int[][] transition =
{idleTransition, startedTransition, pendingTransition, authorizedTransition,
unauthorizedTransition};
private int currentState = STATE_IDLE;
// Maps of state machines. Each state machine is represented by an
// unique identifier on the switch: dpid + port number
private static Map<String, StateMachine> sessionIdMap;
private static Map<Integer, StateMachine> identifierMap;
public static void initializeMaps() {
sessionIdMap = Maps.newConcurrentMap();
identifierMap = Maps.newConcurrentMap();
}
public static void destroyMaps() {
sessionIdMap = null;
identifierMap = null;
}
public static Map<String, StateMachine> sessionIdMap() {
return sessionIdMap;
}
public static StateMachine lookupStateMachineById(byte identifier) {
return identifierMap.get((int) identifier);
}
public static StateMachine lookupStateMachineBySessionId(String sessionId) {
return sessionIdMap.get(sessionId);
} /**
* State Machine Constructor.
*
* @param sessionId session Id represented by the switch dpid + port number
*/
public StateMachine(String sessionId) {
log.info("Creating a new state machine for {}", sessionId);
this.sessionId = sessionId;
sessionIdMap.put(sessionId, this);
}
/**
* Gets the connect point for the supplicant side.
*
* @return supplicant connect point
*/
public ConnectPoint supplicantConnectpoint() {
return supplicantConnectpoint;
}
/**
* Sets the supplicant side connect point.
*
* @param supplicantConnectpoint supplicant select point.
*/
public void setSupplicantConnectpoint(ConnectPoint supplicantConnectpoint) {
this.supplicantConnectpoint = supplicantConnectpoint;
}
/**
* Gets the MAC address of the supplicant.
*
* @return supplicant MAC address
*/
public MacAddress supplicantAddress() {
return supplicantAddress;
}
/**
* Sets the supplicant MAC address.
*
* @param supplicantAddress new supplicant MAC address
*/
public void setSupplicantAddress(MacAddress supplicantAddress) {
this.supplicantAddress = supplicantAddress;
}
/**
* Gets the client's Vlan ID.
*
* @return client vlan ID
*/
public short vlanId() {
return vlanId;
}
/**
* Sets the client's vlan ID.
*
* @param vlanId new client vlan ID
*/
public void setVlanId(short vlanId) {
this.vlanId = vlanId;
}
/**
* Gets the client id that is requesting for access.
*
* @return The client id.
*/
public String sessionId() {
return this.sessionId;
}
/**
* Create the identifier for the state machine (happens when goes to STARTED state).
*/
private void createIdentifier() throws StateMachineException {
log.debug("Creating Identifier.");
int index;
try {
//find the first available spot for identifier assignment
index = StateMachine.bitSet.nextClearBit(0);
//there is a limit of 256 identifiers
if (index == 256) {
throw new StateMachineException("Cannot handle any new identifier. Limit is 256.");
}
} catch (IndexOutOfBoundsException e) {
throw new StateMachineException(e.getMessage());
}
log.info("Assigning identifier {}", index);
StateMachine.bitSet.set(index);
this.identifier = index;
}
/**
* Set the challenge identifier and the state issued by the RADIUS.
*
* @param challengeIdentifier The challenge identifier set into the EAP packet from the RADIUS message.
* @param challengeState The challenge state from the RADIUS.
*/
protected void setChallengeInfo(byte challengeIdentifier, byte[] challengeState) {
this.challengeIdentifier = challengeIdentifier;
this.challengeState = challengeState;
}
/**
* Set the challenge identifier issued by the RADIUS on the access challenge request.
*
* @param challengeIdentifier The challenge identifier set into the EAP packet from the RADIUS message.
*/
protected void setChallengeIdentifier(byte challengeIdentifier) {
log.info("Set Challenge Identifier to {}", challengeIdentifier);
this.challengeIdentifier = challengeIdentifier;
}
/**
* Gets the challenge EAP identifier set by the RADIUS.
*
* @return The challenge EAP identifier.
*/
protected byte challengeIdentifier() {
return this.challengeIdentifier;
}
/**
* Set the challenge state info issued by the RADIUS.
*
* @param challengeState The challenge state from the RADIUS.
*/
protected void setChallengeState(byte[] challengeState) {
log.info("Set Challenge State");
this.challengeState = challengeState;
}
/**
* Gets the challenge state set by the RADIUS.
*
* @return The challenge state.
*/
protected byte[] challengeState() {
return this.challengeState;
}
/**
* Set the username.
*
* @param username The username sent to the RADIUS upon access request.
*/
protected void setUsername(byte[] username) {
this.username = username;
}
/**
* Gets the username.
*
* @return The requestAuthenticator.
*/
protected byte[] requestAuthenticator() {
return this.requestAuthenticator;
}
/**
* Sets the authenticator.
*
* @param authenticator The username sent to the RADIUS upon access request.
*/
protected void setRequestAuthenticator(byte[] authenticator) {
this.requestAuthenticator = authenticator;
}
/**
* Gets the username.
*
* @return The username.
*/
protected byte[] username() {
return this.username;
}
/**
* Return the identifier of the state machine.
*
* @return The state machine identifier.
*/
public byte identifier() {
return (byte) this.identifier;
}
protected void deleteIdentifier() {
if (this.identifier != -1) {
log.info("Freeing up " + this.identifier);
//this state machine should be deleted and free up the identifier
StateMachine.bitSet.clear(this.identifier);
this.identifier = -1;
}
}
/**
* Move to the next state.
*
* @param msg message
*/
private void next(int msg) {
currentState = transition[currentState][msg];
log.info("Current State " + currentState);
}
/**
* Client has requested the start action to allow network access.
*
* @throws StateMachineException if authentication protocol is violated
*/
public void start() throws StateMachineException {
states[currentState].start();
//move to the next state
next(TRANSITION_START);
createIdentifier();
identifierMap.put(identifier, this);
}
/**
* An Identification information has been sent by the supplicant.
* Move to the next state if possible.
*
* @throws StateMachineException if authentication protocol is violated
*/
public void requestAccess() throws StateMachineException {
states[currentState].requestAccess();
//move to the next state
next(TRANSITION_REQUEST_ACCESS);
}
/**
* RADIUS has accepted the identification.
* Move to the next state if possible.
*
* @throws StateMachineException if authentication protocol is violated
*/
public void authorizeAccess() throws StateMachineException {
states[currentState].radiusAccepted();
//move to the next state
next(TRANSITION_AUTHORIZE_ACCESS);
// TODO: put in calls to launch vSG here
deleteIdentifier();
}
/**
* RADIUS has denied the identification.
* Move to the next state if possible.
*
* @throws StateMachineException if authentication protocol is violated
*/
public void denyAccess() throws StateMachineException {
states[currentState].radiusDenied();
//move to the next state
next(TRANSITION_DENY_ACCESS);
deleteIdentifier();
}
/**
* Logoff request has been requested.
* Move to the next state if possible.
*
* @throws StateMachineException if authentication protocol is violated
*/
public void logoff() throws StateMachineException {
states[currentState].logoff();
//move to the next state
next(TRANSITION_LOGOFF);
}
/**
* Gets the current state.
*
* @return The current state. Could be STATE_IDLE, STATE_STARTED, STATE_PENDING, STATE_AUTHORIZED,
* STATE_UNAUTHORIZED.
*/
public int state() {
return currentState;
}
@Override
public String toString() {
return ("sessionId: " + this.sessionId) + "\t" + ("identifier: " + this.identifier) + "\t" +
("state: " + this.currentState);
}
abstract class State {
private final Logger log = getLogger(getClass());
private String name = "State";
public void start() throws StateMachineInvalidTransitionException {
log.warn("START transition from this state is not allowed.");
}
public void requestAccess() throws StateMachineInvalidTransitionException {
log.warn("REQUEST ACCESS transition from this state is not allowed.");
}
public void radiusAccepted() throws StateMachineInvalidTransitionException {
log.warn("AUTHORIZE ACCESS transition from this state is not allowed.");
}
public void radiusDenied() throws StateMachineInvalidTransitionException {
log.warn("DENY ACCESS transition from this state is not allowed.");
}
public void logoff() throws StateMachineInvalidTransitionException {
log.warn("LOGOFF transition from this state is not allowed.");
}
}
/**
* Idle state: supplicant is logged of from the network.
*/
class Idle extends State {
private final Logger log = getLogger(getClass());
private String name = "IDLE_STATE";
public void start() {
log.info("Moving from IDLE state to STARTED state.");
}
}
/**
* Started state: supplicant has entered the network and informed the authenticator.
*/
class Started extends State {
private final Logger log = getLogger(getClass());
private String name = "STARTED_STATE";
public void requestAccess() {
log.info("Moving from STARTED state to PENDING state.");
}
}
/**
* Pending state: supplicant has been identified by the authenticator but has not access yet.
*/
class Pending extends State {
private final Logger log = getLogger(getClass());
private String name = "PENDING_STATE";
public void radiusAccepted() {
log.info("Moving from PENDING state to AUTHORIZED state.");
}
public void radiusDenied() {
log.info("Moving from PENDING state to UNAUTHORIZED state.");
}
}
/**
* Authorized state: supplicant port has been accepted, access is granted.
*/
class Authorized extends State {
private final Logger log = getLogger(getClass());
private String name = "AUTHORIZED_STATE";
public void start() {
log.info("Moving from AUTHORIZED state to STARTED state.");
}
public void logoff() {
log.info("Moving from AUTHORIZED state to IDLE state.");
}
}
/**
* Unauthorized state: supplicant port has been rejected, access is denied.
*/
class Unauthorized extends State {
private final Logger log = getLogger(getClass());
private String name = "UNAUTHORIZED_STATE";
public void logoff() {
log.info("Moving from UNAUTHORIZED state to IDLE state.");
}
}
}