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 Dec 14 20:55:40 2017 +0000
Revision:
59:e0e556c8bd46
Parent:
57:e0fb648acf48
Added buffer length to help avoid over runs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 37:793b39683406 1 #include "mbed.h"
andrewboyson 37:793b39683406 2 #include "io.h"
andrewboyson 37:793b39683406 3 #include "log.h"
andrewboyson 37:793b39683406 4 #include "net.h"
andrewboyson 37:793b39683406 5 #include "action.h"
andrewboyson 37:793b39683406 6 #include "arp.h"
andrewboyson 37:793b39683406 7 #include "ip4.h"
andrewboyson 37:793b39683406 8 #include "ip6.h"
andrewboyson 59:e0e556c8bd46 9 #include "link.h"
andrewboyson 37:793b39683406 10 #include "eth.h"
andrewboyson 37:793b39683406 11 #include "mac.h"
andrewboyson 10:f0854784e960 12
andrewboyson 59:e0e556c8bd46 13 #define MTU 1500
andrewboyson 13:9cd54f7db57a 14
andrewboyson 42:222a4f45f916 15 //header variables
andrewboyson 10:f0854784e960 16 __packed struct header
andrewboyson 10:f0854784e960 17 {
andrewboyson 10:f0854784e960 18 char dst[6];
andrewboyson 10:f0854784e960 19 char src[6];
andrewboyson 10:f0854784e960 20 uint16_t typ;
andrewboyson 10:f0854784e960 21 };
andrewboyson 42:222a4f45f916 22 static uint16_t protocol;
andrewboyson 47:73af5c0b0dc2 23 void EthProtocolLog(uint16_t prototype)
andrewboyson 10:f0854784e960 24 {
andrewboyson 14:e75a59c1123d 25 switch (prototype)
andrewboyson 10:f0854784e960 26 {
andrewboyson 47:73af5c0b0dc2 27 case ARP: Log("ARP"); break;
andrewboyson 47:73af5c0b0dc2 28 case IPV4: Log("IPV4"); break;
andrewboyson 47:73af5c0b0dc2 29 case IPV6: Log("IPV6"); break;
andrewboyson 47:73af5c0b0dc2 30 default: LogF("%04hX", prototype); break;
andrewboyson 10:f0854784e960 31 }
andrewboyson 10:f0854784e960 32 }
andrewboyson 37:793b39683406 33 void LogHeader(struct header* pHeader)
andrewboyson 13:9cd54f7db57a 34 {
andrewboyson 43:bc028d5a6424 35 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 36 {
andrewboyson 47:73af5c0b0dc2 37 Log("ETH header\r\n");
andrewboyson 47:73af5c0b0dc2 38 Log(" Destination: "); MacLog(pHeader->dst); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 39 Log(" Source: "); MacLog(pHeader->src); Log("\r\n");
andrewboyson 47:73af5c0b0dc2 40 Log(" EtherType: "); EthProtocolLog(NetToHost16(pHeader->typ)); Log("\r\n");
andrewboyson 43:bc028d5a6424 41 }
andrewboyson 43:bc028d5a6424 42 else
andrewboyson 43:bc028d5a6424 43 {
andrewboyson 44:83ce5ace337b 44 Log("ETH header ");
andrewboyson 47:73af5c0b0dc2 45 EthProtocolLog(NetToHost16(pHeader->typ));
andrewboyson 43:bc028d5a6424 46 Log(" ");
andrewboyson 47:73af5c0b0dc2 47 MacLog(pHeader->src);
andrewboyson 43:bc028d5a6424 48 Log(" >>> ");
andrewboyson 47:73af5c0b0dc2 49 MacLog(pHeader->dst);
andrewboyson 43:bc028d5a6424 50 Log("\r\n");
andrewboyson 43:bc028d5a6424 51 }
andrewboyson 13:9cd54f7db57a 52 }
andrewboyson 37:793b39683406 53 static void* tracePacket;
andrewboyson 37:793b39683406 54 static void trace()
andrewboyson 10:f0854784e960 55 {
andrewboyson 37:793b39683406 56 struct header * pHeader = (header*)tracePacket;
andrewboyson 37:793b39683406 57 LogHeader(pHeader);
andrewboyson 37:793b39683406 58 }
andrewboyson 59:e0e556c8bd46 59 int EthHandlePacket(void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx)
andrewboyson 37:793b39683406 60 {
andrewboyson 59:e0e556c8bd46 61 tracePacket = pPacketRx;
andrewboyson 59:e0e556c8bd46 62
andrewboyson 59:e0e556c8bd46 63 struct header * pHeaderRx = (header*)pPacketRx;
andrewboyson 59:e0e556c8bd46 64 struct header * pHeaderTx = (header*)pPacketTx;
andrewboyson 59:e0e556c8bd46 65 void* pDataRx = (char*)pPacketRx + sizeof(header);
andrewboyson 59:e0e556c8bd46 66 void* pDataTx = (char*)pPacketTx + sizeof(header);
andrewboyson 59:e0e556c8bd46 67 int dataLengthRx = sizeRx - sizeof(header);
andrewboyson 59:e0e556c8bd46 68 int dataLengthTx = *pSizeTx - sizeof(header);
andrewboyson 59:e0e556c8bd46 69 if (dataLengthTx > MTU) dataLengthTx = MTU; //Limit the transmitted length to the maximum ethernet frame payload length
andrewboyson 14:e75a59c1123d 70
andrewboyson 59:e0e556c8bd46 71 if (!MacAccept(pHeaderRx->dst)) return DO_NOTHING;
andrewboyson 13:9cd54f7db57a 72
andrewboyson 59:e0e556c8bd46 73 protocol = NetToHost16(pHeaderRx->typ);
andrewboyson 42:222a4f45f916 74 if (protocol < 1500) return DO_NOTHING; //drop 802.3 messages
andrewboyson 10:f0854784e960 75
andrewboyson 59:e0e556c8bd46 76 NetTraceHostCheckMac(pHeaderRx->src);
andrewboyson 57:e0fb648acf48 77
andrewboyson 10:f0854784e960 78 int action = DO_NOTHING;
andrewboyson 59:e0e556c8bd46 79 char* macRemote = pHeaderRx->src;
andrewboyson 42:222a4f45f916 80 switch (protocol)
andrewboyson 10:f0854784e960 81 {
andrewboyson 59:e0e556c8bd46 82 case ARP: action = ArpHandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx); break;
andrewboyson 59:e0e556c8bd46 83 case IPV4: action = Ip4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
andrewboyson 59:e0e556c8bd46 84 case IPV6: action = Ip6HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
andrewboyson 28:edc17eeb4142 85 case 0x6970: break; //Drop Sonos group membership packet
andrewboyson 16:f416ef583c89 86 case 0x7374: break; //Drop Sky Q packet
andrewboyson 16:f416ef583c89 87 case 0x7475: break; //Drop Sky Q packet
andrewboyson 16:f416ef583c89 88 case 0x7380: break; //Drop Sky Q packet
andrewboyson 23:b641979389b2 89 case 0x8100: break; //Drop Sky Q VLAN 802.1Q packet
andrewboyson 33:714a0345e59b 90 case 0x887b: break; //Drop Sky Q packet
andrewboyson 10:f0854784e960 91 default:
andrewboyson 42:222a4f45f916 92 LogTimeF("ETH protocol %d not handled", protocol);
andrewboyson 10:f0854784e960 93 break;
andrewboyson 10:f0854784e960 94 }
andrewboyson 47:73af5c0b0dc2 95 if (!action) return DO_NOTHING;
andrewboyson 59:e0e556c8bd46 96
andrewboyson 59:e0e556c8bd46 97 MacMakeFromDest(ActionGetDestPart(action), protocol, macRemote);
andrewboyson 59:e0e556c8bd46 98 MacCopy(pHeaderTx->src, MacLocal);
andrewboyson 59:e0e556c8bd46 99 MacCopy(pHeaderTx->dst, macRemote);
andrewboyson 59:e0e556c8bd46 100 pHeaderTx->typ = NetToHost16(protocol);
andrewboyson 10:f0854784e960 101
andrewboyson 59:e0e556c8bd46 102 *pSizeTx = sizeof(header) + dataLengthTx;
andrewboyson 37:793b39683406 103
andrewboyson 59:e0e556c8bd46 104 if (ActionGetTracePart(action)) LogHeader(pHeaderTx);
andrewboyson 37:793b39683406 105
andrewboyson 10:f0854784e960 106 return action;
andrewboyson 10:f0854784e960 107 }
andrewboyson 10:f0854784e960 108 int EthPollForPacketToSend(void* pPacket, int* pSize)
andrewboyson 10:f0854784e960 109 {
andrewboyson 10:f0854784e960 110 struct header * pHeader = (header*)pPacket;
andrewboyson 59:e0e556c8bd46 111 void* pData = (char*)pPacket + sizeof(header);
andrewboyson 10:f0854784e960 112
andrewboyson 10:f0854784e960 113 int dataLength = 0;
andrewboyson 42:222a4f45f916 114 protocol = 0;
andrewboyson 10:f0854784e960 115 int action = DO_NOTHING;
andrewboyson 47:73af5c0b0dc2 116 if (!action)
andrewboyson 10:f0854784e960 117 {
andrewboyson 22:914b970356f0 118 action = ArpPollForPacketToSend(pData, &dataLength);
andrewboyson 42:222a4f45f916 119 protocol = ARP;
andrewboyson 22:914b970356f0 120 }
andrewboyson 22:914b970356f0 121
andrewboyson 47:73af5c0b0dc2 122 if (!action)
andrewboyson 22:914b970356f0 123 {
andrewboyson 10:f0854784e960 124 action = Ip6PollForPacketToSend(pData, &dataLength, pHeader->dst);
andrewboyson 42:222a4f45f916 125 protocol = IPV6;
andrewboyson 10:f0854784e960 126 }
andrewboyson 10:f0854784e960 127
andrewboyson 47:73af5c0b0dc2 128 if (!action)
andrewboyson 10:f0854784e960 129 {
andrewboyson 10:f0854784e960 130 action = Ip4PollForPacketToSend(pData, &dataLength, pHeader->dst);
andrewboyson 42:222a4f45f916 131 protocol = IPV4;
andrewboyson 10:f0854784e960 132 }
andrewboyson 10:f0854784e960 133
andrewboyson 47:73af5c0b0dc2 134 if (!action) return DO_NOTHING;
andrewboyson 47:73af5c0b0dc2 135
andrewboyson 59:e0e556c8bd46 136 MacMakeFromDest(ActionGetDestPart(action), protocol, pHeader->dst);
andrewboyson 59:e0e556c8bd46 137 MacCopy(pHeader->src, MacLocal);
andrewboyson 59:e0e556c8bd46 138 pHeader->typ = NetToHost16(protocol);
andrewboyson 59:e0e556c8bd46 139
andrewboyson 59:e0e556c8bd46 140 *pSize = sizeof(header) + dataLength;
andrewboyson 10:f0854784e960 141
andrewboyson 37:793b39683406 142 if (ActionGetTracePart(action)) LogHeader(pHeader);
andrewboyson 13:9cd54f7db57a 143
andrewboyson 10:f0854784e960 144 return action;
andrewboyson 10:f0854784e960 145 }
andrewboyson 10:f0854784e960 146