blob: 5804b78fe5b883b126777b729d074f8d690677c7 [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());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100421 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
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());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100437 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
438
439 stateMachine.authorizeAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000440 aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100441 break;
442 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400443 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100444 //send an EAPOL - Failure to the supplicant.
445 byte[] eapMessageFailure;
446 eapPayload = new EAP();
447 RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE);
448 if (radiusAttrEap == null) {
449 eapPayload.setCode(EAP.FAILURE);
450 eapPayload.setIdentifier(stateMachine.challengeIdentifier());
451 eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL);
452 } else {
453 eapMessageFailure = radiusAttrEap.getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700454 eapPayload = EAP.deserializer().deserialize(
455 eapMessageFailure, 0, eapMessageFailure.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100456 }
457 eth = buildEapolResponse(stateMachine.supplicantAddress(),
458 MacAddress.valueOf(nasMacAddress),
459 stateMachine.vlanId(),
460 EAPOL.EAPOL_PACKET,
461 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400462 log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100463 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
464 stateMachine.denyAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000465 aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100466 break;
467 default:
468 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
kartikey dubeye1545422019-05-22 12:53:45 +0000469 aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100470 }
kartikey dubeye1545422019-05-22 12:53:45 +0000471 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700472 }
473
Ray Milkey967776a2015-10-07 14:37:17 -0700474 /**
475 * Send the ethernet packet to the supplicant.
476 *
477 * @param ethernetPkt the ethernet packet
478 * @param connectPoint the connect point to send out
479 */
480 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) {
481 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
482 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
483 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
Saurav Das987441a2018-09-18 16:33:47 -0700484 if (log.isTraceEnabled()) {
485 EAPOL eap = ((EAPOL) ethernetPkt.getPayload());
486 log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}",
487 eap, ethernetPkt, connectPoint);
488 }
Ray Milkey967776a2015-10-07 14:37:17 -0700489 packetService.emit(packet);
490 }
491
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400492 @Override
493 public String toString() {
494 return ToStringBuilder.reflectionToString(this);
495 }
496
Ari Saha89831742015-06-26 10:31:48 -0700497 // our handler defined as a private inner class
498
499 /**
500 * Packet processor responsible for forwarding packets along their paths.
501 */
502 private class ReactivePacketProcessor implements PacketProcessor {
503 @Override
504 public void process(PacketContext context) {
505
506 // Extract the original Ethernet frame from the packet information
507 InboundPacket pkt = context.inPacket();
508 Ethernet ethPkt = pkt.parsed();
509 if (ethPkt == null) {
510 return;
511 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100512
Ray Milkeyf51eba22015-09-25 10:24:23 -0700513 try {
514 // identify if incoming packet comes from supplicant (EAP) or RADIUS
515 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
516 case EAPOL:
517 handleSupplicantPacket(context.inPacket());
518 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700519 default:
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100520 // any other packets let the specific implementation handle
521 impl.handlePacketFromServer(context);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700522 }
Ray Milkey967776a2015-10-07 14:37:17 -0700523 } catch (StateMachineException e) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100524 log.warn("Unable to process packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700525 }
526 }
527
Ray Milkey9eb293f2015-09-30 15:09:17 -0700528 /**
529 * Creates and initializes common fields of a RADIUS packet.
530 *
Ray Milkey967776a2015-10-07 14:37:17 -0700531 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700532 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700533 * @return RADIUS packet
534 */
Ray Milkey967776a2015-10-07 14:37:17 -0700535 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700536 RADIUS radiusPayload =
537 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
538 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700539
540 // set Request Authenticator in StateMachine
541 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
542
Ray Milkey9eb293f2015-09-30 15:09:17 -0700543 radiusPayload.setIdentifier(identifier);
544 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700545 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700546
547 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800548 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700549
550 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700551
552 return radiusPayload;
553 }
Ari Saha89831742015-06-26 10:31:48 -0700554
555 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700556 * Handles PAE packets (supplicant).
557 *
558 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700559 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700560 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700561 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700562 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800563 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700564
Jonathan Harta46dddf2015-06-30 15:31:20 -0700565 DeviceId deviceId = inPacket.receivedFrom().deviceId();
566 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700567 String sessionId = deviceId.toString() + portNumber.toString();
Saurav Das987441a2018-09-18 16:33:47 -0700568 EAPOL eapol = (EAPOL) ethPkt.getPayload();
569 if (log.isTraceEnabled()) {
570 log.trace("Received EAPOL packet {} in enclosing packet {} from "
571 + "dev/port: {}/{}", eapol, ethPkt, deviceId,
572 portNumber);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100573 }
Jonathan Harta46dddf2015-06-30 15:31:20 -0700574
Saurav Das987441a2018-09-18 16:33:47 -0700575 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
576 if (stateMachine == null) {
577 log.debug("Creating new state machine for sessionId: {} for "
578 + "dev/port: {}/{}", sessionId, deviceId, portNumber);
579 stateMachine = new StateMachine(sessionId);
580 } else {
581 log.debug("Using existing state-machine for sessionId: {}", sessionId);
582 }
583
Ari Saha89831742015-06-26 10:31:48 -0700584 switch (eapol.getEapolType()) {
585 case EAPOL.EAPOL_START:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400586 log.debug("EAP packet: EAPOL_START");
Ray Milkeyf51eba22015-09-25 10:24:23 -0700587 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Jonathan Hart5db44532018-07-12 18:13:54 -0700588 stateMachine.start();
Ari Saha89831742015-06-26 10:31:48 -0700589
Ray Milkeyf51eba22015-09-25 10:24:23 -0700590 //send an EAP Request/Identify to the supplicant
591 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100592 if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
593 stateMachine.setPriorityCode(ethPkt.getPriorityCode());
594 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800595 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700596 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100597 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400598
Jonathan Hart092dfb22015-11-16 23:05:21 -0800599 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700600 stateMachine.setVlanId(ethPkt.getVlanID());
Ari Saha89831742015-06-26 10:31:48 -0700601
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400602 log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString());
Ray Milkey967776a2015-10-07 14:37:17 -0700603 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700604
605 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800606 case EAPOL.EAPOL_LOGOFF:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400607 log.debug("EAP packet: EAPOL_LOGOFF");
Ray Milkeyb34b4962016-01-04 10:24:43 -0800608 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
609 stateMachine.logoff();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800610 }
611
612 break;
Ari Saha89831742015-06-26 10:31:48 -0700613 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700614 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700615 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700616 EAP eapPacket = (EAP) eapol.getPayload();
617
618 byte dataType = eapPacket.getDataType();
619 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700620
Ray Milkey9eb293f2015-09-30 15:09:17 -0700621 case EAP.ATTR_IDENTITY:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400622 log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700623 // request id access to RADIUS
624 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700625
Ray Milkey967776a2015-10-07 14:37:17 -0700626 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100627 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800628 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700629
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100630 sendRadiusPacket(radiusPayload, inPacket);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700631
Ray Milkey9eb293f2015-09-30 15:09:17 -0700632 // change the state to "PENDING"
kartikey dubeye1545422019-05-22 12:53:45 +0000633 if (stateMachine.state() == StateMachine.STATE_PENDING) {
634 aaaStatisticsManager.getAaaStats().increaseRequestReTx();
635 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700636 stateMachine.requestAccess();
637 break;
Ari Saha89831742015-06-26 10:31:48 -0700638 case EAP.ATTR_MD5:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400639 log.debug("EAP packet: EAPOL_PACKET ATTR_MD5");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700640 // verify if the EAP identifier corresponds to the
641 // challenge identifier from the client state
642 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700643 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700644 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700645 radiusPayload =
646 getRadiusPayload(stateMachine,
647 stateMachine.identifier(),
648 eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100649 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700650
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800651 if (stateMachine.challengeState() != null) {
652 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
653 stateMachine.challengeState());
654 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800655 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100656 sendRadiusPacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700657 }
658 break;
659 case EAP.ATTR_TLS:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400660 log.debug("EAP packet: EAPOL_PACKET ATTR_TLS");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700661 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700662 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100663 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700664
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800665 if (stateMachine.challengeState() != null) {
666 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
667 stateMachine.challengeState());
668 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700669 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700670
Jonathan Hart092dfb22015-11-16 23:05:21 -0800671 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100672 sendRadiusPacket(radiusPayload, inPacket);
Ray Milkey5493b512015-10-21 12:13:49 -0700673
Ray Milkeyf3790b82015-10-21 16:28:08 -0700674 if (stateMachine.state() != StateMachine.STATE_PENDING) {
675 stateMachine.requestAccess();
676 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700677
Ari Saha89831742015-06-26 10:31:48 -0700678 break;
679 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400680 log.warn("Unknown EAP packet type");
Ari Saha89831742015-06-26 10:31:48 -0700681 return;
682 }
683 break;
684 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400685 log.debug("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700686 }
687 }
Ray Milkey967776a2015-10-07 14:37:17 -0700688 }
689
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100690 /**
Jonathan Hart5db44532018-07-12 18:13:54 -0700691 * Delegate allowing the StateMachine to notify us of events.
692 */
693 private class InternalStateMachineDelegate implements StateMachineDelegate {
694
695 @Override
696 public void notify(AuthenticationEvent authenticationEvent) {
697 log.info("Auth event {} for {}",
698 authenticationEvent.type(), authenticationEvent.subject());
699 post(authenticationEvent);
700 }
701 }
702
703 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100704 * Configuration Listener, handles change in configuration.
705 */
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700706 private class InternalConfigListener implements NetworkConfigListener {
707
708 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800709 * Reconfigures the AAA application according to the
710 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700711 *
712 * @param cfg configuration object
713 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800714 private void reconfigureNetwork(AaaConfig cfg) {
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400715 log.info("Reconfiguring AaaConfig from config: {}", cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100716
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700717 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800718 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700719 } else {
720 newCfg = cfg;
721 }
722 if (newCfg.nasIp() != null) {
723 nasIpAddress = newCfg.nasIp();
724 }
725 if (newCfg.radiusIp() != null) {
726 radiusIpAddress = newCfg.radiusIp();
727 }
728 if (newCfg.radiusMac() != null) {
729 radiusMacAddress = newCfg.radiusMac();
730 }
731 if (newCfg.nasMac() != null) {
732 nasMacAddress = newCfg.nasMac();
733 }
734 if (newCfg.radiusSecret() != null) {
735 radiusSecret = newCfg.radiusSecret();
736 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100737
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530738 boolean reconfigureCustomizer = false;
739 if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) {
740 customizer = newCfg.radiusPktCustomizer();
741 configurePacketCustomizer();
742 reconfigureCustomizer = true;
743 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100744
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530745 if (radiusConnectionType == null
746 || reconfigureCustomizer
747 || !radiusConnectionType.equals(newCfg.radiusConnectionType())) {
748 radiusConnectionType = newCfg.radiusConnectionType();
749 if (impl != null) {
750 impl.withdrawIntercepts();
751 impl.clearLocalState();
752 }
753 configureRadiusCommunication();
754 impl.initializeLocalState(newCfg);
755 impl.requestIntercepts();
756 } else if (impl != null) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100757 impl.clearLocalState();
758 impl.initializeLocalState(newCfg);
Ray Milkey5d99bd12015-10-06 15:41:30 -0700759 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700760 }
761
762 @Override
763 public void event(NetworkConfigEvent event) {
764
765 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
766 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800767 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700768
Jonathan Hart092dfb22015-11-16 23:05:21 -0800769 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700770 reconfigureNetwork(cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100771
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400772 log.info("Reconfigured: {}", cfg.toString());
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700773 }
774 }
775 }
Amit Ghoshf739be52017-09-21 15:49:37 +0100776
777 private class InternalDeviceListener implements DeviceListener {
778 @Override
779 public void event(DeviceEvent event) {
780
781 switch (event.type()) {
782 case PORT_REMOVED:
783 DeviceId devId = event.subject().id();
784 PortNumber portNumber = event.port().number();
785 String sessionId = devId.toString() + portNumber.toString();
786
787 Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap();
788 StateMachine removed = sessionIdMap.remove(sessionId);
789 if (removed != null) {
790 StateMachine.deleteStateMachineMapping(removed);
791 }
792
793 break;
794 default:
795 return;
796 }
797 }
798 }
kartikey dubeye1545422019-05-22 12:53:45 +0000799 private class AuthenticationStatisticsEventPublisher implements Runnable {
800 private final Logger log = getLogger(getClass());
801 public void run() {
802 log.info("Notifying AuthenticationStatisticsEvent");
803 aaaStatisticsManager.calculatePacketRoundtripTime();
804 log.debug("AcceptResponsesRx---" + aaaStatisticsManager.getAaaStats().getAcceptResponsesRx());
805 log.debug("AccessRequestsTx---" + aaaStatisticsManager.getAaaStats().getAccessRequestsTx());
806 log.debug("ChallengeResponsesRx---" + aaaStatisticsManager.getAaaStats().getChallengeResponsesRx());
807 log.debug("DroppedResponsesRx---" + aaaStatisticsManager.getAaaStats().getDroppedResponsesRx());
808 log.debug("InvalidValidatorsRx---" + aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx());
809 log.debug("MalformedResponsesRx---" + aaaStatisticsManager.getAaaStats().getMalformedResponsesRx());
810 log.debug("PendingRequests---" + aaaStatisticsManager.getAaaStats().getPendingRequests());
811 log.debug("RejectResponsesRx---" + aaaStatisticsManager.getAaaStats().getRejectResponsesRx());
812 log.debug("RequestReTx---" + aaaStatisticsManager.getAaaStats().getRequestReTx());
813 log.debug("RequestRttMilis---" + aaaStatisticsManager.getAaaStats().getRequestRttMilis());
814 log.debug("UnknownServerRx---" + aaaStatisticsManager.getAaaStats().getUnknownServerRx());
815 log.debug("UnknownTypeRx---" + aaaStatisticsManager.getAaaStats().getUnknownTypeRx());
816 aaaStatisticsManager.getStatsDelegate().
817 notify(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE,
818 aaaStatisticsManager.getAaaStats()));
819 }
820 }
Ari Saha89831742015-06-26 10:31:48 -0700821}