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