blob: dd324eee5627afee09c12886da3397d0873902fa [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);
Ari Saha89831742015-06-26 10:31:48 -0700195 }
196
197 @Deactivate
198 public void deactivate() {
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700199 appId = coreService.registerApplication("org.onosproject.aaa");
200 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();
207 }
208
Jonathan Hart092dfb22015-11-16 23:05:21 -0800209 protected void sendRadiusPacket(RADIUS radiusPacket) {
Ray Milkey967776a2015-10-07 14:37:17 -0700210
211 try {
212 final byte[] data = radiusPacket.serialize();
213 final DatagramSocket socket = radiusSocket;
214
215 DatagramPacket packet =
216 new DatagramPacket(data, data.length,
217 radiusIpAddress, radiusServerPort);
218
219 socket.send(packet);
220 } catch (IOException e) {
221 log.info("Cannot send packet to RADIUS server", e);
222 }
Ari Saha89831742015-06-26 10:31:48 -0700223 }
224
Jonathan Harta46dddf2015-06-30 15:31:20 -0700225 /**
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700226 * Request packet in via PacketService.
227 */
228 private void requestIntercepts() {
229 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
230 selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
231 packetService.requestPackets(selector.build(),
232 CONTROL, appId);
233 }
234
235 /**
236 * Cancel request for packet in via PacketService.
237 */
238 private void withdrawIntercepts() {
239 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
240 selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
241 packetService.cancelPackets(selector.build(), CONTROL, appId);
242 }
243
Ray Milkey967776a2015-10-07 14:37:17 -0700244 /**
245 * Send the ethernet packet to the supplicant.
246 *
247 * @param ethernetPkt the ethernet packet
248 * @param connectPoint the connect point to send out
249 */
250 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) {
251 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
252 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
253 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
254 packetService.emit(packet);
255 }
256
Ari Saha89831742015-06-26 10:31:48 -0700257 // our handler defined as a private inner class
258
259 /**
260 * Packet processor responsible for forwarding packets along their paths.
261 */
262 private class ReactivePacketProcessor implements PacketProcessor {
263 @Override
264 public void process(PacketContext context) {
265
266 // Extract the original Ethernet frame from the packet information
267 InboundPacket pkt = context.inPacket();
268 Ethernet ethPkt = pkt.parsed();
269 if (ethPkt == null) {
270 return;
271 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700272 try {
273 // identify if incoming packet comes from supplicant (EAP) or RADIUS
274 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
275 case EAPOL:
276 handleSupplicantPacket(context.inPacket());
277 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700278 default:
279 log.trace("Skipping Ethernet packet type {}",
280 EthType.EtherType.lookup(ethPkt.getEtherType()));
281 }
Ray Milkey967776a2015-10-07 14:37:17 -0700282 } catch (StateMachineException e) {
Ray Milkeyf51eba22015-09-25 10:24:23 -0700283 log.warn("Unable to process RADIUS packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700284 }
285 }
286
Ray Milkey9eb293f2015-09-30 15:09:17 -0700287 /**
288 * Creates and initializes common fields of a RADIUS packet.
289 *
Ray Milkey967776a2015-10-07 14:37:17 -0700290 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700291 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700292 * @return RADIUS packet
293 */
Ray Milkey967776a2015-10-07 14:37:17 -0700294 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700295 RADIUS radiusPayload =
296 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
297 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700298
299 // set Request Authenticator in StateMachine
300 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
301
Ray Milkey9eb293f2015-09-30 15:09:17 -0700302 radiusPayload.setIdentifier(identifier);
303 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700304 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700305
306 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800307 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700308
309 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700310
311 return radiusPayload;
312 }
Ari Saha89831742015-06-26 10:31:48 -0700313
314 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700315 * Handles PAE packets (supplicant).
316 *
317 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700318 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700319 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700320 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700321 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800322 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700323
Jonathan Harta46dddf2015-06-30 15:31:20 -0700324 DeviceId deviceId = inPacket.receivedFrom().deviceId();
325 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700326 String sessionId = deviceId.toString() + portNumber.toString();
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700327 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
328 if (stateMachine == null) {
329 stateMachine = new StateMachine(sessionId, voltTenantService);
330 }
331
Jonathan Harta46dddf2015-06-30 15:31:20 -0700332
333 EAPOL eapol = (EAPOL) ethPkt.getPayload();
Ari Saha89831742015-06-26 10:31:48 -0700334
335 switch (eapol.getEapolType()) {
336 case EAPOL.EAPOL_START:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700337 stateMachine.start();
338 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Ari Saha89831742015-06-26 10:31:48 -0700339
Ray Milkeyf51eba22015-09-25 10:24:23 -0700340 //send an EAP Request/Identify to the supplicant
341 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800342 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700343 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
344 eapPayload);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800345 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700346 stateMachine.setVlanId(ethPkt.getVlanID());
Ari Saha89831742015-06-26 10:31:48 -0700347
Ray Milkey967776a2015-10-07 14:37:17 -0700348 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700349
350 break;
351 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700352 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700353 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700354 EAP eapPacket = (EAP) eapol.getPayload();
355
356 byte dataType = eapPacket.getDataType();
357 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700358
Ray Milkey9eb293f2015-09-30 15:09:17 -0700359 case EAP.ATTR_IDENTITY:
360 // request id access to RADIUS
361 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700362
Ray Milkey967776a2015-10-07 14:37:17 -0700363 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800364 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700365
Jonathan Hart092dfb22015-11-16 23:05:21 -0800366 sendRadiusPacket(radiusPayload);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700367
Ray Milkey9eb293f2015-09-30 15:09:17 -0700368 // change the state to "PENDING"
369 stateMachine.requestAccess();
370 break;
Ari Saha89831742015-06-26 10:31:48 -0700371 case EAP.ATTR_MD5:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700372 // verify if the EAP identifier corresponds to the
373 // challenge identifier from the client state
374 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700375 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700376 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700377 radiusPayload =
378 getRadiusPayload(stateMachine,
379 stateMachine.identifier(),
380 eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700381
382 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700383 stateMachine.challengeState());
Jonathan Hart092dfb22015-11-16 23:05:21 -0800384 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
385 sendRadiusPacket(radiusPayload);
Ari Saha89831742015-06-26 10:31:48 -0700386 }
387 break;
388 case EAP.ATTR_TLS:
Ray Milkey9eb293f2015-09-30 15:09:17 -0700389 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700390 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Ari Saha89831742015-06-26 10:31:48 -0700391
Ray Milkeyf51eba22015-09-25 10:24:23 -0700392 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800393 stateMachine.challengeState());
Ray Milkeyf51eba22015-09-25 10:24:23 -0700394 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700395
Jonathan Hart092dfb22015-11-16 23:05:21 -0800396 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
397 sendRadiusPacket(radiusPayload);
Ray Milkey5493b512015-10-21 12:13:49 -0700398
Ray Milkeyf3790b82015-10-21 16:28:08 -0700399 if (stateMachine.state() != StateMachine.STATE_PENDING) {
400 stateMachine.requestAccess();
401 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700402
Ari Saha89831742015-06-26 10:31:48 -0700403 break;
404 default:
405 return;
406 }
407 break;
408 default:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700409 log.trace("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700410 }
Ray Milkey967776a2015-10-07 14:37:17 -0700411
Ari Saha89831742015-06-26 10:31:48 -0700412 }
Ray Milkey967776a2015-10-07 14:37:17 -0700413 }
414
415 class RadiusListener implements Runnable {
Ari Saha89831742015-06-26 10:31:48 -0700416
417 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700418 * Handles RADIUS packets.
419 *
Ari Saha89831742015-06-26 10:31:48 -0700420 * @param radiusPacket RADIUS packet coming from the RADIUS server.
Ray Milkey17d6c492015-10-27 10:39:42 -0700421 * @throws StateMachineException if an illegal state transition is triggered
Ari Saha89831742015-06-26 10:31:48 -0700422 */
Ray Milkey967776a2015-10-07 14:37:17 -0700423 protected void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException {
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700424 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
Ari Saha89831742015-06-26 10:31:48 -0700425 if (stateMachine == null) {
426 log.error("Invalid session identifier, exiting...");
427 return;
428 }
429
Ray Milkeyf51eba22015-09-25 10:24:23 -0700430 EAP eapPayload;
431 Ethernet eth;
Ari Saha89831742015-06-26 10:31:48 -0700432 switch (radiusPacket.getCode()) {
433 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Ray Milkey967776a2015-10-07 14:37:17 -0700434 byte[] challengeState =
435 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue();
Ari Saha89831742015-06-26 10:31:48 -0700436 eapPayload = radiusPacket.decapsulateMessage();
437 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700438 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700439 MacAddress.valueOf(nasMacAddress),
440 stateMachine.vlanId(),
441 EAPOL.EAPOL_PACKET,
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700442 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700443 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700444 break;
445 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700446 //send an EAPOL - Success to the supplicant.
447 byte[] eapMessage =
448 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
449 eapPayload = new EAP();
450 eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length);
451 eth = buildEapolResponse(stateMachine.supplicantAddress(),
Ray Milkey967776a2015-10-07 14:37:17 -0700452 MacAddress.valueOf(nasMacAddress),
453 stateMachine.vlanId(),
454 EAPOL.EAPOL_PACKET,
Ray Milkeyf51eba22015-09-25 10:24:23 -0700455 eapPayload);
Ray Milkey967776a2015-10-07 14:37:17 -0700456 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700457
Ray Milkeyf51eba22015-09-25 10:24:23 -0700458 stateMachine.authorizeAccess();
Ari Saha89831742015-06-26 10:31:48 -0700459 break;
460 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700461 stateMachine.denyAccess();
Ari Saha89831742015-06-26 10:31:48 -0700462 break;
463 default:
464 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
465 }
466 }
467
Ari Saha89831742015-06-26 10:31:48 -0700468
Ray Milkey967776a2015-10-07 14:37:17 -0700469 @Override
470 public void run() {
471 boolean done = false;
472 int packetNumber = 1;
473
474 log.info("UDP listener thread starting up");
475 RADIUS inboundRadiusPacket;
476 while (!done) {
477 try {
Ray Milkey5493b512015-10-21 12:13:49 -0700478 byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH];
479 DatagramPacket inboundBasePacket =
480 new DatagramPacket(packetBuffer, packetBuffer.length);
Ray Milkey967776a2015-10-07 14:37:17 -0700481 DatagramSocket socket = radiusSocket;
482 socket.receive(inboundBasePacket);
483 log.info("Packet #{} received", packetNumber++);
484 try {
485 inboundRadiusPacket =
486 RADIUS.deserializer()
487 .deserialize(inboundBasePacket.getData(),
488 0,
489 inboundBasePacket.getLength());
490 handleRadiusPacket(inboundRadiusPacket);
491 } catch (DeserializationException dex) {
492 log.error("Cannot deserialize packet", dex);
493 } catch (StateMachineException sme) {
494 log.error("Illegal state machine operation", sme);
495 }
496
497 } catch (IOException e) {
498 log.info("Socket was closed, exiting listener thread");
499 done = true;
500 }
Ari Saha89831742015-06-26 10:31:48 -0700501 }
Ari Saha89831742015-06-26 10:31:48 -0700502 }
Ari Saha89831742015-06-26 10:31:48 -0700503 }
504
Ray Milkey967776a2015-10-07 14:37:17 -0700505 RadiusListener radiusListener = new RadiusListener();
506
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700507 private class InternalConfigListener implements NetworkConfigListener {
508
509 /**
510 * Reconfigures the DHCP Server according to the configuration parameters passed.
511 *
512 * @param cfg configuration object
513 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800514 private void reconfigureNetwork(AaaConfig cfg) {
515 AaaConfig newCfg;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700516 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800517 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700518 } else {
519 newCfg = cfg;
520 }
521 if (newCfg.nasIp() != null) {
522 nasIpAddress = newCfg.nasIp();
523 }
524 if (newCfg.radiusIp() != null) {
525 radiusIpAddress = newCfg.radiusIp();
526 }
527 if (newCfg.radiusMac() != null) {
528 radiusMacAddress = newCfg.radiusMac();
529 }
530 if (newCfg.nasMac() != null) {
531 nasMacAddress = newCfg.nasMac();
532 }
533 if (newCfg.radiusSecret() != null) {
534 radiusSecret = newCfg.radiusSecret();
535 }
536 if (newCfg.radiusSwitch() != null) {
537 radiusSwitch = newCfg.radiusSwitch();
538 }
539 if (newCfg.radiusPort() != -1) {
540 radiusPort = newCfg.radiusPort();
541 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800542 if (newCfg.radiusServerUdpPort() != -1) {
543 radiusServerPort = newCfg.radiusServerUdpPort();
Ray Milkey5d99bd12015-10-06 15:41:30 -0700544 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700545 }
546
547 @Override
548 public void event(NetworkConfigEvent event) {
549
550 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
551 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800552 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700553
Jonathan Hart092dfb22015-11-16 23:05:21 -0800554 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700555 reconfigureNetwork(cfg);
556 log.info("Reconfigured");
557 }
558 }
559 }
560
561
Ari Saha89831742015-06-26 10:31:48 -0700562}