blob: 4567f0168a97baa5efab8e9f39a7afff02f94141 [file] [log] [blame]
Ari Saha89831742015-06-26 10:31:48 -07001/*
2 * Copyright 2015 AT&T Foundry
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 */
16package org.onosproject.aaa;
17
Jonathan Hart092dfb22015-11-16 23:05:21 -080018import com.google.common.util.concurrent.ThreadFactoryBuilder;
Ari Saha89831742015-06-26 10:31:48 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Ari Saha89831742015-06-26 10:31:48 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harta46dddf2015-06-30 15:31:20 -070024import org.onlab.packet.DeserializationException;
25import org.onlab.packet.EAP;
26import org.onlab.packet.EAPOL;
27import org.onlab.packet.EthType;
Ari Saha89831742015-06-26 10:31:48 -070028import org.onlab.packet.Ethernet;
Ari Saha89831742015-06-26 10:31:48 -070029import org.onlab.packet.MacAddress;
Jonathan Harta46dddf2015-06-30 15:31:20 -070030import org.onlab.packet.RADIUS;
31import org.onlab.packet.RADIUSAttribute;
Ari Saha89831742015-06-26 10:31:48 -070032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.DeviceId;
Ari Saha89831742015-06-26 10:31:48 -070036import org.onosproject.net.PortNumber;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070037import org.onosproject.net.config.ConfigFactory;
38import org.onosproject.net.config.NetworkConfigEvent;
39import org.onosproject.net.config.NetworkConfigListener;
40import org.onosproject.net.config.NetworkConfigRegistry;
Ari Saha89831742015-06-26 10:31:48 -070041import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070043import org.onosproject.net.flow.TrafficSelector;
44import org.onosproject.net.flow.TrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070045import org.onosproject.net.packet.DefaultOutboundPacket;
46import org.onosproject.net.packet.InboundPacket;
47import org.onosproject.net.packet.OutboundPacket;
48import org.onosproject.net.packet.PacketContext;
Ari Saha89831742015-06-26 10:31:48 -070049import org.onosproject.net.packet.PacketProcessor;
50import org.onosproject.net.packet.PacketService;
Ari Saha89831742015-06-26 10:31:48 -070051import org.onosproject.xosintegration.VoltTenantService;
Ari Saha89831742015-06-26 10:31:48 -070052import org.slf4j.Logger;
53
Jonathan Hart092dfb22015-11-16 23:05:21 -080054import java.io.IOException;
55import java.net.DatagramPacket;
56import java.net.DatagramSocket;
57import java.net.InetAddress;
58import java.nio.ByteBuffer;
59import java.util.concurrent.ExecutorService;
60import java.util.concurrent.Executors;
Ray Milkey967776a2015-10-07 14:37:17 -070061
Ray Milkeyfcb623d2015-10-01 16:48:18 -070062import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -070063import static org.onosproject.net.packet.PacketPriority.CONTROL;
Ari Saha89831742015-06-26 10:31:48 -070064import static org.slf4j.LoggerFactory.getLogger;
65
Ari Saha89831742015-06-26 10:31:48 -070066/**
Jonathan Harta46dddf2015-06-30 15:31:20 -070067 * AAA application for ONOS.
Ari Saha89831742015-06-26 10:31:48 -070068 */
69@Component(immediate = true)
Jonathan Hart092dfb22015-11-16 23:05:21 -080070public class AaaManager {
Ray Milkeyf51eba22015-09-25 10:24:23 -070071
Ray Milkeyf61a24e2015-09-24 16:34:02 -070072 // for verbose output
73 private final Logger log = getLogger(getClass());
Ray Milkeyf51eba22015-09-25 10:24:23 -070074
Ray Milkeyf61a24e2015-09-24 16:34:02 -070075 // a list of our dependencies :
76 // to register with ONOS as an application - described next
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected CoreService coreService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070079
Ray Milkeyf61a24e2015-09-24 16:34:02 -070080 // to receive Packet-in events that we'll respond to
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected PacketService packetService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070083
Ray Milkeyf61a24e2015-09-24 16:34:02 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected VoltTenantService voltTenantService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070086
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkeyfcb623d2015-10-01 16:48:18 -070088 protected NetworkConfigRegistry netCfgService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070089
Ray Milkeyfcb623d2015-10-01 16:48:18 -070090 // Parsed RADIUS server addresses
91 protected InetAddress radiusIpAddress;
92 protected String radiusMacAddress;
93
94 // NAS IP address
95 protected InetAddress nasIpAddress;
96 protected String nasMacAddress;
97
98 // RADIUS server secret
99 protected String radiusSecret;
100
101 // ID of RADIUS switch
102 protected String radiusSwitch;
103
104 // RADIUS port number
105 protected long radiusPort;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700106
Ray Milkey5d99bd12015-10-06 15:41:30 -0700107 // RADIUS server TCP port number
108 protected short radiusServerPort;
109
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700110 // our application-specific event handler
111 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700112
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700113 // our unique identifier
114 private ApplicationId appId;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700115
Ray Milkey967776a2015-10-07 14:37:17 -0700116 // Socket used for UDP communications with RADIUS server
117 private DatagramSocket radiusSocket;
118
119 // Executor for RADIUS communication thread
120 private ExecutorService executor;
121
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700122 // Configuration properties factory
123 private final ConfigFactory factory =
Jonathan Hart092dfb22015-11-16 23:05:21 -0800124 new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY,
125 AaaConfig.class,
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700126 "AAA") {
127 @Override
Jonathan Hart092dfb22015-11-16 23:05:21 -0800128 public AaaConfig createConfig() {
129 return new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700130 }
131 };
Ray Milkeyf51eba22015-09-25 10:24:23 -0700132
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700133 // Listener for config changes
134 private final InternalConfigListener cfgListener = new InternalConfigListener();
Ari Saha89831742015-06-26 10:31:48 -0700135
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700136 /**
137 * Builds an EAPOL packet based on the given parameters.
138 *
139 * @param dstMac destination MAC address
140 * @param srcMac source MAC address
141 * @param vlan vlan identifier
142 * @param eapolType EAPOL type
143 * @param eap EAP payload
144 * @return Ethernet frame
145 */
146 private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
147 short vlan, byte eapolType, EAP eap) {
Ari Saha89831742015-06-26 10:31:48 -0700148
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700149 Ethernet eth = new Ethernet();
150 eth.setDestinationMACAddress(dstMac.toBytes());
151 eth.setSourceMACAddress(srcMac.toBytes());
152 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
153 if (vlan != Ethernet.VLAN_UNTAGGED) {
154 eth.setVlanID(vlan);
155 }
156 //eapol header
157 EAPOL eapol = new EAPOL();
158 eapol.setEapolType(eapolType);
159 eapol.setPacketLength(eap.getLength());
Ari Saha89831742015-06-26 10:31:48 -0700160
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700161 //eap part
162 eapol.setPayload(eap);
163
164 eth.setPayload(eapol);
165 eth.setPad(true);
166 return eth;
167 }
Ari Saha89831742015-06-26 10:31:48 -0700168
Ari Saha89831742015-06-26 10:31:48 -0700169 @Activate
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700170 public void activate() {
171 netCfgService.addListener(cfgListener);
172 netCfgService.registerConfigFactory(factory);
173
Ari Saha89831742015-06-26 10:31:48 -0700174 // "org.onosproject.aaa" is the FQDN of our app
175 appId = coreService.registerApplication("org.onosproject.aaa");
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700176
Jonathan Hart092dfb22015-11-16 23:05:21 -0800177 cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class));
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700178
Ari Saha89831742015-06-26 10:31:48 -0700179 // register our event handler
Brian O'Connord9c7da02015-07-29 17:49:24 -0700180 packetService.addProcessor(processor, PacketProcessor.director(2));
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700181 requestIntercepts();
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700182
183 StateMachine.initializeMaps();
Ari Saha89831742015-06-26 10:31:48 -0700184
Ray Milkey967776a2015-10-07 14:37:17 -0700185 try {
186 radiusSocket = new DatagramSocket(radiusServerPort);
187 } catch (Exception ex) {
188 log.error("Can't open RADIUS socket", ex);
189 }
190
191 executor = Executors.newSingleThreadExecutor(
192 new ThreadFactoryBuilder()
193 .setNameFormat("AAA-radius-%d").build());
194 executor.execute(radiusListener);
Jian Li13c67162015-12-09 13:20:34 -0800195 log.info("Started");
Ari Saha89831742015-06-26 10:31:48 -0700196 }
197
198 @Deactivate
199 public void deactivate() {
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700200 withdrawIntercepts();
Ari Saha89831742015-06-26 10:31:48 -0700201 // de-register and null our handler
202 packetService.removeProcessor(processor);
203 processor = null;
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700204 StateMachine.destroyMaps();
Ray Milkey967776a2015-10-07 14:37:17 -0700205 radiusSocket.close();
206 executor.shutdownNow();
Jian Li13c67162015-12-09 13:20:34 -0800207 log.info("Stopped");
Ray Milkey967776a2015-10-07 14:37:17 -0700208 }
209
Jonathan Hart092dfb22015-11-16 23:05:21 -0800210 protected void sendRadiusPacket(RADIUS radiusPacket) {
Ray Milkey967776a2015-10-07 14:37:17 -0700211
212 try {
213 final byte[] data = radiusPacket.serialize();
214 final DatagramSocket socket = radiusSocket;
215
216 DatagramPacket packet =
217 new DatagramPacket(data, data.length,
218 radiusIpAddress, radiusServerPort);
219
220 socket.send(packet);
221 } catch (IOException e) {
222 log.info("Cannot send packet to RADIUS server", e);
223 }
Ari Saha89831742015-06-26 10:31:48 -0700224 }
225
Jonathan Harta46dddf2015-06-30 15:31:20 -0700226 /**
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700227 * Request packet in via PacketService.
228 */
229 private void requestIntercepts() {
230 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
231 selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
232 packetService.requestPackets(selector.build(),
233 CONTROL, appId);
234 }
235
236 /**
237 * Cancel request for packet in via PacketService.
238 */
239 private void withdrawIntercepts() {
240 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
241 selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
242 packetService.cancelPackets(selector.build(), CONTROL, appId);
243 }
244
Ray Milkey967776a2015-10-07 14:37:17 -0700245 /**
246 * Send the ethernet packet to the supplicant.
247 *
248 * @param ethernetPkt the ethernet packet
249 * @param connectPoint the connect point to send out
250 */
251 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) {
252 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
253 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
254 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
255 packetService.emit(packet);
256 }
257
Ari Saha89831742015-06-26 10:31:48 -0700258 // our handler defined as a private inner class
259
260 /**
261 * Packet processor responsible for forwarding packets along their paths.
262 */
263 private class ReactivePacketProcessor implements PacketProcessor {
264 @Override
265 public void process(PacketContext context) {
266
267 // Extract the original Ethernet frame from the packet information
268 InboundPacket pkt = context.inPacket();
269 Ethernet ethPkt = pkt.parsed();
270 if (ethPkt == null) {
271 return;
272 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700273 try {
274 // identify if incoming packet comes from supplicant (EAP) or RADIUS
275 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
276 case EAPOL:
277 handleSupplicantPacket(context.inPacket());
278 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700279 default:
280 log.trace("Skipping Ethernet packet type {}",
281 EthType.EtherType.lookup(ethPkt.getEtherType()));
282 }
Ray Milkey967776a2015-10-07 14:37:17 -0700283 } catch (StateMachineException e) {
Ray Milkeyf51eba22015-09-25 10:24:23 -0700284 log.warn("Unable to process RADIUS packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700285 }
286 }
287
Ray Milkey9eb293f2015-09-30 15:09:17 -0700288 /**
289 * Creates and initializes common fields of a RADIUS packet.
290 *
Ray Milkey967776a2015-10-07 14:37:17 -0700291 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700292 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700293 * @return RADIUS packet
294 */
Ray Milkey967776a2015-10-07 14:37:17 -0700295 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700296 RADIUS radiusPayload =
297 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
298 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700299
300 // set Request Authenticator in StateMachine
301 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
302
Ray Milkey9eb293f2015-09-30 15:09:17 -0700303 radiusPayload.setIdentifier(identifier);
304 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700305 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700306
307 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800308 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700309
310 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700311
312 return radiusPayload;
313 }
Ari Saha89831742015-06-26 10:31:48 -0700314
315 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700316 * Handles PAE packets (supplicant).
317 *
318 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700319 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700320 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700321 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700322 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800323 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700324
Jonathan Harta46dddf2015-06-30 15:31:20 -0700325 DeviceId deviceId = inPacket.receivedFrom().deviceId();
326 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700327 String sessionId = deviceId.toString() + portNumber.toString();
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700328 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
329 if (stateMachine == null) {
330 stateMachine = new StateMachine(sessionId, voltTenantService);
331 }
332
Jonathan Harta46dddf2015-06-30 15:31:20 -0700333
334 EAPOL eapol = (EAPOL) ethPkt.getPayload();
Ari Saha89831742015-06-26 10:31:48 -0700335
336 switch (eapol.getEapolType()) {
337 case EAPOL.EAPOL_START:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700338 stateMachine.start();
339 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Ari Saha89831742015-06-26 10:31:48 -0700340
Ray Milkeyf51eba22015-09-25 10:24:23 -0700341 //send an EAP Request/Identify to the supplicant
342 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800343 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700344 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
345 eapPayload);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800346 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700347 stateMachine.setVlanId(ethPkt.getVlanID());
Ari Saha89831742015-06-26 10:31:48 -0700348
Ray Milkey967776a2015-10-07 14:37:17 -0700349 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700350
351 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800352 case EAPOL.EAPOL_LOGOFF:
Ray Milkeyb34b4962016-01-04 10:24:43 -0800353 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
354 stateMachine.logoff();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800355 }
356
357 break;
Ari Saha89831742015-06-26 10:31:48 -0700358 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700359 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700360 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700361 EAP eapPacket = (EAP) eapol.getPayload();
362
363 byte dataType = eapPacket.getDataType();
364 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700365
Ray Milkey9eb293f2015-09-30 15:09:17 -0700366 case EAP.ATTR_IDENTITY:
367 // request id access to RADIUS
368 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700369
Ray Milkey967776a2015-10-07 14:37:17 -0700370 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800371 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700372
Jonathan Hart092dfb22015-11-16 23:05:21 -0800373 sendRadiusPacket(radiusPayload);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700374
Ray Milkey9eb293f2015-09-30 15:09:17 -0700375 // change the state to "PENDING"
376 stateMachine.requestAccess();
377 break;
Ari Saha89831742015-06-26 10:31:48 -0700378 case EAP.ATTR_MD5:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700379 // verify if the EAP identifier corresponds to the
380 // challenge identifier from the client state
381 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700382 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700383 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700384 radiusPayload =
385 getRadiusPayload(stateMachine,
386 stateMachine.identifier(),
387 eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700388
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800389 if (stateMachine.challengeState() != null) {
390 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
391 stateMachine.challengeState());
392 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800393 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
394 sendRadiusPacket(radiusPayload);
Ari Saha89831742015-06-26 10:31:48 -0700395 }
396 break;
397 case EAP.ATTR_TLS:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700398 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700399 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700400
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800401 if (stateMachine.challengeState() != null) {
402 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
403 stateMachine.challengeState());
404 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700405 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700406
Jonathan Hart092dfb22015-11-16 23:05:21 -0800407 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
408 sendRadiusPacket(radiusPayload);
Ray Milkey5493b512015-10-21 12:13:49 -0700409
Ray Milkeyf3790b82015-10-21 16:28:08 -0700410 if (stateMachine.state() != StateMachine.STATE_PENDING) {
411 stateMachine.requestAccess();
412 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700413
Ari Saha89831742015-06-26 10:31:48 -0700414 break;
415 default:
416 return;
417 }
418 break;
419 default:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700420 log.trace("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700421 }
Ray Milkey967776a2015-10-07 14:37:17 -0700422
Ari Saha89831742015-06-26 10:31:48 -0700423 }
Ray Milkey967776a2015-10-07 14:37:17 -0700424 }
425
426 class RadiusListener implements Runnable {
Ari Saha89831742015-06-26 10:31:48 -0700427
428 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700429 * Handles RADIUS packets.
430 *
Ari Saha89831742015-06-26 10:31:48 -0700431 * @param radiusPacket RADIUS packet coming from the RADIUS server.
Ray Milkey17d6c492015-10-27 10:39:42 -0700432 * @throws StateMachineException if an illegal state transition is triggered
Ari Saha89831742015-06-26 10:31:48 -0700433 */
Ray Milkey967776a2015-10-07 14:37:17 -0700434 protected void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException {
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700435 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
Ari Saha89831742015-06-26 10:31:48 -0700436 if (stateMachine == null) {
437 log.error("Invalid session identifier, exiting...");
438 return;
439 }
440
Ray Milkeyf51eba22015-09-25 10:24:23 -0700441 EAP eapPayload;
442 Ethernet eth;
Ari Saha89831742015-06-26 10:31:48 -0700443 switch (radiusPacket.getCode()) {
444 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800445 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
446 byte[] challengeState = null;
447 if (radiusAttrState != null) {
448 challengeState = radiusAttrState.getValue();
449 }
Ari Saha89831742015-06-26 10:31:48 -0700450 eapPayload = radiusPacket.decapsulateMessage();
451 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700452 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700453 MacAddress.valueOf(nasMacAddress),
454 stateMachine.vlanId(),
455 EAPOL.EAPOL_PACKET,
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700456 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700457 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700458 break;
459 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700460 //send an EAPOL - Success to the supplicant.
461 byte[] eapMessage =
462 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
463 eapPayload = new EAP();
464 eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length);
465 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700466 MacAddress.valueOf(nasMacAddress),
467 stateMachine.vlanId(),
468 EAPOL.EAPOL_PACKET,
Ray Milkeyf51eba22015-09-25 10:24:23 -0700469 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700470 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700471
Ray Milkeyf51eba22015-09-25 10:24:23 -0700472 stateMachine.authorizeAccess();
Ari Saha89831742015-06-26 10:31:48 -0700473 break;
474 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700475 stateMachine.denyAccess();
Ari Saha89831742015-06-26 10:31:48 -0700476 break;
477 default:
478 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
479 }
480 }
481
Ari Saha89831742015-06-26 10:31:48 -0700482
Ray Milkey967776a2015-10-07 14:37:17 -0700483 @Override
484 public void run() {
485 boolean done = false;
486 int packetNumber = 1;
487
488 log.info("UDP listener thread starting up");
489 RADIUS inboundRadiusPacket;
490 while (!done) {
491 try {
Ray Milkey5493b512015-10-21 12:13:49 -0700492 byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH];
493 DatagramPacket inboundBasePacket =
494 new DatagramPacket(packetBuffer, packetBuffer.length);
Ray Milkey967776a2015-10-07 14:37:17 -0700495 DatagramSocket socket = radiusSocket;
496 socket.receive(inboundBasePacket);
497 log.info("Packet #{} received", packetNumber++);
498 try {
499 inboundRadiusPacket =
500 RADIUS.deserializer()
501 .deserialize(inboundBasePacket.getData(),
502 0,
503 inboundBasePacket.getLength());
504 handleRadiusPacket(inboundRadiusPacket);
505 } catch (DeserializationException dex) {
506 log.error("Cannot deserialize packet", dex);
507 } catch (StateMachineException sme) {
508 log.error("Illegal state machine operation", sme);
509 }
510
511 } catch (IOException e) {
512 log.info("Socket was closed, exiting listener thread");
513 done = true;
514 }
Ari Saha89831742015-06-26 10:31:48 -0700515 }
Ari Saha89831742015-06-26 10:31:48 -0700516 }
Ari Saha89831742015-06-26 10:31:48 -0700517 }
518
Ray Milkey967776a2015-10-07 14:37:17 -0700519 RadiusListener radiusListener = new RadiusListener();
520
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700521 private class InternalConfigListener implements NetworkConfigListener {
522
523 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800524 * Reconfigures the AAA application according to the
525 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700526 *
527 * @param cfg configuration object
528 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800529 private void reconfigureNetwork(AaaConfig cfg) {
530 AaaConfig newCfg;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700531 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800532 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700533 } else {
534 newCfg = cfg;
535 }
536 if (newCfg.nasIp() != null) {
537 nasIpAddress = newCfg.nasIp();
538 }
539 if (newCfg.radiusIp() != null) {
540 radiusIpAddress = newCfg.radiusIp();
541 }
542 if (newCfg.radiusMac() != null) {
543 radiusMacAddress = newCfg.radiusMac();
544 }
545 if (newCfg.nasMac() != null) {
546 nasMacAddress = newCfg.nasMac();
547 }
548 if (newCfg.radiusSecret() != null) {
549 radiusSecret = newCfg.radiusSecret();
550 }
551 if (newCfg.radiusSwitch() != null) {
552 radiusSwitch = newCfg.radiusSwitch();
553 }
554 if (newCfg.radiusPort() != -1) {
555 radiusPort = newCfg.radiusPort();
556 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800557 if (newCfg.radiusServerUdpPort() != -1) {
558 radiusServerPort = newCfg.radiusServerUdpPort();
Ray Milkey5d99bd12015-10-06 15:41:30 -0700559 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700560 }
561
562 @Override
563 public void event(NetworkConfigEvent event) {
564
565 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
566 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800567 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700568
Jonathan Hart092dfb22015-11-16 23:05:21 -0800569 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700570 reconfigureNetwork(cfg);
571 log.info("Reconfigured");
572 }
573 }
574 }
575
576
Ari Saha89831742015-06-26 10:31:48 -0700577}