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 Nov 02 08:10:55 2017 +0000
Revision:
49:1a6336f2b3f9
Parent:
48:952dddb74b8b
Child:
50:492f2d2954e4
Separated the address handling parts of IP4 and IP6 from the protocol parts.

Who changed what in which revision?

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