-
Notifications
You must be signed in to change notification settings - Fork 102
/
functions.c
1853 lines (1680 loc) · 61.1 KB
/
functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h> /* To set non blocking on socket */
#include<sys/socket.h> /* Generic socket calls */
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/ioctl.h>
#include<time.h>
#include<sys/types.h>
#include<signal.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<linux/if_arp.h>
#include "headers.h"
//Defined in dhtest.c
extern int sock_packet;
extern struct sockaddr_ll ll;
extern int iface;
extern u_int16_t vlan;
extern u_int8_t l3_tos;
extern u_int16_t l2_hdr_size;
extern u_int16_t l3_hdr_size;
extern u_int16_t l4_hdr_size;
extern u_int16_t dhcp_hdr_size;
extern u_int16_t fqdn_n;
extern u_int16_t fqdn_s;
extern u_char dhcp_packet_disc[1514];
extern u_char dhcp_packet_offer[1514];
extern u_char dhcp_packet_request[1514];
extern u_char dhcp_packet_ack[1514];
extern u_char dhcp_packet_release[1514];
extern u_char dhcp_packet_decline[1514];
extern u_int8_t dhopt_buff[500];
extern u_int32_t dhopt_size;
extern u_int32_t dhcp_xid;
extern u_int32_t bcast_flag;
extern u_int8_t timeout;
extern u_int8_t rtrmac_flag;
extern u_int8_t padding_flag;
extern u_int8_t vci_buff[256];
extern u_int8_t hostname_buff[256];
extern u_int8_t fqdn_buff[256];
extern u_int32_t option51_lease_time;
extern u_int8_t option55_req_flag;
extern u_int8_t option55_req_list[256];
extern u_int32_t option55_req_len;
extern u_int32_t port;
extern u_int8_t unicast_flag;
extern u_int8_t nagios_flag;
extern u_int8_t json_flag;
extern u_int8_t json_first;
extern u_char *giaddr;
extern u_char *server_addr;
extern u_int8_t no_custom_dhcp_options;
extern struct custom_dhcp_option_hdr custom_dhcp_options[255];
extern struct ethernet_hdr *eth_hg;
extern struct vlan_hdr *vlan_hg;
extern struct iphdr *iph_g;
extern struct udphdr *uh_g;
extern struct dhcpv4_hdr *dhcph_g;
extern u_int8_t *dhopt_pointer_g;
extern struct arp_hdr *arp_hg;
extern struct icmp_hdr *icmp_hg;
extern u_char dhmac[ETHER_ADDR_LEN];
extern u_char rtrmac[ETHER_ADDR_LEN];
extern u_char dmac[ETHER_ADDR_LEN];
extern u_char iface_mac[ETHER_ADDR_LEN];
extern char dhmac_fname[20];
extern char iface_name[30];
extern char ip_str[128];
extern u_int32_t server_id, option50_ip;
extern u_int8_t dhcp_release_flag;
extern u_int32_t unicast_ip_address;
extern u_int32_t ip_address;
extern u_char ip_listen_flag;
extern u_char arp_icmp_packet[1514];
extern u_char arp_icmp_reply[1514];
extern u_int16_t icmp_len;
extern struct timeval tval_listen;
extern u_int32_t listen_timeout;
/*
* Opens PF_PACKET socket and return error if socket
* opens fails
*/
int open_socket()
{
sock_packet = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(sock_packet < 0) {
perror("--Error on creating the socket--");
return SOCKET_ERR;
}
/* Set link layer parameters */
ll.sll_family = AF_PACKET;
ll.sll_protocol = htons(ETH_P_ALL);
ll.sll_ifindex = iface;
ll.sll_hatype = ARPHRD_ETHER;
ll.sll_pkttype = PACKET_OTHERHOST;
ll.sll_halen = 6;
bind(sock_packet, (struct sockaddr *)&ll, sizeof(struct sockaddr_ll));
return 0;
}
/*
* Closes PF_PACKET socket
*/
int close_socket()
{
close(sock_packet);
return 0;
}
/*
* Sets the promiscous mode on the interface
*/
int set_promisc()
{
int status;
struct ifreq ifr;
if(!strlen((const char *) iface_name)) {
strcpy(iface_name, "eth0");
}
strcpy(ifr.ifr_name, iface_name);
ifr.ifr_flags = (IFF_PROMISC | IFF_UP);
status = ioctl(sock_packet, SIOCSIFFLAGS, &ifr);
if(status < 0) {
if (nagios_flag) {
fprintf(stdout, "CRITICAL: Error setting promisc.");
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"Error setting promisc.\","
"\"result\":\"error\","
"\"error-type\":\"enable-promisc\","
"\"error-msg\":\"Error setting promisc.\"}"
"]");
} else {
perror("Error on setting promisc");
}
exit(2);
}
return 0;
}
int clear_promisc()
{
int status;
struct ifreq ifr;
strcpy(ifr.ifr_name, iface_name);
ifr.ifr_flags = IFF_UP;
status = ioctl(sock_packet, SIOCSIFFLAGS, &ifr);
if(status < 0) {
if (nagios_flag) {
fprintf(stdout, "CRITICAL: Error on disabling promisc");
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"Error on disabling promisc.\","
"\"result\":\"error\","
"\"error-type\":\"disable-promisc\","
"\"error-msg\":\"Error on disabling promisc.\"}"
"]");
} else {
perror("Error on disabling promisc");
}
exit(2);
}
return 0;
}
/*
* Get address from the interface
*/
u_int32_t get_interface_address()
{
int status;
struct ifreq ifr;
if(!strlen((const char *) iface_name)) {
strcpy(iface_name, "eth0");
}
strcpy(ifr.ifr_name, iface_name);
ifr.ifr_addr.sa_family = AF_INET;
status = ioctl(sock_packet, SIOCGIFADDR, &ifr);
if(status < 0) {
if (nagios_flag) {
fprintf(stdout, "CRITICAL: Error getting interface address.");
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"Error getting interface address.\","
"\"result\":\"error\","
"\"error-type\":\"get-iface-addr\","
"\"error-msg\":\"Error getting interface address.\"}"
"]");
} else {
perror("Error getting interface address.");
}
exit(2);
}
return ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
}
/*
* Sends DHCP packet on the socket. Packet type
* is passed as argument. Extended to send ARP and ICMP packets
*/
int send_packet(int pkt_type)
{
int ret = -1;
if(pkt_type == DHCP_MSGDISCOVER) {
ret = sendto(sock_packet,\
dhcp_packet_disc,\
(l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size),\
0,\
(struct sockaddr *) &ll,\
sizeof(ll));
} else if(pkt_type == DHCP_MSGREQUEST) {
ret = sendto(sock_packet,\
dhcp_packet_request,\
(l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size),\
0,\
(struct sockaddr *) &ll,\
sizeof(ll));
} else if(pkt_type == DHCP_MSGRELEASE) {
ret = sendto(sock_packet,\
dhcp_packet_release,\
(l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size),\
0,\
(struct sockaddr *) &ll,\
sizeof(ll));
} else if(pkt_type == DHCP_MSGDECLINE) {
ret = sendto(sock_packet,\
dhcp_packet_decline,\
(l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size),\
0,\
(struct sockaddr *) &ll,\
sizeof(ll));
} else if(pkt_type == ARP_SEND) {
ret = sendto(sock_packet,\
arp_icmp_reply,\
60,\
0,\
(struct sockaddr *) &ll,\
sizeof(ll));
} else if(pkt_type == ICMP_SEND) {
ret = sendto(sock_packet,\
arp_icmp_reply,\
(l2_hdr_size + l3_hdr_size + ICMP_H + icmp_len),\
0,\
(struct sockaddr *) &ll,\
sizeof(ll));
}
if(ret < 0) {
if (nagios_flag) {
fprintf(stdout, "CRITICAL: Packet send failure.");
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"Packet send failure.\","
"\"result\":\"error\","
"\"error-type\":\"pkg-send\","
"\"error-msg\":\"Packet send failure.\"}"
"]");
} else {
perror("Packet send failure");
}
close(sock_packet);
exit(2);
return PACK_SEND_ERR;
} else {
if(pkt_type == DHCP_MSGDISCOVER) {
if (!nagios_flag && !json_flag) {
fprintf(stdout, "DHCP discover sent\t - ");
fprintf(stdout, "Client MAC : %02x:%02x:%02x:%02x:%02x:%02x\n", \
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"DHCP discover sent - Client MAC: "
"%02x:%02x:%02x:%02x:%02x:%02x\","
"\"result\":\"success\","
"\"result-type\":\"sent\","
"\"result-subtype\":\"DISCOVER\","
"\"result-mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\""
"}",
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5],
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
}
} else if (pkt_type == DHCP_MSGREQUEST) {
if (!nagios_flag && !json_flag) {
fprintf(stdout, "DHCP request sent\t - ");
fprintf(stdout, "Client MAC : %02x:%02x:%02x:%02x:%02x:%02x\n", \
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"DHCP request sent - Client MAC: "
"%02x:%02x:%02x:%02x:%02x:%02x\","
"\"result\":\"success\","
"\"result-type\":\"sent\","
"\"result-subtype\":\"REQUEST\","
"\"result-mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\""
"}",
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5],
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
}
} else if (pkt_type == DHCP_MSGRELEASE) {
if (!nagios_flag && !json_flag) {
fprintf(stdout, "DHCP release sent\t - ");
fprintf(stdout, "Client MAC : %02x:%02x:%02x:%02x:%02x:%02x\n", \
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"DHCP release sent - Client MAC: "
"%02x:%02x:%02x:%02x:%02x:%02x\","
"\"result\":\"success\","
"\"result-type\":\"sent\","
"\"result-subtype\":\"RELEASE\","
"\"result-mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\""
"}",
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5],
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
}
} else if (pkt_type == DHCP_MSGDECLINE) {
if (!nagios_flag && !json_flag) {
fprintf(stdout, "DHCP decline sent\t - ");
fprintf(stdout, "Client MAC : %02x:%02x:%02x:%02x:%02x:%02x\n", \
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
} else if(json_flag) {
if(!json_first) {
fprintf(stdout, ",");
} else {
json_first = 0;
}
fprintf(stdout, "{\"msg\":\"DHCP decline sent - Client MAC: "
"%02x:%02x:%02x:%02x:%02x:%02x\","
"\"result\":\"success\","
"\"result-type\":\"sent\","
"\"result-subtype\":\"DECLINE\","
"\"result-mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\""
"}",
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5],
dhmac[0], dhmac[1], dhmac[2], dhmac[3], dhmac[4], dhmac[5]);
}
}
}
return 0;
}
/*
* Receives DHCP packet. Packet type is passed as argument
* Extended to recv ARP and ICMP packets
*/
int recv_packet(int pkt_type)
{
int ret = -1, sock_len, retval, chk_pkt_state;
fd_set read_fd;
struct timeval tval;
tval.tv_sec = 5;
tval.tv_usec = 0;
if(pkt_type == DHCP_MSGOFFER) {
while(tval.tv_sec != 0) {
FD_ZERO(&read_fd);
FD_SET(sock_packet, &read_fd);
retval = select(sock_packet + 1, &read_fd, NULL, NULL, &tval);
if(retval == 0) {
return DHCP_DISC_RESEND;
break;
} else if ( retval > 0 && FD_ISSET(sock_packet, &read_fd)) {
bzero(dhcp_packet_offer, sizeof(dhcp_packet_offer));
sock_len = sizeof(ll);
ret = recvfrom(sock_packet,\
dhcp_packet_offer,\
sizeof(dhcp_packet_offer),\
0,\
(struct sockaddr *)&ll,
(socklen_t *) &sock_len);
}
if(ret >= 60) {
chk_pkt_state = check_packet(DHCP_MSGOFFER);
if(chk_pkt_state == DHCP_OFFR_RCVD) {
return DHCP_OFFR_RCVD;
}
}
}
return DHCP_DISC_RESEND;
} else if(pkt_type == DHCP_MSGACK) {
while(tval.tv_sec != 0) {
FD_ZERO(&read_fd);
FD_SET(sock_packet, &read_fd);
retval = select(sock_packet + 1, &read_fd, NULL, NULL, &tval);
if(retval == 0) {
return DHCP_REQ_RESEND;
break;
} else if ( retval > 0 && FD_ISSET(sock_packet, &read_fd)){
bzero(dhcp_packet_ack, sizeof(dhcp_packet_ack));
sock_len = sizeof(ll);
ret = recvfrom(sock_packet,\
dhcp_packet_ack,\
sizeof(dhcp_packet_ack),\
0,\
(struct sockaddr *)&ll,
(socklen_t *) &sock_len);
}
if(ret >= 60) {
chk_pkt_state = check_packet(DHCP_MSGACK);
if(chk_pkt_state == DHCP_ACK_RCVD) {
return DHCP_ACK_RCVD;
} else if(chk_pkt_state == DHCP_NAK_RCVD) {
return DHCP_NAK_RCVD;
}
}
}
return DHCP_REQ_RESEND;
} else if(pkt_type == ARP_ICMP_RCV) {
while(tval_listen.tv_sec != 0) {
FD_ZERO(&read_fd);
FD_SET(sock_packet, &read_fd);
retval = select(sock_packet + 1, &read_fd, NULL, NULL, &tval_listen);
if(retval == 0) {
return LISTEN_TIMOUET;
break;
} else if ( retval > 0 && FD_ISSET(sock_packet, &read_fd)) {
bzero(arp_icmp_packet, sizeof(arp_icmp_packet));
sock_len = sizeof(ll);
ret = recvfrom(sock_packet,\
arp_icmp_packet,\
sizeof(arp_icmp_packet),\
0,\
(struct sockaddr *)&ll,
(socklen_t *) &sock_len);
}
if(ret >= 60) {
chk_pkt_state = check_packet(ARP_ICMP_RCV);
if(chk_pkt_state == ARP_RCVD) {
return ARP_RCVD;
break;
} else if(chk_pkt_state == ICMP_RCVD) {
return ICMP_RCVD;
break;
}
}
}
return LISTEN_TIMOUET;
}
return UNKNOWN_PACKET;
}
/* Debug function - Prints the buffer on HEX format */
int print_buff(u_int8_t *buff, int size)
{
int tmp;
fprintf(stdout, "\n---------Buffer data---------\n");
for(tmp = 0; tmp < size; tmp++) {
fprintf(stdout, "%02X ", buff[tmp]);
if((tmp % 16) == 0 && tmp != 0) {
fprintf(stdout, "\n");
}
}
fprintf(stdout, "\n");
return 0;
}
/* Reset the DHCP option buffer to zero and dhopt_size to zero */
int reset_dhopt_size()
{
bzero(dhopt_buff, sizeof(dhopt_buff));
dhopt_size = 0;
return 0;
}
/*
* Sets a random DHCP xid
*/
int set_rand_dhcp_xid()
{
if(dhcp_xid == 0) {
srand(time(NULL) ^ (getpid() << 16));
dhcp_xid = rand() % 0xffffffff;
}
return 0;
}
/*
* IP checksum function - Calculates the IP checksum
*/
u_int16_t ipchksum(u_int16_t *buff, int words)
{
unsigned int sum, i;
sum = 0;
for(i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = (sum >> 16) + sum;
return (u_int16_t)~sum;
}
/*
* ICMP checksum function - Calculates the ICMP checksum
*/
u_int16_t icmpchksum(u_int16_t *buff, int words)
{
unsigned int sum, i;
unsigned int last_word = 0;
/* Checksum enhancement for odd packets */
if((icmp_len % 2) == 1) {
last_word = *((u_int8_t *)buff + icmp_len + ICMP_H - 1);
last_word = (htons(last_word) << 8);
sum = 0;
for(i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = sum + last_word;
sum = (sum >> 16) + sum;
return (u_int16_t)~sum;
} else {
sum = 0;
for(i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = (sum >> 16) + sum;
return (u_int16_t)~sum;
}
}
/*
* TCP/UDP checksum function
*/
u_int16_t l4_sum(u_int16_t *buff, int words, u_int16_t *srcaddr, u_int16_t *dstaddr, u_int16_t proto, u_int16_t len)
{
unsigned int i, last_word;
uint32_t sum;
/* Checksum enhancement - Support for odd byte packets */
if((htons(len) % 2) == 1) {
last_word = *((u_int8_t *)buff + ntohs(len) - 1);
last_word = (htons(last_word) << 8);
} else {
/* Original checksum function */
last_word = 0;
}
sum = 0;
for(i = 0;i < words; i++){
sum = sum + *(buff + i);
}
sum = sum + last_word;
sum = sum + *(srcaddr) + *(srcaddr + 1) + *(dstaddr) + *(dstaddr + 1) + proto + len;
sum = (sum >> 16) + sum;
return ~sum;
}
/*
* Builds DHCP option53 on dhopt_buff
*/
int build_option53(int msg_type)
{
if(msg_type == DHCP_MSGDISCOVER ||
msg_type == DHCP_MSGREQUEST ||
msg_type == DHCP_MSGRELEASE ||
msg_type == DHCP_MSGDECLINE) {
u_int8_t msgtype = DHCP_MESSAGETYPE;
u_int8_t msglen = 1;
u_int8_t msg = (u_int8_t) msg_type;
memcpy(dhopt_buff, &msgtype, 1);
memcpy(dhopt_buff + 1, &msglen, 1);
memcpy(dhopt_buff + 2, &msg, 1);
dhopt_size = dhopt_size + 3;
}
return 0;
}
/*
* Builds DHCP option50 on dhopt_buff
*/
int build_option50()
{
u_int8_t msgtype = DHCP_REQUESTEDIP;
u_int8_t msglen = 4;
u_int32_t msg = option50_ip;
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), &msg, 4);
dhopt_size = dhopt_size + 6;
return 0;
}
/*
* Builds DHCP option51 on dhopt_buff - DHCP lease time requested
*/
int build_option51()
{
u_int8_t msgtype = DHCP_LEASETIME;
u_int8_t msglen = 4;
u_int32_t msg = htonl(option51_lease_time);
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), &msg, 4);
dhopt_size = dhopt_size + 6;
return 0;
}
/*
* Builds DHCP option54 on dhopt_buff
*/
int build_option54()
{
u_int8_t msgtype = DHCP_SERVIDENT;
u_int8_t msglen = 4;
u_int32_t msg = server_id;
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), &msg, 4);
dhopt_size = dhopt_size + 6;
return 0;
}
/*
* Builds DHCP option55 on dhopt_buff
*/
int build_option55()
{
if (option55_req_flag == 0) {
u_int32_t msgtype = DHCP_PARAMREQUEST;
u_int32_t msglen = 5;
u_int8_t msg[5] = { 0 };
msg[0] = DHCP_SUBNETMASK;
msg[1] = DHCP_BROADCASTADDR;
msg[2] = DHCP_ROUTER;
msg[3] = DHCP_DOMAINNAME;
msg[4] = DHCP_DNS;
/* msg[5] = DHCP_LOGSERV; */
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), msg, 5);
dhopt_size = dhopt_size + 7;
return 0;
} else if (option55_req_flag == 1) {
u_int32_t msgtype = DHCP_PARAMREQUEST;
u_int32_t msglen = option55_req_len;
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), option55_req_list, option55_req_len);
dhopt_size = dhopt_size + option55_req_len + 2;
}
}
/*
* Builds DHCP option60 on dhopt_buff
*/
int build_option60_vci()
{
u_int32_t msgtype = DHCP_CLASSSID;
u_int32_t msglen = strlen((const char *) vci_buff);
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), vci_buff, strlen((const char *) vci_buff));
dhopt_size = dhopt_size + 2 + strlen((const char *) vci_buff);
return 0;
}
/*
* Builds DHCP option 12, hostname, on dhopt_buff
*/
int build_option12_hostname()
{
u_int32_t msgtype = DHCP_HOSTNAME;
u_int32_t msglen = strlen((const char *) hostname_buff);
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), hostname_buff, strlen((const char *) hostname_buff));
dhopt_size = dhopt_size + 2 + strlen((const char *) hostname_buff);
return 0;
}
/*
* Builds DHCP option 81, fqdn, on dhopt_buff
*/
int build_option81_fqdn()
{
u_int32_t msgtype = DHCP_FQDN;
u_int8_t flags = 0;
u_int8_t rcode1 = 0;
u_int8_t rcode2 = 0;
u_int32_t msglen = strlen((const char *) fqdn_buff) + 3;
if (fqdn_n)
flags |= FQDN_N_FLAG;
if (fqdn_s)
flags |= FQDN_S_FLAG;
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
memcpy((dhopt_buff + dhopt_size + 2), &flags, 1);
memcpy((dhopt_buff + dhopt_size + 3), &rcode1, 1);
memcpy((dhopt_buff + dhopt_size + 4), &rcode2, 1);
memcpy((dhopt_buff + dhopt_size + 5), fqdn_buff, strlen((const char *) fqdn_buff));
dhopt_size = dhopt_size + 2 + msglen;
return 0;
}
/*
* Builds custom DHCP options passed from command line
*/
int build_custom_dhcp_options()
{
int option_index;
for(option_index = 0; option_index < no_custom_dhcp_options; option_index++) {
u_int8_t msgtype = custom_dhcp_options[option_index].option_no;
u_int8_t msglen = custom_dhcp_options[option_index].option_len;
u_int8_t option_type = custom_dhcp_options[option_index].option_type;
memcpy((dhopt_buff + dhopt_size), &msgtype, 1);
memcpy((dhopt_buff + dhopt_size + 1), &msglen, 1);
if(option_type == CUST_DHCP_OPTION_IP) {
memcpy((dhopt_buff + dhopt_size + 2), &custom_dhcp_options[option_index].option_value_ip, msglen);
} else if(option_type == CUST_DHCP_OPTION_NUMBER) {
memcpy((dhopt_buff + dhopt_size + 2), &custom_dhcp_options[option_index].option_value_num, msglen);
} else {
memcpy((dhopt_buff + dhopt_size + 2), custom_dhcp_options[option_index].option_value, msglen);
}
//memcpy((dhopt_buff + dhopt_size + 2), hostname_buff, strlen((const char *) hostname_buff));
dhopt_size = dhopt_size + 2 + msglen;
}
return 0;
}
/*
* Builds DHCP end of option on dhopt_buff
*/
int build_optioneof()
{
u_int8_t eof = 0xff;
memcpy((dhopt_buff + dhopt_size), &eof, 1);
dhopt_size = dhopt_size + 1;
return 0;
}
/*
* Build DHCP packet. Packet type is passed as argument
*/
int build_dhpacket(int pkt_type)
{
u_int32_t dhcp_packet_size = dhcp_hdr_size + dhopt_size;
if(!dhcp_release_flag) {
if (!rtrmac_flag) {
u_char dmac_tmp[ETHER_ADDR_LEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
memcpy(dmac, dmac_tmp, ETHER_ADDR_LEN);
} else {
memcpy (dmac, rtrmac, ETHER_ADDR_LEN);
}
}
if(pkt_type == DHCP_MSGDISCOVER) {
if(vlan == 0) {
struct ethernet_hdr *ethhdr = (struct ethernet_hdr *)dhcp_packet_disc;
memcpy(ethhdr->ether_dhost, dmac, ETHER_ADDR_LEN);
memcpy(ethhdr->ether_shost, iface_mac, ETHER_ADDR_LEN);
ethhdr->ether_type = htons(ETHERTYPE_IP);
} else {
struct vlan_hdr *vhdr = (struct vlan_hdr *)dhcp_packet_disc;
memcpy(vhdr->vlan_dhost, dmac, ETHER_ADDR_LEN);
memcpy(vhdr->vlan_shost, iface_mac, ETHER_ADDR_LEN);
vhdr->vlan_tpi = htons(ETHERTYPE_VLAN);
vhdr->vlan_priority_c_vid = htons(vlan);
vhdr->vlan_len = htons(ETHERTYPE_IP);
}
//print_buff(dhcp_packet_disc, sizeof(struct ethernet_hdr));
if (padding_flag && dhcp_packet_size < MINIMUM_PACKET_SIZE) {
memset(dhopt_buff + dhopt_size, 0, MINIMUM_PACKET_SIZE - dhcp_packet_size);
dhopt_size += MINIMUM_PACKET_SIZE - dhcp_packet_size;
}
struct iphdr *iph = (struct iphdr *)(dhcp_packet_disc + l2_hdr_size);
iph->version = 4;
iph->ihl = 5;
iph->tos = l3_tos;
iph->tot_len = htons(l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size);
iph->id = 0;
iph->frag_off = 0;
iph->ttl = 64;
iph->protocol = 17;
iph->check = 0; // Filled later;
if (unicast_flag)
iph->saddr = unicast_ip_address;
else
iph->saddr = inet_addr("0.0.0.0");
iph->daddr = inet_addr((const char *) server_addr);
iph->check = ipchksum((u_int16_t *)(dhcp_packet_disc + l2_hdr_size), iph->ihl << 1);
struct udphdr *uh = (struct udphdr *) (dhcp_packet_disc + l2_hdr_size + l3_hdr_size);
uh->source = htons(port + 1);
uh->dest = htons(port);
u_int16_t l4_proto = 17;
u_int16_t l4_len = (l4_hdr_size + dhcp_hdr_size + dhopt_size);
uh->len = htons(l4_len);
uh->check = 0; /* UDP checksum will be done after dhcp header*/
struct dhcpv4_hdr *dhpointer = (struct dhcpv4_hdr *)(dhcp_packet_disc + l2_hdr_size + l3_hdr_size + l4_hdr_size);
dhpointer->dhcp_opcode = DHCP_REQUEST;
dhpointer->dhcp_htype = ARPHRD_ETHER;
dhpointer->dhcp_hlen = ETHER_ADDR_LEN;
dhpointer->dhcp_hopcount = 0;
dhpointer->dhcp_xid = htonl(dhcp_xid);
dhpointer->dhcp_secs = 0;
dhpointer->dhcp_flags = bcast_flag;
if (unicast_flag)
dhpointer->dhcp_cip = unicast_ip_address;
else
dhpointer->dhcp_cip = 0;
dhpointer->dhcp_yip = 0;
dhpointer->dhcp_sip = 0;
dhpointer->dhcp_gip = inet_addr((const char *) giaddr);
memcpy(dhpointer->dhcp_chaddr, dhmac, ETHER_ADDR_LEN);
/*dhpointer->dhcp_sname
dhpointer->dhcp_file*/
dhpointer->dhcp_magic = htonl(DHCP_MAGIC);
/* DHCP option buffer is copied here to DHCP packet */
u_char *dhopt_pointer = (u_char *)(dhcp_packet_disc + l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size);
memcpy(dhopt_pointer, dhopt_buff, dhopt_size);
/* UDP checksum is done here */
uh->check = l4_sum((u_int16_t *) (dhcp_packet_disc + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), (u_int16_t *)&iph->saddr, (u_int16_t *)&iph->daddr, htons(l4_proto), htons(l4_len));
}
if(pkt_type == DHCP_MSGREQUEST) {
if(vlan == 0) {
struct ethernet_hdr *ethhdr = (struct ethernet_hdr *)dhcp_packet_request;
memcpy(ethhdr->ether_dhost, dmac, ETHER_ADDR_LEN);
memcpy(ethhdr->ether_shost, iface_mac, ETHER_ADDR_LEN);
ethhdr->ether_type = htons(ETHERTYPE_IP);
} else {
struct vlan_hdr *vhdr = (struct vlan_hdr *)dhcp_packet_request;
memcpy(vhdr->vlan_dhost, dmac, ETHER_ADDR_LEN);
memcpy(vhdr->vlan_shost, iface_mac, ETHER_ADDR_LEN);
vhdr->vlan_tpi = htons(ETHERTYPE_VLAN);
vhdr->vlan_priority_c_vid = htons(vlan);
vhdr->vlan_len = htons(ETHERTYPE_IP);
}
//print_buff(dhcp_packet_request, sizeof(struct ethernet_hdr));
if (padding_flag && dhcp_packet_size < MINIMUM_PACKET_SIZE) {
memset(dhopt_buff + dhopt_size, 0, MINIMUM_PACKET_SIZE - dhcp_packet_size);
dhopt_size += MINIMUM_PACKET_SIZE - dhcp_packet_size;
}
struct iphdr *iph = (struct iphdr *)(dhcp_packet_request + l2_hdr_size);
iph->version = 4;
iph->ihl = 5;
iph->tos = l3_tos;
iph->tot_len = htons(l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size);
iph->id = 0;
iph->frag_off = 0;
iph->ttl = 64;
iph->protocol = 17;
iph->check = 0; // Filled later;
if (unicast_flag)
iph->saddr = unicast_ip_address;
else
iph->saddr = inet_addr("0.0.0.0");
iph->daddr = inet_addr((const char *) server_addr);
iph->check = ipchksum((u_int16_t *)(dhcp_packet_request + l2_hdr_size), iph->ihl << 1);
struct udphdr *uh = (struct udphdr *) (dhcp_packet_request + l2_hdr_size + l3_hdr_size);
uh->source = htons(port + 1);
uh->dest = htons(port);
u_int16_t l4_proto = 17;
u_int16_t l4_len = (l4_hdr_size + dhcp_hdr_size + dhopt_size);
uh->len = htons(l4_len);
uh->check = 0; /* UDP checksum will be done after building dhcp header*/
struct dhcpv4_hdr *dhpointer = (struct dhcpv4_hdr *)(dhcp_packet_request + l2_hdr_size + l3_hdr_size + l4_hdr_size);
dhpointer->dhcp_opcode = DHCP_REQUEST;
dhpointer->dhcp_htype = ARPHRD_ETHER;
dhpointer->dhcp_hlen = ETHER_ADDR_LEN;
dhpointer->dhcp_hopcount = 0;
dhpointer->dhcp_xid = htonl(dhcp_xid);
dhpointer->dhcp_secs = 0;
dhpointer->dhcp_flags = bcast_flag;
if (unicast_flag)
dhpointer->dhcp_cip = unicast_ip_address;
else
dhpointer->dhcp_cip = 0;
dhpointer->dhcp_yip = 0;
dhpointer->dhcp_sip = 0;
dhpointer->dhcp_gip = inet_addr((const char *) giaddr);
memcpy(dhpointer->dhcp_chaddr, dhmac, ETHER_ADDR_LEN);
/*dhpointer->dhcp_sname
dhpointer->dhcp_file*/
dhpointer->dhcp_magic = htonl(DHCP_MAGIC);
/* DHCP option buffer is copied here to DHCP packet */
u_char *dhopt_pointer = (u_char *)(dhcp_packet_request + l2_hdr_size + l3_hdr_size + l4_hdr_size + dhcp_hdr_size);
memcpy(dhopt_pointer, dhopt_buff, dhopt_size);
/* UDP checksum is done here */
uh->check = l4_sum((u_int16_t *) (dhcp_packet_request + l2_hdr_size + l3_hdr_size), ((dhcp_hdr_size + dhopt_size + l4_hdr_size) / 2), (u_int16_t *)&iph->saddr, (u_int16_t *)&iph->daddr, htons(l4_proto), htons(l4_len));
}
if(pkt_type == DHCP_MSGRELEASE) {
if(vlan == 0) {
struct ethernet_hdr *ethhdr = (struct ethernet_hdr *)dhcp_packet_release;
memcpy(ethhdr->ether_dhost, dmac, ETHER_ADDR_LEN);
memcpy(ethhdr->ether_shost, iface_mac, ETHER_ADDR_LEN);
ethhdr->ether_type = htons(ETHERTYPE_IP);
} else {
struct vlan_hdr *vhdr = (struct vlan_hdr *)dhcp_packet_release;
memcpy(vhdr->vlan_dhost, dmac, ETHER_ADDR_LEN);
memcpy(vhdr->vlan_shost, iface_mac, ETHER_ADDR_LEN);
vhdr->vlan_tpi = htons(ETHERTYPE_VLAN);
vhdr->vlan_priority_c_vid = htons(vlan);
vhdr->vlan_len = htons(ETHERTYPE_IP);
}
//(dhcp_packet_disc, sizeof(struct ethernet_hdr));
if (padding_flag && dhcp_packet_size < MINIMUM_PACKET_SIZE) {
memset(dhopt_buff + dhopt_size, 0, MINIMUM_PACKET_SIZE - dhcp_packet_size);
dhopt_size += MINIMUM_PACKET_SIZE - dhcp_packet_size;
}
struct iphdr *iph = (struct iphdr *)(dhcp_packet_release + l2_hdr_size);
iph->version = 4;
iph->ihl = 5;
iph->tos = l3_tos;
iph->tot_len = htons(l3_hdr_size + l4_hdr_size + dhcp_hdr_size + dhopt_size);
iph->id = 0;
iph->frag_off = 0;
iph->ttl = 64;
iph->protocol = 17;
iph->check = 0; // Filled later;
iph->saddr = option50_ip; //inet_addr("0.0.0.0");
iph->daddr = server_id; //inet_addr("255.255.255.255");
iph->check = ipchksum((u_int16_t *)(dhcp_packet_release + l2_hdr_size), iph->ihl << 1);