File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / sample / bgp4_hdr.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *
    3:  * libnet 1.1
    4:  * Build a BGP4 header with what you want as payload
    5:  *
    6:  * Copyright (c) 2003 Frédéric Raynal <pappy@security-labs.org>
    7:  * All rights reserved.
    8:  *
    9:  *
   10:  * Example:
   11:  *
   12:  *   Sending of a BGP KEEPALIVE
   13:  *
   14:  *   # ./bgp4_hdr -s 1.1.1.1 -d 2.2.2.2 -t 4
   15:  *   libnet 1.1 packet shaping: BGP4 hdr + payload[raw]
   16:  *   Wrote 59 byte TCP packet; check the wire.
   17:  *   
   18:  *   13:55:53.811579 1.1.1.1.26214 > 2.2.2.2.179: S [tcp sum ok] 
   19:  *            16843009:16843028(19) win 32767: BGP (ttl 64, id 242, len 59)
   20:  *   0x0000   4500 003b 00f2 0000 4006 73c6 0101 0101        E..;....@.s.....
   21:  *   0x0010   0202 0202 6666 00b3 0101 0101 0202 0202        ....ff..........
   22:  *   0x0020   5002 7fff b090 0000 0101 0101 0101 0101        P...............
   23:  *   0x0030   0101 0101 0101 0101 0013 04                    ...........
   24:  *   
   25:  *
   26:  * Redistribution and use in source and binary forms, with or without
   27:  * modification, are permitted provided that the following conditions
   28:  * are met:
   29:  * 1. Redistributions of source code must retain the above copyright
   30:  *    notice, this list of conditions and the following disclaimer.
   31:  * 2. Redistributions in binary form must reproduce the above copyright
   32:  *    notice, this list of conditions and the following disclaimer in the
   33:  *    documentation and/or other materials provided with the distribution.
   34:  *
   35:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   36:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   37:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   38:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   39:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   40:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   41:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   42:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   43:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   44:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   45:  * SUCH DAMAGE.
   46:  *
   47:  */
   48: #if (HAVE_CONFIG_H)
   49: #include "../include/config.h"
   50: #endif
   51: #include "./libnet_test.h"
   52: 
   53: int
   54: main(int argc, char *argv[])
   55: {
   56:     int c;
   57:     libnet_t *l;
   58:     u_long src_ip, dst_ip, length;
   59:     libnet_ptag_t t = 0;
   60:     char errbuf[LIBNET_ERRBUF_SIZE];
   61:     u_char *payload = NULL;
   62:     u_long payload_s = 0;
   63:     u_char marker[LIBNET_BGP4_MARKER_SIZE];
   64:     u_char type;
   65: 
   66:     printf("libnet 1.1 packet shaping: BGP4 hdr + payload[raw]\n");
   67: 
   68:     l = libnet_init(
   69:             LIBNET_RAW4,                            /* injection type */
   70:             NULL,                                   /* network interface */
   71:             errbuf);                                /* error buffer */
   72: 
   73:     if (l == NULL)
   74:     {
   75:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
   76:         exit(EXIT_FAILURE); 
   77:     }
   78: 
   79:     src_ip  = 0;
   80:     dst_ip  = 0;
   81:     memset(marker, 0x1, LIBNET_BGP4_MARKER_SIZE);
   82:     type = 0;
   83: 
   84:     while ((c = getopt(argc, argv, "d:s:t:m:p:")) != EOF)
   85:     {
   86:         switch (c)
   87:         {
   88:             /*
   89:              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
   90:              *  point cp to the last dot of the IP address/port string and
   91:              *  then seperate them with a NULL byte.  The optarg now points to
   92:              *  just the IP address, and cp points to the port.
   93:              */
   94:             case 'd':
   95:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
   96:                 {
   97:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
   98:                     exit(EXIT_FAILURE);
   99:                 }
  100:                 break;
  101: 
  102:             case 's':
  103:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
  104:                 {
  105:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  106:                     exit(EXIT_FAILURE);
  107:                 }
  108:                 break;
  109: 
  110: 	    case 'm':
  111: 		memcpy(marker, optarg, LIBNET_BGP4_MARKER_SIZE);
  112: 		break;
  113: 
  114: 	    case 't':
  115: 		type = atoi(optarg);
  116: 		break;
  117: 
  118: 	    case 'p':
  119: 		payload = (u_char *)optarg;
  120: 		payload_s = strlen((char *)optarg);
  121: 		break;
  122: 
  123:             default:
  124:                 exit(EXIT_FAILURE);
  125:         }
  126:     }
  127: 
  128:     if (!src_ip || !dst_ip)
  129:     {
  130:         usage(argv[0]);
  131:         exit(EXIT_FAILURE);
  132:     }
  133: 
  134: 
  135:     length = LIBNET_BGP4_HEADER_H + payload_s;
  136:     t = libnet_build_bgp4_header(
  137: 	marker,                                     /* marker */   
  138: 	length,                                     /* length */
  139: 	type,                                       /* message type */
  140:         payload,                                    /* payload */
  141:         payload_s,                                  /* payload size */
  142:         l,                                          /* libnet handle */
  143:         0);                                         /* libnet id */
  144:     if (t == -1)
  145:     {
  146:         fprintf(stderr, "Can't build BGP4 header: %s\n", libnet_geterror(l));
  147:         goto bad;
  148:     }
  149: 
  150:     length+=LIBNET_TCP_H;
  151:     t = libnet_build_tcp(
  152:         0x6666,                                     /* source port */
  153:         179,                                        /* destination port */
  154:         0x01010101,                                 /* sequence number */
  155:         0x02020202,                                 /* acknowledgement num */
  156:         TH_SYN,                                     /* control flags */
  157:         32767,                                      /* window size */
  158:         0,                                          /* checksum */
  159:         0,                                          /* urgent pointer */
  160: 	length,                                     /* TCP packet size */
  161:         NULL,                                       /* payload */
  162:         0,                                          /* payload size */
  163:         l,                                          /* libnet handle */
  164:         0);                                         /* libnet id */
  165:     if (t == -1)
  166:     {
  167:         fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
  168:         goto bad;
  169:     }
  170: 
  171:     length+=LIBNET_IPV4_H;
  172:     t = libnet_build_ipv4(
  173:         length,                                     /* length */
  174:         0,                                          /* TOS */
  175:         242,                                        /* IP ID */
  176:         0,                                          /* IP Frag */
  177:         64,                                         /* TTL */
  178:         IPPROTO_TCP,                                /* protocol */
  179:         0,                                          /* checksum */
  180:         src_ip,                                     /* source IP */
  181:         dst_ip,                                     /* destination IP */
  182:         NULL,                                       /* payload */
  183:         0,                                          /* payload size */
  184:         l,                                          /* libnet handle */
  185:         0);                                         /* libnet id */
  186:     if (t == -1)
  187:     {
  188:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
  189:         goto bad;
  190:     }
  191: 
  192:     /*
  193:      *  Write it to the wire.
  194:      */
  195:     c = libnet_write(l);
  196:     if (c == -1)
  197:     {
  198:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
  199:         goto bad;
  200:     }
  201:     else
  202:     {
  203:         fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
  204:     }
  205: 
  206:     libnet_destroy(l);
  207:     return (EXIT_SUCCESS);
  208: bad:
  209:     libnet_destroy(l);
  210:     return (EXIT_FAILURE);
  211: }
  212: 
  213: void
  214: usage(char *name)
  215: {
  216:     fprintf(stderr,
  217:         "usage: %s -s source_ip -d destination_ip"
  218:         " [-m marker] [-p payload] [-t BGP type]\n",
  219:         name);
  220: }
  221: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>