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 03 17:23:38 2019 +0000
Revision:
98:b977424ec7f7
Parent:
97:d91f7db00235
Child:
136:8a65abb0dc63
Added more fault positions and removed need for a net server module. Added need for the ethernet jack leds definition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 37:793b39683406 2 #include "log.h"
andrewboyson 37:793b39683406 3 #include "net.h"
andrewboyson 37:793b39683406 4 #include "action.h"
andrewboyson 37:793b39683406 5 #include "arp.h"
andrewboyson 37:793b39683406 6 #include "ip4.h"
andrewboyson 37:793b39683406 7 #include "ip6.h"
andrewboyson 59:e0e556c8bd46 8 #include "link.h"
andrewboyson 37:793b39683406 9 #include "eth.h"
andrewboyson 37:793b39683406 10 #include "mac.h"
andrewboyson 97:d91f7db00235 11 #include "fault.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 61:aad055f1b0d1 56 struct header * pHeader = (struct 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 98:b977424ec7f7 61 int lastFaultPoint = FaultPoint;
andrewboyson 97:d91f7db00235 62 FaultPoint = FAULT_POINT_EthHandlePacket;
andrewboyson 97:d91f7db00235 63
andrewboyson 59:e0e556c8bd46 64 tracePacket = pPacketRx;
andrewboyson 59:e0e556c8bd46 65
andrewboyson 61:aad055f1b0d1 66 struct header * pHeaderRx = (struct header*)pPacketRx;
andrewboyson 61:aad055f1b0d1 67 struct header * pHeaderTx = (struct header*)pPacketTx;
andrewboyson 61:aad055f1b0d1 68 void* pDataRx = (char*)pPacketRx + sizeof(struct header);
andrewboyson 61:aad055f1b0d1 69 void* pDataTx = (char*)pPacketTx + sizeof(struct header);
andrewboyson 61:aad055f1b0d1 70 int dataLengthRx = sizeRx - sizeof(struct header);
andrewboyson 61:aad055f1b0d1 71 int dataLengthTx = *pSizeTx - sizeof(struct header);
andrewboyson 59:e0e556c8bd46 72 if (dataLengthTx > MTU) dataLengthTx = MTU; //Limit the transmitted length to the maximum ethernet frame payload length
andrewboyson 14:e75a59c1123d 73
andrewboyson 86:55bc5ddac16c 74 if (!MacAccept(pHeaderRx->dst))
andrewboyson 86:55bc5ddac16c 75 {
andrewboyson 98:b977424ec7f7 76 FaultPoint = lastFaultPoint;
andrewboyson 86:55bc5ddac16c 77 return DO_NOTHING;
andrewboyson 86:55bc5ddac16c 78 }
andrewboyson 13:9cd54f7db57a 79
andrewboyson 59:e0e556c8bd46 80 protocol = NetToHost16(pHeaderRx->typ);
andrewboyson 86:55bc5ddac16c 81 if (protocol < 1500)
andrewboyson 86:55bc5ddac16c 82 {
andrewboyson 98:b977424ec7f7 83 FaultPoint = lastFaultPoint;
andrewboyson 86:55bc5ddac16c 84 return DO_NOTHING; //drop 802.3 messages
andrewboyson 86:55bc5ddac16c 85 }
andrewboyson 10:f0854784e960 86
andrewboyson 59:e0e556c8bd46 87 NetTraceHostCheckMac(pHeaderRx->src);
andrewboyson 57:e0fb648acf48 88
andrewboyson 10:f0854784e960 89 int action = DO_NOTHING;
andrewboyson 59:e0e556c8bd46 90 char* macRemote = pHeaderRx->src;
andrewboyson 42:222a4f45f916 91 switch (protocol)
andrewboyson 10:f0854784e960 92 {
andrewboyson 59:e0e556c8bd46 93 case ARP: action = ArpHandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx); break;
andrewboyson 59:e0e556c8bd46 94 case IPV4: action = Ip4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
andrewboyson 59:e0e556c8bd46 95 case IPV6: action = Ip6HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
andrewboyson 28:edc17eeb4142 96 case 0x6970: break; //Drop Sonos group membership packet
andrewboyson 16:f416ef583c89 97 case 0x7374: break; //Drop Sky Q packet
andrewboyson 16:f416ef583c89 98 case 0x7475: break; //Drop Sky Q packet
andrewboyson 16:f416ef583c89 99 case 0x7380: break; //Drop Sky Q packet
andrewboyson 23:b641979389b2 100 case 0x8100: break; //Drop Sky Q VLAN 802.1Q packet
andrewboyson 33:714a0345e59b 101 case 0x887b: break; //Drop Sky Q packet
andrewboyson 10:f0854784e960 102 default:
andrewboyson 42:222a4f45f916 103 LogTimeF("ETH protocol %d not handled", protocol);
andrewboyson 10:f0854784e960 104 break;
andrewboyson 10:f0854784e960 105 }
andrewboyson 86:55bc5ddac16c 106 if (!action)
andrewboyson 86:55bc5ddac16c 107 {
andrewboyson 98:b977424ec7f7 108 FaultPoint = lastFaultPoint;
andrewboyson 86:55bc5ddac16c 109 return DO_NOTHING;
andrewboyson 86:55bc5ddac16c 110 }
andrewboyson 59:e0e556c8bd46 111
andrewboyson 59:e0e556c8bd46 112 MacMakeFromDest(ActionGetDestPart(action), protocol, macRemote);
andrewboyson 59:e0e556c8bd46 113 MacCopy(pHeaderTx->src, MacLocal);
andrewboyson 59:e0e556c8bd46 114 MacCopy(pHeaderTx->dst, macRemote);
andrewboyson 59:e0e556c8bd46 115 pHeaderTx->typ = NetToHost16(protocol);
andrewboyson 10:f0854784e960 116
andrewboyson 61:aad055f1b0d1 117 *pSizeTx = sizeof(struct header) + dataLengthTx;
andrewboyson 37:793b39683406 118
andrewboyson 59:e0e556c8bd46 119 if (ActionGetTracePart(action)) LogHeader(pHeaderTx);
andrewboyson 37:793b39683406 120
andrewboyson 98:b977424ec7f7 121 FaultPoint = lastFaultPoint;
andrewboyson 10:f0854784e960 122 return action;
andrewboyson 10:f0854784e960 123 }
andrewboyson 10:f0854784e960 124 int EthPollForPacketToSend(void* pPacket, int* pSize)
andrewboyson 10:f0854784e960 125 {
andrewboyson 61:aad055f1b0d1 126 struct header * pHeader = (struct header*)pPacket;
andrewboyson 61:aad055f1b0d1 127 void* pData = (char*)pPacket + sizeof(struct header);
andrewboyson 10:f0854784e960 128
andrewboyson 94:e2973a2c488e 129 int dataLength = *pSize - sizeof(struct header);
andrewboyson 94:e2973a2c488e 130 if (dataLength > MTU) dataLength = MTU; //Limit the transmitted length to the maximum ethernet frame payload length
andrewboyson 42:222a4f45f916 131 protocol = 0;
andrewboyson 10:f0854784e960 132 int action = DO_NOTHING;
andrewboyson 47:73af5c0b0dc2 133 if (!action)
andrewboyson 10:f0854784e960 134 {
andrewboyson 22:914b970356f0 135 action = ArpPollForPacketToSend(pData, &dataLength);
andrewboyson 42:222a4f45f916 136 protocol = ARP;
andrewboyson 22:914b970356f0 137 }
andrewboyson 22:914b970356f0 138
andrewboyson 47:73af5c0b0dc2 139 if (!action)
andrewboyson 22:914b970356f0 140 {
andrewboyson 10:f0854784e960 141 action = Ip6PollForPacketToSend(pData, &dataLength, pHeader->dst);
andrewboyson 42:222a4f45f916 142 protocol = IPV6;
andrewboyson 10:f0854784e960 143 }
andrewboyson 10:f0854784e960 144
andrewboyson 47:73af5c0b0dc2 145 if (!action)
andrewboyson 10:f0854784e960 146 {
andrewboyson 10:f0854784e960 147 action = Ip4PollForPacketToSend(pData, &dataLength, pHeader->dst);
andrewboyson 42:222a4f45f916 148 protocol = IPV4;
andrewboyson 10:f0854784e960 149 }
andrewboyson 10:f0854784e960 150
andrewboyson 47:73af5c0b0dc2 151 if (!action) return DO_NOTHING;
andrewboyson 47:73af5c0b0dc2 152
andrewboyson 59:e0e556c8bd46 153 MacMakeFromDest(ActionGetDestPart(action), protocol, pHeader->dst);
andrewboyson 59:e0e556c8bd46 154 MacCopy(pHeader->src, MacLocal);
andrewboyson 59:e0e556c8bd46 155 pHeader->typ = NetToHost16(protocol);
andrewboyson 59:e0e556c8bd46 156
andrewboyson 61:aad055f1b0d1 157 *pSize = sizeof(struct header) + dataLength;
andrewboyson 10:f0854784e960 158
andrewboyson 37:793b39683406 159 if (ActionGetTracePart(action)) LogHeader(pHeader);
andrewboyson 13:9cd54f7db57a 160
andrewboyson 10:f0854784e960 161 return action;
andrewboyson 10:f0854784e960 162 }
andrewboyson 10:f0854784e960 163