src/internet-stack/ipv4-l3-protocol.h
author Qasim Javed <qasimj@gmail.com>
Thu Aug 06 01:55:49 2009 +0600 (2009-08-06)
changeset 4638 19aa5f9b4bdf
parent 4634 0417b4c836da
permissions -rw-r--r--
Source NAT working! Run (examples/netfilter-example.cc)
mathieu@236
     1
// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*-
mathieu@236
     2
//
mathieu@236
     3
// Copyright (c) 2006 Georgia Tech Research Corporation
mathieu@236
     4
//
mathieu@236
     5
// This program is free software; you can redistribute it and/or modify
mathieu@236
     6
// it under the terms of the GNU General Public License version 2 as
mathieu@236
     7
// published by the Free Software Foundation;
mathieu@236
     8
//
mathieu@236
     9
// This program is distributed in the hope that it will be useful,
mathieu@236
    10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@236
    11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@236
    12
// GNU General Public License for more details.
mathieu@236
    13
//
mathieu@236
    14
// You should have received a copy of the GNU General Public License
mathieu@236
    15
// along with this program; if not, write to the Free Software
mathieu@236
    16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@236
    17
//
mathieu@236
    18
// Author: George F. Riley<riley@ece.gatech.edu>
mathieu@236
    19
//
mathieu@236
    20
mathieu@752
    21
#ifndef IPV4_L3_PROTOCOL_H
mathieu@752
    22
#define IPV4_L3_PROTOCOL_H
mathieu@236
    23
mathieu@236
    24
#include <list>
mathieu@236
    25
#include <stdint.h>
mathieu@524
    26
#include "ns3/ipv4-address.h"
mathieu@555
    27
#include "ns3/ptr.h"
gjc@3448
    28
#include "ns3/net-device.h"
gjc@983
    29
#include "ns3/ipv4.h"
mathieu@2498
    30
#include "ns3/traced-callback.h"
mathieu@3216
    31
#include "ns3/ipv4-header.h"
tomh@4472
    32
#include "ns3/ipv4-routing-protocol.h"
qasimj@4634
    33
#include "ipv4-netfilter.h"
mathieu@236
    34
mathieu@236
    35
namespace ns3 {
mathieu@236
    36
mathieu@236
    37
class Packet;
mathieu@236
    38
class NetDevice;
mathieu@551
    39
class Ipv4Interface;
mathieu@236
    40
class Ipv4Address;
mathieu@236
    41
class Ipv4Header;
tomh@4472
    42
class Ipv4RoutingTableEntry;
mathieu@236
    43
class Ipv4Route;
mathieu@728
    44
class Node;
craigdo@3820
    45
class Socket;
craigdo@3820
    46
class Ipv4RawSocketImpl;
mathieu@3588
    47
class Ipv4L4Protocol;
craigdo@3820
    48
class Ipv4L4Protocol;
craigdo@3820
    49
class Icmpv4L4Protocol;
mathieu@1229
    50
mathieu@236
    51
mathieu@2961
    52
/**
mathieu@2961
    53
 * \brief Implement the Ipv4 layer.
raj@3266
    54
 * 
raj@3266
    55
 * This is the actual implementation of IP.  It contains APIs to send and
raj@3266
    56
 * receive packets at the IP layer, as well as APIs for IP routing.
mathieu@2961
    57
 */
tomh@4377
    58
class Ipv4L3Protocol : public Ipv4
mathieu@293
    59
{
mathieu@236
    60
public:
mathieu@2251
    61
  static TypeId GetTypeId (void);
mathieu@293
    62
  static const uint16_t PROT_NUMBER;
mathieu@293
    63
mathieu@2498
    64
  Ipv4L3Protocol();
mathieu@734
    65
  virtual ~Ipv4L3Protocol ();
mathieu@236
    66
mathieu@2592
    67
  void SetNode (Ptr<Node> node);
mathieu@2592
    68
tomh@4472
    69
  // functions defined in base class Ipv4
tomh@4481
    70
tomh@4481
    71
  void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
tomh@4472
    72
  Ptr<Ipv4RoutingProtocol> GetRoutingProtocol (void) const;
tomh@4472
    73
craigdo@3820
    74
  Ptr<Socket> CreateRawSocket (void);
craigdo@3820
    75
  void DeleteRawSocket (Ptr<Socket> socket);
craigdo@3820
    76
mathieu@406
    77
  /**
mathieu@3588
    78
   * \param protocol a template for the protocol to add to this L4 Demux.
mathieu@3588
    79
   * \returns the L4Protocol effectively added.
mathieu@3588
    80
   *
mathieu@3588
    81
   * Invoke Copy on the input template to get a copy of the input
mathieu@3588
    82
   * protocol which can be used on the Node on which this L4 Demux 
mathieu@3588
    83
   * is running. The new L4Protocol is registered internally as
mathieu@3588
    84
   * a working L4 Protocol and returned from this method.
mathieu@3588
    85
   * The caller does not get ownership of the returned pointer.
mathieu@3588
    86
   */
mathieu@3588
    87
  void Insert(Ptr<Ipv4L4Protocol> protocol);
mathieu@3588
    88
  /**
mathieu@3588
    89
   * \param protocolNumber number of protocol to lookup
mathieu@3588
    90
   *        in this L4 Demux
mathieu@3588
    91
   * \returns a matching L4 Protocol
mathieu@3588
    92
   *
mathieu@3588
    93
   * This method is typically called by lower layers
mathieu@3588
    94
   * to forward packets up the stack to the right protocol.
mathieu@3588
    95
   * It is also called from NodeImpl::GetUdp for example.
mathieu@3588
    96
   */
mathieu@3744
    97
  Ptr<Ipv4L4Protocol> GetProtocol(int protocolNumber) const;
mathieu@3588
    98
  /**
mathieu@3588
    99
   * \param protocol protocol to remove from this demux.
mathieu@3588
   100
   *
mathieu@3588
   101
   * The input value to this method should be the value
mathieu@3588
   102
   * returned from the Ipv4L4Protocol::Insert method.
mathieu@3588
   103
   */
mathieu@3588
   104
  void Remove (Ptr<Ipv4L4Protocol> protocol);
mathieu@3588
   105
mathieu@3588
   106
  /**
mathieu@406
   107
   * \param ttl default ttl to use
mathieu@406
   108
   *
mathieu@406
   109
   * When we need to send an ipv4 packet, we use this default
mathieu@406
   110
   * ttl value.
mathieu@406
   111
   */
mathieu@236
   112
  void SetDefaultTtl (uint8_t ttl);
mathieu@406
   113
mathieu@406
   114
  /**
mathieu@236
   115
   * Lower layer calls this method after calling L3Demux::Lookup
mathieu@236
   116
   * The ARP subclass needs to know from which NetDevice this
mathieu@236
   117
   * packet is coming to:
mathieu@236
   118
   *    - implement a per-NetDevice ARP cache
mathieu@236
   119
   *    - send back arp replies on the right device
mathieu@236
   120
   */
mathieu@3548
   121
  void Receive( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
gjc@3448
   122
                const Address &to, NetDevice::PacketType packetType);
mathieu@236
   123
mathieu@406
   124
  /**
mathieu@406
   125
   * \param packet packet to send
mathieu@406
   126
   * \param source source address of packet
mathieu@406
   127
   * \param destination address of packet
mathieu@406
   128
   * \param protocol number of packet
tomh@4472
   129
   * \param route route entry
mathieu@406
   130
   *
mathieu@406
   131
   * Higher-level layers call this method to send a packet
mathieu@406
   132
   * down the stack to the MAC and PHY layers.
mathieu@406
   133
   */
mathieu@1866
   134
  void Send (Ptr<Packet> packet, Ipv4Address source, 
tomh@4472
   135
	     Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
craigdo@1428
   136
mathieu@639
   137
  uint32_t AddInterface (Ptr<NetDevice> device);
mathieu@1341
   138
  Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
mathieu@639
   139
  uint32_t GetNInterfaces (void) const;
mathieu@639
   140
tomh@4472
   141
  int32_t GetInterfaceForAddress (Ipv4Address addr) const;
tomh@4472
   142
  int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
tomh@4472
   143
  int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
mathieu@1229
   144
tomh@4563
   145
  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
tomh@4373
   146
  Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
tomh@4373
   147
  uint32_t GetNAddresses (uint32_t interface) const;
tomh@4563
   148
  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
tomh@4373
   149
tomh@1776
   150
  void SetMetric (uint32_t i, uint16_t metric);
tomh@1776
   151
  uint16_t GetMetric (uint32_t i) const;
mathieu@444
   152
  uint16_t GetMtu (uint32_t i) const;
mathieu@444
   153
  bool IsUp (uint32_t i) const;
mathieu@444
   154
  void SetUp (uint32_t i);
mathieu@444
   155
  void SetDown (uint32_t i);
tomh@4607
   156
  bool IsForwarding (uint32_t i) const;
tomh@4607
   157
  void SetForwarding (uint32_t i, bool val);
mathieu@444
   158
tomh@4377
   159
  Ptr<NetDevice> GetNetDevice (uint32_t i);
qasimj@4638
   160
  Ipv4Netfilter* GetNetfilter();
tomh@4377
   161
mathieu@513
   162
protected:
gjc@983
   163
mathieu@513
   164
  virtual void DoDispose (void);
tomh@4472
   165
  /**
tomh@4472
   166
   * This function will notify other components connected to the node that a new stack member is now connected
tomh@4472
   167
   * This will be used to notify Layer 3 protocol of layer 4 protocol stack to connect them together.
tomh@4472
   168
   */
tomh@4472
   169
  virtual void NotifyNewAggregate ();
mathieu@513
   170
private:
tomh@4571
   171
  friend class Ipv4L3ProtocolTest;
craigdo@3820
   172
  Ipv4L3Protocol(const Ipv4L3Protocol &);
craigdo@3820
   173
  Ipv4L3Protocol &operator = (const Ipv4L3Protocol &);
tomh@4472
   174
tomh@4472
   175
  virtual void SetIpForward (bool forward);
tomh@4472
   176
  virtual bool GetIpForward (void) const;
tomh@4472
   177
tomh@4472
   178
  Ipv4Header BuildHeader (
tomh@4472
   179
            Ipv4Address source,
tomh@4472
   180
            Ipv4Address destination,
tomh@4472
   181
            uint8_t protocol,
tomh@4472
   182
            uint16_t payloadSize,
tomh@4472
   183
            uint8_t ttl,
tomh@4472
   184
            bool mayFragment);
tomh@4472
   185
tomh@4472
   186
  void
tomh@4472
   187
  SendRealOut (Ptr<Ipv4Route> route,
mathieu@1866
   188
               Ptr<Packet> packet,
tomh@4472
   189
               Ipv4Header const &ipHeader);
gjc@983
   190
tomh@4472
   191
  void 
tomh@4472
   192
  IpForward (Ptr<Ipv4Route> rtentry, 
tomh@4472
   193
             Ptr<const Packet> p, 
tomh@4472
   194
             const Ipv4Header &header);
tomh@4472
   195
tomh@4472
   196
  void
tomh@4472
   197
  IpMulticastForward (Ptr<Ipv4MulticastRoute> mrtentry, 
tomh@4472
   198
                      Ptr<const Packet> p, 
tomh@4472
   199
                      const Ipv4Header &header);
tomh@4472
   200
tomh@4472
   201
  void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
tomh@4607
   202
  void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
tomh@4607
   203
mathieu@1341
   204
  uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
mathieu@444
   205
  void SetupLoopback (void);
craigdo@3820
   206
  Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
craigdo@3820
   207
  bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
mathieu@236
   208
mathieu@1341
   209
  typedef std::list<Ptr<Ipv4Interface> > Ipv4InterfaceList;
craigdo@3820
   210
  typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
craigdo@3820
   211
  typedef std::list<Ptr<Ipv4L4Protocol> > L4List_t;
mathieu@236
   212
tomh@4472
   213
  bool m_ipForward;
mathieu@3588
   214
  L4List_t m_protocols;
mathieu@236
   215
  Ipv4InterfaceList m_interfaces;
mathieu@236
   216
  uint32_t m_nInterfaces;
mathieu@236
   217
  uint8_t m_defaultTtl;
mathieu@236
   218
  uint16_t m_identification;
mathieu@728
   219
  Ptr<Node> m_node;
mathieu@2498
   220
  TracedCallback<Ptr<const Packet>, uint32_t> m_txTrace;
mathieu@2498
   221
  TracedCallback<Ptr<const Packet>, uint32_t> m_rxTrace;
mathieu@2498
   222
  TracedCallback<Ptr<const Packet> > m_dropTrace;
gjc@983
   223
tomh@4472
   224
  Ptr<Ipv4RoutingProtocol> m_routingProtocol;
craigdo@3820
   225
craigdo@3820
   226
  SocketList m_sockets;
qasimj@4634
   227
  Ipv4Netfilter netfilter;
mathieu@236
   228
};
mathieu@236
   229
mathieu@236
   230
} // Namespace ns3
mathieu@236
   231
gjc@983
   232
#endif /* IPV4_L3_PROTOCOL_H */