Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 1 | /* |
Joey Armstrong | 4de0b45 | 2023-01-18 15:26:54 -0500 | [diff] [blame^] | 2 | * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 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 | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 22 | import org.onlab.packet.Ethernet; |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame] | 25 | import org.onlab.packet.IPv4; |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 26 | import org.onlab.packet.RADIUS; |
| 27 | import org.onlab.packet.RADIUSAttribute; |
| 28 | import org.onosproject.net.AnnotationKeys; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.packet.InboundPacket; |
| 31 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 32 | import org.slf4j.Logger; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 33 | |
| 34 | |
| 35 | /** |
| 36 | * Sample RADIUS Packet Customization. |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 37 | */ |
Ilayda Ozdemir | 9fdeee7 | 2021-02-26 12:24:27 +0000 | [diff] [blame] | 38 | public class SamplePacketCustomizer extends PacketCustomizer { |
| 39 | private static final String SADIS_NOT_RUNNING = "Sadis is not running."; |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | public SamplePacketCustomizer(CustomizationInfo customInfo) { |
| 44 | super(customInfo); |
| 45 | } |
| 46 | |
| 47 | /** |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 48 | * Determines if NAS IP Attribute should be updated or not. |
| 49 | * |
| 50 | * @return true if updating NAS IP is desired |
| 51 | */ |
| 52 | protected boolean updateNasIp() { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | /** |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 57 | * Customize the packet as per specific Setup or RADIUS |
| 58 | * server requirements. |
| 59 | * |
| 60 | * @param inPkt RADIUS packet to be customized |
| 61 | * @param eapPacket Incoming packet containing EAP for which this the |
| 62 | * RADIUS message is being created |
| 63 | * @return Customized RADIUS packet |
| 64 | */ |
| 65 | @Override |
| 66 | public RADIUS customizePacket(RADIUS inPkt, InboundPacket eapPacket) { |
| 67 | Port p = customInfo.deviceService().getPort(eapPacket.receivedFrom()); |
| 68 | |
| 69 | String id = p.annotations().value(AnnotationKeys.PORT_NAME); |
| 70 | |
| 71 | log.info("Customizing packet Port received for {}", id); |
| 72 | |
Ilayda Ozdemir | 9fdeee7 | 2021-02-26 12:24:27 +0000 | [diff] [blame] | 73 | if (customInfo.subscriberService() == null) { |
| 74 | log.warn(SADIS_NOT_RUNNING); |
| 75 | return inPkt; |
| 76 | } |
| 77 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 78 | SubscriberAndDeviceInformation subscriber = customInfo. |
| 79 | subscriberService().get(id); |
| 80 | |
| 81 | if (subscriber == null) { |
| 82 | log.warn("No subscriber found with id {}", id); |
| 83 | return inPkt; |
| 84 | } |
| 85 | |
| 86 | String nasPortId = subscriber.nasPortId(); |
| 87 | |
| 88 | Ethernet ethPkt = eapPacket.parsed(); |
| 89 | MacAddress srcMac = ethPkt.getSourceMAC(); |
| 90 | |
| 91 | // Get the nasId from subscriber service using the Serial Number |
| 92 | String serialNo = customInfo.deviceService().getDevice(eapPacket. |
| 93 | receivedFrom().deviceId()).serialNumber(); |
| 94 | |
| 95 | log.info("SampleRadiusCustomizer serial = {}", serialNo); |
Ilayda Ozdemir | 9fdeee7 | 2021-02-26 12:24:27 +0000 | [diff] [blame] | 96 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 97 | SubscriberAndDeviceInformation deviceInfo = customInfo. |
| 98 | subscriberService().get(serialNo); |
| 99 | |
| 100 | if (deviceInfo == null) { |
| 101 | log.warn("No Device found with SN {}", serialNo); |
| 102 | return inPkt; |
| 103 | } |
| 104 | String nodeName = deviceInfo.nasId(); |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame] | 105 | Ip4Address ipAddress = deviceInfo.ipAddress(); |
| 106 | if (nasPortId == null || nodeName == null || ipAddress == null) { |
| 107 | log.warn("Insufficient data to Customize packet" + |
| 108 | " : nasPortId = {}, nodeName = {}, ipAddress = {}", |
| 109 | nasPortId, nodeName, ipAddress); |
| 110 | return inPkt; |
| 111 | } |
| 112 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 113 | |
Amit Ghosh | 6c712f2 | 2017-09-18 17:04:51 +0100 | [diff] [blame] | 114 | log.info("Setting nasId={} nasPortId{}", nodeName, nasPortId); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 115 | |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 116 | if (updateNasIp()) { |
| 117 | inPkt.updateAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
| 118 | deviceInfo.ipAddress().toOctets()); |
| 119 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 120 | |
| 121 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_CALLING_STATION_ID, |
Matt Jeanneret | a137692 | 2018-11-28 10:59:35 -0500 | [diff] [blame] | 122 | srcMac.toBytes()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 123 | |
| 124 | // Check value - 16 was used in PoC2, as per PoC3 TS value should be 15 |
| 125 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT_TYPE, |
| 126 | ByteBuffer.allocate(4).putInt(15).array()); |
| 127 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 128 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT, |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 129 | ByteBuffer.allocate(4).putInt((int) p.number().toLong()).array()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 130 | // Check - If this is needed, worked with this value in PoC2 |
| 131 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_ACCT_SESSION_ID, |
| 132 | "023:27:46:00000".getBytes()); |
| 133 | |
| 134 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_ID, |
| 135 | nodeName.getBytes()); |
| 136 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT_ID, |
| 137 | nasPortId.getBytes()); |
| 138 | |
| 139 | return inPkt; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Customize the Ethernet header as per specific Setup or RADIUS |
| 144 | * server requirements. |
| 145 | * |
| 146 | * @param inPkt Ethernet packet to be changed |
| 147 | * @param eapPacket Incoming packet containing EAP for which this the |
| 148 | * RADIUS message is being created |
| 149 | * @return Changed Ethernet packet |
| 150 | */ |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 151 | @Override |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 152 | public Ethernet customizeEthernetIPHeaders(Ethernet inPkt, |
| 153 | InboundPacket eapPacket) { |
| 154 | |
| 155 | String serialNo = customInfo.deviceService().getDevice(eapPacket. |
| 156 | receivedFrom().deviceId()).serialNumber(); |
| 157 | |
| 158 | log.info("SampleRadiusCustomzer customizer serial = {}", serialNo); |
Ilayda Ozdemir | 9fdeee7 | 2021-02-26 12:24:27 +0000 | [diff] [blame] | 159 | |
| 160 | if (customInfo.subscriberService() == null) { |
| 161 | log.warn(SADIS_NOT_RUNNING); |
| 162 | return inPkt; |
| 163 | } |
| 164 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 165 | SubscriberAndDeviceInformation deviceInfo = customInfo. |
| 166 | subscriberService().get(serialNo); |
| 167 | |
| 168 | if (deviceInfo == null) { |
| 169 | log.warn("No Device found with SN {}", serialNo); |
| 170 | return inPkt; |
| 171 | } |
| 172 | |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame] | 173 | MacAddress macAddress = deviceInfo.hardwareIdentifier(); |
| 174 | Ip4Address ipAddress = deviceInfo.ipAddress(); |
| 175 | if (macAddress == null || ipAddress == null) { |
| 176 | log.warn("Insufficient data to Customize Ethernet IP Headers" + |
| 177 | " : hardwareIdentifier = {}, ipAddress = {}", |
| 178 | macAddress, ipAddress); |
| 179 | return inPkt; |
| 180 | } |
| 181 | inPkt.setSourceMACAddress(macAddress); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 182 | |
| 183 | IPv4 ipv4Packet = (IPv4) inPkt.getPayload(); |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame] | 184 | ipv4Packet.setSourceAddress(ipAddress.toString()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 185 | inPkt.setPayload(ipv4Packet); |
| 186 | |
| 187 | return inPkt; |
| 188 | } |
| 189 | } |