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.cpp@57:e0fb648acf48, 2017-11-28 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Nov 28 17:05:46 2017 +0000
- Revision:
- 57:e0fb648acf48
- Parent:
- 47:73af5c0b0dc2
- Child:
- 59:e0e556c8bd46
Added TFTP
Who changed what in which revision?
User | Revision | Line number | New 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 | 37:793b39683406 | 9 | #include "phy.h" |
andrewboyson | 37:793b39683406 | 10 | #include "eth.h" |
andrewboyson | 37:793b39683406 | 11 | #include "mac.h" |
andrewboyson | 10:f0854784e960 | 12 | |
andrewboyson | 10:f0854784e960 | 13 | #define HEADER_SIZE 14 |
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 | 10:f0854784e960 | 33 | |
andrewboyson | 47:73af5c0b0dc2 | 34 | static void finalisePacket(int dest, int dataLength, void* pPacket, int* pSize) |
andrewboyson | 10:f0854784e960 | 35 | { |
andrewboyson | 47:73af5c0b0dc2 | 36 | if (!dest) return; |
andrewboyson | 14:e75a59c1123d | 37 | |
andrewboyson | 10:f0854784e960 | 38 | struct header * pHeader = (header*)pPacket; |
andrewboyson | 14:e75a59c1123d | 39 | |
andrewboyson | 47:73af5c0b0dc2 | 40 | MacMakeFromDest(dest, protocol, pHeader->dst); |
andrewboyson | 10:f0854784e960 | 41 | |
andrewboyson | 36:900e24b27bfb | 42 | MacCopy(pHeader->src, MacLocal); //Put our MAC into the source |
andrewboyson | 42:222a4f45f916 | 43 | pHeader->typ = NetToHost16(protocol); |
andrewboyson | 10:f0854784e960 | 44 | |
andrewboyson | 10:f0854784e960 | 45 | *pSize = HEADER_SIZE + dataLength; |
andrewboyson | 10:f0854784e960 | 46 | } |
andrewboyson | 37:793b39683406 | 47 | void LogHeader(struct header* pHeader) |
andrewboyson | 13:9cd54f7db57a | 48 | { |
andrewboyson | 43:bc028d5a6424 | 49 | if (NetTraceVerbose) |
andrewboyson | 43:bc028d5a6424 | 50 | { |
andrewboyson | 47:73af5c0b0dc2 | 51 | Log("ETH header\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 52 | Log(" Destination: "); MacLog(pHeader->dst); Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 53 | Log(" Source: "); MacLog(pHeader->src); Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 54 | Log(" EtherType: "); EthProtocolLog(NetToHost16(pHeader->typ)); Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 55 | } |
andrewboyson | 43:bc028d5a6424 | 56 | else |
andrewboyson | 43:bc028d5a6424 | 57 | { |
andrewboyson | 44:83ce5ace337b | 58 | Log("ETH header "); |
andrewboyson | 47:73af5c0b0dc2 | 59 | EthProtocolLog(NetToHost16(pHeader->typ)); |
andrewboyson | 43:bc028d5a6424 | 60 | Log(" "); |
andrewboyson | 47:73af5c0b0dc2 | 61 | MacLog(pHeader->src); |
andrewboyson | 43:bc028d5a6424 | 62 | Log(" >>> "); |
andrewboyson | 47:73af5c0b0dc2 | 63 | MacLog(pHeader->dst); |
andrewboyson | 43:bc028d5a6424 | 64 | Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 65 | } |
andrewboyson | 13:9cd54f7db57a | 66 | } |
andrewboyson | 37:793b39683406 | 67 | static void (*pTraceBack)(void); |
andrewboyson | 37:793b39683406 | 68 | static void* tracePacket; |
andrewboyson | 37:793b39683406 | 69 | static void trace() |
andrewboyson | 10:f0854784e960 | 70 | { |
andrewboyson | 37:793b39683406 | 71 | pTraceBack(); |
andrewboyson | 37:793b39683406 | 72 | struct header * pHeader = (header*)tracePacket; |
andrewboyson | 37:793b39683406 | 73 | LogHeader(pHeader); |
andrewboyson | 37:793b39683406 | 74 | } |
andrewboyson | 37:793b39683406 | 75 | int EthHandlePacket(void (*traceback)(void), void* pPacket, int* pSize) |
andrewboyson | 37:793b39683406 | 76 | { |
andrewboyson | 37:793b39683406 | 77 | pTraceBack = traceback; |
andrewboyson | 37:793b39683406 | 78 | tracePacket = pPacket; |
andrewboyson | 10:f0854784e960 | 79 | struct header * pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 80 | int dataLength = *pSize - HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 81 | void* pData = (char*)pPacket + HEADER_SIZE; |
andrewboyson | 14:e75a59c1123d | 82 | |
andrewboyson | 14:e75a59c1123d | 83 | if (!MacAccept(pHeader->dst)) return DO_NOTHING; |
andrewboyson | 13:9cd54f7db57a | 84 | |
andrewboyson | 42:222a4f45f916 | 85 | protocol = NetToHost16(pHeader->typ); |
andrewboyson | 42:222a4f45f916 | 86 | if (protocol < 1500) return DO_NOTHING; //drop 802.3 messages |
andrewboyson | 10:f0854784e960 | 87 | |
andrewboyson | 57:e0fb648acf48 | 88 | NetTraceHostCheckMac(pHeader->src); |
andrewboyson | 57:e0fb648acf48 | 89 | |
andrewboyson | 10:f0854784e960 | 90 | int action = DO_NOTHING; |
andrewboyson | 42:222a4f45f916 | 91 | switch (protocol) |
andrewboyson | 10:f0854784e960 | 92 | { |
andrewboyson | 37:793b39683406 | 93 | case ARP: action = ArpHandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break; |
andrewboyson | 37:793b39683406 | 94 | case IPV4: action = Ip4HandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break; |
andrewboyson | 37:793b39683406 | 95 | case IPV6: action = Ip6HandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); 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 | 47:73af5c0b0dc2 | 106 | if (!action) return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 107 | |
andrewboyson | 47:73af5c0b0dc2 | 108 | finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize); |
andrewboyson | 37:793b39683406 | 109 | |
andrewboyson | 37:793b39683406 | 110 | if (ActionGetTracePart(action)) LogHeader(pHeader); |
andrewboyson | 37:793b39683406 | 111 | |
andrewboyson | 10:f0854784e960 | 112 | return action; |
andrewboyson | 10:f0854784e960 | 113 | } |
andrewboyson | 10:f0854784e960 | 114 | int EthPollForPacketToSend(void* pPacket, int* pSize) |
andrewboyson | 10:f0854784e960 | 115 | { |
andrewboyson | 10:f0854784e960 | 116 | struct header * pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 117 | void* pData = (char*)pPacket + HEADER_SIZE; |
andrewboyson | 10:f0854784e960 | 118 | |
andrewboyson | 10:f0854784e960 | 119 | int dataLength = 0; |
andrewboyson | 42:222a4f45f916 | 120 | protocol = 0; |
andrewboyson | 10:f0854784e960 | 121 | int action = DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 122 | |
andrewboyson | 47:73af5c0b0dc2 | 123 | if (!action) |
andrewboyson | 10:f0854784e960 | 124 | { |
andrewboyson | 22:914b970356f0 | 125 | action = ArpPollForPacketToSend(pData, &dataLength); |
andrewboyson | 42:222a4f45f916 | 126 | protocol = ARP; |
andrewboyson | 22:914b970356f0 | 127 | } |
andrewboyson | 22:914b970356f0 | 128 | |
andrewboyson | 47:73af5c0b0dc2 | 129 | if (!action) |
andrewboyson | 22:914b970356f0 | 130 | { |
andrewboyson | 10:f0854784e960 | 131 | action = Ip6PollForPacketToSend(pData, &dataLength, pHeader->dst); |
andrewboyson | 42:222a4f45f916 | 132 | protocol = IPV6; |
andrewboyson | 10:f0854784e960 | 133 | } |
andrewboyson | 10:f0854784e960 | 134 | |
andrewboyson | 47:73af5c0b0dc2 | 135 | if (!action) |
andrewboyson | 10:f0854784e960 | 136 | { |
andrewboyson | 10:f0854784e960 | 137 | action = Ip4PollForPacketToSend(pData, &dataLength, pHeader->dst); |
andrewboyson | 42:222a4f45f916 | 138 | protocol = IPV4; |
andrewboyson | 10:f0854784e960 | 139 | } |
andrewboyson | 10:f0854784e960 | 140 | |
andrewboyson | 47:73af5c0b0dc2 | 141 | if (!action) return DO_NOTHING; |
andrewboyson | 47:73af5c0b0dc2 | 142 | |
andrewboyson | 47:73af5c0b0dc2 | 143 | finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize); |
andrewboyson | 10:f0854784e960 | 144 | |
andrewboyson | 37:793b39683406 | 145 | if (ActionGetTracePart(action)) LogHeader(pHeader); |
andrewboyson | 13:9cd54f7db57a | 146 | |
andrewboyson | 10:f0854784e960 | 147 | return action; |
andrewboyson | 10:f0854784e960 | 148 | } |
andrewboyson | 10:f0854784e960 | 149 |