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:
Tue Nov 28 17:05:46 2017 +0000
Revision:
57:e0fb648acf48
Parent:
53:77f8a49adf89
Child:
59:e0e556c8bd46
Added TFTP

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