blob: 75f23dd20df1daf0d50baab162570e462bf25c67 [file] [log] [blame]
Amit Ghoshc9ac1e52017-07-28 12:31:18 +01001/*
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 Scandolocf847b82019-04-26 15:00:00 -070016package org.opencord.aaa.impl;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010017
Saurav Dase72358a2018-11-13 21:56:46 -080018import static org.slf4j.LoggerFactory.getLogger;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010019
20import java.nio.ByteBuffer;
21
Saurav Dase72358a2018-11-13 21:56:46 -080022import org.onlab.packet.Ethernet;
dvaddire2ae58382017-10-29 08:57:42 +053023import org.onlab.packet.Ip4Address;
Saurav Dase72358a2018-11-13 21:56:46 -080024import org.onlab.packet.MacAddress;
dvaddire2ae58382017-10-29 08:57:42 +053025import org.onlab.packet.IPv4;
Saurav Dase72358a2018-11-13 21:56:46 -080026import org.onlab.packet.RADIUS;
27import org.onlab.packet.RADIUSAttribute;
28import org.onosproject.net.AnnotationKeys;
29import org.onosproject.net.Port;
30import org.onosproject.net.packet.InboundPacket;
31import org.opencord.sadis.SubscriberAndDeviceInformation;
32import org.slf4j.Logger;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010033
34
35/**
36 * Sample RADIUS Packet Customization.
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010037 */
Ilayda Ozdemir9fdeee72021-02-26 12:24:27 +000038public class SamplePacketCustomizer extends PacketCustomizer {
39 private static final String SADIS_NOT_RUNNING = "Sadis is not running.";
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010040
41 private final Logger log = getLogger(getClass());
42
43 public SamplePacketCustomizer(CustomizationInfo customInfo) {
44 super(customInfo);
45 }
46
47 /**
Saurav Dase72358a2018-11-13 21:56:46 -080048 * 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 Ghoshc9ac1e52017-07-28 12:31:18 +010057 * 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 Ozdemir9fdeee72021-02-26 12:24:27 +000073 if (customInfo.subscriberService() == null) {
74 log.warn(SADIS_NOT_RUNNING);
75 return inPkt;
76 }
77
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010078 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 Ozdemir9fdeee72021-02-26 12:24:27 +000096
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010097 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();
dvaddire2ae58382017-10-29 08:57:42 +0530105 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 Ghoshc9ac1e52017-07-28 12:31:18 +0100113
Amit Ghosh6c712f22017-09-18 17:04:51 +0100114 log.info("Setting nasId={} nasPortId{}", nodeName, nasPortId);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100115
Saurav Dase72358a2018-11-13 21:56:46 -0800116 if (updateNasIp()) {
117 inPkt.updateAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
118 deviceInfo.ipAddress().toOctets());
119 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100120
121 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_CALLING_STATION_ID,
Matt Jeannereta1376922018-11-28 10:59:35 -0500122 srcMac.toBytes());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100123
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 Ghoshc9ac1e52017-07-28 12:31:18 +0100128 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT,
Amit Ghoshf739be52017-09-21 15:49:37 +0100129 ByteBuffer.allocate(4).putInt((int) p.number().toLong()).array());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100130 // 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 Dase72358a2018-11-13 21:56:46 -0800151 @Override
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100152 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 Ozdemir9fdeee72021-02-26 12:24:27 +0000159
160 if (customInfo.subscriberService() == null) {
161 log.warn(SADIS_NOT_RUNNING);
162 return inPkt;
163 }
164
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100165 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
dvaddire2ae58382017-10-29 08:57:42 +0530173 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 Ghoshc9ac1e52017-07-28 12:31:18 +0100182
183 IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
dvaddire2ae58382017-10-29 08:57:42 +0530184 ipv4Packet.setSourceAddress(ipAddress.toString());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100185 inPkt.setPayload(ipv4Packet);
186
187 return inPkt;
188 }
189}