Lines 91-96
Link Here
|
91 |
#include <sys/socket.h> |
91 |
#include <sys/socket.h> |
92 |
#include <sys/un.h> |
92 |
#include <sys/un.h> |
93 |
#include <unistd.h> |
93 |
#include <unistd.h> |
|
|
94 |
#include <sys/ioctl.h> |
95 |
#include <net/if.h> |
96 |
#include <net/if_arp.h> |
97 |
#include <arpa/inet.h> |
98 |
#include <errno.h> |
99 |
#include <linux/netlink.h> |
100 |
#include <linux/rtnetlink.h> |
101 |
|
94 |
|
102 |
|
95 |
#include "snmp.h" |
103 |
#include "snmp.h" |
96 |
#include "frame_structs.h" |
104 |
#include "frame_structs.h" |
Lines 126-131
Link Here
|
126 |
#include <efence.h> |
134 |
#include <efence.h> |
127 |
#endif |
135 |
#endif |
128 |
|
136 |
|
|
|
137 |
|
138 |
uint8_t md5rec[16]; |
139 |
char DNS[20]; |
140 |
|
141 |
|
129 |
struct eap_type_handler { |
142 |
struct eap_type_handler { |
130 |
int eap_auth_type; |
143 |
int eap_auth_type; |
131 |
char *eapname; |
144 |
char *eapname; |
Lines 137-142
Link Here
|
137 |
int (*eap_auth_cleanup)(struct generic_eap_data *); |
150 |
int (*eap_auth_cleanup)(struct generic_eap_data *); |
138 |
}; |
151 |
}; |
139 |
|
152 |
|
|
|
153 |
|
154 |
|
140 |
struct eap_type_handler eaphandlers[] = { |
155 |
struct eap_type_handler eaphandlers[] = { |
141 |
{EAP_TYPE_MD5, "EAP_MD5", eapmd5_setup, eapmd5_process, eapmd5_get_keys, |
156 |
{EAP_TYPE_MD5, "EAP_MD5", eapmd5_setup, eapmd5_process, eapmd5_get_keys, |
142 |
eapmd5_failed, eapmd5_cleanup}, |
157 |
eapmd5_failed, eapmd5_cleanup}, |
Lines 308-319
Link Here
|
308 |
|
323 |
|
309 |
eapol_build_header(EAP_PACKET, eapsize, eapolver, (char *) thisint->sendframe); |
324 |
eapol_build_header(EAP_PACKET, eapsize, eapolver, (char *) thisint->sendframe); |
310 |
thisint->send_size = eapsize+OFFSET_TO_EAP; |
325 |
thisint->send_size = eapsize+OFFSET_TO_EAP; |
|
|
326 |
if(ruijie_op==1) |
327 |
{ |
328 |
append_rgtail(thisint); //by acevery, for Red Gaint Facilities. |
329 |
} |
311 |
return XENONE; |
330 |
return XENONE; |
312 |
} |
331 |
} |
313 |
|
332 |
|
314 |
debug_printf(DEBUG_INT, "No response frame was built!\n"); |
333 |
debug_printf(DEBUG_INT, "No response frame was built!\n"); |
315 |
return XENOFRAMES; |
334 |
return XENOFRAMES; |
316 |
} |
335 |
} |
|
|
336 |
|
337 |
/************************************************* |
338 |
* |
339 |
* Append Red Gaint tail to each response data, |
340 |
* by acevery |
341 |
* |
342 |
*************************************************/ |
343 |
|
344 |
void append_rgtail(struct interface_data *thisint) |
345 |
{ |
346 |
int i; |
347 |
uint8_t rgsig[144] ={ |
348 |
// 0 --> 22 |
349 |
0x00,0x00,0x13,0x11, // Encode( 0x00,0x00,0x13,0x11 ) Ruijie OEM Mark |
350 |
0x01, // Encode( 0x01/00 ) |
351 |
0x00,0x00,0x00,0x00, // Encode( IP ) |
352 |
0x00,0x00,0x00,0x00, // Encode( SubNetMask ) |
353 |
0x00,0x00,0x00,0x00, // Encode( NetGate ) |
354 |
0x00,0x00,0x00,0x00, // Encode( DNS ) |
355 |
0x00,0x00, // Checksum( ) |
356 |
// 23 --> 58 |
357 |
0x00,0x00,0x13,0x11,0x38,0x30,0x32,0x31,0x78,0x2e,0x65,0x78,0x65,0x00,0x00,0x00, // ASCII 8021x.exe |
358 |
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // |
359 |
0x00,0x00,0x00,0x00, |
360 |
// 59 --> 77 |
361 |
0x02,0x38,0x00,0x00, // 8021x.exe File Version (2.56) |
362 |
0x00, // unknow flag |
363 |
0x00,0x00,0x13,0x11,0x00,0x4a,0x1a,0x28,0x00,0x00,0x13,0x11,0x17,0x22, // Const strings |
364 |
// 78 --> 120 |
365 |
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, |
366 |
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, // 32bytes spc. Random strings |
367 |
0x1a,0x0c,0x00,0x00,0x13,0x11,0x18,0x06,0x00,0x00,0x00, // Const strings |
368 |
// 121 |
369 |
0x01, // DHCP and first time flag |
370 |
// V2.56 (and upper?) added |
371 |
// 122 --> |
372 |
0x1a,0x0e,0x00,0x00,0x13,0x11,0x2d,0x08, // Const strings |
373 |
// 130 --> 135 |
374 |
0x00,0x00,0x00,0x00,0x00,0x00, // True NIC MAC |
375 |
// 136--> 143 |
376 |
0x1a,0x08,0x00,0x00,0x13,0x11,0x2f,0x02 // Const strings |
377 |
}; |
378 |
// 1 , we fill in the Network Parameters part: |
379 |
// 0 --> 20 + 121 |
380 |
if(getNP(thisint, &rgsig[RJ_IP_OFFSET], IP)==-1) |
381 |
{ |
382 |
debug_printf(DEBUG_EVERYTHING,"The %s is not up, we don't get Network Parameters for Authentication.\n",thisint->intName); |
383 |
for(i=0;i<16;i++) |
384 |
{ |
385 |
rgsig[RJ_IP_OFFSET+i]=0x00; |
386 |
} |
387 |
rgsig[RJ_DHCP_OFFSET]=0x01; |
388 |
} |
389 |
else |
390 |
{ |
391 |
debug_printf(DEBUG_EVERYTHING,"The %s is up, we will get Network Parameters for Authentication.\n",thisint->intName); |
392 |
debug_printf(DEBUG_NORMAL,"Trying to get Netmask.\n"); |
393 |
if(getNP(thisint, &rgsig[RJ_NM_OFFSET], NM)==-1) |
394 |
{ |
395 |
debug_printf(DEBUG_NORMAL,"Couldn't get subNetmask!\n"); |
396 |
} |
397 |
debug_printf(DEBUG_NORMAL,"Trying to get Gateway.\n"); |
398 |
if(gotRoute(&rgsig[RJ_GW_OFFSET])!=0) |
399 |
{ |
400 |
debug_printf(DEBUG_NORMAL,"Couldn't get Gateway!\n"); |
401 |
} |
402 |
debug_printf(DEBUG_NORMAL,"Trying to get DNS.\n"); |
403 |
if(gotDNS(&rgsig[RJ_DNS_OFFSET])!=0) |
404 |
{ |
405 |
debug_printf(DEBUG_NORMAL,"Couldn't get DNS!\n"); |
406 |
} |
407 |
rgsig[RJ_DHCP_OFFSET]=0x00; |
408 |
} |
409 |
|
410 |
// 2 , we calculate the Checksum of the Network Parameters. |
411 |
// 21 --> 22 |
412 |
checksumNP(rgsig,&rgsig[RJ_CS_OFFSET]); |
413 |
|
414 |
// 3 , we Encode the Network Parameter in Ruijie's algorithm. |
415 |
// 0 -- > 22 |
416 |
for(i=0;i<23;i++) |
417 |
{ |
418 |
rgsig[i]=encodeNP(rgsig[i]); |
419 |
} |
420 |
|
421 |
// 4 , we generate the sepecial 32 byte Hash |
422 |
// 78 --> 109 |
423 |
if(md5rec[0]==0 && md5rec[2]==0) |
424 |
{ |
425 |
debug_printf(DEBUG_EVERYTHING, |
426 |
"We haven't got the random md5 hash, so use randstr() to fill the 32 byte Hash.\n"); |
427 |
randstr(&rgsig[RJ_HASH_OFFSET]); |
428 |
} |
429 |
else |
430 |
{ |
431 |
debug_printf(DEBUG_EVERYTHING, |
432 |
"Use the got random md5 hash to generate the 33 byte Hash.\n"); |
433 |
for(i=0;i<16;i++) |
434 |
debug_printf(DEBUG_NORMAL,"%.2x",md5rec[i]); |
435 |
debug_printf(DEBUG_NORMAL,"\n"); |
436 |
Hash8021x(&rgsig[RJ_HASH_OFFSET]); |
437 |
} |
438 |
|
439 |
// 5 , we fill the NIC MAC |
440 |
// 130 --> 135 |
441 |
memcpy(&rgsig[RJ_MAC_OFFSET],&thisint->source_mac[0],6); |
442 |
// All right, the Ruijie tail is ready :) |
443 |
|
444 |
memcpy(&thisint->sendframe[thisint->send_size], rgsig, RGSIGLEN); |
445 |
thisint->send_size += RGSIGLEN; |
446 |
} |
317 |
|
447 |
|
318 |
/*************************************************** |
448 |
/*************************************************** |
319 |
* |
449 |
* |
Lines 358-363
Link Here
|
358 |
|
488 |
|
359 |
eapol_build_header(EAP_PACKET, eapsize, eapolver, (char *) thisint->sendframe); |
489 |
eapol_build_header(EAP_PACKET, eapsize, eapolver, (char *) thisint->sendframe); |
360 |
thisint->send_size = eapsize+OFFSET_TO_EAP; |
490 |
thisint->send_size = eapsize+OFFSET_TO_EAP; |
|
|
491 |
if(ruijie_op==1) |
492 |
{ |
493 |
append_rgtail(thisint); |
494 |
} |
495 |
|
361 |
|
496 |
|
362 |
return XENONE; |
497 |
return XENONE; |
363 |
} |
498 |
} |
Lines 622-627
Link Here
|
622 |
case EAP_SUCCESS: |
757 |
case EAP_SUCCESS: |
623 |
debug_printf(DEBUG_EVERYTHING, "Got EAP-Success!\n"); |
758 |
debug_printf(DEBUG_EVERYTHING, "Got EAP-Success!\n"); |
624 |
thisint->statemachine->eapSuccess = TRUE; |
759 |
thisint->statemachine->eapSuccess = TRUE; |
|
|
760 |
if(ruijie_op==1) |
761 |
{ |
762 |
get_rg_ssid(thisint); |
763 |
} |
625 |
|
764 |
|
626 |
if (network_data->activemethod == NULL) |
765 |
if (network_data->activemethod == NULL) |
627 |
{ |
766 |
{ |
Lines 668-673
Link Here
|
668 |
return XENONE; |
807 |
return XENONE; |
669 |
} |
808 |
} |
670 |
|
809 |
|
|
|
810 |
/********************************************************** |
811 |
* |
812 |
* Get initial Red Gaint session id for later echo packet |
813 |
* to keep the supplicant connected from the server. |
814 |
* |
815 |
*********************************************************/ |
816 |
|
817 |
void get_rg_ssid(struct interface_data *thisint) |
818 |
{ |
819 |
ULONG_BYTEARRAY uTemp; |
820 |
uint16_t offset; |
821 |
offset = ntohs( *((uint16_t *)&(thisint->recvframe)[0x10])); |
822 |
|
823 |
debug_printf(DEBUG_EVERYTHING,"header lenth : %d \n",offset); |
824 |
|
825 |
uTemp.ulValue = *((uint32_t *)(&(thisint->recvframe)[0x12 + offset - 0x08])); |
826 |
|
827 |
debug_printf(DEBUG_EVERYTHING, "Got Red Gaint SSID :\n"); |
828 |
debug_hex_dump(DEBUG_EVERYTHING,&uTemp.btValue[0],4); |
829 |
|
830 |
thisint->rg_ssid.btValue[0] = encodeNP(uTemp.btValue[3]); |
831 |
thisint->rg_ssid.btValue[1] = encodeNP(uTemp.btValue[2]); |
832 |
thisint->rg_ssid.btValue[2] = encodeNP(uTemp.btValue[1]); |
833 |
thisint->rg_ssid.btValue[3] = encodeNP(uTemp.btValue[0]); |
834 |
|
835 |
} |
836 |
|
671 |
/********************************************************************** |
837 |
/********************************************************************** |
672 |
* |
838 |
* |
673 |
* For certain EAP methods, such as SIM and AKA, we can populate the ID |
839 |
* For certain EAP methods, such as SIM and AKA, we can populate the ID |
Lines 1146-1148
Link Here
|
1146 |
|
1312 |
|
1147 |
return XENONE; |
1313 |
return XENONE; |
1148 |
} |
1314 |
} |
|
|
1315 |
|
1316 |
|
1317 |
/************************************************************************ |
1318 |
* |
1319 |
* Ruijie use its special algorithm to authenticate client. Below are the |
1320 |
* functions need to be pass its test :) |
1321 |
* All the functions are called in |
1322 |
* append_rgtail(). |
1323 |
|
1324 |
************************************************************************/ |
1325 |
|
1326 |
// Special random string function for the seek frame and first Respond frame. |
1327 |
// Comes from Mento Supplicant v 6.0 beta3 by soar. |
1328 |
|
1329 |
void randstr(uint8_t * randhash) |
1330 |
{ |
1331 |
uint16_t a,b,c,d,e; |
1332 |
unsigned t; |
1333 |
int i; |
1334 |
t=time(NULL); |
1335 |
srand(t); |
1336 |
a=rand(); |
1337 |
b=rand(); |
1338 |
c=rand(); |
1339 |
d=rand(); |
1340 |
e=rand(); |
1341 |
|
1342 |
uint8_t strRandom[65]; |
1343 |
// now we get the rand md5 hash from string format: |
1344 |
sprintf(strRandom,"%.4X%.4X%.4X%.4X%.4X%.8X3884",a,b,c,d,e,t); |
1345 |
for(i=0;i<32;i++) |
1346 |
{ |
1347 |
randhash[i]=strRandom[i]; |
1348 |
} |
1349 |
} |
1350 |
/************************************************************************ |
1351 |
* |
1352 |
* Hash8021x is the key algorithm in Ruijie Supplicant, |
1353 |
* Comes from Mento Supplicant v 6.0 beta 3 by soar. |
1354 |
* |
1355 |
* Use 8 MD5 checksum 0x00001000h --> 0x00021000h binary of 8021x.exe |
1356 |
* (every segment is 0x4000h)£¬been added the random MD5 hash from |
1357 |
* authenticator before every segment. Then, use the 8 MD5 hash and the |
1358 |
* random hash from authenticator to form a 0x90h table tableC, then |
1359 |
* checksum tableC. |
1360 |
* |
1361 |
*************************************************************************/ |
1362 |
void Hash8021x( uint8_t * strMD5Hash) |
1363 |
{ |
1364 |
FILE *RJfile; |
1365 |
int BufferSize=0x4a00; |
1366 |
int nLength=0,i=0,j=0; |
1367 |
|
1368 |
uint8_t md5get[16],md5result[16]; |
1369 |
for(i=0;i<16;i++) |
1370 |
{ |
1371 |
md5get[i]=0; |
1372 |
} |
1373 |
|
1374 |
uint8_t Buffer1[0x5000];//the buffer for 8021x.exe reading segment |
1375 |
uint8_t Buffer2[0x5000];// buffer for md5hash got and 8021x.exe segment |
1376 |
RJfile = fopen( "/etc/8021x.exe", "rb"); |
1377 |
fseek(RJfile,0x1000,SEEK_SET); |
1378 |
static uint8_t TableC[0x90]={}; |
1379 |
// Transform to TableC |
1380 |
TableC[0]=md5rec[0]; |
1381 |
for (i=1;i<8;i++) |
1382 |
{ |
1383 |
TableC[i*18-1]=md5rec[i*2-1]; |
1384 |
TableC[i*18]=md5rec[i*2]; |
1385 |
} |
1386 |
TableC[143]=md5rec[15]; |
1387 |
|
1388 |
// Calculate 8021x.exe segment md5sum |
1389 |
j=0; |
1390 |
while((nLength=fread(Buffer1,BufferSize,1,RJfile))>0 && j<8) |
1391 |
{ |
1392 |
for(i=0;i<16;i++) |
1393 |
{ |
1394 |
Buffer2[i]=md5rec[i]; |
1395 |
} |
1396 |
for (i=0;i<BufferSize;i++) |
1397 |
{ |
1398 |
Buffer2[16+i]=Buffer1[i]; |
1399 |
} |
1400 |
MD5(Buffer2,BufferSize+16,md5get); |
1401 |
for (i=0;i<16;i++) |
1402 |
{ |
1403 |
TableC[18*j+i+1]=md5get[i]; |
1404 |
} |
1405 |
j++; |
1406 |
}// end of while, and now we got a ready TableC |
1407 |
fclose(RJfile); |
1408 |
MD5(TableC,144,md5result);// Calculate TableC md5sum, and store to strMD5Hash |
1409 |
sprintf(&strMD5Hash[0],"%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x",md5result[0],md5result[1],md5result[2],md5result[3],md5result[4],md5result[5],md5result[6],md5result[7],md5result[8],md5result[9],md5result[10],md5result[11],md5result[12],md5result[13],md5result[14],md5result[15],32); |
1410 |
|
1411 |
debug_printf(DEBUG_EVERYTHING, "Ruijie 8021x.exe Check MD5 in ACSII is: "); |
1412 |
for(i=0;i<16;i++) |
1413 |
{ |
1414 |
debug_printf(DEBUG_NORMAL,"%.2x ",strMD5Hash[i]); |
1415 |
} |
1416 |
debug_printf(DEBUG_NORMAL,"\n"); |
1417 |
|
1418 |
} |
1419 |
/************************************************************************ |
1420 |
* |
1421 |
* checksumNP is another algorithm in Ruijie Supplicant, |
1422 |
* it calculate the checksum of Ruijie OEM mart and Net Parameters: local IP, Netmask, |
1423 |
* Gateway and DNS. |
1424 |
* Mainly Comes from Mento Supplicant v 6.0 beta 3 by soar. |
1425 |
* |
1426 |
************************************************************************/ |
1427 |
void checksumNP(uint8_t * sCircleBase,uint8_t * RuijieChecksum) |
1428 |
{ |
1429 |
static uint8_t Table[]={ |
1430 |
0x00,0x00,0x21,0x10,0x42,0x20,0x63,0x30,0x84,0x40,0xA5,0x50,0xC6,0x60,0xE7,0x70, |
1431 |
0x08,0x81,0x29,0x91,0x4A,0xA1,0x6B,0xB1,0x8C,0xC1,0xAD,0xD1,0xCE,0xE1,0xEF,0xF1, |
1432 |
0x31,0x12,0x10,0x02,0x73,0x32,0x52,0x22,0xB5,0x52,0x94,0x42,0xF7,0x72,0xD6,0x62, |
1433 |
0x39,0x93,0x18,0x83,0x7B,0xB3,0x5A,0xA3,0xBD,0xD3,0x9C,0xC3,0xFF,0xF3,0xDE,0xE3, |
1434 |
0x62,0x24,0x43,0x34,0x20,0x04,0x01,0x14,0xE6,0x64,0xC7,0x74,0xA4,0x44,0x85,0x54, |
1435 |
0x6A,0xA5,0x4B,0xB5,0x28,0x85,0x09,0x95,0xEE,0xE5,0xCF,0xF5,0xAC,0xC5,0x8D,0xD5, |
1436 |
0x53,0x36,0x72,0x26,0x11,0x16,0x30,0x06,0xD7,0x76,0xF6,0x66,0x95,0x56,0xB4,0x46, |
1437 |
0x5B,0xB7,0x7A,0xA7,0x19,0x97,0x38,0x87,0xDF,0xF7,0xFE,0xE7,0x9D,0xD7,0xBC,0xC7, |
1438 |
0xC4,0x48,0xE5,0x58,0x86,0x68,0xA7,0x78,0x40,0x08,0x61,0x18,0x02,0x28,0x23,0x38, |
1439 |
0xCC,0xC9,0xED,0xD9,0x8E,0xE9,0xAF,0xF9,0x48,0x89,0x69,0x99,0x0A,0xA9,0x2B,0xB9, |
1440 |
0xF5,0x5A,0xD4,0x4A,0xB7,0x7A,0x96,0x6A,0x71,0x1A,0x50,0x0A,0x33,0x3A,0x12,0x2A, |
1441 |
0xFD,0xDB,0xDC,0xCB,0xBF,0xFB,0x9E,0xEB,0x79,0x9B,0x58,0x8B,0x3B,0xBB,0x1A,0xAB, |
1442 |
0xA6,0x6C,0x87,0x7C,0xE4,0x4C,0xC5,0x5C,0x22,0x2C,0x03,0x3C,0x60,0x0C,0x41,0x1C, |
1443 |
0xAE,0xED,0x8F,0xFD,0xEC,0xCD,0xCD,0xDD,0x2A,0xAD,0x0B,0xBD,0x68,0x8D,0x49,0x9D, |
1444 |
0x97,0x7E,0xB6,0x6E,0xD5,0x5E,0xF4,0x4E,0x13,0x3E,0x32,0x2E,0x51,0x1E,0x70,0x0E, |
1445 |
0x9F,0xFF,0xBE,0xEF,0xDD,0xDF,0xFC,0xCF,0x1B,0xBF,0x3A,0xAF,0x59,0x9F,0x78,0x8F, |
1446 |
0x88,0x91,0xA9,0x81,0xCA,0xB1,0xEB,0xA1,0x0C,0xD1,0x2D,0xC1,0x4E,0xF1,0x6F,0xE1, |
1447 |
0x80,0x10,0xA1,0x00,0xC2,0x30,0xE3,0x20,0x04,0x50,0x25,0x40,0x46,0x70,0x67,0x60, |
1448 |
0xB9,0x83,0x98,0x93,0xFB,0xA3,0xDA,0xB3,0x3D,0xC3,0x1C,0xD3,0x7F,0xE3,0x5E,0xF3, |
1449 |
0xB1,0x02,0x90,0x12,0xF3,0x22,0xD2,0x32,0x35,0x42,0x14,0x52,0x77,0x62,0x56,0x72, |
1450 |
0xEA,0xB5,0xCB,0xA5,0xA8,0x95,0x89,0x85,0x6E,0xF5,0x4F,0xE5,0x2C,0xD5,0x0D,0xC5, |
1451 |
0xE2,0x34,0xC3,0x24,0xA0,0x14,0x81,0x04,0x66,0x74,0x47,0x64,0x24,0x54,0x05,0x44, |
1452 |
0xDB,0xA7,0xFA,0xB7,0x99,0x87,0xB8,0x97,0x5F,0xE7,0x7E,0xF7,0x1D,0xC7,0x3C,0xD7, |
1453 |
0xD3,0x26,0xF2,0x36,0x91,0x06,0xB0,0x16,0x57,0x66,0x76,0x76,0x15,0x46,0x34,0x56, |
1454 |
0x4C,0xD9,0x6D,0xC9,0x0E,0xF9,0x2F,0xE9,0xC8,0x99,0xE9,0x89,0x8A,0xB9,0xAB,0xA9, |
1455 |
0x44,0x58,0x65,0x48,0x06,0x78,0x27,0x68,0xC0,0x18,0xE1,0x08,0x82,0x38,0xA3,0x28, |
1456 |
0x7D,0xCB,0x5C,0xDB,0x3F,0xEB,0x1E,0xFB,0xF9,0x8B,0xD8,0x9B,0xBB,0xAB,0x9A,0xBB, |
1457 |
0x75,0x4A,0x54,0x5A,0x37,0x6A,0x16,0x7A,0xF1,0x0A,0xD0,0x1A,0xB3,0x2A,0x92,0x3A, |
1458 |
0x2E,0xFD,0x0F,0xED,0x6C,0xDD,0x4D,0xCD,0xAA,0xBD,0x8B,0xAD,0xE8,0x9D,0xC9,0x8D, |
1459 |
0x26,0x7C,0x07,0x6C,0x64,0x5C,0x45,0x4C,0xA2,0x3C,0x83,0x2C,0xE0,0x1C,0xC1,0x0C, |
1460 |
0x1F,0xEF,0x3E,0xFF,0x5D,0xCF,0x7C,0xDF,0x9B,0xAF,0xBA,0xBF,0xD9,0x8F,0xF8,0x9F, |
1461 |
0x17,0x6E,0x36,0x7E,0x55,0x4E,0x74,0x5E,0x93,0x2E,0xB2,0x3E,0xD1,0x0E,0xF0,0x1E}; |
1462 |
int ax=0,bx=0,dx=0,i; |
1463 |
|
1464 |
for (i=0 ; i<0x15 ; i++ ) |
1465 |
{ |
1466 |
dx = ax; |
1467 |
bx = 0; |
1468 |
bx = (bx&0xff00) | (sCircleBase[i]); |
1469 |
dx &= 0xffff; |
1470 |
dx >>= 8; |
1471 |
dx ^= bx; |
1472 |
bx = 0; |
1473 |
bx &= 0x00ff; |
1474 |
bx |= (ax&0xff)<<8; |
1475 |
|
1476 |
ax = Table[dx*2] | Table[dx*2+1]<<8; |
1477 |
ax ^= bx; |
1478 |
} |
1479 |
RuijieChecksum[0] = (uint8_t) ((ax&0xff00)>>8); |
1480 |
RuijieChecksum[1] = (uint8_t) (ax&0x00ff); |
1481 |
} |
1482 |
|
1483 |
/************************************************************************ |
1484 |
* getNP is use to get IP, Netmask, Hardward Address of eth0. |
1485 |
* It also check wether we use static IP or shift to 2nd authentication. |
1486 |
*************************************************************************/ |
1487 |
|
1488 |
int getNP(struct interface_data *thisint, uint8_t *address, int request) |
1489 |
{ |
1490 |
|
1491 |
int sock,i; |
1492 |
struct ifreq ifr; |
1493 |
// initiate the socket |
1494 |
sock = socket(AF_INET,SOCK_DGRAM,0); |
1495 |
if (sock == -1) |
1496 |
{ |
1497 |
perror("socket"); |
1498 |
return -1; |
1499 |
} |
1500 |
|
1501 |
|
1502 |
strncpy(ifr.ifr_name, thisint->intName, IFNAMSIZ); |
1503 |
ifr.ifr_name[IFNAMSIZ - 1] = 0; |
1504 |
|
1505 |
if (ioctl(sock,request,&ifr) < 0) |
1506 |
{ |
1507 |
perror("ioctl"); |
1508 |
return -1; |
1509 |
} |
1510 |
//memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); |
1511 |
|
1512 |
memcpy(address, ifr.ifr_addr.sa_data+2, 4); |
1513 |
|
1514 |
for(i=0;i<4;i++) |
1515 |
debug_printf(DEBUG_NORMAL,"%.2x ",address[i]); |
1516 |
debug_printf(DEBUG_NORMAL,"\n"); |
1517 |
// bring down the socket |
1518 |
close(sock); |
1519 |
return 0; |
1520 |
} |
1521 |
|
1522 |
/************************************************************************ |
1523 |
* the readNLSock, parseRoutes and gotRoutes are using to get the route |
1524 |
* information via socket. |
1525 |
* Modified from Bob's orginal code. |
1526 |
************************************************************************/ |
1527 |
|
1528 |
|
1529 |
uint8_t ggateway[4]; |
1530 |
|
1531 |
int readNlSock(int sockFd, uint8_t *bufPtr, int seqNum, int pId) |
1532 |
{ |
1533 |
struct nlmsghdr *nlHdr; |
1534 |
int readLen = 0, msgLen = 0; |
1535 |
|
1536 |
do{ |
1537 |
/* Recieve response from the kernel */ |
1538 |
if((readLen = recv(sockFd, bufPtr, BUFSIZE - msgLen, 0)) < 0) |
1539 |
{ |
1540 |
perror("SOCK READ: "); |
1541 |
return -1; |
1542 |
} |
1543 |
|
1544 |
nlHdr = (struct nlmsghdr *)bufPtr; |
1545 |
|
1546 |
/* Check if the header is valid */ |
1547 |
if((NLMSG_OK(nlHdr, readLen) == 0) || (nlHdr->nlmsg_type == NLMSG_ERROR)) |
1548 |
{ |
1549 |
perror("Error in recieved packet"); |
1550 |
return -1; |
1551 |
} |
1552 |
|
1553 |
/* Check if the its the last message */ |
1554 |
if(nlHdr->nlmsg_type == NLMSG_DONE) |
1555 |
{ |
1556 |
break; |
1557 |
} |
1558 |
else |
1559 |
{ |
1560 |
/* Else move the pointer to buffer appropriately */ |
1561 |
bufPtr += readLen; |
1562 |
msgLen += readLen; |
1563 |
} |
1564 |
|
1565 |
/* Check if its a multi part message */ |
1566 |
if((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0) |
1567 |
{ |
1568 |
/* return if its not */ |
1569 |
break; |
1570 |
} |
1571 |
} |
1572 |
while((nlHdr->nlmsg_seq != seqNum) || (nlHdr->nlmsg_pid != pId)); |
1573 |
return msgLen; |
1574 |
} |
1575 |
|
1576 |
/* For parsing the route info returned */ |
1577 |
int parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo, uint8_t * gateway) |
1578 |
{ |
1579 |
struct rtmsg *rtMsg; |
1580 |
struct rtattr *rtAttr; |
1581 |
int rtLen,i; |
1582 |
uint8_t *tempBuf = NULL; |
1583 |
|
1584 |
tempBuf = (uint8_t *)malloc(100); |
1585 |
rtMsg = (struct rtmsg *)NLMSG_DATA(nlHdr); |
1586 |
|
1587 |
/* If the route is not for AF_INET or does not belong to main routing table |
1588 |
then return. */ |
1589 |
if((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN)) |
1590 |
return -1; |
1591 |
|
1592 |
/* get the rtattr field */ |
1593 |
rtAttr = (struct rtattr *)RTM_RTA(rtMsg); |
1594 |
rtLen = RTM_PAYLOAD(nlHdr); |
1595 |
for(;RTA_OK(rtAttr,rtLen);rtAttr = RTA_NEXT(rtAttr,rtLen)) |
1596 |
{ |
1597 |
switch(rtAttr->rta_type) |
1598 |
{ |
1599 |
case RTA_OIF: |
1600 |
if_indextoname(*(int *)RTA_DATA(rtAttr), rtInfo->ifName); |
1601 |
break; |
1602 |
case RTA_GATEWAY: |
1603 |
rtInfo->gateWay = *(uint32_t *)RTA_DATA(rtAttr); |
1604 |
break; |
1605 |
case RTA_PREFSRC: |
1606 |
rtInfo->srcAddr = *(uint32_t *)RTA_DATA(rtAttr); |
1607 |
break; |
1608 |
case RTA_DST: |
1609 |
rtInfo->dstAddr = *(uint32_t *)RTA_DATA(rtAttr); |
1610 |
break; |
1611 |
} |
1612 |
} |
1613 |
//printf("%s\n", (uint8_t *)inet_ntoa(rtInfo->dstAddr)); |
1614 |
// ADDED BY BOB - ALSO COMMENTED printRoute |
1615 |
if (strstr((uint8_t *)inet_ntoa(*(struct in_addr *)&rtInfo->dstAddr), "0.0.0.0")) |
1616 |
{ |
1617 |
memcpy(gateway,&rtInfo->gateWay,4); |
1618 |
for(i=0;i<4;i++) |
1619 |
printf("%.2x ",gateway[i]); |
1620 |
printf("\n"); |
1621 |
} |
1622 |
//printRoute(rtInfo); |
1623 |
free(tempBuf); |
1624 |
return 0; |
1625 |
} |
1626 |
|
1627 |
int gotRoute(uint8_t * Gateway) |
1628 |
{ |
1629 |
struct nlmsghdr *nlMsg; |
1630 |
struct rtmsg *rtMsg; |
1631 |
struct route_info *rtInfo; |
1632 |
uint8_t msgBuf[BUFSIZE]; |
1633 |
|
1634 |
int sock, len, msgSeq = 0; |
1635 |
|
1636 |
/* Create Socket */ |
1637 |
if((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) |
1638 |
perror("Socket Creation: "); |
1639 |
|
1640 |
/* Initialize the buffer */ |
1641 |
memset(msgBuf, 0, BUFSIZE); |
1642 |
|
1643 |
/* point the header and the msg structure pointers into the buffer */ |
1644 |
nlMsg = (struct nlmsghdr *)msgBuf; |
1645 |
rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg); |
1646 |
|
1647 |
/* Fill in the nlmsg header*/ |
1648 |
nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length of message. |
1649 |
nlMsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table . |
1650 |
|
1651 |
nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump. |
1652 |
nlMsg->nlmsg_seq = msgSeq++; // Sequence of the message packet. |
1653 |
nlMsg->nlmsg_pid = getpid(); // PID of process sending the request. |
1654 |
|
1655 |
/* Send the request */ |
1656 |
if(send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0) |
1657 |
{ |
1658 |
printf("Write To Socket Failed...\n"); |
1659 |
return -1; |
1660 |
} |
1661 |
|
1662 |
/* Read the response */ |
1663 |
if((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0) |
1664 |
{ |
1665 |
printf("Read From Socket Failed...\n"); |
1666 |
return -1; |
1667 |
} |
1668 |
/* Parse and print the response */ |
1669 |
rtInfo = (struct route_info *)malloc(sizeof(struct route_info)); |
1670 |
// ADDED BY BOB |
1671 |
/* THIS IS THE NETTSTAT -RL code I commented out the printing here and in parse routes */ |
1672 |
//fprintf(stdout, "Destination\tGateway\tInterface\tSource\n"); |
1673 |
for(;NLMSG_OK(nlMsg,len);nlMsg = NLMSG_NEXT(nlMsg,len)) |
1674 |
{ |
1675 |
memset(rtInfo, 0, sizeof(struct route_info)); |
1676 |
parseRoutes(nlMsg, rtInfo,Gateway); |
1677 |
} |
1678 |
if(Gateway[0]==0) |
1679 |
{ |
1680 |
printf("Couldn't get route!\n"); |
1681 |
return -1; |
1682 |
} |
1683 |
free(rtInfo); |
1684 |
close(sock); |
1685 |
// ADDED BY BOB |
1686 |
return 0; |
1687 |
} |
1688 |
|
1689 |
|
1690 |
/************************************************************************ |
1691 |
* gotDNS use some trick to got DNS, because I feel that system also look |
1692 |
* into /etc/resolv.conf for DNS :) |
1693 |
* **********************************************************************/ |
1694 |
int gotDNS(uint8_t * dns) |
1695 |
{ |
1696 |
FILE* dnsfile; |
1697 |
char input[20]; |
1698 |
system("cat /etc/resolv.conf | sed -n '/^nameserver/p' | awk '{print $2}' > /tmp/dns.conf"); |
1699 |
dnsfile=fopen("/tmp/dns.conf","r"); |
1700 |
if(dnsfile==NULL) |
1701 |
{ |
1702 |
perror("Error opening file"); |
1703 |
return -1; |
1704 |
} |
1705 |
fgets(input,20,dnsfile); |
1706 |
input[19]=0; |
1707 |
sprintf(DNS,"%s",input); |
1708 |
DNS[19]=0; |
1709 |
inet_aton(input,dns); |
1710 |
fclose(dnsfile); |
1711 |
remove("/tmp/dns.conf"); |
1712 |
debug_printf(DEBUG_EVERYTHING,"The DNS is %s\n",input); |
1713 |
return 0; |
1714 |
} |
1715 |
|