blob: ff3c812f4467209e677ac48c1a9e8903081d6f28 [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;
Shubham Sharma47869542019-09-13 10:39:47 +000028import java.util.List;
Saurav Das987441a2018-09-18 16:33:47 -070029
Jonathan Hart932bedc2018-07-12 13:46:09 -070030import org.apache.commons.lang3.builder.ToStringBuilder;
Ari Saha89831742015-06-26 10:31:48 -070031import org.apache.felix.scr.annotations.Component;
32import org.apache.felix.scr.annotations.Deactivate;
kartikey dubeye1545422019-05-22 12:53:45 +000033import org.apache.felix.scr.annotations.Property;
Ari Saha89831742015-06-26 10:31:48 -070034import org.apache.felix.scr.annotations.Reference;
35import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart5db44532018-07-12 18:13:54 -070036import org.apache.felix.scr.annotations.Service;
Jonathan Hart4731dd92018-05-02 17:30:05 -070037import org.onlab.packet.DeserializationException;
Jonathan Harta46dddf2015-06-30 15:31:20 -070038import org.onlab.packet.EAP;
39import org.onlab.packet.EAPOL;
40import org.onlab.packet.EthType;
Ari Saha89831742015-06-26 10:31:48 -070041import org.onlab.packet.Ethernet;
Ari Saha89831742015-06-26 10:31:48 -070042import org.onlab.packet.MacAddress;
Jonathan Harta46dddf2015-06-30 15:31:20 -070043import org.onlab.packet.RADIUS;
44import org.onlab.packet.RADIUSAttribute;
kartikey dubeye1545422019-05-22 12:53:45 +000045import org.onlab.util.Tools;
46import org.onosproject.cfg.ComponentConfigService;
Ari Saha89831742015-06-26 10:31:48 -070047import org.onosproject.core.ApplicationId;
48import org.onosproject.core.CoreService;
Jonathan Hart5db44532018-07-12 18:13:54 -070049import org.onosproject.event.AbstractListenerManager;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010050import org.onosproject.mastership.MastershipService;
Ari Saha89831742015-06-26 10:31:48 -070051import org.onosproject.net.ConnectPoint;
52import org.onosproject.net.DeviceId;
Ari Saha89831742015-06-26 10:31:48 -070053import org.onosproject.net.PortNumber;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070054import org.onosproject.net.config.ConfigFactory;
55import org.onosproject.net.config.NetworkConfigEvent;
56import org.onosproject.net.config.NetworkConfigListener;
57import org.onosproject.net.config.NetworkConfigRegistry;
Amit Ghoshf739be52017-09-21 15:49:37 +010058import org.onosproject.net.device.DeviceEvent;
59import org.onosproject.net.device.DeviceListener;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010060import org.onosproject.net.device.DeviceService;
Ari Saha89831742015-06-26 10:31:48 -070061import org.onosproject.net.flow.DefaultTrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070062import org.onosproject.net.flow.TrafficTreatment;
Ari Saha89831742015-06-26 10:31:48 -070063import org.onosproject.net.packet.DefaultOutboundPacket;
64import org.onosproject.net.packet.InboundPacket;
65import org.onosproject.net.packet.OutboundPacket;
66import org.onosproject.net.packet.PacketContext;
Ari Saha89831742015-06-26 10:31:48 -070067import org.onosproject.net.packet.PacketProcessor;
68import org.onosproject.net.packet.PacketService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070069import org.opencord.aaa.AaaConfig;
70import org.opencord.aaa.AuthenticationEvent;
71import org.opencord.aaa.AuthenticationEventListener;
72import org.opencord.aaa.AuthenticationService;
kartikey dubeye1545422019-05-22 12:53:45 +000073import org.opencord.aaa.AuthenticationStatisticsEvent;
74import org.opencord.aaa.AuthenticationStatisticsService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070075import org.opencord.aaa.RadiusCommunicator;
76import org.opencord.aaa.StateMachineDelegate;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000077import org.opencord.sadis.BaseInformationService;
78import org.opencord.sadis.SadisService;
79import org.opencord.sadis.SubscriberAndDeviceInformation;
kartikey dubeye1545422019-05-22 12:53:45 +000080import org.osgi.service.component.ComponentContext;
Kartikey Dubeyf72e1952019-06-24 07:09:00 +000081import org.apache.felix.scr.annotations.Modified;
82import org.apache.felix.scr.annotations.Activate;
Ari Saha89831742015-06-26 10:31:48 -070083import org.slf4j.Logger;
kartikey dubeye1545422019-05-22 12:53:45 +000084import com.google.common.base.Strings;
Ari Saha89831742015-06-26 10:31:48 -070085
kartikey dubeye1545422019-05-22 12:53:45 +000086import java.util.concurrent.Executors;
87import java.util.concurrent.ScheduledExecutorService;
88import java.util.concurrent.ScheduledFuture;
89import java.util.concurrent.TimeUnit;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +000090
91import 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 */
Jonathan Hart5db44532018-07-12 18:13:54 -070096@Service
Ari Saha89831742015-06-26 10:31:48 -070097@Component(immediate = true)
Jonathan Hart5db44532018-07-12 18:13:54 -070098public class AaaManager
99 extends AbstractListenerManager<AuthenticationEvent, AuthenticationEventListener>
100 implements AuthenticationService {
101
Charles Chandf7ff862017-01-20 11:22:05 -0800102 private static final String APP_NAME = "org.opencord.aaa";
Ray Milkeyf51eba22015-09-25 10:24:23 -0700103
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700104 private final Logger log = getLogger(getClass());
Ray Milkeyf51eba22015-09-25 10:24:23 -0700105
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected CoreService coreService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700108
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected PacketService packetService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700111
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700113 protected NetworkConfigRegistry netCfgService;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700114
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected DeviceService deviceService;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000119 protected SadisService sadisService;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected MastershipService mastershipService;
123
kartikey dubeye1545422019-05-22 12:53:45 +0000124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected AuthenticationStatisticsService aaaStatisticsManager;
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000126
kartikey dubeye1545422019-05-22 12:53:45 +0000127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected ComponentConfigService cfgService;
129
130 protected AuthenticationStatisticsEventPublisher authenticationStatisticsPublisher;
131 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
Amit Ghoshf739be52017-09-21 15:49:37 +0100132 private final DeviceListener deviceListener = new InternalDeviceListener();
133
kartikey dubeye1545422019-05-22 12:53:45 +0000134 private static final int DEFAULT_REPEAT_DELAY = 20;
135 @Property(name = "statisticsGenerationEvent", intValue = DEFAULT_REPEAT_DELAY,
136 label = "statisticsGenerationEvent")
137 private int statisticsGenerationEvent = DEFAULT_REPEAT_DELAY;
138
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700139 // NAS IP address
140 protected InetAddress nasIpAddress;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100141
142 // self MAC address
Jonathan Hart5db44532018-07-12 18:13:54 -0700143 protected String nasMacAddress;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100144
145 // Parsed RADIUS server addresses
146 protected InetAddress radiusIpAddress;
147
148 // MAC address of RADIUS server or net hop router
149 protected String radiusMacAddress;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700150
151 // RADIUS server secret
152 protected String radiusSecret;
153
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100154 // bindings
155 protected CustomizationInfo customInfo;
Ray Milkey5d99bd12015-10-06 15:41:30 -0700156
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700157 // our application-specific event handler
158 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700159
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700160 // our unique identifier
161 private ApplicationId appId;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700162
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000163 // TimeOut time for cleaning up stateMachines stuck due to pending AAA/EAPOL message.
164 protected int cleanupTimerTimeOutInMins;
165
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100166 // Setup specific customization/attributes on the RADIUS packets
167 PacketCustomizer pktCustomizer;
Ray Milkey967776a2015-10-07 14:37:17 -0700168
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100169 // packet customizer to use
170 private String customizer;
171
172 // Type of connection to use to communicate with Radius server, options are
173 // "socket" or "packet_out"
174 private String radiusConnectionType;
175
Jonathan Hart5db44532018-07-12 18:13:54 -0700176 // Object for the specific type of communication with the RADIUS
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100177 // server, socket based or packet_out based
178 RadiusCommunicator impl = null;
179
180 // latest configuration
181 AaaConfig newCfg;
Ray Milkey967776a2015-10-07 14:37:17 -0700182
kartikey dubeye1545422019-05-22 12:53:45 +0000183 ScheduledFuture<?> scheduledFuture;
184
185 ScheduledExecutorService executor;
186 String configuredAaaServerAddress;
187 HashSet<Byte> outPacketSet = new HashSet<Byte>();
Shubham Sharma47869542019-09-13 10:39:47 +0000188 HashSet<Byte> outPacketSupp = new HashSet<Byte>();
189 static final List<Byte> VALID_EAPOL_TYPE = Arrays.asList(EAPOL.EAPOL_START, EAPOL.EAPOL_LOGOFF, EAPOL.EAPOL_PACKET);
190 static final int HEADER_LENGTH = 4;
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700191 // Configuration properties factory
192 private final ConfigFactory factory =
Jonathan Hart092dfb22015-11-16 23:05:21 -0800193 new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY,
194 AaaConfig.class,
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700195 "AAA") {
196 @Override
Jonathan Hart092dfb22015-11-16 23:05:21 -0800197 public AaaConfig createConfig() {
198 return new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700199 }
200 };
Ray Milkeyf51eba22015-09-25 10:24:23 -0700201
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700202 // Listener for config changes
203 private final InternalConfigListener cfgListener = new InternalConfigListener();
Ari Saha89831742015-06-26 10:31:48 -0700204
Jonathan Hart5db44532018-07-12 18:13:54 -0700205 private StateMachineDelegate delegate = new InternalStateMachineDelegate();
206
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700207 /**
208 * Builds an EAPOL packet based on the given parameters.
209 *
210 * @param dstMac destination MAC address
211 * @param srcMac source MAC address
212 * @param vlan vlan identifier
213 * @param eapolType EAPOL type
214 * @param eap EAP payload
215 * @return Ethernet frame
216 */
217 private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100218 short vlan, byte eapolType, EAP eap, byte priorityCode) {
Ari Saha89831742015-06-26 10:31:48 -0700219
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700220 Ethernet eth = new Ethernet();
221 eth.setDestinationMACAddress(dstMac.toBytes());
222 eth.setSourceMACAddress(srcMac.toBytes());
223 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
224 if (vlan != Ethernet.VLAN_UNTAGGED) {
225 eth.setVlanID(vlan);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100226 eth.setPriorityCode(priorityCode);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700227 }
228 //eapol header
229 EAPOL eapol = new EAPOL();
230 eapol.setEapolType(eapolType);
231 eapol.setPacketLength(eap.getLength());
Ari Saha89831742015-06-26 10:31:48 -0700232
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700233 //eap part
234 eapol.setPayload(eap);
235
236 eth.setPayload(eapol);
237 eth.setPad(true);
238 return eth;
239 }
Ari Saha89831742015-06-26 10:31:48 -0700240
Ari Saha89831742015-06-26 10:31:48 -0700241 @Activate
kartikey dubeye1545422019-05-22 12:53:45 +0000242 public void activate(ComponentContext context) {
Charles Chandf7ff862017-01-20 11:22:05 -0800243 appId = coreService.registerApplication(APP_NAME);
Jonathan Hart5db44532018-07-12 18:13:54 -0700244 eventDispatcher.addSink(AuthenticationEvent.class, listenerRegistry);
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400245 netCfgService.addListener(cfgListener);
246 netCfgService.registerConfigFactory(factory);
kartikey dubeye1545422019-05-22 12:53:45 +0000247 cfgService.registerProperties(getClass());
248 modified(context);
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000249 subsService = sadisService.getSubscriberInfoService();
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530250 customInfo = new CustomizationInfo(subsService, deviceService);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800251 cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class));
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400252 log.info("Starting with config {} {}", this, newCfg);
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530253 configureRadiusCommunication();
Ari Saha89831742015-06-26 10:31:48 -0700254 // register our event handler
Brian O'Connord9c7da02015-07-29 17:49:24 -0700255 packetService.addProcessor(processor, PacketProcessor.director(2));
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700256 StateMachine.initializeMaps();
Jonathan Hart5db44532018-07-12 18:13:54 -0700257 StateMachine.setDelegate(delegate);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000258 cleanupTimerTimeOutInMins = newCfg.sessionCleanupTimer();
259 StateMachine.setcleanupTimerTimeOutInMins(cleanupTimerTimeOutInMins);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100260 impl.initializeLocalState(newCfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100261 impl.requestIntercepts();
Amit Ghoshf739be52017-09-21 15:49:37 +0100262 deviceService.addListener(deviceListener);
kartikey dubeye1545422019-05-22 12:53:45 +0000263 getConfiguredAaaServerAddress();
264 authenticationStatisticsPublisher =
265 new AuthenticationStatisticsEventPublisher();
266 executor = Executors.newScheduledThreadPool(1);
267 scheduledFuture = executor.scheduleAtFixedRate(authenticationStatisticsPublisher,
268 0, statisticsGenerationEvent, TimeUnit.SECONDS);
Amit Ghoshf739be52017-09-21 15:49:37 +0100269
Jian Li13c67162015-12-09 13:20:34 -0800270 log.info("Started");
Ari Saha89831742015-06-26 10:31:48 -0700271 }
272
273 @Deactivate
kartikey dubeye1545422019-05-22 12:53:45 +0000274 public void deactivate(ComponentContext context) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100275 impl.withdrawIntercepts();
Ari Saha89831742015-06-26 10:31:48 -0700276 packetService.removeProcessor(processor);
Deepa Vaddireddyb9c24c62017-09-21 13:45:30 +0530277 netCfgService.removeListener(cfgListener);
kartikey dubeye1545422019-05-22 12:53:45 +0000278 cfgService.unregisterProperties(getClass(), false);
Jonathan Hart5db44532018-07-12 18:13:54 -0700279 StateMachine.unsetDelegate(delegate);
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530280 StateMachine.destroyMaps();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100281 impl.deactivate();
Amit Ghoshf739be52017-09-21 15:49:37 +0100282 deviceService.removeListener(deviceListener);
Jonathan Hart5db44532018-07-12 18:13:54 -0700283 eventDispatcher.removeSink(AuthenticationEvent.class);
kartikey dubeye1545422019-05-22 12:53:45 +0000284 scheduledFuture.cancel(true);
285 executor.shutdown();
Jian Li13c67162015-12-09 13:20:34 -0800286 log.info("Stopped");
Ray Milkey967776a2015-10-07 14:37:17 -0700287 }
288
kartikey dubeye1545422019-05-22 12:53:45 +0000289 @Modified
290 public void modified(ComponentContext context) {
291 Dictionary<?, ?> properties = context.getProperties();
292 String s = Tools.get(properties, "statisticsGenerationEvent");
293 statisticsGenerationEvent = Strings.isNullOrEmpty(s) ? DEFAULT_REPEAT_DELAY : Integer.parseInt(s.trim());
294 }
295
Shubham Sharma1ad16632019-11-26 11:09:21 +0000296 protected void configureRadiusCommunication() {
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530297 if (radiusConnectionType.toLowerCase().equals("socket")) {
298 impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
299 } else {
300 impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService,
301 deviceService, subsService, pktCustomizer, this);
302 }
303 }
304
305 private void configurePacketCustomizer() {
306 switch (customizer.toLowerCase()) {
307 case "sample":
308 pktCustomizer = new SamplePacketCustomizer(customInfo);
309 log.info("Created SamplePacketCustomizer");
310 break;
Saurav Dase72358a2018-11-13 21:56:46 -0800311 case "att":
312 pktCustomizer = new AttPacketCustomizer(customInfo);
313 log.info("Created AttPacketCustomizer");
314 break;
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530315 default:
316 pktCustomizer = new PacketCustomizer(customInfo);
317 log.info("Created default PacketCustomizer");
318 break;
319 }
320 }
321
kartikey dubeye1545422019-05-22 12:53:45 +0000322 private void getConfiguredAaaServerAddress() {
323 try {
324 InetAddress address;
325 if (newCfg.radiusHostName() != null) {
326 address = InetAddress.getByName(newCfg.radiusHostName());
327 } else {
328 address = newCfg.radiusIp();
329 }
330
331 configuredAaaServerAddress = address.getHostAddress();
332 } catch (UnknownHostException uhe) {
333 log.warn("Unable to resolve host {}", newCfg.radiusHostName());
334 }
335 }
336
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000337 private void checkReceivedPacketForValidValidator(RADIUS radiusPacket, byte[] requestAuthenticator) {
338 if (!checkResponseMessageAuthenticator(radiusSecret, radiusPacket, requestAuthenticator)) {
kartikey dubeye1545422019-05-22 12:53:45 +0000339 aaaStatisticsManager.getAaaStats().increaseInvalidValidatorsRx();
340 }
341 }
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000342
343 private boolean checkResponseMessageAuthenticator(String key, RADIUS radiusPacket, byte[] requestAuthenticator) {
344 byte[] newHash = new byte[16];
345 Arrays.fill(newHash, (byte) 0);
346 byte[] messageAuthenticator = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue();
347 byte[] authenticator = radiusPacket.getAuthenticator();
348 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash);
349 radiusPacket.setAuthenticator(requestAuthenticator);
350 // Calculate the MD5 HMAC based on the message
351 try {
352 SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5");
353 Mac mac = Mac.getInstance("HmacMD5");
354 mac.init(keySpec);
355 newHash = mac.doFinal(radiusPacket.serialize());
356 } catch (Exception e) {
357 log.error("Failed to generate message authenticator: {}", e.getMessage());
358 }
359 radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator);
360 radiusPacket.setAuthenticator(authenticator);
361 // Compare the calculated Message-Authenticator with the one in the message
362 return Arrays.equals(newHash, messageAuthenticator);
363 }
kartikey dubeye1545422019-05-22 12:53:45 +0000364 public void checkForPacketFromUnknownServer(String hostAddress) {
365 if (!hostAddress.equals(configuredAaaServerAddress)) {
Vijaykumar Kushwahafffd3d12019-08-01 11:09:59 +0000366 getConfiguredAaaServerAddress();
367 if (!hostAddress.equals(configuredAaaServerAddress)) {
368 aaaStatisticsManager.getAaaStats().incrementUnknownServerRx();
369 }
kartikey dubeye1545422019-05-22 12:53:45 +0000370 }
371 }
372
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100373 /**
374 * Send RADIUS packet to the RADIUS server.
375 *
376 * @param radiusPacket RADIUS packet to be sent to server.
377 * @param inPkt Incoming EAPOL packet
378 */
379 protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) {
kartikey dubeye1545422019-05-22 12:53:45 +0000380 outPacketSet.add(radiusPacket.getIdentifier());
381 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(true);
382 aaaStatisticsManager.getAaaStats().increaseAccessRequestsTx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100383 impl.sendRadiusPacket(radiusPacket, inPkt);
384 }
Ray Milkey967776a2015-10-07 14:37:17 -0700385
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000386 /**
387 * For scheduling the timer required for cleaning up StateMachine
388 * when no response
389 * from RADIUS SERVER.
390 *
391 * @param sessionId SessionId of the current session
392 * @param stateMachine StateMachine for the id
393 */
394 public void scheduleStateMachineCleanupTimer(String sessionId, StateMachine stateMachine) {
395 StateMachine.CleanupTimerTask cleanupTask = stateMachine.new CleanupTimerTask(sessionId, this);
396 ScheduledFuture<?> cleanupTimer = executor.schedule(cleanupTask, cleanupTimerTimeOutInMins, TimeUnit.MINUTES);
397 stateMachine.setCleanupTimer(cleanupTimer);
398
399 }
400
401 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100402 * Handles RADIUS packets.
403 *
404 * @param radiusPacket RADIUS packet coming from the RADIUS server.
405 * @throws StateMachineException if an illegal state transition is triggered
Jonathan Hart4731dd92018-05-02 17:30:05 -0700406 * @throws DeserializationException if packet deserialization fails
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100407 */
Jonathan Hart4731dd92018-05-02 17:30:05 -0700408 public void handleRadiusPacket(RADIUS radiusPacket)
409 throws StateMachineException, DeserializationException {
Saurav Das987441a2018-09-18 16:33:47 -0700410 if (log.isTraceEnabled()) {
411 log.trace("Received RADIUS packet {}", radiusPacket);
412 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100413 StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier());
414 if (stateMachine == null) {
Saurav Das987441a2018-09-18 16:33:47 -0700415 log.error("Invalid packet identifier {}, could not find corresponding "
416 + "state machine ... exiting", radiusPacket.getIdentifier());
kartikey dubeye1545422019-05-22 12:53:45 +0000417 aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired();
418 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100419 return;
Ray Milkey967776a2015-10-07 14:37:17 -0700420 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100421 EAP eapPayload;
422 Ethernet eth;
Vijaykumar Kushwaha056e4cd2019-08-06 13:16:25 +0000423 checkReceivedPacketForValidValidator(radiusPacket, stateMachine.requestAuthenticator());
kartikey dubeye1545422019-05-22 12:53:45 +0000424 if (outPacketSet.contains(radiusPacket.getIdentifier())) {
425 aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(false);
426 outPacketSet.remove(new Byte(radiusPacket.getIdentifier()));
427 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100428 switch (radiusPacket.getCode()) {
429 case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400430 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100431 RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE);
432 byte[] challengeState = null;
433 if (radiusAttrState != null) {
434 challengeState = radiusAttrState.getValue();
435 }
436 eapPayload = radiusPacket.decapsulateMessage();
437 stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState);
438 eth = buildEapolResponse(stateMachine.supplicantAddress(),
439 MacAddress.valueOf(nasMacAddress),
440 stateMachine.vlanId(),
441 EAPOL.EAPOL_PACKET,
442 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400443 log.debug("Send EAP challenge response to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000444 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), true);
kartikey dubeye1545422019-05-22 12:53:45 +0000445 aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx();
Shubham Sharma47869542019-09-13 10:39:47 +0000446 outPacketSupp.add(eapPayload.getIdentifier());
447 aaaStatisticsManager.getAaaStats().incrementPendingResSupp();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100448 break;
449 case RADIUS.RADIUS_CODE_ACCESS_ACCEPT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400450 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100451 //send an EAPOL - Success to the supplicant.
452 byte[] eapMessageSuccess =
453 radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700454 eapPayload = EAP.deserializer().deserialize(
455 eapMessageSuccess, 0, eapMessageSuccess.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100456 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.info("Send EAP success message to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000462 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
463 aaaStatisticsManager.getAaaStats().incrementEapolAuthSuccessTrans();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100464
465 stateMachine.authorizeAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000466 aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100467 break;
468 case RADIUS.RADIUS_CODE_ACCESS_REJECT:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400469 log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT");
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100470 //send an EAPOL - Failure to the supplicant.
471 byte[] eapMessageFailure;
472 eapPayload = new EAP();
473 RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE);
474 if (radiusAttrEap == null) {
475 eapPayload.setCode(EAP.FAILURE);
476 eapPayload.setIdentifier(stateMachine.challengeIdentifier());
477 eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL);
478 } else {
479 eapMessageFailure = radiusAttrEap.getValue();
Jonathan Hart4731dd92018-05-02 17:30:05 -0700480 eapPayload = EAP.deserializer().deserialize(
481 eapMessageFailure, 0, eapMessageFailure.length);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100482 }
483 eth = buildEapolResponse(stateMachine.supplicantAddress(),
484 MacAddress.valueOf(nasMacAddress),
485 stateMachine.vlanId(),
486 EAPOL.EAPOL_PACKET,
487 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400488 log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000489 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
490 aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans();
491
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100492 stateMachine.denyAccess();
kartikey dubeye1545422019-05-22 12:53:45 +0000493 aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100494 break;
495 default:
496 log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode());
kartikey dubeye1545422019-05-22 12:53:45 +0000497 aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100498 }
kartikey dubeye1545422019-05-22 12:53:45 +0000499 aaaStatisticsManager.getAaaStats().countDroppedResponsesRx();
Aaron Kruglikovd39d99e2015-07-03 13:30:57 -0700500 }
501
Ray Milkey967776a2015-10-07 14:37:17 -0700502 /**
503 * Send the ethernet packet to the supplicant.
504 *
505 * @param ethernetPkt the ethernet packet
506 * @param connectPoint the connect point to send out
507 */
Shubham Sharmac155ef82019-07-11 12:12:41 +0000508 private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint, boolean isChallengeResponse) {
Ray Milkey967776a2015-10-07 14:37:17 -0700509 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build();
510 OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(),
511 treatment, ByteBuffer.wrap(ethernetPkt.serialize()));
Shubham Sharmac155ef82019-07-11 12:12:41 +0000512 EAPOL eap = ((EAPOL) ethernetPkt.getPayload());
513 EAP eapPkt = (EAP) eap.getPayload();
Saurav Das987441a2018-09-18 16:33:47 -0700514 if (log.isTraceEnabled()) {
Saurav Das987441a2018-09-18 16:33:47 -0700515 log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}",
516 eap, ethernetPkt, connectPoint);
517 }
Ray Milkey967776a2015-10-07 14:37:17 -0700518 packetService.emit(packet);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000519 if (isChallengeResponse) {
520 aaaStatisticsManager.getAaaStats().incrementEapPktTxauthEap();
521 }
Shubham Sharma47869542019-09-13 10:39:47 +0000522 aaaStatisticsManager.getAaaStats().incrementEapolFramesTx();
523 aaaStatisticsManager.getAaaStats().countReqEapFramesTx();
Ray Milkey967776a2015-10-07 14:37:17 -0700524 }
525
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400526 @Override
527 public String toString() {
528 return ToStringBuilder.reflectionToString(this);
529 }
530
Ari Saha89831742015-06-26 10:31:48 -0700531 // our handler defined as a private inner class
532
533 /**
534 * Packet processor responsible for forwarding packets along their paths.
535 */
536 private class ReactivePacketProcessor implements PacketProcessor {
537 @Override
538 public void process(PacketContext context) {
539
540 // Extract the original Ethernet frame from the packet information
541 InboundPacket pkt = context.inPacket();
542 Ethernet ethPkt = pkt.parsed();
543 if (ethPkt == null) {
544 return;
545 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100546
Ray Milkeyf51eba22015-09-25 10:24:23 -0700547 try {
548 // identify if incoming packet comes from supplicant (EAP) or RADIUS
549 switch (EthType.EtherType.lookup(ethPkt.getEtherType())) {
550 case EAPOL:
551 handleSupplicantPacket(context.inPacket());
552 break;
Ray Milkeyf51eba22015-09-25 10:24:23 -0700553 default:
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100554 // any other packets let the specific implementation handle
555 impl.handlePacketFromServer(context);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700556 }
Ray Milkey967776a2015-10-07 14:37:17 -0700557 } catch (StateMachineException e) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100558 log.warn("Unable to process packet:", e);
Ari Saha89831742015-06-26 10:31:48 -0700559 }
560 }
561
Ray Milkey9eb293f2015-09-30 15:09:17 -0700562 /**
563 * Creates and initializes common fields of a RADIUS packet.
564 *
Ray Milkey967776a2015-10-07 14:37:17 -0700565 * @param stateMachine state machine for the request
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700566 * @param eapPacket EAP packet
Ray Milkey9eb293f2015-09-30 15:09:17 -0700567 * @return RADIUS packet
568 */
Ray Milkey967776a2015-10-07 14:37:17 -0700569 private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) {
Ray Milkey9eb293f2015-09-30 15:09:17 -0700570 RADIUS radiusPayload =
571 new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST,
572 eapPacket.getIdentifier());
Ray Milkey967776a2015-10-07 14:37:17 -0700573
574 // set Request Authenticator in StateMachine
575 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
576
Ray Milkey9eb293f2015-09-30 15:09:17 -0700577 radiusPayload.setIdentifier(identifier);
578 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME,
Ray Milkey967776a2015-10-07 14:37:17 -0700579 stateMachine.username());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700580
581 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
Jonathan Hart092dfb22015-11-16 23:05:21 -0800582 AaaManager.this.nasIpAddress.getAddress());
Ray Milkey9eb293f2015-09-30 15:09:17 -0700583
584 radiusPayload.encapsulateMessage(eapPacket);
Ray Milkey9eb293f2015-09-30 15:09:17 -0700585
586 return radiusPayload;
587 }
Ari Saha89831742015-06-26 10:31:48 -0700588
589 /**
Jonathan Harta46dddf2015-06-30 15:31:20 -0700590 * Handles PAE packets (supplicant).
591 *
592 * @param inPacket Ethernet packet coming from the supplicant
Ari Saha89831742015-06-26 10:31:48 -0700593 */
Ray Milkeyf51eba22015-09-25 10:24:23 -0700594 private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException {
Jonathan Harta46dddf2015-06-30 15:31:20 -0700595 Ethernet ethPkt = inPacket.parsed();
Ari Saha89831742015-06-26 10:31:48 -0700596 // Where does it come from?
Jonathan Hart092dfb22015-11-16 23:05:21 -0800597 MacAddress srcMac = ethPkt.getSourceMAC();
Ari Saha89831742015-06-26 10:31:48 -0700598
Jonathan Harta46dddf2015-06-30 15:31:20 -0700599 DeviceId deviceId = inPacket.receivedFrom().deviceId();
600 PortNumber portNumber = inPacket.receivedFrom().port();
Ari Saha89831742015-06-26 10:31:48 -0700601 String sessionId = deviceId.toString() + portNumber.toString();
Saurav Das987441a2018-09-18 16:33:47 -0700602 EAPOL eapol = (EAPOL) ethPkt.getPayload();
603 if (log.isTraceEnabled()) {
604 log.trace("Received EAPOL packet {} in enclosing packet {} from "
605 + "dev/port: {}/{}", eapol, ethPkt, deviceId,
606 portNumber);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100607 }
Jonathan Harta46dddf2015-06-30 15:31:20 -0700608
Shubham Sharma47869542019-09-13 10:39:47 +0000609 short pktlen = eapol.getPacketLength();
610 byte[] eapPayLoadBuffer = eapol.serialize();
611 int len = eapPayLoadBuffer.length;
612 if (len != (HEADER_LENGTH + pktlen)) {
613 aaaStatisticsManager.getAaaStats().incrementInvalidBodyLength();
614 return;
615 }
616 if (!VALID_EAPOL_TYPE.contains(eapol.getEapolType())) {
617 aaaStatisticsManager.getAaaStats().incrementInvalidPktType();
618 return;
619 }
620 if (pktlen >= 0 && ethPkt.getEtherType() == EthType.EtherType.EAPOL.ethType().toShort()) {
621 aaaStatisticsManager.getAaaStats().incrementValidEapolFramesRx();
622 }
Saurav Das987441a2018-09-18 16:33:47 -0700623 StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId);
624 if (stateMachine == null) {
625 log.debug("Creating new state machine for sessionId: {} for "
626 + "dev/port: {}/{}", sessionId, deviceId, portNumber);
627 stateMachine = new StateMachine(sessionId);
628 } else {
629 log.debug("Using existing state-machine for sessionId: {}", sessionId);
630 }
631
Ari Saha89831742015-06-26 10:31:48 -0700632 switch (eapol.getEapolType()) {
633 case EAPOL.EAPOL_START:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400634 log.debug("EAP packet: EAPOL_START");
Ray Milkeyf51eba22015-09-25 10:24:23 -0700635 stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom());
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000636 if (stateMachine.getCleanupTimer() == null) {
637 scheduleStateMachineCleanupTimer(sessionId, stateMachine);
638 }
Jonathan Hart5db44532018-07-12 18:13:54 -0700639 stateMachine.start();
Shubham Sharmac155ef82019-07-11 12:12:41 +0000640 aaaStatisticsManager.getAaaStats().incrementEapolStartReqTrans();
Ray Milkeyf51eba22015-09-25 10:24:23 -0700641 //send an EAP Request/Identify to the supplicant
642 EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100643 if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
644 stateMachine.setPriorityCode(ethPkt.getPriorityCode());
645 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800646 Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress),
Ray Milkeyf51eba22015-09-25 10:24:23 -0700647 ethPkt.getVlanID(), EAPOL.EAPOL_PACKET,
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100648 eapPayload, stateMachine.priorityCode());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400649
Jonathan Hart092dfb22015-11-16 23:05:21 -0800650 stateMachine.setSupplicantAddress(srcMac);
Ray Milkeyf51eba22015-09-25 10:24:23 -0700651 stateMachine.setVlanId(ethPkt.getVlanID());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400652 log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000653 sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false);
Shubham Sharma47869542019-09-13 10:39:47 +0000654 aaaStatisticsManager.getAaaStats().incrementRequestIdFramesTx();
Ari Saha89831742015-06-26 10:31:48 -0700655
656 break;
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800657 case EAPOL.EAPOL_LOGOFF:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400658 log.debug("EAP packet: EAPOL_LOGOFF");
Ray Milkeyb34b4962016-01-04 10:24:43 -0800659 if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) {
660 stateMachine.logoff();
Shubham Sharmac155ef82019-07-11 12:12:41 +0000661 aaaStatisticsManager.getAaaStats().incrementEapolLogoffRx();
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800662 }
Shubham Sharma47869542019-09-13 10:39:47 +0000663 if (stateMachine.state() == StateMachine.STATE_IDLE) {
664 aaaStatisticsManager.getAaaStats().incrementAuthStateIdle();
665 }
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800666
667 break;
Ari Saha89831742015-06-26 10:31:48 -0700668 case EAPOL.EAPOL_PACKET:
Ray Milkeyf51eba22015-09-25 10:24:23 -0700669 RADIUS radiusPayload;
Ray Milkey9eb293f2015-09-30 15:09:17 -0700670 // check if this is a Response/Identify or a Response/TLS
Ari Saha89831742015-06-26 10:31:48 -0700671 EAP eapPacket = (EAP) eapol.getPayload();
Shubham Sharma47869542019-09-13 10:39:47 +0000672 Byte identifier = new Byte(eapPacket.getIdentifier());
Ari Saha89831742015-06-26 10:31:48 -0700673
674 byte dataType = eapPacket.getDataType();
675 switch (dataType) {
Ari Saha89831742015-06-26 10:31:48 -0700676
Ray Milkey9eb293f2015-09-30 15:09:17 -0700677 case EAP.ATTR_IDENTITY:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400678 log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY");
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000679 //Setting the time of this response from RG, only when its not a re-transmission.
680 if (stateMachine.getLastPacketReceivedTime() == 0) {
681 stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
682 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700683 // request id access to RADIUS
684 stateMachine.setUsername(eapPacket.getData());
Ari Saha89831742015-06-26 10:31:48 -0700685
Ray Milkey967776a2015-10-07 14:37:17 -0700686 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100687 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800688 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Ari Saha89831742015-06-26 10:31:48 -0700689
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100690 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000691 stateMachine.setWaitingForRadiusResponse(true);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000692 aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity();
Ray Milkey9eb293f2015-09-30 15:09:17 -0700693 // change the state to "PENDING"
kartikey dubeye1545422019-05-22 12:53:45 +0000694 if (stateMachine.state() == StateMachine.STATE_PENDING) {
695 aaaStatisticsManager.getAaaStats().increaseRequestReTx();
696 }
Ray Milkey9eb293f2015-09-30 15:09:17 -0700697 stateMachine.requestAccess();
698 break;
Ari Saha89831742015-06-26 10:31:48 -0700699 case EAP.ATTR_MD5:
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000700 stateMachine.setLastPacketReceivedTime(System.currentTimeMillis());
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400701 log.debug("EAP packet: EAPOL_PACKET ATTR_MD5");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700702 // verify if the EAP identifier corresponds to the
703 // challenge identifier from the client state
704 // machine.
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700705 if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) {
Ari Saha89831742015-06-26 10:31:48 -0700706 //send the RADIUS challenge response
Ray Milkey967776a2015-10-07 14:37:17 -0700707 radiusPayload =
708 getRadiusPayload(stateMachine,
709 stateMachine.identifier(),
710 eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100711 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700712
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800713 if (stateMachine.challengeState() != null) {
714 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
715 stateMachine.challengeState());
716 }
Jonathan Hart092dfb22015-11-16 23:05:21 -0800717 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Shubham Sharma47869542019-09-13 10:39:47 +0000718 if (outPacketSupp.contains(eapPacket.getIdentifier())) {
719 aaaStatisticsManager.getAaaStats().decrementPendingResSupp();
720 outPacketSupp.remove(identifier);
721 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100722 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000723 stateMachine.setWaitingForRadiusResponse(true);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000724 aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall();
Ari Saha89831742015-06-26 10:31:48 -0700725 }
726 break;
727 case EAP.ATTR_TLS:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400728 log.debug("EAP packet: EAPOL_PACKET ATTR_TLS");
Ray Milkey9eb293f2015-09-30 15:09:17 -0700729 // request id access to RADIUS
Ray Milkey967776a2015-10-07 14:37:17 -0700730 radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100731 radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket);
Ari Saha89831742015-06-26 10:31:48 -0700732
Qianqian Hub55a1ac2015-12-23 20:44:48 +0800733 if (stateMachine.challengeState() != null) {
734 radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
735 stateMachine.challengeState());
736 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700737 stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode());
Ari Saha89831742015-06-26 10:31:48 -0700738
Jonathan Hart092dfb22015-11-16 23:05:21 -0800739 radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret);
Shubham Sharma47869542019-09-13 10:39:47 +0000740 if (outPacketSupp.contains(eapPacket.getIdentifier())) {
741 aaaStatisticsManager.getAaaStats().decrementPendingResSupp();
742 outPacketSupp.remove(identifier);
743 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100744 sendRadiusPacket(radiusPayload, inPacket);
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000745 stateMachine.setWaitingForRadiusResponse(true);
Shubham Sharmac155ef82019-07-11 12:12:41 +0000746 aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall();
Ray Milkey5493b512015-10-21 12:13:49 -0700747
Ray Milkeyf3790b82015-10-21 16:28:08 -0700748 if (stateMachine.state() != StateMachine.STATE_PENDING) {
749 stateMachine.requestAccess();
750 }
Ray Milkeyf51eba22015-09-25 10:24:23 -0700751
Ari Saha89831742015-06-26 10:31:48 -0700752 break;
753 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400754 log.warn("Unknown EAP packet type");
Ari Saha89831742015-06-26 10:31:48 -0700755 return;
756 }
757 break;
758 default:
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400759 log.debug("Skipping EAPOL message {}", eapol.getEapolType());
Ari Saha89831742015-06-26 10:31:48 -0700760 }
Shubham Sharmac155ef82019-07-11 12:12:41 +0000761 aaaStatisticsManager.getAaaStats().countTransRespNotNak();
762 aaaStatisticsManager.getAaaStats().countEapolResIdentityMsgTrans();
Ari Saha89831742015-06-26 10:31:48 -0700763 }
Ray Milkey967776a2015-10-07 14:37:17 -0700764 }
765
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100766 /**
Jonathan Hart5db44532018-07-12 18:13:54 -0700767 * Delegate allowing the StateMachine to notify us of events.
768 */
769 private class InternalStateMachineDelegate implements StateMachineDelegate {
770
771 @Override
772 public void notify(AuthenticationEvent authenticationEvent) {
773 log.info("Auth event {} for {}",
774 authenticationEvent.type(), authenticationEvent.subject());
775 post(authenticationEvent);
776 }
777 }
778
779 /**
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100780 * Configuration Listener, handles change in configuration.
781 */
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700782 private class InternalConfigListener implements NetworkConfigListener {
783
784 /**
Ray Milkeyc9e8dcc2015-12-30 10:31:32 -0800785 * Reconfigures the AAA application according to the
786 * configuration parameters passed.
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700787 *
788 * @param cfg configuration object
789 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800790 private void reconfigureNetwork(AaaConfig cfg) {
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400791 log.info("Reconfiguring AaaConfig from config: {}", cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100792
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700793 if (cfg == null) {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800794 newCfg = new AaaConfig();
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700795 } else {
796 newCfg = cfg;
797 }
798 if (newCfg.nasIp() != null) {
799 nasIpAddress = newCfg.nasIp();
800 }
801 if (newCfg.radiusIp() != null) {
802 radiusIpAddress = newCfg.radiusIp();
803 }
804 if (newCfg.radiusMac() != null) {
805 radiusMacAddress = newCfg.radiusMac();
806 }
807 if (newCfg.nasMac() != null) {
808 nasMacAddress = newCfg.nasMac();
809 }
810 if (newCfg.radiusSecret() != null) {
811 radiusSecret = newCfg.radiusSecret();
812 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100813
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530814 boolean reconfigureCustomizer = false;
815 if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) {
816 customizer = newCfg.radiusPktCustomizer();
817 configurePacketCustomizer();
818 reconfigureCustomizer = true;
819 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100820
Deepa Vaddireddye0e10722017-09-27 05:00:10 +0530821 if (radiusConnectionType == null
822 || reconfigureCustomizer
823 || !radiusConnectionType.equals(newCfg.radiusConnectionType())) {
824 radiusConnectionType = newCfg.radiusConnectionType();
825 if (impl != null) {
826 impl.withdrawIntercepts();
827 impl.clearLocalState();
828 }
829 configureRadiusCommunication();
830 impl.initializeLocalState(newCfg);
831 impl.requestIntercepts();
832 } else if (impl != null) {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100833 impl.clearLocalState();
834 impl.initializeLocalState(newCfg);
Ray Milkey5d99bd12015-10-06 15:41:30 -0700835 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700836 }
837
838 @Override
839 public void event(NetworkConfigEvent event) {
840
841 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
842 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
Jonathan Hart092dfb22015-11-16 23:05:21 -0800843 event.configClass().equals(AaaConfig.class)) {
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700844
Jonathan Hart092dfb22015-11-16 23:05:21 -0800845 AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class);
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700846 reconfigureNetwork(cfg);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100847
Matt Jeanneret2ff1a782018-06-13 15:24:25 -0400848 log.info("Reconfigured: {}", cfg.toString());
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700849 }
850 }
851 }
Amit Ghoshf739be52017-09-21 15:49:37 +0100852
853 private class InternalDeviceListener implements DeviceListener {
854 @Override
855 public void event(DeviceEvent event) {
856
857 switch (event.type()) {
858 case PORT_REMOVED:
859 DeviceId devId = event.subject().id();
860 PortNumber portNumber = event.port().number();
861 String sessionId = devId.toString() + portNumber.toString();
862
863 Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap();
864 StateMachine removed = sessionIdMap.remove(sessionId);
865 if (removed != null) {
866 StateMachine.deleteStateMachineMapping(removed);
867 }
868
869 break;
870 default:
871 return;
872 }
873 }
874 }
kartikey dubeye1545422019-05-22 12:53:45 +0000875 private class AuthenticationStatisticsEventPublisher implements Runnable {
876 private final Logger log = getLogger(getClass());
877 public void run() {
878 log.info("Notifying AuthenticationStatisticsEvent");
879 aaaStatisticsManager.calculatePacketRoundtripTime();
880 log.debug("AcceptResponsesRx---" + aaaStatisticsManager.getAaaStats().getAcceptResponsesRx());
881 log.debug("AccessRequestsTx---" + aaaStatisticsManager.getAaaStats().getAccessRequestsTx());
882 log.debug("ChallengeResponsesRx---" + aaaStatisticsManager.getAaaStats().getChallengeResponsesRx());
883 log.debug("DroppedResponsesRx---" + aaaStatisticsManager.getAaaStats().getDroppedResponsesRx());
884 log.debug("InvalidValidatorsRx---" + aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx());
885 log.debug("MalformedResponsesRx---" + aaaStatisticsManager.getAaaStats().getMalformedResponsesRx());
886 log.debug("PendingRequests---" + aaaStatisticsManager.getAaaStats().getPendingRequests());
887 log.debug("RejectResponsesRx---" + aaaStatisticsManager.getAaaStats().getRejectResponsesRx());
888 log.debug("RequestReTx---" + aaaStatisticsManager.getAaaStats().getRequestReTx());
889 log.debug("RequestRttMilis---" + aaaStatisticsManager.getAaaStats().getRequestRttMilis());
890 log.debug("UnknownServerRx---" + aaaStatisticsManager.getAaaStats().getUnknownServerRx());
891 log.debug("UnknownTypeRx---" + aaaStatisticsManager.getAaaStats().getUnknownTypeRx());
Shubham Sharmaa3b1bd32019-06-19 14:18:12 +0000892 log.debug("TimedOutPackets----" + aaaStatisticsManager.getAaaStats().getTimedOutPackets());
Shubham Sharmac155ef82019-07-11 12:12:41 +0000893 log.debug("EapolLogoffRx---" + aaaStatisticsManager.getAaaStats().getEapolLogoffRx());
894 log.debug("EapolAuthSuccessTrans---" + aaaStatisticsManager.getAaaStats().getEapolAuthSuccessTrans());
895 log.debug("EapolAuthFailureTrans---" +
896 aaaStatisticsManager.getAaaStats().getEapolAuthFailureTrans());
897 log.debug("EapolStartReqTrans---" +
898 aaaStatisticsManager.getAaaStats().getEapolStartReqTrans());
899 log.debug("EapolTransRespNotNak---" +
900 aaaStatisticsManager.getAaaStats().getEapolTransRespNotNak());
901 log.debug("EapPktTxauthChooseEap---" +
902 aaaStatisticsManager.getAaaStats().getEapPktTxauthChooseEap());
903 log.debug("EapolResIdentityMsgTrans---" +
904 aaaStatisticsManager.getAaaStats().getEapolResIdentityMsgTrans());
Shubham Sharma47869542019-09-13 10:39:47 +0000905 log.debug("EapolFramesTx---" + aaaStatisticsManager.getAaaStats().getEapolFramesTx());
906 log.debug("AuthStateIdle---" + aaaStatisticsManager.getAaaStats().getAuthStateIdle());
907 log.debug("RequestIdFramesTx---" + aaaStatisticsManager.getAaaStats().getRequestIdFramesTx());
908 log.debug("ReqEapFramesTx---" + aaaStatisticsManager.getAaaStats().getReqEapFramesTx());
909 log.debug("InvalidPktType---" + aaaStatisticsManager.getAaaStats().getInvalidPktType());
910 log.debug("InvalidBodyLength---" + aaaStatisticsManager.getAaaStats().getInvalidBodyLength());
911 log.debug("ValidEapolFramesRx---" + aaaStatisticsManager.getAaaStats().getValidEapolFramesRx());
912 log.debug("PendingResSupp---" + aaaStatisticsManager.getAaaStats().getPendingResSupp());
913 log.debug("ResIdEapFramesRx---" + aaaStatisticsManager.getAaaStats().getEapolattrIdentity());
kartikey dubeye1545422019-05-22 12:53:45 +0000914 aaaStatisticsManager.getStatsDelegate().
915 notify(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE,
916 aaaStatisticsManager.getAaaStats()));
917 }
918 }
Ari Saha89831742015-06-26 10:31:48 -0700919}