blob: da9ed6f8a1cdde8c04f1cfde32f2acb482576c28 [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;
Carmelo Cascone58b53292019-09-30 12:35:31 -070030import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hart4731dd92018-05-02 17:30:05 -070034import org.onlab.packet.DeserializationException;
Jonathan Harta46dddf2015-06-30 15:31:20 -070035import org.onlab.packet.EAP;
36import org.onlab.packet.EAPOL;
37import org.onlab.packet.EthType;
Ari Saha89831742015-06-26 10:31:48 -070038import org.onlab.packet.Ethernet;
Ari Saha89831742015-06-26 10:31:48 -070039import org.onlab.packet.MacAddress;
Jonathan Harta46dddf2015-06-30 15:31:20 -070040import org.onlab.packet.RADIUS;
41import org.onlab.packet.RADIUSAttribute;
kartikey dubeye1545422019-05-22 12:53:45 +000042import org.onlab.util.Tools;
43import org.onosproject.cfg.ComponentConfigService;
Ari Saha89831742015-06-26 10:31:48 -070044import org.onosproject.core.ApplicationId;
45import org.onosproject.core.CoreService;
Jonathan Hart5db44532018-07-12 18:13:54 -070046import org.onosproject.event.AbstractListenerManager;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010047import org.onosproject.mastership.MastershipService;
Ari Saha89831742015-06-26 10:31:48 -070048import org.onosproject.net.ConnectPoint;
49import org.onosproject.net.DeviceId;
Ari Saha89831742015-06-26 10:31:48 -070050import org.onosproject.net.PortNumber;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070051import org.onosproject.net.config.ConfigFactory;
52import org.onosproject.net.config.NetworkConfigEvent;
53import org.onosproject.net.config.NetworkConfigListener;
54import org.onosproject.net.config.NetworkConfigRegistry;
Amit Ghoshf739be52017-09-21 15:49:37 +010055import org.onosproject.net.device.DeviceEvent;
56import org.onosproject.net.device.DeviceListener;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010057import org.onosproject.net.device.DeviceService;
Ari Saha89831742015-06-26 10:31:48 -070058import org.onosproject.net.flow.DefaultTrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070059import org.onosproject.net.flow.TrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070060import org.onosproject.net.packet.DefaultOutboundPacket;
61import org.onosproject.net.packet.InboundPacket;
62import org.onosproject.net.packet.OutboundPacket;
63import org.onosproject.net.packet.PacketContext;
Ari Saha89831742015-06-26 10:31:48 -070064import org.onosproject.net.packet.PacketProcessor;
65import org.onosproject.net.packet.PacketService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070066import org.opencord.aaa.AaaConfig;
67import org.opencord.aaa.AuthenticationEvent;
68import org.opencord.aaa.AuthenticationEventListener;
69import org.opencord.aaa.AuthenticationService;
kartikey dubeye1545422019-05-22 12:53:45 +000070import org.opencord.aaa.AuthenticationStatisticsEvent;
71import org.opencord.aaa.AuthenticationStatisticsService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070072import org.opencord.aaa.RadiusCommunicator;
73import org.opencord.aaa.StateMachineDelegate;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000074import org.opencord.sadis.BaseInformationService;
75import org.opencord.sadis.SadisService;
76import org.opencord.sadis.SubscriberAndDeviceInformation;
kartikey dubeye1545422019-05-22 12:53:45 +000077import org.osgi.service.component.ComponentContext;
Carmelo Cascone58b53292019-09-30 12:35:31 -070078import org.osgi.service.component.annotations.Modified;
79import org.osgi.service.component.annotations.Activate;
Ari Saha89831742015-06-26 10:31:48 -070080import org.slf4j.Logger;
kartikey dubeye1545422019-05-22 12:53:45 +000081import com.google.common.base.Strings;
Ari Saha89831742015-06-26 10:31:48 -070082
kartikey dubeye1545422019-05-22 12:53:45 +000083import java.util.concurrent.Executors;
84import java.util.concurrent.ScheduledExecutorService;
85import java.util.concurrent.ScheduledFuture;
86import java.util.concurrent.TimeUnit;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000087
Carmelo Cascone58b53292019-09-30 12:35:31 -070088import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_GENERATION_EVENT;
89import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_GENERATION_EVENT_DEFAULT;
90
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000091import javax.crypto.Mac;
92import javax.crypto.spec.SecretKeySpec;
Ari Saha89831742015-06-26 10:31:48 -070093/**
Jonathan Harta46dddf2015-06-30 15:31:20 -070094 * AAA application for ONOS.
Ari Saha89831742015-06-26 10:31:48 -070095 */
Carmelo Cascone58b53292019-09-30 12:35:31 -070096@Component(immediate = true, property = {
97 STATISTICS_GENERATION_EVENT + ":Integer=" + STATISTICS_GENERATION_EVENT_DEFAULT,
98})
Jonathan Hart5db44532018-07-12 18:13:54 -070099public class AaaManager
100 extends AbstractListenerManager<AuthenticationEvent, AuthenticationEventListener>
101 implements AuthenticationService {
102
Charles Chandf7ff862017-01-20 11:22:05 -0800103 private static final String APP_NAME = "org.opencord.aaa";
Ray Milkeyf51eba22015-09-25 10:24:23 -0700104
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700105 private final Logger log = getLogger(getClass());
Ray Milkeyf51eba22015-09-25 10:24:23 -0700106
Carmelo Cascone58b53292019-09-30 12:35:31 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700108 protected CoreService coreService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700109
Carmelo Cascone58b53292019-09-30 12:35:31 -0700110 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700111 protected PacketService packetService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700112
Carmelo Cascone58b53292019-09-30 12:35:31 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700114 protected NetworkConfigRegistry netCfgService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700115
Carmelo Cascone58b53292019-09-30 12:35:31 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100117 protected DeviceService deviceService;
118
Carmelo Cascone58b53292019-09-30 12:35:31 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000120 protected SadisService sadisService;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100121
Carmelo Cascone58b53292019-09-30 12:35:31 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100123 protected MastershipService mastershipService;
124
Carmelo Cascone58b53292019-09-30 12:35:31 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
kartikey dubeye1545422019-05-22 12:53:45 +0000126 protected AuthenticationStatisticsService aaaStatisticsManager;
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000127
Carmelo Cascone58b53292019-09-30 12:35:31 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
kartikey dubeye1545422019-05-22 12:53:45 +0000129 protected ComponentConfigService cfgService;
130
131 protected AuthenticationStatisticsEventPublisher authenticationStatisticsPublisher;
132 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
Amit Ghoshf739be52017-09-21 15:49:37 +0100133 private final DeviceListener deviceListener = new InternalDeviceListener();
134
Carmelo Cascone58b53292019-09-30 12:35:31 -0700135 /** Statistics generation interval. */
136 private int statisticsGenerationEvent = STATISTICS_GENERATION_EVENT_DEFAULT;
kartikey dubeye1545422019-05-22 12:53:45 +0000137
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
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100162 // Setup specific customization/attributes on the RADIUS packets
163 PacketCustomizer pktCustomizer;
Ray Milkey967776a2015-10-07 14:37:17 -0700164
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100165 // packet customizer to use
166 private String customizer;
167
168 // Type of connection to use to communicate with Radius server, options are
169 // "socket" or "packet_out"
170 private String radiusConnectionType;
171
Jonathan Hart5db44532018-07-12 18:13:54 -0700172 // Object for the specific type of communication with the RADIUS
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100173 // server, socket based or packet_out based
174 RadiusCommunicator impl = null;
175
176 // latest configuration
177 AaaConfig newCfg;
Ray Milkey967776a2015-10-07 14:37:17 -0700178
kartikey dubeye1545422019-05-22 12:53:45 +0000179 ScheduledFuture<?> scheduledFuture;
180
181 ScheduledExecutorService executor;
182 String configuredAaaServerAddress;
183 HashSet<Byte> outPacketSet = new HashSet<Byte>();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700184 // Configuration properties factory
185 private final ConfigFactory factory =
Jonathan Hart092dfb22015-11-16 23:05:21 -0800186 new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY,
187 AaaConfig.class,
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700188 "AAA") {
189 @Override
Jonathan Hart092dfb22015-11-16 23:05:21 -0800190 public AaaConfig createConfig() {
191 return new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700192 }
193 };
Ray Milkeyf51eba22015-09-25 10:24:23 -0700194
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700195 // Listener for config changes
196 private final InternalConfigListener cfgListener = new InternalConfigListener();
Ari Saha89831742015-06-26 10:31:48 -0700197
Jonathan Hart5db44532018-07-12 18:13:54 -0700198 private StateMachineDelegate delegate = new InternalStateMachineDelegate();
199
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700200 /**
201 * Builds an EAPOL packet based on the given parameters.
202 *
203 * @param dstMac destination MAC address
204 * @param srcMac source MAC address
205 * @param vlan vlan identifier
206 * @param eapolType EAPOL type
207 * @param eap EAP payload
208 * @return Ethernet frame
209 */
210 private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100211 short vlan, byte eapolType, EAP eap, byte priorityCode) {
Ari Saha89831742015-06-26 10:31:48 -0700212
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700213 Ethernet eth = new Ethernet();
214 eth.setDestinationMACAddress(dstMac.toBytes());
215 eth.setSourceMACAddress(srcMac.toBytes());
216 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
217 if (vlan != Ethernet.VLAN_UNTAGGED) {
218 eth.setVlanID(vlan);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100219 eth.setPriorityCode(priorityCode);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700220 }
221 //eapol header
222 EAPOL eapol = new EAPOL();
223 eapol.setEapolType(eapolType);
224 eapol.setPacketLength(eap.getLength());
Ari Saha89831742015-06-26 10:31:48 -0700225
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700226 //eap part
227 eapol.setPayload(eap);
228
229 eth.setPayload(eapol);
230 eth.setPad(true);
231 return eth;
232 }
Ari Saha89831742015-06-26 10:31:48 -0700233
Ari Saha89831742015-06-26 10:31:48 -0700234 @Activate
kartikey dubeye1545422019-05-22 12:53:45 +0000235 public void activate(ComponentContext context) {
Charles Chandf7ff862017-01-20 11:22:05 -0800236 appId = coreService.registerApplication(APP_NAME);
Jonathan Hart5db44532018-07-12 18:13:54 -0700237 eventDispatcher.addSink(AuthenticationEvent.class, listenerRegistry);
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400238 netCfgService.addListener(cfgListener);
239 netCfgService.registerConfigFactory(factory);
kartikey dubeye1545422019-05-22 12:53:45 +0000240 cfgService.registerProperties(getClass());
241 modified(context);
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000242 subsService = sadisService.getSubscriberInfoService();
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530243 customInfo = new CustomizationInfo(subsService, deviceService);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800244 cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class));
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400245 log.info("Starting with config {} {}", this, newCfg);
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530246 configureRadiusCommunication();
Ari Saha89831742015-06-26 10:31:48 -0700247 // register our event handler
Brian O'Connord9c7da02015-07-29 17:49:24 -0700248 packetService.addProcessor(processor, PacketProcessor.director(2));
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700249 StateMachine.initializeMaps();
Jonathan Hart5db44532018-07-12 18:13:54 -0700250 StateMachine.setDelegate(delegate);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100251 impl.initializeLocalState(newCfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100252 impl.requestIntercepts();
Amit Ghoshf739be52017-09-21 15:49:37 +0100253 deviceService.addListener(deviceListener);
kartikey dubeye1545422019-05-22 12:53:45 +0000254 getConfiguredAaaServerAddress();
255 authenticationStatisticsPublisher =
256 new AuthenticationStatisticsEventPublisher();
257 executor = Executors.newScheduledThreadPool(1);
258 scheduledFuture = executor.scheduleAtFixedRate(authenticationStatisticsPublisher,
259 0, statisticsGenerationEvent, TimeUnit.SECONDS);
Amit Ghoshf739be52017-09-21 15:49:37 +0100260
Jian Li13c67162015-12-09 13:20:34 -0800261 log.info("Started");
Ari Saha89831742015-06-26 10:31:48 -0700262 }
263
264 @Deactivate
kartikey dubeye1545422019-05-22 12:53:45 +0000265 public void deactivate(ComponentContext context) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100266 impl.withdrawIntercepts();
Ari Saha89831742015-06-26 10:31:48 -0700267 packetService.removeProcessor(processor);
Deepa Vaddireddyb9c24c62017-09-21 13:45:30 +0530268 netCfgService.removeListener(cfgListener);
kartikey dubeye1545422019-05-22 12:53:45 +0000269 cfgService.unregisterProperties(getClass(), false);
Jonathan Hart5db44532018-07-12 18:13:54 -0700270 StateMachine.unsetDelegate(delegate);
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530271 StateMachine.destroyMaps();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100272 impl.deactivate();
Amit Ghoshf739be52017-09-21 15:49:37 +0100273 deviceService.removeListener(deviceListener);
Jonathan Hart5db44532018-07-12 18:13:54 -0700274 eventDispatcher.removeSink(AuthenticationEvent.class);
kartikey dubeye1545422019-05-22 12:53:45 +0000275 scheduledFuture.cancel(true);
276 executor.shutdown();
Jian Li13c67162015-12-09 13:20:34 -0800277 log.info("Stopped");
Ray Milkey967776a2015-10-07 14:37:17 -0700278 }
279
kartikey dubeye1545422019-05-22 12:53:45 +0000280 @Modified
281 public void modified(ComponentContext context) {
282 Dictionary<?, ?> properties = context.getProperties();
Carmelo Cascone58b53292019-09-30 12:35:31 -0700283 String s = Tools.get(properties, STATISTICS_GENERATION_EVENT);
284 statisticsGenerationEvent = Strings.isNullOrEmpty(s)
285 ? STATISTICS_GENERATION_EVENT_DEFAULT : Integer.parseInt(s.trim());
kartikey dubeye1545422019-05-22 12:53:45 +0000286 }
287
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530288 private void configureRadiusCommunication() {
289 if (radiusConnectionType.toLowerCase().equals("socket")) {
290 impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
291 } else {
292 impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService,
293 deviceService, subsService, pktCustomizer, this);
294 }
295 }
296
297 private void configurePacketCustomizer() {
298 switch (customizer.toLowerCase()) {
299 case "sample":
300 pktCustomizer = new SamplePacketCustomizer(customInfo);
301 log.info("Created SamplePacketCustomizer");
302 break;
Saurav Dase72358a2018-11-13 21:56:46 -0800303 case "att":
304 pktCustomizer = new AttPacketCustomizer(customInfo);
305 log.info("Created AttPacketCustomizer");
306 break;
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530307 default:
308 pktCustomizer = new PacketCustomizer(customInfo);
309 log.info("Created default PacketCustomizer");
310 break;
311 }
312 }
313
kartikey dubeye1545422019-05-22 12:53:45 +0000314 private void getConfiguredAaaServerAddress() {
315 try {
316 InetAddress address;
317 if (newCfg.radiusHostName() != null) {
318 address = InetAddress.getByName(newCfg.radiusHostName());
319 } else {
320 address = newCfg.radiusIp();
321 }
322
323 configuredAaaServerAddress = address.getHostAddress();
324 } catch (UnknownHostException uhe) {
325 log.warn("Unable to resolve host {}", newCfg.radiusHostName());
326 }
327 }
328
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000329 private void checkReceivedPacketForValidValidator(RADIUS radiusPacket, byte[] requestAuthenticator) {
330 if (!checkResponseMessageAuthenticator(radiusSecret, radiusPacket, requestAuthenticator)) {
kartikey dubeye1545422019-05-22 12:53:45 +0000331 aaaStatisticsManager.getAaaStats().increaseInvalidValidatorsRx();
332 }
333 }
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000334
335 private boolean checkResponseMessageAuthenticator(String key, RADIUS radiusPacket, byte[] requestAuthenticator) {
336 byte[] newHash = new byte[16];
337 Arrays.fill(newHash, (byte) 0);
338 byte[] messageAuthenticator = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue();
339 byte[] authenticator = radiusPacket.getAuthenticator();
340 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash);
341 radiusPacket.setAuthenticator(requestAuthenticator);
342 // Calculate the MD5 HMAC based on the message
343 try {
344 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
345 Mac mac = Mac.getInstance("HmacMD5");
346 mac.init(keySpec);
347 newHash = mac.doFinal(radiusPacket.serialize());
348 } catch (Exception e) {
349 log.error("Failed to generate message authenticator: {}", e.getMessage());
350 }
351 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator);
352 radiusPacket.setAuthenticator(authenticator);
353 // Compare the calculated Message-Authenticator with the one in the message
354 return Arrays.equals(newHash, messageAuthenticator);
355 }
kartikey dubeye1545422019-05-22 12:53:45 +0000356 public void checkForPacketFromUnknownServer(String hostAddress) {
357 if (!hostAddress.equals(configuredAaaServerAddress)) {
Vijaykumar Kushwahafffd3d12019-08-01 11:09:59 +0000358 getConfiguredAaaServerAddress();
359 if (!hostAddress.equals(configuredAaaServerAddress)) {
360 aaaStatisticsManager.getAaaStats().incrementUnknownServerRx();
361 }
kartikey dubeye1545422019-05-22 12:53:45 +0000362 }
363 }
364
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100365 /**
366 * Send RADIUS packet to the RADIUS server.
367 *
368 * @param radiusPacket RADIUS packet to be sent to server.
369 * @param inPkt Incoming EAPOL packet
370 */
371 protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) {
kartikey dubeye1545422019-05-22 12:53:45 +0000372 outPacketSet.add(radiusPacket.getIdentifier());
373 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(true);
374 aaaStatisticsManager.getAaaStats().increaseAccessRequestsTx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100375 impl.sendRadiusPacket(radiusPacket, inPkt);
376 }
Ray Milkey967776a2015-10-07 14:37:17 -0700377
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100378 /**
379 * Handles RADIUS packets.
380 *
381 * @param radiusPacket RADIUS packet coming from the RADIUS server.
382 * @throws StateMachineException if an illegal state transition is triggered
Jonathan Hart4731dd92018-05-02 17:30:05 -0700383 * @throws DeserializationException if packet deserialization fails
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100384 */
Jonathan Hart4731dd92018-05-02 17:30:05 -0700385 public void handleRadiusPacket(RADIUS radiusPacket)
386 throws StateMachineException, DeserializationException {
Saurav Das987441a2018-09-18 16:33:47 -0700387 if (log.isTraceEnabled()) {
388 log.trace("Received RADIUS packet {}", radiusPacket);
389 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100390 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
391 if (stateMachine == null) {
Saurav Das987441a2018-09-18 16:33:47 -0700392 log.error("Invalid packet identifier {}, could not find corresponding "
393 + "state machine ... exiting", radiusPacket.getIdentifier());
kartikey dubeye1545422019-05-22 12:53:45 +0000394 aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired();
395 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100396 return;
Ray Milkey967776a2015-10-07 14:37:17 -0700397 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100398 EAP eapPayload;
399 Ethernet eth;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000400 checkReceivedPacketForValidValidator(radiusPacket, stateMachine.requestAuthenticator());
kartikey dubeye1545422019-05-22 12:53:45 +0000401 if (outPacketSet.contains(radiusPacket.getIdentifier())) {
402 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(false);
403 outPacketSet.remove(new Byte(radiusPacket.getIdentifier()));
404 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100405 switch (radiusPacket.getCode()) {
406 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400407 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100408 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
409 byte[] challengeState = null;
410 if (radiusAttrState != null) {
411 challengeState = radiusAttrState.getValue();
412 }
413 eapPayload = radiusPacket.decapsulateMessage();
414 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
415 eth = buildEapolResponse(stateMachine.supplicantAddress(),
416 MacAddress.valueOf(nasMacAddress),
417 stateMachine.vlanId(),
418 EAPOL.EAPOL_PACKET,
419 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400420 log.debug("Send EAP challenge response to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharma1f193582019-07-11 12:12:41 +0000421 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), true);
kartikey dubeye1545422019-05-22 12:53:45 +0000422 aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100423 break;
424 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400425 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100426 //send an EAPOL - Success to the supplicant.
427 byte[] eapMessageSuccess =
428 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700429 eapPayload = EAP.deserializer().deserialize(
430 eapMessageSuccess, 0, eapMessageSuccess.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100431 eth = buildEapolResponse(stateMachine.supplicantAddress(),
432 MacAddress.valueOf(nasMacAddress),
433 stateMachine.vlanId(),
434 EAPOL.EAPOL_PACKET,
435 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400436 log.info("Send EAP success message to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharma1f193582019-07-11 12:12:41 +0000437 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
438 aaaStatisticsManager.getAaaStats().incrementEapolAuthSuccessTrans();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100439
440 stateMachine.authorizeAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000441 aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100442 break;
443 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400444 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100445 //send an EAPOL - Failure to the supplicant.
446 byte[] eapMessageFailure;
447 eapPayload = new EAP();
448 RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE);
449 if (radiusAttrEap == null) {
450 eapPayload.setCode(EAP.FAILURE);
451 eapPayload.setIdentifier(stateMachine.challengeIdentifier());
452 eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL);
453 } else {
454 eapMessageFailure = radiusAttrEap.getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700455 eapPayload = EAP.deserializer().deserialize(
456 eapMessageFailure, 0, eapMessageFailure.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100457 }
458 eth = buildEapolResponse(stateMachine.supplicantAddress(),
459 MacAddress.valueOf(nasMacAddress),
460 stateMachine.vlanId(),
461 EAPOL.EAPOL_PACKET,
462 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400463 log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharma1f193582019-07-11 12:12:41 +0000464 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
465 aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans();
466
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100467 stateMachine.denyAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000468 aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100469 break;
470 default:
471 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
kartikey dubeye1545422019-05-22 12:53:45 +0000472 aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100473 }
kartikey dubeye1545422019-05-22 12:53:45 +0000474 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700475 }
476
Ray Milkey967776a2015-10-07 14:37:17 -0700477 /**
478 * Send the ethernet packet to the supplicant.
479 *
480 * @param ethernetPkt the ethernet packet
481 * @param connectPoint the connect point to send out
482 */
Shubham Sharma1f193582019-07-11 12:12:41 +0000483 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint, boolean isChallengeResponse) {
Ray Milkey967776a2015-10-07 14:37:17 -0700484 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
485 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
486 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
Shubham Sharma1f193582019-07-11 12:12:41 +0000487 EAPOL eap = ((EAPOL) ethernetPkt.getPayload());
488 EAP eapPkt = (EAP) eap.getPayload();
Saurav Das987441a2018-09-18 16:33:47 -0700489 if (log.isTraceEnabled()) {
Saurav Das987441a2018-09-18 16:33:47 -0700490 log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}",
491 eap, ethernetPkt, connectPoint);
492 }
Ray Milkey967776a2015-10-07 14:37:17 -0700493 packetService.emit(packet);
Shubham Sharma1f193582019-07-11 12:12:41 +0000494 if (isChallengeResponse) {
495 aaaStatisticsManager.getAaaStats().incrementEapPktTxauthEap();
496 }
Ray Milkey967776a2015-10-07 14:37:17 -0700497 }
498
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400499 @Override
500 public String toString() {
501 return ToStringBuilder.reflectionToString(this);
502 }
503
Ari Saha89831742015-06-26 10:31:48 -0700504 // our handler defined as a private inner class
505
506 /**
507 * Packet processor responsible for forwarding packets along their paths.
508 */
509 private class ReactivePacketProcessor implements PacketProcessor {
510 @Override
511 public void process(PacketContext context) {
512
513 // Extract the original Ethernet frame from the packet information
514 InboundPacket pkt = context.inPacket();
515 Ethernet ethPkt = pkt.parsed();
516 if (ethPkt == null) {
517 return;
518 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100519
Ray Milkeyf51eba22015-09-25 10:24:23 -0700520 try {
521 // identify if incoming packet comes from supplicant (EAP) or RADIUS
522 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
523 case EAPOL:
524 handleSupplicantPacket(context.inPacket());
525 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700526 default:
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100527 // any other packets let the specific implementation handle
528 impl.handlePacketFromServer(context);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700529 }
Ray Milkey967776a2015-10-07 14:37:17 -0700530 } catch (StateMachineException e) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100531 log.warn("Unable to process packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700532 }
533 }
534
Ray Milkey9eb293f2015-09-30 15:09:17 -0700535 /**
536 * Creates and initializes common fields of a RADIUS packet.
537 *
Ray Milkey967776a2015-10-07 14:37:17 -0700538 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700539 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700540 * @return RADIUS packet
541 */
Ray Milkey967776a2015-10-07 14:37:17 -0700542 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700543 RADIUS radiusPayload =
544 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
545 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700546
547 // set Request Authenticator in StateMachine
548 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
549
Ray Milkey9eb293f2015-09-30 15:09:17 -0700550 radiusPayload.setIdentifier(identifier);
551 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700552 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700553
554 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800555 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700556
557 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700558
559 return radiusPayload;
560 }
Ari Saha89831742015-06-26 10:31:48 -0700561
562 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700563 * Handles PAE packets (supplicant).
564 *
565 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700566 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700567 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700568 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700569 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800570 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700571
Jonathan Harta46dddf2015-06-30 15:31:20 -0700572 DeviceId deviceId = inPacket.receivedFrom().deviceId();
573 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700574 String sessionId = deviceId.toString() + portNumber.toString();
Saurav Das987441a2018-09-18 16:33:47 -0700575 EAPOL eapol = (EAPOL) ethPkt.getPayload();
576 if (log.isTraceEnabled()) {
577 log.trace("Received EAPOL packet {} in enclosing packet {} from "
578 + "dev/port: {}/{}", eapol, ethPkt, deviceId,
579 portNumber);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100580 }
Jonathan Harta46dddf2015-06-30 15:31:20 -0700581
Saurav Das987441a2018-09-18 16:33:47 -0700582 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
583 if (stateMachine == null) {
584 log.debug("Creating new state machine for sessionId: {} for "
585 + "dev/port: {}/{}", sessionId, deviceId, portNumber);
586 stateMachine = new StateMachine(sessionId);
587 } else {
588 log.debug("Using existing state-machine for sessionId: {}", sessionId);
589 }
590
Ari Saha89831742015-06-26 10:31:48 -0700591 switch (eapol.getEapolType()) {
592 case EAPOL.EAPOL_START:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400593 log.debug("EAP packet: EAPOL_START");
Ray Milkeyf51eba22015-09-25 10:24:23 -0700594 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Jonathan Hart5db44532018-07-12 18:13:54 -0700595 stateMachine.start();
Shubham Sharma1f193582019-07-11 12:12:41 +0000596 aaaStatisticsManager.getAaaStats().incrementEapolStartReqTrans();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700597 //send an EAP Request/Identify to the supplicant
598 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100599 if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
600 stateMachine.setPriorityCode(ethPkt.getPriorityCode());
601 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800602 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700603 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100604 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400605
Jonathan Hart092dfb22015-11-16 23:05:21 -0800606 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700607 stateMachine.setVlanId(ethPkt.getVlanID());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400608 log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharma1f193582019-07-11 12:12:41 +0000609 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
Ari Saha89831742015-06-26 10:31:48 -0700610
611 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800612 case EAPOL.EAPOL_LOGOFF:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400613 log.debug("EAP packet: EAPOL_LOGOFF");
Ray Milkeyb34b4962016-01-04 10:24:43 -0800614 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
615 stateMachine.logoff();
Shubham Sharma1f193582019-07-11 12:12:41 +0000616 aaaStatisticsManager.getAaaStats().incrementEapolLogoffRx();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800617 }
618
619 break;
Ari Saha89831742015-06-26 10:31:48 -0700620 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700621 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700622 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700623 EAP eapPacket = (EAP) eapol.getPayload();
624
625 byte dataType = eapPacket.getDataType();
626 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700627
Ray Milkey9eb293f2015-09-30 15:09:17 -0700628 case EAP.ATTR_IDENTITY:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400629 log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700630 // request id access to RADIUS
631 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700632
Ray Milkey967776a2015-10-07 14:37:17 -0700633 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100634 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800635 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700636
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100637 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharma1f193582019-07-11 12:12:41 +0000638 aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity();
Ray Milkey9eb293f2015-09-30 15:09:17 -0700639 // change the state to "PENDING"
kartikey dubeye1545422019-05-22 12:53:45 +0000640 if (stateMachine.state() == StateMachine.STATE_PENDING) {
641 aaaStatisticsManager.getAaaStats().increaseRequestReTx();
642 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700643 stateMachine.requestAccess();
644 break;
Ari Saha89831742015-06-26 10:31:48 -0700645 case EAP.ATTR_MD5:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400646 log.debug("EAP packet: EAPOL_PACKET ATTR_MD5");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700647 // verify if the EAP identifier corresponds to the
648 // challenge identifier from the client state
649 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700650 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700651 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700652 radiusPayload =
653 getRadiusPayload(stateMachine,
654 stateMachine.identifier(),
655 eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100656 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700657
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800658 if (stateMachine.challengeState() != null) {
659 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
660 stateMachine.challengeState());
661 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800662 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100663 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharma1f193582019-07-11 12:12:41 +0000664 aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall();
Ari Saha89831742015-06-26 10:31:48 -0700665 }
666 break;
667 case EAP.ATTR_TLS:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400668 log.debug("EAP packet: EAPOL_PACKET ATTR_TLS");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700669 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700670 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100671 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700672
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800673 if (stateMachine.challengeState() != null) {
674 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
675 stateMachine.challengeState());
676 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700677 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700678
Jonathan Hart092dfb22015-11-16 23:05:21 -0800679 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100680 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharma1f193582019-07-11 12:12:41 +0000681 aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall();
Ray Milkey5493b512015-10-21 12:13:49 -0700682
Ray Milkeyf3790b82015-10-21 16:28:08 -0700683 if (stateMachine.state() != StateMachine.STATE_PENDING) {
684 stateMachine.requestAccess();
685 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700686
Ari Saha89831742015-06-26 10:31:48 -0700687 break;
688 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400689 log.warn("Unknown EAP packet type");
Ari Saha89831742015-06-26 10:31:48 -0700690 return;
691 }
692 break;
693 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400694 log.debug("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700695 }
Shubham Sharma1f193582019-07-11 12:12:41 +0000696 aaaStatisticsManager.getAaaStats().countTransRespNotNak();
697 aaaStatisticsManager.getAaaStats().countEapolResIdentityMsgTrans();
Ari Saha89831742015-06-26 10:31:48 -0700698 }
Ray Milkey967776a2015-10-07 14:37:17 -0700699 }
700
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100701 /**
Jonathan Hart5db44532018-07-12 18:13:54 -0700702 * Delegate allowing the StateMachine to notify us of events.
703 */
704 private class InternalStateMachineDelegate implements StateMachineDelegate {
705
706 @Override
707 public void notify(AuthenticationEvent authenticationEvent) {
708 log.info("Auth event {} for {}",
709 authenticationEvent.type(), authenticationEvent.subject());
710 post(authenticationEvent);
711 }
712 }
713
714 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100715 * Configuration Listener, handles change in configuration.
716 */
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700717 private class InternalConfigListener implements NetworkConfigListener {
718
719 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800720 * Reconfigures the AAA application according to the
721 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700722 *
723 * @param cfg configuration object
724 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800725 private void reconfigureNetwork(AaaConfig cfg) {
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400726 log.info("Reconfiguring AaaConfig from config: {}", cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100727
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700728 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800729 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700730 } else {
731 newCfg = cfg;
732 }
733 if (newCfg.nasIp() != null) {
734 nasIpAddress = newCfg.nasIp();
735 }
736 if (newCfg.radiusIp() != null) {
737 radiusIpAddress = newCfg.radiusIp();
738 }
739 if (newCfg.radiusMac() != null) {
740 radiusMacAddress = newCfg.radiusMac();
741 }
742 if (newCfg.nasMac() != null) {
743 nasMacAddress = newCfg.nasMac();
744 }
745 if (newCfg.radiusSecret() != null) {
746 radiusSecret = newCfg.radiusSecret();
747 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100748
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530749 boolean reconfigureCustomizer = false;
750 if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) {
751 customizer = newCfg.radiusPktCustomizer();
752 configurePacketCustomizer();
753 reconfigureCustomizer = true;
754 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100755
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530756 if (radiusConnectionType == null
757 || reconfigureCustomizer
758 || !radiusConnectionType.equals(newCfg.radiusConnectionType())) {
759 radiusConnectionType = newCfg.radiusConnectionType();
760 if (impl != null) {
761 impl.withdrawIntercepts();
762 impl.clearLocalState();
763 }
764 configureRadiusCommunication();
765 impl.initializeLocalState(newCfg);
766 impl.requestIntercepts();
767 } else if (impl != null) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100768 impl.clearLocalState();
769 impl.initializeLocalState(newCfg);
Ray Milkey5d99bd12015-10-06 15:41:30 -0700770 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700771 }
772
773 @Override
774 public void event(NetworkConfigEvent event) {
775
776 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
777 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800778 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700779
Jonathan Hart092dfb22015-11-16 23:05:21 -0800780 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700781 reconfigureNetwork(cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100782
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400783 log.info("Reconfigured: {}", cfg.toString());
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700784 }
785 }
786 }
Amit Ghoshf739be52017-09-21 15:49:37 +0100787
788 private class InternalDeviceListener implements DeviceListener {
789 @Override
790 public void event(DeviceEvent event) {
791
792 switch (event.type()) {
793 case PORT_REMOVED:
794 DeviceId devId = event.subject().id();
795 PortNumber portNumber = event.port().number();
796 String sessionId = devId.toString() + portNumber.toString();
797
798 Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap();
799 StateMachine removed = sessionIdMap.remove(sessionId);
800 if (removed != null) {
801 StateMachine.deleteStateMachineMapping(removed);
802 }
803
804 break;
805 default:
806 return;
807 }
808 }
809 }
kartikey dubeye1545422019-05-22 12:53:45 +0000810 private class AuthenticationStatisticsEventPublisher implements Runnable {
811 private final Logger log = getLogger(getClass());
812 public void run() {
813 log.info("Notifying AuthenticationStatisticsEvent");
814 aaaStatisticsManager.calculatePacketRoundtripTime();
815 log.debug("AcceptResponsesRx---" + aaaStatisticsManager.getAaaStats().getAcceptResponsesRx());
816 log.debug("AccessRequestsTx---" + aaaStatisticsManager.getAaaStats().getAccessRequestsTx());
817 log.debug("ChallengeResponsesRx---" + aaaStatisticsManager.getAaaStats().getChallengeResponsesRx());
818 log.debug("DroppedResponsesRx---" + aaaStatisticsManager.getAaaStats().getDroppedResponsesRx());
819 log.debug("InvalidValidatorsRx---" + aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx());
820 log.debug("MalformedResponsesRx---" + aaaStatisticsManager.getAaaStats().getMalformedResponsesRx());
821 log.debug("PendingRequests---" + aaaStatisticsManager.getAaaStats().getPendingRequests());
822 log.debug("RejectResponsesRx---" + aaaStatisticsManager.getAaaStats().getRejectResponsesRx());
823 log.debug("RequestReTx---" + aaaStatisticsManager.getAaaStats().getRequestReTx());
824 log.debug("RequestRttMilis---" + aaaStatisticsManager.getAaaStats().getRequestRttMilis());
825 log.debug("UnknownServerRx---" + aaaStatisticsManager.getAaaStats().getUnknownServerRx());
826 log.debug("UnknownTypeRx---" + aaaStatisticsManager.getAaaStats().getUnknownTypeRx());
Shubham Sharma1f193582019-07-11 12:12:41 +0000827 log.debug("EapolLogoffRx---" + aaaStatisticsManager.getAaaStats().getEapolLogoffRx());
828 log.debug("EapolAuthSuccessTrans---" + aaaStatisticsManager.getAaaStats().getEapolAuthSuccessTrans());
829 log.debug("EapolAuthFailureTrans---" +
830 aaaStatisticsManager.getAaaStats().getEapolAuthFailureTrans());
831 log.debug("EapolStartReqTrans---" +
832 aaaStatisticsManager.getAaaStats().getEapolStartReqTrans());
833 log.debug("EapolTransRespNotNak---" +
834 aaaStatisticsManager.getAaaStats().getEapolTransRespNotNak());
835 log.debug("EapPktTxauthChooseEap---" +
836 aaaStatisticsManager.getAaaStats().getEapPktTxauthChooseEap());
837 log.debug("EapolResIdentityMsgTrans---" +
838 aaaStatisticsManager.getAaaStats().getEapolResIdentityMsgTrans());
kartikey dubeye1545422019-05-22 12:53:45 +0000839 aaaStatisticsManager.getStatsDelegate().
840 notify(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE,
841 aaaStatisticsManager.getAaaStats()));
842 }
843 }
Ari Saha89831742015-06-26 10:31:48 -0700844}