blob: f15895666d3d1565f335b4cebe7cc737cea3bde9 [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 */
alshabib6d527452016-06-01 18:00:47 -070016package org.opencord.aaa;
Ari Saha89831742015-06-26 10:31:48 -070017
Ray Milkey508cec52016-02-16 14:23:26 -080018import java.io.IOException;
19import java.net.DatagramPacket;
20import java.net.DatagramSocket;
21import java.net.InetAddress;
A.R Karthick6af9f432016-03-11 20:24:33 -080022import java.net.InetSocketAddress;
Ray Milkey508cec52016-02-16 14:23:26 -080023import java.nio.ByteBuffer;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.Executors;
26
Ari Saha89831742015-06-26 10:31:48 -070027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
Ari Saha89831742015-06-26 10:31:48 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harta46dddf2015-06-30 15:31:20 -070032import org.onlab.packet.DeserializationException;
33import org.onlab.packet.EAP;
34import org.onlab.packet.EAPOL;
35import org.onlab.packet.EthType;
Ari Saha89831742015-06-26 10:31:48 -070036import org.onlab.packet.Ethernet;
Ari Saha89831742015-06-26 10:31:48 -070037import org.onlab.packet.MacAddress;
Jonathan Harta46dddf2015-06-30 15:31:20 -070038import org.onlab.packet.RADIUS;
39import org.onlab.packet.RADIUSAttribute;
Ari Saha89831742015-06-26 10:31:48 -070040import org.onosproject.core.ApplicationId;
41import org.onosproject.core.CoreService;
42import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.DeviceId;
Ari Saha89831742015-06-26 10:31:48 -070044import org.onosproject.net.PortNumber;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070045import org.onosproject.net.config.ConfigFactory;
46import org.onosproject.net.config.NetworkConfigEvent;
47import org.onosproject.net.config.NetworkConfigListener;
48import org.onosproject.net.config.NetworkConfigRegistry;
Ari Saha89831742015-06-26 10:31:48 -070049import org.onosproject.net.flow.DefaultTrafficSelector;
50import org.onosproject.net.flow.DefaultTrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070051import org.onosproject.net.flow.TrafficSelector;
52import org.onosproject.net.flow.TrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070053import org.onosproject.net.packet.DefaultOutboundPacket;
54import org.onosproject.net.packet.InboundPacket;
55import org.onosproject.net.packet.OutboundPacket;
56import org.onosproject.net.packet.PacketContext;
Ari Saha89831742015-06-26 10:31:48 -070057import org.onosproject.net.packet.PacketProcessor;
58import org.onosproject.net.packet.PacketService;
Ari Saha89831742015-06-26 10:31:48 -070059import org.slf4j.Logger;
60
Ray Milkey508cec52016-02-16 14:23:26 -080061import com.google.common.util.concurrent.ThreadFactoryBuilder;
Ray Milkey967776a2015-10-07 14:37:17 -070062
Ray Milkeyfcb623d2015-10-01 16:48:18 -070063import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -070064import static org.onosproject.net.packet.PacketPriority.CONTROL;
Ari Saha89831742015-06-26 10:31:48 -070065import static org.slf4j.LoggerFactory.getLogger;
66
Ari Saha89831742015-06-26 10:31:48 -070067/**
Jonathan Harta46dddf2015-06-30 15:31:20 -070068 * AAA application for ONOS.
Ari Saha89831742015-06-26 10:31:48 -070069 */
70@Component(immediate = true)
Jonathan Hart092dfb22015-11-16 23:05:21 -080071public class AaaManager {
Ray Milkeyf51eba22015-09-25 10:24:23 -070072
Ray Milkeyf61a24e2015-09-24 16:34:02 -070073 // for verbose output
74 private final Logger log = getLogger(getClass());
Ray Milkeyf51eba22015-09-25 10:24:23 -070075
Ray Milkeyf61a24e2015-09-24 16:34:02 -070076 // a list of our dependencies :
77 // to register with ONOS as an application - described next
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected CoreService coreService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070080
Ray Milkeyf61a24e2015-09-24 16:34:02 -070081 // to receive Packet-in events that we'll respond to
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected PacketService packetService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070084
Ray Milkeyf61a24e2015-09-24 16:34:02 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkeyfcb623d2015-10-01 16:48:18 -070086 protected NetworkConfigRegistry netCfgService;
Ray Milkeyf51eba22015-09-25 10:24:23 -070087
Ray Milkeyfcb623d2015-10-01 16:48:18 -070088 // Parsed RADIUS server addresses
89 protected InetAddress radiusIpAddress;
90 protected String radiusMacAddress;
91
92 // NAS IP address
93 protected InetAddress nasIpAddress;
94 protected String nasMacAddress;
95
96 // RADIUS server secret
97 protected String radiusSecret;
98
99 // ID of RADIUS switch
100 protected String radiusSwitch;
101
102 // RADIUS port number
103 protected long radiusPort;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700104
Ray Milkey5d99bd12015-10-06 15:41:30 -0700105 // RADIUS server TCP port number
106 protected short radiusServerPort;
107
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700108 // our application-specific event handler
109 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700110
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700111 // our unique identifier
112 private ApplicationId appId;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700113
Ray Milkey967776a2015-10-07 14:37:17 -0700114 // Socket used for UDP communications with RADIUS server
115 private DatagramSocket radiusSocket;
116
117 // Executor for RADIUS communication thread
118 private ExecutorService executor;
119
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700120 // Configuration properties factory
121 private final ConfigFactory factory =
Jonathan Hart092dfb22015-11-16 23:05:21 -0800122 new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY,
123 AaaConfig.class,
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700124 "AAA") {
125 @Override
Jonathan Hart092dfb22015-11-16 23:05:21 -0800126 public AaaConfig createConfig() {
127 return new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700128 }
129 };
Ray Milkeyf51eba22015-09-25 10:24:23 -0700130
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700131 // Listener for config changes
132 private final InternalConfigListener cfgListener = new InternalConfigListener();
Ari Saha89831742015-06-26 10:31:48 -0700133
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700134 /**
135 * Builds an EAPOL packet based on the given parameters.
136 *
137 * @param dstMac destination MAC address
138 * @param srcMac source MAC address
139 * @param vlan vlan identifier
140 * @param eapolType EAPOL type
141 * @param eap EAP payload
142 * @return Ethernet frame
143 */
144 private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
145 short vlan, byte eapolType, EAP eap) {
Ari Saha89831742015-06-26 10:31:48 -0700146
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700147 Ethernet eth = new Ethernet();
148 eth.setDestinationMACAddress(dstMac.toBytes());
149 eth.setSourceMACAddress(srcMac.toBytes());
150 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
151 if (vlan != Ethernet.VLAN_UNTAGGED) {
152 eth.setVlanID(vlan);
153 }
154 //eapol header
155 EAPOL eapol = new EAPOL();
156 eapol.setEapolType(eapolType);
157 eapol.setPacketLength(eap.getLength());
Ari Saha89831742015-06-26 10:31:48 -0700158
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700159 //eap part
160 eapol.setPayload(eap);
161
162 eth.setPayload(eapol);
163 eth.setPad(true);
164 return eth;
165 }
Ari Saha89831742015-06-26 10:31:48 -0700166
Ray Milkeyfe4fe562016-01-05 13:14:43 -0800167 private void initializeLocalState() {
168 try {
A.R Karthick6af9f432016-03-11 20:24:33 -0800169 radiusSocket = new DatagramSocket(null);
170 radiusSocket.setReuseAddress(true);
171 radiusSocket.bind(new InetSocketAddress(radiusServerPort));
Ray Milkeyfe4fe562016-01-05 13:14:43 -0800172 } catch (Exception ex) {
173 log.error("Can't open RADIUS socket", ex);
174 }
175
176 executor = Executors.newSingleThreadExecutor(
177 new ThreadFactoryBuilder()
178 .setNameFormat("AAA-radius-%d").build());
179 executor.execute(radiusListener);
180 }
181
Ari Saha89831742015-06-26 10:31:48 -0700182 @Activate
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700183 public void activate() {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700184 netCfgService.registerConfigFactory(factory);
Ari Saha89831742015-06-26 10:31:48 -0700185 // "org.onosproject.aaa" is the FQDN of our app
186 appId = coreService.registerApplication("org.onosproject.aaa");
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700187
Jonathan Hart092dfb22015-11-16 23:05:21 -0800188 cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class));
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700189
Ari Saha89831742015-06-26 10:31:48 -0700190 // register our event handler
Brian O'Connord9c7da02015-07-29 17:49:24 -0700191 packetService.addProcessor(processor, PacketProcessor.director(2));
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700192 requestIntercepts();
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700193
194 StateMachine.initializeMaps();
Ari Saha89831742015-06-26 10:31:48 -0700195
Ray Milkeyfe4fe562016-01-05 13:14:43 -0800196 initializeLocalState();
alshabib27afc1d2016-12-23 00:33:58 -0800197 netCfgService.addListener(cfgListener);
198
Jian Li13c67162015-12-09 13:20:34 -0800199 log.info("Started");
Ari Saha89831742015-06-26 10:31:48 -0700200 }
201
202 @Deactivate
203 public void deactivate() {
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700204 withdrawIntercepts();
Ari Saha89831742015-06-26 10:31:48 -0700205 // de-register and null our handler
206 packetService.removeProcessor(processor);
207 processor = null;
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700208 StateMachine.destroyMaps();
Ray Milkey967776a2015-10-07 14:37:17 -0700209 radiusSocket.close();
210 executor.shutdownNow();
Jian Li13c67162015-12-09 13:20:34 -0800211 log.info("Stopped");
Ray Milkey967776a2015-10-07 14:37:17 -0700212 }
213
Jonathan Hart092dfb22015-11-16 23:05:21 -0800214 protected void sendRadiusPacket(RADIUS radiusPacket) {
Ray Milkey967776a2015-10-07 14:37:17 -0700215
216 try {
217 final byte[] data = radiusPacket.serialize();
218 final DatagramSocket socket = radiusSocket;
219
220 DatagramPacket packet =
221 new DatagramPacket(data, data.length,
222 radiusIpAddress, radiusServerPort);
223
224 socket.send(packet);
225 } catch (IOException e) {
226 log.info("Cannot send packet to RADIUS server", e);
227 }
Ari Saha89831742015-06-26 10:31:48 -0700228 }
229
Jonathan Harta46dddf2015-06-30 15:31:20 -0700230 /**
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700231 * Request packet in via PacketService.
232 */
233 private void requestIntercepts() {
234 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
235 selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
236 packetService.requestPackets(selector.build(),
237 CONTROL, appId);
238 }
239
240 /**
241 * Cancel request for packet in via PacketService.
242 */
243 private void withdrawIntercepts() {
244 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
245 selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
246 packetService.cancelPackets(selector.build(), CONTROL, appId);
247 }
248
Ray Milkey967776a2015-10-07 14:37:17 -0700249 /**
250 * Send the ethernet packet to the supplicant.
251 *
252 * @param ethernetPkt the ethernet packet
253 * @param connectPoint the connect point to send out
254 */
255 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) {
256 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
257 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
258 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
259 packetService.emit(packet);
260 }
261
Ari Saha89831742015-06-26 10:31:48 -0700262 // our handler defined as a private inner class
263
264 /**
265 * Packet processor responsible for forwarding packets along their paths.
266 */
267 private class ReactivePacketProcessor implements PacketProcessor {
268 @Override
269 public void process(PacketContext context) {
270
271 // Extract the original Ethernet frame from the packet information
272 InboundPacket pkt = context.inPacket();
273 Ethernet ethPkt = pkt.parsed();
274 if (ethPkt == null) {
275 return;
276 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700277 try {
278 // identify if incoming packet comes from supplicant (EAP) or RADIUS
279 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
280 case EAPOL:
281 handleSupplicantPacket(context.inPacket());
282 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700283 default:
284 log.trace("Skipping Ethernet packet type {}",
285 EthType.EtherType.lookup(ethPkt.getEtherType()));
286 }
Ray Milkey967776a2015-10-07 14:37:17 -0700287 } catch (StateMachineException e) {
Ray Milkeyf51eba22015-09-25 10:24:23 -0700288 log.warn("Unable to process RADIUS packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700289 }
290 }
291
Ray Milkey9eb293f2015-09-30 15:09:17 -0700292 /**
293 * Creates and initializes common fields of a RADIUS packet.
294 *
Ray Milkey967776a2015-10-07 14:37:17 -0700295 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700296 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700297 * @return RADIUS packet
298 */
Ray Milkey967776a2015-10-07 14:37:17 -0700299 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700300 RADIUS radiusPayload =
301 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
302 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700303
304 // set Request Authenticator in StateMachine
305 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
306
Ray Milkey9eb293f2015-09-30 15:09:17 -0700307 radiusPayload.setIdentifier(identifier);
308 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700309 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700310
311 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800312 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700313
314 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700315
316 return radiusPayload;
317 }
Ari Saha89831742015-06-26 10:31:48 -0700318
319 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700320 * Handles PAE packets (supplicant).
321 *
322 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700323 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700324 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700325 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700326 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800327 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700328
Jonathan Harta46dddf2015-06-30 15:31:20 -0700329 DeviceId deviceId = inPacket.receivedFrom().deviceId();
330 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700331 String sessionId = deviceId.toString() + portNumber.toString();
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700332 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
333 if (stateMachine == null) {
Ray Milkey508cec52016-02-16 14:23:26 -0800334 stateMachine = new StateMachine(sessionId);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700335 }
336
Jonathan Harta46dddf2015-06-30 15:31:20 -0700337
338 EAPOL eapol = (EAPOL) ethPkt.getPayload();
Ari Saha89831742015-06-26 10:31:48 -0700339
340 switch (eapol.getEapolType()) {
341 case EAPOL.EAPOL_START:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700342 stateMachine.start();
343 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Ari Saha89831742015-06-26 10:31:48 -0700344
Ray Milkeyf51eba22015-09-25 10:24:23 -0700345 //send an EAP Request/Identify to the supplicant
346 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800347 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700348 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
349 eapPayload);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800350 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700351 stateMachine.setVlanId(ethPkt.getVlanID());
Ari Saha89831742015-06-26 10:31:48 -0700352
Ray Milkey967776a2015-10-07 14:37:17 -0700353 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700354
355 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800356 case EAPOL.EAPOL_LOGOFF:
Ray Milkeyb34b4962016-01-04 10:24:43 -0800357 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
358 stateMachine.logoff();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800359 }
360
361 break;
Ari Saha89831742015-06-26 10:31:48 -0700362 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700363 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700364 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700365 EAP eapPacket = (EAP) eapol.getPayload();
366
367 byte dataType = eapPacket.getDataType();
368 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700369
Ray Milkey9eb293f2015-09-30 15:09:17 -0700370 case EAP.ATTR_IDENTITY:
371 // request id access to RADIUS
372 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700373
Ray Milkey967776a2015-10-07 14:37:17 -0700374 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800375 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700376
Jonathan Hart092dfb22015-11-16 23:05:21 -0800377 sendRadiusPacket(radiusPayload);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700378
Ray Milkey9eb293f2015-09-30 15:09:17 -0700379 // change the state to "PENDING"
380 stateMachine.requestAccess();
381 break;
Ari Saha89831742015-06-26 10:31:48 -0700382 case EAP.ATTR_MD5:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700383 // verify if the EAP identifier corresponds to the
384 // challenge identifier from the client state
385 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700386 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700387 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700388 radiusPayload =
389 getRadiusPayload(stateMachine,
390 stateMachine.identifier(),
391 eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700392
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800393 if (stateMachine.challengeState() != null) {
394 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
395 stateMachine.challengeState());
396 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800397 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
398 sendRadiusPacket(radiusPayload);
Ari Saha89831742015-06-26 10:31:48 -0700399 }
400 break;
401 case EAP.ATTR_TLS:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700402 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700403 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700404
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800405 if (stateMachine.challengeState() != null) {
406 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
407 stateMachine.challengeState());
408 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700409 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700410
Jonathan Hart092dfb22015-11-16 23:05:21 -0800411 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
412 sendRadiusPacket(radiusPayload);
Ray Milkey5493b512015-10-21 12:13:49 -0700413
Ray Milkeyf3790b82015-10-21 16:28:08 -0700414 if (stateMachine.state() != StateMachine.STATE_PENDING) {
415 stateMachine.requestAccess();
416 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700417
Ari Saha89831742015-06-26 10:31:48 -0700418 break;
419 default:
420 return;
421 }
422 break;
423 default:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700424 log.trace("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700425 }
Ray Milkey967776a2015-10-07 14:37:17 -0700426
Ari Saha89831742015-06-26 10:31:48 -0700427 }
Ray Milkey967776a2015-10-07 14:37:17 -0700428 }
429
430 class RadiusListener implements Runnable {
Ari Saha89831742015-06-26 10:31:48 -0700431
432 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700433 * Handles RADIUS packets.
434 *
Ari Saha89831742015-06-26 10:31:48 -0700435 * @param radiusPacket RADIUS packet coming from the RADIUS server.
Ray Milkey17d6c492015-10-27 10:39:42 -0700436 * @throws StateMachineException if an illegal state transition is triggered
Ari Saha89831742015-06-26 10:31:48 -0700437 */
Ray Milkey967776a2015-10-07 14:37:17 -0700438 protected void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException {
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700439 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
Ari Saha89831742015-06-26 10:31:48 -0700440 if (stateMachine == null) {
441 log.error("Invalid session identifier, exiting...");
442 return;
443 }
444
Ray Milkeyf51eba22015-09-25 10:24:23 -0700445 EAP eapPayload;
446 Ethernet eth;
Ari Saha89831742015-06-26 10:31:48 -0700447 switch (radiusPacket.getCode()) {
448 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800449 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
450 byte[] challengeState = null;
451 if (radiusAttrState != null) {
452 challengeState = radiusAttrState.getValue();
453 }
Ari Saha89831742015-06-26 10:31:48 -0700454 eapPayload = radiusPacket.decapsulateMessage();
455 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700456 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700457 MacAddress.valueOf(nasMacAddress),
458 stateMachine.vlanId(),
459 EAPOL.EAPOL_PACKET,
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700460 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700461 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700462 break;
463 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700464 //send an EAPOL - Success to the supplicant.
ke han04e47f32016-10-28 14:15:43 +0800465 byte[] eapMessageSuccess =
Ray Milkeyf51eba22015-09-25 10:24:23 -0700466 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
467 eapPayload = new EAP();
ke han04e47f32016-10-28 14:15:43 +0800468 eapPayload = (EAP) eapPayload.deserialize(eapMessageSuccess, 0, eapMessageSuccess.length);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700469 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700470 MacAddress.valueOf(nasMacAddress),
471 stateMachine.vlanId(),
472 EAPOL.EAPOL_PACKET,
Ray Milkeyf51eba22015-09-25 10:24:23 -0700473 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700474 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700475
Ray Milkeyf51eba22015-09-25 10:24:23 -0700476 stateMachine.authorizeAccess();
Ari Saha89831742015-06-26 10:31:48 -0700477 break;
478 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
ke han04e47f32016-10-28 14:15:43 +0800479 //send an EAPOL - Failure to the supplicant.
ke han5dcfa522016-11-29 14:21:30 +0800480 byte[] eapMessageFailure;
ke han04e47f32016-10-28 14:15:43 +0800481 eapPayload = new EAP();
ke han5dcfa522016-11-29 14:21:30 +0800482 RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE);
483 if (radiusAttrEap == null) {
484 eapPayload.setCode(EAP.FAILURE);
485 eapPayload.setIdentifier(stateMachine.challengeIdentifier());
486 eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL);
487 } else {
488 eapMessageFailure = radiusAttrEap.getValue();
489 eapPayload = (EAP) eapPayload.deserialize(eapMessageFailure, 0, eapMessageFailure.length);
490 }
ke han04e47f32016-10-28 14:15:43 +0800491 eth = buildEapolResponse(stateMachine.supplicantAddress(),
492 MacAddress.valueOf(nasMacAddress),
493 stateMachine.vlanId(),
494 EAPOL.EAPOL_PACKET,
495 eapPayload);
496 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
ke han5dcfa522016-11-29 14:21:30 +0800497 stateMachine.denyAccess();
Ari Saha89831742015-06-26 10:31:48 -0700498 break;
499 default:
500 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
501 }
502 }
503
Ari Saha89831742015-06-26 10:31:48 -0700504
Ray Milkey967776a2015-10-07 14:37:17 -0700505 @Override
506 public void run() {
507 boolean done = false;
508 int packetNumber = 1;
509
510 log.info("UDP listener thread starting up");
511 RADIUS inboundRadiusPacket;
512 while (!done) {
513 try {
Ray Milkey5493b512015-10-21 12:13:49 -0700514 byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH];
515 DatagramPacket inboundBasePacket =
516 new DatagramPacket(packetBuffer, packetBuffer.length);
Ray Milkey967776a2015-10-07 14:37:17 -0700517 DatagramSocket socket = radiusSocket;
518 socket.receive(inboundBasePacket);
519 log.info("Packet #{} received", packetNumber++);
520 try {
521 inboundRadiusPacket =
522 RADIUS.deserializer()
523 .deserialize(inboundBasePacket.getData(),
524 0,
525 inboundBasePacket.getLength());
526 handleRadiusPacket(inboundRadiusPacket);
527 } catch (DeserializationException dex) {
528 log.error("Cannot deserialize packet", dex);
529 } catch (StateMachineException sme) {
530 log.error("Illegal state machine operation", sme);
531 }
532
533 } catch (IOException e) {
534 log.info("Socket was closed, exiting listener thread");
535 done = true;
536 }
Ari Saha89831742015-06-26 10:31:48 -0700537 }
Ari Saha89831742015-06-26 10:31:48 -0700538 }
Ari Saha89831742015-06-26 10:31:48 -0700539 }
540
Ray Milkey967776a2015-10-07 14:37:17 -0700541 RadiusListener radiusListener = new RadiusListener();
542
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700543 private class InternalConfigListener implements NetworkConfigListener {
544
545 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800546 * Reconfigures the AAA application according to the
547 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700548 *
549 * @param cfg configuration object
550 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800551 private void reconfigureNetwork(AaaConfig cfg) {
552 AaaConfig newCfg;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700553 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800554 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700555 } else {
556 newCfg = cfg;
557 }
558 if (newCfg.nasIp() != null) {
559 nasIpAddress = newCfg.nasIp();
560 }
561 if (newCfg.radiusIp() != null) {
562 radiusIpAddress = newCfg.radiusIp();
563 }
564 if (newCfg.radiusMac() != null) {
565 radiusMacAddress = newCfg.radiusMac();
566 }
567 if (newCfg.nasMac() != null) {
568 nasMacAddress = newCfg.nasMac();
569 }
570 if (newCfg.radiusSecret() != null) {
571 radiusSecret = newCfg.radiusSecret();
572 }
573 if (newCfg.radiusSwitch() != null) {
574 radiusSwitch = newCfg.radiusSwitch();
575 }
576 if (newCfg.radiusPort() != -1) {
577 radiusPort = newCfg.radiusPort();
578 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800579 if (newCfg.radiusServerUdpPort() != -1) {
580 radiusServerPort = newCfg.radiusServerUdpPort();
Ray Milkey5d99bd12015-10-06 15:41:30 -0700581 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700582 }
583
584 @Override
585 public void event(NetworkConfigEvent event) {
586
587 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
588 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800589 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700590
Jonathan Hart092dfb22015-11-16 23:05:21 -0800591 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700592 reconfigureNetwork(cfg);
Ray Milkeyfe4fe562016-01-05 13:14:43 -0800593 radiusSocket.close();
594 executor.shutdownNow();
595 initializeLocalState();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700596 log.info("Reconfigured");
597 }
598 }
599 }
600
601
Ari Saha89831742015-06-26 10:31:48 -0700602}