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