blob: 6130073d148dba0fb27a9bbb00c5bb2847314dbe [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 */
16package org.opencord.aaa;
17
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;
23import org.onlab.packet.IPv4;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.RADIUS;
26import org.onlab.packet.RADIUSAttribute;
27import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.Port;
29import org.onosproject.net.packet.InboundPacket;
30import org.opencord.sadis.SubscriberAndDeviceInformation;
31import org.slf4j.Logger;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010032
33
34/**
35 * Sample RADIUS Packet Customization.
36 *
37 */
38public 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 Dase72358a2018-11-13 21:56:46 -080047 * 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 Ghoshc9ac1e52017-07-28 12:31:18 +010056 * 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 Ghosh6c712f22017-09-18 17:04:51 +010099 log.info("Setting nasId={} nasPortId{}", nodeName, nasPortId);
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100100
Saurav Dase72358a2018-11-13 21:56:46 -0800101 if (updateNasIp()) {
102 inPkt.updateAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP,
103 deviceInfo.ipAddress().toOctets());
104 }
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100105
106 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_CALLING_STATION_ID,
107 srcMac.toString().getBytes());
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 Ghoshc9ac1e52017-07-28 12:31:18 +0100113 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT,
Amit Ghoshf739be52017-09-21 15:49:37 +0100114 ByteBuffer.allocate(4).putInt((int) p.number().toLong()).array());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100115 // 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 Dase72358a2018-11-13 21:56:46 -0800136 @Override
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100137 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}