summaryrefslogtreecommitdiff
path: root/staging/mops_ip.c
blob: 46102d757def3c9470eecb031c9634f5ce4d51be (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
/*
 * 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
 * 
*/


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


// PURPOSE
// 
//   Determine destination MAC address to provide direct or indirect
//   delivery of IP packets, depending on which is appropriate.
//  
//   Doing this, the caller must provide 
//     1) A pointer to the interface (within the device_list)
//     2) The destination IP address
//     3) A pointer to the destination MAC address 
//
//   If a Class D (multicast) address is given, a proper IEEE multicast MAC
//   address is derived.
//   
// EXAMPLE
//  
//    u_int8_t ip[4], 
//             mac[6];
//   
//    mops_hton4 (mp->ip_src, ip);
//   
//    mops_ip_get_dst_mac(&device_list[0], ip, mac);
//   
// RETURN VALUES
// 
//    0 upon success
//    1 upon error
// 
int mops_ip_get_dst_mac(struct device_struct *dev, u_int8_t *ip, u_int8_t *mac)
{    
	int i;
	u_int8_t dst_net[4];

	if ((dev==NULL)||(ip==NULL)||(mac==NULL)) return 1;

	// Multicast address?
	if ((0xe0 & ip[0]) == 0xe0) {
		mac[0] = 0x01;
		mac[1] = 0x00;
		mac[2] = 0x5e;
		mac[3] = ip[1] & 127;
		mac[4] = ip[2];
		mac[5] = ip[3];
		return 0;
	}
	
	// Is destination network == local network?
	for (i=0; i<4; i++) {
		dst_net[i] = ip[i] & (u_int8_t) dev->mask[i];
	}
	
	if (compare_ip(dst_net, dev->net)==0) { 
		// dst is on local LAN => resolve MAC!
		service_arp(dev->dev, ip, mac);
	} else { // dst is on a remote network => use default gw!
		for (i=0; i<6; i++) mac[i] = dev->mac_gw[i];
	}

	return 0;
}


///////////////////////////////////////////////////////////////////////////////////
//
// PURPOSE
//
//   Accept a DSCP specification as string argument 
//   and configure the IP-ToS field accordingly.
// 
// EXAMPLE STRINGS
// 
//   AF32        .... specify AF codepoint with class 3 and drop probability 2
//   EF          .... specify Expedited Forwarding
//   CS7         .... specify Code Selector 7
//   101110      .... specify the DSCP in binary
//   56          .... specify the DSCP in decimal
//
// RETURN VALUES
// 
//  -1   general bad argument format
//   0   upon success
//   1   Invalid AF format (Format: AFxy, e. g. af31 or AF23)
//   2   Invalid CS format
//   3   Invalid decimal DSCP value
//   
int mops_ip_dscp (struct mops* mp, char *argv)
{
   int i;
   char cs[4], ps[4], str[16];
   u_int8_t c=0, p=0, dscp=0;
   
   if (strlen(argv)==0) return -1;
   strncpy(str,argv,15);
   
   if (strncasecmp(str, "af", 2)==0)    // e.g. 'AF32' or 'af41'
     {
	if (strlen(str)!=4)  return 1; // ERROR: Invalid AF codepoint
	i=sscanf(str, "%*[afAF]%c%c", cs, ps);
	cs[1]=0x00; ps[1]=0x00;
	c=(u_int8_t) str2int(cs);
	p=(u_int8_t) str2int(ps);
	if ((c<1)||(c>4)||(p<1)||(p>3)) return 1;
	// Now create correct ToS-byte representation: This is simple, since if a=3 and b=1
	// we have in binary already a=0000 0011 and b=0000 0001 and with bit-shifting we 
	// get the desired dscp=011 01 000 (the least signfificant three bits are always 0).
	c <<=5;
	p <<=3;
	dscp = c | p;
     }
   else if (strncasecmp(str, "cs", 2)==0)    // e.g. 'CS7' or 'cs4'
     {
	if (strlen(str)!=2)  return 2; // ERROR: Invalid Code Selector
	i=sscanf(str, "%*[afAF]%c", cs);
	cs[1]=0x00;
	c=(u_int8_t) str2int(cs);
	if (c>7) return 2; 
	c <<=5;
	dscp = c; 
     }
   else if (mz_strcmp(str, "ef", 2)==0) // e.g. 'ef' or 'EF'
     {
	dscp = 0xb8;  // = DSCP 46 = 101110 00 or 1011 1000
     }
   else if (mz_strisbinary(str)==6)  // binary, e. g. 101110
     {
	for (i=0; i<6; i++) if (str[i]=='1') dscp |= ( 0x01 << (5-i) ); 
	dscp <<= 2;
     }
   else if (strlen(str)==2) // decimal DSCP value
     {
	if ( !(isdigit(str[0])) || !(isdigit(str[1]))) return 3;
	dscp = (u_int8_t) str2int(str);
	if (dscp>63) return 3;
	dscp <<= 2;
     }
   else return -1;
   
   // TEST: printf("dscp=%02x\n",dscp);
   mp->ip_tos = dscp;
   
   return 0;
}








//
// IP TOS-FIELD FORMAT
//
//      MSB                                       LSB
//       0     1     2     3     4     5     6     7
//    +-----+-----+-----+-----+-----+-----+-----+-----+   Note that the bit numbering is usually from right
//    |                 | Del   Trp   Rel   Cst |     |   to left, but here is the original pic of the RFC
//    |   PRECEDENCE    |          TOS          | MBZ |   1349. Also here, the MSB is left (strangely bit 0)
//    |                 |                       |     |   and the LSB is right (strangely bit 7).
//    +-----+-----+-----+-----+-----+-----+-----+-----+
//      
// ARGUMENTS
//                                               if unused
//   ipp  ... IP Precedence (0..7)                  or -1
//   tos  ... Type of Service (0..15)               or -1
//   mbz  ... if 1 sets MBZ                         or 0
int mops_ip_tos (struct mops* mp, int ipp, int tos, int mbz)
{
   u_int8_t TOS=0;
   
   if (ipp!=-1)
     {
	if (ipp>7) return 1; // Invalid IPP value
	TOS |= (ipp << 5);
     }

   if (tos!=-1)
     {
	if (tos>15) return 2; // Invalid ToS value
	TOS |= (tos << 1);
     }

   if (mbz==1) // not used if mbz is either 0 or -1
     {
	TOS |= 0x01; // set
     }
   
   mp->ip_tos = TOS;
   
   return 0;
}



//
//
// =================== ONLY IP OPTION HANDLING FUNCTION BELOW ================
// 
///////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////
// 
// There are two cases for the format of an option:
// 
//   Case 1:  A single octet of option-type.
//   Case 2:  An option-type octet, an option-length octet, and the
//            actual option-data octets.
// 
//  The option-length octet counts the WHOLE number of bytes of the option
//
//  The option-type consists of: 
//  
//  +--------+--------+--------+--------+--------+--------+--------+--------+
//  | copied |  option class   |         number (identifies option)         |
//  |  flag  |                 |                                            |
//  +--------+-----------------+--------------------------------------------+
//  
// 
// The following Internet options are defined in RFC 791:
//
//        CLASS NUMBER LENGTH DESCRIPTION
//        ----- ------ ------ -----------
//          0     0      -    End of Option list.  This option occupies only
//                            1 octet; it has no length octet.
//          0     1      -    No Operation.  This option occupies only 1
//                            octet; it has no length octet.
//          0     2     11    Security.  Used to carry Security,
//                            Compartmentation, User Group (TCC), and
//                            Handling Restriction Codes compatible with DOD
//                            requirements.
//          0     3     var.  Loose Source Routing.  Used to route the
//                            internet datagram based on information
//                            supplied by the source.
//          0     9     var.  Strict Source Routing.  Used to route the
//                            internet datagram based on information
//                            supplied by the source.
//          0     7     var.  Record Route.  Used to trace the route an
//                            internet datagram takes.
//          0     8      4    Stream ID.  Used to carry the stream
//                            identifier.
//          2     4     var.  Internet Timestamp.
// 
// 
// Possible options and associated number in mp->ip_option_used
// 
//   1 - Security and handling restrictions (for military applications)
//   2 - Record route
//   4 - Timestamp
//   8 - Loose source routing
//  16 - Strict source routing
//            
//

// *** See RFCs 791, 1071, 1108 ***

// Remove all options
int mops_ip_option_remove_all (struct mops* mp)
{
	mp->ip_option_used = 0;
	mp->ip_option_s = 0;
   return 0;
}


// Add no-option
int mops_ip_option_nop (struct mops* mp)
{
   
   return 0;
}

// Add end of option list 
int mops_ip_option_eol (struct mops* mp)
{
   
   return 0;
}



// Add loose source route option
int mops_ip_option_lsr (struct mops* mp)
{
   
   return 0;
}

// Add strict source route option
int mops_ip_option_ssr (struct mops* mp)
{
   
   return 0;
}

// Add record route option
int mops_ip_option_rr (struct mops* mp)
{
   
   return 0;
}

// Add time stamp option
int mops_ip_option_ts (struct mops* mp)
{
   
   return 0;
}



// Add security option.
//
// This option provides a way for hosts to send security, compartmentation, 
// handling restrictions, and TCC (closed user group) parameters.  The format 
// for this option is as follows:
//
//          +--------+--------+---//---+---//---+---//---+---//---+
//          |10000010|00001011|SSS  SSS|CCC  CCC|HHH  HHH|  TCC   |
//          +--------+--------+---//---+---//---+---//---+---//---+
//           Type=130 Length=11
//
//        Security (S field):  16 bits
//
//          Specifies one of 16 levels of security (eight of which are
//          reserved for future use).
//
//            00000000 00000000 - Unclassified
//            11110001 00110101 - Confidential
//            01111000 10011010 - EFTO
//            10111100 01001101 - MMMM
//            01011110 00100110 - PROG
//            10101111 00010011 - Restricted
//            11010111 10001000 - Secret
//            01101011 11000101 - Top Secret
//            00110101 11100010 - (Reserved for future use)
//            10011010 11110001 - (Reserved for future use)
//            01001101 01111000 - (Reserved for future use)
//            00100100 10111101 - (Reserved for future use)
//            00010011 01011110 - (Reserved for future use)
//            10001001 10101111 - (Reserved for future use)
//            11000100 11010110 - (Reserved for future use)
//            11100010 01101011 - (Reserved for future use)
//
//
//        Compartments (C field):  16 bits
//
//          An all zero value is used when the information transmitted is not 
//          compartmented.  Other values for the compartments field may be obtained 
//          from the Defense Intelligence Agency.
//
//        Handling Restrictions (H field):  16 bits
//
//          The values for the control and release markings are alphanumeric digraphs 
//          and are defined in the Defense Intelligence Agency Manual DIAM 65-19, 
//          "Standard Security Markings".
//
//        Transmission Control Code (TCC field):  24 bits
//
//          Provides a means to segregate traffic and define controlled communities 
//          of interest among subscribers. The TCC values are trigraphs, and are available 
//          from HQ DCA Code 530.
//
//        Must be copied on fragmentation.  This option appears at most
//        once in a datagram.

int mops_ip_option_sec (struct mops* mp)
{
   
   return 0;
}


// Add the IP Router Alert Option - a method to efficiently signal
// transit routers to more closely examine the contents of an IP packet.
// See RFC 2113, and FYI also 3175 (RSVP Aggregation), and RFC 5350 
// (new IANA-defined Router Alert Options (RAO)).
// 
// The Router Alert option has the following format:
//
//     +--------+--------+--------+--------+
//     |10010100|00000100|  2 octet value  |
//     +--------+--------+--------+--------+
//
// Type:
//   Copied flag:  1 (all fragments must carry the option)
//   Option class: 0 (control)
//   Option number: 20 (decimal)
//			
// Length: 4
//	
// Value:  A two octet code with the following values:
//   0 - Router shall examine packet
//   1-65535 - Reserved
//	
// RETURN VALUE: 0 upon success
//               1 upon failure
//               
int mops_ip_option_ra (struct mops* mp, int value)
{
	int ptr;
	u_int16_t val;
	
	if ((mp==NULL) || (value>0xffff)) return 1;

	val = (u_int16_t) value;
	
	ptr = mp->ip_option_s; // add option at the end of existing option list (if any)
	mp->ip_option_used=20;  
	
	// create option header
	mp->ip_option[ptr] = 0x94;

	ptr++;
	mp->ip_option[ptr] = 0x04;

	ptr++;
	mops_hton2 (&val, &mp->ip_option[ptr]);
	ptr+=2;
	mp->ip_option_s=4;
   
	return 0;
}