1.2.4.5 IPv4 Module

IP is the workhorse protocol of the TCP/IP protocol suite. All TCP, UDP, and ICMP data gets transmitted as IP datagrams. IP provides an unreliable, connectionless datagram delivery service.

IP provides a best effort service. When something goes wrong, such as a router temporarily running out of buffers, IP has a simple error handling algorithm: throw away the datagram and try to send an ICMP message back to the source. Any required reliability must be provided by the upper layers (e.g., TCP).

The term connectionless means that IP does not maintain any state information about successive datagrams. Each datagram is handled independently from all other datagrams. This also means that IP datagrams can get delivered out of order. If a source sends two consecutive datagrams (first A, and then B) to the same destination, each is routed independently and can take different routes, with B arriving before A.

Abstraction Model

This module provides software abstraction of the IPv4 module existent in any TCP/IP Stack implementation.

The typical usage of the IP layer_in a TCP/IP stack for transmitting a packet is shown in the following diagram:

TCPIP IPv4 ip_abstraction

Core Functionality

TCPIP IPV4 ip_flow

To transmit an IP packet over the network, the application has to prepare an IPV4_PACKET data structure and properly format it so that the recipients of this packet (the IP and the MAC layers) can process the packet.

The TCP/IP Stack modules allocate the IPV4_PACKET data structures dynamically using the TCP/IP Stack private heap using packet allocation functions.

Note: Currently, the packet and heap allocation functions are not exposed as public APIs as they are meant for stack’s private usage. This may change in the future.

The IPV4_PACKET packet can be allocated statically by the application.

When the IPV4_PACKET is constructed an important thing to remember is to fill in the appropriate packet acknowledge function. Once the MAC layer/driver is done with processing the packet (successfully or not) it will call the IPV4_PACKET acknowledge function, indicating that the packet is now available (and could be freed or reused).

The next step is to fill in the packet source and destination addresses, as well as the outgoing interface (please keep in mind that the TCP/IP stack supports multiple network interfaces).

This could be done with a call to TCPIP_IPV4_SelectSourceInterface or the application can select all these addresses manually.

All that’s left before handing the packet is to call TCPIP_IPV4_PacketFormatTx. This call fills in the supplied packet with the IP header structure and updates the IP checksum. Please note that this function expects a properly formatted packet, with the source and destination addresses filled in.

After this, the application can call TCPIP_IPV4_PacketTransmit. This function does some basic packet checks and expects that the packet has a valid network interface selected otherwise the call will fail.

After further formatting (with the MAC required headers), the IP layer_will try to send the packet over the network. If the destination hardware address is known (the ARP resolve call succeeds) the packet is handed over to the MAC. Otherwise, the IP will insert the packet into a queue, waiting for the ARP resolution. When ARP signals that the request is done (either success or timeout) the IP layer_intercepts the signal and removes the packet from the wait queue: either hands it over to the MAC for transmission or it discards it if the ARP resolution failed.

The MAC layer/driver in its turn can transmit it immediately, queue the packet for transmission or discard it (if, for example the network link is down). Regardless, once the packet is processed, the packet acknowledge function is called with an updated acknowledge result. This informs the owner of the packet about the result of the operation and also that the packet is no longer in traffic.

Note: Transmission of chained packets is not supported. Each packet has to be transmitted individually.

How the Library Works

On the transmit side, the IP layer_is responsible for routing and forwarding the IP datagrams to the required destination host. To do that, the IP layer_has internal functionality that allows the selection of the proper network interface and destination hardware address of a packet.

Based on the destination of a packet/datagram, the IP layer_will send the packet to either a host on the network or to a gateway if either is available. To accomplish this, the IP module works closely with the ARP layer_from which it requests the destination hardware addresses for outgoing packets. If the destination address of a packet is not known, the IP module will request an ARP probe and will wait for the ARP module reply. Whenever ARP signals that the solicited address is available, the IP module will send the packet over the network. If a time-out is reported (no such host could be found on that particular network), the packet is silently discarded.

On the receive side, the IP module processes all the incoming IPv4 packets and performs a basic sanity check of the packet checksum and other attributes. If the packet check fails, the packet is simply discarded. Otherwise, it is dispatched for further processing to the appropriate module (UDP, TCP, ICMP, etc.).

The IP layer_operation within the TCP/IP stack is transparent to the applications using the preferred_socket_API (MCHP UDP, TCP or BSD). This means that normally an application does not need to interface directly to the IP_layer. However, the IP module exposes an API that can be used by the application for sending IP datagrams directly, bypassing the normal higher layer_protocols (UDP, TCP).

Notes:

  • Currently, it is not possible for an application to intercept the incoming IP datagrams and interfere with the default IP processing. However, this may change in the future and support can be added for the receive side.

  • For a detailed description of the IP layer_functionality, please consult RFC 791, RFC 1122, RFC 2474, etc.

  • This implementation supports packet fragmentation both on transmit and receive side.

Library Interface

NameDescription
Macros
TCPIP_IPV4_FRAGMENT_MAX_NUMBERUpper limit for the number of fragments in a fragmented stream This also imposes a limit on how large a packet could be depending on the MTU and fragmentations that occur If more fragments received for a certain stream, the stream will be discarded
TCPIP_IPV4_FRAGMENT_MAX_STREAMSUpper limit for the number of fragmented streams that could be handled simultaneously
TCPIP_IPV4_FRAGMENT_TIMEOUTInitial fragment timeout, in seconds Timer Lower Bound (RFC 791) The RFC states this value to be 15 seconds
TCPIP_IPV4_FRAGMENTATIONIf 1, enable IPv4 fragmentation processing, RX + TX If 0 (default), IPv4 fragmentation not supported
TCPIP_IPV4_TASK_TICK_RATEThe IP task processing rate: number of milliseconds to generate an IP tick. This is the tick that advances the IP state machine. The default value is around 30 milliseconds. The lower the rate (higher the frequency) the higher the module priority and higher module performance can be obtained The value cannot be lower than the TCPIP_STACK_TICK_RATE. The IP state machine tick is used only when fragmentation is enabled.
IP_CONFIG_HThis is macro IP_CONFIG_H.
TCPIP_IPV4_EXTERN_PACKET_PROCESSThis setting enables/disables the processing of the RX packets by an external handler The user of the IPv4 can register an external function to process the incoming packets If true, the functionality is built in and could be used by the application If false, the functionality does not exist and the generated code is slightly smaller
Functions
TCPIP_IPV4_PacketGetDestAddressReturns the IPv4 destination address associated with a TCPIP_MAC_PACKET
TCPIP_IPV4_PacketGetSourceAddressReturns the IPv4 source address associated with a TCPIP_MAC_PACKET
TCPIP_IPV4_PacketTransmitTransmits an IPV4 packet over the network.
TCPIP_IPV4_SelectSourceInterfaceSelects a source address and an interface based on the IPv4 destination address
TCPIP_IPV4_PacketFilterClearClears the IPV4 packet filters
TCPIP_IPV4_PacketFilterSetSets the IPV4 packet filters
TCPIP_IPV4_TaskStandard TCP/IP stack module task function.
TCPIP_IPV4_MacPacketTransmitTransmits a fully constructed MAC packet over the network.
TCPIP_IPV4_PacketAllocAllocates an IPv4 packet for transmission over the network.
TCPIP_IPV4_PacketFormatTxFormats an IPV4 packet and makes it ready for transmission.
TCPIP_IPV4_PacketOptionFieldSetHelper to set an IPv4 packet option field.
TCPIP_IPV4_PacketOptionGetHelper to get the options that a IPv4 receive packet has.
TCPIP_IPV4_PacketOptionListGetHelper to get the options that a IPv4 receive packet has.
TCPIP_IPV4_IsFragmentationEnabledHelper to get the IPv4 fragmentation support.
TCPIP_IPV4_MaxDatagramDataSizeGetHelper to get the maximum datagream data size (MDDS) on a specific interface.
TCPIP_IPV4_PacketHandlerDeregisterDeregisters a previously registered packet handler.
TCPIP_IPV4_PacketHandlerRegisterSets a new packet processing handler.
Data Types and Constants
IPV4_HEADER_TYPEList of supported protocols.
IPV4_PACKETIPv4 packet structure.
TCPIP_IPV4_FILTER_TYPEList of supported IPv4 packet filters.
TCPIP_IPV4_MODULE_CONFIGInitialization parameters supported by IPv4
IPV4_FRAGMENT_INFOStructure of an IPv4 fragment info.
IPV4_OPTION_FIELDStructure of an IPv4 option.
IPV4_TYPE_OF_SERVICEStructure of an IPv4 type of service field.
TCPIP_IPV4_OPTION_DCPTStructure describing the options for an IPv4 packet
TCPIP_IPV4_OPTION_TYPEList of supported IPv4 options
TCPIP_IPV4_PACKET_PARAMSIPv4 packet formatting parameters
TCPIP_IPV4_PACKET_HANDLERPointer to a function(handler) that will get called to process an incoming IPv4 packet.
TCPIP_IPV4_PROCESS_HANDLEDefines a IPv4 packet processing handle.