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 | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 20 | |
| 21 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 22 | import static org.onlab.util.Tools.groupedThreads; |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 23 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 24 | import static org.opencord.aaa.impl.OsgiPropertyConstants.*; |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 26 | |
| 27 | import com.google.common.collect.Sets; |
| 28 | import java.net.InetAddress; |
| 29 | import java.net.UnknownHostException; |
| 30 | import java.nio.ByteBuffer; |
| 31 | import java.util.Dictionary; |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.List; |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 35 | import java.util.Map; |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 36 | import java.util.Set; |
| 37 | import java.util.Map.Entry; |
| 38 | |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 39 | import org.apache.commons.lang3.builder.ToStringBuilder; |
Jonathan Hart | 4731dd9 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 40 | import org.onlab.packet.DeserializationException; |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 41 | import org.onlab.packet.EAP; |
| 42 | import org.onlab.packet.EAPOL; |
| 43 | import org.onlab.packet.EthType; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 44 | import org.onlab.packet.Ethernet; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 45 | import org.onlab.packet.MacAddress; |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 46 | import org.onlab.packet.RADIUS; |
| 47 | import org.onlab.packet.RADIUSAttribute; |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 48 | import org.onlab.util.KryoNamespace; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 49 | import org.onlab.util.Tools; |
| 50 | import org.onosproject.cfg.ComponentConfigService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 51 | import org.onosproject.core.ApplicationId; |
| 52 | import org.onosproject.core.CoreService; |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 53 | import org.onosproject.event.AbstractListenerManager; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 54 | import org.onosproject.mastership.MastershipService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 55 | import org.onosproject.net.ConnectPoint; |
| 56 | import org.onosproject.net.DeviceId; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 57 | import org.onosproject.net.PortNumber; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 58 | import org.onosproject.net.config.ConfigFactory; |
| 59 | import org.onosproject.net.config.NetworkConfigEvent; |
| 60 | import org.onosproject.net.config.NetworkConfigListener; |
| 61 | import org.onosproject.net.config.NetworkConfigRegistry; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 62 | import org.onosproject.net.device.DeviceEvent; |
| 63 | import org.onosproject.net.device.DeviceListener; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 64 | import org.onosproject.net.device.DeviceService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 65 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 66 | import org.onosproject.net.flow.TrafficTreatment; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 67 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 68 | import org.onosproject.net.packet.InboundPacket; |
| 69 | import org.onosproject.net.packet.OutboundPacket; |
| 70 | import org.onosproject.net.packet.PacketContext; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 71 | import org.onosproject.net.packet.PacketProcessor; |
| 72 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 73 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 74 | import org.onosproject.store.service.ConsistentMap; |
| 75 | import org.onosproject.store.service.MapEvent; |
| 76 | import org.onosproject.store.service.MapEventListener; |
| 77 | import org.onosproject.store.service.Serializer; |
| 78 | import org.onosproject.store.service.StorageService; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 79 | import org.opencord.aaa.AaaConfig; |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 80 | import org.opencord.aaa.AaaMachineStatisticsEvent; |
| 81 | import org.opencord.aaa.AaaMachineStatisticsService; |
| 82 | import org.opencord.aaa.AaaSupplicantMachineStats; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 83 | import org.opencord.aaa.AuthenticationEvent; |
| 84 | import org.opencord.aaa.AuthenticationEventListener; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 85 | import org.opencord.aaa.AuthenticationRecord; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 86 | import org.opencord.aaa.AuthenticationService; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 87 | import org.opencord.aaa.AuthenticationStatisticsService; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 88 | import org.opencord.aaa.RadiusCommunicator; |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 89 | import org.opencord.aaa.RadiusOperationalStatusEvent; |
| 90 | import org.opencord.aaa.RadiusOperationalStatusService; |
| 91 | import org.opencord.aaa.RadiusOperationalStatusService.RadiusOperationalStatusEvaluationMode; |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 92 | import org.opencord.aaa.StateMachineDelegate; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 93 | import org.opencord.sadis.BaseInformationService; |
| 94 | import org.opencord.sadis.SadisService; |
| 95 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 96 | import org.osgi.service.component.ComponentContext; |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 97 | import org.osgi.service.component.annotations.Activate; |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 98 | import org.osgi.service.component.annotations.Component; |
| 99 | import org.osgi.service.component.annotations.Deactivate; |
| 100 | import org.osgi.service.component.annotations.Modified; |
| 101 | import org.osgi.service.component.annotations.Reference; |
| 102 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 103 | import org.slf4j.Logger; |
| 104 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 105 | import javax.crypto.Mac; |
| 106 | import javax.crypto.spec.SecretKeySpec; |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 107 | import java.util.Optional; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 108 | import java.util.concurrent.ConcurrentMap; |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 109 | import java.util.concurrent.ExecutorService; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 110 | import java.util.concurrent.Executors; |
| 111 | import java.util.concurrent.ScheduledExecutorService; |
| 112 | import java.util.concurrent.ScheduledFuture; |
| 113 | import java.util.concurrent.TimeUnit; |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 114 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 115 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 116 | * AAA application for ONOS. |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 117 | */ |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 118 | @Component(immediate = true, property = { |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 119 | OPERATIONAL_STATUS_SERVER_EVENT_GENERATION + ":Integer=" + OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT, |
| 120 | OPERATIONAL_STATUS_SERVER_TIMEOUT + ":Integer=" + OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT, |
| 121 | STATUS_SERVER_MODE + ":String=" + STATUS_SERVER_MODE_DEFAULT, |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 122 | PACKET_PROCESSOR_THREADS + ":Integer=" + PACKET_PROCESSOR_THREADS_DEFAULT, |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 123 | }) |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 124 | public class AaaManager |
| 125 | extends AbstractListenerManager<AuthenticationEvent, AuthenticationEventListener> |
| 126 | implements AuthenticationService { |
| 127 | |
Charles Chan | df7ff86 | 2017-01-20 11:22:05 -0800 | [diff] [blame] | 128 | private static final String APP_NAME = "org.opencord.aaa"; |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 129 | private static final int STATE_MACHINE_THREADS = 3; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 130 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 131 | private final Logger log = getLogger(getClass()); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 132 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 134 | protected CoreService coreService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 135 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 137 | protected StorageService storageService; |
| 138 | |
| 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 140 | protected PacketService packetService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 141 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 143 | protected NetworkConfigRegistry netCfgService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 144 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 146 | protected DeviceService deviceService; |
| 147 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 149 | protected SadisService sadisService; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 150 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 152 | protected MastershipService mastershipService; |
| 153 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 155 | protected AuthenticationStatisticsService aaaStatisticsManager; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 156 | |
Carmelo Cascone | 58b5329 | 2019-09-30 12:35:31 -0700 | [diff] [blame] | 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 158 | protected AaaMachineStatisticsService aaaSupplicantStatsManager; |
| 159 | |
| 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 161 | protected ComponentConfigService cfgService; |
| 162 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 164 | protected RadiusOperationalStatusService radiusOperationalStatusService; |
| 165 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 166 | protected BaseInformationService<SubscriberAndDeviceInformation> subsService; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 167 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 168 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 169 | // Properties |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 170 | private int operationalStatusEventGenerationPeriodInSeconds = OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT; |
| 171 | private int operationalStatusServerTimeoutInSeconds = OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT; |
| 172 | protected String operationalStatusEvaluationMode = STATUS_SERVER_MODE_DEFAULT; |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 173 | /** |
| 174 | * Number of threads used to process the packet. |
| 175 | */ |
| 176 | protected int packetProcessorThreads = PACKET_PROCESSOR_THREADS_DEFAULT; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 177 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 178 | private IdentifierManager idManager; |
| 179 | |
| 180 | private ConcurrentMap<String, StateMachine> stateMachines; |
| 181 | |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 182 | private ConsistentMap<ConnectPoint, AuthenticationRecord> authenticationsConsistentMap; |
| 183 | private Map<ConnectPoint, AuthenticationRecord> authentications; |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 184 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 185 | // NAS IP address |
| 186 | protected InetAddress nasIpAddress; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 187 | |
| 188 | // self MAC address |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 189 | protected String nasMacAddress; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 190 | |
| 191 | // Parsed RADIUS server addresses |
| 192 | protected InetAddress radiusIpAddress; |
| 193 | |
| 194 | // MAC address of RADIUS server or net hop router |
| 195 | protected String radiusMacAddress; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 196 | |
| 197 | // RADIUS server secret |
| 198 | protected String radiusSecret; |
| 199 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 200 | // bindings |
| 201 | protected CustomizationInfo customInfo; |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 202 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 203 | // our application-specific event handler |
| 204 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 205 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 206 | // our unique identifier |
| 207 | private ApplicationId appId; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 208 | |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 209 | // TimeOut time for cleaning up stateMachines stuck due to pending AAA/EAPOL message. |
| 210 | protected int cleanupTimerTimeOutInMins; |
| 211 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 212 | // Setup specific customization/attributes on the RADIUS packets |
| 213 | PacketCustomizer pktCustomizer; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 214 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 215 | // packet customizer to use |
| 216 | private String customizer; |
| 217 | |
| 218 | // Type of connection to use to communicate with Radius server, options are |
| 219 | // "socket" or "packet_out" |
| 220 | private String radiusConnectionType; |
| 221 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 222 | // Object for the specific type of communication with the RADIUS |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 223 | // server, socket based or packet_out based |
| 224 | RadiusCommunicator impl = null; |
| 225 | |
| 226 | // latest configuration |
| 227 | AaaConfig newCfg; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 228 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 229 | ScheduledFuture<?> scheduledStatusServerChecker; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 230 | String configuredAaaServerAddress; |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 231 | HashSet<Byte> outPacketSet = new HashSet<>(); |
| 232 | HashSet<Byte> outPacketSupp = new HashSet<>(); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 233 | static final List<Byte> VALID_EAPOL_TYPE = Arrays.asList(EAPOL.EAPOL_START, EAPOL.EAPOL_LOGOFF, EAPOL.EAPOL_PACKET); |
| 234 | static final int HEADER_LENGTH = 4; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 235 | // Configuration properties factory |
| 236 | private final ConfigFactory factory = |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 237 | new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY, |
| 238 | AaaConfig.class, |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 239 | "AAA") { |
| 240 | @Override |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 241 | public AaaConfig createConfig() { |
| 242 | return new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 243 | } |
| 244 | }; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 245 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 246 | // Listener for config changes |
| 247 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 248 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 249 | private final InternalMapEventListener mapListener = new InternalMapEventListener(); |
| 250 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 251 | private StateMachineDelegate delegate = new InternalStateMachineDelegate(); |
| 252 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 253 | protected ExecutorService packetProcessorExecutor; |
| 254 | protected ScheduledExecutorService serverStatusAndStateMachineTimeoutExecutor; |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Builds an EAPOL packet based on the given parameters. |
| 257 | * |
| 258 | * @param dstMac destination MAC address |
| 259 | * @param srcMac source MAC address |
| 260 | * @param vlan vlan identifier |
| 261 | * @param eapolType EAPOL type |
| 262 | * @param eap EAP payload |
| 263 | * @return Ethernet frame |
| 264 | */ |
| 265 | private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac, |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 266 | short vlan, byte eapolType, EAP eap, byte priorityCode) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 267 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 268 | Ethernet eth = new Ethernet(); |
| 269 | eth.setDestinationMACAddress(dstMac.toBytes()); |
| 270 | eth.setSourceMACAddress(srcMac.toBytes()); |
| 271 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 272 | if (vlan != Ethernet.VLAN_UNTAGGED) { |
| 273 | eth.setVlanID(vlan); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 274 | eth.setPriorityCode(priorityCode); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 275 | } |
| 276 | //eapol header |
| 277 | EAPOL eapol = new EAPOL(); |
| 278 | eapol.setEapolType(eapolType); |
| 279 | eapol.setPacketLength(eap.getLength()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 280 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 281 | //eap part |
| 282 | eapol.setPayload(eap); |
| 283 | |
| 284 | eth.setPayload(eapol); |
| 285 | eth.setPad(true); |
| 286 | return eth; |
| 287 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 288 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 289 | @Activate |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 290 | public void activate(ComponentContext context) { |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 291 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 292 | idManager = new IdentifierManager(); |
| 293 | stateMachines = Maps.newConcurrentMap(); |
Charles Chan | df7ff86 | 2017-01-20 11:22:05 -0800 | [diff] [blame] | 294 | appId = coreService.registerApplication(APP_NAME); |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 295 | |
| 296 | KryoNamespace authSerializer = KryoNamespace.newBuilder() |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 297 | .register(KryoNamespaces.API) |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 298 | .register(AuthenticationRecord.class) |
| 299 | .build(); |
| 300 | |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 301 | authenticationsConsistentMap = storageService.<ConnectPoint, AuthenticationRecord>consistentMapBuilder() |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 302 | .withApplicationId(appId) |
| 303 | .withName("authentications") |
| 304 | .withSerializer(Serializer.using(authSerializer)) |
| 305 | .build(); |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 306 | authenticationsConsistentMap.addListener(mapListener); |
| 307 | authentications = authenticationsConsistentMap.asJavaMap(); |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 308 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 309 | eventDispatcher.addSink(AuthenticationEvent.class, listenerRegistry); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 310 | netCfgService.addListener(cfgListener); |
| 311 | netCfgService.registerConfigFactory(factory); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 312 | cfgService.registerProperties(getClass()); |
| 313 | modified(context); |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 314 | subsService = sadisService.getSubscriberInfoService(); |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 315 | customInfo = new CustomizationInfo(subsService, deviceService); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 316 | cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class)); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 317 | log.info("Starting with config {} {}", this, newCfg); |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 318 | configureRadiusCommunication(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 319 | // register our event handler |
Brian O'Connor | d9c7da0 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 320 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 321 | StateMachine.setDelegate(delegate); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 322 | cleanupTimerTimeOutInMins = newCfg.sessionCleanupTimer(); |
| 323 | StateMachine.setcleanupTimerTimeOutInMins(cleanupTimerTimeOutInMins); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 324 | impl.initializeLocalState(newCfg); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 325 | impl.requestIntercepts(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 326 | deviceService.addListener(deviceListener); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 327 | getConfiguredAaaServerAddress(); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 328 | radiusOperationalStatusService.initialize(nasIpAddress.getAddress(), radiusSecret, impl); |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 329 | serverStatusAndStateMachineTimeoutExecutor = Executors.newScheduledThreadPool(STATE_MACHINE_THREADS, |
| 330 | groupedThreads("onos/aaa", "aaa-machine-%d", log)); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 331 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 332 | scheduledStatusServerChecker = serverStatusAndStateMachineTimeoutExecutor.scheduleAtFixedRate( |
| 333 | new ServerStatusChecker(), 0, |
| 334 | operationalStatusEventGenerationPeriodInSeconds, TimeUnit.SECONDS); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 335 | |
Jian Li | 13c6716 | 2015-12-09 13:20:34 -0800 | [diff] [blame] | 336 | log.info("Started"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Deactivate |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 340 | public void deactivate(ComponentContext context) { |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 341 | impl.withdrawIntercepts(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 342 | packetService.removeProcessor(processor); |
Deepa Vaddireddy | b9c24c6 | 2017-09-21 13:45:30 +0530 | [diff] [blame] | 343 | netCfgService.removeListener(cfgListener); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 344 | cfgService.unregisterProperties(getClass(), false); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 345 | StateMachine.unsetDelegate(delegate); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 346 | impl.deactivate(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 347 | deviceService.removeListener(deviceListener); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 348 | eventDispatcher.removeSink(AuthenticationEvent.class); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 349 | scheduledStatusServerChecker.cancel(true); |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 350 | serverStatusAndStateMachineTimeoutExecutor.shutdown(); |
| 351 | packetProcessorExecutor.shutdown(); |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 352 | authenticationsConsistentMap.removeListener(mapListener); |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 353 | |
Jian Li | 13c6716 | 2015-12-09 13:20:34 -0800 | [diff] [blame] | 354 | log.info("Stopped"); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 355 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 356 | @Modified |
| 357 | public void modified(ComponentContext context) { |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 358 | Dictionary<String, Object> properties = context.getProperties(); |
| 359 | |
Jonathan Hart | c41227c | 2020-01-28 16:56:49 -0800 | [diff] [blame] | 360 | String s = Tools.get(properties, "operationalStatusEventGenerationPeriodInSeconds"); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 361 | operationalStatusEventGenerationPeriodInSeconds = Strings.isNullOrEmpty(s) |
| 362 | ? OPERATIONAL_STATUS_SERVER_EVENT_GENERATION_DEFAULT |
| 363 | : Integer.parseInt(s.trim()); |
| 364 | |
| 365 | s = Tools.get(properties, "operationalStatusServerTimeoutInSeconds"); |
| 366 | operationalStatusServerTimeoutInSeconds = Strings.isNullOrEmpty(s) ? OPERATIONAL_STATUS_SERVER_TIMEOUT_DEFAULT |
| 367 | : Integer.parseInt(s.trim()); |
| 368 | |
| 369 | s = Tools.get(properties, "operationalStatusEvaluationMode"); |
| 370 | String newEvaluationModeString = Strings.isNullOrEmpty(s) ? STATUS_SERVER_MODE_DEFAULT : s.trim(); |
| 371 | |
| 372 | radiusOperationalStatusService |
| 373 | .setOperationalStatusServerTimeoutInMillis(operationalStatusServerTimeoutInSeconds * 1000); |
| 374 | RadiusOperationalStatusEvaluationMode newEvaluationMode = |
| 375 | RadiusOperationalStatusEvaluationMode.getValue(newEvaluationModeString); |
| 376 | if (newEvaluationMode != null) { |
| 377 | radiusOperationalStatusService.setRadiusOperationalStatusEvaluationMode(newEvaluationMode); |
| 378 | operationalStatusEvaluationMode = newEvaluationModeString; |
| 379 | } else { |
| 380 | properties.put("operationalStatusEvaluationMode", operationalStatusEvaluationMode); |
| 381 | } |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 382 | |
| 383 | s = Tools.get(properties, PACKET_PROCESSOR_THREADS); |
| 384 | int oldpacketProcessorThreads = packetProcessorThreads; |
| 385 | packetProcessorThreads = Strings.isNullOrEmpty(s) ? oldpacketProcessorThreads |
| 386 | : Integer.parseInt(s.trim()); |
| 387 | if (packetProcessorExecutor == null || oldpacketProcessorThreads != packetProcessorThreads) { |
| 388 | if (packetProcessorExecutor != null) { |
| 389 | packetProcessorExecutor.shutdown(); |
| 390 | } |
| 391 | packetProcessorExecutor = newFixedThreadPool(packetProcessorThreads, |
| 392 | groupedThreads("onos/aaa", "aaa-packet-%d", log)); |
| 393 | |
| 394 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Shubham Sharma | cf5e503 | 2019-11-26 11:09:21 +0000 | [diff] [blame] | 397 | protected void configureRadiusCommunication() { |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 398 | if (radiusConnectionType.toLowerCase().equals("socket")) { |
| 399 | impl = new SocketBasedRadiusCommunicator(appId, packetService, this); |
| 400 | } else { |
| 401 | impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService, |
| 402 | deviceService, subsService, pktCustomizer, this); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | private void configurePacketCustomizer() { |
| 407 | switch (customizer.toLowerCase()) { |
| 408 | case "sample": |
| 409 | pktCustomizer = new SamplePacketCustomizer(customInfo); |
| 410 | log.info("Created SamplePacketCustomizer"); |
| 411 | break; |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 412 | case "att": |
| 413 | pktCustomizer = new AttPacketCustomizer(customInfo); |
| 414 | log.info("Created AttPacketCustomizer"); |
| 415 | break; |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 416 | default: |
| 417 | pktCustomizer = new PacketCustomizer(customInfo); |
| 418 | log.info("Created default PacketCustomizer"); |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 423 | private void getConfiguredAaaServerAddress() { |
| 424 | try { |
| 425 | InetAddress address; |
| 426 | if (newCfg.radiusHostName() != null) { |
| 427 | address = InetAddress.getByName(newCfg.radiusHostName()); |
| 428 | } else { |
| 429 | address = newCfg.radiusIp(); |
| 430 | } |
| 431 | |
| 432 | configuredAaaServerAddress = address.getHostAddress(); |
| 433 | } catch (UnknownHostException uhe) { |
| 434 | log.warn("Unable to resolve host {}", newCfg.radiusHostName()); |
| 435 | } |
| 436 | } |
| 437 | |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 438 | private void checkReceivedPacketForValidValidator(RADIUS radiusPacket, byte[] requestAuthenticator) { |
| 439 | if (!checkResponseMessageAuthenticator(radiusSecret, radiusPacket, requestAuthenticator)) { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 440 | aaaStatisticsManager.getAaaStats().increaseInvalidValidatorsRx(); |
| 441 | } |
| 442 | } |
Vijaykumar Kushwaha | 056e4cd | 2019-08-06 13:16:25 +0000 | [diff] [blame] | 443 | |
| 444 | private boolean checkResponseMessageAuthenticator(String key, RADIUS radiusPacket, byte[] requestAuthenticator) { |
| 445 | byte[] newHash = new byte[16]; |
| 446 | Arrays.fill(newHash, (byte) 0); |
| 447 | byte[] messageAuthenticator = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH).getValue(); |
| 448 | byte[] authenticator = radiusPacket.getAuthenticator(); |
| 449 | radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, newHash); |
| 450 | radiusPacket.setAuthenticator(requestAuthenticator); |
| 451 | // Calculate the MD5 HMAC based on the message |
| 452 | try { |
| 453 | SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacMD5"); |
| 454 | Mac mac = Mac.getInstance("HmacMD5"); |
| 455 | mac.init(keySpec); |
| 456 | newHash = mac.doFinal(radiusPacket.serialize()); |
| 457 | } catch (Exception e) { |
| 458 | log.error("Failed to generate message authenticator: {}", e.getMessage()); |
| 459 | } |
| 460 | radiusPacket.updateAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuthenticator); |
| 461 | radiusPacket.setAuthenticator(authenticator); |
| 462 | // Compare the calculated Message-Authenticator with the one in the message |
| 463 | return Arrays.equals(newHash, messageAuthenticator); |
| 464 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 465 | public void checkForPacketFromUnknownServer(String hostAddress) { |
| 466 | if (!hostAddress.equals(configuredAaaServerAddress)) { |
Vijaykumar Kushwaha | fffd3d1 | 2019-08-01 11:09:59 +0000 | [diff] [blame] | 467 | getConfiguredAaaServerAddress(); |
| 468 | if (!hostAddress.equals(configuredAaaServerAddress)) { |
| 469 | aaaStatisticsManager.getAaaStats().incrementUnknownServerRx(); |
| 470 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 474 | /** |
| 475 | * Send RADIUS packet to the RADIUS server. |
| 476 | * |
| 477 | * @param radiusPacket RADIUS packet to be sent to server. |
| 478 | * @param inPkt Incoming EAPOL packet |
| 479 | */ |
| 480 | protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) { |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 481 | outPacketSet.add(radiusPacket.getIdentifier()); |
| 482 | aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(true); |
| 483 | aaaStatisticsManager.getAaaStats().increaseAccessRequestsTx(); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 484 | aaaStatisticsManager.putOutgoingIdentifierToMap(radiusPacket.getIdentifier()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 485 | impl.sendRadiusPacket(radiusPacket, inPkt); |
| 486 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 487 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 488 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 489 | * Handles RADIUS packets. |
| 490 | * |
| 491 | * @param radiusPacket RADIUS packet coming from the RADIUS server. |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 492 | */ |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 493 | public void handleRadiusPacket(RADIUS radiusPacket) { |
| 494 | packetProcessorExecutor.execute(() -> { |
| 495 | if (log.isTraceEnabled()) { |
| 496 | log.trace("Received RADIUS packet {}", radiusPacket); |
| 497 | } |
| 498 | if (radiusOperationalStatusService.isRadiusResponseForOperationalStatus(radiusPacket.getIdentifier())) { |
| 499 | radiusOperationalStatusService.handleRadiusPacketForOperationalStatus(radiusPacket); |
| 500 | return; |
| 501 | } |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 502 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 503 | RequestIdentifier identifier = RequestIdentifier.of(radiusPacket.getIdentifier()); |
| 504 | String sessionId = idManager.getSessionId(identifier); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 505 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 506 | if (sessionId == null) { |
| 507 | log.error("Invalid packet identifier {}, could not find corresponding " |
| 508 | + "state machine ... exiting", radiusPacket.getIdentifier()); |
| 509 | aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired(); |
| 510 | aaaStatisticsManager.getAaaStats().countDroppedResponsesRx(); |
| 511 | return; |
| 512 | } |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 513 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 514 | idManager.releaseIdentifier(identifier); |
| 515 | StateMachine stateMachine = stateMachines.get(sessionId); |
| 516 | if (stateMachine == null) { |
| 517 | log.error("Invalid packet identifier {}, could not find corresponding " |
| 518 | + "state machine ... exiting", radiusPacket.getIdentifier()); |
| 519 | aaaStatisticsManager.getAaaStats().incrementNumberOfSessionsExpired(); |
| 520 | aaaStatisticsManager.getAaaStats().countDroppedResponsesRx(); |
| 521 | return; |
| 522 | } |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 523 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 524 | //instance of StateMachine using the sessionId for updating machine stats |
| 525 | StateMachine machineStats = stateMachines.get(stateMachine.sessionId()); |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 526 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 527 | EAP eapPayload; |
| 528 | Ethernet eth; |
| 529 | checkReceivedPacketForValidValidator(radiusPacket, stateMachine.requestAuthenticator()); |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 530 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 531 | //increasing packets and octets received from server |
| 532 | machineStats.incrementTotalPacketsReceived(); |
| 533 | try { |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 534 | machineStats.incrementTotalOctetReceived(radiusPacket.decapsulateMessage().getLength()); |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 535 | } catch (DeserializationException e) { |
| 536 | log.error(e.getMessage()); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | if (outPacketSet.contains(radiusPacket.getIdentifier())) { |
| 541 | aaaStatisticsManager.getAaaStats().increaseOrDecreasePendingRequests(false); |
| 542 | outPacketSet.remove(new Byte(radiusPacket.getIdentifier())); |
| 543 | } |
| 544 | |
| 545 | switch (radiusPacket.getCode()) { |
| 546 | case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE: |
| 547 | log.debug("RADIUS packet: RADIUS_CODE_ACCESS_CHALLENGE"); |
| 548 | RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE); |
| 549 | byte[] challengeState = null; |
| 550 | if (radiusAttrState != null) { |
| 551 | challengeState = radiusAttrState.getValue(); |
| 552 | } |
| 553 | try { |
| 554 | eapPayload = radiusPacket.decapsulateMessage(); |
| 555 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
| 556 | MacAddress.valueOf(nasMacAddress), |
| 557 | stateMachine.vlanId(), |
| 558 | EAPOL.EAPOL_PACKET, |
| 559 | eapPayload, stateMachine.priorityCode()); |
| 560 | stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState); |
| 561 | } catch (DeserializationException e) { |
| 562 | log.error(e.getMessage()); |
| 563 | break; |
| 564 | } |
| 565 | log.debug("Send EAP challenge response to supplicant {}", |
| 566 | stateMachine.supplicantAddress().toString()); |
| 567 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), true); |
| 568 | aaaStatisticsManager.getAaaStats().increaseChallengeResponsesRx(); |
| 569 | outPacketSupp.add(eapPayload.getIdentifier()); |
| 570 | aaaStatisticsManager.getAaaStats().incrementPendingResSupp(); |
| 571 | //increasing packets send to server |
| 572 | machineStats.incrementTotalPacketsSent(); |
| 573 | machineStats.incrementTotalOctetSent(eapPayload.getLength()); |
| 574 | break; |
| 575 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: |
| 576 | log.debug("RADIUS packet: RADIUS_CODE_ACCESS_ACCEPT"); |
| 577 | //send an EAPOL - Success to the supplicant. |
| 578 | byte[] eapMessageSuccess = |
| 579 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); |
| 580 | try { |
| 581 | eapPayload = EAP.deserializer().deserialize( |
| 582 | eapMessageSuccess, 0, eapMessageSuccess.length); |
| 583 | } catch (DeserializationException e) { |
| 584 | log.error(e.getMessage()); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
| 589 | MacAddress.valueOf(nasMacAddress), |
| 590 | stateMachine.vlanId(), |
| 591 | EAPOL.EAPOL_PACKET, |
| 592 | eapPayload, stateMachine.priorityCode()); |
| 593 | log.info("Send EAP success message to supplicant {}", stateMachine.supplicantAddress().toString()); |
| 594 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false); |
| 595 | aaaStatisticsManager.getAaaStats().incrementEapolAuthSuccessTrans(); |
| 596 | |
| 597 | stateMachine.authorizeAccess(); |
| 598 | aaaStatisticsManager.getAaaStats().increaseAcceptResponsesRx(); |
| 599 | //increasing packets send to server |
| 600 | machineStats.incrementTotalPacketsSent(); |
| 601 | machineStats.incrementTotalOctetSent(eapPayload.getLength()); |
| 602 | break; |
| 603 | case RADIUS.RADIUS_CODE_ACCESS_REJECT: |
| 604 | log.debug("RADIUS packet: RADIUS_CODE_ACCESS_REJECT"); |
| 605 | //send an EAPOL - Failure to the supplicant. |
| 606 | byte[] eapMessageFailure; |
| 607 | eapPayload = new EAP(); |
| 608 | RADIUSAttribute radiusAttrEap = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE); |
| 609 | if (radiusAttrEap == null) { |
| 610 | eapPayload.setCode(EAP.FAILURE); |
| 611 | eapPayload.setIdentifier(stateMachine.challengeIdentifier()); |
| 612 | eapPayload.setLength(EAP.EAP_HDR_LEN_SUC_FAIL); |
| 613 | } else { |
| 614 | eapMessageFailure = radiusAttrEap.getValue(); |
| 615 | try { |
| 616 | eapPayload = EAP.deserializer().deserialize( |
| 617 | eapMessageFailure, 0, eapMessageFailure.length); |
| 618 | } catch (DeserializationException e) { |
| 619 | log.error(e.getMessage()); |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
| 624 | MacAddress.valueOf(nasMacAddress), |
| 625 | stateMachine.vlanId(), |
| 626 | EAPOL.EAPOL_PACKET, |
| 627 | eapPayload, stateMachine.priorityCode()); |
| 628 | log.warn("Send EAP failure message to supplicant {}", stateMachine.supplicantAddress().toString()); |
| 629 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false); |
| 630 | aaaStatisticsManager.getAaaStats().incrementEapolauthFailureTrans(); |
| 631 | |
| 632 | stateMachine.denyAccess(); |
| 633 | aaaStatisticsManager.getAaaStats().increaseRejectResponsesRx(); |
| 634 | //increasing packets send to server |
| 635 | machineStats.incrementTotalPacketsSent(); |
| 636 | machineStats.incrementTotalOctetSent(eapPayload.getLength()); |
| 637 | //pushing machine stats to kafka |
| 638 | AaaSupplicantMachineStats machineObj = aaaSupplicantStatsManager.getSupplicantStats(machineStats); |
| 639 | aaaSupplicantStatsManager.getMachineStatsDelegate() |
| 640 | .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, |
| 641 | machineObj)); |
| 642 | break; |
| 643 | default: |
| 644 | log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode()); |
| 645 | aaaStatisticsManager.getAaaStats().increaseUnknownTypeRx(); |
| 646 | //increasing packets received to server |
| 647 | machineStats.incrementTotalPacketsReceived(); |
| 648 | try { |
| 649 | machineStats.incrementTotalOctetReceived(radiusPacket.decapsulateMessage().getLength()); |
| 650 | } catch (DeserializationException e) { |
| 651 | log.error(e.getMessage()); |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | aaaStatisticsManager.getAaaStats().countDroppedResponsesRx(); |
| 656 | }); |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 659 | /** |
| 660 | * Send the ethernet packet to the supplicant. |
| 661 | * |
| 662 | * @param ethernetPkt the ethernet packet |
| 663 | * @param connectPoint the connect point to send out |
| 664 | */ |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 665 | private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint, boolean isChallengeResponse) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 666 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 667 | OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 668 | treatment, ByteBuffer.wrap(ethernetPkt.serialize())); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 669 | EAPOL eap = ((EAPOL) ethernetPkt.getPayload()); |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 670 | if (log.isTraceEnabled()) { |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 671 | log.trace("Sending eapol payload {} enclosed in {} to supplicant at {}", |
| 672 | eap, ethernetPkt, connectPoint); |
| 673 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 674 | packetService.emit(packet); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 675 | if (isChallengeResponse) { |
| 676 | aaaStatisticsManager.getAaaStats().incrementEapPktTxauthEap(); |
| 677 | } |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 678 | aaaStatisticsManager.getAaaStats().incrementEapolFramesTx(); |
| 679 | aaaStatisticsManager.getAaaStats().countReqEapFramesTx(); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 682 | @Override |
| 683 | public String toString() { |
| 684 | return ToStringBuilder.reflectionToString(this); |
| 685 | } |
| 686 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 687 | @Override |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 688 | public Iterable<AuthenticationRecord> getAuthenticationRecords() { |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 689 | return authentications.values(); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | @Override |
| 693 | public boolean removeAuthenticationStateByMac(MacAddress mac) { |
| 694 | |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 695 | Optional<AuthenticationRecord> r = authentications.values().stream() |
| 696 | .filter(v -> v.supplicantAddress().equals(mac)) |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 697 | .findFirst(); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 698 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 699 | if (r.isEmpty()) { |
| 700 | return false; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Hardik Windlass | d0b4969 | 2020-02-26 18:17:14 +0000 | [diff] [blame] | 703 | AuthenticationRecord removed = |
| 704 | authentications.remove(r.get().supplicantConnectPoint()); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 705 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 706 | return removed != null; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 709 | StateMachine getStateMachine(String sessionId) { |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 710 | return stateMachines.get(sessionId); |
| 711 | } |
| 712 | |
| 713 | private String sessionId(ConnectPoint cp) { |
| 714 | return cp.deviceId().toString() + cp.port().toString(); |
| 715 | } |
| 716 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 717 | // our handler defined as a private inner class |
| 718 | |
| 719 | /** |
| 720 | * Packet processor responsible for forwarding packets along their paths. |
| 721 | */ |
| 722 | private class ReactivePacketProcessor implements PacketProcessor { |
| 723 | @Override |
| 724 | public void process(PacketContext context) { |
| 725 | |
| 726 | // Extract the original Ethernet frame from the packet information |
| 727 | InboundPacket pkt = context.inPacket(); |
| 728 | Ethernet ethPkt = pkt.parsed(); |
| 729 | if (ethPkt == null) { |
| 730 | return; |
| 731 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 732 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 733 | // identify if incoming packet comes from supplicant (EAP) or RADIUS |
| 734 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { |
| 735 | case EAPOL: |
| 736 | handleSupplicantPacket(context.inPacket()); |
| 737 | break; |
| 738 | default: |
| 739 | // any other packets let the specific implementation handle |
| 740 | impl.handlePacketFromServer(context); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 744 | /** |
| 745 | * Creates and initializes common fields of a RADIUS packet. |
| 746 | * |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 747 | * @param stateMachine state machine for the request |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 748 | * @param eapPacket EAP packet |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 749 | * @return RADIUS packet |
| 750 | */ |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 751 | private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) { |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 752 | RADIUS radiusPayload = |
| 753 | new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
| 754 | eapPacket.getIdentifier()); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 755 | |
| 756 | // set Request Authenticator in StateMachine |
| 757 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
| 758 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 759 | radiusPayload.setIdentifier(identifier); |
| 760 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 761 | stateMachine.username()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 762 | |
| 763 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 764 | AaaManager.this.nasIpAddress.getAddress()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 765 | |
| 766 | radiusPayload.encapsulateMessage(eapPacket); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 767 | |
| 768 | return radiusPayload; |
| 769 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 770 | |
| 771 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 772 | * Handles PAE packets (supplicant). |
| 773 | * |
| 774 | * @param inPacket Ethernet packet coming from the supplicant |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 775 | */ |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 776 | private void handleSupplicantPacket(InboundPacket inPacket) { |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 777 | Ethernet ethPkt = inPacket.parsed(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 778 | // Where does it come from? |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 779 | MacAddress srcMac = ethPkt.getSourceMAC(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 780 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 781 | DeviceId deviceId = inPacket.receivedFrom().deviceId(); |
| 782 | PortNumber portNumber = inPacket.receivedFrom().port(); |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 783 | String sessionId = sessionId(inPacket.receivedFrom()); |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 784 | EAPOL eapol = (EAPOL) ethPkt.getPayload(); |
| 785 | if (log.isTraceEnabled()) { |
| 786 | log.trace("Received EAPOL packet {} in enclosing packet {} from " |
| 787 | + "dev/port: {}/{}", eapol, ethPkt, deviceId, |
| 788 | portNumber); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 789 | } |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 790 | |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 791 | short pktlen = eapol.getPacketLength(); |
| 792 | byte[] eapPayLoadBuffer = eapol.serialize(); |
| 793 | int len = eapPayLoadBuffer.length; |
| 794 | if (len != (HEADER_LENGTH + pktlen)) { |
| 795 | aaaStatisticsManager.getAaaStats().incrementInvalidBodyLength(); |
| 796 | return; |
| 797 | } |
| 798 | if (!VALID_EAPOL_TYPE.contains(eapol.getEapolType())) { |
| 799 | aaaStatisticsManager.getAaaStats().incrementInvalidPktType(); |
| 800 | return; |
| 801 | } |
| 802 | if (pktlen >= 0 && ethPkt.getEtherType() == EthType.EtherType.EAPOL.ethType().toShort()) { |
| 803 | aaaStatisticsManager.getAaaStats().incrementValidEapolFramesRx(); |
| 804 | } |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 805 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame^] | 806 | StateMachine stateMachine = stateMachines.computeIfAbsent(sessionId, id -> |
| 807 | new StateMachine(id, serverStatusAndStateMachineTimeoutExecutor)); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 808 | stateMachine.setEapolTypeVal(eapol.getEapolType()); |
Saurav Das | 987441a | 2018-09-18 16:33:47 -0700 | [diff] [blame] | 809 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 810 | switch (eapol.getEapolType()) { |
| 811 | case EAPOL.EAPOL_START: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 812 | log.debug("EAP packet: EAPOL_START"); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 813 | stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 814 | stateMachine.setSupplicantAddress(srcMac); |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 815 | stateMachine.start(); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 816 | |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 817 | aaaStatisticsManager.getAaaStats().incrementEapolStartReqTrans(); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 818 | //send an EAP Request/Identify to the supplicant |
| 819 | EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 820 | if (ethPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) { |
| 821 | stateMachine.setPriorityCode(ethPkt.getPriorityCode()); |
| 822 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 823 | Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress), |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 824 | ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 825 | eapPayload, stateMachine.priorityCode()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 826 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 827 | stateMachine.setVlanId(ethPkt.getVlanID()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 828 | log.debug("Getting EAP identity from supplicant {}", stateMachine.supplicantAddress().toString()); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 829 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint(), false); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 830 | aaaStatisticsManager.getAaaStats().incrementRequestIdFramesTx(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 831 | |
| 832 | break; |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 833 | case EAPOL.EAPOL_LOGOFF: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 834 | log.debug("EAP packet: EAPOL_LOGOFF"); |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 835 | //posting the machine stat data for current supplicant device. |
| 836 | if (stateMachine.getSessionTerminateReason() == null || |
| 837 | stateMachine.getSessionTerminateReason().equals("")) { |
| 838 | stateMachine.setSessionTerminateReason( |
| 839 | StateMachine.SessionTerminationReasons.SUPPLICANT_LOGOFF.getReason()); |
| 840 | } |
| 841 | AaaSupplicantMachineStats obj = aaaSupplicantStatsManager.getSupplicantStats(stateMachine); |
| 842 | aaaSupplicantStatsManager.getMachineStatsDelegate() |
| 843 | .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj)); |
Ray Milkey | b34b496 | 2016-01-04 10:24:43 -0800 | [diff] [blame] | 844 | if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) { |
| 845 | stateMachine.logoff(); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 846 | aaaStatisticsManager.getAaaStats().incrementEapolLogoffRx(); |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 847 | } |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 848 | if (stateMachine.state() == StateMachine.STATE_IDLE) { |
| 849 | aaaStatisticsManager.getAaaStats().incrementAuthStateIdle(); |
| 850 | } |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 851 | |
| 852 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 853 | case EAPOL.EAPOL_PACKET: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 854 | RADIUS radiusPayload; |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 855 | // check if this is a Response/Identify or a Response/TLS |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 856 | EAP eapPacket = (EAP) eapol.getPayload(); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 857 | Byte identifier = new Byte(eapPacket.getIdentifier()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 858 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 859 | // get identifier for request and store mapping to session ID |
| 860 | RequestIdentifier radiusIdentifier = idManager.getNewIdentifier(sessionId); |
| 861 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 862 | byte dataType = eapPacket.getDataType(); |
| 863 | switch (dataType) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 864 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 865 | case EAP.ATTR_IDENTITY: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 866 | log.debug("EAP packet: EAPOL_PACKET ATTR_IDENTITY"); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 867 | //Setting the time of this response from RG, only when its not a re-transmission. |
| 868 | if (stateMachine.getLastPacketReceivedTime() == 0) { |
| 869 | stateMachine.setLastPacketReceivedTime(System.currentTimeMillis()); |
| 870 | } |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 871 | // request id access to RADIUS |
| 872 | stateMachine.setUsername(eapPacket.getData()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 873 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 874 | radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 875 | radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 876 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 877 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 878 | sendRadiusPacket(radiusPayload, inPacket); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 879 | stateMachine.setWaitingForRadiusResponse(true); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 880 | aaaStatisticsManager.getAaaStats().incrementEapolAtrrIdentity(); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 881 | // change the state to "PENDING" |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 882 | if (stateMachine.state() == StateMachine.STATE_PENDING) { |
| 883 | aaaStatisticsManager.getAaaStats().increaseRequestReTx(); |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 884 | stateMachine.incrementTotalPacketsSent(); |
| 885 | stateMachine.incrementTotalOctetSent(eapol.getPacketLength()); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 886 | } |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 887 | stateMachine.requestAccess(); |
| 888 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 889 | case EAP.ATTR_MD5: |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 890 | stateMachine.setLastPacketReceivedTime(System.currentTimeMillis()); |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 891 | log.debug("EAP packet: EAPOL_PACKET ATTR_MD5"); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 892 | // verify if the EAP identifier corresponds to the |
| 893 | // challenge identifier from the client state |
| 894 | // machine. |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 895 | if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 896 | //send the RADIUS challenge response |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 897 | radiusPayload = getRadiusPayload(stateMachine, |
| 898 | radiusIdentifier.identifier(), eapPacket); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 899 | radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 900 | |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 901 | if (stateMachine.challengeState() != null) { |
| 902 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 903 | stateMachine.challengeState()); |
| 904 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 905 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 906 | if (outPacketSupp.contains(eapPacket.getIdentifier())) { |
| 907 | aaaStatisticsManager.getAaaStats().decrementPendingResSupp(); |
| 908 | outPacketSupp.remove(identifier); |
| 909 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 910 | sendRadiusPacket(radiusPayload, inPacket); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 911 | stateMachine.setWaitingForRadiusResponse(true); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 912 | aaaStatisticsManager.getAaaStats().incrementEapolMd5RspChall(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 913 | } |
| 914 | break; |
| 915 | case EAP.ATTR_TLS: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 916 | log.debug("EAP packet: EAPOL_PACKET ATTR_TLS"); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 917 | // request id access to RADIUS |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 918 | radiusPayload = getRadiusPayload(stateMachine, radiusIdentifier.identifier(), eapPacket); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 919 | radiusPayload = pktCustomizer.customizePacket(radiusPayload, inPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 920 | |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 921 | if (stateMachine.challengeState() != null) { |
| 922 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 923 | stateMachine.challengeState()); |
| 924 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 925 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 926 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 927 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Shubham Sharma | 3c8c702 | 2019-09-13 10:39:47 +0000 | [diff] [blame] | 928 | if (outPacketSupp.contains(eapPacket.getIdentifier())) { |
| 929 | aaaStatisticsManager.getAaaStats().decrementPendingResSupp(); |
| 930 | outPacketSupp.remove(identifier); |
| 931 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 932 | sendRadiusPacket(radiusPayload, inPacket); |
Shubham Sharma | 1e43c56 | 2019-06-19 14:18:12 +0000 | [diff] [blame] | 933 | stateMachine.setWaitingForRadiusResponse(true); |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 934 | aaaStatisticsManager.getAaaStats().incrementEapolTlsRespChall(); |
Ray Milkey | 5493b51 | 2015-10-21 12:13:49 -0700 | [diff] [blame] | 935 | |
Ray Milkey | f3790b8 | 2015-10-21 16:28:08 -0700 | [diff] [blame] | 936 | if (stateMachine.state() != StateMachine.STATE_PENDING) { |
| 937 | stateMachine.requestAccess(); |
| 938 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 939 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 940 | break; |
| 941 | default: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 942 | log.warn("Unknown EAP packet type"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 943 | return; |
| 944 | } |
| 945 | break; |
| 946 | default: |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 947 | log.debug("Skipping EAPOL message {}", eapol.getEapolType()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 948 | } |
Shubham Sharma | 1f19358 | 2019-07-11 12:12:41 +0000 | [diff] [blame] | 949 | aaaStatisticsManager.getAaaStats().countTransRespNotNak(); |
| 950 | aaaStatisticsManager.getAaaStats().countEapolResIdentityMsgTrans(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 951 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 954 | /** |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 955 | * Delegate allowing the StateMachine to notify us of events. |
| 956 | */ |
| 957 | private class InternalStateMachineDelegate implements StateMachineDelegate { |
| 958 | |
| 959 | @Override |
| 960 | public void notify(AuthenticationEvent authenticationEvent) { |
| 961 | log.info("Auth event {} for {}", |
| 962 | authenticationEvent.type(), authenticationEvent.subject()); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 963 | |
| 964 | if (authenticationEvent.type() == AuthenticationEvent.Type.TIMEOUT) { |
| 965 | handleStateMachineTimeout(authenticationEvent.subject()); |
| 966 | } |
| 967 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 968 | AuthenticationRecord record = authenticationEvent.authenticationRecord(); |
| 969 | if (record == null) { |
| 970 | authentications.remove(authenticationEvent.subject()); |
| 971 | } else { |
| 972 | authentications.put(authenticationEvent.subject(), record); |
| 973 | } |
| 974 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 975 | post(authenticationEvent); |
| 976 | } |
| 977 | } |
| 978 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 979 | private void handleStateMachineTimeout(ConnectPoint supplicantConnectPoint) { |
| 980 | StateMachine stateMachine = stateMachines.remove(sessionId(supplicantConnectPoint)); |
Andrea Campanella | e66466a | 2020-02-03 14:05:45 +0000 | [diff] [blame] | 981 | //pushing captured machine stats to kafka |
| 982 | stateMachine.setSessionTerminateReason("Time out"); |
| 983 | AaaSupplicantMachineStats obj = aaaSupplicantStatsManager |
| 984 | .getSupplicantStats(stateMachine); |
| 985 | aaaSupplicantStatsManager.getMachineStatsDelegate() |
| 986 | .notify(new AaaMachineStatisticsEvent( |
| 987 | AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj)); |
| 988 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 989 | if (stateMachine.state() == StateMachine.STATE_PENDING && stateMachine.isWaitingForRadiusResponse()) { |
| 990 | aaaStatisticsManager.getAaaStats().increaseTimedOutPackets(); |
| 991 | } |
| 992 | |
| 993 | StateMachine.deleteStateMachineMapping(stateMachine); |
| 994 | } |
| 995 | |
Jonathan Hart | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame] | 996 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 997 | * Configuration Listener, handles change in configuration. |
| 998 | */ |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 999 | private class InternalConfigListener implements NetworkConfigListener { |
| 1000 | |
| 1001 | /** |
Ray Milkey | c9e8dcc | 2015-12-30 10:31:32 -0800 | [diff] [blame] | 1002 | * Reconfigures the AAA application according to the |
| 1003 | * configuration parameters passed. |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1004 | * |
| 1005 | * @param cfg configuration object |
| 1006 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 1007 | private void reconfigureNetwork(AaaConfig cfg) { |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 1008 | log.info("Reconfiguring AaaConfig from config: {}", cfg); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1009 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1010 | if (cfg == null) { |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 1011 | newCfg = new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1012 | } else { |
| 1013 | newCfg = cfg; |
| 1014 | } |
| 1015 | if (newCfg.nasIp() != null) { |
| 1016 | nasIpAddress = newCfg.nasIp(); |
| 1017 | } |
| 1018 | if (newCfg.radiusIp() != null) { |
| 1019 | radiusIpAddress = newCfg.radiusIp(); |
| 1020 | } |
| 1021 | if (newCfg.radiusMac() != null) { |
| 1022 | radiusMacAddress = newCfg.radiusMac(); |
| 1023 | } |
| 1024 | if (newCfg.nasMac() != null) { |
| 1025 | nasMacAddress = newCfg.nasMac(); |
| 1026 | } |
| 1027 | if (newCfg.radiusSecret() != null) { |
| 1028 | radiusSecret = newCfg.radiusSecret(); |
| 1029 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1030 | |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 1031 | boolean reconfigureCustomizer = false; |
| 1032 | if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) { |
| 1033 | customizer = newCfg.radiusPktCustomizer(); |
| 1034 | configurePacketCustomizer(); |
| 1035 | reconfigureCustomizer = true; |
| 1036 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1037 | |
Deepa Vaddireddy | e0e1072 | 2017-09-27 05:00:10 +0530 | [diff] [blame] | 1038 | if (radiusConnectionType == null |
| 1039 | || reconfigureCustomizer |
| 1040 | || !radiusConnectionType.equals(newCfg.radiusConnectionType())) { |
| 1041 | radiusConnectionType = newCfg.radiusConnectionType(); |
| 1042 | if (impl != null) { |
| 1043 | impl.withdrawIntercepts(); |
| 1044 | impl.clearLocalState(); |
| 1045 | } |
| 1046 | configureRadiusCommunication(); |
| 1047 | impl.initializeLocalState(newCfg); |
| 1048 | impl.requestIntercepts(); |
| 1049 | } else if (impl != null) { |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1050 | impl.clearLocalState(); |
| 1051 | impl.initializeLocalState(newCfg); |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 1052 | } |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | @Override |
| 1056 | public void event(NetworkConfigEvent event) { |
| 1057 | |
| 1058 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 1059 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 1060 | event.configClass().equals(AaaConfig.class)) { |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1061 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 1062 | AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1063 | reconfigureNetwork(cfg); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1064 | |
Matt Jeanneret | 2ff1a78 | 2018-06-13 15:24:25 -0400 | [diff] [blame] | 1065 | log.info("Reconfigured: {}", cfg.toString()); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | } |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1069 | |
Jonathan Hart | 9d1ce80 | 2020-01-28 10:45:08 -0800 | [diff] [blame] | 1070 | private class InternalMapEventListener implements MapEventListener<ConnectPoint, AuthenticationRecord> { |
| 1071 | @Override |
| 1072 | public void event(MapEvent<ConnectPoint, AuthenticationRecord> event) { |
| 1073 | if (event.type() == MapEvent.Type.REMOVE) { |
| 1074 | // remove local state machine if user has requested remove |
| 1075 | StateMachine sm = stateMachines.remove(sessionId(event.key())); |
| 1076 | if (sm != null) { |
| 1077 | sm.stop(); |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1083 | private class InternalDeviceListener implements DeviceListener { |
| 1084 | @Override |
| 1085 | public void event(DeviceEvent event) { |
Andrea Campanella | bba3367 | 2020-02-11 14:03:01 +0100 | [diff] [blame] | 1086 | DeviceId deviceId = event.subject().id(); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1087 | switch (event.type()) { |
| 1088 | case PORT_REMOVED: |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1089 | PortNumber portNumber = event.port().number(); |
Andrea Campanella | bba3367 | 2020-02-11 14:03:01 +0100 | [diff] [blame] | 1090 | String sessionId = deviceId.toString() + portNumber.toString(); |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1091 | log.debug("Received PORT_REMOVED event. Clearing AAA Session with Id {}", sessionId); |
Hardik Windlass | 75c6771 | 2020-02-21 10:57:20 +0000 | [diff] [blame] | 1092 | |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1093 | flushStateMachineSession(sessionId, |
| 1094 | StateMachine.SessionTerminationReasons.PORT_REMOVED.getReason()); |
Kartikey Dubey | adeb26e | 2019-10-01 12:18:35 +0000 | [diff] [blame] | 1095 | |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1096 | break; |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1097 | case DEVICE_REMOVED: |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1098 | log.debug("Received DEVICE_REMOVED event for {}", deviceId); |
Andrea Campanella | bba3367 | 2020-02-11 14:03:01 +0100 | [diff] [blame] | 1099 | clearAllSessionStateForDevice(deviceId); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1100 | break; |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1101 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1102 | default: |
| 1103 | return; |
| 1104 | } |
| 1105 | } |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1106 | |
Andrea Campanella | bba3367 | 2020-02-11 14:03:01 +0100 | [diff] [blame] | 1107 | private void clearAllSessionStateForDevice(DeviceId deviceId) { |
| 1108 | Set<String> associatedSessions = Sets.newHashSet(); |
| 1109 | for (Entry<String, StateMachine> stateMachineEntry : stateMachines.entrySet()) { |
| 1110 | ConnectPoint cp = stateMachineEntry.getValue().supplicantConnectpoint(); |
| 1111 | if (cp != null && cp.deviceId().toString().equals(deviceId.toString())) { |
| 1112 | associatedSessions.add(stateMachineEntry.getKey()); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | for (String session : associatedSessions) { |
| 1117 | log.debug("Clearing AAA Session {} associated with Removed Device", session); |
| 1118 | flushStateMachineSession(session, |
| 1119 | StateMachine.SessionTerminationReasons.DEVICE_REMOVED.getReason()); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1123 | private void flushStateMachineSession(String sessionId, String terminationReason) { |
| 1124 | StateMachine stateMachine = stateMachines.get(sessionId); |
Girish Kumar | e5a1aa9 | 2020-02-14 14:08:54 +0000 | [diff] [blame] | 1125 | if (stateMachine == null) { |
| 1126 | // No active AAA sessions for this UNI port |
| 1127 | log.debug("No Active AAA Session found with Id {}", sessionId); |
| 1128 | return; |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Hardik Windlass | 9a09b83 | 2020-02-25 11:45:35 +0000 | [diff] [blame] | 1131 | authentications.remove(stateMachine.supplicantConnectpoint()); |
Girish Kumar | e5a1aa9 | 2020-02-14 14:08:54 +0000 | [diff] [blame] | 1132 | stateMachine.setSessionTerminateReason(terminationReason); |
| 1133 | |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1134 | //pushing captured machine stats to kafka |
| 1135 | AaaSupplicantMachineStats obj = aaaSupplicantStatsManager.getSupplicantStats(stateMachine); |
| 1136 | aaaSupplicantStatsManager.getMachineStatsDelegate() |
| 1137 | .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj)); |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1138 | StateMachine removed = stateMachines.remove(sessionId); |
Shubham Sharma | 8d7a982 | 2020-01-28 10:04:01 +0000 | [diff] [blame] | 1139 | |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1140 | if (removed != null) { |
| 1141 | StateMachine.deleteStateMachineMapping(removed); |
| 1142 | } |
| 1143 | } |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 1144 | } |
Girish Kumar | 064084c | 2020-02-04 08:32:46 +0000 | [diff] [blame] | 1145 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 1146 | private class ServerStatusChecker implements Runnable { |
| 1147 | @Override |
| 1148 | public void run() { |
Matteo Scandolo | 3498bc0 | 2020-01-08 09:54:12 -0800 | [diff] [blame] | 1149 | log.debug("Notifying RadiusOperationalStatusEvent"); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 1150 | radiusOperationalStatusService.checkServerOperationalStatus(); |
Matteo Scandolo | 3498bc0 | 2020-01-08 09:54:12 -0800 | [diff] [blame] | 1151 | log.trace("--POSTING--" + radiusOperationalStatusService.getRadiusServerOperationalStatus()); |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 1152 | radiusOperationalStatusService.getRadiusOprStDelegate() |
| 1153 | .notify(new RadiusOperationalStatusEvent( |
| 1154 | RadiusOperationalStatusEvent.Type.RADIUS_OPERATIONAL_STATUS, |
| 1155 | radiusOperationalStatusService. |
| 1156 | getRadiusServerOperationalStatus())); |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 1157 | } |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 1158 | |
| 1159 | } |
Shubham Sharma | 544ffa2 | 2020-02-13 06:41:02 +0000 | [diff] [blame] | 1160 | |
| 1161 | @Override |
| 1162 | public AaaSupplicantMachineStats getSupplicantMachineStats(String sessionId) { |
| 1163 | StateMachine aaaSupplicantMachine = stateMachines.get(sessionId); |
| 1164 | if (aaaSupplicantMachine != null) { |
| 1165 | return aaaSupplicantStatsManager.getSupplicantStats(aaaSupplicantMachine); |
| 1166 | } else { |
| 1167 | return null; |
| 1168 | } |
| 1169 | } |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 1170 | } |