blob: 35f880d1722917d0907fae3ef48da7ee55d24126 [file] [log] [blame]
Ari Saha89831742015-06-26 10:31:48 -07001/*
Amit Ghoshc9ac1e52017-07-28 12:31:18 +01002 * Copyright 2017-present Open Networking Foundation
Ari Saha89831742015-06-26 10:31:48 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matteo Scandolocf847b82019-04-26 15:00:00 -070016package org.opencord.aaa.impl;
Ari Saha89831742015-06-26 10:31:48 -070017
Saurav Das987441a2018-09-18 16:33:47 -070018import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.net.InetAddress;
kartikey dubeye1545422019-05-22 12:53:45 +000022import java.net.UnknownHostException;
Saurav Das987441a2018-09-18 16:33:47 -070023import java.nio.ByteBuffer;
24import java.util.Map;
kartikey dubeye1545422019-05-22 12:53:45 +000025import java.util.Dictionary;
26import java.util.HashSet;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000027import java.util.Arrays;
Saurav Das987441a2018-09-18 16:33:47 -070028
Jonathan Hart932bedc2018-07-12 13:46:09 -070029import org.apache.commons.lang3.builder.ToStringBuilder;
Ari Saha89831742015-06-26 10:31:48 -070030import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
kartikey dubeye1545422019-05-22 12:53:45 +000032import org.apache.felix.scr.annotations.Property;
Ari Saha89831742015-06-26 10:31:48 -070033import org.apache.felix.scr.annotations.Reference;
34import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart5db44532018-07-12 18:13:54 -070035import org.apache.felix.scr.annotations.Service;
Jonathan Hart4731dd92018-05-02 17:30:05 -070036import org.onlab.packet.DeserializationException;
Jonathan Harta46dddf2015-06-30 15:31:20 -070037import org.onlab.packet.EAP;
38import org.onlab.packet.EAPOL;
39import org.onlab.packet.EthType;
Ari Saha89831742015-06-26 10:31:48 -070040import org.onlab.packet.Ethernet;
Ari Saha89831742015-06-26 10:31:48 -070041import org.onlab.packet.MacAddress;
Jonathan Harta46dddf2015-06-30 15:31:20 -070042import org.onlab.packet.RADIUS;
43import org.onlab.packet.RADIUSAttribute;
kartikey dubeye1545422019-05-22 12:53:45 +000044import org.onlab.util.Tools;
45import org.onosproject.cfg.ComponentConfigService;
Ari Saha89831742015-06-26 10:31:48 -070046import org.onosproject.core.ApplicationId;
47import org.onosproject.core.CoreService;
Jonathan Hart5db44532018-07-12 18:13:54 -070048import org.onosproject.event.AbstractListenerManager;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010049import org.onosproject.mastership.MastershipService;
Ari Saha89831742015-06-26 10:31:48 -070050import org.onosproject.net.ConnectPoint;
51import org.onosproject.net.DeviceId;
Ari Saha89831742015-06-26 10:31:48 -070052import org.onosproject.net.PortNumber;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070053import org.onosproject.net.config.ConfigFactory;
54import org.onosproject.net.config.NetworkConfigEvent;
55import org.onosproject.net.config.NetworkConfigListener;
56import org.onosproject.net.config.NetworkConfigRegistry;
Amit Ghoshf739be52017-09-21 15:49:37 +010057import org.onosproject.net.device.DeviceEvent;
58import org.onosproject.net.device.DeviceListener;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010059import org.onosproject.net.device.DeviceService;
Ari Saha89831742015-06-26 10:31:48 -070060import org.onosproject.net.flow.DefaultTrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070061import org.onosproject.net.flow.TrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070062import org.onosproject.net.packet.DefaultOutboundPacket;
63import org.onosproject.net.packet.InboundPacket;
64import org.onosproject.net.packet.OutboundPacket;
65import org.onosproject.net.packet.PacketContext;
Ari Saha89831742015-06-26 10:31:48 -070066import org.onosproject.net.packet.PacketProcessor;
67import org.onosproject.net.packet.PacketService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070068import org.opencord.aaa.AaaConfig;
69import org.opencord.aaa.AuthenticationEvent;
70import org.opencord.aaa.AuthenticationEventListener;
71import org.opencord.aaa.AuthenticationService;
kartikey dubeye1545422019-05-22 12:53:45 +000072import org.opencord.aaa.AuthenticationStatisticsEvent;
73import org.opencord.aaa.AuthenticationStatisticsService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070074import org.opencord.aaa.RadiusCommunicator;
75import org.opencord.aaa.StateMachineDelegate;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000076import org.opencord.sadis.BaseInformationService;
77import org.opencord.sadis.SadisService;
78import org.opencord.sadis.SubscriberAndDeviceInformation;
kartikey dubeye1545422019-05-22 12:53:45 +000079import org.osgi.service.component.ComponentContext;
Kartikey Dubeyf72e1952019-06-24 07:09:00 +000080import org.apache.felix.scr.annotations.Modified;
81import org.apache.felix.scr.annotations.Activate;
Ari Saha89831742015-06-26 10:31:48 -070082import org.slf4j.Logger;
kartikey dubeye1545422019-05-22 12:53:45 +000083import com.google.common.base.Strings;
Ari Saha89831742015-06-26 10:31:48 -070084
kartikey dubeye1545422019-05-22 12:53:45 +000085import java.util.concurrent.Executors;
86import java.util.concurrent.ScheduledExecutorService;
87import java.util.concurrent.ScheduledFuture;
88import java.util.concurrent.TimeUnit;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000089
90import javax.crypto.Mac;
91import javax.crypto.spec.SecretKeySpec;
Ari Saha89831742015-06-26 10:31:48 -070092/**
Jonathan Harta46dddf2015-06-30 15:31:20 -070093 * AAA application for ONOS.
Ari Saha89831742015-06-26 10:31:48 -070094 */
Jonathan Hart5db44532018-07-12 18:13:54 -070095@Service
Ari Saha89831742015-06-26 10:31:48 -070096@Component(immediate = true)
Jonathan Hart5db44532018-07-12 18:13:54 -070097public class AaaManager
98 extends AbstractListenerManager<AuthenticationEvent, AuthenticationEventListener>
99 implements AuthenticationService {
100
Charles Chandf7ff862017-01-20 11:22:05 -0800101 private static final String APP_NAME = "org.opencord.aaa";
Ray Milkeyf51eba22015-09-25 10:24:23 -0700102
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700103 private final Logger log = getLogger(getClass());
Ray Milkeyf51eba22015-09-25 10:24:23 -0700104
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected CoreService coreService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700107
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected PacketService packetService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700110
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700112 protected NetworkConfigRegistry netCfgService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700113
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected DeviceService deviceService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000118 protected SadisService sadisService;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected MastershipService mastershipService;
122
kartikey dubeye1545422019-05-22 12:53:45 +0000123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected AuthenticationStatisticsService aaaStatisticsManager;
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000125
kartikey dubeye1545422019-05-22 12:53:45 +0000126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected ComponentConfigService cfgService;
128
129 protected AuthenticationStatisticsEventPublisher authenticationStatisticsPublisher;
130 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
Amit Ghoshf739be52017-09-21 15:49:37 +0100131 private final DeviceListener deviceListener = new InternalDeviceListener();
132
kartikey dubeye1545422019-05-22 12:53:45 +0000133 private static final int DEFAULT_REPEAT_DELAY = 20;
134 @Property(name = "statisticsGenerationEvent", intValue = DEFAULT_REPEAT_DELAY,
135 label = "statisticsGenerationEvent")
136 private int statisticsGenerationEvent = DEFAULT_REPEAT_DELAY;
137
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700138 // NAS IP address
139 protected InetAddress nasIpAddress;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100140
141 // self MAC address
Jonathan Hart5db44532018-07-12 18:13:54 -0700142 protected String nasMacAddress;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100143
144 // Parsed RADIUS server addresses
145 protected InetAddress radiusIpAddress;
146
147 // MAC address of RADIUS server or net hop router
148 protected String radiusMacAddress;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700149
150 // RADIUS server secret
151 protected String radiusSecret;
152
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100153 // bindings
154 protected CustomizationInfo customInfo;
Ray Milkey5d99bd12015-10-06 15:41:30 -0700155
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700156 // our application-specific event handler
157 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700158
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700159 // our unique identifier
160 private ApplicationId appId;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700161
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();
283 String s = Tools.get(properties, "statisticsGenerationEvent");
284 statisticsGenerationEvent = Strings.isNullOrEmpty(s) ? DEFAULT_REPEAT_DELAY : Integer.parseInt(s.trim());
285 }
286
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530287 private void configureRadiusCommunication() {
288 if (radiusConnectionType.toLowerCase().equals("socket")) {
289 impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
290 } else {
291 impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService,
292 deviceService, subsService, pktCustomizer, this);
293 }
294 }
295
296 private void configurePacketCustomizer() {
297 switch (customizer.toLowerCase()) {
298 case "sample":
299 pktCustomizer = new SamplePacketCustomizer(customInfo);
300 log.info("Created SamplePacketCustomizer");
301 break;
Saurav Dase72358a2018-11-13 21:56:46 -0800302 case "att":
303 pktCustomizer = new AttPacketCustomizer(customInfo);
304 log.info("Created AttPacketCustomizer");
305 break;
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530306 default:
307 pktCustomizer = new PacketCustomizer(customInfo);
308 log.info("Created default PacketCustomizer");
309 break;
310 }
311 }
312
kartikey dubeye1545422019-05-22 12:53:45 +0000313 private void getConfiguredAaaServerAddress() {
314 try {
315 InetAddress address;
316 if (newCfg.radiusHostName() != null) {
317 address = InetAddress.getByName(newCfg.radiusHostName());
318 } else {
319 address = newCfg.radiusIp();
320 }
321
322 configuredAaaServerAddress = address.getHostAddress();
323 } catch (UnknownHostException uhe) {
324 log.warn("Unable to resolve host {}", newCfg.radiusHostName());
325 }
326 }
327
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000328 private void checkReceivedPacketForValidValidator(RADIUS radiusPacket, byte[] requestAuthenticator) {
329 if (!checkResponseMessageAuthenticator(radiusSecret, radiusPacket, requestAuthenticator)) {
kartikey dubeye1545422019-05-22 12:53:45 +0000330 aaaStatisticsManager.getAaaStats().increaseInvalidValidatorsRx();
331 }
332 }
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000333
334 private boolean checkResponseMessageAuthenticator(String key, RADIUS radiusPacket, byte[] requestAuthenticator) {
335 byte[] newHash = new byte[16];
336 Arrays.fill(newHash, (byte) 0);
337 byte[] messageAuthenticator = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue();
338 byte[] authenticator = radiusPacket.getAuthenticator();
339 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash);
340 radiusPacket.setAuthenticator(requestAuthenticator);
341 // Calculate the MD5 HMAC based on the message
342 try {
343 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
344 Mac mac = Mac.getInstance("HmacMD5");
345 mac.init(keySpec);
346 newHash = mac.doFinal(radiusPacket.serialize());
347 } catch (Exception e) {
348 log.error("Failed to generate message authenticator: {}", e.getMessage());
349 }
350 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator);
351 radiusPacket.setAuthenticator(authenticator);
352 // Compare the calculated Message-Authenticator with the one in the message
353 return Arrays.equals(newHash, messageAuthenticator);
354 }
kartikey dubeye1545422019-05-22 12:53:45 +0000355 public void checkForPacketFromUnknownServer(String hostAddress) {
356 if (!hostAddress.equals(configuredAaaServerAddress)) {
Vijaykumar Kushwahafffd3d12019-08-01 11:09:59 +0000357 getConfiguredAaaServerAddress();
358 if (!hostAddress.equals(configuredAaaServerAddress)) {
359 aaaStatisticsManager.getAaaStats().incrementUnknownServerRx();
360 }
kartikey dubeye1545422019-05-22 12:53:45 +0000361 }
362 }
363
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100364 /**
365 * Send RADIUS packet to the RADIUS server.
366 *
367 * @param radiusPacket RADIUS packet to be sent to server.
368 * @param inPkt Incoming EAPOL packet
369 */
370 protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) {
kartikey dubeye1545422019-05-22 12:53:45 +0000371 outPacketSet.add(radiusPacket.getIdentifier());
372 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(true);
373 aaaStatisticsManager.getAaaStats().increaseAccessRequestsTx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100374 impl.sendRadiusPacket(radiusPacket, inPkt);
375 }
Ray Milkey967776a2015-10-07 14:37:17 -0700376
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100377 /**
378 * Handles RADIUS packets.
379 *
380 * @param radiusPacket RADIUS packet coming from the RADIUS server.
381 * @throws StateMachineException if an illegal state transition is triggered
Jonathan Hart4731dd92018-05-02 17:30:05 -0700382 * @throws DeserializationException if packet deserialization fails
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100383 */
Jonathan Hart4731dd92018-05-02 17:30:05 -0700384 public void handleRadiusPacket(RADIUS radiusPacket)
385 throws StateMachineException, DeserializationException {
Saurav Das987441a2018-09-18 16:33:47 -0700386 if (log.isTraceEnabled()) {
387 log.trace("Received RADIUS packet {}", radiusPacket);
388 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100389 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
390 if (stateMachine == null) {
Saurav Das987441a2018-09-18 16:33:47 -0700391 log.error("Invalid packet identifier {}, could not find corresponding "
392 + "state machine ... exiting", radiusPacket.getIdentifier());
kartikey dubeye1545422019-05-22 12:53:45 +0000393 aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired();
394 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100395 return;
Ray Milkey967776a2015-10-07 14:37:17 -0700396 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100397 EAP eapPayload;
398 Ethernet eth;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000399 checkReceivedPacketForValidValidator(radiusPacket, stateMachine.requestAuthenticator());
kartikey dubeye1545422019-05-22 12:53:45 +0000400 if (outPacketSet.contains(radiusPacket.getIdentifier())) {
401 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(false);
402 outPacketSet.remove(new Byte(radiusPacket.getIdentifier()));
403 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100404 switch (radiusPacket.getCode()) {
405 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400406 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100407 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
408 byte[] challengeState = null;
409 if (radiusAttrState != null) {
410 challengeState = radiusAttrState.getValue();
411 }
412 eapPayload = radiusPacket.decapsulateMessage();
413 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
414 eth = buildEapolResponse(stateMachine.supplicantAddress(),
415 MacAddress.valueOf(nasMacAddress),
416 stateMachine.vlanId(),
417 EAPOL.EAPOL_PACKET,
418 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400419 log.debug("Send EAP challenge response to supplicant {}", stateMachine.supplicantAddress().toString());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100420 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
kartikey dubeye1545422019-05-22 12:53:45 +0000421 aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100422 break;
423 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400424 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100425 //send an EAPOL - Success to the supplicant.
426 byte[] eapMessageSuccess =
427 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700428 eapPayload = EAP.deserializer().deserialize(
429 eapMessageSuccess, 0, eapMessageSuccess.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100430 eth = buildEapolResponse(stateMachine.supplicantAddress(),
431 MacAddress.valueOf(nasMacAddress),
432 stateMachine.vlanId(),
433 EAPOL.EAPOL_PACKET,
434 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400435 log.info("Send EAP success message to supplicant {}", stateMachine.supplicantAddress().toString());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100436 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
437
438 stateMachine.authorizeAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000439 aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100440 break;
441 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400442 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100443 //send an EAPOL - Failure to the supplicant.
444 byte[] eapMessageFailure;
445 eapPayload = new EAP();
446 RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE);
447 if (radiusAttrEap == null) {
448 eapPayload.setCode(EAP.FAILURE);
449 eapPayload.setIdentifier(stateMachine.challengeIdentifier());
450 eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL);
451 } else {
452 eapMessageFailure = radiusAttrEap.getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700453 eapPayload = EAP.deserializer().deserialize(
454 eapMessageFailure, 0, eapMessageFailure.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100455 }
456 eth = buildEapolResponse(stateMachine.supplicantAddress(),
457 MacAddress.valueOf(nasMacAddress),
458 stateMachine.vlanId(),
459 EAPOL.EAPOL_PACKET,
460 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400461 log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100462 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
463 stateMachine.denyAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000464 aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100465 break;
466 default:
467 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
kartikey dubeye1545422019-05-22 12:53:45 +0000468 aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100469 }
kartikey dubeye1545422019-05-22 12:53:45 +0000470 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700471 }
472
Ray Milkey967776a2015-10-07 14:37:17 -0700473 /**
474 * Send the ethernet packet to the supplicant.
475 *
476 * @param ethernetPkt the ethernet packet
477 * @param connectPoint the connect point to send out
478 */
479 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) {
480 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
481 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
482 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
Saurav Das987441a2018-09-18 16:33:47 -0700483 if (log.isTraceEnabled()) {
484 EAPOL eap = ((EAPOL) ethernetPkt.getPayload());
485 log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}",
486 eap, ethernetPkt, connectPoint);
487 }
Ray Milkey967776a2015-10-07 14:37:17 -0700488 packetService.emit(packet);
489 }
490
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400491 @Override
492 public String toString() {
493 return ToStringBuilder.reflectionToString(this);
494 }
495
Ari Saha89831742015-06-26 10:31:48 -0700496 // our handler defined as a private inner class
497
498 /**
499 * Packet processor responsible for forwarding packets along their paths.
500 */
501 private class ReactivePacketProcessor implements PacketProcessor {
502 @Override
503 public void process(PacketContext context) {
504
505 // Extract the original Ethernet frame from the packet information
506 InboundPacket pkt = context.inPacket();
507 Ethernet ethPkt = pkt.parsed();
508 if (ethPkt == null) {
509 return;
510 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100511
Ray Milkeyf51eba22015-09-25 10:24:23 -0700512 try {
513 // identify if incoming packet comes from supplicant (EAP) or RADIUS
514 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
515 case EAPOL:
516 handleSupplicantPacket(context.inPacket());
517 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700518 default:
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100519 // any other packets let the specific implementation handle
520 impl.handlePacketFromServer(context);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700521 }
Ray Milkey967776a2015-10-07 14:37:17 -0700522 } catch (StateMachineException e) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100523 log.warn("Unable to process packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700524 }
525 }
526
Ray Milkey9eb293f2015-09-30 15:09:17 -0700527 /**
528 * Creates and initializes common fields of a RADIUS packet.
529 *
Ray Milkey967776a2015-10-07 14:37:17 -0700530 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700531 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700532 * @return RADIUS packet
533 */
Ray Milkey967776a2015-10-07 14:37:17 -0700534 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700535 RADIUS radiusPayload =
536 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
537 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700538
539 // set Request Authenticator in StateMachine
540 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
541
Ray Milkey9eb293f2015-09-30 15:09:17 -0700542 radiusPayload.setIdentifier(identifier);
543 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700544 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700545
546 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800547 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700548
549 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700550
551 return radiusPayload;
552 }
Ari Saha89831742015-06-26 10:31:48 -0700553
554 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700555 * Handles PAE packets (supplicant).
556 *
557 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700558 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700559 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700560 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700561 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800562 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700563
Jonathan Harta46dddf2015-06-30 15:31:20 -0700564 DeviceId deviceId = inPacket.receivedFrom().deviceId();
565 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700566 String sessionId = deviceId.toString() + portNumber.toString();
Saurav Das987441a2018-09-18 16:33:47 -0700567 EAPOL eapol = (EAPOL) ethPkt.getPayload();
568 if (log.isTraceEnabled()) {
569 log.trace("Received EAPOL packet {} in enclosing packet {} from "
570 + "dev/port: {}/{}", eapol, ethPkt, deviceId,
571 portNumber);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100572 }
Jonathan Harta46dddf2015-06-30 15:31:20 -0700573
Saurav Das987441a2018-09-18 16:33:47 -0700574 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
575 if (stateMachine == null) {
576 log.debug("Creating new state machine for sessionId: {} for "
577 + "dev/port: {}/{}", sessionId, deviceId, portNumber);
578 stateMachine = new StateMachine(sessionId);
579 } else {
580 log.debug("Using existing state-machine for sessionId: {}", sessionId);
581 }
582
Ari Saha89831742015-06-26 10:31:48 -0700583 switch (eapol.getEapolType()) {
584 case EAPOL.EAPOL_START:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400585 log.debug("EAP packet: EAPOL_START");
Ray Milkeyf51eba22015-09-25 10:24:23 -0700586 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Jonathan Hart5db44532018-07-12 18:13:54 -0700587 stateMachine.start();
Ari Saha89831742015-06-26 10:31:48 -0700588
Ray Milkeyf51eba22015-09-25 10:24:23 -0700589 //send an EAP Request/Identify to the supplicant
590 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100591 if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
592 stateMachine.setPriorityCode(ethPkt.getPriorityCode());
593 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800594 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700595 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100596 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400597
Jonathan Hart092dfb22015-11-16 23:05:21 -0800598 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700599 stateMachine.setVlanId(ethPkt.getVlanID());
Ari Saha89831742015-06-26 10:31:48 -0700600
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400601 log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString());
Ray Milkey967776a2015-10-07 14:37:17 -0700602 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint());
Ari Saha89831742015-06-26 10:31:48 -0700603
604 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800605 case EAPOL.EAPOL_LOGOFF:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400606 log.debug("EAP packet: EAPOL_LOGOFF");
Ray Milkeyb34b4962016-01-04 10:24:43 -0800607 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
608 stateMachine.logoff();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800609 }
610
611 break;
Ari Saha89831742015-06-26 10:31:48 -0700612 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700613 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700614 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700615 EAP eapPacket = (EAP) eapol.getPayload();
616
617 byte dataType = eapPacket.getDataType();
618 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700619
Ray Milkey9eb293f2015-09-30 15:09:17 -0700620 case EAP.ATTR_IDENTITY:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400621 log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700622 // request id access to RADIUS
623 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700624
Ray Milkey967776a2015-10-07 14:37:17 -0700625 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100626 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800627 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700628
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100629 sendRadiusPacket(radiusPayload, inPacket);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700630
Ray Milkey9eb293f2015-09-30 15:09:17 -0700631 // change the state to "PENDING"
kartikey dubeye1545422019-05-22 12:53:45 +0000632 if (stateMachine.state() == StateMachine.STATE_PENDING) {
633 aaaStatisticsManager.getAaaStats().increaseRequestReTx();
634 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700635 stateMachine.requestAccess();
636 break;
Ari Saha89831742015-06-26 10:31:48 -0700637 case EAP.ATTR_MD5:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400638 log.debug("EAP packet: EAPOL_PACKET ATTR_MD5");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700639 // verify if the EAP identifier corresponds to the
640 // challenge identifier from the client state
641 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700642 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700643 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700644 radiusPayload =
645 getRadiusPayload(stateMachine,
646 stateMachine.identifier(),
647 eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100648 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700649
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800650 if (stateMachine.challengeState() != null) {
651 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
652 stateMachine.challengeState());
653 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800654 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100655 sendRadiusPacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700656 }
657 break;
658 case EAP.ATTR_TLS:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400659 log.debug("EAP packet: EAPOL_PACKET ATTR_TLS");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700660 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700661 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100662 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700663
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800664 if (stateMachine.challengeState() != null) {
665 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
666 stateMachine.challengeState());
667 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700668 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700669
Jonathan Hart092dfb22015-11-16 23:05:21 -0800670 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100671 sendRadiusPacket(radiusPayload, inPacket);
Ray Milkey5493b512015-10-21 12:13:49 -0700672
Ray Milkeyf3790b82015-10-21 16:28:08 -0700673 if (stateMachine.state() != StateMachine.STATE_PENDING) {
674 stateMachine.requestAccess();
675 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700676
Ari Saha89831742015-06-26 10:31:48 -0700677 break;
678 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400679 log.warn("Unknown EAP packet type");
Ari Saha89831742015-06-26 10:31:48 -0700680 return;
681 }
682 break;
683 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400684 log.debug("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700685 }
686 }
Ray Milkey967776a2015-10-07 14:37:17 -0700687 }
688
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100689 /**
Jonathan Hart5db44532018-07-12 18:13:54 -0700690 * Delegate allowing the StateMachine to notify us of events.
691 */
692 private class InternalStateMachineDelegate implements StateMachineDelegate {
693
694 @Override
695 public void notify(AuthenticationEvent authenticationEvent) {
696 log.info("Auth event {} for {}",
697 authenticationEvent.type(), authenticationEvent.subject());
698 post(authenticationEvent);
699 }
700 }
701
702 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100703 * Configuration Listener, handles change in configuration.
704 */
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700705 private class InternalConfigListener implements NetworkConfigListener {
706
707 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800708 * Reconfigures the AAA application according to the
709 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700710 *
711 * @param cfg configuration object
712 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800713 private void reconfigureNetwork(AaaConfig cfg) {
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400714 log.info("Reconfiguring AaaConfig from config: {}", cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100715
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700716 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800717 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700718 } else {
719 newCfg = cfg;
720 }
721 if (newCfg.nasIp() != null) {
722 nasIpAddress = newCfg.nasIp();
723 }
724 if (newCfg.radiusIp() != null) {
725 radiusIpAddress = newCfg.radiusIp();
726 }
727 if (newCfg.radiusMac() != null) {
728 radiusMacAddress = newCfg.radiusMac();
729 }
730 if (newCfg.nasMac() != null) {
731 nasMacAddress = newCfg.nasMac();
732 }
733 if (newCfg.radiusSecret() != null) {
734 radiusSecret = newCfg.radiusSecret();
735 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100736
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530737 boolean reconfigureCustomizer = false;
738 if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) {
739 customizer = newCfg.radiusPktCustomizer();
740 configurePacketCustomizer();
741 reconfigureCustomizer = true;
742 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100743
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530744 if (radiusConnectionType == null
745 || reconfigureCustomizer
746 || !radiusConnectionType.equals(newCfg.radiusConnectionType())) {
747 radiusConnectionType = newCfg.radiusConnectionType();
748 if (impl != null) {
749 impl.withdrawIntercepts();
750 impl.clearLocalState();
751 }
752 configureRadiusCommunication();
753 impl.initializeLocalState(newCfg);
754 impl.requestIntercepts();
755 } else if (impl != null) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100756 impl.clearLocalState();
757 impl.initializeLocalState(newCfg);
Ray Milkey5d99bd12015-10-06 15:41:30 -0700758 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700759 }
760
761 @Override
762 public void event(NetworkConfigEvent event) {
763
764 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
765 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800766 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700767
Jonathan Hart092dfb22015-11-16 23:05:21 -0800768 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700769 reconfigureNetwork(cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100770
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400771 log.info("Reconfigured: {}", cfg.toString());
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700772 }
773 }
774 }
Amit Ghoshf739be52017-09-21 15:49:37 +0100775
776 private class InternalDeviceListener implements DeviceListener {
777 @Override
778 public void event(DeviceEvent event) {
779
780 switch (event.type()) {
781 case PORT_REMOVED:
782 DeviceId devId = event.subject().id();
783 PortNumber portNumber = event.port().number();
784 String sessionId = devId.toString() + portNumber.toString();
785
786 Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap();
787 StateMachine removed = sessionIdMap.remove(sessionId);
788 if (removed != null) {
789 StateMachine.deleteStateMachineMapping(removed);
790 }
791
792 break;
793 default:
794 return;
795 }
796 }
797 }
kartikey dubeye1545422019-05-22 12:53:45 +0000798 private class AuthenticationStatisticsEventPublisher implements Runnable {
799 private final Logger log = getLogger(getClass());
800 public void run() {
801 log.info("Notifying AuthenticationStatisticsEvent");
802 aaaStatisticsManager.calculatePacketRoundtripTime();
803 log.debug("AcceptResponsesRx---" + aaaStatisticsManager.getAaaStats().getAcceptResponsesRx());
804 log.debug("AccessRequestsTx---" + aaaStatisticsManager.getAaaStats().getAccessRequestsTx());
805 log.debug("ChallengeResponsesRx---" + aaaStatisticsManager.getAaaStats().getChallengeResponsesRx());
806 log.debug("DroppedResponsesRx---" + aaaStatisticsManager.getAaaStats().getDroppedResponsesRx());
807 log.debug("InvalidValidatorsRx---" + aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx());
808 log.debug("MalformedResponsesRx---" + aaaStatisticsManager.getAaaStats().getMalformedResponsesRx());
809 log.debug("PendingRequests---" + aaaStatisticsManager.getAaaStats().getPendingRequests());
810 log.debug("RejectResponsesRx---" + aaaStatisticsManager.getAaaStats().getRejectResponsesRx());
811 log.debug("RequestReTx---" + aaaStatisticsManager.getAaaStats().getRequestReTx());
812 log.debug("RequestRttMilis---" + aaaStatisticsManager.getAaaStats().getRequestRttMilis());
813 log.debug("UnknownServerRx---" + aaaStatisticsManager.getAaaStats().getUnknownServerRx());
814 log.debug("UnknownTypeRx---" + aaaStatisticsManager.getAaaStats().getUnknownTypeRx());
815 aaaStatisticsManager.getStatsDelegate().
816 notify(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE,
817 aaaStatisticsManager.getAaaStats()));
818 }
819 }
Ari Saha89831742015-06-26 10:31:48 -0700820}