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); |
Jian Li | 13c6716 | 2015-12-09 13:20:34 -0800 | [diff] [blame] | 195 | log.info("Started"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Deactivate |
| 199 | public void deactivate() { |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 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(); |
Jian Li | 13c6716 | 2015-12-09 13:20:34 -0800 | [diff] [blame] | 207 | log.info("Stopped"); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 210 | protected void sendRadiusPacket(RADIUS radiusPacket) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 211 | |
| 212 | try { |
| 213 | final byte[] data = radiusPacket.serialize(); |
| 214 | final DatagramSocket socket = radiusSocket; |
| 215 | |
| 216 | DatagramPacket packet = |
| 217 | new DatagramPacket(data, data.length, |
| 218 | radiusIpAddress, radiusServerPort); |
| 219 | |
| 220 | socket.send(packet); |
| 221 | } catch (IOException e) { |
| 222 | log.info("Cannot send packet to RADIUS server", e); |
| 223 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 226 | /** |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 227 | * Request packet in via PacketService. |
| 228 | */ |
| 229 | private void requestIntercepts() { |
| 230 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 231 | selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 232 | packetService.requestPackets(selector.build(), |
| 233 | CONTROL, appId); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Cancel request for packet in via PacketService. |
| 238 | */ |
| 239 | private void withdrawIntercepts() { |
| 240 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 241 | selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 242 | packetService.cancelPackets(selector.build(), CONTROL, appId); |
| 243 | } |
| 244 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Send the ethernet packet to the supplicant. |
| 247 | * |
| 248 | * @param ethernetPkt the ethernet packet |
| 249 | * @param connectPoint the connect point to send out |
| 250 | */ |
| 251 | private void sendPacketToSupplicant(Ethernet ethernetPkt, ConnectPoint connectPoint) { |
| 252 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(connectPoint.port()).build(); |
| 253 | OutboundPacket packet = new DefaultOutboundPacket(connectPoint.deviceId(), |
| 254 | treatment, ByteBuffer.wrap(ethernetPkt.serialize())); |
| 255 | packetService.emit(packet); |
| 256 | } |
| 257 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 258 | // our handler defined as a private inner class |
| 259 | |
| 260 | /** |
| 261 | * Packet processor responsible for forwarding packets along their paths. |
| 262 | */ |
| 263 | private class ReactivePacketProcessor implements PacketProcessor { |
| 264 | @Override |
| 265 | public void process(PacketContext context) { |
| 266 | |
| 267 | // Extract the original Ethernet frame from the packet information |
| 268 | InboundPacket pkt = context.inPacket(); |
| 269 | Ethernet ethPkt = pkt.parsed(); |
| 270 | if (ethPkt == null) { |
| 271 | return; |
| 272 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 273 | try { |
| 274 | // identify if incoming packet comes from supplicant (EAP) or RADIUS |
| 275 | switch (EthType.EtherType.lookup(ethPkt.getEtherType())) { |
| 276 | case EAPOL: |
| 277 | handleSupplicantPacket(context.inPacket()); |
| 278 | break; |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 279 | default: |
| 280 | log.trace("Skipping Ethernet packet type {}", |
| 281 | EthType.EtherType.lookup(ethPkt.getEtherType())); |
| 282 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 283 | } catch (StateMachineException e) { |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 284 | log.warn("Unable to process RADIUS packet:", e); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Creates and initializes common fields of a RADIUS packet. |
| 290 | * |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 291 | * @param stateMachine state machine for the request |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 292 | * @param eapPacket EAP packet |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 293 | * @return RADIUS packet |
| 294 | */ |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 295 | private RADIUS getRadiusPayload(StateMachine stateMachine, byte identifier, EAP eapPacket) { |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 296 | RADIUS radiusPayload = |
| 297 | new RADIUS(RADIUS.RADIUS_CODE_ACCESS_REQUEST, |
| 298 | eapPacket.getIdentifier()); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 299 | |
| 300 | // set Request Authenticator in StateMachine |
| 301 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
| 302 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 303 | radiusPayload.setIdentifier(identifier); |
| 304 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME, |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 305 | stateMachine.username()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 306 | |
| 307 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 308 | AaaManager.this.nasIpAddress.getAddress()); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 309 | |
| 310 | radiusPayload.encapsulateMessage(eapPacket); |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 311 | |
| 312 | return radiusPayload; |
| 313 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 314 | |
| 315 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 316 | * Handles PAE packets (supplicant). |
| 317 | * |
| 318 | * @param inPacket Ethernet packet coming from the supplicant |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 319 | */ |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 320 | private void handleSupplicantPacket(InboundPacket inPacket) throws StateMachineException { |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 321 | Ethernet ethPkt = inPacket.parsed(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 322 | // Where does it come from? |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 323 | MacAddress srcMac = ethPkt.getSourceMAC(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 324 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 325 | DeviceId deviceId = inPacket.receivedFrom().deviceId(); |
| 326 | PortNumber portNumber = inPacket.receivedFrom().port(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 327 | String sessionId = deviceId.toString() + portNumber.toString(); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 328 | StateMachine stateMachine = StateMachine.lookupStateMachineBySessionId(sessionId); |
| 329 | if (stateMachine == null) { |
| 330 | stateMachine = new StateMachine(sessionId, voltTenantService); |
| 331 | } |
| 332 | |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 333 | |
| 334 | EAPOL eapol = (EAPOL) ethPkt.getPayload(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 335 | |
| 336 | switch (eapol.getEapolType()) { |
| 337 | case EAPOL.EAPOL_START: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 338 | stateMachine.start(); |
| 339 | stateMachine.setSupplicantConnectpoint(inPacket.receivedFrom()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 340 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 341 | //send an EAP Request/Identify to the supplicant |
| 342 | EAP eapPayload = new EAP(EAP.REQUEST, stateMachine.identifier(), EAP.ATTR_IDENTITY, null); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 343 | Ethernet eth = buildEapolResponse(srcMac, MacAddress.valueOf(nasMacAddress), |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 344 | ethPkt.getVlanID(), EAPOL.EAPOL_PACKET, |
| 345 | eapPayload); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 346 | stateMachine.setSupplicantAddress(srcMac); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 347 | stateMachine.setVlanId(ethPkt.getVlanID()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 348 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 349 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 350 | |
| 351 | break; |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 352 | case EAPOL.EAPOL_LOGOFF: |
Ray Milkey | b34b496 | 2016-01-04 10:24:43 -0800 | [diff] [blame^] | 353 | if (stateMachine.state() == StateMachine.STATE_AUTHORIZED) { |
| 354 | stateMachine.logoff(); |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 358 | case EAPOL.EAPOL_PACKET: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 359 | RADIUS radiusPayload; |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 360 | // check if this is a Response/Identify or a Response/TLS |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 361 | EAP eapPacket = (EAP) eapol.getPayload(); |
| 362 | |
| 363 | byte dataType = eapPacket.getDataType(); |
| 364 | switch (dataType) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 365 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 366 | case EAP.ATTR_IDENTITY: |
| 367 | // request id access to RADIUS |
| 368 | stateMachine.setUsername(eapPacket.getData()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 369 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 370 | radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 371 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 372 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 373 | sendRadiusPacket(radiusPayload); |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 374 | |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 375 | // change the state to "PENDING" |
| 376 | stateMachine.requestAccess(); |
| 377 | break; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 378 | case EAP.ATTR_MD5: |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 379 | // verify if the EAP identifier corresponds to the |
| 380 | // challenge identifier from the client state |
| 381 | // machine. |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 382 | if (eapPacket.getIdentifier() == stateMachine.challengeIdentifier()) { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 383 | //send the RADIUS challenge response |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 384 | radiusPayload = |
| 385 | getRadiusPayload(stateMachine, |
| 386 | stateMachine.identifier(), |
| 387 | eapPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 388 | |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 389 | if (stateMachine.challengeState() != null) { |
| 390 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 391 | stateMachine.challengeState()); |
| 392 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 393 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
| 394 | sendRadiusPacket(radiusPayload); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 395 | } |
| 396 | break; |
| 397 | case EAP.ATTR_TLS: |
Ray Milkey | 9eb293f | 2015-09-30 15:09:17 -0700 | [diff] [blame] | 398 | // request id access to RADIUS |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 399 | radiusPayload = getRadiusPayload(stateMachine, stateMachine.identifier(), eapPacket); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 400 | |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 401 | if (stateMachine.challengeState() != null) { |
| 402 | radiusPayload.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 403 | stateMachine.challengeState()); |
| 404 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 405 | stateMachine.setRequestAuthenticator(radiusPayload.generateAuthCode()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 406 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 407 | radiusPayload.addMessageAuthenticator(AaaManager.this.radiusSecret); |
| 408 | sendRadiusPacket(radiusPayload); |
Ray Milkey | 5493b51 | 2015-10-21 12:13:49 -0700 | [diff] [blame] | 409 | |
Ray Milkey | f3790b8 | 2015-10-21 16:28:08 -0700 | [diff] [blame] | 410 | if (stateMachine.state() != StateMachine.STATE_PENDING) { |
| 411 | stateMachine.requestAccess(); |
| 412 | } |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 413 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | return; |
| 417 | } |
| 418 | break; |
| 419 | default: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 420 | log.trace("Skipping EAPOL message {}", eapol.getEapolType()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 421 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 422 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 423 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | class RadiusListener implements Runnable { |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 427 | |
| 428 | /** |
Jonathan Hart | a46dddf | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 429 | * Handles RADIUS packets. |
| 430 | * |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 431 | * @param radiusPacket RADIUS packet coming from the RADIUS server. |
Ray Milkey | 17d6c49 | 2015-10-27 10:39:42 -0700 | [diff] [blame] | 432 | * @throws StateMachineException if an illegal state transition is triggered |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 433 | */ |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 434 | protected void handleRadiusPacket(RADIUS radiusPacket) throws StateMachineException { |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 435 | StateMachine stateMachine = StateMachine.lookupStateMachineById(radiusPacket.getIdentifier()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 436 | if (stateMachine == null) { |
| 437 | log.error("Invalid session identifier, exiting..."); |
| 438 | return; |
| 439 | } |
| 440 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 441 | EAP eapPayload; |
| 442 | Ethernet eth; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 443 | switch (radiusPacket.getCode()) { |
| 444 | case RADIUS.RADIUS_CODE_ACCESS_CHALLENGE: |
Qianqian Hu | b55a1ac | 2015-12-23 20:44:48 +0800 | [diff] [blame] | 445 | RADIUSAttribute radiusAttrState = radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_STATE); |
| 446 | byte[] challengeState = null; |
| 447 | if (radiusAttrState != null) { |
| 448 | challengeState = radiusAttrState.getValue(); |
| 449 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 450 | eapPayload = radiusPacket.decapsulateMessage(); |
| 451 | stateMachine.setChallengeInfo(eapPayload.getIdentifier(), challengeState); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 452 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 453 | MacAddress.valueOf(nasMacAddress), |
| 454 | stateMachine.vlanId(), |
| 455 | EAPOL.EAPOL_PACKET, |
Aaron Kruglikov | d39d99e | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 456 | eapPayload); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 457 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 458 | break; |
| 459 | case RADIUS.RADIUS_CODE_ACCESS_ACCEPT: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 460 | //send an EAPOL - Success to the supplicant. |
| 461 | byte[] eapMessage = |
| 462 | radiusPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE).getValue(); |
| 463 | eapPayload = new EAP(); |
| 464 | eapPayload = (EAP) eapPayload.deserialize(eapMessage, 0, eapMessage.length); |
| 465 | eth = buildEapolResponse(stateMachine.supplicantAddress(), |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 466 | MacAddress.valueOf(nasMacAddress), |
| 467 | stateMachine.vlanId(), |
| 468 | EAPOL.EAPOL_PACKET, |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 469 | eapPayload); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 470 | sendPacketToSupplicant(eth, stateMachine.supplicantConnectpoint()); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 471 | |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 472 | stateMachine.authorizeAccess(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 473 | break; |
| 474 | case RADIUS.RADIUS_CODE_ACCESS_REJECT: |
Ray Milkey | f51eba2 | 2015-09-25 10:24:23 -0700 | [diff] [blame] | 475 | stateMachine.denyAccess(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 476 | break; |
| 477 | default: |
| 478 | log.warn("Unknown RADIUS message received with code: {}", radiusPacket.getCode()); |
| 479 | } |
| 480 | } |
| 481 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 482 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 483 | @Override |
| 484 | public void run() { |
| 485 | boolean done = false; |
| 486 | int packetNumber = 1; |
| 487 | |
| 488 | log.info("UDP listener thread starting up"); |
| 489 | RADIUS inboundRadiusPacket; |
| 490 | while (!done) { |
| 491 | try { |
Ray Milkey | 5493b51 | 2015-10-21 12:13:49 -0700 | [diff] [blame] | 492 | byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH]; |
| 493 | DatagramPacket inboundBasePacket = |
| 494 | new DatagramPacket(packetBuffer, packetBuffer.length); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 495 | DatagramSocket socket = radiusSocket; |
| 496 | socket.receive(inboundBasePacket); |
| 497 | log.info("Packet #{} received", packetNumber++); |
| 498 | try { |
| 499 | inboundRadiusPacket = |
| 500 | RADIUS.deserializer() |
| 501 | .deserialize(inboundBasePacket.getData(), |
| 502 | 0, |
| 503 | inboundBasePacket.getLength()); |
| 504 | handleRadiusPacket(inboundRadiusPacket); |
| 505 | } catch (DeserializationException dex) { |
| 506 | log.error("Cannot deserialize packet", dex); |
| 507 | } catch (StateMachineException sme) { |
| 508 | log.error("Illegal state machine operation", sme); |
| 509 | } |
| 510 | |
| 511 | } catch (IOException e) { |
| 512 | log.info("Socket was closed, exiting listener thread"); |
| 513 | done = true; |
| 514 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 515 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 516 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 519 | RadiusListener radiusListener = new RadiusListener(); |
| 520 | |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 521 | private class InternalConfigListener implements NetworkConfigListener { |
| 522 | |
| 523 | /** |
Ray Milkey | c9e8dcc | 2015-12-30 10:31:32 -0800 | [diff] [blame] | 524 | * Reconfigures the AAA application according to the |
| 525 | * configuration parameters passed. |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 526 | * |
| 527 | * @param cfg configuration object |
| 528 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 529 | private void reconfigureNetwork(AaaConfig cfg) { |
| 530 | AaaConfig newCfg; |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 531 | if (cfg == null) { |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 532 | newCfg = new AaaConfig(); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 533 | } else { |
| 534 | newCfg = cfg; |
| 535 | } |
| 536 | if (newCfg.nasIp() != null) { |
| 537 | nasIpAddress = newCfg.nasIp(); |
| 538 | } |
| 539 | if (newCfg.radiusIp() != null) { |
| 540 | radiusIpAddress = newCfg.radiusIp(); |
| 541 | } |
| 542 | if (newCfg.radiusMac() != null) { |
| 543 | radiusMacAddress = newCfg.radiusMac(); |
| 544 | } |
| 545 | if (newCfg.nasMac() != null) { |
| 546 | nasMacAddress = newCfg.nasMac(); |
| 547 | } |
| 548 | if (newCfg.radiusSecret() != null) { |
| 549 | radiusSecret = newCfg.radiusSecret(); |
| 550 | } |
| 551 | if (newCfg.radiusSwitch() != null) { |
| 552 | radiusSwitch = newCfg.radiusSwitch(); |
| 553 | } |
| 554 | if (newCfg.radiusPort() != -1) { |
| 555 | radiusPort = newCfg.radiusPort(); |
| 556 | } |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 557 | if (newCfg.radiusServerUdpPort() != -1) { |
| 558 | radiusServerPort = newCfg.radiusServerUdpPort(); |
Ray Milkey | 5d99bd1 | 2015-10-06 15:41:30 -0700 | [diff] [blame] | 559 | } |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | @Override |
| 563 | public void event(NetworkConfigEvent event) { |
| 564 | |
| 565 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 566 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 567 | event.configClass().equals(AaaConfig.class)) { |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 568 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 569 | AaaConfig cfg = netCfgService.getConfig(appId, AaaConfig.class); |
Ray Milkey | fcb623d | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 570 | reconfigureNetwork(cfg); |
| 571 | log.info("Reconfigured"); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 577 | } |