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 | 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. |
| 37 | * |
| 38 | */ |
| 39 | public class SamplePacketCustomizer extends PacketCustomizer { |
| 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 | |
| 73 | SubscriberAndDeviceInformation subscriber = customInfo. |
| 74 | subscriberService().get(id); |
| 75 | |
| 76 | if (subscriber == null) { |
| 77 | log.warn("No subscriber found with id {}", id); |
| 78 | return inPkt; |
| 79 | } |
| 80 | |
| 81 | String nasPortId = subscriber.nasPortId(); |
| 82 | |
| 83 | Ethernet ethPkt = eapPacket.parsed(); |
| 84 | MacAddress srcMac = ethPkt.getSourceMAC(); |
| 85 | |
| 86 | // Get the nasId from subscriber service using the Serial Number |
| 87 | String serialNo = customInfo.deviceService().getDevice(eapPacket. |
| 88 | receivedFrom().deviceId()).serialNumber(); |
| 89 | |
| 90 | log.info("SampleRadiusCustomizer serial = {}", serialNo); |
| 91 | SubscriberAndDeviceInformation deviceInfo = customInfo. |
| 92 | subscriberService().get(serialNo); |
| 93 | |
| 94 | if (deviceInfo == null) { |
| 95 | log.warn("No Device found with SN {}", serialNo); |
| 96 | return inPkt; |
| 97 | } |
| 98 | String nodeName = deviceInfo.nasId(); |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame^] | 99 | Ip4Address ipAddress = deviceInfo.ipAddress(); |
| 100 | if (nasPortId == null || nodeName == null || ipAddress == null) { |
| 101 | log.warn("Insufficient data to Customize packet" + |
| 102 | " : nasPortId = {}, nodeName = {}, ipAddress = {}", |
| 103 | nasPortId, nodeName, ipAddress); |
| 104 | return inPkt; |
| 105 | } |
| 106 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 107 | |
Amit Ghosh | 6c712f2 | 2017-09-18 17:04:51 +0100 | [diff] [blame] | 108 | log.info("Setting nasId={} nasPortId{}", nodeName, nasPortId); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 109 | |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 110 | if (updateNasIp()) { |
| 111 | inPkt.updateAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP, |
| 112 | deviceInfo.ipAddress().toOctets()); |
| 113 | } |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 114 | |
| 115 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_CALLING_STATION_ID, |
Matt Jeanneret | a137692 | 2018-11-28 10:59:35 -0500 | [diff] [blame] | 116 | srcMac.toBytes()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 117 | |
| 118 | // Check value - 16 was used in PoC2, as per PoC3 TS value should be 15 |
| 119 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT_TYPE, |
| 120 | ByteBuffer.allocate(4).putInt(15).array()); |
| 121 | |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 122 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT, |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 123 | ByteBuffer.allocate(4).putInt((int) p.number().toLong()).array()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 124 | // Check - If this is needed, worked with this value in PoC2 |
| 125 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_ACCT_SESSION_ID, |
| 126 | "023:27:46:00000".getBytes()); |
| 127 | |
| 128 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_ID, |
| 129 | nodeName.getBytes()); |
| 130 | inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT_ID, |
| 131 | nasPortId.getBytes()); |
| 132 | |
| 133 | return inPkt; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Customize the Ethernet header as per specific Setup or RADIUS |
| 138 | * server requirements. |
| 139 | * |
| 140 | * @param inPkt Ethernet packet to be changed |
| 141 | * @param eapPacket Incoming packet containing EAP for which this the |
| 142 | * RADIUS message is being created |
| 143 | * @return Changed Ethernet packet |
| 144 | */ |
Saurav Das | e72358a | 2018-11-13 21:56:46 -0800 | [diff] [blame] | 145 | @Override |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 146 | public Ethernet customizeEthernetIPHeaders(Ethernet inPkt, |
| 147 | InboundPacket eapPacket) { |
| 148 | |
| 149 | String serialNo = customInfo.deviceService().getDevice(eapPacket. |
| 150 | receivedFrom().deviceId()).serialNumber(); |
| 151 | |
| 152 | log.info("SampleRadiusCustomzer customizer serial = {}", serialNo); |
| 153 | SubscriberAndDeviceInformation deviceInfo = customInfo. |
| 154 | subscriberService().get(serialNo); |
| 155 | |
| 156 | if (deviceInfo == null) { |
| 157 | log.warn("No Device found with SN {}", serialNo); |
| 158 | return inPkt; |
| 159 | } |
| 160 | |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame^] | 161 | MacAddress macAddress = deviceInfo.hardwareIdentifier(); |
| 162 | Ip4Address ipAddress = deviceInfo.ipAddress(); |
| 163 | if (macAddress == null || ipAddress == null) { |
| 164 | log.warn("Insufficient data to Customize Ethernet IP Headers" + |
| 165 | " : hardwareIdentifier = {}, ipAddress = {}", |
| 166 | macAddress, ipAddress); |
| 167 | return inPkt; |
| 168 | } |
| 169 | inPkt.setSourceMACAddress(macAddress); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 170 | |
| 171 | IPv4 ipv4Packet = (IPv4) inPkt.getPayload(); |
dvaddire | 2ae5838 | 2017-10-29 08:57:42 +0530 | [diff] [blame^] | 172 | ipv4Packet.setSourceAddress(ipAddress.toString()); |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 173 | inPkt.setPayload(ipv4Packet); |
| 174 | |
| 175 | return inPkt; |
| 176 | } |
| 177 | } |