Committed by
Gerrit Code Review
More AAA app refactoring
- cleaned up exception handling a little - reformatting of the code to be easier to read and group similar code together - fixed some style problems found by IntelliJ Change-Id: I932ac48d82b598b1f59c906477fb5e4deded413f
Showing
1 changed file
with
49 additions
and
39 deletions
... | @@ -79,62 +79,85 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -79,62 +79,85 @@ import static org.slf4j.LoggerFactory.getLogger; |
79 | public class AAA { | 79 | public class AAA { |
80 | // RADIUS server IP address | 80 | // RADIUS server IP address |
81 | private static final String DEFAULT_RADIUS_IP = "192.168.1.10"; | 81 | private static final String DEFAULT_RADIUS_IP = "192.168.1.10"; |
82 | + | ||
82 | // NAS IP address | 83 | // NAS IP address |
83 | private static final String DEFAULT_NAS_IP = "192.168.1.11"; | 84 | private static final String DEFAULT_NAS_IP = "192.168.1.11"; |
85 | + | ||
84 | // RADIUS uplink port | 86 | // RADIUS uplink port |
85 | private static final int DEFAULT_RADIUS_UPLINK = 2; | 87 | private static final int DEFAULT_RADIUS_UPLINK = 2; |
88 | + | ||
86 | // RADIUS server shared secret | 89 | // RADIUS server shared secret |
87 | private static final String DEFAULT_RADIUS_SECRET = "ONOSecret"; | 90 | private static final String DEFAULT_RADIUS_SECRET = "ONOSecret"; |
91 | + | ||
88 | // RADIUS MAC address | 92 | // RADIUS MAC address |
89 | private static final String RADIUS_MAC_ADDRESS = "00:00:00:00:01:10"; | 93 | private static final String RADIUS_MAC_ADDRESS = "00:00:00:00:01:10"; |
94 | + | ||
90 | // NAS MAC address | 95 | // NAS MAC address |
91 | private static final String NAS_MAC_ADDRESS = "00:00:00:00:10:01"; | 96 | private static final String NAS_MAC_ADDRESS = "00:00:00:00:10:01"; |
97 | + | ||
92 | // Radius Switch Id | 98 | // Radius Switch Id |
93 | private static final String DEFAULT_RADIUS_SWITCH = "of:90e2ba82f97791e9"; | 99 | private static final String DEFAULT_RADIUS_SWITCH = "of:90e2ba82f97791e9"; |
100 | + | ||
94 | // Radius Port Number | 101 | // Radius Port Number |
95 | private static final String DEFAULT_RADIUS_PORT = "129"; | 102 | private static final String DEFAULT_RADIUS_PORT = "129"; |
103 | + | ||
96 | // for verbose output | 104 | // for verbose output |
97 | private final Logger log = getLogger(getClass()); | 105 | private final Logger log = getLogger(getClass()); |
106 | + | ||
98 | // a list of our dependencies : | 107 | // a list of our dependencies : |
99 | // to register with ONOS as an application - described next | 108 | // to register with ONOS as an application - described next |
100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
101 | protected CoreService coreService; | 110 | protected CoreService coreService; |
111 | + | ||
102 | // to receive Packet-in events that we'll respond to | 112 | // to receive Packet-in events that we'll respond to |
103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
104 | protected PacketService packetService; | 114 | protected PacketService packetService; |
115 | + | ||
105 | // end host information | 116 | // end host information |
106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
107 | protected HostService hostService; | 118 | protected HostService hostService; |
119 | + | ||
108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
109 | protected VoltTenantService voltTenantService; | 121 | protected VoltTenantService voltTenantService; |
122 | + | ||
123 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
124 | + protected ComponentConfigService cfgService; | ||
125 | + | ||
110 | // Parsed RADIUS server IP address | 126 | // Parsed RADIUS server IP address |
111 | protected InetAddress parsedRadiusIpAddress; | 127 | protected InetAddress parsedRadiusIpAddress; |
112 | // Parsed NAS IP address | 128 | // Parsed NAS IP address |
113 | protected InetAddress parsedNasIpAddress; | 129 | protected InetAddress parsedNasIpAddress; |
114 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 130 | + |
115 | - protected ComponentConfigService cfgService; | ||
116 | // our application-specific event handler | 131 | // our application-specific event handler |
117 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); | 132 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
133 | + | ||
118 | // our unique identifier | 134 | // our unique identifier |
119 | private ApplicationId appId; | 135 | private ApplicationId appId; |
136 | + | ||
120 | @Property(name = "radiusIpAddress", value = DEFAULT_RADIUS_IP, | 137 | @Property(name = "radiusIpAddress", value = DEFAULT_RADIUS_IP, |
121 | label = "RADIUS IP Address") | 138 | label = "RADIUS IP Address") |
122 | private String radiusIpAddress = DEFAULT_RADIUS_IP; | 139 | private String radiusIpAddress = DEFAULT_RADIUS_IP; |
140 | + | ||
123 | @Property(name = "nasIpAddress", value = DEFAULT_NAS_IP, | 141 | @Property(name = "nasIpAddress", value = DEFAULT_NAS_IP, |
124 | label = "NAS IP Address") | 142 | label = "NAS IP Address") |
125 | private String nasIpAddress = DEFAULT_NAS_IP; | 143 | private String nasIpAddress = DEFAULT_NAS_IP; |
144 | + | ||
126 | @Property(name = "radiusMacAddress", value = RADIUS_MAC_ADDRESS, | 145 | @Property(name = "radiusMacAddress", value = RADIUS_MAC_ADDRESS, |
127 | label = "RADIUS MAC Address") | 146 | label = "RADIUS MAC Address") |
128 | private String radiusMacAddress = RADIUS_MAC_ADDRESS; | 147 | private String radiusMacAddress = RADIUS_MAC_ADDRESS; |
148 | + | ||
129 | @Property(name = "nasMacAddress", value = NAS_MAC_ADDRESS, | 149 | @Property(name = "nasMacAddress", value = NAS_MAC_ADDRESS, |
130 | label = "NAS MAC Address") | 150 | label = "NAS MAC Address") |
131 | private String nasMacAddress = NAS_MAC_ADDRESS; | 151 | private String nasMacAddress = NAS_MAC_ADDRESS; |
152 | + | ||
132 | @Property(name = "radiusSecret", value = DEFAULT_RADIUS_SECRET, | 153 | @Property(name = "radiusSecret", value = DEFAULT_RADIUS_SECRET, |
133 | label = "RADIUS shared secret") | 154 | label = "RADIUS shared secret") |
134 | private String radiusSecret = DEFAULT_RADIUS_SECRET; | 155 | private String radiusSecret = DEFAULT_RADIUS_SECRET; |
156 | + | ||
135 | @Property(name = "radiusSwitchId", value = DEFAULT_RADIUS_SWITCH, | 157 | @Property(name = "radiusSwitchId", value = DEFAULT_RADIUS_SWITCH, |
136 | label = "Radius switch") | 158 | label = "Radius switch") |
137 | private String radiusSwitch = DEFAULT_RADIUS_SWITCH; | 159 | private String radiusSwitch = DEFAULT_RADIUS_SWITCH; |
160 | + | ||
138 | @Property(name = "radiusPortNumber", value = DEFAULT_RADIUS_PORT, | 161 | @Property(name = "radiusPortNumber", value = DEFAULT_RADIUS_PORT, |
139 | label = "Radius port") | 162 | label = "Radius port") |
140 | private String radiusPort = DEFAULT_RADIUS_PORT; | 163 | private String radiusPort = DEFAULT_RADIUS_PORT; |
... | @@ -181,14 +204,14 @@ public class AAA { | ... | @@ -181,14 +204,14 @@ public class AAA { |
181 | parsedRadiusIpAddress = InetAddress.getByName(s); | 204 | parsedRadiusIpAddress = InetAddress.getByName(s); |
182 | radiusIpAddress = Strings.isNullOrEmpty(s) ? DEFAULT_RADIUS_IP : s; | 205 | radiusIpAddress = Strings.isNullOrEmpty(s) ? DEFAULT_RADIUS_IP : s; |
183 | } catch (UnknownHostException e) { | 206 | } catch (UnknownHostException e) { |
184 | - log.error("Invalid RADIUS IP address specification: {}", s); | 207 | + log.error("Invalid RADIUS IP address specification: {}", s, e); |
185 | } | 208 | } |
186 | try { | 209 | try { |
187 | s = Tools.get(properties, "nasIpAddress"); | 210 | s = Tools.get(properties, "nasIpAddress"); |
188 | parsedNasIpAddress = InetAddress.getByName(s); | 211 | parsedNasIpAddress = InetAddress.getByName(s); |
189 | nasIpAddress = Strings.isNullOrEmpty(s) ? DEFAULT_NAS_IP : s; | 212 | nasIpAddress = Strings.isNullOrEmpty(s) ? DEFAULT_NAS_IP : s; |
190 | } catch (UnknownHostException e) { | 213 | } catch (UnknownHostException e) { |
191 | - log.error("Invalid NAS IP address specification: {}", s); | 214 | + log.error("Invalid NAS IP address specification: {}", s, e); |
192 | } | 215 | } |
193 | 216 | ||
194 | s = Tools.get(properties, "radiusMacAddress"); | 217 | s = Tools.get(properties, "radiusMacAddress"); |
... | @@ -284,6 +307,7 @@ public class AAA { | ... | @@ -284,6 +307,7 @@ public class AAA { |
284 | if (ethPkt == null) { | 307 | if (ethPkt == null) { |
285 | return; | 308 | return; |
286 | } | 309 | } |
310 | + try { | ||
287 | // identify if incoming packet comes from supplicant (EAP) or RADIUS | 311 | // identify if incoming packet comes from supplicant (EAP) or RADIUS |
288 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { | 312 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { |
289 | case EAPOL: | 313 | case EAPOL: |
... | @@ -299,17 +323,17 @@ public class AAA { | ... | @@ -299,17 +323,17 @@ public class AAA { |
299 | 323 | ||
300 | byte[] datagram = udpPacket.getPayload().serialize(); | 324 | byte[] datagram = udpPacket.getPayload().serialize(); |
301 | RADIUS radiusPacket; | 325 | RADIUS radiusPacket; |
302 | - try { | ||
303 | radiusPacket = RADIUS.deserializer().deserialize(datagram, 0, datagram.length); | 326 | radiusPacket = RADIUS.deserializer().deserialize(datagram, 0, datagram.length); |
304 | - } catch (DeserializationException e) { | ||
305 | - log.warn("Unable to deserialize RADIUS packet:", e); | ||
306 | - return; | ||
307 | - } | ||
308 | handleRadiusPacket(radiusPacket); | 327 | handleRadiusPacket(radiusPacket); |
309 | } | 328 | } |
329 | + | ||
310 | break; | 330 | break; |
311 | default: | 331 | default: |
312 | - return; | 332 | + log.trace("Skipping Ethernet packet type {}", |
333 | + EthType.EtherType.lookup(ethPkt.getEtherType())); | ||
334 | + } | ||
335 | + } catch (DeserializationException | StateMachineException e) { | ||
336 | + log.warn("Unable to process RADIUS packet:", e); | ||
313 | } | 337 | } |
314 | } | 338 | } |
315 | 339 | ||
... | @@ -319,7 +343,7 @@ public class AAA { | ... | @@ -319,7 +343,7 @@ public class AAA { |
319 | * | 343 | * |
320 | * @param inPacket Ethernet packet coming from the supplicant | 344 | * @param inPacket Ethernet packet coming from the supplicant |
321 | */ | 345 | */ |
322 | - private void handleSupplicantPacket(InboundPacket inPacket) { | 346 | + private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException { |
323 | Ethernet ethPkt = inPacket.parsed(); | 347 | Ethernet ethPkt = inPacket.parsed(); |
324 | // Where does it come from? | 348 | // Where does it come from? |
325 | MacAddress srcMAC = ethPkt.getSourceMAC(); | 349 | MacAddress srcMAC = ethPkt.getSourceMAC(); |
... | @@ -337,7 +361,6 @@ public class AAA { | ... | @@ -337,7 +361,6 @@ public class AAA { |
337 | 361 | ||
338 | switch (eapol.getEapolType()) { | 362 | switch (eapol.getEapolType()) { |
339 | case EAPOL.EAPOL_START: | 363 | case EAPOL.EAPOL_START: |
340 | - try { | ||
341 | stateMachine.start(); | 364 | stateMachine.start(); |
342 | stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); | 365 | stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); |
343 | 366 | ||
... | @@ -350,26 +373,27 @@ public class AAA { | ... | @@ -350,26 +373,27 @@ public class AAA { |
350 | stateMachine.setVlanId(ethPkt.getVlanID()); | 373 | stateMachine.setVlanId(ethPkt.getVlanID()); |
351 | 374 | ||
352 | this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); | 375 | this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
353 | - } catch (StateMachineException e) { | ||
354 | - e.printStackTrace(); | ||
355 | - } | ||
356 | 376 | ||
357 | break; | 377 | break; |
358 | case EAPOL.EAPOL_PACKET: | 378 | case EAPOL.EAPOL_PACKET: |
379 | + RADIUS radiusPayload; | ||
359 | //check if this is a Response/Identify or a Response/TLS | 380 | //check if this is a Response/Identify or a Response/TLS |
360 | EAP eapPacket = (EAP) eapol.getPayload(); | 381 | EAP eapPacket = (EAP) eapol.getPayload(); |
361 | 382 | ||
362 | byte dataType = eapPacket.getDataType(); | 383 | byte dataType = eapPacket.getDataType(); |
363 | switch (dataType) { | 384 | switch (dataType) { |
385 | + | ||
364 | case EAP.ATTR_IDENTITY: | 386 | case EAP.ATTR_IDENTITY: |
365 | - try { | ||
366 | //request id access to RADIUS | 387 | //request id access to RADIUS |
367 | - RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | 388 | + stateMachine.setUsername(eapPacket.getData()); |
389 | + | ||
390 | + radiusPayload = | ||
391 | + new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | ||
368 | eapPacket.getIdentifier()); | 392 | eapPacket.getIdentifier()); |
369 | radiusPayload.setIdentifier(stateMachine.identifier()); | 393 | radiusPayload.setIdentifier(stateMachine.identifier()); |
370 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, | 394 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
371 | eapPacket.getData()); | 395 | eapPacket.getData()); |
372 | - stateMachine.setUsername(eapPacket.getData()); | 396 | + |
373 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, | 397 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
374 | AAA.this.parsedNasIpAddress.getAddress()); | 398 | AAA.this.parsedNasIpAddress.getAddress()); |
375 | 399 | ||
... | @@ -382,16 +406,13 @@ public class AAA { | ... | @@ -382,16 +406,13 @@ public class AAA { |
382 | 406 | ||
383 | //change the state to "PENDING" | 407 | //change the state to "PENDING" |
384 | stateMachine.requestAccess(); | 408 | stateMachine.requestAccess(); |
385 | - } catch (StateMachineException e) { | ||
386 | - e.printStackTrace(); | ||
387 | - } | ||
388 | break; | 409 | break; |
389 | case EAP.ATTR_MD5: | 410 | case EAP.ATTR_MD5: |
390 | //verify if the EAP identifier corresponds to the challenge identifier from the client state | 411 | //verify if the EAP identifier corresponds to the challenge identifier from the client state |
391 | //machine. | 412 | //machine. |
392 | if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { | 413 | if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { |
393 | //send the RADIUS challenge response | 414 | //send the RADIUS challenge response |
394 | - RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | 415 | + radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
395 | eapPacket.getIdentifier()); | 416 | eapPacket.getIdentifier()); |
396 | radiusPayload.setIdentifier(stateMachine.challengeIdentifier()); | 417 | radiusPayload.setIdentifier(stateMachine.challengeIdentifier()); |
397 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, | 418 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
... | @@ -408,9 +429,8 @@ public class AAA { | ... | @@ -408,9 +429,8 @@ public class AAA { |
408 | } | 429 | } |
409 | break; | 430 | break; |
410 | case EAP.ATTR_TLS: | 431 | case EAP.ATTR_TLS: |
411 | - try { | ||
412 | //request id access to RADIUS | 432 | //request id access to RADIUS |
413 | - RADIUS radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, | 433 | + radiusPayload = new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
414 | eapPacket.getIdentifier()); | 434 | eapPacket.getIdentifier()); |
415 | radiusPayload.setIdentifier(stateMachine.identifier()); | 435 | radiusPayload.setIdentifier(stateMachine.identifier()); |
416 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, | 436 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
... | @@ -429,16 +449,14 @@ public class AAA { | ... | @@ -429,16 +449,14 @@ public class AAA { |
429 | sendRadiusMessage(radiusPayload); | 449 | sendRadiusMessage(radiusPayload); |
430 | // TODO: this gets called on every fragment, should only be called at TLS-Start | 450 | // TODO: this gets called on every fragment, should only be called at TLS-Start |
431 | stateMachine.requestAccess(); | 451 | stateMachine.requestAccess(); |
432 | - } catch (StateMachineException e) { | 452 | + |
433 | - e.printStackTrace(); | ||
434 | - } | ||
435 | break; | 453 | break; |
436 | default: | 454 | default: |
437 | return; | 455 | return; |
438 | } | 456 | } |
439 | break; | 457 | break; |
440 | default: | 458 | default: |
441 | - return; | 459 | + log.trace("Skipping EAPOL message {}", eapol.getEapolType()); |
442 | } | 460 | } |
443 | } | 461 | } |
444 | 462 | ||
... | @@ -447,15 +465,15 @@ public class AAA { | ... | @@ -447,15 +465,15 @@ public class AAA { |
447 | * | 465 | * |
448 | * @param radiusPacket RADIUS packet coming from the RADIUS server. | 466 | * @param radiusPacket RADIUS packet coming from the RADIUS server. |
449 | */ | 467 | */ |
450 | - private void handleRadiusPacket(RADIUS radiusPacket) { | 468 | + private void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException { |
451 | StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier()); | 469 | StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier()); |
452 | if (stateMachine == null) { | 470 | if (stateMachine == null) { |
453 | log.error("Invalid session identifier, exiting..."); | 471 | log.error("Invalid session identifier, exiting..."); |
454 | return; | 472 | return; |
455 | } | 473 | } |
456 | 474 | ||
457 | - EAP eapPayload = new EAP(); | 475 | + EAP eapPayload; |
458 | - Ethernet eth = null; | 476 | + Ethernet eth; |
459 | switch (radiusPacket.getCode()) { | 477 | switch (radiusPacket.getCode()) { |
460 | case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE: | 478 | case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE: |
461 | byte[] challengeState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue(); | 479 | byte[] challengeState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue(); |
... | @@ -467,7 +485,6 @@ public class AAA { | ... | @@ -467,7 +485,6 @@ public class AAA { |
467 | this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); | 485 | this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
468 | break; | 486 | break; |
469 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: | 487 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: |
470 | - try { | ||
471 | //send an EAPOL - Success to the supplicant. | 488 | //send an EAPOL - Success to the supplicant. |
472 | byte[] eapMessage = | 489 | byte[] eapMessage = |
473 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); | 490 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); |
... | @@ -479,16 +496,9 @@ public class AAA { | ... | @@ -479,16 +496,9 @@ public class AAA { |
479 | this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); | 496 | this.sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
480 | 497 | ||
481 | stateMachine.authorizeAccess(); | 498 | stateMachine.authorizeAccess(); |
482 | - } catch (StateMachineException e) { | ||
483 | - e.printStackTrace(); | ||
484 | - } | ||
485 | break; | 499 | break; |
486 | case RADIUS.RADIUS_CODE_ACCESS_REJECT: | 500 | case RADIUS.RADIUS_CODE_ACCESS_REJECT: |
487 | - try { | ||
488 | stateMachine.denyAccess(); | 501 | stateMachine.denyAccess(); |
489 | - } catch (StateMachineException e) { | ||
490 | - e.printStackTrace(); | ||
491 | - } | ||
492 | break; | 502 | break; |
493 | default: | 503 | default: |
494 | log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode()); | 504 | log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode()); | ... | ... |
-
Please register or login to post a comment