1
2
3
4
5
6
7
8
9 package org.opendaylight.lispflowmapping.southbound.lisp;
10
11 import io.netty.buffer.ByteBufUtil;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.SimpleChannelInboundHandler;
15 import io.netty.channel.socket.DatagramPacket;
16
17 import java.net.InetAddress;
18 import java.nio.ByteBuffer;
19
20 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
21 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
22 import org.opendaylight.lispflowmapping.southbound.LispSouthboundStats;
23 import org.opendaylight.lispflowmapping.southbound.util.LispNotificationHelper;
24 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
25 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
26 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
27 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
28 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
29 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
30 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
31 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
32 import org.opendaylight.lispflowmapping.southbound.lisp.network.PacketHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMappingBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotifyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReplyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMappingBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @ChannelHandler.Sharable
48 public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramPacket> implements ILispSouthboundService {
49 private NotificationPublishService notificationPublishService;
50 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundHandler.class);
51
52 private final LispSouthboundPlugin lispSbPlugin;
53 private LispSouthboundStats lispSbStats = null;
54
55 public LispSouthboundHandler(LispSouthboundPlugin lispSbPlugin) {
56 this.lispSbPlugin = lispSbPlugin;
57 if (lispSbPlugin != null) {
58 this.lispSbStats = lispSbPlugin.getStats();
59 }
60 }
61
62 public void setNotificationProvider(NotificationPublishService nps) {
63 this.notificationPublishService = nps;
64 }
65
66 public void handlePacket(DatagramPacket msg) {
67 ByteBuffer inBuffer = msg.content().nioBuffer();
68 int type = ByteUtil.getUnsignedByte(inBuffer, LispMessage.Pos.TYPE) >> 4;
69 handleStats(type);
70 Object lispType = MessageType.forValue(type);
71 if (lispType == MessageType.EncapsulatedControlMessage) {
72 LOG.trace("Received packet of type Encapsulated Control Message");
73 handleEncapsulatedControlMessage(inBuffer, msg.sender().getAddress());
74 } else if (lispType == MessageType.MapRequest) {
75 LOG.trace("Received packet of type Map-Request");
76 handleMapRequest(inBuffer, msg.sender().getPort());
77 } else if (lispType == MessageType.MapRegister) {
78 LOG.trace("Received packet of type Map-Register");
79 handleMapRegister(inBuffer, msg.sender().getAddress(), msg.sender().getPort());
80 } else if (lispType == MessageType.MapNotify) {
81 LOG.trace("Received packet of type Map-Notify");
82 handleMapNotify(inBuffer, msg.sender().getAddress(), msg.sender().getPort());
83 } else if (lispType == MessageType.MapReply) {
84 LOG.trace("Received packet of type Map-Reply");
85 handleMapReply(inBuffer, msg.sender().getAddress(), msg.sender().getPort());
86 } else {
87 LOG.warn("Received unknown LISP control packet (type " + ((lispType != null) ? lispType : type) + ")");
88 }
89 }
90
91 private void handleEncapsulatedControlMessage(ByteBuffer inBuffer, InetAddress sourceAddress) {
92 try {
93 handleMapRequest(inBuffer, extractEncapsulatedSourcePort(inBuffer));
94 } catch (RuntimeException re) {
95 throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
96 }
97 }
98
99 private void handleMapRequest(ByteBuffer inBuffer, int port) {
100 try {
101 MapRequest request = MapRequestSerializer.getInstance().deserialize(inBuffer);
102 InetAddress finalSourceAddress = MapRequestUtil.selectItrRloc(request);
103 if (finalSourceAddress == null) {
104 throw new LispMalformedPacketException("Couldn't deserialize Map-Request, no ITR Rloc found!");
105 }
106
107 RequestMappingBuilder requestMappingBuilder = new RequestMappingBuilder();
108 requestMappingBuilder.setMapRequest(LispNotificationHelper.convertMapRequest(request));
109 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
110 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(finalSourceAddress));
111 transportAddressBuilder.setPort(new PortNumber(port));
112 requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
113 if (notificationPublishService != null) {
114 notificationPublishService.putNotification(requestMappingBuilder.build());
115 LOG.trace("MapRequest was published!");
116 } else {
117 LOG.warn("Notification Provider is null!");
118 }
119 } catch (RuntimeException re) {
120 throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
121 } catch (InterruptedException e) {
122 LOG.warn("Notification publication interrupted!");
123 }
124 }
125
126 private int extractEncapsulatedSourcePort(ByteBuffer inBuffer) {
127 try {
128 inBuffer.position(PacketHeader.Length.LISP_ENCAPSULATION);
129 int ipType = (inBuffer.get() >> 4);
130 if (ipType == 4) {
131 inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV4 - 1);
132 } else if (ipType == 6) {
133 inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV6_NO_EXT - 1);
134 } else {
135 throw new LispMalformedPacketException("Couldn't deserialize Map-Request: inner packet has unknown IP version: " + ipType);
136 }
137
138 int encapsulatedSourcePort = inBuffer.getShort() & 0xFFFF;
139 inBuffer.position(inBuffer.position() + PacketHeader.Length.UDP - 2);
140 return encapsulatedSourcePort;
141 } catch (RuntimeException re) {
142 throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
143 }
144 }
145
146 private void handleMapRegister(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
147 try {
148 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress);
149 AddMappingBuilder addMappingBuilder = new AddMappingBuilder();
150 addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister));
151 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
152 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
153 transportAddressBuilder.setPort(new PortNumber(port));
154 addMappingBuilder.setTransportAddress(transportAddressBuilder.build());
155 if (notificationPublishService != null) {
156 notificationPublishService.putNotification(addMappingBuilder.build());
157 LOG.trace("MapRegister was published!");
158 } else {
159 LOG.warn("Notification Provider is null!");
160 }
161 } catch (RuntimeException re) {
162 throw new LispMalformedPacketException("Couldn't deserialize Map-Register (len=" + inBuffer.capacity() + ")", re);
163 } catch (InterruptedException e) {
164 LOG.warn("Notification publication interrupted!");
165 }
166 }
167
168 private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
169 try {
170 MapNotify mapNotify = MapNotifySerializer.getInstance().deserialize(inBuffer);
171 GotMapNotifyBuilder gotMapNotifyBuilder = new GotMapNotifyBuilder();
172 gotMapNotifyBuilder.setMapNotify(LispNotificationHelper.convertMapNotify(mapNotify));
173 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
174 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
175 transportAddressBuilder.setPort(new PortNumber(port));
176 gotMapNotifyBuilder.setTransportAddress(transportAddressBuilder.build());
177 if (notificationPublishService != null) {
178 notificationPublishService.putNotification(gotMapNotifyBuilder.build());
179 LOG.trace("MapNotify was published!");
180 } else {
181 LOG.warn("Notification Provider is null!");
182 }
183 } catch (RuntimeException re) {
184 throw new LispMalformedPacketException("Couldn't deserialize Map-Notify (len=" + inBuffer.capacity() + ")", re);
185 } catch (InterruptedException e) {
186 LOG.warn("Notification publication interrupted!");
187 }
188 }
189
190 private void handleMapReply(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
191 try {
192 MapReply mapReply = MapReplySerializer.getInstance().deserialize(inBuffer);
193 GotMapReplyBuilder gotMapReplyBuilder = new GotMapReplyBuilder();
194 gotMapReplyBuilder.setMapReply(LispNotificationHelper.convertMapReply(mapReply));
195 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
196 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
197 transportAddressBuilder.setPort(new PortNumber(port));
198 gotMapReplyBuilder.setTransportAddress(transportAddressBuilder.build());
199 if (notificationPublishService != null) {
200 notificationPublishService.putNotification(gotMapReplyBuilder.build());
201 LOG.trace("MapReply was published!");
202 } else {
203 LOG.warn("Notification Provider is null!");
204 }
205 } catch (RuntimeException re) {
206 throw new LispMalformedPacketException("Couldn't deserialize Map-Reply (len=" + inBuffer.capacity() + ")", re);
207 } catch (InterruptedException e) {
208 LOG.warn("Notification publication interrupted!");
209 }
210 }
211
212 private void handleStats(int type) {
213 if (lispSbStats != null) {
214 if (type <= LispSouthboundStats.MAX_LISP_TYPES) {
215 lispSbStats.incrementRx(type);
216 } else {
217 lispSbStats.incrementRxUnknown();
218 }
219 }
220 }
221
222 @Override
223 protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
224 if (LOG.isTraceEnabled()) {
225 LOG.trace("Received UDP packet from {}:{} with content:\n{}", msg.sender().getHostString(),
226 msg.sender().getPort(), ByteBufUtil.prettyHexDump(msg.content()));
227 }
228 handlePacket(msg);
229 }
230
231 @Override
232 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
233 ctx.flush();
234 }
235
236 @Override
237 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
238 LOG.error("Error on channel: " + cause, cause);
239 }
240 }