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