blob: 52536d179f90fe7503cfc0a115ab329fa37b90f6 [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.
37 *
38 */
39public 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 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
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();
dvaddire2ae58382017-10-29 08:57:42 +053099 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 Ghoshc9ac1e52017-07-28 12:31:18 +0100107
Amit Ghosh6c712f22017-09-18 17:04:51 +0100108 log.info("Setting nasId={} nasPortId{}", nodeName, nasPortId);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100109
Saurav Dase72358a2018-11-13 21:56:46 -0800110 if (updateNasIp()) {
111 inPkt.updateAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
112 deviceInfo.ipAddress().toOctets());
113 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100114
115 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_CALLING_STATION_ID,
Matt Jeannereta1376922018-11-28 10:59:35 -0500116 srcMac.toBytes());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100117
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 Ghoshc9ac1e52017-07-28 12:31:18 +0100122 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT,
Amit Ghoshf739be52017-09-21 15:49:37 +0100123 ByteBuffer.allocate(4).putInt((int) p.number().toLong()).array());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100124 // 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 Dase72358a2018-11-13 21:56:46 -0800145 @Override
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100146 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
dvaddire2ae58382017-10-29 08:57:42 +0530161 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 Ghoshc9ac1e52017-07-28 12:31:18 +0100170
171 IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
dvaddire2ae58382017-10-29 08:57:42 +0530172 ipv4Packet.setSourceAddress(ipAddress.toString());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100173 inPkt.setPayload(ipv4Packet);
174
175 return inPkt;
176 }
177}