Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
ip6/icmp/icmp6.cpp@49:1a6336f2b3f9, 2017-11-02 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Nov 02 08:10:55 2017 +0000
- Revision:
- 49:1a6336f2b3f9
- Parent:
- 48:952dddb74b8b
- Child:
- 57:e0fb648acf48
Separated the address handling parts of IP4 and IP6 from the protocol parts.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 43:bc028d5a6424 | 1 | #include "mbed.h" |
| andrewboyson | 43:bc028d5a6424 | 2 | #include "log.h" |
| andrewboyson | 43:bc028d5a6424 | 3 | #include "net.h" |
| andrewboyson | 43:bc028d5a6424 | 4 | #include "action.h" |
| andrewboyson | 43:bc028d5a6424 | 5 | #include "ns.h" |
| andrewboyson | 43:bc028d5a6424 | 6 | #include "ra.h" |
| andrewboyson | 46:40d33e9037e4 | 7 | #include "rs.h" |
| andrewboyson | 43:bc028d5a6424 | 8 | #include "ip.h" |
| andrewboyson | 49:1a6336f2b3f9 | 9 | #include "ip6addr.h" |
| andrewboyson | 43:bc028d5a6424 | 10 | #include "slaac.h" |
| andrewboyson | 43:bc028d5a6424 | 11 | #include "echo6.h" |
| andrewboyson | 43:bc028d5a6424 | 12 | |
| andrewboyson | 43:bc028d5a6424 | 13 | #define HEADER_LENGTH 4 |
| andrewboyson | 43:bc028d5a6424 | 14 | __packed struct header |
| andrewboyson | 43:bc028d5a6424 | 15 | { |
| andrewboyson | 43:bc028d5a6424 | 16 | uint8_t type; |
| andrewboyson | 43:bc028d5a6424 | 17 | uint8_t code; |
| andrewboyson | 43:bc028d5a6424 | 18 | uint16_t checksum; |
| andrewboyson | 43:bc028d5a6424 | 19 | }; |
| andrewboyson | 43:bc028d5a6424 | 20 | static uint8_t type; |
| andrewboyson | 43:bc028d5a6424 | 21 | static uint8_t code; |
| andrewboyson | 43:bc028d5a6424 | 22 | static uint16_t checksum; |
| andrewboyson | 43:bc028d5a6424 | 23 | static uint16_t calculatedChecksum; |
| andrewboyson | 43:bc028d5a6424 | 24 | static int dataLength; |
| andrewboyson | 43:bc028d5a6424 | 25 | static void* pData; |
| andrewboyson | 43:bc028d5a6424 | 26 | |
| andrewboyson | 47:73af5c0b0dc2 | 27 | static void logType(uint8_t type) |
| andrewboyson | 43:bc028d5a6424 | 28 | { |
| andrewboyson | 43:bc028d5a6424 | 29 | switch (type) |
| andrewboyson | 43:bc028d5a6424 | 30 | { |
| andrewboyson | 49:1a6336f2b3f9 | 31 | case 1: Log ("Destination unreacheable" ); break; |
| andrewboyson | 49:1a6336f2b3f9 | 32 | case 128: Log ("Echo Request" ); break; |
| andrewboyson | 49:1a6336f2b3f9 | 33 | case 129: Log ("Echo Reply" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 34 | case 133: Log ("Router solicit" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 35 | case 134: Log ("Router advertisement" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 36 | case 135: Log ("Neighbour solicit" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 37 | case 136: Log ("Neighbour advertisement"); break; |
| andrewboyson | 47:73af5c0b0dc2 | 38 | case 137: Log ("Redirect" ); break; |
| andrewboyson | 47:73af5c0b0dc2 | 39 | default: LogF("Unknown type %u", type ); break; |
| andrewboyson | 43:bc028d5a6424 | 40 | } |
| andrewboyson | 43:bc028d5a6424 | 41 | } |
| andrewboyson | 43:bc028d5a6424 | 42 | static uint16_t calculateChecksum(char* pSrcIp, char* pDstIp, int size, void* pPacket) |
| andrewboyson | 43:bc028d5a6424 | 43 | { |
| andrewboyson | 43:bc028d5a6424 | 44 | __packed struct pseudo |
| andrewboyson | 43:bc028d5a6424 | 45 | { |
| andrewboyson | 43:bc028d5a6424 | 46 | char src[16]; |
| andrewboyson | 43:bc028d5a6424 | 47 | char dst[16]; |
| andrewboyson | 43:bc028d5a6424 | 48 | uint32_t len; |
| andrewboyson | 43:bc028d5a6424 | 49 | uint8_t zer[3]; |
| andrewboyson | 43:bc028d5a6424 | 50 | uint8_t pro; |
| andrewboyson | 43:bc028d5a6424 | 51 | } pseudo; |
| andrewboyson | 43:bc028d5a6424 | 52 | |
| andrewboyson | 49:1a6336f2b3f9 | 53 | Ip6AddressCopy(pseudo.src, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 54 | Ip6AddressCopy(pseudo.dst, pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 55 | pseudo.len = NetToHost32(size); |
| andrewboyson | 43:bc028d5a6424 | 56 | pseudo.zer[0] = 0; |
| andrewboyson | 43:bc028d5a6424 | 57 | pseudo.zer[1] = 0; |
| andrewboyson | 43:bc028d5a6424 | 58 | pseudo.zer[2] = 0; |
| andrewboyson | 43:bc028d5a6424 | 59 | pseudo.pro = ICMP6; |
| andrewboyson | 43:bc028d5a6424 | 60 | |
| andrewboyson | 43:bc028d5a6424 | 61 | return NetCheckSumTwo(sizeof(pseudo), &pseudo, size, pPacket); |
| andrewboyson | 43:bc028d5a6424 | 62 | } |
| andrewboyson | 43:bc028d5a6424 | 63 | static void logHeader() |
| andrewboyson | 43:bc028d5a6424 | 64 | { |
| andrewboyson | 43:bc028d5a6424 | 65 | if (NetTraceVerbose) |
| andrewboyson | 43:bc028d5a6424 | 66 | { |
| andrewboyson | 43:bc028d5a6424 | 67 | Log ("ICMP6 header\r\n"); |
| andrewboyson | 47:73af5c0b0dc2 | 68 | Log (" Type "); logType(type); Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 69 | LogF(" Code %u\r\n", code); |
| andrewboyson | 43:bc028d5a6424 | 70 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
| andrewboyson | 43:bc028d5a6424 | 71 | LogF(" Calculated %04hX\r\n", calculatedChecksum); |
| andrewboyson | 43:bc028d5a6424 | 72 | LogF(" Data length %d\r\n", dataLength); |
| andrewboyson | 43:bc028d5a6424 | 73 | } |
| andrewboyson | 43:bc028d5a6424 | 74 | else |
| andrewboyson | 43:bc028d5a6424 | 75 | { |
| andrewboyson | 43:bc028d5a6424 | 76 | Log ("ICMP6 header "); |
| andrewboyson | 47:73af5c0b0dc2 | 77 | logType(type); |
| andrewboyson | 43:bc028d5a6424 | 78 | Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 79 | } |
| andrewboyson | 43:bc028d5a6424 | 80 | } |
| andrewboyson | 43:bc028d5a6424 | 81 | static void readHeader(char* pSrcIp, char* pDstIp, void* pPacket, int size) |
| andrewboyson | 43:bc028d5a6424 | 82 | { |
| andrewboyson | 43:bc028d5a6424 | 83 | struct header* pHeader = (header*)pPacket; |
| andrewboyson | 43:bc028d5a6424 | 84 | type = pHeader->type; |
| andrewboyson | 43:bc028d5a6424 | 85 | code = pHeader->code; |
| andrewboyson | 43:bc028d5a6424 | 86 | checksum = NetToHost16(pHeader->checksum); |
| andrewboyson | 43:bc028d5a6424 | 87 | calculatedChecksum = calculateChecksum(pSrcIp, pDstIp, size, pPacket); |
| andrewboyson | 43:bc028d5a6424 | 88 | pData = (char*)pPacket + HEADER_LENGTH; |
| andrewboyson | 43:bc028d5a6424 | 89 | dataLength = size - HEADER_LENGTH; |
| andrewboyson | 43:bc028d5a6424 | 90 | } |
| andrewboyson | 43:bc028d5a6424 | 91 | static void writeHeader(void* pPacket, int size, char* pSrcIp, char* pDstIp) |
| andrewboyson | 43:bc028d5a6424 | 92 | { |
| andrewboyson | 43:bc028d5a6424 | 93 | struct header* pHeader = (header*)pPacket; |
| andrewboyson | 43:bc028d5a6424 | 94 | pHeader->type = type; |
| andrewboyson | 43:bc028d5a6424 | 95 | pHeader->code = code; |
| andrewboyson | 43:bc028d5a6424 | 96 | pHeader->checksum = 0; |
| andrewboyson | 47:73af5c0b0dc2 | 97 | checksum = calculateChecksum(pSrcIp, pDstIp, size, pPacket); |
| andrewboyson | 47:73af5c0b0dc2 | 98 | pHeader->checksum = checksum; |
| andrewboyson | 43:bc028d5a6424 | 99 | calculatedChecksum = 0; |
| andrewboyson | 43:bc028d5a6424 | 100 | } |
| andrewboyson | 43:bc028d5a6424 | 101 | static void (*pTraceBack)(void); |
| andrewboyson | 43:bc028d5a6424 | 102 | static void trace() |
| andrewboyson | 43:bc028d5a6424 | 103 | { |
| andrewboyson | 43:bc028d5a6424 | 104 | pTraceBack(); |
| andrewboyson | 43:bc028d5a6424 | 105 | logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 106 | } |
| andrewboyson | 44:83ce5ace337b | 107 | int Icmp6HandleReceivedPacket(void (*traceback)(void), int scope, char* pSrcIp, char* pDstIp, int* pSize, void* pPacket) |
| andrewboyson | 43:bc028d5a6424 | 108 | { |
| andrewboyson | 43:bc028d5a6424 | 109 | pTraceBack = traceback; |
| andrewboyson | 43:bc028d5a6424 | 110 | |
| andrewboyson | 43:bc028d5a6424 | 111 | readHeader(pSrcIp, pDstIp, pPacket, *pSize); |
| andrewboyson | 43:bc028d5a6424 | 112 | |
| andrewboyson | 43:bc028d5a6424 | 113 | int action = DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 114 | switch (type) |
| andrewboyson | 43:bc028d5a6424 | 115 | { |
| andrewboyson | 43:bc028d5a6424 | 116 | case 128: //Echo request - Ping |
| andrewboyson | 43:bc028d5a6424 | 117 | action = Echo6HandleRequest(trace, &type, &code); |
| andrewboyson | 43:bc028d5a6424 | 118 | break; |
| andrewboyson | 43:bc028d5a6424 | 119 | case 133: //Router solicit |
| andrewboyson | 43:bc028d5a6424 | 120 | return DO_NOTHING; //We are not a router so quietly drop this |
| andrewboyson | 43:bc028d5a6424 | 121 | case 134: //Router advertisement |
| andrewboyson | 43:bc028d5a6424 | 122 | action = RaHandleReceivedAdvertisement(trace, pData, &dataLength); |
| andrewboyson | 43:bc028d5a6424 | 123 | break; |
| andrewboyson | 43:bc028d5a6424 | 124 | case 135: //Neighbour solicit |
| andrewboyson | 43:bc028d5a6424 | 125 | action = NsHandleReceivedSolicitation(trace, pData, &dataLength, &type, &code); |
| andrewboyson | 43:bc028d5a6424 | 126 | break; |
| andrewboyson | 43:bc028d5a6424 | 127 | case 136: //Neighbour advertisement |
| andrewboyson | 43:bc028d5a6424 | 128 | action = NsHandleReceivedAdvertisement(trace, pData, &dataLength); |
| andrewboyson | 43:bc028d5a6424 | 129 | break; |
| andrewboyson | 43:bc028d5a6424 | 130 | default: |
| andrewboyson | 48:952dddb74b8b | 131 | LogTimeF("ICMP6 unknown packet type %d\r\n", type); |
| andrewboyson | 43:bc028d5a6424 | 132 | return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 133 | } |
| andrewboyson | 43:bc028d5a6424 | 134 | if (!action) return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 135 | |
| andrewboyson | 49:1a6336f2b3f9 | 136 | Ip6AddressCopy(pDstIp, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 137 | SlaacAddressFromScope(scope, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 138 | Ip6AddressFromDest (ActionGetDestPart(action), pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 139 | |
| andrewboyson | 43:bc028d5a6424 | 140 | *pSize = HEADER_LENGTH + dataLength; |
| andrewboyson | 43:bc028d5a6424 | 141 | |
| andrewboyson | 43:bc028d5a6424 | 142 | writeHeader(pPacket, *pSize, pSrcIp, pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 143 | |
| andrewboyson | 43:bc028d5a6424 | 144 | if (ActionGetTracePart(action)) logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 145 | |
| andrewboyson | 43:bc028d5a6424 | 146 | return action; |
| andrewboyson | 43:bc028d5a6424 | 147 | } |
| andrewboyson | 43:bc028d5a6424 | 148 | int Icmp6PollForPacketToSend(void* pPacket, int* pSize, char* pSrcIp, char* pDstIp) |
| andrewboyson | 43:bc028d5a6424 | 149 | { |
| andrewboyson | 43:bc028d5a6424 | 150 | pData = (char*)pPacket + HEADER_LENGTH; |
| andrewboyson | 43:bc028d5a6424 | 151 | int action = DO_NOTHING; |
| andrewboyson | 46:40d33e9037e4 | 152 | if (!action) action = RsGetWaitingSolicitation(pData, &dataLength, &type, &code); |
| andrewboyson | 43:bc028d5a6424 | 153 | if (!action) action = NsGetWaitingSolicitation(pData, &dataLength, &type, &code); |
| andrewboyson | 43:bc028d5a6424 | 154 | if (!action) return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 155 | |
| andrewboyson | 44:83ce5ace337b | 156 | int scope = SCOPE_LOCAL; |
| andrewboyson | 49:1a6336f2b3f9 | 157 | SlaacAddressFromScope(scope, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 158 | Ip6AddressFromDest (ActionGetDestPart(action), pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 159 | |
| andrewboyson | 43:bc028d5a6424 | 160 | *pSize = HEADER_LENGTH + dataLength; |
| andrewboyson | 43:bc028d5a6424 | 161 | |
| andrewboyson | 43:bc028d5a6424 | 162 | writeHeader(pPacket, *pSize, pSrcIp, pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 163 | |
| andrewboyson | 43:bc028d5a6424 | 164 | if (ActionGetTracePart(action)) logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 165 | |
| andrewboyson | 43:bc028d5a6424 | 166 | return action; |
| andrewboyson | 43:bc028d5a6424 | 167 | |
| andrewboyson | 43:bc028d5a6424 | 168 | } |