blob: 30ef879e1c59825d3ac2fb5481ca347d8a98b530 [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;
352 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700353 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700354 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700355 EAP eapPacket = (EAP) eapol.getPayload();
356
357 byte dataType = eapPacket.getDataType();
358 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700359
Ray Milkey9eb293f2015-09-30 15:09:17 -0700360 case EAP.ATTR_IDENTITY:
361 // request id access to RADIUS
362 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700363
Ray Milkey967776a2015-10-07 14:37:17 -0700364 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800365 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700366
Jonathan Hart092dfb22015-11-16 23:05:21 -0800367 sendRadiusPacket(radiusPayload);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700368
Ray Milkey9eb293f2015-09-30 15:09:17 -0700369 // change the state to "PENDING"
370 stateMachine.requestAccess();
371 break;
Ari Saha89831742015-06-26 10:31:48 -0700372 case EAP.ATTR_MD5:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700373 // verify if the EAP identifier corresponds to the
374 // challenge identifier from the client state
375 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700376 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700377 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700378 radiusPayload =
379 getRadiusPayload(stateMachine,
380 stateMachine.identifier(),
381 eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700382
383 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700384 stateMachine.challengeState());
Jonathan Hart092dfb22015-11-16 23:05:21 -0800385 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
386 sendRadiusPacket(radiusPayload);
Ari Saha89831742015-06-26 10:31:48 -0700387 }
388 break;
389 case EAP.ATTR_TLS:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700390 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700391 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700392
Ray Milkeyf51eba22015-09-25 10:24:23 -0700393 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800394 stateMachine.challengeState());
Ray Milkeyf51eba22015-09-25 10:24:23 -0700395 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700396
Jonathan Hart092dfb22015-11-16 23:05:21 -0800397 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
398 sendRadiusPacket(radiusPayload);
Ray Milkey5493b512015-10-21 12:13:49 -0700399
Ray Milkeyf3790b82015-10-21 16:28:08 -0700400 if (stateMachine.state() != StateMachine.STATE_PENDING) {
401 stateMachine.requestAccess();
402 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700403
Ari Saha89831742015-06-26 10:31:48 -0700404 break;
405 default:
406 return;
407 }
408 break;
409 default:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700410 log.trace("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700411 }
Ray Milkey967776a2015-10-07 14:37:17 -0700412
Ari Saha89831742015-06-26 10:31:48 -0700413 }
Ray Milkey967776a2015-10-07 14:37:17 -0700414 }
415
416 class RadiusListener implements Runnable {
Ari Saha89831742015-06-26 10:31:48 -0700417
418 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700419 * Handles RADIUS packets.
420 *
Ari Saha89831742015-06-26 10:31:48 -0700421 * @param radiusPacket RADIUS packet coming from the RADIUS server.
Ray Milkey17d6c492015-10-27 10:39:42 -0700422 * @throws StateMachineException if an illegal state transition is triggered
Ari Saha89831742015-06-26 10:31:48 -0700423 */
Ray Milkey967776a2015-10-07 14:37:17 -0700424 protected void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException {
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700425 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
Ari Saha89831742015-06-26 10:31:48 -0700426 if (stateMachine == null) {
427 log.error("Invalid session identifier, exiting...");
428 return;
429 }
430
Ray Milkeyf51eba22015-09-25 10:24:23 -0700431 EAP eapPayload;
432 Ethernet eth;
Ari Saha89831742015-06-26 10:31:48 -0700433 switch (radiusPacket.getCode()) {
434 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Ray Milkey967776a2015-10-07 14:37:17 -0700435 byte[] challengeState =
436 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue();
Ari Saha89831742015-06-26 10:31:48 -0700437 eapPayload = radiusPacket.decapsulateMessage();
438 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700439 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700440 MacAddress.valueOf(nasMacAddress),
441 stateMachine.vlanId(),
442 EAPOL.EAPOL_PACKET,
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700443 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700444 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700445 break;
446 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700447 //send an EAPOL - Success to the supplicant.
448 byte[] eapMessage =
449 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
450 eapPayload = new EAP();
451 eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length);
452 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700453 MacAddress.valueOf(nasMacAddress),
454 stateMachine.vlanId(),
455 EAPOL.EAPOL_PACKET,
Ray Milkeyf51eba22015-09-25 10:24:23 -0700456 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700457 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700458
Ray Milkeyf51eba22015-09-25 10:24:23 -0700459 stateMachine.authorizeAccess();
Ari Saha89831742015-06-26 10:31:48 -0700460 break;
461 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700462 stateMachine.denyAccess();
Ari Saha89831742015-06-26 10:31:48 -0700463 break;
464 default:
465 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
466 }
467 }
468
Ari Saha89831742015-06-26 10:31:48 -0700469
Ray Milkey967776a2015-10-07 14:37:17 -0700470 @Override
471 public void run() {
472 boolean done = false;
473 int packetNumber = 1;
474
475 log.info("UDP listener thread starting up");
476 RADIUS inboundRadiusPacket;
477 while (!done) {
478 try {
Ray Milkey5493b512015-10-21 12:13:49 -0700479 byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH];
480 DatagramPacket inboundBasePacket =
481 new DatagramPacket(packetBuffer, packetBuffer.length);
Ray Milkey967776a2015-10-07 14:37:17 -0700482 DatagramSocket socket = radiusSocket;
483 socket.receive(inboundBasePacket);
484 log.info("Packet #{} received", packetNumber++);
485 try {
486 inboundRadiusPacket =
487 RADIUS.deserializer()
488 .deserialize(inboundBasePacket.getData(),
489 0,
490 inboundBasePacket.getLength());
491 handleRadiusPacket(inboundRadiusPacket);
492 } catch (DeserializationException dex) {
493 log.error("Cannot deserialize packet", dex);
494 } catch (StateMachineException sme) {
495 log.error("Illegal state machine operation", sme);
496 }
497
498 } catch (IOException e) {
499 log.info("Socket was closed, exiting listener thread");
500 done = true;
501 }
Ari Saha89831742015-06-26 10:31:48 -0700502 }
Ari Saha89831742015-06-26 10:31:48 -0700503 }
Ari Saha89831742015-06-26 10:31:48 -0700504 }
505
Ray Milkey967776a2015-10-07 14:37:17 -0700506 RadiusListener radiusListener = new RadiusListener();
507
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700508 private class InternalConfigListener implements NetworkConfigListener {
509
510 /**
511 * Reconfigures the DHCP Server according to the configuration parameters passed.
512 *
513 * @param cfg configuration object
514 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800515 private void reconfigureNetwork(AaaConfig cfg) {
516 AaaConfig newCfg;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700517 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800518 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700519 } else {
520 newCfg = cfg;
521 }
522 if (newCfg.nasIp() != null) {
523 nasIpAddress = newCfg.nasIp();
524 }
525 if (newCfg.radiusIp() != null) {
526 radiusIpAddress = newCfg.radiusIp();
527 }
528 if (newCfg.radiusMac() != null) {
529 radiusMacAddress = newCfg.radiusMac();
530 }
531 if (newCfg.nasMac() != null) {
532 nasMacAddress = newCfg.nasMac();
533 }
534 if (newCfg.radiusSecret() != null) {
535 radiusSecret = newCfg.radiusSecret();
536 }
537 if (newCfg.radiusSwitch() != null) {
538 radiusSwitch = newCfg.radiusSwitch();
539 }
540 if (newCfg.radiusPort() != -1) {
541 radiusPort = newCfg.radiusPort();
542 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800543 if (newCfg.radiusServerUdpPort() != -1) {
544 radiusServerPort = newCfg.radiusServerUdpPort();
Ray Milkey5d99bd12015-10-06 15:41:30 -0700545 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700546 }
547
548 @Override
549 public void event(NetworkConfigEvent event) {
550
551 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
552 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800553 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700554
Jonathan Hart092dfb22015-11-16 23:05:21 -0800555 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700556 reconfigureNetwork(cfg);
557 log.info("Reconfigured");
558 }
559 }
560 }
561
562
Ari Saha89831742015-06-26 10:31:48 -0700563}