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