blob: 55239a5959c795ee525e3f523a06f5396597bfcc [file] [log] [blame]
Ari Saha89831742015-06-26 10:31:48 -07001/*
Amit Ghoshc9ac1e52017-07-28 12:31:18 +01002 * Copyright 2017-present Open Networking Foundation
Ari Saha89831742015-06-26 10:31:48 -07003 *
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 */
Matteo Scandolocf847b82019-04-26 15:00:00 -070016package org.opencord.aaa.impl;
Ari Saha89831742015-06-26 10:31:48 -070017
Saurav Das987441a2018-09-18 16:33:47 -070018import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.net.InetAddress;
kartikey dubeye1545422019-05-22 12:53:45 +000022import java.net.UnknownHostException;
Saurav Das987441a2018-09-18 16:33:47 -070023import java.nio.ByteBuffer;
24import java.util.Map;
kartikey dubeye1545422019-05-22 12:53:45 +000025import java.util.Dictionary;
26import java.util.HashSet;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000027import java.util.Arrays;
Saurav Das987441a2018-09-18 16:33:47 -070028
Jonathan Hart932bedc2018-07-12 13:46:09 -070029import org.apache.commons.lang3.builder.ToStringBuilder;
Ari Saha89831742015-06-26 10:31:48 -070030import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
kartikey dubeye1545422019-05-22 12:53:45 +000032import org.apache.felix.scr.annotations.Property;
Ari Saha89831742015-06-26 10:31:48 -070033import org.apache.felix.scr.annotations.Reference;
34import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart5db44532018-07-12 18:13:54 -070035import org.apache.felix.scr.annotations.Service;
Jonathan Hart4731dd92018-05-02 17:30:05 -070036import org.onlab.packet.DeserializationException;
Jonathan Harta46dddf2015-06-30 15:31:20 -070037import org.onlab.packet.EAP;
38import org.onlab.packet.EAPOL;
39import org.onlab.packet.EthType;
Ari Saha89831742015-06-26 10:31:48 -070040import org.onlab.packet.Ethernet;
Ari Saha89831742015-06-26 10:31:48 -070041import org.onlab.packet.MacAddress;
Jonathan Harta46dddf2015-06-30 15:31:20 -070042import org.onlab.packet.RADIUS;
43import org.onlab.packet.RADIUSAttribute;
kartikey dubeye1545422019-05-22 12:53:45 +000044import org.onlab.util.Tools;
45import org.onosproject.cfg.ComponentConfigService;
Ari Saha89831742015-06-26 10:31:48 -070046import org.onosproject.core.ApplicationId;
47import org.onosproject.core.CoreService;
Jonathan Hart5db44532018-07-12 18:13:54 -070048import org.onosproject.event.AbstractListenerManager;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010049import org.onosproject.mastership.MastershipService;
Ari Saha89831742015-06-26 10:31:48 -070050import org.onosproject.net.ConnectPoint;
51import org.onosproject.net.DeviceId;
Ari Saha89831742015-06-26 10:31:48 -070052import org.onosproject.net.PortNumber;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070053import org.onosproject.net.config.ConfigFactory;
54import org.onosproject.net.config.NetworkConfigEvent;
55import org.onosproject.net.config.NetworkConfigListener;
56import org.onosproject.net.config.NetworkConfigRegistry;
Amit Ghoshf739be52017-09-21 15:49:37 +010057import org.onosproject.net.device.DeviceEvent;
58import org.onosproject.net.device.DeviceListener;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010059import org.onosproject.net.device.DeviceService;
Ari Saha89831742015-06-26 10:31:48 -070060import org.onosproject.net.flow.DefaultTrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070061import org.onosproject.net.flow.TrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070062import org.onosproject.net.packet.DefaultOutboundPacket;
63import org.onosproject.net.packet.InboundPacket;
64import org.onosproject.net.packet.OutboundPacket;
65import org.onosproject.net.packet.PacketContext;
Ari Saha89831742015-06-26 10:31:48 -070066import org.onosproject.net.packet.PacketProcessor;
67import org.onosproject.net.packet.PacketService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070068import org.opencord.aaa.AaaConfig;
69import org.opencord.aaa.AuthenticationEvent;
70import org.opencord.aaa.AuthenticationEventListener;
71import org.opencord.aaa.AuthenticationService;
kartikey dubeye1545422019-05-22 12:53:45 +000072import org.opencord.aaa.AuthenticationStatisticsEvent;
73import org.opencord.aaa.AuthenticationStatisticsService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070074import org.opencord.aaa.RadiusCommunicator;
75import org.opencord.aaa.StateMachineDelegate;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000076import org.opencord.sadis.BaseInformationService;
77import org.opencord.sadis.SadisService;
78import org.opencord.sadis.SubscriberAndDeviceInformation;
kartikey dubeye1545422019-05-22 12:53:45 +000079import org.osgi.service.component.ComponentContext;
Kartikey Dubeyf72e1952019-06-24 07:09:00 +000080import org.apache.felix.scr.annotations.Modified;
81import org.apache.felix.scr.annotations.Activate;
Ari Saha89831742015-06-26 10:31:48 -070082import org.slf4j.Logger;
kartikey dubeye1545422019-05-22 12:53:45 +000083import com.google.common.base.Strings;
Ari Saha89831742015-06-26 10:31:48 -070084
kartikey dubeye1545422019-05-22 12:53:45 +000085import java.util.concurrent.Executors;
86import java.util.concurrent.ScheduledExecutorService;
87import java.util.concurrent.ScheduledFuture;
88import java.util.concurrent.TimeUnit;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000089
90import javax.crypto.Mac;
91import javax.crypto.spec.SecretKeySpec;
Ari Saha89831742015-06-26 10:31:48 -070092/**
Jonathan Harta46dddf2015-06-30 15:31:20 -070093 * AAA application for ONOS.
Ari Saha89831742015-06-26 10:31:48 -070094 */
Jonathan Hart5db44532018-07-12 18:13:54 -070095@Service
Ari Saha89831742015-06-26 10:31:48 -070096@Component(immediate = true)
Jonathan Hart5db44532018-07-12 18:13:54 -070097public class AaaManager
98 extends AbstractListenerManager<AuthenticationEvent, AuthenticationEventListener>
99 implements AuthenticationService {
100
Charles Chandf7ff862017-01-20 11:22:05 -0800101 private static final String APP_NAME = "org.opencord.aaa";
Ray Milkeyf51eba22015-09-25 10:24:23 -0700102
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700103 private final Logger log = getLogger(getClass());
Ray Milkeyf51eba22015-09-25 10:24:23 -0700104
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected CoreService coreService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700107
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected PacketService packetService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700110
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700112 protected NetworkConfigRegistry netCfgService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700113
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected DeviceService deviceService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000118 protected SadisService sadisService;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected MastershipService mastershipService;
122
kartikey dubeye1545422019-05-22 12:53:45 +0000123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected AuthenticationStatisticsService aaaStatisticsManager;
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000125
kartikey dubeye1545422019-05-22 12:53:45 +0000126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected ComponentConfigService cfgService;
128
129 protected AuthenticationStatisticsEventPublisher authenticationStatisticsPublisher;
130 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
Amit Ghoshf739be52017-09-21 15:49:37 +0100131 private final DeviceListener deviceListener = new InternalDeviceListener();
132
kartikey dubeye1545422019-05-22 12:53:45 +0000133 private static final int DEFAULT_REPEAT_DELAY = 20;
134 @Property(name = "statisticsGenerationEvent", intValue = DEFAULT_REPEAT_DELAY,
135 label = "statisticsGenerationEvent")
136 private int statisticsGenerationEvent = DEFAULT_REPEAT_DELAY;
137
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700138 // NAS IP address
139 protected InetAddress nasIpAddress;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100140
141 // self MAC address
Jonathan Hart5db44532018-07-12 18:13:54 -0700142 protected String nasMacAddress;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100143
144 // Parsed RADIUS server addresses
145 protected InetAddress radiusIpAddress;
146
147 // MAC address of RADIUS server or net hop router
148 protected String radiusMacAddress;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700149
150 // RADIUS server secret
151 protected String radiusSecret;
152
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100153 // bindings
154 protected CustomizationInfo customInfo;
Ray Milkey5d99bd12015-10-06 15:41:30 -0700155
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700156 // our application-specific event handler
157 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700158
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700159 // our unique identifier
160 private ApplicationId appId;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700161
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000162 // TimeOut time for cleaning up stateMachines stuck due to pending AAA/EAPOL message.
163 protected int cleanupTimerTimeOutInMins;
164
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100165 // Setup specific customization/attributes on the RADIUS packets
166 PacketCustomizer pktCustomizer;
Ray Milkey967776a2015-10-07 14:37:17 -0700167
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100168 // packet customizer to use
169 private String customizer;
170
171 // Type of connection to use to communicate with Radius server, options are
172 // "socket" or "packet_out"
173 private String radiusConnectionType;
174
Jonathan Hart5db44532018-07-12 18:13:54 -0700175 // Object for the specific type of communication with the RADIUS
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100176 // server, socket based or packet_out based
177 RadiusCommunicator impl = null;
178
179 // latest configuration
180 AaaConfig newCfg;
Ray Milkey967776a2015-10-07 14:37:17 -0700181
kartikey dubeye1545422019-05-22 12:53:45 +0000182 ScheduledFuture<?> scheduledFuture;
183
184 ScheduledExecutorService executor;
185 String configuredAaaServerAddress;
186 HashSet<Byte> outPacketSet = new HashSet<Byte>();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700187 // Configuration properties factory
188 private final ConfigFactory factory =
Jonathan Hart092dfb22015-11-16 23:05:21 -0800189 new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY,
190 AaaConfig.class,
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700191 "AAA") {
192 @Override
Jonathan Hart092dfb22015-11-16 23:05:21 -0800193 public AaaConfig createConfig() {
194 return new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700195 }
196 };
Ray Milkeyf51eba22015-09-25 10:24:23 -0700197
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700198 // Listener for config changes
199 private final InternalConfigListener cfgListener = new InternalConfigListener();
Ari Saha89831742015-06-26 10:31:48 -0700200
Jonathan Hart5db44532018-07-12 18:13:54 -0700201 private StateMachineDelegate delegate = new InternalStateMachineDelegate();
202
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700203 /**
204 * Builds an EAPOL packet based on the given parameters.
205 *
206 * @param dstMac destination MAC address
207 * @param srcMac source MAC address
208 * @param vlan vlan identifier
209 * @param eapolType EAPOL type
210 * @param eap EAP payload
211 * @return Ethernet frame
212 */
213 private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100214 short vlan, byte eapolType, EAP eap, byte priorityCode) {
Ari Saha89831742015-06-26 10:31:48 -0700215
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700216 Ethernet eth = new Ethernet();
217 eth.setDestinationMACAddress(dstMac.toBytes());
218 eth.setSourceMACAddress(srcMac.toBytes());
219 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
220 if (vlan != Ethernet.VLAN_UNTAGGED) {
221 eth.setVlanID(vlan);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100222 eth.setPriorityCode(priorityCode);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700223 }
224 //eapol header
225 EAPOL eapol = new EAPOL();
226 eapol.setEapolType(eapolType);
227 eapol.setPacketLength(eap.getLength());
Ari Saha89831742015-06-26 10:31:48 -0700228
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700229 //eap part
230 eapol.setPayload(eap);
231
232 eth.setPayload(eapol);
233 eth.setPad(true);
234 return eth;
235 }
Ari Saha89831742015-06-26 10:31:48 -0700236
Ari Saha89831742015-06-26 10:31:48 -0700237 @Activate
kartikey dubeye1545422019-05-22 12:53:45 +0000238 public void activate(ComponentContext context) {
Charles Chandf7ff862017-01-20 11:22:05 -0800239 appId = coreService.registerApplication(APP_NAME);
Jonathan Hart5db44532018-07-12 18:13:54 -0700240 eventDispatcher.addSink(AuthenticationEvent.class, listenerRegistry);
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400241 netCfgService.addListener(cfgListener);
242 netCfgService.registerConfigFactory(factory);
kartikey dubeye1545422019-05-22 12:53:45 +0000243 cfgService.registerProperties(getClass());
244 modified(context);
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000245 subsService = sadisService.getSubscriberInfoService();
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530246 customInfo = new CustomizationInfo(subsService, deviceService);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800247 cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class));
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400248 log.info("Starting with config {} {}", this, newCfg);
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530249 configureRadiusCommunication();
Ari Saha89831742015-06-26 10:31:48 -0700250 // register our event handler
Brian O'Connord9c7da02015-07-29 17:49:24 -0700251 packetService.addProcessor(processor, PacketProcessor.director(2));
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700252 StateMachine.initializeMaps();
Jonathan Hart5db44532018-07-12 18:13:54 -0700253 StateMachine.setDelegate(delegate);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000254 cleanupTimerTimeOutInMins = newCfg.sessionCleanupTimer();
255 StateMachine.setcleanupTimerTimeOutInMins(cleanupTimerTimeOutInMins);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100256 impl.initializeLocalState(newCfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100257 impl.requestIntercepts();
Amit Ghoshf739be52017-09-21 15:49:37 +0100258 deviceService.addListener(deviceListener);
kartikey dubeye1545422019-05-22 12:53:45 +0000259 getConfiguredAaaServerAddress();
260 authenticationStatisticsPublisher =
261 new AuthenticationStatisticsEventPublisher();
262 executor = Executors.newScheduledThreadPool(1);
263 scheduledFuture = executor.scheduleAtFixedRate(authenticationStatisticsPublisher,
264 0, statisticsGenerationEvent, TimeUnit.SECONDS);
Amit Ghoshf739be52017-09-21 15:49:37 +0100265
Jian Li13c67162015-12-09 13:20:34 -0800266 log.info("Started");
Ari Saha89831742015-06-26 10:31:48 -0700267 }
268
269 @Deactivate
kartikey dubeye1545422019-05-22 12:53:45 +0000270 public void deactivate(ComponentContext context) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100271 impl.withdrawIntercepts();
Ari Saha89831742015-06-26 10:31:48 -0700272 packetService.removeProcessor(processor);
Deepa Vaddireddyb9c24c62017-09-21 13:45:30 +0530273 netCfgService.removeListener(cfgListener);
kartikey dubeye1545422019-05-22 12:53:45 +0000274 cfgService.unregisterProperties(getClass(), false);
Jonathan Hart5db44532018-07-12 18:13:54 -0700275 StateMachine.unsetDelegate(delegate);
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530276 StateMachine.destroyMaps();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100277 impl.deactivate();
Amit Ghoshf739be52017-09-21 15:49:37 +0100278 deviceService.removeListener(deviceListener);
Jonathan Hart5db44532018-07-12 18:13:54 -0700279 eventDispatcher.removeSink(AuthenticationEvent.class);
kartikey dubeye1545422019-05-22 12:53:45 +0000280 scheduledFuture.cancel(true);
281 executor.shutdown();
Jian Li13c67162015-12-09 13:20:34 -0800282 log.info("Stopped");
Ray Milkey967776a2015-10-07 14:37:17 -0700283 }
284
kartikey dubeye1545422019-05-22 12:53:45 +0000285 @Modified
286 public void modified(ComponentContext context) {
287 Dictionary<?, ?> properties = context.getProperties();
288 String s = Tools.get(properties, "statisticsGenerationEvent");
289 statisticsGenerationEvent = Strings.isNullOrEmpty(s) ? DEFAULT_REPEAT_DELAY : Integer.parseInt(s.trim());
290 }
291
Shubham Sharma1ad16632019-11-26 11:09:21 +0000292 protected void configureRadiusCommunication() {
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530293 if (radiusConnectionType.toLowerCase().equals("socket")) {
294 impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
295 } else {
296 impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService,
297 deviceService, subsService, pktCustomizer, this);
298 }
299 }
300
301 private void configurePacketCustomizer() {
302 switch (customizer.toLowerCase()) {
303 case "sample":
304 pktCustomizer = new SamplePacketCustomizer(customInfo);
305 log.info("Created SamplePacketCustomizer");
306 break;
Saurav Dase72358a2018-11-13 21:56:46 -0800307 case "att":
308 pktCustomizer = new AttPacketCustomizer(customInfo);
309 log.info("Created AttPacketCustomizer");
310 break;
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530311 default:
312 pktCustomizer = new PacketCustomizer(customInfo);
313 log.info("Created default PacketCustomizer");
314 break;
315 }
316 }
317
kartikey dubeye1545422019-05-22 12:53:45 +0000318 private void getConfiguredAaaServerAddress() {
319 try {
320 InetAddress address;
321 if (newCfg.radiusHostName() != null) {
322 address = InetAddress.getByName(newCfg.radiusHostName());
323 } else {
324 address = newCfg.radiusIp();
325 }
326
327 configuredAaaServerAddress = address.getHostAddress();
328 } catch (UnknownHostException uhe) {
329 log.warn("Unable to resolve host {}", newCfg.radiusHostName());
330 }
331 }
332
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000333 private void checkReceivedPacketForValidValidator(RADIUS radiusPacket, byte[] requestAuthenticator) {
334 if (!checkResponseMessageAuthenticator(radiusSecret, radiusPacket, requestAuthenticator)) {
kartikey dubeye1545422019-05-22 12:53:45 +0000335 aaaStatisticsManager.getAaaStats().increaseInvalidValidatorsRx();
336 }
337 }
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000338
339 private boolean checkResponseMessageAuthenticator(String key, RADIUS radiusPacket, byte[] requestAuthenticator) {
340 byte[] newHash = new byte[16];
341 Arrays.fill(newHash, (byte) 0);
342 byte[] messageAuthenticator = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue();
343 byte[] authenticator = radiusPacket.getAuthenticator();
344 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash);
345 radiusPacket.setAuthenticator(requestAuthenticator);
346 // Calculate the MD5 HMAC based on the message
347 try {
348 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
349 Mac mac = Mac.getInstance("HmacMD5");
350 mac.init(keySpec);
351 newHash = mac.doFinal(radiusPacket.serialize());
352 } catch (Exception e) {
353 log.error("Failed to generate message authenticator: {}", e.getMessage());
354 }
355 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator);
356 radiusPacket.setAuthenticator(authenticator);
357 // Compare the calculated Message-Authenticator with the one in the message
358 return Arrays.equals(newHash, messageAuthenticator);
359 }
kartikey dubeye1545422019-05-22 12:53:45 +0000360 public void checkForPacketFromUnknownServer(String hostAddress) {
361 if (!hostAddress.equals(configuredAaaServerAddress)) {
Vijaykumar Kushwahafffd3d12019-08-01 11:09:59 +0000362 getConfiguredAaaServerAddress();
363 if (!hostAddress.equals(configuredAaaServerAddress)) {
364 aaaStatisticsManager.getAaaStats().incrementUnknownServerRx();
365 }
kartikey dubeye1545422019-05-22 12:53:45 +0000366 }
367 }
368
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100369 /**
370 * Send RADIUS packet to the RADIUS server.
371 *
372 * @param radiusPacket RADIUS packet to be sent to server.
373 * @param inPkt Incoming EAPOL packet
374 */
375 protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) {
kartikey dubeye1545422019-05-22 12:53:45 +0000376 outPacketSet.add(radiusPacket.getIdentifier());
377 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(true);
378 aaaStatisticsManager.getAaaStats().increaseAccessRequestsTx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100379 impl.sendRadiusPacket(radiusPacket, inPkt);
380 }
Ray Milkey967776a2015-10-07 14:37:17 -0700381
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000382 /**
383 * For scheduling the timer required for cleaning up StateMachine
384 * when no response
385 * from RADIUS SERVER.
386 *
387 * @param sessionId SessionId of the current session
388 * @param stateMachine StateMachine for the id
389 */
390 public void scheduleStateMachineCleanupTimer(String sessionId, StateMachine stateMachine) {
391 StateMachine.CleanupTimerTask cleanupTask = stateMachine.new CleanupTimerTask(sessionId, this);
392 ScheduledFuture<?> cleanupTimer = executor.schedule(cleanupTask, cleanupTimerTimeOutInMins, TimeUnit.MINUTES);
393 stateMachine.setCleanupTimer(cleanupTimer);
394
395 }
396
397 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100398 * Handles RADIUS packets.
399 *
400 * @param radiusPacket RADIUS packet coming from the RADIUS server.
401 * @throws StateMachineException if an illegal state transition is triggered
Jonathan Hart4731dd92018-05-02 17:30:05 -0700402 * @throws DeserializationException if packet deserialization fails
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100403 */
Jonathan Hart4731dd92018-05-02 17:30:05 -0700404 public void handleRadiusPacket(RADIUS radiusPacket)
405 throws StateMachineException, DeserializationException {
Saurav Das987441a2018-09-18 16:33:47 -0700406 if (log.isTraceEnabled()) {
407 log.trace("Received RADIUS packet {}", radiusPacket);
408 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100409 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
410 if (stateMachine == null) {
Saurav Das987441a2018-09-18 16:33:47 -0700411 log.error("Invalid packet identifier {}, could not find corresponding "
412 + "state machine ... exiting", radiusPacket.getIdentifier());
kartikey dubeye1545422019-05-22 12:53:45 +0000413 aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired();
414 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100415 return;
Ray Milkey967776a2015-10-07 14:37:17 -0700416 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100417 EAP eapPayload;
418 Ethernet eth;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000419 checkReceivedPacketForValidValidator(radiusPacket, stateMachine.requestAuthenticator());
kartikey dubeye1545422019-05-22 12:53:45 +0000420 if (outPacketSet.contains(radiusPacket.getIdentifier())) {
421 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(false);
422 outPacketSet.remove(new Byte(radiusPacket.getIdentifier()));
423 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100424 switch (radiusPacket.getCode()) {
425 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400426 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100427 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
428 byte[] challengeState = null;
429 if (radiusAttrState != null) {
430 challengeState = radiusAttrState.getValue();
431 }
432 eapPayload = radiusPacket.decapsulateMessage();
433 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
434 eth = buildEapolResponse(stateMachine.supplicantAddress(),
435 MacAddress.valueOf(nasMacAddress),
436 stateMachine.vlanId(),
437 EAPOL.EAPOL_PACKET,
438 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400439 log.debug("Send EAP challenge response to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000440 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), true);
kartikey dubeye1545422019-05-22 12:53:45 +0000441 aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100442 break;
443 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400444 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100445 //send an EAPOL - Success to the supplicant.
446 byte[] eapMessageSuccess =
447 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700448 eapPayload = EAP.deserializer().deserialize(
449 eapMessageSuccess, 0, eapMessageSuccess.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100450 eth = buildEapolResponse(stateMachine.supplicantAddress(),
451 MacAddress.valueOf(nasMacAddress),
452 stateMachine.vlanId(),
453 EAPOL.EAPOL_PACKET,
454 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400455 log.info("Send EAP success message to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000456 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
457 aaaStatisticsManager.getAaaStats().incrementEapolAuthSuccessTrans();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100458
459 stateMachine.authorizeAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000460 aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100461 break;
462 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400463 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100464 //send an EAPOL - Failure to the supplicant.
465 byte[] eapMessageFailure;
466 eapPayload = new EAP();
467 RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE);
468 if (radiusAttrEap == null) {
469 eapPayload.setCode(EAP.FAILURE);
470 eapPayload.setIdentifier(stateMachine.challengeIdentifier());
471 eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL);
472 } else {
473 eapMessageFailure = radiusAttrEap.getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700474 eapPayload = EAP.deserializer().deserialize(
475 eapMessageFailure, 0, eapMessageFailure.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100476 }
477 eth = buildEapolResponse(stateMachine.supplicantAddress(),
478 MacAddress.valueOf(nasMacAddress),
479 stateMachine.vlanId(),
480 EAPOL.EAPOL_PACKET,
481 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400482 log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000483 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
484 aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans();
485
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100486 stateMachine.denyAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000487 aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100488 break;
489 default:
490 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
kartikey dubeye1545422019-05-22 12:53:45 +0000491 aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100492 }
kartikey dubeye1545422019-05-22 12:53:45 +0000493 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700494 }
495
Ray Milkey967776a2015-10-07 14:37:17 -0700496 /**
497 * Send the ethernet packet to the supplicant.
498 *
499 * @param ethernetPkt the ethernet packet
500 * @param connectPoint the connect point to send out
501 */
Shubham Sharmac155ef82019-07-11 12:12:41 +0000502 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint, boolean isChallengeResponse) {
Ray Milkey967776a2015-10-07 14:37:17 -0700503 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
504 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
505 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
Shubham Sharmac155ef82019-07-11 12:12:41 +0000506 EAPOL eap = ((EAPOL) ethernetPkt.getPayload());
507 EAP eapPkt = (EAP) eap.getPayload();
Saurav Das987441a2018-09-18 16:33:47 -0700508 if (log.isTraceEnabled()) {
Saurav Das987441a2018-09-18 16:33:47 -0700509 log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}",
510 eap, ethernetPkt, connectPoint);
511 }
Ray Milkey967776a2015-10-07 14:37:17 -0700512 packetService.emit(packet);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000513 if (isChallengeResponse) {
514 aaaStatisticsManager.getAaaStats().incrementEapPktTxauthEap();
515 }
Ray Milkey967776a2015-10-07 14:37:17 -0700516 }
517
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400518 @Override
519 public String toString() {
520 return ToStringBuilder.reflectionToString(this);
521 }
522
Ari Saha89831742015-06-26 10:31:48 -0700523 // our handler defined as a private inner class
524
525 /**
526 * Packet processor responsible for forwarding packets along their paths.
527 */
528 private class ReactivePacketProcessor implements PacketProcessor {
529 @Override
530 public void process(PacketContext context) {
531
532 // Extract the original Ethernet frame from the packet information
533 InboundPacket pkt = context.inPacket();
534 Ethernet ethPkt = pkt.parsed();
535 if (ethPkt == null) {
536 return;
537 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100538
Ray Milkeyf51eba22015-09-25 10:24:23 -0700539 try {
540 // identify if incoming packet comes from supplicant (EAP) or RADIUS
541 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
542 case EAPOL:
543 handleSupplicantPacket(context.inPacket());
544 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700545 default:
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100546 // any other packets let the specific implementation handle
547 impl.handlePacketFromServer(context);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700548 }
Ray Milkey967776a2015-10-07 14:37:17 -0700549 } catch (StateMachineException e) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100550 log.warn("Unable to process packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700551 }
552 }
553
Ray Milkey9eb293f2015-09-30 15:09:17 -0700554 /**
555 * Creates and initializes common fields of a RADIUS packet.
556 *
Ray Milkey967776a2015-10-07 14:37:17 -0700557 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700558 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700559 * @return RADIUS packet
560 */
Ray Milkey967776a2015-10-07 14:37:17 -0700561 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700562 RADIUS radiusPayload =
563 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
564 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700565
566 // set Request Authenticator in StateMachine
567 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
568
Ray Milkey9eb293f2015-09-30 15:09:17 -0700569 radiusPayload.setIdentifier(identifier);
570 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700571 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700572
573 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800574 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700575
576 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700577
578 return radiusPayload;
579 }
Ari Saha89831742015-06-26 10:31:48 -0700580
581 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700582 * Handles PAE packets (supplicant).
583 *
584 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700585 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700586 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700587 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700588 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800589 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700590
Jonathan Harta46dddf2015-06-30 15:31:20 -0700591 DeviceId deviceId = inPacket.receivedFrom().deviceId();
592 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700593 String sessionId = deviceId.toString() + portNumber.toString();
Saurav Das987441a2018-09-18 16:33:47 -0700594 EAPOL eapol = (EAPOL) ethPkt.getPayload();
595 if (log.isTraceEnabled()) {
596 log.trace("Received EAPOL packet {} in enclosing packet {} from "
597 + "dev/port: {}/{}", eapol, ethPkt, deviceId,
598 portNumber);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100599 }
Jonathan Harta46dddf2015-06-30 15:31:20 -0700600
Saurav Das987441a2018-09-18 16:33:47 -0700601 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
602 if (stateMachine == null) {
603 log.debug("Creating new state machine for sessionId: {} for "
604 + "dev/port: {}/{}", sessionId, deviceId, portNumber);
605 stateMachine = new StateMachine(sessionId);
606 } else {
607 log.debug("Using existing state-machine for sessionId: {}", sessionId);
608 }
609
Ari Saha89831742015-06-26 10:31:48 -0700610 switch (eapol.getEapolType()) {
611 case EAPOL.EAPOL_START:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400612 log.debug("EAP packet: EAPOL_START");
Ray Milkeyf51eba22015-09-25 10:24:23 -0700613 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000614 if (stateMachine.getCleanupTimer() == null) {
615 scheduleStateMachineCleanupTimer(sessionId, stateMachine);
616 }
Jonathan Hart5db44532018-07-12 18:13:54 -0700617 stateMachine.start();
Shubham Sharmac155ef82019-07-11 12:12:41 +0000618 aaaStatisticsManager.getAaaStats().incrementEapolStartReqTrans();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700619 //send an EAP Request/Identify to the supplicant
620 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100621 if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
622 stateMachine.setPriorityCode(ethPkt.getPriorityCode());
623 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800624 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700625 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100626 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400627
Jonathan Hart092dfb22015-11-16 23:05:21 -0800628 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700629 stateMachine.setVlanId(ethPkt.getVlanID());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400630 log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000631 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
Ari Saha89831742015-06-26 10:31:48 -0700632
633 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800634 case EAPOL.EAPOL_LOGOFF:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400635 log.debug("EAP packet: EAPOL_LOGOFF");
Ray Milkeyb34b4962016-01-04 10:24:43 -0800636 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
637 stateMachine.logoff();
Shubham Sharmac155ef82019-07-11 12:12:41 +0000638 aaaStatisticsManager.getAaaStats().incrementEapolLogoffRx();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800639 }
640
641 break;
Ari Saha89831742015-06-26 10:31:48 -0700642 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700643 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700644 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700645 EAP eapPacket = (EAP) eapol.getPayload();
646
647 byte dataType = eapPacket.getDataType();
648 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700649
Ray Milkey9eb293f2015-09-30 15:09:17 -0700650 case EAP.ATTR_IDENTITY:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400651 log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY");
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000652 //Setting the time of this response from RG, only when its not a re-transmission.
653 if (stateMachine.getLastPacketReceivedTime() == 0) {
654 stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
655 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700656 // request id access to RADIUS
657 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700658
Ray Milkey967776a2015-10-07 14:37:17 -0700659 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100660 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800661 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700662
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100663 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000664 stateMachine.setWaitingForRadiusResponse(true);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000665 aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity();
Ray Milkey9eb293f2015-09-30 15:09:17 -0700666 // change the state to "PENDING"
kartikey dubeye1545422019-05-22 12:53:45 +0000667 if (stateMachine.state() == StateMachine.STATE_PENDING) {
668 aaaStatisticsManager.getAaaStats().increaseRequestReTx();
669 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700670 stateMachine.requestAccess();
671 break;
Ari Saha89831742015-06-26 10:31:48 -0700672 case EAP.ATTR_MD5:
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000673 stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400674 log.debug("EAP packet: EAPOL_PACKET ATTR_MD5");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700675 // verify if the EAP identifier corresponds to the
676 // challenge identifier from the client state
677 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700678 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700679 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700680 radiusPayload =
681 getRadiusPayload(stateMachine,
682 stateMachine.identifier(),
683 eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100684 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700685
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800686 if (stateMachine.challengeState() != null) {
687 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
688 stateMachine.challengeState());
689 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800690 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100691 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000692 stateMachine.setWaitingForRadiusResponse(true);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000693 aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall();
Ari Saha89831742015-06-26 10:31:48 -0700694 }
695 break;
696 case EAP.ATTR_TLS:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400697 log.debug("EAP packet: EAPOL_PACKET ATTR_TLS");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700698 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700699 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100700 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700701
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800702 if (stateMachine.challengeState() != null) {
703 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
704 stateMachine.challengeState());
705 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700706 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700707
Jonathan Hart092dfb22015-11-16 23:05:21 -0800708 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100709 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000710 stateMachine.setWaitingForRadiusResponse(true);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000711 aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall();
Ray Milkey5493b512015-10-21 12:13:49 -0700712
Ray Milkeyf3790b82015-10-21 16:28:08 -0700713 if (stateMachine.state() != StateMachine.STATE_PENDING) {
714 stateMachine.requestAccess();
715 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700716
Ari Saha89831742015-06-26 10:31:48 -0700717 break;
718 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400719 log.warn("Unknown EAP packet type");
Ari Saha89831742015-06-26 10:31:48 -0700720 return;
721 }
722 break;
723 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400724 log.debug("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700725 }
Shubham Sharmac155ef82019-07-11 12:12:41 +0000726 aaaStatisticsManager.getAaaStats().countTransRespNotNak();
727 aaaStatisticsManager.getAaaStats().countEapolResIdentityMsgTrans();
Ari Saha89831742015-06-26 10:31:48 -0700728 }
Ray Milkey967776a2015-10-07 14:37:17 -0700729 }
730
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100731 /**
Jonathan Hart5db44532018-07-12 18:13:54 -0700732 * Delegate allowing the StateMachine to notify us of events.
733 */
734 private class InternalStateMachineDelegate implements StateMachineDelegate {
735
736 @Override
737 public void notify(AuthenticationEvent authenticationEvent) {
738 log.info("Auth event {} for {}",
739 authenticationEvent.type(), authenticationEvent.subject());
740 post(authenticationEvent);
741 }
742 }
743
744 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100745 * Configuration Listener, handles change in configuration.
746 */
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700747 private class InternalConfigListener implements NetworkConfigListener {
748
749 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800750 * Reconfigures the AAA application according to the
751 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700752 *
753 * @param cfg configuration object
754 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800755 private void reconfigureNetwork(AaaConfig cfg) {
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400756 log.info("Reconfiguring AaaConfig from config: {}", cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100757
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700758 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800759 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700760 } else {
761 newCfg = cfg;
762 }
763 if (newCfg.nasIp() != null) {
764 nasIpAddress = newCfg.nasIp();
765 }
766 if (newCfg.radiusIp() != null) {
767 radiusIpAddress = newCfg.radiusIp();
768 }
769 if (newCfg.radiusMac() != null) {
770 radiusMacAddress = newCfg.radiusMac();
771 }
772 if (newCfg.nasMac() != null) {
773 nasMacAddress = newCfg.nasMac();
774 }
775 if (newCfg.radiusSecret() != null) {
776 radiusSecret = newCfg.radiusSecret();
777 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100778
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530779 boolean reconfigureCustomizer = false;
780 if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) {
781 customizer = newCfg.radiusPktCustomizer();
782 configurePacketCustomizer();
783 reconfigureCustomizer = true;
784 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100785
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530786 if (radiusConnectionType == null
787 || reconfigureCustomizer
788 || !radiusConnectionType.equals(newCfg.radiusConnectionType())) {
789 radiusConnectionType = newCfg.radiusConnectionType();
790 if (impl != null) {
791 impl.withdrawIntercepts();
792 impl.clearLocalState();
793 }
794 configureRadiusCommunication();
795 impl.initializeLocalState(newCfg);
796 impl.requestIntercepts();
797 } else if (impl != null) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100798 impl.clearLocalState();
799 impl.initializeLocalState(newCfg);
Ray Milkey5d99bd12015-10-06 15:41:30 -0700800 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700801 }
802
803 @Override
804 public void event(NetworkConfigEvent event) {
805
806 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
807 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800808 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700809
Jonathan Hart092dfb22015-11-16 23:05:21 -0800810 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700811 reconfigureNetwork(cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100812
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400813 log.info("Reconfigured: {}", cfg.toString());
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700814 }
815 }
816 }
Amit Ghoshf739be52017-09-21 15:49:37 +0100817
818 private class InternalDeviceListener implements DeviceListener {
819 @Override
820 public void event(DeviceEvent event) {
821
822 switch (event.type()) {
823 case PORT_REMOVED:
824 DeviceId devId = event.subject().id();
825 PortNumber portNumber = event.port().number();
826 String sessionId = devId.toString() + portNumber.toString();
827
828 Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap();
829 StateMachine removed = sessionIdMap.remove(sessionId);
830 if (removed != null) {
831 StateMachine.deleteStateMachineMapping(removed);
832 }
833
834 break;
835 default:
836 return;
837 }
838 }
839 }
kartikey dubeye1545422019-05-22 12:53:45 +0000840 private class AuthenticationStatisticsEventPublisher implements Runnable {
841 private final Logger log = getLogger(getClass());
842 public void run() {
843 log.info("Notifying AuthenticationStatisticsEvent");
844 aaaStatisticsManager.calculatePacketRoundtripTime();
845 log.debug("AcceptResponsesRx---" + aaaStatisticsManager.getAaaStats().getAcceptResponsesRx());
846 log.debug("AccessRequestsTx---" + aaaStatisticsManager.getAaaStats().getAccessRequestsTx());
847 log.debug("ChallengeResponsesRx---" + aaaStatisticsManager.getAaaStats().getChallengeResponsesRx());
848 log.debug("DroppedResponsesRx---" + aaaStatisticsManager.getAaaStats().getDroppedResponsesRx());
849 log.debug("InvalidValidatorsRx---" + aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx());
850 log.debug("MalformedResponsesRx---" + aaaStatisticsManager.getAaaStats().getMalformedResponsesRx());
851 log.debug("PendingRequests---" + aaaStatisticsManager.getAaaStats().getPendingRequests());
852 log.debug("RejectResponsesRx---" + aaaStatisticsManager.getAaaStats().getRejectResponsesRx());
853 log.debug("RequestReTx---" + aaaStatisticsManager.getAaaStats().getRequestReTx());
854 log.debug("RequestRttMilis---" + aaaStatisticsManager.getAaaStats().getRequestRttMilis());
855 log.debug("UnknownServerRx---" + aaaStatisticsManager.getAaaStats().getUnknownServerRx());
856 log.debug("UnknownTypeRx---" + aaaStatisticsManager.getAaaStats().getUnknownTypeRx());
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000857 log.debug("TimedOutPackets----" + aaaStatisticsManager.getAaaStats().getTimedOutPackets());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000858 log.debug("EapolLogoffRx---" + aaaStatisticsManager.getAaaStats().getEapolLogoffRx());
859 log.debug("EapolAuthSuccessTrans---" + aaaStatisticsManager.getAaaStats().getEapolAuthSuccessTrans());
860 log.debug("EapolAuthFailureTrans---" +
861 aaaStatisticsManager.getAaaStats().getEapolAuthFailureTrans());
862 log.debug("EapolStartReqTrans---" +
863 aaaStatisticsManager.getAaaStats().getEapolStartReqTrans());
864 log.debug("EapolTransRespNotNak---" +
865 aaaStatisticsManager.getAaaStats().getEapolTransRespNotNak());
866 log.debug("EapPktTxauthChooseEap---" +
867 aaaStatisticsManager.getAaaStats().getEapPktTxauthChooseEap());
868 log.debug("EapolResIdentityMsgTrans---" +
869 aaaStatisticsManager.getAaaStats().getEapolResIdentityMsgTrans());
kartikey dubeye1545422019-05-22 12:53:45 +0000870 aaaStatisticsManager.getStatsDelegate().
871 notify(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE,
872 aaaStatisticsManager.getAaaStats()));
873 }
874 }
Ari Saha89831742015-06-26 10:31:48 -0700875}