Support for SNAT and DNAT features in L3 forwarding services.
[vpnservice.git] / natservice / natservice-impl / src / main / java / org / opendaylight / vpnservice / natservice / internal / NaptPacketInHandler.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.natservice.internal;
9
10 import org.opendaylight.controller.liblldp.NetUtils;
11 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
12 import org.opendaylight.vpnservice.mdsalutil.NWUtil;
13 import org.opendaylight.vpnservice.mdsalutil.packet.Ethernet;
14 import org.opendaylight.vpnservice.mdsalutil.packet.IPv4;
15 import org.opendaylight.vpnservice.mdsalutil.packet.TCP;
16 import org.opendaylight.vpnservice.mdsalutil.packet.UDP;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.math.BigInteger;
23 import java.util.HashSet;
24 import com.google.common.primitives.Ints;
25
26 public class NaptPacketInHandler implements PacketProcessingListener {
27
28     private static final Logger LOG = LoggerFactory.getLogger(NaptPacketInHandler.class);
29     private final static HashSet<String> incomingPacketMap = new HashSet<>();
30     private final EventDispatcher naptEventdispatcher;
31
32     public NaptPacketInHandler(EventDispatcher eventDispatcher) {
33         this.naptEventdispatcher = eventDispatcher;
34     }
35
36     @Override
37     public void onPacketReceived(PacketReceived packetReceived) {
38         String internalIPAddress = "";
39         int portNumber = 0;
40         long routerId = 0L;
41         NAPTEntryEvent.Operation operation = NAPTEntryEvent.Operation.ADD;
42         NAPTEntryEvent.Protocol protocol;
43
44         Short tableId = packetReceived.getTableId().getValue();
45
46         if (LOG.isTraceEnabled()) {
47             LOG.trace("packet: {}, tableId {}", packetReceived, tableId);
48         }
49
50         if (tableId == NatConstants.OUTBOUND_NAPT_TABLE) {
51             LOG.debug("NAT Service : NAPTPacketInHandler Packet for Outbound NAPT Table");
52             byte[] inPayload = packetReceived.getPayload();
53             Ethernet ethPkt = new Ethernet();
54             if (inPayload != null) {
55                 try {
56                     ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NumBitsInAByte);
57                 } catch (Exception e) {
58                     LOG.warn("Failed to decode Packet", e);
59                     return;
60                 }
61                 if (ethPkt.getPayload() instanceof IPv4) {
62                     IPv4 ipPkt = (IPv4) ethPkt.getPayload();
63                     byte[] ipSrc = Ints.toByteArray(ipPkt.getSourceAddress());
64
65                     internalIPAddress = NatUtil.toStringIpAddress(ipSrc, LOG);
66                     LOG.trace("Retrieved internalIPAddress {}", internalIPAddress);
67                     if (ipPkt.getPayload() instanceof TCP) {
68                         TCP tcpPkt = (TCP) ipPkt.getPayload();
69                         portNumber = tcpPkt.getSourcePort();
70                         protocol = NAPTEntryEvent.Protocol.TCP;
71                         LOG.trace("Retrieved TCP portNumber {}", portNumber);
72                     } else if (ipPkt.getPayload() instanceof UDP) {
73                         UDP udpPkt = (UDP) ipPkt.getPayload();
74                         portNumber = udpPkt.getSourcePort();
75                         protocol = NAPTEntryEvent.Protocol.UDP;
76                         LOG.trace("Retrieved UDP portNumber {}", portNumber);
77                     } else {
78                         LOG.error("Incoming Packet is neither TCP or UDP packet");
79                         return;
80                     }
81                 } else {
82                     LOG.error("Incoming Packet is not IPv4 packet");
83                     return;
84                 }
85
86                 if(internalIPAddress != null) {
87                     String sourceIPPortKey = internalIPAddress + ":" + portNumber;
88                     LOG.debug("NAT Service : sourceIPPortKey {} mapping maintained in the map", sourceIPPortKey);
89                     if (!incomingPacketMap.contains(sourceIPPortKey)) {
90                         incomingPacketMap.add(internalIPAddress + portNumber);
91
92                         BigInteger metadata = packetReceived.getMatch().getMetadata().getMetadata();
93                         routerId = (metadata.and(MetaDataUtil.METADATA_MASK_VRFID)).longValue();
94                         if( routerId <= 0) {
95                             LOG.error("Nat Service : Router ID is invalid");
96                             return;
97                         }
98                         //send to Event Queue
99                         NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(internalIPAddress,portNumber,routerId,
100                                 operation,protocol);
101                         naptEventdispatcher.addNaptEvent(naptEntryEvent);
102                     } else {
103                         LOG.trace("Ignore the packet, already processed");
104                     }
105                 }else {
106                     LOG.error("Nullpointer exception in retrieving internalIPAddress");
107                 }
108             }
109         }else {
110             LOG.trace("Packet is not from the Outbound NAPT table");
111         }
112     }
113
114     public void removeIncomingPacketMap(String sourceIPPortKey) {
115         incomingPacketMap.remove(sourceIPPortKey);
116         LOG.debug("NAT Service : sourceIPPortKey {} mapping is removed from map", sourceIPPortKey);
117     }
118 }