Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.opencord.aaa; |
| 17 | |
| 18 | import org.onlab.packet.DeserializationException; |
| 19 | import org.onlab.packet.EthType; |
| 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.RADIUS; |
| 22 | |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.packet.InboundPacket; |
| 27 | import org.onosproject.net.packet.PacketContext; |
| 28 | import org.onosproject.net.packet.PacketService; |
| 29 | |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 33 | |
| 34 | import static org.onosproject.net.packet.PacketPriority.CONTROL; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.net.DatagramPacket; |
| 39 | import java.net.DatagramSocket; |
| 40 | import java.net.InetAddress; |
| 41 | import java.net.InetSocketAddress; |
| 42 | import java.util.concurrent.ExecutorService; |
| 43 | import java.util.concurrent.Executors; |
| 44 | |
| 45 | /** |
| 46 | * Handles Socket based communication with the RADIUS server. |
| 47 | */ |
| 48 | public class SocketBasedRadiusCommunicator implements RadiusCommunicator { |
| 49 | |
| 50 | // for verbose output |
| 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | // our unique identifier |
| 54 | private ApplicationId appId; |
| 55 | |
| 56 | // to receive Packet-in events that we'll respond to |
| 57 | PacketService packetService; |
| 58 | |
| 59 | // Socket used for UDP communications with RADIUS server |
| 60 | private DatagramSocket radiusSocket; |
| 61 | |
| 62 | // Parsed RADIUS server addresses |
| 63 | protected InetAddress radiusIpAddress; |
| 64 | |
| 65 | // RADIUS server TCP port number |
| 66 | protected short radiusServerPort; |
| 67 | |
| 68 | // Executor for RADIUS communication thread |
| 69 | private ExecutorService executor; |
| 70 | |
| 71 | AaaManager aaaManager; |
| 72 | |
| 73 | SocketBasedRadiusCommunicator(ApplicationId appId, PacketService pktService, |
| 74 | AaaManager aaaManager) { |
| 75 | this.appId = appId; |
| 76 | this.packetService = pktService; |
| 77 | this.aaaManager = aaaManager; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void initializeLocalState(AaaConfig newCfg) { |
| 82 | if (newCfg.radiusIp() != null) { |
| 83 | radiusIpAddress = newCfg.radiusIp(); |
| 84 | } |
| 85 | radiusServerPort = newCfg.radiusServerUdpPort(); |
| 86 | |
| 87 | try { |
| 88 | radiusSocket = new DatagramSocket(null); |
| 89 | radiusSocket.setReuseAddress(true); |
| 90 | radiusSocket.bind(new InetSocketAddress(radiusServerPort)); |
| 91 | } catch (Exception ex) { |
| 92 | log.error("Can't open RADIUS socket", ex); |
| 93 | } |
| 94 | |
| 95 | executor = Executors.newSingleThreadExecutor( |
| 96 | new ThreadFactoryBuilder() |
| 97 | .setNameFormat("AAA-radius-%d").build()); |
| 98 | executor.execute(radiusListener); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void clearLocalState() { |
| 103 | radiusSocket.close(); |
| 104 | executor.shutdownNow(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void deactivate() { } |
| 109 | |
| 110 | @Override |
| 111 | public void requestIntercepts() { |
| 112 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 113 | selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 114 | packetService.requestPackets(selector.build(), CONTROL, appId); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void withdrawIntercepts() { |
| 119 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 120 | selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 121 | packetService.cancelPackets(selector.build(), CONTROL, appId); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) { |
| 126 | try { |
| 127 | final byte[] data = radiusPacket.serialize(); |
| 128 | final DatagramSocket socket = radiusSocket; |
| 129 | |
| 130 | DatagramPacket packet = |
| 131 | new DatagramPacket(data, data.length, |
| 132 | radiusIpAddress, radiusServerPort); |
| 133 | |
| 134 | socket.send(packet); |
Amit Ghosh | cf39d97 | 2017-08-16 08:15:20 +0100 | [diff] [blame] | 135 | log.info("Packet sent to Radius Server using socket packet = {}", packet); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 136 | } catch (IOException e) { |
| 137 | log.info("Cannot send packet to RADIUS server", e); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void handlePacketFromServer(PacketContext context) { |
| 143 | InboundPacket pkt = context.inPacket(); |
| 144 | Ethernet ethPkt = pkt.parsed(); |
| 145 | |
| 146 | log.trace("Skipping Ethernet packet type {}", |
| 147 | EthType.EtherType.lookup(ethPkt.getEtherType())); |
| 148 | } |
| 149 | |
| 150 | class RadiusListener implements Runnable { |
| 151 | |
| 152 | @Override |
| 153 | public void run() { |
| 154 | boolean done = false; |
| 155 | int packetNumber = 1; |
| 156 | |
| 157 | log.info("UDP listener thread starting up"); |
| 158 | RADIUS inboundRadiusPacket; |
| 159 | while (!done) { |
| 160 | try { |
| 161 | byte[] packetBuffer = new byte[RADIUS.RADIUS_MAX_LENGTH]; |
| 162 | DatagramPacket inboundBasePacket = |
| 163 | new DatagramPacket(packetBuffer, packetBuffer.length); |
| 164 | DatagramSocket socket = radiusSocket; |
| 165 | socket.receive(inboundBasePacket); |
| 166 | log.info("Packet #{} received", packetNumber++); |
| 167 | try { |
| 168 | inboundRadiusPacket = |
| 169 | RADIUS.deserializer() |
| 170 | .deserialize(inboundBasePacket.getData(), |
| 171 | 0, |
| 172 | inboundBasePacket.getLength()); |
| 173 | aaaManager.handleRadiusPacket(inboundRadiusPacket); |
| 174 | } catch (DeserializationException dex) { |
| 175 | log.error("Cannot deserialize packet", dex); |
| 176 | } catch (StateMachineException sme) { |
| 177 | log.error("Illegal state machine operation", sme); |
| 178 | } |
| 179 | |
| 180 | } catch (IOException e) { |
| 181 | log.info("Socket was closed, exiting listener thread"); |
| 182 | done = true; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | RadiusListener radiusListener = new RadiusListener(); |
| 189 | } |