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
ip4/ip4.c@71:736a5747ade1, 2018-10-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Oct 20 18:56:13 2018 +0000
- Revision:
- 71:736a5747ade1
- Parent:
- 61:aad055f1b0d1
- Child:
- 74:c3756bfa960e
Started process of making TCP pollable
Who changed what in which revision?
User | Revision | Line number | New 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 | 71:736a5747ade1 | 164 | uint32_t* pCachedRemIp = Ar4AddIpRecord(trace, macRemote, srcIp); |
andrewboyson | 71:736a5747ade1 | 165 | Nr4MakeRequestForNameFromIp(srcIp); |
andrewboyson | 59:e0e556c8bd46 | 166 | |
andrewboyson | 10:f0854784e960 | 167 | int action = DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 168 | switch (protocol) |
andrewboyson | 10:f0854784e960 | 169 | { |
andrewboyson | 59:e0e556c8bd46 | 170 | case ICMP: action = Icmp4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, &srcIp, &dstIp); break; |
andrewboyson | 59:e0e556c8bd46 | 171 | case IGMP: break; |
andrewboyson | 59:e0e556c8bd46 | 172 | case UDP: action = Udp4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, &srcIp, &dstIp); break; |
andrewboyson | 71:736a5747ade1 | 173 | case TCP: action = Tcp4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, &srcIp, &dstIp, pCachedRemIp); break; |
andrewboyson | 59:e0e556c8bd46 | 174 | case IP6IN4: break; |
andrewboyson | 10:f0854784e960 | 175 | default: |
andrewboyson | 37:793b39683406 | 176 | LogTimeF("IP4 received packet unknown protocol %d\r\n"); |
andrewboyson | 10:f0854784e960 | 177 | return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 178 | } |
andrewboyson | 11:c051adb70c5a | 179 | if (!action) return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 180 | |
andrewboyson | 53:77f8a49adf89 | 181 | if (DhcpIpNeedsToBeRouted(dstIp)) |
andrewboyson | 53:77f8a49adf89 | 182 | { |
andrewboyson | 59:e0e556c8bd46 | 183 | Ar4IpToMac(DhcpRouter, macRemote); //Send back to the router |
andrewboyson | 53:77f8a49adf89 | 184 | ttl = OFF_LINK_TTL; |
andrewboyson | 53:77f8a49adf89 | 185 | } |
andrewboyson | 53:77f8a49adf89 | 186 | else |
andrewboyson | 53:77f8a49adf89 | 187 | { |
andrewboyson | 53:77f8a49adf89 | 188 | ttl = 255; |
andrewboyson | 53:77f8a49adf89 | 189 | } |
andrewboyson | 59:e0e556c8bd46 | 190 | |
andrewboyson | 61:aad055f1b0d1 | 191 | totalLength = sizeof(struct header) + dataLengthTx; |
andrewboyson | 61:aad055f1b0d1 | 192 | headerLength = sizeof(struct header); |
andrewboyson | 59:e0e556c8bd46 | 193 | |
andrewboyson | 59:e0e556c8bd46 | 194 | writeHeader(pHeaderTx); |
andrewboyson | 53:77f8a49adf89 | 195 | |
andrewboyson | 59:e0e556c8bd46 | 196 | *pSizeTx = totalLength; |
andrewboyson | 10:f0854784e960 | 197 | |
andrewboyson | 37:793b39683406 | 198 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 37:793b39683406 | 199 | |
andrewboyson | 10:f0854784e960 | 200 | return action; |
andrewboyson | 10:f0854784e960 | 201 | } |
andrewboyson | 10:f0854784e960 | 202 | int Ip4PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac) |
andrewboyson | 59:e0e556c8bd46 | 203 | { |
andrewboyson | 61:aad055f1b0d1 | 204 | headerLength = sizeof(struct header); |
andrewboyson | 59:e0e556c8bd46 | 205 | char* pData = (char*)pPacket + headerLength; |
andrewboyson | 10:f0854784e960 | 206 | version = 4; |
andrewboyson | 10:f0854784e960 | 207 | tos = 0; |
andrewboyson | 10:f0854784e960 | 208 | id = 0; |
andrewboyson | 10:f0854784e960 | 209 | dontFragment = true; |
andrewboyson | 10:f0854784e960 | 210 | moreFragments = false; |
andrewboyson | 10:f0854784e960 | 211 | offset = 0; |
andrewboyson | 10:f0854784e960 | 212 | |
andrewboyson | 61:aad055f1b0d1 | 213 | int dataLength = *pSize - sizeof(struct header); |
andrewboyson | 59:e0e556c8bd46 | 214 | |
andrewboyson | 11:c051adb70c5a | 215 | int action = DO_NOTHING; |
andrewboyson | 71:736a5747ade1 | 216 | if (!action) { action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp); protocol = UDP; } |
andrewboyson | 71:736a5747ade1 | 217 | if (!action) { action = Tcp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp); protocol = TCP; } |
andrewboyson | 11:c051adb70c5a | 218 | if (!action) return DO_NOTHING; |
andrewboyson | 42:222a4f45f916 | 219 | int dest = ActionGetDestPart(action); |
andrewboyson | 42:222a4f45f916 | 220 | switch (dest) |
andrewboyson | 11:c051adb70c5a | 221 | { |
andrewboyson | 11:c051adb70c5a | 222 | case UNICAST: |
andrewboyson | 11:c051adb70c5a | 223 | case UNICAST_DNS: |
andrewboyson | 11:c051adb70c5a | 224 | case UNICAST_DHCP: |
andrewboyson | 22:914b970356f0 | 225 | case UNICAST_NTP: |
andrewboyson | 57:e0fb648acf48 | 226 | case UNICAST_TFTP: |
andrewboyson | 53:77f8a49adf89 | 227 | if (DhcpIpNeedsToBeRouted(dstIp)) |
andrewboyson | 53:77f8a49adf89 | 228 | { |
andrewboyson | 53:77f8a49adf89 | 229 | Ar4IpToMac(DhcpRouter, pDstMac); //send via router |
andrewboyson | 53:77f8a49adf89 | 230 | ttl = OFF_LINK_TTL; |
andrewboyson | 53:77f8a49adf89 | 231 | } |
andrewboyson | 53:77f8a49adf89 | 232 | else |
andrewboyson | 53:77f8a49adf89 | 233 | { |
andrewboyson | 53:77f8a49adf89 | 234 | Ar4IpToMac(dstIp, pDstMac); //Send direct |
andrewboyson | 53:77f8a49adf89 | 235 | ttl = 255; |
andrewboyson | 53:77f8a49adf89 | 236 | } |
andrewboyson | 11:c051adb70c5a | 237 | break; |
andrewboyson | 42:222a4f45f916 | 238 | case MULTICAST_NODE: |
andrewboyson | 42:222a4f45f916 | 239 | case MULTICAST_ROUTER: |
andrewboyson | 42:222a4f45f916 | 240 | case MULTICAST_MDNS: |
andrewboyson | 42:222a4f45f916 | 241 | case MULTICAST_LLMNR: |
andrewboyson | 22:914b970356f0 | 242 | case BROADCAST: |
andrewboyson | 53:77f8a49adf89 | 243 | ttl = 255; |
andrewboyson | 22:914b970356f0 | 244 | break; |
andrewboyson | 22:914b970356f0 | 245 | default: |
andrewboyson | 42:222a4f45f916 | 246 | LogTimeF("Ip4PollForPacketToSend - undefined destination %d\r\n", dest); |
andrewboyson | 53:77f8a49adf89 | 247 | return DO_NOTHING; |
andrewboyson | 11:c051adb70c5a | 248 | } |
andrewboyson | 10:f0854784e960 | 249 | |
andrewboyson | 59:e0e556c8bd46 | 250 | struct header* pHeader = (struct header*)pPacket; |
andrewboyson | 59:e0e556c8bd46 | 251 | |
andrewboyson | 61:aad055f1b0d1 | 252 | totalLength = sizeof(struct header) + dataLength; |
andrewboyson | 61:aad055f1b0d1 | 253 | headerLength = sizeof(struct header); |
andrewboyson | 59:e0e556c8bd46 | 254 | |
andrewboyson | 59:e0e556c8bd46 | 255 | writeHeader(pHeader); |
andrewboyson | 10:f0854784e960 | 256 | |
andrewboyson | 59:e0e556c8bd46 | 257 | *pSize = totalLength; |
andrewboyson | 10:f0854784e960 | 258 | |
andrewboyson | 37:793b39683406 | 259 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 37:793b39683406 | 260 | |
andrewboyson | 10:f0854784e960 | 261 | return action; |
andrewboyson | 10:f0854784e960 | 262 | } |