2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.natservice.internal;
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;
22 import java.math.BigInteger;
23 import java.util.HashSet;
24 import com.google.common.primitives.Ints;
26 public class NaptPacketInHandler implements PacketProcessingListener {
28 private static final Logger LOG = LoggerFactory.getLogger(NaptPacketInHandler.class);
29 private final static HashSet<String> incomingPacketMap = new HashSet<>();
30 private final EventDispatcher naptEventdispatcher;
32 public NaptPacketInHandler(EventDispatcher eventDispatcher) {
33 this.naptEventdispatcher = eventDispatcher;
37 public void onPacketReceived(PacketReceived packetReceived) {
38 String internalIPAddress = "";
41 NAPTEntryEvent.Operation operation = NAPTEntryEvent.Operation.ADD;
42 NAPTEntryEvent.Protocol protocol;
44 Short tableId = packetReceived.getTableId().getValue();
46 if (LOG.isTraceEnabled()) {
47 LOG.trace("packet: {}, tableId {}", packetReceived, tableId);
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) {
56 ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NumBitsInAByte);
57 } catch (Exception e) {
58 LOG.warn("Failed to decode Packet", e);
61 if (ethPkt.getPayload() instanceof IPv4) {
62 IPv4 ipPkt = (IPv4) ethPkt.getPayload();
63 byte[] ipSrc = Ints.toByteArray(ipPkt.getSourceAddress());
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);
78 LOG.error("Incoming Packet is neither TCP or UDP packet");
82 LOG.error("Incoming Packet is not IPv4 packet");
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);
92 BigInteger metadata = packetReceived.getMatch().getMetadata().getMetadata();
93 routerId = (metadata.and(MetaDataUtil.METADATA_MASK_VRFID)).longValue();
95 LOG.error("Nat Service : Router ID is invalid");
99 NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(internalIPAddress,portNumber,routerId,
101 naptEventdispatcher.addNaptEvent(naptEntryEvent);
103 LOG.trace("Ignore the packet, already processed");
106 LOG.error("Nullpointer exception in retrieving internalIPAddress");
110 LOG.trace("Packet is not from the Outbound NAPT table");
114 public void removeIncomingPacketMap(String sourceIPPortKey) {
115 incomingPacketMap.remove(sourceIPPortKey);
116 LOG.debug("NAT Service : sourceIPPortKey {} mapping is removed from map", sourceIPPortKey);