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