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