Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 AT&T Foundry |
| 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 | */ |
| 16 | package org.onosproject.aaa; |
| 17 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 18 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 24 | import org.onlab.packet.DeserializationException; |
| 25 | import org.onlab.packet.EAP; |
| 26 | import org.onlab.packet.EAPOL; |
| 27 | import org.onlab.packet.EthType; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 28 | import org.onlab.packet.Ethernet; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 30 | import org.onlab.packet.RADIUS; |
| 31 | import org.onlab.packet.RADIUSAttribute; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 37 | import org.onosproject.net.config.ConfigFactory; |
| 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
| 40 | import org.onosproject.net.config.NetworkConfigRegistry; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.TrafficSelector; |
| 44 | import org.onosproject.net.flow.TrafficTreatment; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 45 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 46 | import org.onosproject.net.packet.InboundPacket; |
| 47 | import org.onosproject.net.packet.OutboundPacket; |
| 48 | import org.onosproject.net.packet.PacketContext; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 49 | import org.onosproject.net.packet.PacketProcessor; |
| 50 | import org.onosproject.net.packet.PacketService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 51 | import org.onosproject.xosintegration.VoltTenantService; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 54 | import java.io.IOException; |
| 55 | import java.net.DatagramPacket; |
| 56 | import java.net.DatagramSocket; |
| 57 | import java.net.InetAddress; |
| 58 | import java.nio.ByteBuffer; |
| 59 | import java.util.concurrent.ExecutorService; |
| 60 | import java.util.concurrent.Executors; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 61 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 62 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 63 | import static org.onosproject.net.packet.PacketPriority.CONTROL; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 64 | import static org.slf4j.LoggerFactory.getLogger; |
| 65 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 66 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 67 | * AAA application for ONOS. |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 68 | */ |
| 69 | @Component(immediate = true) |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 70 | public class AaaManager { |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 71 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 72 | // for verbose output |
| 73 | private final Logger log = getLogger(getClass()); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 74 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 75 | // a list of our dependencies : |
| 76 | // to register with ONOS as an application - described next |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected CoreService coreService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 79 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 80 | // to receive Packet-in events that we'll respond to |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected PacketService packetService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 83 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected VoltTenantService voltTenantService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 88 | protected NetworkConfigRegistry netCfgService; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 89 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 90 | // Parsed RADIUS server addresses |
| 91 | protected InetAddress radiusIpAddress; |
| 92 | protected String radiusMacAddress; |
| 93 | |
| 94 | // NAS IP address |
| 95 | protected InetAddress nasIpAddress; |
| 96 | protected String nasMacAddress; |
| 97 | |
| 98 | // RADIUS server secret |
| 99 | protected String radiusSecret; |
| 100 | |
| 101 | // ID of RADIUS switch |
| 102 | protected String radiusSwitch; |
| 103 | |
| 104 | // RADIUS port number |
| 105 | protected long radiusPort; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 106 | |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 107 | // RADIUS server TCP port number |
| 108 | protected short radiusServerPort; |
| 109 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 110 | // our application-specific event handler |
| 111 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 112 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 113 | // our unique identifier |
| 114 | private ApplicationId appId; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 115 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 116 | // Socket used for UDP communications with RADIUS server |
| 117 | private DatagramSocket radiusSocket; |
| 118 | |
| 119 | // Executor for RADIUS communication thread |
| 120 | private ExecutorService executor; |
| 121 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 122 | // Configuration properties factory |
| 123 | private final ConfigFactory factory = |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 124 | new ConfigFactory<ApplicationId, AaaConfig>(APP_SUBJECT_FACTORY, |
| 125 | AaaConfig.class, |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 126 | "AAA") { |
| 127 | @Override |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 128 | public AaaConfig createConfig() { |
| 129 | return new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 130 | } |
| 131 | }; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 132 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 133 | // Listener for config changes |
| 134 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 135 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Builds an EAPOL packet based on the given parameters. |
| 138 | * |
| 139 | * @param dstMac destination MAC address |
| 140 | * @param srcMac source MAC address |
| 141 | * @param vlan vlan identifier |
| 142 | * @param eapolType EAPOL type |
| 143 | * @param eap EAP payload |
| 144 | * @return Ethernet frame |
| 145 | */ |
| 146 | private static Ethernet buildEapolResponse(MacAddress dstMac, MacAddress srcMac, |
| 147 | short vlan, byte eapolType, EAP eap) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 148 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 149 | Ethernet eth = new Ethernet(); |
| 150 | eth.setDestinationMACAddress(dstMac.toBytes()); |
| 151 | eth.setSourceMACAddress(srcMac.toBytes()); |
| 152 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 153 | if (vlan != Ethernet.VLAN_UNTAGGED) { |
| 154 | eth.setVlanID(vlan); |
| 155 | } |
| 156 | //eapol header |
| 157 | EAPOL eapol = new EAPOL(); |
| 158 | eapol.setEapolType(eapolType); |
| 159 | eapol.setPacketLength(eap.getLength()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 160 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 161 | //eap part |
| 162 | eapol.setPayload(eap); |
| 163 | |
| 164 | eth.setPayload(eapol); |
| 165 | eth.setPad(true); |
| 166 | return eth; |
| 167 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 168 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 169 | @Activate |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 170 | public void activate() { |
| 171 | netCfgService.addListener(cfgListener); |
| 172 | netCfgService.registerConfigFactory(factory); |
| 173 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 174 | // "org.onosproject.aaa" is the FQDN of our app |
| 175 | appId = coreService.registerApplication("org.onosproject.aaa"); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 176 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 177 | cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class)); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 178 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 179 | // register our event handler |
Brian O'Connor | d9c7da0 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 180 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 181 | requestIntercepts(); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 182 | |
| 183 | StateMachine.initializeMaps(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 184 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 185 | try { |
| 186 | radiusSocket = new DatagramSocket(radiusServerPort); |
| 187 | } catch (Exception ex) { |
| 188 | log.error("Can't open RADIUS socket", ex); |
| 189 | } |
| 190 | |
| 191 | executor = Executors.newSingleThreadExecutor( |
| 192 | new ThreadFactoryBuilder() |
| 193 | .setNameFormat("AAA-radius-%d").build()); |
| 194 | executor.execute(radiusListener); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Deactivate |
| 198 | public void deactivate() { |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 199 | appId = coreService.registerApplication("org.onosproject.aaa"); |
| 200 | withdrawIntercepts(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 201 | // de-register and null our handler |
| 202 | packetService.removeProcessor(processor); |
| 203 | processor = null; |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 204 | StateMachine.destroyMaps(); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 205 | radiusSocket.close(); |
| 206 | executor.shutdownNow(); |
| 207 | } |
| 208 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 209 | protected void sendRadiusPacket(RADIUS radiusPacket) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 210 | |
| 211 | try { |
| 212 | final byte[] data = radiusPacket.serialize(); |
| 213 | final DatagramSocket socket = radiusSocket; |
| 214 | |
| 215 | DatagramPacket packet = |
| 216 | new DatagramPacket(data, data.length, |
| 217 | radiusIpAddress, radiusServerPort); |
| 218 | |
| 219 | socket.send(packet); |
| 220 | } catch (IOException e) { |
| 221 | log.info("Cannot send packet to RADIUS server", e); |
| 222 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 225 | /** |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 226 | * Request packet in via PacketService. |
| 227 | */ |
| 228 | private void requestIntercepts() { |
| 229 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 230 | selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 231 | packetService.requestPackets(selector.build(), |
| 232 | CONTROL, appId); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Cancel request for packet in via PacketService. |
| 237 | */ |
| 238 | private void withdrawIntercepts() { |
| 239 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 240 | selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 241 | packetService.cancelPackets(selector.build(), CONTROL, appId); |
| 242 | } |
| 243 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Send the ethernet packet to the supplicant. |
| 246 | * |
| 247 | * @param ethernetPkt the ethernet packet |
| 248 | * @param connectPoint the connect point to send out |
| 249 | */ |
| 250 | private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) { |
| 251 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 252 | OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 253 | treatment, ByteBuffer.wrap(ethernetPkt.serialize())); |
| 254 | packetService.emit(packet); |
| 255 | } |
| 256 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 257 | // our handler defined as a private inner class |
| 258 | |
| 259 | /** |
| 260 | * Packet processor responsible for forwarding packets along their paths. |
| 261 | */ |
| 262 | private class ReactivePacketProcessor implements PacketProcessor { |
| 263 | @Override |
| 264 | public void process(PacketContext context) { |
| 265 | |
| 266 | // Extract the original Ethernet frame from the packet information |
| 267 | InboundPacket pkt = context.inPacket(); |
| 268 | Ethernet ethPkt = pkt.parsed(); |
| 269 | if (ethPkt == null) { |
| 270 | return; |
| 271 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 272 | try { |
| 273 | // identify if incoming packet comes from supplicant (EAP) or RADIUS |
| 274 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { |
| 275 | case EAPOL: |
| 276 | handleSupplicantPacket(context.inPacket()); |
| 277 | break; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 278 | default: |
| 279 | log.trace("Skipping Ethernet packet type {}", |
| 280 | EthType.EtherType.lookup(ethPkt.getEtherType())); |
| 281 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 282 | } catch (StateMachineException e) { |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 283 | log.warn("Unable to process RADIUS packet:", e); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Creates and initializes common fields of a RADIUS packet. |
| 289 | * |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 290 | * @param stateMachine state machine for the request |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 291 | * @param eapPacket EAP packet |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 292 | * @return RADIUS packet |
| 293 | */ |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 294 | private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) { |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 295 | RADIUS radiusPayload = |
| 296 | new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
| 297 | eapPacket.getIdentifier()); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 298 | |
| 299 | // set Request Authenticator in StateMachine |
| 300 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
| 301 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 302 | radiusPayload.setIdentifier(identifier); |
| 303 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 304 | stateMachine.username()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 305 | |
| 306 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 307 | AaaManager.this.nasIpAddress.getAddress()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 308 | |
| 309 | radiusPayload.encapsulateMessage(eapPacket); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 310 | |
| 311 | return radiusPayload; |
| 312 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 315 | * Handles PAE packets (supplicant). |
| 316 | * |
| 317 | * @param inPacket Ethernet packet coming from the supplicant |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 318 | */ |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 319 | private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException { |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 320 | Ethernet ethPkt = inPacket.parsed(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 321 | // Where does it come from? |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 322 | MacAddress srcMac = ethPkt.getSourceMAC(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 323 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 324 | DeviceId deviceId = inPacket.receivedFrom().deviceId(); |
| 325 | PortNumber portNumber = inPacket.receivedFrom().port(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 326 | String sessionId = deviceId.toString() + portNumber.toString(); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 327 | StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId); |
| 328 | if (stateMachine == null) { |
| 329 | stateMachine = new StateMachine(sessionId, voltTenantService); |
| 330 | } |
| 331 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 332 | |
| 333 | EAPOL eapol = (EAPOL) ethPkt.getPayload(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 334 | |
| 335 | switch (eapol.getEapolType()) { |
| 336 | case EAPOL.EAPOL_START: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 337 | stateMachine.start(); |
| 338 | stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 339 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 340 | //send an EAP Request/Identify to the supplicant |
| 341 | EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 342 | Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress), |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 343 | ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, |
| 344 | eapPayload); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 345 | stateMachine.setSupplicantAddress(srcMac); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 346 | stateMachine.setVlanId(ethPkt.getVlanID()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 347 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 348 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 349 | |
| 350 | break; |
| 351 | case EAPOL.EAPOL_PACKET: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 352 | RADIUS radiusPayload; |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 353 | // check if this is a Response/Identify or a Response/TLS |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 354 | EAP eapPacket = (EAP) eapol.getPayload(); |
| 355 | |
| 356 | byte dataType = eapPacket.getDataType(); |
| 357 | switch (dataType) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 358 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 359 | case EAP.ATTR_IDENTITY: |
| 360 | // request id access to RADIUS |
| 361 | stateMachine.setUsername(eapPacket.getData()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 362 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 363 | radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 364 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 365 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 366 | sendRadiusPacket(radiusPayload); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 367 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 368 | // change the state to "PENDING" |
| 369 | stateMachine.requestAccess(); |
| 370 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 371 | case EAP.ATTR_MD5: |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 372 | // verify if the EAP identifier corresponds to the |
| 373 | // challenge identifier from the client state |
| 374 | // machine. |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 375 | if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 376 | //send the RADIUS challenge response |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 377 | radiusPayload = |
| 378 | getRadiusPayload(stateMachine, |
| 379 | stateMachine.identifier(), |
| 380 | eapPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 381 | |
| 382 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 383 | stateMachine.challengeState()); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 384 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
| 385 | sendRadiusPacket(radiusPayload); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 386 | } |
| 387 | break; |
| 388 | case EAP.ATTR_TLS: |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 389 | // request id access to RADIUS |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 390 | radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 391 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 392 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 393 | stateMachine.challengeState()); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 394 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 395 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 396 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
| 397 | sendRadiusPacket(radiusPayload); |
Ray Milkey | 5493b51 | 2015-10-21 12:13:49 -0700 | [diff] [blame] | 398 | |
Ray Milkey | f3790b8 | 2015-10-21 16:28:08 -0700 | [diff] [blame] | 399 | if (stateMachine.state() != StateMachine.STATE_PENDING) { |
| 400 | stateMachine.requestAccess(); |
| 401 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 402 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 403 | break; |
| 404 | default: |
| 405 | return; |
| 406 | } |
| 407 | break; |
| 408 | default: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 409 | log.trace("Skipping EAPOL message {}", eapol.getEapolType()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 410 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 411 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 412 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | class RadiusListener implements Runnable { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 416 | |
| 417 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 418 | * Handles RADIUS packets. |
| 419 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 420 | * @param radiusPacket RADIUS packet coming from the RADIUS server. |
Ray Milkey | 17d6c49 | 2015-10-27 10:39:42 -0700 | [diff] [blame] | 421 | * @throws StateMachineException if an illegal state transition is triggered |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 422 | */ |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 423 | protected void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException { |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 424 | StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 425 | if (stateMachine == null) { |
| 426 | log.error("Invalid session identifier, exiting..."); |
| 427 | return; |
| 428 | } |
| 429 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 430 | EAP eapPayload; |
| 431 | Ethernet eth; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 432 | switch (radiusPacket.getCode()) { |
| 433 | case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE: |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 434 | byte[] challengeState = |
| 435 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE).getValue(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 436 | eapPayload = radiusPacket.decapsulateMessage(); |
| 437 | stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 438 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 439 | MacAddress.valueOf(nasMacAddress), |
| 440 | stateMachine.vlanId(), |
| 441 | EAPOL.EAPOL_PACKET, |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 442 | eapPayload); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 443 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 444 | break; |
| 445 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 446 | //send an EAPOL - Success to the supplicant. |
| 447 | byte[] eapMessage = |
| 448 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); |
| 449 | eapPayload = new EAP(); |
| 450 | eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length); |
| 451 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 452 | MacAddress.valueOf(nasMacAddress), |
| 453 | stateMachine.vlanId(), |
| 454 | EAPOL.EAPOL_PACKET, |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 455 | eapPayload); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 456 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 457 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 458 | stateMachine.authorizeAccess(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 459 | break; |
| 460 | case RADIUS.RADIUS_CODE_ACCESS_REJECT: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 461 | stateMachine.denyAccess(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 462 | break; |
| 463 | default: |
| 464 | log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode()); |
| 465 | } |
| 466 | } |
| 467 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 468 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 469 | @Override |
| 470 | public void run() { |
| 471 | boolean done = false; |
| 472 | int packetNumber = 1; |
| 473 | |
| 474 | log.info("UDP listener thread starting up"); |
| 475 | RADIUS inboundRadiusPacket; |
| 476 | while (!done) { |
| 477 | try { |
Ray Milkey | 5493b51 | 2015-10-21 12:13:49 -0700 | [diff] [blame] | 478 | byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH]; |
| 479 | DatagramPacket inboundBasePacket = |
| 480 | new DatagramPacket(packetBuffer, packetBuffer.length); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 481 | DatagramSocket socket = radiusSocket; |
| 482 | socket.receive(inboundBasePacket); |
| 483 | log.info("Packet #{} received", packetNumber++); |
| 484 | try { |
| 485 | inboundRadiusPacket = |
| 486 | RADIUS.deserializer() |
| 487 | .deserialize(inboundBasePacket.getData(), |
| 488 | 0, |
| 489 | inboundBasePacket.getLength()); |
| 490 | handleRadiusPacket(inboundRadiusPacket); |
| 491 | } catch (DeserializationException dex) { |
| 492 | log.error("Cannot deserialize packet", dex); |
| 493 | } catch (StateMachineException sme) { |
| 494 | log.error("Illegal state machine operation", sme); |
| 495 | } |
| 496 | |
| 497 | } catch (IOException e) { |
| 498 | log.info("Socket was closed, exiting listener thread"); |
| 499 | done = true; |
| 500 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 501 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 502 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 505 | RadiusListener radiusListener = new RadiusListener(); |
| 506 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 507 | private class InternalConfigListener implements NetworkConfigListener { |
| 508 | |
| 509 | /** |
| 510 | * Reconfigures the DHCP Server according to the configuration parameters passed. |
| 511 | * |
| 512 | * @param cfg configuration object |
| 513 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 514 | private void reconfigureNetwork(AaaConfig cfg) { |
| 515 | AaaConfig newCfg; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 516 | if (cfg == null) { |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 517 | newCfg = new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 518 | } else { |
| 519 | newCfg = cfg; |
| 520 | } |
| 521 | if (newCfg.nasIp() != null) { |
| 522 | nasIpAddress = newCfg.nasIp(); |
| 523 | } |
| 524 | if (newCfg.radiusIp() != null) { |
| 525 | radiusIpAddress = newCfg.radiusIp(); |
| 526 | } |
| 527 | if (newCfg.radiusMac() != null) { |
| 528 | radiusMacAddress = newCfg.radiusMac(); |
| 529 | } |
| 530 | if (newCfg.nasMac() != null) { |
| 531 | nasMacAddress = newCfg.nasMac(); |
| 532 | } |
| 533 | if (newCfg.radiusSecret() != null) { |
| 534 | radiusSecret = newCfg.radiusSecret(); |
| 535 | } |
| 536 | if (newCfg.radiusSwitch() != null) { |
| 537 | radiusSwitch = newCfg.radiusSwitch(); |
| 538 | } |
| 539 | if (newCfg.radiusPort() != -1) { |
| 540 | radiusPort = newCfg.radiusPort(); |
| 541 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 542 | if (newCfg.radiusServerUdpPort() != -1) { |
| 543 | radiusServerPort = newCfg.radiusServerUdpPort(); |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 544 | } |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void event(NetworkConfigEvent event) { |
| 549 | |
| 550 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 551 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 552 | event.configClass().equals(AaaConfig.class)) { |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 553 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 554 | AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 555 | reconfigureNetwork(cfg); |
| 556 | log.info("Reconfigured"); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 562 | } |