Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Sun Oct 22 17:19:48 2017 +0000
Revision:
45:3dd57903ec99
Fixed DnsServer

Who changed what in which revision?

UserRevisionLine numberNew 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 45:3dd57903ec99 8 #include "ar.h"
andrewboyson 45:3dd57903ec99 9 #include "ip6.h"
andrewboyson 45:3dd57903ec99 10 #include "slaac.h"
andrewboyson 45:3dd57903ec99 11
andrewboyson 45:3dd57903ec99 12 bool NsTraceRecvSol = false;
andrewboyson 45:3dd57903ec99 13 bool NsTraceRecvAdv = false;
andrewboyson 45:3dd57903ec99 14 bool NsTraceSendSol = false;
andrewboyson 45:3dd57903ec99 15
andrewboyson 45:3dd57903ec99 16 static char srcMAC[6];
andrewboyson 45:3dd57903ec99 17 static char tgtMAC[6];
andrewboyson 45:3dd57903ec99 18
andrewboyson 45:3dd57903ec99 19 char NsAddressToResolve[16];
andrewboyson 45:3dd57903ec99 20 bool NsResolveRequestFlag = false;
andrewboyson 45:3dd57903ec99 21
andrewboyson 45:3dd57903ec99 22 static int logOptions(char* p)
andrewboyson 45:3dd57903ec99 23 {
andrewboyson 45:3dd57903ec99 24 int type = *p++;
andrewboyson 45:3dd57903ec99 25 int size = *p++;
andrewboyson 45:3dd57903ec99 26 char text[100];
andrewboyson 45:3dd57903ec99 27 switch (type)
andrewboyson 45:3dd57903ec99 28 {
andrewboyson 45:3dd57903ec99 29 case 1:
andrewboyson 45:3dd57903ec99 30 MacToString(p, sizeof(text), text);
andrewboyson 45:3dd57903ec99 31 LogF(" Src MAC %s\r\n", text);
andrewboyson 45:3dd57903ec99 32 break;
andrewboyson 45:3dd57903ec99 33 case 2:
andrewboyson 45:3dd57903ec99 34 MacToString(p, sizeof(text), text);
andrewboyson 45:3dd57903ec99 35 LogF(" Tgt MAC %s\r\n", text);
andrewboyson 45:3dd57903ec99 36 break;
andrewboyson 45:3dd57903ec99 37 default:
andrewboyson 45:3dd57903ec99 38 LogF(" Unknown option %d\r\n", type);
andrewboyson 45:3dd57903ec99 39 break;
andrewboyson 45:3dd57903ec99 40 }
andrewboyson 45:3dd57903ec99 41 return size * 8;
andrewboyson 45:3dd57903ec99 42 }
andrewboyson 45:3dd57903ec99 43
andrewboyson 45:3dd57903ec99 44 void logHeader(void* pPacket, int* pSize)
andrewboyson 45:3dd57903ec99 45 {
andrewboyson 45:3dd57903ec99 46 __packed struct header
andrewboyson 45:3dd57903ec99 47 {
andrewboyson 45:3dd57903ec99 48 uint32_t reserved;
andrewboyson 45:3dd57903ec99 49 char target[16];
andrewboyson 45:3dd57903ec99 50 };
andrewboyson 45:3dd57903ec99 51 struct header* pHeader = (header*)pPacket;
andrewboyson 45:3dd57903ec99 52 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 45:3dd57903ec99 53 int dataLength = *pSize - sizeof(struct header);
andrewboyson 45:3dd57903ec99 54
andrewboyson 45:3dd57903ec99 55 Log("NDP header\r\n");
andrewboyson 45:3dd57903ec99 56 LogF(" Size %d\r\n", *pSize);
andrewboyson 45:3dd57903ec99 57 char text[100];
andrewboyson 45:3dd57903ec99 58 Ip6AddressToString(pHeader->target, sizeof(text), text);
andrewboyson 45:3dd57903ec99 59 LogF(" Target %s\r\n", text);
andrewboyson 45:3dd57903ec99 60
andrewboyson 45:3dd57903ec99 61 char* p = pData;
andrewboyson 45:3dd57903ec99 62 char* pE = pData + dataLength;
andrewboyson 45:3dd57903ec99 63
andrewboyson 45:3dd57903ec99 64 while(p < pE) p += logOptions(p);
andrewboyson 45:3dd57903ec99 65 }
andrewboyson 45:3dd57903ec99 66
andrewboyson 45:3dd57903ec99 67 static int decodeOptions(char* p)
andrewboyson 45:3dd57903ec99 68 {
andrewboyson 45:3dd57903ec99 69 int type = *p++;
andrewboyson 45:3dd57903ec99 70 int size = *p++;
andrewboyson 45:3dd57903ec99 71 switch (type)
andrewboyson 45:3dd57903ec99 72 {
andrewboyson 45:3dd57903ec99 73 case 1:
andrewboyson 45:3dd57903ec99 74 memcpy(srcMAC, p, 6);
andrewboyson 45:3dd57903ec99 75 break;
andrewboyson 45:3dd57903ec99 76 case 2:
andrewboyson 45:3dd57903ec99 77 memcpy(tgtMAC, p, 6);
andrewboyson 45:3dd57903ec99 78 break;
andrewboyson 45:3dd57903ec99 79 default:
andrewboyson 45:3dd57903ec99 80 LogF(" Unknown option %d\r\n", type);
andrewboyson 45:3dd57903ec99 81 break;
andrewboyson 45:3dd57903ec99 82 }
andrewboyson 45:3dd57903ec99 83 return size * 8;
andrewboyson 45:3dd57903ec99 84 }
andrewboyson 45:3dd57903ec99 85
andrewboyson 45:3dd57903ec99 86 int NsHandleReceivedSolicitation(void (*traceback)(void), void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode)
andrewboyson 45:3dd57903ec99 87 {
andrewboyson 45:3dd57903ec99 88 __packed struct header
andrewboyson 45:3dd57903ec99 89 {
andrewboyson 45:3dd57903ec99 90 uint32_t reserved;
andrewboyson 45:3dd57903ec99 91 char target[16];
andrewboyson 45:3dd57903ec99 92 };
andrewboyson 45:3dd57903ec99 93 struct header* pHeader = (header*)pPacket;
andrewboyson 45:3dd57903ec99 94 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 45:3dd57903ec99 95 int dataLength = *pSize - sizeof(struct header);
andrewboyson 45:3dd57903ec99 96
andrewboyson 45:3dd57903ec99 97 //Check it is us
andrewboyson 45:3dd57903ec99 98 if (!SlaacScope(pHeader->target)) return DO_NOTHING;
andrewboyson 45:3dd57903ec99 99
andrewboyson 45:3dd57903ec99 100 if (NsTraceRecvSol)
andrewboyson 45:3dd57903ec99 101 {
andrewboyson 45:3dd57903ec99 102 if (NetTraceNewLine) Log("\r\n");
andrewboyson 45:3dd57903ec99 103 LogTimeF("NDP received neighbour solicit\r\n");
andrewboyson 45:3dd57903ec99 104 if (NetTraceStack) traceback();
andrewboyson 45:3dd57903ec99 105 logHeader(pPacket, pSize);
andrewboyson 45:3dd57903ec99 106 }
andrewboyson 45:3dd57903ec99 107 char* p = pData;
andrewboyson 45:3dd57903ec99 108 char* pE = pData + dataLength;
andrewboyson 45:3dd57903ec99 109
andrewboyson 45:3dd57903ec99 110 while(p < pE) p += decodeOptions(p);
andrewboyson 45:3dd57903ec99 111
andrewboyson 45:3dd57903ec99 112 //Send advertisement
andrewboyson 45:3dd57903ec99 113 *pType = 136;
andrewboyson 45:3dd57903ec99 114 *pCode = 0;
andrewboyson 45:3dd57903ec99 115 pHeader->reserved = 0x00000060; //R=0 (not a router); S=1 (solicited); O=1 (override)
andrewboyson 45:3dd57903ec99 116 //pHeader->target is unchanged
andrewboyson 45:3dd57903ec99 117
andrewboyson 45:3dd57903ec99 118 p = pData;
andrewboyson 45:3dd57903ec99 119 *p++ = 2; //Target MAC option
andrewboyson 45:3dd57903ec99 120 *p++ = 1; //8 bytes
andrewboyson 45:3dd57903ec99 121 memcpy(p, MacLocal, 6);
andrewboyson 45:3dd57903ec99 122 p += 6;
andrewboyson 45:3dd57903ec99 123
andrewboyson 45:3dd57903ec99 124 *pSize = sizeof(struct header) + p - pData;
andrewboyson 45:3dd57903ec99 125
andrewboyson 45:3dd57903ec99 126 if (NsTraceRecvSol) logHeader(pPacket, pSize);
andrewboyson 45:3dd57903ec99 127
andrewboyson 45:3dd57903ec99 128 return ActionMakeFromDestAndTrace(UNICAST, NsTraceRecvSol && NetTraceStack);
andrewboyson 45:3dd57903ec99 129
andrewboyson 45:3dd57903ec99 130 }
andrewboyson 45:3dd57903ec99 131 int NsHandleReceivedAdvertisement(void (*traceback)(void), void* pPacket, int* pSize)
andrewboyson 45:3dd57903ec99 132 {
andrewboyson 45:3dd57903ec99 133 __packed struct header
andrewboyson 45:3dd57903ec99 134 {
andrewboyson 45:3dd57903ec99 135 uint32_t reserved;
andrewboyson 45:3dd57903ec99 136 char target[16];
andrewboyson 45:3dd57903ec99 137 };
andrewboyson 45:3dd57903ec99 138 struct header* pHeader = (header*)pPacket;
andrewboyson 45:3dd57903ec99 139 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 45:3dd57903ec99 140 int dataLength = *pSize - sizeof(struct header);
andrewboyson 45:3dd57903ec99 141
andrewboyson 45:3dd57903ec99 142 if (NsTraceRecvAdv)
andrewboyson 45:3dd57903ec99 143 {
andrewboyson 45:3dd57903ec99 144 if (NetTraceNewLine) Log("\r\n");
andrewboyson 45:3dd57903ec99 145 LogTimeF("NDP received neighbour advertise\r\n");
andrewboyson 45:3dd57903ec99 146 if (NetTraceStack) traceback();
andrewboyson 45:3dd57903ec99 147 logHeader(pPacket, pSize);
andrewboyson 45:3dd57903ec99 148 }
andrewboyson 45:3dd57903ec99 149
andrewboyson 45:3dd57903ec99 150 char* p = pData;
andrewboyson 45:3dd57903ec99 151 char* pE = pData + dataLength;
andrewboyson 45:3dd57903ec99 152
andrewboyson 45:3dd57903ec99 153 while(p < pE) p += decodeOptions(p);
andrewboyson 45:3dd57903ec99 154
andrewboyson 45:3dd57903ec99 155 ArAddIp6Record(tgtMAC, pHeader->target);
andrewboyson 45:3dd57903ec99 156 NrMakeRequestForNameFromIp6(pHeader->target);
andrewboyson 45:3dd57903ec99 157
andrewboyson 45:3dd57903ec99 158 return DO_NOTHING;
andrewboyson 45:3dd57903ec99 159 }
andrewboyson 45:3dd57903ec99 160
andrewboyson 45:3dd57903ec99 161 int NsGetWaitingSolicitation(void* pPacket, int* pSize, uint8_t* pType, uint8_t* pCode)
andrewboyson 45:3dd57903ec99 162 {
andrewboyson 45:3dd57903ec99 163 if (!NsResolveRequestFlag) return DO_NOTHING;
andrewboyson 45:3dd57903ec99 164
andrewboyson 45:3dd57903ec99 165 __packed struct header
andrewboyson 45:3dd57903ec99 166 {
andrewboyson 45:3dd57903ec99 167 uint32_t reserved;
andrewboyson 45:3dd57903ec99 168 char target[16];
andrewboyson 45:3dd57903ec99 169 };
andrewboyson 45:3dd57903ec99 170 struct header* pHeader = (header*)pPacket;
andrewboyson 45:3dd57903ec99 171 pHeader->reserved = 0;
andrewboyson 45:3dd57903ec99 172 memcpy(pHeader->target, NsAddressToResolve, 16);
andrewboyson 45:3dd57903ec99 173
andrewboyson 45:3dd57903ec99 174 *pSize = sizeof(struct header);
andrewboyson 45:3dd57903ec99 175 *pType = 135; //Neighbour solicitation
andrewboyson 45:3dd57903ec99 176 *pCode = 0;
andrewboyson 45:3dd57903ec99 177
andrewboyson 45:3dd57903ec99 178 char* pData = (char*)pHeader + sizeof(struct header);
andrewboyson 45:3dd57903ec99 179 char* p = pData;
andrewboyson 45:3dd57903ec99 180 *p++ = 1; //Source MAC option
andrewboyson 45:3dd57903ec99 181 *p++ = 1; //8 bytes
andrewboyson 45:3dd57903ec99 182 memcpy(p, MacLocal, 6);
andrewboyson 45:3dd57903ec99 183 p += 6;
andrewboyson 45:3dd57903ec99 184
andrewboyson 45:3dd57903ec99 185 *pSize = sizeof(struct header) + p - pData;
andrewboyson 45:3dd57903ec99 186
andrewboyson 45:3dd57903ec99 187 if (NsTraceSendSol)
andrewboyson 45:3dd57903ec99 188 {
andrewboyson 45:3dd57903ec99 189 if (NetTraceNewLine) Log("\r\n");
andrewboyson 45:3dd57903ec99 190 LogTimeF("NDP sent neighbour solicit\r\n");
andrewboyson 45:3dd57903ec99 191 logHeader(pPacket, pSize);
andrewboyson 45:3dd57903ec99 192 }
andrewboyson 45:3dd57903ec99 193
andrewboyson 45:3dd57903ec99 194 return ActionMakeFromDestAndTrace(MULTICAST_NODE, NsTraceSendSol && NetTraceStack);
andrewboyson 45:3dd57903ec99 195
andrewboyson 45:3dd57903ec99 196 }