-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicmp6.c
1117 lines (1001 loc) · 25 KB
/
icmp6.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
/**
* This file is part of pgen, a packet generator tool.
* Copyright (C) 2013 Balakumaran Kannan <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pgen.h"
struct icmp6_hdr{
char type;
char code;
int16_t checksum;
};
struct echo6_pkt {
uint16_t identifier;
uint16_t seq_num;
/**
* data is just a place holder. It only specifies the starting address.
* The entire value from ECHO6_DATA option will be dumped here.
* So user should take care of the buffer size.
*/
char data;
};
struct ndisc_ns_pkt {
/* 4-Byte reserved data */
uint32_t reserved;
char target_addr[16];
/**
* data is just a place holder. It only specifies the starting address.
* The entire value from NDISC_NS_OPTION option will be dumped here.
* So user should take care of the buffer size.
*/
char option;
};
struct ndisc_na_pkt {
/* R, S, O flag [1-bit each] and 29-bits resvered */
uint32_t RSO_res;
char target_addr[16];
/**
* data is just a place holder. It only specifies the starting address.
* The entire value from NDISC_NA_OPTION option will be dumped here.
* So user should take care of the buffer size.
*/
char option;
};
struct ndisc_rs_pkt {
/* 4-Byte reserved field */
uint32_t res;
/**
* data is just a place holder. It only specifies the starting address.
* The entire value from NDISC_RS_OPTION option will be dumped here.
* So user should take care of the buffer size.
*/
char option;
};
struct ndisc_ra_pkt {
char cur_hop_limit;
/* m(1) + o(1) + res(6) */
char m_o_res;
uint16_t router_lifetime;
uint32_t reachable_time;
uint32_t retrans_timer;
/**
* data is just a place holder. It only specifies the starting address.
* The entire value from NDISC_RA_OPTION option will be dumped here.
* So user should take care of the buffer size.
*/
char option;
};
/**
* @param pkt The packet buffer
* @param len length of the ICMPv6 packet
* @param fp file pointer to the configuration file
*
* @return
* 0 Success
* -1 Error
*
* @Description
* This function reads the ICMPv6 packet, calculates checksum and fills
* the checksum field in the packet.
* [RFC-4443]
* The checksum is the 16-bit one's complement of the one's complement sum of
* the entire ICMPv6 message, starting with the ICMPv6 message type field, and
* prepended with a "pseudo-header" of IPv6 header fields...
*/
int32_t calculate_icmp6_checksum(struct icmp6_hdr *pkt, int32_t len, FILE *fp) {
char option[MAX_OPTION_LEN], value[MAX_VALUE_LEN];
uint32_t checksum = 0;
int32_t init_pos, pos;
char src[16] = {0}, dst[16] = {0};
uint16_t *icmp6_pkt = (uint16_t *)pkt;
int32_t i;
/* store the current position of cursur in the conf file */
init_pos = ftell(fp);
if (init_pos < 0)
goto err;
/* Move cursur to the starting of conf file */
if (fseek(fp, 0, SEEK_SET))
goto err;
/* Find src and dst addresses that immediately above from current pos */
do {
if (pgen_parse_option(fp, option, value))
goto err;
pos = ftell(fp);
if (!strcmp(option, "IPV6_SRC_ADDR")) {
if (ip6_writer(src, value))
goto err;
}
else if (!strcmp(option, "IPV6_DST_ADDR")) {
if (ip6_writer(dst, value))
goto err;
}
} while ((pos != -1) && (pos < init_pos));
if (pos == -1)
goto err;
/* Process ipv6 src and dst addresses for checksum */
for (i = 0; i < 16; i += 2) {
checksum += (((src[i] & 0xff) << 8) | (src[i+1] & 0xff));
checksum += (((dst[i] & 0xff) << 8) | (dst[i+1] & 0xff));
}
/* process length of the icmpv6 message */
checksum += len;
/* Process packet type. Packet type is always icmpv6 */
checksum += 0x3a;
/* Process icmpv6 packet data */
while (len > 1) {
checksum += htons(*icmp6_pkt);
icmp6_pkt++;
len -= 2;
}
if (len > 0)
checksum += ((*(char *)icmp6_pkt) << 8);
/* Make it into 16 bits */
while (checksum >> 16 != 0)
checksum = (checksum & 0xffff) + (checksum >> 16);
/* One's complement */
checksum = ~checksum;
/**
* The checksum is the 16-bit one's complement of the one's complement
* sum of the entire ICMPv6 message starting with the ICMPv6 message
* type field, prepended with a "pseudo-header" of IPv6 header fields
* having only src addr, dst addr and packet type.
* [Ref: RFC-2463 same in RFC-4443]
*/
pkt->checksum = htons((uint16_t)checksum);
/* Restore the file position for furthur processing */
if (fseek(fp, init_pos, SEEK_SET))
goto err;
return 0;
err:
PGEN_INFO("Error while writing ICMPv6 checksum");
return -1;
}
/**
* @param fp file pointer to the configuration file
* @param cp_cur the packet buffer
*
* @return
* 0 Success
* -1 Error
*
* @Description
* Writes ICMPv6 echo6 packet
*/
char* pgen_echo6_writer(FILE *fp, char *cp_cur) {
struct echo6_pkt *pkt = (struct echo6_pkt *)cp_cur;
char option[MAX_OPTION_LEN], value[MAX_VALUE_LEN];
/**
* Three itmes need for an echo6 packet
*
* 1. Identifier
* 2. Sequence number
* 3. Data
*/
int32_t items = 3, tmp, len;
while (items--) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "ECHO6_IDENTIFIER")) {
if (pgen_store_num(&tmp, value))
goto err;
pkt->identifier = htons(tmp);
}
else if (!strcmp(option, "ECHO6_SEQ")) {
if (pgen_store_num(&tmp, value))
goto err;
pkt->seq_num = htons(tmp);
}
else if (!strcmp(option, "ECHO6_DATA")) {
if (!strcmp(value, "NO_DATA"))
len = 0;
else {
len = raw_data_writer(fp, &(pkt->data));
if (len == 0)
goto err;
}
}
}
return cp_cur + len + 4;
err:
PGEN_INFO("Error while writing echo6 packet");
return NULL;
}
/**
* @param fp file pointer to the configuration file
* @param cp_cur the packet buffer
*
* @return
* 0 Success
* -1 Error
*
* @Description
* Writes ICMPv6 Router Advertisement packet
*/
char* pgen_ndisc_ra_writer(FILE *fp, char *cp_cur) {
struct ndisc_ra_pkt *pkt = (struct ndisc_ra_pkt *)cp_cur;
char option[MAX_OPTION_LEN], value[MAX_VALUE_LEN];
/**
* Totally 7 items for packet
* 1. Cur_Hop_Limit - 1B
* 2. M flag - 1b
* 3. O flag - 1b
* 4. Router Lifetime - 2B
* 5. Reachable Time - 4B
* 6. Retrans Time - 4B
* 7. Option - variable length
*----------------------------
*
* And one for program control
* 8. Total number of options
*/
int32_t items = 8, tmp, op_len = 0, op_num;
char *op;
int32_t count, len, ret;
while (items--) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_CUR_HOP_LIMIT")) {
if (pgen_store_num(&tmp, value))
goto err;
pkt->cur_hop_limit = (char)tmp;
}
else if (!strcmp(option, "NDISC_RA_M_FLAG")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
pkt->m_o_res |= 0x80;
}
else if (!strcmp(option, "NDISC_RA_O_FLAG")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
pkt->m_o_res |= 0x40;
}
else if (!strcmp(option, "NDISC_RA_ROUTER_LIFETIME")) {
if (pgen_store_num(&tmp, value))
goto err;
pkt->router_lifetime = htons(tmp);
}
else if (!strcmp(option, "NDISC_RA_REACHABLE_TIME")) {
if (pgen_store_num(&tmp, value))
goto err;
pkt->reachable_time = htonl(tmp);
}
else if (!strcmp(option, "NDISC_RA_RETRANS_TIMER")) {
if (pgen_store_num(&tmp, value))
goto err;
pkt->retrans_timer = htonl(tmp);
}
else if (!strcmp(option, "NDISC_RA_OPTION_NUM")) {
if (pgen_store_num(&tmp, value))
goto err;
op_num = tmp;
}
else if (!strcmp(option, "NDISC_RA_OPTION")) {
op = &pkt->option;
/**
* Only three known options as of now [RFC-4681]
* 1. Source Link Address
* 2. Prefix Information
* 3. MTU
*
* From here the program expects the options to be in order
*/
while (op_num--) {
if (!strcmp(value, "NO_OPTION"))
op_len += 0;
/* Source Link Layer address */
else if (!strcmp(value, "NDISC_RA_SRC_LINK_ADDR")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
/* Option length will be 8 octets unit */
op_len += tmp * 8;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_SRC_LINK_ADDR")) {
if (mac_writer(op, value))
goto err;
op += 6;
}
else
goto err;
}
/* Prefix information */
else if (!strcmp(value, "NDISC_RA_PREFIX_INFO")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
/* option length is in 8 octets uint */
op_len += tmp * 8;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_PREFIX_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_L_FLAG")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
*op |= 0x80;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_A_FLAG")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
*op |= 0x40;
}
else
goto err;
op++;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_PREFIX_VALID_LIFETIME")) {
if (pgen_store_num(&tmp, value))
goto err;
tmp = htonl(tmp);
memcpy(op, &tmp, 4);
op += 4;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_PREFIX_PREFERRED_LIFETIME")) {
if (pgen_store_num(&tmp, value))
goto err;
tmp = htonl(tmp);
memcpy(op, &tmp, 4);
op += 4;
}
else
goto err;
/* 4 Bytes Reserved */
op += 4;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_PREFIX")) {
if (ip6_prefix_writer(op, value))
goto err;
op += 16;
}
else
goto err;
}
/* MTU */
else if (!strcmp(value, "NDISC_RA_MTU")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
/* Option length will be in 8 octets uint */
op_len += tmp * 8;
op++;
}
else
goto err;
/* 2-Bytes Reserverd */
op += 2;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_MTU")) {
if (pgen_store_num(&tmp, value))
goto err;
tmp = htonl(tmp);
memcpy(op, &tmp, 4);
op += 4;
}
else
goto err;
}
/* Recursive DNS Server Option. RFC-6106 */
else if (!strcmp(value, "NDISC_RA_RDNSS")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
/* option len will be in 8 octets unit */
op_len += tmp * 8;
op++;
count = (tmp - 1) / 2;
}
else
goto err;
/* 2-Bytes reserved */
op += 2;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LIFETIME")) {
if (pgen_store_num(&tmp, value))
goto err;
tmp = htonl(tmp);
memcpy(op, &tmp, 4);
op += 4;
}
else
goto err;
while (count--) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_DNS_ADDR")) {
if (ip6_writer(op, value))
goto err;
op += 16;
}
else
goto err;
}
}
/* DNS Search List Option. RFC-6106 */
else if (!strcmp(value, "NDISC_RA_DNSSL")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
/* option length in 8-octet unit */
op_len += tmp * 8;
op++;
}
else
goto err;
/* 2-Bytes reserved */
op += 2;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_LIFETIME")) {
if (pgen_store_num(&tmp, value))
goto err;
tmp = htonl(tmp);
memcpy(op, &tmp, 4);
op += 4;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_NUM")) {
if (pgen_store_num(&count, value))
goto err;
}
else
goto err;
len = 0;
while(count--) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RA_OP_NAME")) {
ret = encode_name(op, value);
if (ret < 0)
goto err;
len += ret;
op += ret;
}
else
goto err;
}
/* pad to 8-octet multiple */
op += (8 - (len % 8));
}
/* RAW */
else if (!strcmp(value, "RAW")) {
tmp = raw_data_writer(fp, op);
if (tmp < 0)
goto err;
op_len += tmp;
op += tmp;
}
/* Unknown option */
else
goto err;
if (op_num && pgen_parse_option(fp, option, value))
goto err;
if (op_num && strcmp(option, "NDISC_RA_OPTION"))
goto err;
}
}
else
goto err;
}
/* length_of_packet(12) + op_len */
return (cp_cur + 12 + op_len);
err:
PGEN_INFO("Error while writing ND-RA packet");
PGEN_PRINT_DATA("Option: %s\tValue: %s\n", option, value);
return NULL;
}
/**
* @param fp file pointer to the configuration file
* @param cp_cur the packet buffer
*
* @return
* 0 Success
* -1 Error
*
* @Description
* Writes ICMPv6 Router Solicitation packet
*/
char* pgen_ndisc_rs_writer(FILE *fp, char *cp_cur) {
struct ndisc_rs_pkt *pkt = (struct ndisc_rs_pkt *)cp_cur;
char option[MAX_OPTION_LEN], value[MAX_VALUE_LEN];
char *op;
/**
* Having 1 item
* 1. Option - variable length
*/
int32_t tmp, op_len = 0, op_num;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RS_OP_NUM")) {
if (pgen_store_num(&tmp, value))
goto err;
op_num = tmp;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
/* Program expects the option to be in order */
if (!strcmp(option, "NDISC_RS_OPTION")) {
while (op_num--) {
if (!strcmp(value, "NO_OPTION"))
op_len += 0;
/* Source Link Layer address and the only known option */
else if (!strcmp(value, "NDISC_RS_SRC_LINK_ADDR")) {
op = (char *)&(pkt->option);
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RS_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RS_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op = (char)tmp;
/* option length will be in 8 octets unit */
op_len += tmp * 8;
op++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_RS_OP_SRC_LINK_ADDR")) {
if (mac_writer(op, value))
goto err;
op += 6;
}
else
goto err;
}
/* RAW */
else if (!strcmp(value, "RAW")) {
tmp = raw_data_writer(fp, op);
if (tmp < 0)
goto err;
op += tmp;
op_len += tmp;
}
/* Unknown option */
else
goto err;
if (op_num) {
if (pgen_parse_option(fp, option, value))
goto err;
if (strcmp(option, "NDISC_RS_OPTION"))
goto err;
}
}
}
else
goto err;
/* len = res(4) + op_len */
return (cp_cur + 4 + op_len);
err:
PGEN_INFO("Error while writing ICMPv6 RS packet");
PGEN_PRINT_DATA("%s\t%s\n", option, value);
return NULL;
}
/**
* @param fp File pointer to the configuration file
* @param cp_cur the packet buffer
*
* @returns
* 0 Success
* -1 Error
*
* @Description
* Writes ICMPv6 Neighbour Solicitation packet
*/
char* pgen_ndisc_ns_writer(FILE *fp, char *cp_cur) {
struct ndisc_ns_pkt *pkt = (struct ndisc_ns_pkt *)cp_cur;
char option[MAX_OPTION_LEN], value[MAX_VALUE_LEN];
char *op_ptr;
/**
* Having only two items.
*
* 1. Target address - 16B
* 2. Option - Variable length
*
* 3. Number of options
*/
int32_t items = 3, tmp, op_len = 0, op_num;
while (items--) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NS_TARGET_ADDR")) {
if (ip6_writer(pkt->target_addr, value))
goto err;
}
else if (!strcmp(option, "NDISC_NS_OP_NUM")) {
if (pgen_store_num(&op_num, value))
goto err;
}
else if (!strcmp(option, "NDISC_NS_OPTION")) {
op_ptr = (char *)&(pkt->option);
/* Program expects the option to be in order */
while (op_num--) {
if (!strcmp(value, "NO_OPTION"))
op_len += 0;
/* This is only known option as of now. [RFC-4861] */
else if (!strcmp(value, "NDISC_NS_SRC_LINK_ADDR")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NS_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op_ptr = (char)tmp;
op_ptr++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NS_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op_ptr = (char)tmp;
op_ptr++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NS_OP_SRC_LINK_ADDR")) {
if (mac_writer(op_ptr, value))
goto err;
op_ptr += 6;
}
else
goto err;
/* len is in 8 octets uint */
op_len += tmp * 8;
}
/* RAW */
else if (!strcmp(value, "RAW")) {
tmp = raw_data_writer(fp, op_ptr);
if (tmp < 0)
goto err;
op_ptr += tmp;
op_len += tmp;
}
/* Unknown option */
else
goto err;
if (op_num) {
if (pgen_parse_option(fp, option, value))
goto err;
if (strcmp(option, "NDISC_NS_OPTION"))
goto err;
}
}
}
else
goto err;
}
/* len = Reserved(4) + Target_addr(16) + op_len */
return (cp_cur + 4 + 16 + op_len);
err:
PGEN_INFO("Error while writing ICMPv6 NS packet");
PGEN_PRINT_DATA("%s\t%s\n", option, value);
return NULL;
}
/**
* @param fp file pointer to the configuration file
* @param cp_cur the packet buffer
*
* @return
* 0 Success
* -1 Error
*
* @Description
* Writes ICMPv6 Neighbour Advertisement packet
*/
char* pgen_ndisc_na_writer(FILE *fp, char *cp_cur) {
struct ndisc_na_pkt *pkt = (struct ndisc_na_pkt *)cp_cur;
char option[MAX_OPTION_LEN], value[MAX_VALUE_LEN];
char *op_ptr;
/**
* Totally 5 options
*
* 1. R Flag
* 2. S Flag
* 3. O Flag
* 4. Target addr
* 5. The option
*
* 6. Number of options
*/
int32_t items = 6, tmp, op_len = 0, op_num;
while (items--) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NA_R")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
pkt->RSO_res |= 0x80000000;
}
else if (!strcmp(option, "NDISC_NA_S")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
pkt->RSO_res |= 0x40000000;
}
else if (!strcmp(option, "NDISC_NA_O")) {
if (pgen_store_num(&tmp, value))
goto err;
if (tmp)
pkt->RSO_res |= 0x20000000;
}
else if (!strcmp(option, "NDISC_NA_TARGET_ADDR")) {
if (ip6_writer(pkt->target_addr, value))
goto err;
}
else if (!strcmp(option, "NDISC_NA_OP_NUM")) {
if (pgen_store_num(&op_num, value))
goto err;
}
else if (!strcmp(option, "NDISC_NA_OPTION")) {
op_ptr = (char *)&(pkt->option);
while (op_num--) {
/* Program expects the options to be in order */
if (!strcmp(value, "NO_OPTION"))
op_len += 0;
/* This is only known option as of now. [RFC-4861] */
else if (!strcmp(value, "NDISC_NA_SRC_LINK_ADDR")) {
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NA_OP_TYPE")) {
if (pgen_store_num(&tmp, value))
goto err;
*op_ptr = (char)tmp;
op_ptr++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NA_OP_LEN")) {
if (pgen_store_num(&tmp, value))
goto err;
*op_ptr = (char)tmp;
op_ptr++;
}
else
goto err;
if (pgen_parse_option(fp, option, value))
goto err;
if (!strcmp(option, "NDISC_NA_OP_TAR_LINK_ADDR")) {
if (mac_writer(op_ptr, value))
goto err;
op_ptr += 6;
}
else
goto err;
/* len in 8 octets unit */
op_len += tmp * 8;
}