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