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.c@125:8c84daac38ab, 2019-02-22 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Feb 22 11:26:55 2019 +0000
- Revision:
- 125:8c84daac38ab
- Parent:
- 61:aad055f1b0d1
- Child:
- 136:8a65abb0dc63
tidied up ntp data types for calculating ms from clktime.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
| andrewboyson | 61:aad055f1b0d1 | 2 | |
| andrewboyson | 57:e0fb648acf48 | 3 | #include "log.h" |
| andrewboyson | 57:e0fb648acf48 | 4 | #include "net.h" |
| andrewboyson | 57:e0fb648acf48 | 5 | #include "action.h" |
| andrewboyson | 57:e0fb648acf48 | 6 | #include "ns.h" |
| andrewboyson | 57:e0fb648acf48 | 7 | #include "ra.h" |
| andrewboyson | 57:e0fb648acf48 | 8 | #include "rs.h" |
| andrewboyson | 57:e0fb648acf48 | 9 | #include "ip.h" |
| andrewboyson | 49:1a6336f2b3f9 | 10 | #include "ip6addr.h" |
| andrewboyson | 57:e0fb648acf48 | 11 | #include "slaac.h" |
| andrewboyson | 57:e0fb648acf48 | 12 | #include "echo6.h" |
| andrewboyson | 57:e0fb648acf48 | 13 | #include "dest6.h" |
| andrewboyson | 43:bc028d5a6424 | 14 | |
| andrewboyson | 43:bc028d5a6424 | 15 | __packed struct header |
| andrewboyson | 43:bc028d5a6424 | 16 | { |
| andrewboyson | 43:bc028d5a6424 | 17 | uint8_t type; |
| andrewboyson | 43:bc028d5a6424 | 18 | uint8_t code; |
| andrewboyson | 43:bc028d5a6424 | 19 | uint16_t checksum; |
| andrewboyson | 43:bc028d5a6424 | 20 | }; |
| andrewboyson | 43:bc028d5a6424 | 21 | static uint8_t type; |
| andrewboyson | 43:bc028d5a6424 | 22 | static uint8_t code; |
| andrewboyson | 43:bc028d5a6424 | 23 | static uint16_t checksum; |
| andrewboyson | 43:bc028d5a6424 | 24 | static uint16_t calculatedChecksum; |
| andrewboyson | 43:bc028d5a6424 | 25 | |
| andrewboyson | 47:73af5c0b0dc2 | 26 | static void logType(uint8_t type) |
| andrewboyson | 43:bc028d5a6424 | 27 | { |
| andrewboyson | 43:bc028d5a6424 | 28 | switch (type) |
| andrewboyson | 43:bc028d5a6424 | 29 | { |
| andrewboyson | 49:1a6336f2b3f9 | 30 | case 1: Log ("Destination unreacheable" ); break; |
| andrewboyson | 49:1a6336f2b3f9 | 31 | case 128: Log ("Echo Request" ); break; |
| andrewboyson | 49:1a6336f2b3f9 | 32 | case 129: Log ("Echo Reply" ); break; |
| andrewboyson | 57:e0fb648acf48 | 33 | case 133: Log ("Router solicit" ); break; |
| andrewboyson | 57:e0fb648acf48 | 34 | case 134: Log ("Router advertisement" ); break; |
| andrewboyson | 57:e0fb648acf48 | 35 | case 135: Log ("Neighbour solicit" ); break; |
| andrewboyson | 57:e0fb648acf48 | 36 | case 136: Log ("Neighbour advertisement" ); break; |
| andrewboyson | 57:e0fb648acf48 | 37 | case 137: Log ("Redirect" ); break; |
| andrewboyson | 57:e0fb648acf48 | 38 | default: LogF("Unknown type %u", type ); break; |
| andrewboyson | 43:bc028d5a6424 | 39 | } |
| andrewboyson | 43:bc028d5a6424 | 40 | } |
| andrewboyson | 43:bc028d5a6424 | 41 | static uint16_t calculateChecksum(char* pSrcIp, char* pDstIp, int size, void* pPacket) |
| andrewboyson | 43:bc028d5a6424 | 42 | { |
| andrewboyson | 43:bc028d5a6424 | 43 | __packed struct pseudo |
| andrewboyson | 43:bc028d5a6424 | 44 | { |
| andrewboyson | 43:bc028d5a6424 | 45 | char src[16]; |
| andrewboyson | 43:bc028d5a6424 | 46 | char dst[16]; |
| andrewboyson | 43:bc028d5a6424 | 47 | uint32_t len; |
| andrewboyson | 43:bc028d5a6424 | 48 | uint8_t zer[3]; |
| andrewboyson | 43:bc028d5a6424 | 49 | uint8_t pro; |
| andrewboyson | 43:bc028d5a6424 | 50 | } pseudo; |
| andrewboyson | 43:bc028d5a6424 | 51 | |
| andrewboyson | 49:1a6336f2b3f9 | 52 | Ip6AddressCopy(pseudo.src, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 53 | Ip6AddressCopy(pseudo.dst, pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 54 | pseudo.len = NetToHost32(size); |
| andrewboyson | 43:bc028d5a6424 | 55 | pseudo.zer[0] = 0; |
| andrewboyson | 43:bc028d5a6424 | 56 | pseudo.zer[1] = 0; |
| andrewboyson | 43:bc028d5a6424 | 57 | pseudo.zer[2] = 0; |
| andrewboyson | 43:bc028d5a6424 | 58 | pseudo.pro = ICMP6; |
| andrewboyson | 43:bc028d5a6424 | 59 | |
| andrewboyson | 43:bc028d5a6424 | 60 | return NetCheckSumTwo(sizeof(pseudo), &pseudo, size, pPacket); |
| andrewboyson | 43:bc028d5a6424 | 61 | } |
| andrewboyson | 43:bc028d5a6424 | 62 | static void logHeader() |
| andrewboyson | 43:bc028d5a6424 | 63 | { |
| andrewboyson | 43:bc028d5a6424 | 64 | if (NetTraceVerbose) |
| andrewboyson | 43:bc028d5a6424 | 65 | { |
| andrewboyson | 43:bc028d5a6424 | 66 | Log ("ICMP6 header\r\n"); |
| andrewboyson | 47:73af5c0b0dc2 | 67 | Log (" Type "); logType(type); Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 68 | LogF(" Code %u\r\n", code); |
| andrewboyson | 43:bc028d5a6424 | 69 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
| andrewboyson | 43:bc028d5a6424 | 70 | LogF(" Calculated %04hX\r\n", calculatedChecksum); |
| andrewboyson | 43:bc028d5a6424 | 71 | } |
| andrewboyson | 43:bc028d5a6424 | 72 | else |
| andrewboyson | 43:bc028d5a6424 | 73 | { |
| andrewboyson | 43:bc028d5a6424 | 74 | Log ("ICMP6 header "); |
| andrewboyson | 47:73af5c0b0dc2 | 75 | logType(type); |
| andrewboyson | 43:bc028d5a6424 | 76 | Log("\r\n"); |
| andrewboyson | 43:bc028d5a6424 | 77 | } |
| andrewboyson | 43:bc028d5a6424 | 78 | } |
| andrewboyson | 43:bc028d5a6424 | 79 | static void readHeader(char* pSrcIp, char* pDstIp, void* pPacket, int size) |
| andrewboyson | 43:bc028d5a6424 | 80 | { |
| andrewboyson | 61:aad055f1b0d1 | 81 | struct header* pHeader = (struct header*)pPacket; |
| andrewboyson | 43:bc028d5a6424 | 82 | type = pHeader->type; |
| andrewboyson | 43:bc028d5a6424 | 83 | code = pHeader->code; |
| andrewboyson | 43:bc028d5a6424 | 84 | checksum = NetToHost16(pHeader->checksum); |
| andrewboyson | 43:bc028d5a6424 | 85 | calculatedChecksum = calculateChecksum(pSrcIp, pDstIp, size, pPacket); |
| andrewboyson | 43:bc028d5a6424 | 86 | } |
| andrewboyson | 43:bc028d5a6424 | 87 | static void writeHeader(void* pPacket, int size, char* pSrcIp, char* pDstIp) |
| andrewboyson | 43:bc028d5a6424 | 88 | { |
| andrewboyson | 61:aad055f1b0d1 | 89 | struct header* pHeader = (struct header*)pPacket; |
| andrewboyson | 43:bc028d5a6424 | 90 | pHeader->type = type; |
| andrewboyson | 43:bc028d5a6424 | 91 | pHeader->code = code; |
| andrewboyson | 43:bc028d5a6424 | 92 | pHeader->checksum = 0; |
| andrewboyson | 47:73af5c0b0dc2 | 93 | checksum = calculateChecksum(pSrcIp, pDstIp, size, pPacket); |
| andrewboyson | 47:73af5c0b0dc2 | 94 | pHeader->checksum = checksum; |
| andrewboyson | 43:bc028d5a6424 | 95 | calculatedChecksum = 0; |
| andrewboyson | 43:bc028d5a6424 | 96 | } |
| andrewboyson | 43:bc028d5a6424 | 97 | static void (*pTraceBack)(void); |
| andrewboyson | 43:bc028d5a6424 | 98 | static void trace() |
| andrewboyson | 43:bc028d5a6424 | 99 | { |
| andrewboyson | 43:bc028d5a6424 | 100 | pTraceBack(); |
| andrewboyson | 43:bc028d5a6424 | 101 | logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 102 | } |
| andrewboyson | 59:e0e556c8bd46 | 103 | int Icmp6HandleReceivedPacket(void (*traceback)(void), int scope, void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* pSrcIp, char* pDstIp) |
| andrewboyson | 43:bc028d5a6424 | 104 | { |
| andrewboyson | 43:bc028d5a6424 | 105 | pTraceBack = traceback; |
| andrewboyson | 59:e0e556c8bd46 | 106 | |
| andrewboyson | 59:e0e556c8bd46 | 107 | readHeader(pSrcIp, pDstIp, pPacketRx, sizeRx); |
| andrewboyson | 43:bc028d5a6424 | 108 | |
| andrewboyson | 61:aad055f1b0d1 | 109 | int dataLengthRx = sizeRx - sizeof(struct header); |
| andrewboyson | 61:aad055f1b0d1 | 110 | int dataLengthTx = *pSizeTx - sizeof(struct header); |
| andrewboyson | 61:aad055f1b0d1 | 111 | char* pPayloadRx = (char*)pPacketRx + sizeof(struct header); |
| andrewboyson | 61:aad055f1b0d1 | 112 | char* pPayloadTx = (char*)pPacketTx + sizeof(struct header); |
| andrewboyson | 43:bc028d5a6424 | 113 | |
| andrewboyson | 43:bc028d5a6424 | 114 | int action = DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 115 | switch (type) |
| andrewboyson | 43:bc028d5a6424 | 116 | { |
| andrewboyson | 57:e0fb648acf48 | 117 | case 1: //Destination unreacheable |
| andrewboyson | 57:e0fb648acf48 | 118 | action = Dest6HandleRequest(trace, &type, &code); |
| andrewboyson | 57:e0fb648acf48 | 119 | break; |
| andrewboyson | 43:bc028d5a6424 | 120 | case 128: //Echo request - Ping |
| andrewboyson | 59:e0e556c8bd46 | 121 | action = Echo6HandleRequest(trace, &type, &code, pPayloadRx, dataLengthRx, pPayloadTx, &dataLengthTx); |
| andrewboyson | 43:bc028d5a6424 | 122 | break; |
| andrewboyson | 43:bc028d5a6424 | 123 | case 133: //Router solicit |
| andrewboyson | 43:bc028d5a6424 | 124 | return DO_NOTHING; //We are not a router so quietly drop this |
| andrewboyson | 43:bc028d5a6424 | 125 | case 134: //Router advertisement |
| andrewboyson | 59:e0e556c8bd46 | 126 | action = RaHandleReceivedAdvertisement(trace, pPayloadRx, &dataLengthRx); |
| andrewboyson | 43:bc028d5a6424 | 127 | break; |
| andrewboyson | 43:bc028d5a6424 | 128 | case 135: //Neighbour solicit |
| andrewboyson | 59:e0e556c8bd46 | 129 | action = NsHandleReceivedSolicitation(trace, &type, &code, pPayloadRx, dataLengthRx, pPayloadTx, &dataLengthTx); |
| andrewboyson | 43:bc028d5a6424 | 130 | break; |
| andrewboyson | 43:bc028d5a6424 | 131 | case 136: //Neighbour advertisement |
| andrewboyson | 59:e0e556c8bd46 | 132 | action = NsHandleReceivedAdvertisement(trace, pPayloadRx, &dataLengthRx); |
| andrewboyson | 43:bc028d5a6424 | 133 | break; |
| andrewboyson | 43:bc028d5a6424 | 134 | default: |
| andrewboyson | 48:952dddb74b8b | 135 | LogTimeF("ICMP6 unknown packet type %d\r\n", type); |
| andrewboyson | 43:bc028d5a6424 | 136 | return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 137 | } |
| andrewboyson | 43:bc028d5a6424 | 138 | if (!action) return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 139 | |
| andrewboyson | 49:1a6336f2b3f9 | 140 | Ip6AddressCopy(pDstIp, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 141 | SlaacAddressFromScope(scope, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 142 | Ip6AddressFromDest (ActionGetDestPart(action), pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 143 | |
| andrewboyson | 61:aad055f1b0d1 | 144 | *pSizeTx = sizeof(struct header) + dataLengthTx; |
| andrewboyson | 43:bc028d5a6424 | 145 | |
| andrewboyson | 59:e0e556c8bd46 | 146 | writeHeader(pPacketTx, *pSizeTx, pSrcIp, pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 147 | |
| andrewboyson | 43:bc028d5a6424 | 148 | if (ActionGetTracePart(action)) logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 149 | |
| andrewboyson | 43:bc028d5a6424 | 150 | return action; |
| andrewboyson | 43:bc028d5a6424 | 151 | } |
| andrewboyson | 43:bc028d5a6424 | 152 | int Icmp6PollForPacketToSend(void* pPacket, int* pSize, char* pSrcIp, char* pDstIp) |
| andrewboyson | 43:bc028d5a6424 | 153 | { |
| andrewboyson | 61:aad055f1b0d1 | 154 | char* pData = (char*)pPacket + sizeof(struct header); |
| andrewboyson | 61:aad055f1b0d1 | 155 | int dataLength = *pSize - sizeof(struct header); |
| andrewboyson | 43:bc028d5a6424 | 156 | int action = DO_NOTHING; |
| andrewboyson | 46:40d33e9037e4 | 157 | if (!action) action = RsGetWaitingSolicitation(pData, &dataLength, &type, &code); |
| andrewboyson | 43:bc028d5a6424 | 158 | if (!action) action = NsGetWaitingSolicitation(pData, &dataLength, &type, &code); |
| andrewboyson | 43:bc028d5a6424 | 159 | if (!action) return DO_NOTHING; |
| andrewboyson | 43:bc028d5a6424 | 160 | |
| andrewboyson | 44:83ce5ace337b | 161 | int scope = SCOPE_LOCAL; |
| andrewboyson | 49:1a6336f2b3f9 | 162 | SlaacAddressFromScope(scope, pSrcIp); |
| andrewboyson | 49:1a6336f2b3f9 | 163 | Ip6AddressFromDest (ActionGetDestPart(action), pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 164 | |
| andrewboyson | 61:aad055f1b0d1 | 165 | *pSize = sizeof(struct header) + dataLength; |
| andrewboyson | 43:bc028d5a6424 | 166 | |
| andrewboyson | 43:bc028d5a6424 | 167 | writeHeader(pPacket, *pSize, pSrcIp, pDstIp); |
| andrewboyson | 43:bc028d5a6424 | 168 | |
| andrewboyson | 43:bc028d5a6424 | 169 | if (ActionGetTracePart(action)) logHeader(); |
| andrewboyson | 43:bc028d5a6424 | 170 | |
| andrewboyson | 43:bc028d5a6424 | 171 | return action; |
| andrewboyson | 43:bc028d5a6424 | 172 | |
| andrewboyson | 43:bc028d5a6424 | 173 | } |