blob: 358890ee18aeedf26a36ed17a94e16bcce097329 [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
105 // Check - This may not be needed but was used in PoC2
106 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT,
107 ByteBuffer.allocate(4).putInt(46178304).array());
108 // Check - If this is needed, worked with this value in PoC2
109 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_ACCT_SESSION_ID,
110 "023:27:46:00000".getBytes());
111
112 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_ID,
113 nodeName.getBytes());
114 inPkt.setAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_PORT_ID,
115 nasPortId.getBytes());
116
117 return inPkt;
118 }
119
120 /**
121 * Customize the Ethernet header as per specific Setup or RADIUS
122 * server requirements.
123 *
124 * @param inPkt Ethernet packet to be changed
125 * @param eapPacket Incoming packet containing EAP for which this the
126 * RADIUS message is being created
127 * @return Changed Ethernet packet
128 */
129 public Ethernet customizeEthernetIPHeaders(Ethernet inPkt,
130 InboundPacket eapPacket) {
131
132 String serialNo = customInfo.deviceService().getDevice(eapPacket.
133 receivedFrom().deviceId()).serialNumber();
134
135 log.info("SampleRadiusCustomzer customizer serial = {}", serialNo);
136 SubscriberAndDeviceInformation deviceInfo = customInfo.
137 subscriberService().get(serialNo);
138
139 if (deviceInfo == null) {
140 log.warn("No Device found with SN {}", serialNo);
141 return inPkt;
142 }
143
144 inPkt.setSourceMACAddress(deviceInfo.hardwareIdentifier());
145
146 IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
147 ipv4Packet.setSourceAddress(deviceInfo.ipAddress().toString());
148 inPkt.setPayload(ipv4Packet);
149
150 return inPkt;
151 }
152}