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