summaryrefslogtreecommitdiff
path: root/staging/layer3.c
blob: 7dabc26db67d1f431659d42813c01f9f08b8b4a3 (plain)
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
/*
 * Mausezahn - A fast versatile traffic generator
 * Copyright (C) 2008-2010 Herbert Haas
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the 
 * Free Software Foundation.
 * 
 * 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/gpl-2.0.html
 * 
*/



// ***************************************************************************
//    This sections contains functions to send various L3-based PDUs such as
//    
//     * IP
//     
//     (ahem, yes this is currently all here...)
//     
// ***************************************************************************

#include "mz.h"
#include "cli.h"

#define MZ_IP_HELP \
   		"| IP type: Send raw IP packets.\n" \
		"|\n" \
		"| Supports L3 mode (automatic L2 creation) or 'L2-L3' mode (MAC addresses must be provided).\n" \
		"| In L3 mode the IP checksum and length cannot be manipulated to wrong values (currently).\n" \
		"| The L2-L3 mode is activated when specifying any MAC addresses on the command line\n" \
		"| (options -a, -b). \n" \
		"|\n" \
		"| The IP addresses can be specified via the -A and -B options, which identify the source\n" \
		"| and destination addresses, respectively. A dotted decimal notation, an IP range, or a\n" \
		"| FQDN can be used. The source address can also be random (-A rand).\n" \
		"|\n" \
		"| ARGUMENT SYNTAX:  [<comma separated parameter list>]\n" \
		"|\n" \
		"| Parameters:\n" \
		"|\n" \
		"|  len      0-65535        Only accessible in L2 mode\n" \
		"|  sum      0-65535        Only accessible in L2 mode (0 means auto-calculation)\n" \
		"|  tos      00-ff          Full 8-bit control via hex input (use this also for ECN bits).\n" \
		"|  dscp     0-63           Allows easier specification of DSCP (PHB and Drop Propability)\n" \
		"|  ttl      0-255\n" \
		"|  proto    0-255\n" \
		"|  frag     0-65535        Includes flags (MSB) and offset (LSB)\n" \
     		"|  df                      Sets the \"Don't Fragment\" flag\n" \
		"|  mf                      Sets the \"More Fragments\" flag\n" \
		"|  rf                      Sets the reserved flag.\n" \
		"|  id       0-65535\n" \
		"|  loose    <addresses>    Loose Source Route (LSR) option; specify a sequence of hops\n" \
		"|                          using the notation: 1.1.1.1+2.2.2.2+3.3.3.3+...\n" \
		"|  strict   <addresses>    Strict Source Route (SSR) option; same address notation as above\n" \
		"|  option   <hex_string>   Specify any IP option using a hexadecimal string (aa:bb:cc:...)\n" \
		"|\n" \
		"| Additionally the Ethertype can be specified:\n" \
		"|\n" \
		"|  ether_type 00:00-ff:ff  Only accessible in L2 mode (default = 08:00 = IPv4)\n" \
		"|  \n"


#define MZ_IP6_HELP \
   		"| IP type: Send raw IPv6 packets.\n" \
		"|\n" \
		"| Supports L3 mode (automatic L2 creation) or 'L2-L3' mode (MAC addresses must be provided).\n" \
		"| In L3 mode the IP checksum and length cannot be manipulated to wrong values (currently).\n" \
		"| The L2-L3 mode is activated when specifying any MAC addresses on the command line\n" \
		"| (options -a, -b). \n" \
		"|\n" \
		"| ARGUMENT SYNTAX:  [<comma separated parameter list>]\n" \
		"|\n" \
		"| Parameters:\n" \
		"|\n" \
		"|  len      0-65535        Only accessible in L2 mode\n" \
		"|  sum      0-65535        Only accessible in L2 mode (0 means auto-calculation)\n" \
		"|  tos      00-ff          Full 8-bit control via hex input (use this also for ECN bits).\n" \
		"|  dscp     0-63           Allows easier specification of DSCP (PHB and Drop Propability)\n" \
		"|  flow     0-1048575      Flow label\n" \
		"|  hop      0-255          Hop limit\n" \
		"|  next     0-255          Next protocol or header type\n" \
		"|  frag     0-65535        Includes flags (MSB) and offset (LSB)\n" \
		"|  mf                      Sets the \"More Fragments\" flag\n" \
		"|  frag_res1               Sets the reserved flag 1.\n" \
		"|  frag_res2               Sets the reserved flag 2.\n" \
		"|  id       0-65535	    Fragment ID\n" \
		"|  loose    <addresses>    Source Routing Header\n" \
		"|  rtype    0,2            Source Routing Type: 0 (Deprecated in RFC 5095) or 2 for Mobile IP\n" \
		"|  segments 0-255          Number of route segments left, used by RH0\n" \
		"|\n" \
		"| Additionally the Ethertype can be specified:\n" \
		"|\n" \
		"|  ether_type 00:00-ff:ff  Only accessible in L2 mode (default = 86:dd = IPv6)\n" \
		"|  \n"


// Only used to simplify initialization of libnet
// Return pointer to context
libnet_t* get_link_context(void)
{
   libnet_t * l;
   char errbuf[LIBNET_ERRBUF_SIZE];

   // Don't open context if only a help text is requested
   if  (getarg(tx.arg_string,"help", NULL)==1)
     {
	return NULL;
     }
   
   
   if (tx.packet_mode)  
     {  // Let libnet create an appropriate Ethernet frame
	if (ipv6_mode)
	  l = libnet_init (LIBNET_RAW6_ADV, tx.device, errbuf);
	else
	  l = libnet_init (LIBNET_RAW4_ADV, tx.device, errbuf);
     }
   else // User specified Ethernet header details (src or dst)
     {
	l = libnet_init (LIBNET_LINK_ADV, tx.device, errbuf);
     }
   
   if (l == NULL)
     {
	fprintf(stderr, "%s", errbuf);
	exit(EXIT_FAILURE);
     }

   if (tx.prio != 0 &&
       setsockopt (libnet_getfd (l), SOL_SOCKET, SO_PRIORITY, &tx.prio,
		   sizeof tx.prio) < 0)
     {
       perror("setsockopt SO_PRIORITY");
       exit(EXIT_FAILURE);
     }

   return l;
}


//////////////////////////////////////////////////////////////////////////////
// Prepare IP packet
libnet_ptag_t  create_ip_packet (libnet_t *l)
{
   libnet_ptag_t  t; 
   char argval[MAX_PAYLOAD_SIZE];
   int i, T; // only an abbreviation for tx.packet_mode 

   if (ipv6_mode)
     return create_ip6_packet(l);

   // Default IP header fields
   tx.ip_len   = LIBNET_IPV4_H;  // Don't forget to add payload length
   tx.ip_id    = 0;
   tx.ip_frag  = 0;              // Flags and Offset !!!
   tx.ip_sum   = 0;              // default: automatically calculate checksum
   tx.ip_tos   = 0;

   // temporary variables
   unsigned int dummy;
   size_t len;
   char *s;

   T = tx.packet_mode; // >0 means automatic L2 creation

   if ( (getarg(tx.arg_string,"help", NULL)==1) && (mode==IP) )
     {
	if (mz_port)
	  {
	     cli_print(gcli, "%s", MZ_IP_HELP);
	     return -1;
	  }
	else
	  {
	     
	     fprintf(stderr,"\n" 
		     MAUSEZAHN_VERSION
		     "\n%s", MZ_IP_HELP);

	     exit(0);
	  }
     }

   // Check if hex_payload already specified (externally)
   if (tx.hex_payload_s)
     {
	memcpy( (void*) tx.ip_payload, (void*) tx.hex_payload, tx.hex_payload_s);
	tx.ip_payload_s = tx.hex_payload_s;
     }

   
   // Evaluate CLI parameters:

   if ( (getarg(tx.arg_string,"payload", argval)==1) || (getarg(tx.arg_string,"p", argval)==1))
     {
        if (mode==IP)
	  tx.ip_payload_s = str2hex (argval, tx.ip_payload, MAX_PAYLOAD_SIZE);
     }
   // else payload has been specified as ASCII text via -P option
   
   
   // NOTE: If 'mode' is NOT IP (e. g. UDP or TCP or something else)
   // then the argument 'len' and 'sum' is NOT meant for the IP header!
   // Instead the user can use 'iplen' and 'ipsum'.
   if (mode==IP)
     {
	if (getarg(tx.arg_string,"len", argval)==1)
	  {
	     if (T) fprintf(stderr, " IP_Warning: 'len' cannot be set in this mode.\n");
	     tx.ip_len = (u_int16_t) str2int(argval);
	  }
	else
	  {
	     tx.ip_len = LIBNET_IPV4_H + tx.ip_payload_s;
	  }
	
	if (getarg(tx.arg_string,"sum", argval)==1)
	  {
	     if (T) fprintf(stderr, " IP_Warning: 'sum' cannot be set in this mode.\n");
	     tx.ip_sum = (u_int16_t) str2int(argval);
	  }
     }
   else // mode is NOT IP
     {
	if (getarg(tx.arg_string,"iplen", argval)==1)
	  {
	     if (T) fprintf(stderr, " IP_Warning: 'len' cannot be set in this mode.\n");
	     tx.ip_len = (u_int16_t) str2int(argval);
	  }
	else
	  {
	     tx.ip_len = LIBNET_IPV4_H + tx.ip_payload_s;
	  }

	if (getarg(tx.arg_string,"ipsum", argval)==1)
	  {
	     if (T) fprintf(stderr, " IP_Warning: 'sum' cannot be set in this mode.\n");
	     tx.ip_sum = (u_int16_t) str2int(argval);
	  }
     }
   
   
   if (getarg(tx.arg_string,"tos", argval)==1)
     {
	tx.ip_tos = (u_int8_t) strtol(argval,NULL,16);
	dummy = (unsigned int) strtol(argval,NULL,16);
	if (dummy > 255) fprintf(stderr, " IP_Warning: 'tos' too big, adjusted to LSBs\n");
     }
   
   if (getarg(tx.arg_string,"dscp", argval)==1)
     {
	dummy = (unsigned int) str2int(argval);
	if (dummy > 63) 
	  { 
	     fprintf(stderr, " IP_Warning: 'dscp' too big, adjusted to 63\n");
	     dummy = 63;
	  }
	tx.ip_tos = (u_int8_t) dummy*4;
     }
   
   if (getarg(tx.arg_string,"id", argval)==1)
     {
	tx.ip_id = (u_int16_t) str2int(argval);
     }

   if (getarg(tx.arg_string,"frag", argval)==1)
     {
	tx.ip_frag = (u_int16_t) str2int(argval);
     }
   
   if (getarg(tx.arg_string,"df", NULL)==1)
     {
	tx.ip_frag |= 0x4000; 
     }
   
   if (getarg(tx.arg_string,"mf", NULL)==1)
     {
	tx.ip_frag |= 0x2000; 
     }
   
   if (getarg(tx.arg_string,"rf", NULL)==1)
     {
	tx.ip_frag |= 0x8000; 
     }

   if (getarg(tx.arg_string, "ttl", argval) == 1)
	tx.ip_ttl = (u_int8_t)str2int(argval);
   else if (tx.ip_ttl == 0)
	tx.ip_ttl = 255;

   if (getarg(tx.arg_string,"proto", argval)==1)
     {
	tx.ip_proto = (u_int8_t) str2int(argval);
     }


   if ((tx.ascii)&&(mode==IP)) // ASCII PAYLOAD overrides hex payload
     {
	strncpy((char *)tx.ip_payload, (char *)tx.ascii_payload, MAX_PAYLOAD_SIZE);
	tx.ip_payload_s = strlen((char *)tx.ascii_payload);
	tx.ip_len += tx.ip_payload_s;
     }

   
   /////////
   // Want some padding? The specified number of padding bytes are ADDED to the 
   // payload. Note that this is only evaluated if we are in IP mode because
   // UDP and TCP already might have been padded and set the ip_payload_s.
   // (Note the difference in send_eth() where you specified the total number
   // of bytes in the frame)
   // 
   if ((tx.padding)&&(mode==IP))
     {
	for (i=0; i<tx.padding; i++)
	  {
	     tx.ip_payload[tx.ip_payload_s+i] = 0x42; // pad with THE ANSWER (why random?)
	  }
	tx.ip_payload_s += tx.padding;
	tx.ip_len += tx.padding;
     }

   

     
   
   // Loose and Strict Source Route 
   // See RFC 791 for most the detailed description
   // 
   if ( (getarg(tx.arg_string,"loose", argval)==1) ||
	(getarg(tx.arg_string,"strict", argval)==1) )
     {
	len = strlen(argval);
	
	if (len<7) // not even a single dotted decimal IP address given!
	  {
	     fprintf(stderr, " IP_Warning: Source route option requires at least one IP address!\n");
	     // But we allow this :-)
	  }

	
	// determine how many IP addresses have been specified
	dummy=0;
	for (i=0; i<len; i++)
	  {
	     if (ispunct(*(argval+i))) dummy++ ;
	  }
	dummy = (dummy+1) / 4; // the number of IP addresses
	
	// Specify: type code, length, pointer
	if (getarg(tx.arg_string,"loose", argval)==1)
	  {
	     tx.ip_option[0] = 131; // loose source route
	  }
	else
	  {
	     tx.ip_option[0] = 137; // strict source route
	  }
	tx.ip_option[1] = 3+(dummy*4); // length
	tx.ip_option[2] = 4; // Use first IP address as next hop
	//tx.ip_option[2] = 4+4*dummy;   // smallest pointer, points to first address, which is
	                               // the 4th byte within this option
	
	tx.ip_option_s = 3;
	s = strtok(argval, ".+-:;/>");
	do
	  {
	     len--;
	     tx.ip_option[tx.ip_option_s] = (u_int8_t) str2int(s);
	     tx.ip_option_s++;
	  } while ( (s=strtok(NULL, ".+-:;/>")) != NULL );

	tx.ip_option_s++; // EOL
	
	// add empty space for record route: //// NONSENSE? /////
	/*
	 for (i=0; i<(4*dummy); i++)
	 {
	 tx.ip_option[tx.ip_option_s] = 0x00;
	 tx.ip_option_s++;
	 }
	 */
     }
   
   
   
   // Allow any IP option specified as hex string
   // An option can be a single byte or consist of multiple bytes in which case
   // a length field is needed, see RFC 791.
   if (getarg(tx.arg_string,"option", argval)==1)
     {
	// check if conflicting with argument "loose" or "strict"
	if (tx.ip_option_s)
	  {
	     fprintf(stderr, " IP_Error: Another IP option already specified. Please check your arguments.\n");
	     exit(1);
	  }
	
	tx.ip_option_s = str2hex (argval, tx.ip_option, 1023);
     }
   
   
   
   if (tx.ip_option_s)
     {
	t = libnet_build_ipv4_options (tx.ip_option,
				       tx.ip_option_s, 
				       l,
				       0);
	tx.ip_len += tx.ip_option_s;
     }
   
   
   ///////
   // Did the user specify ANY payload? We require at least one byte!
   /*
   if (!tx.ip_payload_s)
     {
	tx.ip_payload[0] = 0x42;
	tx.ip_payload_s = 1;
     }
   */
   
   t = libnet_build_ipv4 (tx.ip_len,
			  tx.ip_tos, 
			  tx.ip_id, 
			  tx.ip_frag,
			  tx.ip_ttl, 
			  tx.ip_proto,
			  tx.ip_sum, 
			  tx.ip_src, // init.c defaults this to own SA
			  tx.ip_dst, // init.c defaults this to 255.255.255.255
			  (mode==IP) ? (tx.ip_payload_s) ? tx.ip_payload : NULL : NULL,  // if e.g. mode=UDP ignore payload argument
			  (mode==IP) ? tx.ip_payload_s : 0,
			  
			  /*
			  (mode==IP) ? tx.ip_payload : NULL,  // if e.g. mode=UDP ignore payload argument
			  (mode==IP) ? tx.ip_payload_s : 0,
			   */
			  l, 
			  0);


   if (t == -1)
     {
	fprintf(stderr, " mz/create_ip_packet: Can't build IP header: %s\n", libnet_geterror(l));
	exit (0);
     }

   
   return t;
   
}

//////////////////////////////////////////////////////////////////////////////
// Prepare IPv6 packet
libnet_ptag_t  create_ip6_packet (libnet_t *l)
{
   libnet_ptag_t  t;
   char argval[MAX_PAYLOAD_SIZE];
   int i, T; // only an abbreviation for tx.packet_mode

   // Default IP header fields
   tx.ip_len   = 0;
   tx.ip_id    = 0;
   tx.ip6_segs = 0;
   tx.ip6_rtype = 0;
   tx.ip6_id   = 0;
   tx.ip_frag  = 0;              // Flags and Offset !!!
   tx.ip_tos   = 0;
   tx.ip_ttl   = 255;

   // temporary variables
   unsigned int dummy;
   size_t len;
   char *s;

   T = tx.packet_mode; // >0 means automatic L2 creation

   if ( (getarg(tx.arg_string,"help", NULL)==1) && (mode==IP) )
     {
	if (mz_port)
	  {
	     cli_print(gcli, "%s", MZ_IP6_HELP);
	     return -1;
	  }
	else
	  {
	     fprintf(stderr,"\n"
		     MAUSEZAHN_VERSION
		     "\n%s", MZ_IP6_HELP);

	     exit(0);
	  }
     }

   // Check if hex_payload already specified (externally)
   if (tx.hex_payload_s)
     {
	memcpy( (void*) tx.ip_payload, (void*) tx.hex_payload, tx.hex_payload_s);
	tx.ip_payload_s = tx.hex_payload_s;
     }

   // Evaluate CLI parameters:
   if ( (getarg(tx.arg_string,"payload", argval)==1) || (getarg(tx.arg_string,"p", argval)==1))
     {
        if (mode==IP)
	  tx.ip_payload_s = str2hex (argval, tx.ip_payload, MAX_PAYLOAD_SIZE);
     }
   // else payload has been specified as ASCII text via -P option

   // NOTE: If 'mode' is NOT IP (e. g. UDP or TCP or something else)
   // then the argument 'len' and 'sum' is NOT meant for the IP header!
   // Instead the user can use 'iplen' and 'ipsum'.
   if (mode==IP)
     {
	if (getarg(tx.arg_string,"len", argval)==1)
	  {
	     if (T) fprintf(stderr, " IP_Warning: 'len' cannot be set in this mode.\n");
	     tx.ip_len = (u_int16_t) str2int(argval);
	  }
	else
	  {
	     tx.ip_len += tx.ip_payload_s;
	  }
     }
   else // mode is NOT IP
     {
	if (getarg(tx.arg_string,"iplen", argval)==1)
	  {
	     if (T) fprintf(stderr, " IP_Warning: 'len' cannot be set in this mode.\n");
	     tx.ip_len = (u_int16_t) str2int(argval);
	  }
	else
	  {
	     tx.ip_len += tx.ip_payload_s;
	  }
     }


   if (getarg(tx.arg_string,"tos", argval)==1)
     {
	tx.ip_tos = (u_int8_t) strtol(argval,NULL,16);
	dummy = (unsigned int) strtol(argval,NULL,16);
	if (dummy > 255) fprintf(stderr, " IP_Warning: 'tos' too big, adjusted to LSBs\n");
     }

   if (getarg(tx.arg_string,"flow", argval)==1)
     {
	dummy = (unsigned int) strtol(argval,NULL,16);
	if (dummy > 1048575)
	  {
	    fprintf(stderr, " IP_Warning: 'flow label' too big, adjusted to 0xfffff\n");
	    dummy = 0xfffff;
	  }
        tx.ip_flow = dummy;
     }

   if (getarg(tx.arg_string,"dscp", argval)==1)
     {
	dummy = (unsigned int) str2int(argval);
	if (dummy > 63)
	  {
	     fprintf(stderr, " IP_Warning: 'dscp' too big, adjusted to 63\n");
	     dummy = 63;
	  }
	tx.ip_tos = (u_int8_t) dummy*4;
     }

   if (getarg(tx.arg_string,"id", argval)==1)
     {
	  tx.ip6_id = str2int(argval);
     }

   if (getarg(tx.arg_string,"frag", argval)==1)
     {
          tx.ip_frag = ((u_int16_t) str2int(argval)) << 3;
     }

   if (getarg(tx.arg_string,"mf", NULL)==1)
     {
	  tx.ip_frag |= 0x0001;
     }

   if (getarg(tx.arg_string,"frag_res1", NULL)==1)
     {
          tx.ip_frag |= 0x0002;
     }

   if (getarg(tx.arg_string,"frag_res2", NULL)==1)
     {
          tx.ip_frag |= 0x0004;
     }

   if (getarg(tx.arg_string,"hop", argval)==1)
     {
	tx.ip_ttl = (u_int8_t) str2int(argval);
     }

   if (getarg(tx.arg_string,"next", argval)==1)
     {
	tx.ip_proto = (u_int8_t) str2int(argval);
     }
   else if (mode==IP)
     {
	tx.ip_proto = 59; // No Next Header for IPv6
     }


   if ((tx.ascii)&&(mode==IP)) // ASCII PAYLOAD overrides hex payload
     {
	strncpy((char *)tx.ip_payload, (char *)tx.ascii_payload, MAX_PAYLOAD_SIZE);
	tx.ip_payload_s = strlen((char *)tx.ascii_payload);
	tx.ip_len += tx.ip_payload_s;
     }


   /////////
   // Want some padding? The specified number of padding bytes are ADDED to the
   // payload. Note that this is only evaluated if we are in IP mode because
   // UDP and TCP already might have been padded and set the ip_payload_s.
   // (Note the difference in send_eth() where you specified the total number
   // of bytes in the frame)
   //
   if ((tx.padding)&&(mode==IP))
     {
	for (i=0; i<tx.padding; i++)
	  {
	     tx.ip_payload[tx.ip_payload_s+i] = 0x42; // pad with THE ANSWER (why random?)
	  }
	tx.ip_payload_s += tx.padding;
	tx.ip_len += tx.padding;
     }

   if (tx.ip6_id) {
     t = libnet_build_ipv6_frag (tx.ip_proto,
				 0,
				 htons(tx.ip_frag),
				 htonl(tx.ip6_id),
				 (mode==IP) ? (tx.ip_payload_s) ? tx.ip_payload : NULL : NULL,
				 (mode==IP) ? tx.ip_payload_s : 0,
				 l,
				 0);
     tx.ip_len += LIBNET_IPV6_FRAG_H;
     tx.ip_payload_s = 0;
     tx.ip_proto = LIBNET_IPV6_NH_FRAGMENT;
   }

   // See RFC 2460 Routing Header
   //
   if ( (getarg(tx.arg_string,"segments", argval)==1) )
     {
        dummy = (unsigned int) str2int(argval);
        if (dummy > 255) {
          fprintf(stderr, " IP_Error: Maximal Routing Segments are 255!\n");
          exit(1);
        }
        tx.ip6_segs = dummy;
     }

   if ( (getarg(tx.arg_string,"rtype", argval)==1) )
     {
	dummy = (unsigned int) str2int(argval);
	if (dummy > 255) {
	  fprintf(stderr, " IP_Error: Maximum Routing Type is 255!\n");
	  exit(1);
	}
	tx.ip6_segs = dummy;
     }

   if ( (getarg(tx.arg_string,"loose", argval)==1) )
     {
	// Fill reserved
	memset(tx.ip_option, 0, 4);
	tx.ip_option_s=4;

	len = strlen(argval);
	s = strtok(argval, ".+-;/>");
	do
	  {
	     len--;
	     *((struct libnet_in6_addr *) &tx.ip_option[tx.ip_option_s]) = libnet_name2addr6 (l, s, LIBNET_DONT_RESOLVE);
	     tx.ip_option_s += 16;
	  } while ( (s=strtok(NULL, ".+-;/>")) != NULL );

	if (!tx.ip_option_s) {
	  fprintf(stderr, " IP_Error: No Routing Hops found!\n");
	  exit(1);
	}

	if (mode==IP && tx.ip_payload_s)
	  memmove(tx.ip_payload+tx.ip_option_s, tx.ip_payload, tx.ip_payload_s);
	else
	  tx.ip_payload_s = 0;

        memcpy(tx.ip_payload, tx.ip_option, tx.ip_option_s);
        tx.ip_payload_s += tx.ip_option_s;

	t = libnet_build_ipv6_routing(tx.ip_proto,
				      (tx.ip_option_s -4) / 8,
				      tx.ip6_rtype,
				      tx.ip6_segs,
				      tx.ip_payload,
				      tx.ip_payload_s,
				      l,
				      0);
	tx.ip_len += LIBNET_IPV6_ROUTING_H + tx.ip_option_s;
	tx.ip_payload_s = 0;
	tx.ip_proto = LIBNET_IPV6_NH_ROUTING;
     }

   t = libnet_build_ipv6 (tx.ip_tos,
			  tx.ip_flow,
			  tx.ip_len,
			  tx.ip_proto,
			  tx.ip_ttl,
			  tx.ip6_src,
			  tx.ip6_dst,
			  (mode==IP) ? (tx.ip_payload_s) ? tx.ip_payload : NULL : NULL,
			  (mode==IP) ? tx.ip_payload_s : 0,
			  l,
			  0);

   if (t == -1)
     {
	fprintf(stderr, " mz/create_ip_packet: Can't build IPv6 header: %s\n", libnet_geterror(l));
	exit (0);
     }

   return t;
}