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.cpp@48:952dddb74b8b, 2017-10-31 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Oct 31 21:25:09 2017 +0000
- Revision:
- 48:952dddb74b8b
- Parent:
- 47:73af5c0b0dc2
- Child:
- 49:1a6336f2b3f9
Split address resolution into AR4 and AR6. Corrected issue clearing a AR6 record.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 45:3dd57903ec99 | 1 | #include "mbed.h" |
andrewboyson | 45:3dd57903ec99 | 2 | #include "log.h" |
andrewboyson | 45:3dd57903ec99 | 3 | #include "net.h" |
andrewboyson | 45:3dd57903ec99 | 4 | #include "action.h" |
andrewboyson | 45:3dd57903ec99 | 5 | #include "ip6.h" |
andrewboyson | 45:3dd57903ec99 | 6 | #include "mac.h" |
andrewboyson | 45:3dd57903ec99 | 7 | #include "nr.h" |
andrewboyson | 48:952dddb74b8b | 8 | #include "ar6.h" |
andrewboyson | 45:3dd57903ec99 | 9 | #include "ip6.h" |
andrewboyson | 45:3dd57903ec99 | 10 | #include "slaac.h" |
andrewboyson | 47:73af5c0b0dc2 | 11 | #include "ndp.h" |
andrewboyson | 45:3dd57903ec99 | 12 | |
andrewboyson | 45:3dd57903ec99 | 13 | bool NsTraceRecvSol = false; |
andrewboyson | 45:3dd57903ec99 | 14 | bool NsTraceRecvAdv = false; |
andrewboyson | 45:3dd57903ec99 | 15 | bool NsTraceSendSol = false; |
andrewboyson | 45:3dd57903ec99 | 16 | |
andrewboyson | 45:3dd57903ec99 | 17 | char NsAddressToResolve[16]; |
andrewboyson | 45:3dd57903ec99 | 18 | bool NsResolveRequestFlag = false; |
andrewboyson | 45:3dd57903ec99 | 19 | |
andrewboyson | 47:73af5c0b0dc2 | 20 | __packed struct header |
andrewboyson | 45:3dd57903ec99 | 21 | { |
andrewboyson | 47:73af5c0b0dc2 | 22 | uint32_t reserved; |
andrewboyson | 47:73af5c0b0dc2 | 23 | char target[16]; |
andrewboyson | 47:73af5c0b0dc2 | 24 | }; |
andrewboyson | 48:952dddb74b8b | 25 | static struct header * pHeader; |
andrewboyson | 48:952dddb74b8b | 26 | static int size; |
andrewboyson | 45:3dd57903ec99 | 27 | |
andrewboyson | 48:952dddb74b8b | 28 | static void logHeader() |
andrewboyson | 45:3dd57903ec99 | 29 | { |
andrewboyson | 45:3dd57903ec99 | 30 | char* pData = (char*)pHeader + sizeof(struct header); |
andrewboyson | 47:73af5c0b0dc2 | 31 | int dataLength = size - sizeof(struct header); |
andrewboyson | 45:3dd57903ec99 | 32 | |
andrewboyson | 47:73af5c0b0dc2 | 33 | if (NetTraceVerbose) |
andrewboyson | 45:3dd57903ec99 | 34 | { |
andrewboyson | 47:73af5c0b0dc2 | 35 | Log("NS header\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 36 | LogF(" Size %d\r\n", size); |
andrewboyson | 47:73af5c0b0dc2 | 37 | LogF(" Target "); Ip6AddressLog(pHeader->target); Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 38 | NdpLogOptionsVerbose(pData, dataLength); |
andrewboyson | 45:3dd57903ec99 | 39 | } |
andrewboyson | 47:73af5c0b0dc2 | 40 | else |
andrewboyson | 47:73af5c0b0dc2 | 41 | { |
andrewboyson | 47:73af5c0b0dc2 | 42 | Log("NS header "); |
andrewboyson | 47:73af5c0b0dc2 | 43 | Ip6AddressLog(pHeader->target); |
andrewboyson | 47:73af5c0b0dc2 | 44 | NdpLogOptionsQuiet(pData, dataLength); |
andrewboyson | 47:73af5c0b0dc2 | 45 | Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 46 | } |
andrewboyson | 45:3dd57903ec99 | 47 | } |
andrewboyson | 48:952dddb74b8b | 48 | static void (*pTraceBack)(void); |
andrewboyson | 48:952dddb74b8b | 49 | static void trace() |
andrewboyson | 48:952dddb74b8b | 50 | { |
andrewboyson | 48:952dddb74b8b | 51 | pTraceBack(); |
andrewboyson | 48:952dddb74b8b | 52 | logHeader(); |
andrewboyson | 48:952dddb74b8b | 53 | } |
andrewboyson | 45:3dd57903ec99 | 54 | int NsHandleReceivedSolicitation(void (*traceback)(void), void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode) |
andrewboyson | 45:3dd57903ec99 | 55 | { |
andrewboyson | 48:952dddb74b8b | 56 | pTraceBack = traceback; |
andrewboyson | 48:952dddb74b8b | 57 | pHeader = (header*)pPacket; |
andrewboyson | 48:952dddb74b8b | 58 | size = *pSize; |
andrewboyson | 45:3dd57903ec99 | 59 | char* pData = (char*)pHeader + sizeof(struct header); |
andrewboyson | 45:3dd57903ec99 | 60 | |
andrewboyson | 45:3dd57903ec99 | 61 | //Check it is us |
andrewboyson | 45:3dd57903ec99 | 62 | if (!SlaacScope(pHeader->target)) return DO_NOTHING; |
andrewboyson | 45:3dd57903ec99 | 63 | |
andrewboyson | 45:3dd57903ec99 | 64 | if (NsTraceRecvSol) |
andrewboyson | 45:3dd57903ec99 | 65 | { |
andrewboyson | 45:3dd57903ec99 | 66 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 45:3dd57903ec99 | 67 | LogTimeF("NDP received neighbour solicit\r\n"); |
andrewboyson | 45:3dd57903ec99 | 68 | if (NetTraceStack) traceback(); |
andrewboyson | 48:952dddb74b8b | 69 | logHeader(); |
andrewboyson | 45:3dd57903ec99 | 70 | } |
andrewboyson | 45:3dd57903ec99 | 71 | |
andrewboyson | 45:3dd57903ec99 | 72 | //Send advertisement |
andrewboyson | 45:3dd57903ec99 | 73 | *pType = 136; |
andrewboyson | 45:3dd57903ec99 | 74 | *pCode = 0; |
andrewboyson | 45:3dd57903ec99 | 75 | pHeader->reserved = 0x00000060; //R=0 (not a router); S=1 (solicited); O=1 (override) |
andrewboyson | 45:3dd57903ec99 | 76 | //pHeader->target is unchanged |
andrewboyson | 45:3dd57903ec99 | 77 | |
andrewboyson | 47:73af5c0b0dc2 | 78 | //Add target MAC |
andrewboyson | 47:73af5c0b0dc2 | 79 | char* p = pData; |
andrewboyson | 47:73af5c0b0dc2 | 80 | p += NdpAddOptionTargetMac(p, MacLocal); |
andrewboyson | 45:3dd57903ec99 | 81 | |
andrewboyson | 48:952dddb74b8b | 82 | size = sizeof(struct header) + p - pData; |
andrewboyson | 48:952dddb74b8b | 83 | *pSize = *pSize; |
andrewboyson | 45:3dd57903ec99 | 84 | |
andrewboyson | 48:952dddb74b8b | 85 | if (NsTraceRecvSol) logHeader(); |
andrewboyson | 45:3dd57903ec99 | 86 | |
andrewboyson | 45:3dd57903ec99 | 87 | return ActionMakeFromDestAndTrace(UNICAST, NsTraceRecvSol && NetTraceStack); |
andrewboyson | 45:3dd57903ec99 | 88 | |
andrewboyson | 45:3dd57903ec99 | 89 | } |
andrewboyson | 45:3dd57903ec99 | 90 | int NsHandleReceivedAdvertisement(void (*traceback)(void), void* pPacket, int* pSize) |
andrewboyson | 45:3dd57903ec99 | 91 | { |
andrewboyson | 48:952dddb74b8b | 92 | pTraceBack = traceback; |
andrewboyson | 48:952dddb74b8b | 93 | pHeader = (header*)pPacket; |
andrewboyson | 48:952dddb74b8b | 94 | size = *pSize; |
andrewboyson | 45:3dd57903ec99 | 95 | char* pData = (char*)pHeader + sizeof(struct header); |
andrewboyson | 48:952dddb74b8b | 96 | int dataLength = size - sizeof(struct header); |
andrewboyson | 45:3dd57903ec99 | 97 | |
andrewboyson | 45:3dd57903ec99 | 98 | if (NsTraceRecvAdv) |
andrewboyson | 45:3dd57903ec99 | 99 | { |
andrewboyson | 45:3dd57903ec99 | 100 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 45:3dd57903ec99 | 101 | LogTimeF("NDP received neighbour advertise\r\n"); |
andrewboyson | 45:3dd57903ec99 | 102 | if (NetTraceStack) traceback(); |
andrewboyson | 48:952dddb74b8b | 103 | logHeader(); |
andrewboyson | 45:3dd57903ec99 | 104 | } |
andrewboyson | 45:3dd57903ec99 | 105 | |
andrewboyson | 47:73af5c0b0dc2 | 106 | char tgtMac[6]; |
andrewboyson | 47:73af5c0b0dc2 | 107 | NdpDecodeOptions(pData, dataLength, NULL, tgtMac); |
andrewboyson | 45:3dd57903ec99 | 108 | |
andrewboyson | 48:952dddb74b8b | 109 | Ar6AddIpRecord(trace, tgtMac, pHeader->target); |
andrewboyson | 45:3dd57903ec99 | 110 | NrMakeRequestForNameFromIp6(pHeader->target); |
andrewboyson | 45:3dd57903ec99 | 111 | |
andrewboyson | 45:3dd57903ec99 | 112 | return DO_NOTHING; |
andrewboyson | 45:3dd57903ec99 | 113 | } |
andrewboyson | 45:3dd57903ec99 | 114 | |
andrewboyson | 45:3dd57903ec99 | 115 | int NsGetWaitingSolicitation(void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode) |
andrewboyson | 45:3dd57903ec99 | 116 | { |
andrewboyson | 45:3dd57903ec99 | 117 | if (!NsResolveRequestFlag) return DO_NOTHING; |
andrewboyson | 46:40d33e9037e4 | 118 | NsResolveRequestFlag = false; |
andrewboyson | 45:3dd57903ec99 | 119 | |
andrewboyson | 45:3dd57903ec99 | 120 | *pType = 135; //Neighbour solicitation |
andrewboyson | 45:3dd57903ec99 | 121 | *pCode = 0; |
andrewboyson | 47:73af5c0b0dc2 | 122 | |
andrewboyson | 48:952dddb74b8b | 123 | pHeader = (header*)pPacket; |
andrewboyson | 48:952dddb74b8b | 124 | size = *pSize; |
andrewboyson | 47:73af5c0b0dc2 | 125 | pHeader->reserved = 0; |
andrewboyson | 47:73af5c0b0dc2 | 126 | Ip6Copy(pHeader->target, NsAddressToResolve); |
andrewboyson | 45:3dd57903ec99 | 127 | |
andrewboyson | 45:3dd57903ec99 | 128 | char* pData = (char*)pHeader + sizeof(struct header); |
andrewboyson | 45:3dd57903ec99 | 129 | char* p = pData; |
andrewboyson | 47:73af5c0b0dc2 | 130 | p += NdpAddOptionSourceMac(p, MacLocal); |
andrewboyson | 45:3dd57903ec99 | 131 | |
andrewboyson | 48:952dddb74b8b | 132 | size = sizeof(struct header) + p - pData; |
andrewboyson | 48:952dddb74b8b | 133 | *pSize = *pSize; |
andrewboyson | 45:3dd57903ec99 | 134 | |
andrewboyson | 45:3dd57903ec99 | 135 | if (NsTraceSendSol) |
andrewboyson | 45:3dd57903ec99 | 136 | { |
andrewboyson | 45:3dd57903ec99 | 137 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 45:3dd57903ec99 | 138 | LogTimeF("NDP sent neighbour solicit\r\n"); |
andrewboyson | 48:952dddb74b8b | 139 | logHeader(); |
andrewboyson | 45:3dd57903ec99 | 140 | } |
andrewboyson | 45:3dd57903ec99 | 141 | |
andrewboyson | 45:3dd57903ec99 | 142 | return ActionMakeFromDestAndTrace(MULTICAST_NODE, NsTraceSendSol && NetTraceStack); |
andrewboyson | 45:3dd57903ec99 | 143 | |
andrewboyson | 45:3dd57903ec99 | 144 | } |