File: gre.c

package info (click to toggle)
libnet 1.1.6+dfsg-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 4,208 kB
  • ctags: 3,569
  • sloc: ansic: 18,416; sh: 10,754; makefile: 158
file content (410 lines) | stat: -rw-r--r-- 14,785 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  libnet 1.1
 *  Build a GRE packet
 *  To view: tcpdump -s 0 -n -X -vvv proto gre
 *
 *  Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
 *  All rights reserved.
 *
 *
 *  KNOWN BUG
 *    the encapsulated headers have wrong checksums. I guess this is due to
 *    the one pass libnet_pblock_coalesce() which is really to complicated :(
 *
 *
 *  Default packet:
 *   # ./gre -d 1.2.3.4                
 *   libnet 1.1 packet shaping: GRE 1701 [link]
 *   Wrote 78 byte GRE packet; check the wire.
 *
 *   18:58:12.112157 192.168.1.2 > 1.2.3.4: gre 198.35.123.50.1234 > 103.69.139.107.53: S [bad tcp cksum 698a!] 16843009:16843009(0) win 32767 (ttl 64, id 242, len 40, bad cksum 0!) (ttl 255, id 255, len 64)
 *   0x0000   4500 0040 00ff 0000 ff2f f4df c0a8 0102        E..@...../......
 *   0x0010   0102 0304 0000 0800 4500 0028 00f2 0000        ........E..(....
 *   0x0020   4006 0000 c623 7b32 6745 8b6b 04d2 0035        @....#{2gE.k...5
 *   0x0030   0101 0101 0202 0202 5002 7fff 6666 0000        ........P...ff..
 *
 *  Packet with a computed checksum 
 *   # ./gre -d 1.2.3.4 -c 0
 *   libnet 1.1 packet shaping: GRE 1701 [link]
 *   Wrote 82 byte GRE packet; check the wire.
 *   
 *   18:58:22.587513 192.168.1.2 > 1.2.3.4: gre [Cv0] C:7c62 198.35.123.50.1234 > 103.69.139.107.53: S [bad tcp cksum 698a!] 16843009:16843009(0) win 32767 (ttl 64, id 242, len 40, bad cksum 0!) (ttl 255, id 255, len 68)
 *   0x0000   4500 0044 00ff 0000 ff2f f4db c0a8 0102        E..D...../......
 *   0x0010   0102 0304 8000 0800 7c62 0000 4500 0028        ........|b..E..(
 *   0x0020   00f2 0000 4006 0000 c623 7b32 6745 8b6b        ....@....#{2gE.k
 *   0x0030   04d2 0035 0101 0101 0202 0202 5002 7fff        ...5........P...
 *   0x0040   6666 0000                                      ff..
 *
 *
 *  Packet with a forced checksum 
 *   # ./gre -d 1.2.3.4 -c 6666
 *   libnet 1.1 packet shaping: GRE 1701 [link]
 *   Wrote 68 byte GRE packet; check the wire.
 *   
 *   19:04:12.108080 192.168.1.2 > 1.2.3.4: gre [Cv0] C:1a0a 198.35.123.50.1234 > 103.69.139.107.53: S [bad tcp cksum 698a!] 16843009:16843009(0) win 32767 (ttl 64, id 242, len 40, bad cksum 0!) (ttl 255, id 255, len 68)
 *   0x0000   4500 0044 00ff 0000 ff2f f4db c0a8 0102        E..D...../......
 *   0x0010   0102 0304 8000 0800 1a0a 0000 4500 0028        ............E..(
 *   0x0020   00f2 0000 4006 0000 c623 7b32 6745 8b6b        ....@....#{2gE.k
 *   0x0030   04d2 0035 0101 0101 0202 0202 5002 7fff        ...5........P...
 *   0x0040   6666 0000                                      ff..
 *
 *
 * 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "./libnet_test.h"


void
usage(char *prog)
{
    fprintf(stderr, "Usage: %s\n", prog);
    fprintf(stderr, "\t IP options:  -d <dst ip> [-s src ip]\n");
    fprintf(stderr, "\t GRE options: [-v] set RFC 2637 mode (PPP in GRE) (default is RFC 1701 for IP in GRE)\n");
    fprintf(stderr, "\t\t RFC 1701 options (IP in GRE):\n");
    fprintf(stderr, "\t\t   [-c sum]  [-r routing] [-k key] [-n seqnum]\n");
    fprintf(stderr, "\t\t   IP in GRE options: [-S src ip]  [-D dst ip]\n");
    fprintf(stderr, "\t\t RFC 2637 options (PPP in GRE):\n");
    fprintf(stderr, "\t\t   [-a ack]\n");
    
    exit(1);
}

/*
 *                ---------------------------------
 *                |       Delivery Header         |
 *                ---------------------------------
 *                |       GRE Header              |
 *               ---------------------------------
 *                |       Payload packet          |
 *                ---------------------------------
 */
int
main(int argc, char *argv[])
{
    char c;
    libnet_t *l;
    char errbuf[LIBNET_ERRBUF_SIZE];
    u_long src_ip = 0, dst_ip = 0, gre_src_ip = 0, gre_dst_ip = 0;
    u_short checksum = 0, offset = 0;
    u_char *routing = NULL;
    u_long key = 0, seq = 0;
    u_short gre_flags = 0;
    u_long len;
    u_long size = 0;
    libnet_ptag_t t;

    printf("libnet 1.1 packet shaping: GRE [link]\n");
    
    /*
     *  Initialize the library.  Root priviledges are required.
     */
    l = libnet_init(
            LIBNET_LINK,                            /* injection type */
            NULL,                                   /* network interface */
            errbuf);                                /* error buffer */
  
    if (!l)
    {
        fprintf(stderr, "libnet_init: %s", errbuf);
        exit(EXIT_FAILURE);
    }

    /*
     * parse options
     */
    while ((c = getopt(argc, argv, "d:s:D:S:c:r:k:n:va:")) != EOF)
    {
        switch (c)
        {
	    
            case 'd':
                if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                {
                    fprintf(stderr, "Bad destination IP address: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 's':
                if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                {
                    fprintf(stderr, "Bad source IP address: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'D':
                if ((gre_dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                {
                    fprintf(stderr, "Bad destination IP address (GRE): %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'S':
                if ((gre_src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                {
                    fprintf(stderr, "Bad source IP address (GRE): %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
	    case 'c':
		checksum = atoi(optarg);
		gre_flags|=GRE_CSUM;
                break;
	    case 'r':
		routing = (u_char *)optarg;
		gre_flags|=GRE_ROUTING;
                break;
	    case 'k':
		key = atoi(optarg);
		gre_flags|=GRE_KEY;
                break;
	    case 'n':
		seq = atoi(optarg);
		gre_flags|=GRE_SEQ;
                break;
	    case 'v':
		gre_flags|=(GRE_VERSION_1|GRE_KEY);
		break;
	    case 'a':
		if (! (gre_flags & GRE_VERSION_1))
		    usage(argv[0]);
		seq = atoi(optarg);    /* seq in v0 is ack in v1 */
		gre_flags|=GRE_ACK;
		break;
            default:
                exit(EXIT_FAILURE);
        }
    }

    /*
     * check options 
     */
    if (!dst_ip)
    {
	usage(argv[0]);
    }

    if (!src_ip)
    {
	src_ip = libnet_get_ipaddr4(l);
    }

    if (!gre_dst_ip)
    {
	gre_dst_ip = libnet_get_prand(LIBNET_PRu32);
    }

    if (!gre_src_ip)
    {
	gre_src_ip = libnet_get_prand(LIBNET_PRu32);
    }


    if ( (gre_flags & GRE_VERSION_MASK) == 0)
    {
	/*
	 * Build a TCP/IP packet embedded in GRE message
	 */
	size = LIBNET_TCP_H;
	t = libnet_build_tcp(
	    1234,                                       /* source port */
	    53,                                         /* destination port */
	    0x01010101,                                 /* sequence number */
	    0x02020202,                                 /* acknowledgement num */
	    TH_SYN,                                     /* control flags */
	    32767,                                      /* window size */
	    0,                                          /* checksum */
	    0,                                          /* urgent pointer */
	    size,                                       /* TCP packet size */
	    NULL,                                       /* payload */
	    0,                                          /* payload size */
	    l,                                          /* libnet handle */
	    0);                                         /* libnet id */
	if (t == -1)
	{
	    fprintf(stderr, "Can't build TCP header (GRE): %s\n", libnet_geterror(l));
	    goto bad;
	}
	
	size += LIBNET_IPV4_H;
	t = libnet_build_ipv4(
	    size,                                       /* length */
	    0,                                          /* TOS */
	    242,                                        /* IP ID */
	    0,                                          /* IP Frag */
	    64,                                         /* TTL */
	    IPPROTO_TCP,                                /* protocol */
	    0,                                          /* checksum */
	    gre_src_ip,                                 /* source IP */
	    gre_dst_ip,                                 /* destination IP */
	    NULL,                                       /* payload */
	    0,                                          /* payload size */
	    l,                                          /* libnet handle */
	    0);                                         /* libnet id */
	if (t == -1)
	{
	    fprintf(stderr, "Can't build IP header (GRE): %s\n", libnet_geterror(l));
	    goto bad;
	} 
    }

    if ( (gre_flags & GRE_VERSION_MASK) == 1)
    {
	offset = libnet_get_prand(LIBNET_PRu16);
	if (~gre_flags & GRE_ACK)
	{
	    u_char ppp[4] = "\x00\x01"; /* PPP padding */
	    checksum = 2; /* checksum is in fact payload_s in PPP/GRE (v1) */
	    size = 2;
	    gre_flags|=GRE_SEQ;
	    key = libnet_get_prand(LIBNET_PRu32);

	    /*
	     * Build a PPP packet embedded in GRE message
	     */
	    t = libnet_build_data(
		ppp,
		checksum,
		l, 
		0
	    );
	    if (t == -1)
	    {
		fprintf(stderr, "Can't build PPP header (GRE): %s\n", libnet_geterror(l));
		goto bad;
	    }
	}
	gre_flags&=~(GRE_CSUM|GRE_ROUTING);
    }

    /*
     * Build the GRE message
     */
    if (gre_flags & GRE_ROUTING)
    {
	/* as packet are stacked, start by the last one, ie null sre */
	size += LIBNET_GRE_SRE_H;
	t = libnet_build_gre_last_sre(l, 0);
	if (t == -1)
	{
	    fprintf(stderr, "Can't build GRE last SRE header: %s\n", libnet_geterror(l));
	    goto bad;
	}
	size += LIBNET_GRE_SRE_H + strlen((char *)routing);
	t = libnet_build_gre_sre(
	    GRE_IP,                                 /* address family */
	    0,                                      /* offset */
	    strlen((char *)routing),                /* routing length */
	    routing,                                /* routing info */
	    NULL,                                   /* payload */
	    0,                                      /* payload size */
	    l,                                      /* libnet handle */
	    0);                                     /* libnet id */
	if (t == -1)
	{
	    fprintf(stderr, "Can't build GRE last SRE header: %s\n", libnet_geterror(l));
	    goto bad;
	}
    }

    len = libnet_getgre_length(gre_flags);
    size += len;
    t = libnet_build_gre(
        gre_flags,                                  /* flags & version */
        (gre_flags & GRE_VERSION_1 ? GRE_PPP : GRE_IP), /* type */
        checksum,                                   /* v0: checksum / v1: payload_s */
        offset,                                     /* v0: offset   / v1: callID    */
        key,                                        /* v0: key      / v1: seq bum   */
        seq,                                        /* v0: seq num  / v1: ack       */
	size,                                       /* length */
        NULL,                                       /* payload */
        0,                                          /* payload size */
        l,                                          /* libnet handle */
        0);                                         /* libnet id */
    if (t == -1)
    {
        fprintf(stderr, "Can't build GRE header: %s\n", libnet_geterror(l));
        goto bad;
    }

    
    /*
     * Build the "real" IP header
     */
    size+=LIBNET_IPV4_H;
    t = libnet_build_ipv4(
        size,                                       /* length */
        0,                                          /* TOS */
        255,                                        /* IP ID */
        0,                                          /* IP Frag */
        255,                                        /* TTL */
        IPPROTO_GRE,                                /* protocol */
        0,                                          /* checksum */
        src_ip,                                     /* source IP */
        dst_ip,                                     /* destination IP */
        NULL,                                       /* payload */
        0,                                          /* payload size */
        l,                                          /* libnet handle */
        0);                                         /* libnet id */
    if (t == -1)
    {
        fprintf(stderr, "Can't build IP header (GRE): %s\n", libnet_geterror(l));
        goto bad;
    } 

    t = libnet_autobuild_ethernet(
            (uint8_t *)"11:11:11:11:11:11",         /* ethernet destination */
            ETHERTYPE_IP,                           /* protocol type */
            l);                                     /* libnet handle */
    if (t == -1)
    {
        fprintf(stderr, "Can't build ethernet header: %s\n",
                libnet_geterror(l));
        goto bad;
    }
    /*
     *  Write it to the wire.
     */

    c = libnet_write(l);
    if (c == -1)
    {
        fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
        goto bad;
    }
    else
    {
        fprintf(stderr, "Wrote %d byte GRE packet; check the wire.\n", c);
    }
    libnet_destroy(l);
    return (EXIT_SUCCESS);
bad:
    libnet_destroy(l);
    return (EXIT_FAILURE); 
}