A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Jan 11 17:38:21 2018 +0000
Revision:
61:aad055f1b0d1
Parent:
ip4/ip4.cpp@59:e0e556c8bd46
Child:
71:736a5747ade1
Removed dependence on Mbed OS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 43:bc028d5a6424 4 #include "log.h"
andrewboyson 43:bc028d5a6424 5 #include "net.h"
andrewboyson 43:bc028d5a6424 6 #include "action.h"
andrewboyson 43:bc028d5a6424 7 #include "icmp4.h"
andrewboyson 10:f0854784e960 8 #include "udptcp4.h"
andrewboyson 48:952dddb74b8b 9 #include "ar4.h"
andrewboyson 50:492f2d2954e4 10 #include "nr4.h"
andrewboyson 43:bc028d5a6424 11 #include "dhcp.h"
andrewboyson 43:bc028d5a6424 12 #include "eth.h"
andrewboyson 43:bc028d5a6424 13 #include "ip.h"
andrewboyson 49:1a6336f2b3f9 14 #include "ip4addr.h"
andrewboyson 43:bc028d5a6424 15 #include "ntp.h"
andrewboyson 43:bc028d5a6424 16 #include "mac.h"
andrewboyson 10:f0854784e960 17
andrewboyson 48:952dddb74b8b 18 bool Ip4Trace = true;
andrewboyson 10:f0854784e960 19
andrewboyson 53:77f8a49adf89 20 #define OFF_LINK_TTL 64
andrewboyson 53:77f8a49adf89 21
andrewboyson 10:f0854784e960 22 __packed struct header
andrewboyson 10:f0854784e960 23 {
andrewboyson 10:f0854784e960 24 uint8_t versionIhl;
andrewboyson 10:f0854784e960 25 uint8_t tos;
andrewboyson 10:f0854784e960 26 uint16_t length;
andrewboyson 10:f0854784e960 27 uint16_t id;
andrewboyson 10:f0854784e960 28 uint16_t flagsOffset;
andrewboyson 10:f0854784e960 29 uint8_t ttl;
andrewboyson 10:f0854784e960 30 uint8_t protocol;
andrewboyson 10:f0854784e960 31 uint16_t checksum;
andrewboyson 10:f0854784e960 32 uint32_t src;
andrewboyson 10:f0854784e960 33 uint32_t dst;
andrewboyson 10:f0854784e960 34 };
andrewboyson 10:f0854784e960 35
andrewboyson 10:f0854784e960 36 //Header variables
andrewboyson 10:f0854784e960 37 static uint8_t version;
andrewboyson 10:f0854784e960 38 static int headerLength;
andrewboyson 10:f0854784e960 39 static uint8_t tos;
andrewboyson 59:e0e556c8bd46 40 static uint16_t totalLength;
andrewboyson 10:f0854784e960 41 static uint16_t id;
andrewboyson 10:f0854784e960 42 static bool dontFragment;
andrewboyson 10:f0854784e960 43 static bool moreFragments;
andrewboyson 10:f0854784e960 44 static uint16_t offset;
andrewboyson 10:f0854784e960 45 static uint8_t ttl;
andrewboyson 10:f0854784e960 46 static uint8_t protocol;
andrewboyson 10:f0854784e960 47 static uint16_t checksum;
andrewboyson 10:f0854784e960 48 static uint16_t calcsum;
andrewboyson 35:93c39d260a83 49 static uint32_t srcIp;
andrewboyson 35:93c39d260a83 50 static uint32_t dstIp;
andrewboyson 10:f0854784e960 51
andrewboyson 59:e0e556c8bd46 52 static void readHeader(struct header * pHeader)
andrewboyson 10:f0854784e960 53 {
andrewboyson 10:f0854784e960 54 version = pHeader->versionIhl >> 4;
andrewboyson 10:f0854784e960 55 uint8_t ihl = pHeader->versionIhl & 0xF;
andrewboyson 10:f0854784e960 56 headerLength = ihl * 4;
andrewboyson 10:f0854784e960 57 tos = pHeader->tos;
andrewboyson 59:e0e556c8bd46 58 totalLength = NetToHost16(pHeader->length);
andrewboyson 10:f0854784e960 59 id = NetToHost16(pHeader->id);
andrewboyson 10:f0854784e960 60 uint16_t flagsOffset = NetToHost16(pHeader->flagsOffset);
andrewboyson 10:f0854784e960 61 dontFragment = flagsOffset & 0x4000;
andrewboyson 10:f0854784e960 62 moreFragments = flagsOffset & 0x8000;
andrewboyson 10:f0854784e960 63 offset = flagsOffset & 0x1FFF;
andrewboyson 10:f0854784e960 64 ttl = pHeader->ttl;
andrewboyson 10:f0854784e960 65 protocol = pHeader->protocol;
andrewboyson 10:f0854784e960 66 checksum = NetToHost16(pHeader->checksum);
andrewboyson 10:f0854784e960 67 calcsum = NetCheckSum(headerLength, pHeader);
andrewboyson 35:93c39d260a83 68 srcIp = pHeader->src;
andrewboyson 35:93c39d260a83 69 dstIp = pHeader->dst;
andrewboyson 10:f0854784e960 70 }
andrewboyson 59:e0e556c8bd46 71 static void writeHeader(struct header * pHeader)
andrewboyson 10:f0854784e960 72 {
andrewboyson 10:f0854784e960 73 uint16_t flagsOffset = offset;
andrewboyson 10:f0854784e960 74 if (dontFragment) flagsOffset |= 0x4000;
andrewboyson 10:f0854784e960 75 if (moreFragments) flagsOffset |= 0x8000;
andrewboyson 10:f0854784e960 76
andrewboyson 10:f0854784e960 77 uint8_t ihl = headerLength >> 2;
andrewboyson 10:f0854784e960 78 pHeader->versionIhl = (version << 4) + ihl;
andrewboyson 10:f0854784e960 79 pHeader->tos = tos;
andrewboyson 10:f0854784e960 80 pHeader->id = NetToHost16(id);
andrewboyson 10:f0854784e960 81 pHeader->flagsOffset = NetToHost16(flagsOffset);
andrewboyson 10:f0854784e960 82 pHeader->ttl = ttl;
andrewboyson 10:f0854784e960 83 pHeader->protocol = protocol;
andrewboyson 10:f0854784e960 84
andrewboyson 35:93c39d260a83 85 pHeader->dst = dstIp;
andrewboyson 35:93c39d260a83 86 pHeader->src = srcIp;
andrewboyson 59:e0e556c8bd46 87 pHeader->length = NetToHost16(totalLength);
andrewboyson 10:f0854784e960 88 pHeader->checksum = 0;
andrewboyson 10:f0854784e960 89 pHeader->checksum = NetCheckSum(headerLength, pHeader);
andrewboyson 10:f0854784e960 90 calcsum = 0;
andrewboyson 10:f0854784e960 91 }
andrewboyson 11:c051adb70c5a 92
andrewboyson 37:793b39683406 93 static void logHeader()
andrewboyson 11:c051adb70c5a 94 {
andrewboyson 43:bc028d5a6424 95 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 96 {
andrewboyson 44:83ce5ace337b 97 Log ("IP4 header\r\n");
andrewboyson 43:bc028d5a6424 98 LogF(" Version %d\r\n", version);
andrewboyson 43:bc028d5a6424 99 LogF(" Header length %d\r\n", headerLength);
andrewboyson 43:bc028d5a6424 100 LogF(" Type of service %d\r\n", tos);
andrewboyson 59:e0e556c8bd46 101 LogF(" Total length %d\r\n", totalLength);
andrewboyson 43:bc028d5a6424 102 LogF(" Identification %d\r\n", id);
andrewboyson 43:bc028d5a6424 103 if (dontFragment) LogF(" Don't fragment\r\n");
andrewboyson 43:bc028d5a6424 104 else LogF(" Do fragment\r\n");
andrewboyson 43:bc028d5a6424 105 if (moreFragments) LogF(" More fragments\r\n");
andrewboyson 43:bc028d5a6424 106 else LogF(" No more fragments\r\n");
andrewboyson 43:bc028d5a6424 107 LogF(" Offset %d\r\n", offset);
andrewboyson 43:bc028d5a6424 108 LogF(" Time to live %d\r\n", ttl);
andrewboyson 47:73af5c0b0dc2 109 LogF(" Protocol "); IpProtocolLog(protocol); Log("\r\n");
andrewboyson 43:bc028d5a6424 110 LogF(" Checksum (hex) %04hX\r\n", checksum);
andrewboyson 43:bc028d5a6424 111 LogF(" Calculated (hex) %04hX\r\n", calcsum);
andrewboyson 47:73af5c0b0dc2 112 LogF(" Source IP "); Ip4AddressLog(srcIp); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 113 LogF(" Destination IP "); Ip4AddressLog(dstIp); Log("\r\n");
andrewboyson 43:bc028d5a6424 114 }
andrewboyson 43:bc028d5a6424 115 else
andrewboyson 43:bc028d5a6424 116 {
andrewboyson 44:83ce5ace337b 117 Log ("IP4 header ");
andrewboyson 47:73af5c0b0dc2 118 IpProtocolLog(protocol);
andrewboyson 43:bc028d5a6424 119 Log(" ");
andrewboyson 47:73af5c0b0dc2 120 Ip4AddressLog(srcIp);
andrewboyson 43:bc028d5a6424 121 Log(" >>> ");
andrewboyson 47:73af5c0b0dc2 122 Ip4AddressLog(dstIp);
andrewboyson 43:bc028d5a6424 123 Log("\r\n");
andrewboyson 43:bc028d5a6424 124 }
andrewboyson 11:c051adb70c5a 125 }
andrewboyson 37:793b39683406 126 static void (*pTraceBack)(void);
andrewboyson 37:793b39683406 127 static void trace()
andrewboyson 10:f0854784e960 128 {
andrewboyson 37:793b39683406 129 pTraceBack();
andrewboyson 37:793b39683406 130 logHeader();
andrewboyson 37:793b39683406 131 }
andrewboyson 59:e0e556c8bd46 132 int Ip4HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* macRemote)
andrewboyson 37:793b39683406 133 {
andrewboyson 37:793b39683406 134 pTraceBack = traceback;
andrewboyson 61:aad055f1b0d1 135 struct header * pHeaderRx = (struct header*)pPacketRx;
andrewboyson 61:aad055f1b0d1 136 struct header * pHeaderTx = (struct header*)pPacketTx;
andrewboyson 59:e0e556c8bd46 137
andrewboyson 59:e0e556c8bd46 138 char* pDataRx = (char*)pHeaderRx + headerLength;
andrewboyson 59:e0e556c8bd46 139 char* pDataTx = (char*)pHeaderTx + headerLength;
andrewboyson 59:e0e556c8bd46 140
andrewboyson 59:e0e556c8bd46 141 readHeader(pHeaderRx);
andrewboyson 59:e0e556c8bd46 142 if (totalLength < sizeRx) sizeRx = totalLength;
andrewboyson 59:e0e556c8bd46 143 int dataLengthRx = sizeRx - headerLength;
andrewboyson 61:aad055f1b0d1 144 int dataLengthTx = *pSizeTx - sizeof(struct header);
andrewboyson 10:f0854784e960 145
andrewboyson 61:aad055f1b0d1 146 bool isMe = dstIp == DhcpLocalIp;
andrewboyson 61:aad055f1b0d1 147 bool isLocalBroadcast = dstIp == (DhcpLocalIp | 0xFF000000); // dstIp == 192.168.1.255; '|' is lower precendence than '=='
andrewboyson 61:aad055f1b0d1 148 bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS; // dstIp == 255.255.255.255
andrewboyson 37:793b39683406 149 bool isMulticast = (dstIp & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian
andrewboyson 10:f0854784e960 150
andrewboyson 15:6ca6778168b1 151 bool doIt = isMe || isLocalBroadcast || isBroadcast || isMulticast;
andrewboyson 15:6ca6778168b1 152 if (!doIt)
andrewboyson 15:6ca6778168b1 153 {
andrewboyson 48:952dddb74b8b 154 if (Ip4Trace);
andrewboyson 15:6ca6778168b1 155 {
andrewboyson 47:73af5c0b0dc2 156 LogTimeF("IP4 filtered out ip "); Ip4AddressLog(dstIp);
andrewboyson 47:73af5c0b0dc2 157 Log(" from ");
andrewboyson 47:73af5c0b0dc2 158 Ip4AddressLog(srcIp);
andrewboyson 47:73af5c0b0dc2 159 Log("\r\n");
andrewboyson 15:6ca6778168b1 160 }
andrewboyson 15:6ca6778168b1 161 return DO_NOTHING;
andrewboyson 15:6ca6778168b1 162 }
andrewboyson 10:f0854784e960 163
andrewboyson 50:492f2d2954e4 164 if (srcIp)
andrewboyson 50:492f2d2954e4 165 {
andrewboyson 59:e0e556c8bd46 166 Ar4AddIpRecord(trace, macRemote, srcIp);
andrewboyson 50:492f2d2954e4 167 Nr4MakeRequestForNameFromIp(srcIp);
andrewboyson 50:492f2d2954e4 168 }
andrewboyson 59:e0e556c8bd46 169
andrewboyson 10:f0854784e960 170 int action = DO_NOTHING;
andrewboyson 10:f0854784e960 171 switch (protocol)
andrewboyson 10:f0854784e960 172 {
andrewboyson 59:e0e556c8bd46 173 case ICMP: action = Icmp4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, &srcIp, &dstIp); break;
andrewboyson 59:e0e556c8bd46 174 case IGMP: break;
andrewboyson 59:e0e556c8bd46 175 case UDP: action = Udp4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, &srcIp, &dstIp); break;
andrewboyson 59:e0e556c8bd46 176 case TCP: action = Tcp4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, &srcIp, &dstIp); break;
andrewboyson 59:e0e556c8bd46 177 case IP6IN4: break;
andrewboyson 10:f0854784e960 178 default:
andrewboyson 37:793b39683406 179 LogTimeF("IP4 received packet unknown protocol %d\r\n");
andrewboyson 10:f0854784e960 180 return DO_NOTHING;
andrewboyson 10:f0854784e960 181 }
andrewboyson 11:c051adb70c5a 182 if (!action) return DO_NOTHING;
andrewboyson 10:f0854784e960 183
andrewboyson 53:77f8a49adf89 184 if (DhcpIpNeedsToBeRouted(dstIp))
andrewboyson 53:77f8a49adf89 185 {
andrewboyson 59:e0e556c8bd46 186 Ar4IpToMac(DhcpRouter, macRemote); //Send back to the router
andrewboyson 53:77f8a49adf89 187 ttl = OFF_LINK_TTL;
andrewboyson 53:77f8a49adf89 188 }
andrewboyson 53:77f8a49adf89 189 else
andrewboyson 53:77f8a49adf89 190 {
andrewboyson 53:77f8a49adf89 191 ttl = 255;
andrewboyson 53:77f8a49adf89 192 }
andrewboyson 59:e0e556c8bd46 193
andrewboyson 61:aad055f1b0d1 194 totalLength = sizeof(struct header) + dataLengthTx;
andrewboyson 61:aad055f1b0d1 195 headerLength = sizeof(struct header);
andrewboyson 59:e0e556c8bd46 196
andrewboyson 59:e0e556c8bd46 197 writeHeader(pHeaderTx);
andrewboyson 53:77f8a49adf89 198
andrewboyson 59:e0e556c8bd46 199 *pSizeTx = totalLength;
andrewboyson 10:f0854784e960 200
andrewboyson 37:793b39683406 201 if (ActionGetTracePart(action)) logHeader();
andrewboyson 37:793b39683406 202
andrewboyson 10:f0854784e960 203 return action;
andrewboyson 10:f0854784e960 204 }
andrewboyson 10:f0854784e960 205 int Ip4PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac)
andrewboyson 59:e0e556c8bd46 206 {
andrewboyson 61:aad055f1b0d1 207 headerLength = sizeof(struct header);
andrewboyson 59:e0e556c8bd46 208 char* pData = (char*)pPacket + headerLength;
andrewboyson 10:f0854784e960 209 version = 4;
andrewboyson 10:f0854784e960 210 tos = 0;
andrewboyson 10:f0854784e960 211 id = 0;
andrewboyson 10:f0854784e960 212 dontFragment = true;
andrewboyson 10:f0854784e960 213 moreFragments = false;
andrewboyson 10:f0854784e960 214 offset = 0;
andrewboyson 10:f0854784e960 215 protocol = UDP;
andrewboyson 10:f0854784e960 216
andrewboyson 61:aad055f1b0d1 217 int dataLength = *pSize - sizeof(struct header);
andrewboyson 59:e0e556c8bd46 218
andrewboyson 11:c051adb70c5a 219 int action = DO_NOTHING;
andrewboyson 35:93c39d260a83 220 if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp);
andrewboyson 11:c051adb70c5a 221 if (!action) return DO_NOTHING;
andrewboyson 42:222a4f45f916 222 int dest = ActionGetDestPart(action);
andrewboyson 42:222a4f45f916 223 switch (dest)
andrewboyson 11:c051adb70c5a 224 {
andrewboyson 11:c051adb70c5a 225 case UNICAST:
andrewboyson 11:c051adb70c5a 226 case UNICAST_DNS:
andrewboyson 11:c051adb70c5a 227 case UNICAST_DHCP:
andrewboyson 22:914b970356f0 228 case UNICAST_NTP:
andrewboyson 57:e0fb648acf48 229 case UNICAST_TFTP:
andrewboyson 53:77f8a49adf89 230 if (DhcpIpNeedsToBeRouted(dstIp))
andrewboyson 53:77f8a49adf89 231 {
andrewboyson 53:77f8a49adf89 232 Ar4IpToMac(DhcpRouter, pDstMac); //send via router
andrewboyson 53:77f8a49adf89 233 ttl = OFF_LINK_TTL;
andrewboyson 53:77f8a49adf89 234 }
andrewboyson 53:77f8a49adf89 235 else
andrewboyson 53:77f8a49adf89 236 {
andrewboyson 53:77f8a49adf89 237 Ar4IpToMac(dstIp, pDstMac); //Send direct
andrewboyson 53:77f8a49adf89 238 ttl = 255;
andrewboyson 53:77f8a49adf89 239 }
andrewboyson 11:c051adb70c5a 240 break;
andrewboyson 42:222a4f45f916 241 case MULTICAST_NODE:
andrewboyson 42:222a4f45f916 242 case MULTICAST_ROUTER:
andrewboyson 42:222a4f45f916 243 case MULTICAST_MDNS:
andrewboyson 42:222a4f45f916 244 case MULTICAST_LLMNR:
andrewboyson 22:914b970356f0 245 case BROADCAST:
andrewboyson 53:77f8a49adf89 246 ttl = 255;
andrewboyson 22:914b970356f0 247 break;
andrewboyson 22:914b970356f0 248 default:
andrewboyson 42:222a4f45f916 249 LogTimeF("Ip4PollForPacketToSend - undefined destination %d\r\n", dest);
andrewboyson 53:77f8a49adf89 250 return DO_NOTHING;
andrewboyson 11:c051adb70c5a 251 }
andrewboyson 10:f0854784e960 252
andrewboyson 59:e0e556c8bd46 253 struct header* pHeader = (struct header*)pPacket;
andrewboyson 59:e0e556c8bd46 254
andrewboyson 61:aad055f1b0d1 255 totalLength = sizeof(struct header) + dataLength;
andrewboyson 61:aad055f1b0d1 256 headerLength = sizeof(struct header);
andrewboyson 59:e0e556c8bd46 257
andrewboyson 59:e0e556c8bd46 258 writeHeader(pHeader);
andrewboyson 10:f0854784e960 259
andrewboyson 59:e0e556c8bd46 260 *pSize = totalLength;
andrewboyson 10:f0854784e960 261
andrewboyson 37:793b39683406 262 if (ActionGetTracePart(action)) logHeader();
andrewboyson 37:793b39683406 263
andrewboyson 10:f0854784e960 264 return action;
andrewboyson 10:f0854784e960 265 }