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/ndp/ns.c@140:9000ea70b220, 2019-04-10 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Apr 10 10:07:06 2019 +0000
- Revision:
- 140:9000ea70b220
- Parent:
- 136:8a65abb0dc63
- Child:
- 172:9bc3c7b2cca1
Added ajax functions to AR4, AR6, NR4, NR6 modules
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 | #include <stdbool.h> |
| andrewboyson | 61:aad055f1b0d1 | 3 | |
| andrewboyson | 136:8a65abb0dc63 | 4 | #include "log.h" |
| andrewboyson | 136:8a65abb0dc63 | 5 | #include "net.h" |
| andrewboyson | 136:8a65abb0dc63 | 6 | #include "action.h" |
| andrewboyson | 49:1a6336f2b3f9 | 7 | #include "ip6addr.h" |
| andrewboyson | 136:8a65abb0dc63 | 8 | #include "mac.h" |
| andrewboyson | 136:8a65abb0dc63 | 9 | #include "nr6.h" |
| andrewboyson | 136:8a65abb0dc63 | 10 | #include "ar6.h" |
| andrewboyson | 136:8a65abb0dc63 | 11 | #include "ip6.h" |
| andrewboyson | 136:8a65abb0dc63 | 12 | #include "slaac.h" |
| andrewboyson | 136:8a65abb0dc63 | 13 | #include "ndp.h" |
| andrewboyson | 45:3dd57903ec99 | 14 | |
| andrewboyson | 45:3dd57903ec99 | 15 | bool NsTraceRecvSol = false; |
| andrewboyson | 45:3dd57903ec99 | 16 | bool NsTraceRecvAdv = false; |
| andrewboyson | 45:3dd57903ec99 | 17 | bool NsTraceSendSol = false; |
| andrewboyson | 45:3dd57903ec99 | 18 | |
| andrewboyson | 45:3dd57903ec99 | 19 | char NsAddressToResolve[16]; |
| andrewboyson | 45:3dd57903ec99 | 20 | bool NsResolveRequestFlag = false; |
| andrewboyson | 45:3dd57903ec99 | 21 | |
| andrewboyson | 136:8a65abb0dc63 | 22 | static char* hdrPtrReserved(char* pPacket) { return pPacket + 0; } // 4 |
| andrewboyson | 136:8a65abb0dc63 | 23 | static char* hdrPtrTarget (char* pPacket) { return pPacket + 4; } //16 |
| andrewboyson | 136:8a65abb0dc63 | 24 | const int HEADER_LENGTH = 20; |
| andrewboyson | 136:8a65abb0dc63 | 25 | static void hdrSetReserved(char* pPacket, uint32_t value) { NetInvert32(hdrPtrReserved(pPacket), &value); } |
| andrewboyson | 136:8a65abb0dc63 | 26 | |
| andrewboyson | 136:8a65abb0dc63 | 27 | static void logHeader(char* pPacket, int size) |
| andrewboyson | 45:3dd57903ec99 | 28 | { |
| andrewboyson | 136:8a65abb0dc63 | 29 | char* pData = pPacket + HEADER_LENGTH; |
| andrewboyson | 136:8a65abb0dc63 | 30 | int dataLength = size - HEADER_LENGTH; |
| andrewboyson | 45:3dd57903ec99 | 31 | |
| andrewboyson | 47:73af5c0b0dc2 | 32 | if (NetTraceVerbose) |
| andrewboyson | 45:3dd57903ec99 | 33 | { |
| andrewboyson | 47:73af5c0b0dc2 | 34 | Log("NS header\r\n"); |
| andrewboyson | 47:73af5c0b0dc2 | 35 | LogF(" Size %d\r\n", size); |
| andrewboyson | 136:8a65abb0dc63 | 36 | LogF(" Target "); Ip6AddressLog(hdrPtrTarget(pPacket)); Log("\r\n"); |
| andrewboyson | 47:73af5c0b0dc2 | 37 | NdpLogOptionsVerbose(pData, dataLength); |
| andrewboyson | 45:3dd57903ec99 | 38 | } |
| andrewboyson | 47:73af5c0b0dc2 | 39 | else |
| andrewboyson | 47:73af5c0b0dc2 | 40 | { |
| andrewboyson | 47:73af5c0b0dc2 | 41 | Log("NS header "); |
| andrewboyson | 136:8a65abb0dc63 | 42 | Ip6AddressLog(hdrPtrTarget(pPacket)); |
| andrewboyson | 47:73af5c0b0dc2 | 43 | NdpLogOptionsQuiet(pData, dataLength); |
| andrewboyson | 47:73af5c0b0dc2 | 44 | Log("\r\n"); |
| andrewboyson | 47:73af5c0b0dc2 | 45 | } |
| andrewboyson | 45:3dd57903ec99 | 46 | } |
| andrewboyson | 136:8a65abb0dc63 | 47 | static char* pTraceHeader; |
| andrewboyson | 59:e0e556c8bd46 | 48 | static int traceSize; |
| andrewboyson | 48:952dddb74b8b | 49 | static void (*pTraceBack)(void); |
| andrewboyson | 48:952dddb74b8b | 50 | static void trace() |
| andrewboyson | 48:952dddb74b8b | 51 | { |
| andrewboyson | 48:952dddb74b8b | 52 | pTraceBack(); |
| andrewboyson | 59:e0e556c8bd46 | 53 | logHeader(pTraceHeader, traceSize); |
| andrewboyson | 48:952dddb74b8b | 54 | } |
| andrewboyson | 136:8a65abb0dc63 | 55 | int NsHandleReceivedSolicitation(void (*traceback)(void), uint8_t* pType, uint8_t* pCode, char* pPacketRx, int sizeRx, char* pPacketTx, int* pSizeTx) |
| andrewboyson | 45:3dd57903ec99 | 56 | { |
| andrewboyson | 48:952dddb74b8b | 57 | pTraceBack = traceback; |
| andrewboyson | 59:e0e556c8bd46 | 58 | |
| andrewboyson | 136:8a65abb0dc63 | 59 | pTraceHeader = pPacketRx; |
| andrewboyson | 59:e0e556c8bd46 | 60 | traceSize = sizeRx; |
| andrewboyson | 45:3dd57903ec99 | 61 | |
| andrewboyson | 45:3dd57903ec99 | 62 | //Check it is us |
| andrewboyson | 136:8a65abb0dc63 | 63 | if (!SlaacScope(hdrPtrTarget(pPacketRx))) return DO_NOTHING; |
| andrewboyson | 45:3dd57903ec99 | 64 | |
| andrewboyson | 45:3dd57903ec99 | 65 | if (NsTraceRecvSol) |
| andrewboyson | 45:3dd57903ec99 | 66 | { |
| andrewboyson | 45:3dd57903ec99 | 67 | if (NetTraceNewLine) Log("\r\n"); |
| andrewboyson | 45:3dd57903ec99 | 68 | LogTimeF("NDP received neighbour solicit\r\n"); |
| andrewboyson | 45:3dd57903ec99 | 69 | if (NetTraceStack) traceback(); |
| andrewboyson | 136:8a65abb0dc63 | 70 | logHeader(pPacketRx, sizeRx); |
| andrewboyson | 45:3dd57903ec99 | 71 | } |
| andrewboyson | 45:3dd57903ec99 | 72 | |
| andrewboyson | 45:3dd57903ec99 | 73 | //Send advertisement |
| andrewboyson | 45:3dd57903ec99 | 74 | *pType = 136; |
| andrewboyson | 45:3dd57903ec99 | 75 | *pCode = 0; |
| andrewboyson | 136:8a65abb0dc63 | 76 | hdrSetReserved(pPacketTx, 0x60000000); //R=0 (not a router); S=1 (solicited); O=1 (override) |
| andrewboyson | 136:8a65abb0dc63 | 77 | Ip6AddressCopy(hdrPtrTarget(pPacketTx), hdrPtrTarget(pPacketRx)); //Target does not change |
| andrewboyson | 45:3dd57903ec99 | 78 | |
| andrewboyson | 47:73af5c0b0dc2 | 79 | //Add target MAC |
| andrewboyson | 136:8a65abb0dc63 | 80 | char* pDataTx = pPacketTx + HEADER_LENGTH; |
| andrewboyson | 59:e0e556c8bd46 | 81 | char* p = pDataTx; |
| andrewboyson | 47:73af5c0b0dc2 | 82 | p += NdpAddOptionTargetMac(p, MacLocal); |
| andrewboyson | 136:8a65abb0dc63 | 83 | |
| andrewboyson | 136:8a65abb0dc63 | 84 | *pSizeTx = HEADER_LENGTH + p - pDataTx; |
| andrewboyson | 45:3dd57903ec99 | 85 | |
| andrewboyson | 136:8a65abb0dc63 | 86 | if (NsTraceRecvSol) logHeader(pPacketTx, *pSizeTx); |
| andrewboyson | 45:3dd57903ec99 | 87 | |
| andrewboyson | 45:3dd57903ec99 | 88 | return ActionMakeFromDestAndTrace(UNICAST, NsTraceRecvSol && NetTraceStack); |
| andrewboyson | 45:3dd57903ec99 | 89 | } |
| andrewboyson | 136:8a65abb0dc63 | 90 | int NsHandleReceivedAdvertisement(void (*traceback)(void), char* pPacket, int* pSize) |
| andrewboyson | 45:3dd57903ec99 | 91 | { |
| andrewboyson | 48:952dddb74b8b | 92 | pTraceBack = traceback; |
| andrewboyson | 136:8a65abb0dc63 | 93 | char* pData = pPacket + HEADER_LENGTH; |
| andrewboyson | 136:8a65abb0dc63 | 94 | int dataLength = *pSize - HEADER_LENGTH; |
| andrewboyson | 45:3dd57903ec99 | 95 | |
| andrewboyson | 45:3dd57903ec99 | 96 | if (NsTraceRecvAdv) |
| andrewboyson | 45:3dd57903ec99 | 97 | { |
| andrewboyson | 45:3dd57903ec99 | 98 | if (NetTraceNewLine) Log("\r\n"); |
| andrewboyson | 45:3dd57903ec99 | 99 | LogTimeF("NDP received neighbour advertise\r\n"); |
| andrewboyson | 45:3dd57903ec99 | 100 | if (NetTraceStack) traceback(); |
| andrewboyson | 136:8a65abb0dc63 | 101 | logHeader(pPacket, *pSize); |
| andrewboyson | 45:3dd57903ec99 | 102 | } |
| andrewboyson | 45:3dd57903ec99 | 103 | |
| andrewboyson | 47:73af5c0b0dc2 | 104 | char tgtMac[6]; |
| andrewboyson | 47:73af5c0b0dc2 | 105 | NdpDecodeOptions(pData, dataLength, NULL, tgtMac); |
| andrewboyson | 45:3dd57903ec99 | 106 | |
| andrewboyson | 136:8a65abb0dc63 | 107 | Ar6AddIpRecord(trace, tgtMac, hdrPtrTarget(pPacket)); |
| andrewboyson | 136:8a65abb0dc63 | 108 | Nr6MakeRequestForNameFromIp( hdrPtrTarget(pPacket)); |
| andrewboyson | 45:3dd57903ec99 | 109 | |
| andrewboyson | 45:3dd57903ec99 | 110 | return DO_NOTHING; |
| andrewboyson | 45:3dd57903ec99 | 111 | } |
| andrewboyson | 45:3dd57903ec99 | 112 | |
| andrewboyson | 136:8a65abb0dc63 | 113 | int NsGetWaitingSolicitation(char* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode) |
| andrewboyson | 136:8a65abb0dc63 | 114 | { |
| andrewboyson | 45:3dd57903ec99 | 115 | if (!NsResolveRequestFlag) return DO_NOTHING; |
| andrewboyson | 46:40d33e9037e4 | 116 | NsResolveRequestFlag = false; |
| andrewboyson | 45:3dd57903ec99 | 117 | |
| andrewboyson | 45:3dd57903ec99 | 118 | *pType = 135; //Neighbour solicitation |
| andrewboyson | 45:3dd57903ec99 | 119 | *pCode = 0; |
| andrewboyson | 47:73af5c0b0dc2 | 120 | |
| andrewboyson | 136:8a65abb0dc63 | 121 | hdrSetReserved(pPacket, 0); |
| andrewboyson | 136:8a65abb0dc63 | 122 | Ip6AddressCopy(hdrPtrTarget(pPacket), NsAddressToResolve); |
| andrewboyson | 45:3dd57903ec99 | 123 | |
| andrewboyson | 136:8a65abb0dc63 | 124 | char* pData = pPacket + HEADER_LENGTH; |
| andrewboyson | 45:3dd57903ec99 | 125 | char* p = pData; |
| andrewboyson | 47:73af5c0b0dc2 | 126 | p += NdpAddOptionSourceMac(p, MacLocal); |
| andrewboyson | 45:3dd57903ec99 | 127 | |
| andrewboyson | 136:8a65abb0dc63 | 128 | *pSize = HEADER_LENGTH + p - pData; |
| andrewboyson | 45:3dd57903ec99 | 129 | |
| andrewboyson | 45:3dd57903ec99 | 130 | if (NsTraceSendSol) |
| andrewboyson | 45:3dd57903ec99 | 131 | { |
| andrewboyson | 45:3dd57903ec99 | 132 | if (NetTraceNewLine) Log("\r\n"); |
| andrewboyson | 45:3dd57903ec99 | 133 | LogTimeF("NDP sent neighbour solicit\r\n"); |
| andrewboyson | 136:8a65abb0dc63 | 134 | logHeader(pPacket, *pSize); |
| andrewboyson | 45:3dd57903ec99 | 135 | } |
| andrewboyson | 45:3dd57903ec99 | 136 | |
| andrewboyson | 45:3dd57903ec99 | 137 | return ActionMakeFromDestAndTrace(MULTICAST_NODE, NsTraceSendSol && NetTraceStack); |
| andrewboyson | 45:3dd57903ec99 | 138 | |
| andrewboyson | 45:3dd57903ec99 | 139 | } |