Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 3 | * |
| 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 Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 16 | package org.opencord.aaa.impl; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 17 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 18 | import com.google.common.base.Strings; |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 19 | import org.apache.commons.lang3.builder.ToStringBuilder; |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 20 | import org.onlab.packet.DeserializationException; |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 21 | import org.onlab.packet.EAP; |
| 22 | import org.onlab.packet.EAPOL; |
| 23 | import org.onlab.packet.EthType; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 25 | import org.onlab.packet.MacAddress; |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 26 | import org.onlab.packet.RADIUS; |
| 27 | import org.onlab.packet.RADIUSAttribute; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.cfg.ComponentConfigService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 32 | import org.onosproject.event.AbstractListenerManager; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 37 | import org.onosproject.net.config.ConfigFactory; |
| 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
| 40 | import org.onosproject.net.config.NetworkConfigRegistry; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceListener; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 45 | import org.onosproject.net.flow.TrafficTreatment; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 46 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 47 | import org.onosproject.net.packet.InboundPacket; |
| 48 | import org.onosproject.net.packet.OutboundPacket; |
| 49 | import org.onosproject.net.packet.PacketContext; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 50 | import org.onosproject.net.packet.PacketProcessor; |
| 51 | import org.onosproject.net.packet.PacketService; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 52 | import org.opencord.aaa.AaaConfig; |
| 53 | import org.opencord.aaa.AuthenticationEvent; |
| 54 | import org.opencord.aaa.AuthenticationEventListener; |
| 55 | import org.opencord.aaa.AuthenticationService; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 56 | import org.opencord.aaa.AuthenticationStatisticsEvent; |
| 57 | import org.opencord.aaa.AuthenticationStatisticsService; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 58 | import org.opencord.aaa.RadiusCommunicator; |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 59 | import org.opencord.aaa.RadiusOperationalStatusEvent; |
| 60 | import org.opencord.aaa.RadiusOperationalStatusService; |
| 61 | import org.opencord.aaa.RadiusOperationalStatusService.RadiusOperationalStatusEvaluationMode; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 62 | import org.opencord.aaa.StateMachineDelegate; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 63 | import org.opencord.sadis.BaseInformationService; |
| 64 | import org.opencord.sadis.SadisService; |
| 65 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 66 | import org.osgi.service.component.ComponentContext; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 67 | import org.osgi.service.component.annotations.Activate; |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 68 | import org.osgi.service.component.annotations.Component; |
| 69 | import org.osgi.service.component.annotations.Deactivate; |
| 70 | import org.osgi.service.component.annotations.Modified; |
| 71 | import org.osgi.service.component.annotations.Reference; |
| 72 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 73 | import org.slf4j.Logger; |
| 74 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 75 | import javax.crypto.Mac; |
| 76 | import javax.crypto.spec.SecretKeySpec; |
| 77 | import java.net.InetAddress; |
| 78 | import java.net.UnknownHostException; |
| 79 | import java.nio.ByteBuffer; |
| 80 | import java.util.Arrays; |
| 81 | import java.util.Dictionary; |
| 82 | import java.util.HashSet; |
| 83 | import java.util.List; |
| 84 | import java.util.Map; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 85 | import java.util.concurrent.Executors; |
| 86 | import java.util.concurrent.ScheduledExecutorService; |
| 87 | import java.util.concurrent.ScheduledFuture; |
| 88 | import java.util.concurrent.TimeUnit; |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 89 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 90 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 91 | import static org.opencord.aaa.impl.OsgiPropertyConstants.OPERATIONAL_STATUS_SERVER_EVENT_GENERATION; |
| 92 | import static org.opencord.aaa.impl.OsgiPropertyConstants.OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT; |
| 93 | import static org.opencord.aaa.impl.OsgiPropertyConstants.OPERATIONAL_STATUS_SERVER_TIMEOUT; |
| 94 | import static org.opencord.aaa.impl.OsgiPropertyConstants.OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT; |
| 95 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_GENERATION_PERIOD; |
| 96 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATISTICS_GENERATION_PERIOD_DEFAULT; |
| 97 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATUS_SERVER_MODE; |
| 98 | import static org.opencord.aaa.impl.OsgiPropertyConstants.STATUS_SERVER_MODE_DEFAULT; |
| 99 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 100 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 101 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 102 | * AAA application for ONOS. |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 103 | */ |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 104 | @Component(immediate = true, property = { |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 105 | STATISTICS_GENERATION_PERIOD + ":Integer=" + STATISTICS_GENERATION_PERIOD_DEFAULT, |
| 106 | OPERATIONAL_STATUS_SERVER_EVENT_GENERATION + ":Integer=" + OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT, |
| 107 | OPERATIONAL_STATUS_SERVER_TIMEOUT + ":Integer=" + OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT, |
| 108 | STATUS_SERVER_MODE + ":String=" + STATUS_SERVER_MODE_DEFAULT, |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 109 | }) |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 110 | public class AaaManager |
| 111 | extends AbstractListenerManager<AuthenticationEvent, AuthenticationEventListener> |
| 112 | implements AuthenticationService { |
| 113 | |
Charles Chan | df7ff86 | 2017-01-20 11:22:05 -0800 | [diff] [blame] | 114 | private static final String APP_NAME = "org.opencord.aaa"; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 115 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 116 | private final Logger log = getLogger(getClass()); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 117 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 119 | protected CoreService coreService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 120 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 122 | protected PacketService packetService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 123 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 125 | protected NetworkConfigRegistry netCfgService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 126 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 128 | protected DeviceService deviceService; |
| 129 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 131 | protected SadisService sadisService; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 132 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 134 | protected MastershipService mastershipService; |
| 135 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 137 | protected AuthenticationStatisticsService aaaStatisticsManager; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 138 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 140 | protected ComponentConfigService cfgService; |
| 141 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 143 | protected RadiusOperationalStatusService radiusOperationalStatusService; |
| 144 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 145 | protected AuthenticationStatisticsEventPublisher authenticationStatisticsPublisher; |
| 146 | protected BaseInformationService<SubscriberAndDeviceInformation> subsService; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 147 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 148 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 149 | // Properties |
| 150 | private int statisticsGenerationPeriodInSeconds = STATISTICS_GENERATION_PERIOD_DEFAULT; |
| 151 | private int operationalStatusEventGenerationPeriodInSeconds = OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT; |
| 152 | private int operationalStatusServerTimeoutInSeconds = OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT; |
| 153 | protected String operationalStatusEvaluationMode = STATUS_SERVER_MODE_DEFAULT; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 154 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 155 | // NAS IP address |
| 156 | protected InetAddress nasIpAddress; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 157 | |
| 158 | // self MAC address |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 159 | protected String nasMacAddress; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 160 | |
| 161 | // Parsed RADIUS server addresses |
| 162 | protected InetAddress radiusIpAddress; |
| 163 | |
| 164 | // MAC address of RADIUS server or net hop router |
| 165 | protected String radiusMacAddress; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 166 | |
| 167 | // RADIUS server secret |
| 168 | protected String radiusSecret; |
| 169 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 170 | // bindings |
| 171 | protected CustomizationInfo customInfo; |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 172 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 173 | // our application-specific event handler |
| 174 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 175 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 176 | // our unique identifier |
| 177 | private ApplicationId appId; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 178 | |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 179 | // TimeOut time for cleaning up stateMachines stuck due to pending AAA/EAPOL message. |
| 180 | protected int cleanupTimerTimeOutInMins; |
| 181 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 182 | // Setup specific customization/attributes on the RADIUS packets |
| 183 | PacketCustomizer pktCustomizer; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 184 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 185 | // packet customizer to use |
| 186 | private String customizer; |
| 187 | |
| 188 | // Type of connection to use to communicate with Radius server, options are |
| 189 | // "socket" or "packet_out" |
| 190 | private String radiusConnectionType; |
| 191 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 192 | // Object for the specific type of communication with the RADIUS |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 193 | // server, socket based or packet_out based |
| 194 | RadiusCommunicator impl = null; |
| 195 | |
| 196 | // latest configuration |
| 197 | AaaConfig newCfg; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 198 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 199 | ScheduledFuture<?> scheduledFuture; |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 200 | ScheduledFuture<?> scheduledStatusServerChecker; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 201 | ScheduledExecutorService executor; |
| 202 | String configuredAaaServerAddress; |
| 203 | HashSet<Byte> outPacketSet = new HashSet<Byte>(); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 204 | HashSet<Byte> outPacketSupp = new HashSet<Byte>(); |
| 205 | static final List<Byte> VALID_EAPOL_TYPE = Arrays.asList(EAPOL.EAPOL_START, EAPOL.EAPOL_LOGOFF, EAPOL.EAPOL_PACKET); |
| 206 | static final int HEADER_LENGTH = 4; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 207 | // Configuration properties factory |
| 208 | private final ConfigFactory factory = |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 209 | new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY, |
| 210 | AaaConfig.class, |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 211 | "AAA") { |
| 212 | @Override |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 213 | public AaaConfig createConfig() { |
| 214 | return new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 215 | } |
| 216 | }; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 217 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 218 | // Listener for config changes |
| 219 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 220 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 221 | private StateMachineDelegate delegate = new InternalStateMachineDelegate(); |
| 222 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Builds an EAPOL packet based on the given parameters. |
| 225 | * |
| 226 | * @param dstMac destination MAC address |
| 227 | * @param srcMac source MAC address |
| 228 | * @param vlan vlan identifier |
| 229 | * @param eapolType EAPOL type |
| 230 | * @param eap EAP payload |
| 231 | * @return Ethernet frame |
| 232 | */ |
| 233 | private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac, |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 234 | short vlan, byte eapolType, EAP eap, byte priorityCode) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 235 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 236 | Ethernet eth = new Ethernet(); |
| 237 | eth.setDestinationMACAddress(dstMac.toBytes()); |
| 238 | eth.setSourceMACAddress(srcMac.toBytes()); |
| 239 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 240 | if (vlan != Ethernet.VLAN_UNTAGGED) { |
| 241 | eth.setVlanID(vlan); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 242 | eth.setPriorityCode(priorityCode); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 243 | } |
| 244 | //eapol header |
| 245 | EAPOL eapol = new EAPOL(); |
| 246 | eapol.setEapolType(eapolType); |
| 247 | eapol.setPacketLength(eap.getLength()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 248 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 249 | //eap part |
| 250 | eapol.setPayload(eap); |
| 251 | |
| 252 | eth.setPayload(eapol); |
| 253 | eth.setPad(true); |
| 254 | return eth; |
| 255 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 256 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 257 | @Activate |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 258 | public void activate(ComponentContext context) { |
Charles Chan | df7ff86 | 2017-01-20 11:22:05 -0800 | [diff] [blame] | 259 | appId = coreService.registerApplication(APP_NAME); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 260 | eventDispatcher.addSink(AuthenticationEvent.class, listenerRegistry); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 261 | netCfgService.addListener(cfgListener); |
| 262 | netCfgService.registerConfigFactory(factory); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 263 | cfgService.registerProperties(getClass()); |
| 264 | modified(context); |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 265 | subsService = sadisService.getSubscriberInfoService(); |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 266 | customInfo = new CustomizationInfo(subsService, deviceService); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 267 | cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class)); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 268 | log.info("Starting with config {} {}", this, newCfg); |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 269 | configureRadiusCommunication(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 270 | // register our event handler |
Brian O'Connor | d9c7da0 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 271 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 272 | StateMachine.initializeMaps(); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 273 | StateMachine.setDelegate(delegate); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 274 | cleanupTimerTimeOutInMins = newCfg.sessionCleanupTimer(); |
| 275 | StateMachine.setcleanupTimerTimeOutInMins(cleanupTimerTimeOutInMins); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 276 | impl.initializeLocalState(newCfg); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 277 | impl.requestIntercepts(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 278 | deviceService.addListener(deviceListener); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 279 | getConfiguredAaaServerAddress(); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 280 | radiusOperationalStatusService.initialize(nasIpAddress.getAddress(), radiusSecret, impl); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 281 | authenticationStatisticsPublisher = |
| 282 | new AuthenticationStatisticsEventPublisher(); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 283 | executor = Executors.newScheduledThreadPool(3); |
| 284 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 285 | scheduledFuture = executor.scheduleAtFixedRate(authenticationStatisticsPublisher, |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 286 | 0, statisticsGenerationPeriodInSeconds, TimeUnit.SECONDS); |
| 287 | scheduledStatusServerChecker = executor.scheduleAtFixedRate(new ServerStatusChecker(), 0, |
| 288 | operationalStatusEventGenerationPeriodInSeconds, TimeUnit.SECONDS); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 289 | |
Jian Li | 13c6716 | 2015-12-09 13:20:34 -0800 | [diff] [blame] | 290 | log.info("Started"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Deactivate |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 294 | public void deactivate(ComponentContext context) { |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 295 | impl.withdrawIntercepts(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 296 | packetService.removeProcessor(processor); |
Deepa Vaddireddy | b9c24c6 | 2017-09-21 13:45:30 +0530 | [diff] [blame] | 297 | netCfgService.removeListener(cfgListener); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 298 | cfgService.unregisterProperties(getClass(), false); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 299 | StateMachine.unsetDelegate(delegate); |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 300 | StateMachine.destroyMaps(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 301 | impl.deactivate(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 302 | deviceService.removeListener(deviceListener); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 303 | eventDispatcher.removeSink(AuthenticationEvent.class); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 304 | scheduledFuture.cancel(true); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 305 | scheduledStatusServerChecker.cancel(true); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 306 | executor.shutdown(); |
Jian Li | 13c6716 | 2015-12-09 13:20:34 -0800 | [diff] [blame] | 307 | log.info("Stopped"); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 308 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 309 | @Modified |
| 310 | public void modified(ComponentContext context) { |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 311 | Dictionary<String, Object> properties = context.getProperties(); |
| 312 | |
| 313 | String s = Tools.get(properties, "statisticsGenerationPeriodInSeconds"); |
| 314 | statisticsGenerationPeriodInSeconds = Strings.isNullOrEmpty(s) ? STATISTICS_GENERATION_PERIOD_DEFAULT |
| 315 | : Integer.parseInt(s.trim()); |
| 316 | |
| 317 | s = Tools.get(properties, "operationalStatusEventGenerationPeriodInSeconds"); |
| 318 | operationalStatusEventGenerationPeriodInSeconds = Strings.isNullOrEmpty(s) |
| 319 | ? OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT |
| 320 | : Integer.parseInt(s.trim()); |
| 321 | |
| 322 | s = Tools.get(properties, "operationalStatusServerTimeoutInSeconds"); |
| 323 | operationalStatusServerTimeoutInSeconds = Strings.isNullOrEmpty(s) ? OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT |
| 324 | : Integer.parseInt(s.trim()); |
| 325 | |
| 326 | s = Tools.get(properties, "operationalStatusEvaluationMode"); |
| 327 | String newEvaluationModeString = Strings.isNullOrEmpty(s) ? STATUS_SERVER_MODE_DEFAULT : s.trim(); |
| 328 | |
| 329 | radiusOperationalStatusService |
| 330 | .setOperationalStatusServerTimeoutInMillis(operationalStatusServerTimeoutInSeconds * 1000); |
| 331 | RadiusOperationalStatusEvaluationMode newEvaluationMode = |
| 332 | RadiusOperationalStatusEvaluationMode.getValue(newEvaluationModeString); |
| 333 | if (newEvaluationMode != null) { |
| 334 | radiusOperationalStatusService.setRadiusOperationalStatusEvaluationMode(newEvaluationMode); |
| 335 | operationalStatusEvaluationMode = newEvaluationModeString; |
| 336 | } else { |
| 337 | properties.put("operationalStatusEvaluationMode", operationalStatusEvaluationMode); |
| 338 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Shubham Sharma | cf5e503 | 2019-11-26 11:09:21 +0000 | [diff] [blame] | 341 | protected void configureRadiusCommunication() { |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 342 | if (radiusConnectionType.toLowerCase().equals("socket")) { |
| 343 | impl = new SocketBasedRadiusCommunicator(appId, packetService, this); |
| 344 | } else { |
| 345 | impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService, |
| 346 | deviceService, subsService, pktCustomizer, this); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private void configurePacketCustomizer() { |
| 351 | switch (customizer.toLowerCase()) { |
| 352 | case "sample": |
| 353 | pktCustomizer = new SamplePacketCustomizer(customInfo); |
| 354 | log.info("Created SamplePacketCustomizer"); |
| 355 | break; |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 356 | case "att": |
| 357 | pktCustomizer = new AttPacketCustomizer(customInfo); |
| 358 | log.info("Created AttPacketCustomizer"); |
| 359 | break; |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 360 | default: |
| 361 | pktCustomizer = new PacketCustomizer(customInfo); |
| 362 | log.info("Created default PacketCustomizer"); |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 367 | private void getConfiguredAaaServerAddress() { |
| 368 | try { |
| 369 | InetAddress address; |
| 370 | if (newCfg.radiusHostName() != null) { |
| 371 | address = InetAddress.getByName(newCfg.radiusHostName()); |
| 372 | } else { |
| 373 | address = newCfg.radiusIp(); |
| 374 | } |
| 375 | |
| 376 | configuredAaaServerAddress = address.getHostAddress(); |
| 377 | } catch (UnknownHostException uhe) { |
| 378 | log.warn("Unable to resolve host {}", newCfg.radiusHostName()); |
| 379 | } |
| 380 | } |
| 381 | |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 382 | private void checkReceivedPacketForValidValidator(RADIUS radiusPacket, byte[] requestAuthenticator) { |
| 383 | if (!checkResponseMessageAuthenticator(radiusSecret, radiusPacket, requestAuthenticator)) { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 384 | aaaStatisticsManager.getAaaStats().increaseInvalidValidatorsRx(); |
| 385 | } |
| 386 | } |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 387 | |
| 388 | private boolean checkResponseMessageAuthenticator(String key, RADIUS radiusPacket, byte[] requestAuthenticator) { |
| 389 | byte[] newHash = new byte[16]; |
| 390 | Arrays.fill(newHash, (byte) 0); |
| 391 | byte[] messageAuthenticator = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue(); |
| 392 | byte[] authenticator = radiusPacket.getAuthenticator(); |
| 393 | radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash); |
| 394 | radiusPacket.setAuthenticator(requestAuthenticator); |
| 395 | // Calculate the MD5 HMAC based on the message |
| 396 | try { |
| 397 | SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5"); |
| 398 | Mac mac = Mac.getInstance("HmacMD5"); |
| 399 | mac.init(keySpec); |
| 400 | newHash = mac.doFinal(radiusPacket.serialize()); |
| 401 | } catch (Exception e) { |
| 402 | log.error("Failed to generate message authenticator: {}", e.getMessage()); |
| 403 | } |
| 404 | radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator); |
| 405 | radiusPacket.setAuthenticator(authenticator); |
| 406 | // Compare the calculated Message-Authenticator with the one in the message |
| 407 | return Arrays.equals(newHash, messageAuthenticator); |
| 408 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 409 | public void checkForPacketFromUnknownServer(String hostAddress) { |
| 410 | if (!hostAddress.equals(configuredAaaServerAddress)) { |
Vijaykumar Kushwaha | fffd3d1 | 2019-08-01 11:09:59 +0000 | [diff] [blame] | 411 | getConfiguredAaaServerAddress(); |
| 412 | if (!hostAddress.equals(configuredAaaServerAddress)) { |
| 413 | aaaStatisticsManager.getAaaStats().incrementUnknownServerRx(); |
| 414 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 418 | /** |
| 419 | * Send RADIUS packet to the RADIUS server. |
| 420 | * |
| 421 | * @param radiusPacket RADIUS packet to be sent to server. |
| 422 | * @param inPkt Incoming EAPOL packet |
| 423 | */ |
| 424 | protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 425 | outPacketSet.add(radiusPacket.getIdentifier()); |
| 426 | aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(true); |
| 427 | aaaStatisticsManager.getAaaStats().increaseAccessRequestsTx(); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 428 | aaaStatisticsManager.putOutgoingIdentifierToMap(radiusPacket.getIdentifier()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 429 | impl.sendRadiusPacket(radiusPacket, inPkt); |
| 430 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 431 | |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 432 | /** |
| 433 | * For scheduling the timer required for cleaning up StateMachine |
| 434 | * when no response |
| 435 | * from RADIUS SERVER. |
| 436 | * |
| 437 | * @param sessionId SessionId of the current session |
| 438 | * @param stateMachine StateMachine for the id |
| 439 | */ |
| 440 | public void scheduleStateMachineCleanupTimer(String sessionId, StateMachine stateMachine) { |
| 441 | StateMachine.CleanupTimerTask cleanupTask = stateMachine.new CleanupTimerTask(sessionId, this); |
| 442 | ScheduledFuture<?> cleanupTimer = executor.schedule(cleanupTask, cleanupTimerTimeOutInMins, TimeUnit.MINUTES); |
| 443 | stateMachine.setCleanupTimer(cleanupTimer); |
| 444 | |
| 445 | } |
| 446 | |
| 447 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 448 | * Handles RADIUS packets. |
| 449 | * |
| 450 | * @param radiusPacket RADIUS packet coming from the RADIUS server. |
| 451 | * @throws StateMachineException if an illegal state transition is triggered |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 452 | * @throws DeserializationException if packet deserialization fails |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 453 | */ |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 454 | public void handleRadiusPacket(RADIUS radiusPacket) |
| 455 | throws StateMachineException, DeserializationException { |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 456 | if (log.isTraceEnabled()) { |
| 457 | log.trace("Received RADIUS packet {}", radiusPacket); |
| 458 | } |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 459 | if (radiusOperationalStatusService.isRadiusResponseForOperationalStatus(radiusPacket.getIdentifier())) { |
| 460 | radiusOperationalStatusService.handleRadiusPacketForOperationalStatus(radiusPacket); |
| 461 | return; |
| 462 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 463 | StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier()); |
| 464 | if (stateMachine == null) { |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 465 | log.error("Invalid packet identifier {}, could not find corresponding " |
| 466 | + "state machine ... exiting", radiusPacket.getIdentifier()); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 467 | aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired(); |
| 468 | aaaStatisticsManager.getAaaStats().countDroppedResponsesRx(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 469 | return; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 470 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 471 | EAP eapPayload; |
| 472 | Ethernet eth; |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 473 | checkReceivedPacketForValidValidator(radiusPacket, stateMachine.requestAuthenticator()); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 474 | if (outPacketSet.contains(radiusPacket.getIdentifier())) { |
| 475 | aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(false); |
| 476 | outPacketSet.remove(new Byte(radiusPacket.getIdentifier())); |
| 477 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 478 | switch (radiusPacket.getCode()) { |
| 479 | case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 480 | log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE"); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 481 | RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE); |
| 482 | byte[] challengeState = null; |
| 483 | if (radiusAttrState != null) { |
| 484 | challengeState = radiusAttrState.getValue(); |
| 485 | } |
| 486 | eapPayload = radiusPacket.decapsulateMessage(); |
| 487 | stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState); |
| 488 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
| 489 | MacAddress.valueOf(nasMacAddress), |
| 490 | stateMachine.vlanId(), |
| 491 | EAPOL.EAPOL_PACKET, |
| 492 | eapPayload, stateMachine.priorityCode()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 493 | log.debug("Send EAP challenge response to supplicant {}", stateMachine.supplicantAddress().toString()); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 494 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), true); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 495 | aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx(); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 496 | outPacketSupp.add(eapPayload.getIdentifier()); |
| 497 | aaaStatisticsManager.getAaaStats().incrementPendingResSupp(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 498 | break; |
| 499 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 500 | log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT"); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 501 | //send an EAPOL - Success to the supplicant. |
| 502 | byte[] eapMessageSuccess = |
| 503 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 504 | eapPayload = EAP.deserializer().deserialize( |
| 505 | eapMessageSuccess, 0, eapMessageSuccess.length); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 506 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
| 507 | MacAddress.valueOf(nasMacAddress), |
| 508 | stateMachine.vlanId(), |
| 509 | EAPOL.EAPOL_PACKET, |
| 510 | eapPayload, stateMachine.priorityCode()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 511 | log.info("Send EAP success message to supplicant {}", stateMachine.supplicantAddress().toString()); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 512 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false); |
| 513 | aaaStatisticsManager.getAaaStats().incrementEapolAuthSuccessTrans(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 514 | |
| 515 | stateMachine.authorizeAccess(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 516 | aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 517 | break; |
| 518 | case RADIUS.RADIUS_CODE_ACCESS_REJECT: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 519 | log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT"); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 520 | //send an EAPOL - Failure to the supplicant. |
| 521 | byte[] eapMessageFailure; |
| 522 | eapPayload = new EAP(); |
| 523 | RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE); |
| 524 | if (radiusAttrEap == null) { |
| 525 | eapPayload.setCode(EAP.FAILURE); |
| 526 | eapPayload.setIdentifier(stateMachine.challengeIdentifier()); |
| 527 | eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL); |
| 528 | } else { |
| 529 | eapMessageFailure = radiusAttrEap.getValue(); |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 530 | eapPayload = EAP.deserializer().deserialize( |
| 531 | eapMessageFailure, 0, eapMessageFailure.length); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 532 | } |
| 533 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
| 534 | MacAddress.valueOf(nasMacAddress), |
| 535 | stateMachine.vlanId(), |
| 536 | EAPOL.EAPOL_PACKET, |
| 537 | eapPayload, stateMachine.priorityCode()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 538 | log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString()); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 539 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false); |
| 540 | aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans(); |
| 541 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 542 | stateMachine.denyAccess(); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 543 | aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 544 | break; |
| 545 | default: |
| 546 | log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode()); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 547 | aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx(); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 548 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 549 | aaaStatisticsManager.getAaaStats().countDroppedResponsesRx(); |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 552 | /** |
| 553 | * Send the ethernet packet to the supplicant. |
| 554 | * |
| 555 | * @param ethernetPkt the ethernet packet |
| 556 | * @param connectPoint the connect point to send out |
| 557 | */ |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 558 | private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint, boolean isChallengeResponse) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 559 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 560 | OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 561 | treatment, ByteBuffer.wrap(ethernetPkt.serialize())); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 562 | EAPOL eap = ((EAPOL) ethernetPkt.getPayload()); |
| 563 | EAP eapPkt = (EAP) eap.getPayload(); |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 564 | if (log.isTraceEnabled()) { |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 565 | log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}", |
| 566 | eap, ethernetPkt, connectPoint); |
| 567 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 568 | packetService.emit(packet); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 569 | if (isChallengeResponse) { |
| 570 | aaaStatisticsManager.getAaaStats().incrementEapPktTxauthEap(); |
| 571 | } |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 572 | aaaStatisticsManager.getAaaStats().incrementEapolFramesTx(); |
| 573 | aaaStatisticsManager.getAaaStats().countReqEapFramesTx(); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 576 | @Override |
| 577 | public String toString() { |
| 578 | return ToStringBuilder.reflectionToString(this); |
| 579 | } |
| 580 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 581 | // our handler defined as a private inner class |
| 582 | |
| 583 | /** |
| 584 | * Packet processor responsible for forwarding packets along their paths. |
| 585 | */ |
| 586 | private class ReactivePacketProcessor implements PacketProcessor { |
| 587 | @Override |
| 588 | public void process(PacketContext context) { |
| 589 | |
| 590 | // Extract the original Ethernet frame from the packet information |
| 591 | InboundPacket pkt = context.inPacket(); |
| 592 | Ethernet ethPkt = pkt.parsed(); |
| 593 | if (ethPkt == null) { |
| 594 | return; |
| 595 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 596 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 597 | try { |
| 598 | // identify if incoming packet comes from supplicant (EAP) or RADIUS |
| 599 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { |
| 600 | case EAPOL: |
| 601 | handleSupplicantPacket(context.inPacket()); |
| 602 | break; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 603 | default: |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 604 | // any other packets let the specific implementation handle |
| 605 | impl.handlePacketFromServer(context); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 606 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 607 | } catch (StateMachineException e) { |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 608 | log.warn("Unable to process packet:", e); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 612 | /** |
| 613 | * Creates and initializes common fields of a RADIUS packet. |
| 614 | * |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 615 | * @param stateMachine state machine for the request |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 616 | * @param eapPacket EAP packet |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 617 | * @return RADIUS packet |
| 618 | */ |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 619 | private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) { |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 620 | RADIUS radiusPayload = |
| 621 | new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
| 622 | eapPacket.getIdentifier()); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 623 | |
| 624 | // set Request Authenticator in StateMachine |
| 625 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
| 626 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 627 | radiusPayload.setIdentifier(identifier); |
| 628 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 629 | stateMachine.username()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 630 | |
| 631 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 632 | AaaManager.this.nasIpAddress.getAddress()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 633 | |
| 634 | radiusPayload.encapsulateMessage(eapPacket); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 635 | |
| 636 | return radiusPayload; |
| 637 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 638 | |
| 639 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 640 | * Handles PAE packets (supplicant). |
| 641 | * |
| 642 | * @param inPacket Ethernet packet coming from the supplicant |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 643 | */ |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 644 | private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException { |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 645 | Ethernet ethPkt = inPacket.parsed(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 646 | // Where does it come from? |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 647 | MacAddress srcMac = ethPkt.getSourceMAC(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 648 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 649 | DeviceId deviceId = inPacket.receivedFrom().deviceId(); |
| 650 | PortNumber portNumber = inPacket.receivedFrom().port(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 651 | String sessionId = deviceId.toString() + portNumber.toString(); |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 652 | EAPOL eapol = (EAPOL) ethPkt.getPayload(); |
| 653 | if (log.isTraceEnabled()) { |
| 654 | log.trace("Received EAPOL packet {} in enclosing packet {} from " |
| 655 | + "dev/port: {}/{}", eapol, ethPkt, deviceId, |
| 656 | portNumber); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 657 | } |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 658 | |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 659 | short pktlen = eapol.getPacketLength(); |
| 660 | byte[] eapPayLoadBuffer = eapol.serialize(); |
| 661 | int len = eapPayLoadBuffer.length; |
| 662 | if (len != (HEADER_LENGTH + pktlen)) { |
| 663 | aaaStatisticsManager.getAaaStats().incrementInvalidBodyLength(); |
| 664 | return; |
| 665 | } |
| 666 | if (!VALID_EAPOL_TYPE.contains(eapol.getEapolType())) { |
| 667 | aaaStatisticsManager.getAaaStats().incrementInvalidPktType(); |
| 668 | return; |
| 669 | } |
| 670 | if (pktlen >= 0 && ethPkt.getEtherType() == EthType.EtherType.EAPOL.ethType().toShort()) { |
| 671 | aaaStatisticsManager.getAaaStats().incrementValidEapolFramesRx(); |
| 672 | } |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 673 | StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId); |
| 674 | if (stateMachine == null) { |
| 675 | log.debug("Creating new state machine for sessionId: {} for " |
| 676 | + "dev/port: {}/{}", sessionId, deviceId, portNumber); |
| 677 | stateMachine = new StateMachine(sessionId); |
| 678 | } else { |
| 679 | log.debug("Using existing state-machine for sessionId: {}", sessionId); |
| 680 | } |
| 681 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 682 | switch (eapol.getEapolType()) { |
| 683 | case EAPOL.EAPOL_START: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 684 | log.debug("EAP packet: EAPOL_START"); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 685 | stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 686 | if (stateMachine.getCleanupTimer() == null) { |
| 687 | scheduleStateMachineCleanupTimer(sessionId, stateMachine); |
| 688 | } |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 689 | stateMachine.start(); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 690 | aaaStatisticsManager.getAaaStats().incrementEapolStartReqTrans(); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 691 | //send an EAP Request/Identify to the supplicant |
| 692 | EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 693 | if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) { |
| 694 | stateMachine.setPriorityCode(ethPkt.getPriorityCode()); |
| 695 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 696 | Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress), |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 697 | ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 698 | eapPayload, stateMachine.priorityCode()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 699 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 700 | stateMachine.setSupplicantAddress(srcMac); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 701 | stateMachine.setVlanId(ethPkt.getVlanID()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 702 | log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString()); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 703 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 704 | aaaStatisticsManager.getAaaStats().incrementRequestIdFramesTx(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 705 | |
| 706 | break; |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 707 | case EAPOL.EAPOL_LOGOFF: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 708 | log.debug("EAP packet: EAPOL_LOGOFF"); |
Ray Milkey | b34b496 | 2016-01-04 10:24:43 -0800 | [diff] [blame] | 709 | if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) { |
| 710 | stateMachine.logoff(); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 711 | aaaStatisticsManager.getAaaStats().incrementEapolLogoffRx(); |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 712 | } |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 713 | if (stateMachine.state() == StateMachine.STATE_IDLE) { |
| 714 | aaaStatisticsManager.getAaaStats().incrementAuthStateIdle(); |
| 715 | } |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 716 | |
| 717 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 718 | case EAPOL.EAPOL_PACKET: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 719 | RADIUS radiusPayload; |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 720 | // check if this is a Response/Identify or a Response/TLS |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 721 | EAP eapPacket = (EAP) eapol.getPayload(); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 722 | Byte identifier = new Byte(eapPacket.getIdentifier()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 723 | |
| 724 | byte dataType = eapPacket.getDataType(); |
| 725 | switch (dataType) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 726 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 727 | case EAP.ATTR_IDENTITY: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 728 | log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY"); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 729 | //Setting the time of this response from RG, only when its not a re-transmission. |
| 730 | if (stateMachine.getLastPacketReceivedTime() == 0) { |
| 731 | stateMachine.setLastPacketReceivedTime(System.currentTimeMillis()); |
| 732 | } |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 733 | // request id access to RADIUS |
| 734 | stateMachine.setUsername(eapPacket.getData()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 735 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 736 | radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 737 | radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 738 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 739 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 740 | sendRadiusPacket(radiusPayload, inPacket); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 741 | stateMachine.setWaitingForRadiusResponse(true); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 742 | aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity(); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 743 | // change the state to "PENDING" |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 744 | if (stateMachine.state() == StateMachine.STATE_PENDING) { |
| 745 | aaaStatisticsManager.getAaaStats().increaseRequestReTx(); |
| 746 | } |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 747 | stateMachine.requestAccess(); |
| 748 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 749 | case EAP.ATTR_MD5: |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 750 | stateMachine.setLastPacketReceivedTime(System.currentTimeMillis()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 751 | log.debug("EAP packet: EAPOL_PACKET ATTR_MD5"); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 752 | // verify if the EAP identifier corresponds to the |
| 753 | // challenge identifier from the client state |
| 754 | // machine. |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 755 | if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 756 | //send the RADIUS challenge response |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 757 | radiusPayload = |
| 758 | getRadiusPayload(stateMachine, |
| 759 | stateMachine.identifier(), |
| 760 | eapPacket); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 761 | radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 762 | |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 763 | if (stateMachine.challengeState() != null) { |
| 764 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 765 | stateMachine.challengeState()); |
| 766 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 767 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 768 | if (outPacketSupp.contains(eapPacket.getIdentifier())) { |
| 769 | aaaStatisticsManager.getAaaStats().decrementPendingResSupp(); |
| 770 | outPacketSupp.remove(identifier); |
| 771 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 772 | sendRadiusPacket(radiusPayload, inPacket); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 773 | stateMachine.setWaitingForRadiusResponse(true); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 774 | aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 775 | } |
| 776 | break; |
| 777 | case EAP.ATTR_TLS: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 778 | log.debug("EAP packet: EAPOL_PACKET ATTR_TLS"); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 779 | // request id access to RADIUS |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 780 | radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 781 | radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 782 | |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 783 | if (stateMachine.challengeState() != null) { |
| 784 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 785 | stateMachine.challengeState()); |
| 786 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 787 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 788 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 789 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 790 | if (outPacketSupp.contains(eapPacket.getIdentifier())) { |
| 791 | aaaStatisticsManager.getAaaStats().decrementPendingResSupp(); |
| 792 | outPacketSupp.remove(identifier); |
| 793 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 794 | sendRadiusPacket(radiusPayload, inPacket); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 795 | stateMachine.setWaitingForRadiusResponse(true); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 796 | aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall(); |
Ray Milkey | 5493b51 | 2015-10-21 12:13:49 -0700 | [diff] [blame] | 797 | |
Ray Milkey | f3790b8 | 2015-10-21 16:28:08 -0700 | [diff] [blame] | 798 | if (stateMachine.state() != StateMachine.STATE_PENDING) { |
| 799 | stateMachine.requestAccess(); |
| 800 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 801 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 802 | break; |
| 803 | default: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 804 | log.warn("Unknown EAP packet type"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 805 | return; |
| 806 | } |
| 807 | break; |
| 808 | default: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 809 | log.debug("Skipping EAPOL message {}", eapol.getEapolType()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 810 | } |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 811 | aaaStatisticsManager.getAaaStats().countTransRespNotNak(); |
| 812 | aaaStatisticsManager.getAaaStats().countEapolResIdentityMsgTrans(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 813 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 816 | /** |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 817 | * Delegate allowing the StateMachine to notify us of events. |
| 818 | */ |
| 819 | private class InternalStateMachineDelegate implements StateMachineDelegate { |
| 820 | |
| 821 | @Override |
| 822 | public void notify(AuthenticationEvent authenticationEvent) { |
| 823 | log.info("Auth event {} for {}", |
| 824 | authenticationEvent.type(), authenticationEvent.subject()); |
| 825 | post(authenticationEvent); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 830 | * Configuration Listener, handles change in configuration. |
| 831 | */ |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 832 | private class InternalConfigListener implements NetworkConfigListener { |
| 833 | |
| 834 | /** |
Ray Milkey | c9e8dcc | 2015-12-30 10:31:32 -0800 | [diff] [blame] | 835 | * Reconfigures the AAA application according to the |
| 836 | * configuration parameters passed. |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 837 | * |
| 838 | * @param cfg configuration object |
| 839 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 840 | private void reconfigureNetwork(AaaConfig cfg) { |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 841 | log.info("Reconfiguring AaaConfig from config: {}", cfg); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 842 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 843 | if (cfg == null) { |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 844 | newCfg = new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 845 | } else { |
| 846 | newCfg = cfg; |
| 847 | } |
| 848 | if (newCfg.nasIp() != null) { |
| 849 | nasIpAddress = newCfg.nasIp(); |
| 850 | } |
| 851 | if (newCfg.radiusIp() != null) { |
| 852 | radiusIpAddress = newCfg.radiusIp(); |
| 853 | } |
| 854 | if (newCfg.radiusMac() != null) { |
| 855 | radiusMacAddress = newCfg.radiusMac(); |
| 856 | } |
| 857 | if (newCfg.nasMac() != null) { |
| 858 | nasMacAddress = newCfg.nasMac(); |
| 859 | } |
| 860 | if (newCfg.radiusSecret() != null) { |
| 861 | radiusSecret = newCfg.radiusSecret(); |
| 862 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 863 | |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 864 | boolean reconfigureCustomizer = false; |
| 865 | if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) { |
| 866 | customizer = newCfg.radiusPktCustomizer(); |
| 867 | configurePacketCustomizer(); |
| 868 | reconfigureCustomizer = true; |
| 869 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 870 | |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 871 | if (radiusConnectionType == null |
| 872 | || reconfigureCustomizer |
| 873 | || !radiusConnectionType.equals(newCfg.radiusConnectionType())) { |
| 874 | radiusConnectionType = newCfg.radiusConnectionType(); |
| 875 | if (impl != null) { |
| 876 | impl.withdrawIntercepts(); |
| 877 | impl.clearLocalState(); |
| 878 | } |
| 879 | configureRadiusCommunication(); |
| 880 | impl.initializeLocalState(newCfg); |
| 881 | impl.requestIntercepts(); |
| 882 | } else if (impl != null) { |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 883 | impl.clearLocalState(); |
| 884 | impl.initializeLocalState(newCfg); |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 885 | } |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | @Override |
| 889 | public void event(NetworkConfigEvent event) { |
| 890 | |
| 891 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 892 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 893 | event.configClass().equals(AaaConfig.class)) { |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 894 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 895 | AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 896 | reconfigureNetwork(cfg); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 897 | |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 898 | log.info("Reconfigured: {}", cfg.toString()); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 902 | |
| 903 | private class InternalDeviceListener implements DeviceListener { |
| 904 | @Override |
| 905 | public void event(DeviceEvent event) { |
| 906 | |
| 907 | switch (event.type()) { |
| 908 | case PORT_REMOVED: |
| 909 | DeviceId devId = event.subject().id(); |
| 910 | PortNumber portNumber = event.port().number(); |
| 911 | String sessionId = devId.toString() + portNumber.toString(); |
| 912 | |
| 913 | Map<String, StateMachine> sessionIdMap = StateMachine.sessionIdMap(); |
| 914 | StateMachine removed = sessionIdMap.remove(sessionId); |
| 915 | if (removed != null) { |
| 916 | StateMachine.deleteStateMachineMapping(removed); |
| 917 | } |
| 918 | |
| 919 | break; |
| 920 | default: |
| 921 | return; |
| 922 | } |
| 923 | } |
| 924 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 925 | private class AuthenticationStatisticsEventPublisher implements Runnable { |
| 926 | private final Logger log = getLogger(getClass()); |
| 927 | public void run() { |
| 928 | log.info("Notifying AuthenticationStatisticsEvent"); |
| 929 | aaaStatisticsManager.calculatePacketRoundtripTime(); |
| 930 | log.debug("AcceptResponsesRx---" + aaaStatisticsManager.getAaaStats().getAcceptResponsesRx()); |
| 931 | log.debug("AccessRequestsTx---" + aaaStatisticsManager.getAaaStats().getAccessRequestsTx()); |
| 932 | log.debug("ChallengeResponsesRx---" + aaaStatisticsManager.getAaaStats().getChallengeResponsesRx()); |
| 933 | log.debug("DroppedResponsesRx---" + aaaStatisticsManager.getAaaStats().getDroppedResponsesRx()); |
| 934 | log.debug("InvalidValidatorsRx---" + aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx()); |
| 935 | log.debug("MalformedResponsesRx---" + aaaStatisticsManager.getAaaStats().getMalformedResponsesRx()); |
| 936 | log.debug("PendingRequests---" + aaaStatisticsManager.getAaaStats().getPendingRequests()); |
| 937 | log.debug("RejectResponsesRx---" + aaaStatisticsManager.getAaaStats().getRejectResponsesRx()); |
| 938 | log.debug("RequestReTx---" + aaaStatisticsManager.getAaaStats().getRequestReTx()); |
| 939 | log.debug("RequestRttMilis---" + aaaStatisticsManager.getAaaStats().getRequestRttMilis()); |
| 940 | log.debug("UnknownServerRx---" + aaaStatisticsManager.getAaaStats().getUnknownServerRx()); |
| 941 | log.debug("UnknownTypeRx---" + aaaStatisticsManager.getAaaStats().getUnknownTypeRx()); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 942 | log.debug("TimedOutPackets----" + aaaStatisticsManager.getAaaStats().getTimedOutPackets()); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 943 | log.debug("EapolLogoffRx---" + aaaStatisticsManager.getAaaStats().getEapolLogoffRx()); |
| 944 | log.debug("EapolAuthSuccessTrans---" + aaaStatisticsManager.getAaaStats().getEapolAuthSuccessTrans()); |
| 945 | log.debug("EapolAuthFailureTrans---" + |
| 946 | aaaStatisticsManager.getAaaStats().getEapolAuthFailureTrans()); |
| 947 | log.debug("EapolStartReqTrans---" + |
| 948 | aaaStatisticsManager.getAaaStats().getEapolStartReqTrans()); |
| 949 | log.debug("EapolTransRespNotNak---" + |
| 950 | aaaStatisticsManager.getAaaStats().getEapolTransRespNotNak()); |
| 951 | log.debug("EapPktTxauthChooseEap---" + |
| 952 | aaaStatisticsManager.getAaaStats().getEapPktTxauthChooseEap()); |
| 953 | log.debug("EapolResIdentityMsgTrans---" + |
| 954 | aaaStatisticsManager.getAaaStats().getEapolResIdentityMsgTrans()); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 955 | log.debug("EapolFramesTx---" + aaaStatisticsManager.getAaaStats().getEapolFramesTx()); |
| 956 | log.debug("AuthStateIdle---" + aaaStatisticsManager.getAaaStats().getAuthStateIdle()); |
| 957 | log.debug("RequestIdFramesTx---" + aaaStatisticsManager.getAaaStats().getRequestIdFramesTx()); |
| 958 | log.debug("ReqEapFramesTx---" + aaaStatisticsManager.getAaaStats().getReqEapFramesTx()); |
| 959 | log.debug("InvalidPktType---" + aaaStatisticsManager.getAaaStats().getInvalidPktType()); |
| 960 | log.debug("InvalidBodyLength---" + aaaStatisticsManager.getAaaStats().getInvalidBodyLength()); |
| 961 | log.debug("ValidEapolFramesRx---" + aaaStatisticsManager.getAaaStats().getValidEapolFramesRx()); |
| 962 | log.debug("PendingResSupp---" + aaaStatisticsManager.getAaaStats().getPendingResSupp()); |
| 963 | log.debug("ResIdEapFramesRx---" + aaaStatisticsManager.getAaaStats().getEapolattrIdentity()); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 964 | aaaStatisticsManager.getStatsDelegate(). |
| 965 | notify(new AuthenticationStatisticsEvent(AuthenticationStatisticsEvent.Type.STATS_UPDATE, |
| 966 | aaaStatisticsManager.getAaaStats())); |
| 967 | } |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 968 | } |
| 969 | |
| 970 | private class ServerStatusChecker implements Runnable { |
| 971 | @Override |
| 972 | public void run() { |
| 973 | log.info("Notifying RadiusOperationalStatusEvent"); |
| 974 | radiusOperationalStatusService.checkServerOperationalStatus(); |
| 975 | log.info("--POSTING--" + radiusOperationalStatusService.getRadiusServerOperationalStatus()); |
| 976 | radiusOperationalStatusService.getRadiusOprStDelegate() |
| 977 | .notify(new RadiusOperationalStatusEvent( |
| 978 | RadiusOperationalStatusEvent.Type.RADIUS_OPERATIONAL_STATUS, |
| 979 | radiusOperationalStatusService. |
| 980 | getRadiusServerOperationalStatus())); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 981 | } |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 982 | |
| 983 | } |
| 984 | } |