Annotation of embedaddon/libnet/sample/bgp4_notification.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  *
                      3:  * libnet 1.1
                      4:  * Build a BGP4 notification 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 NOTIFICATION : BGP4_OPEN_MESSAGE_ERROR / BGP4_AUTHENTICATION_FAILURE
                     13:  *
                     14:  *   # ./bgp4_hdr -s 1.1.1.1 -d 2.2.2.2 -e 2 -c 5
                     15:  *   libnet 1.1 packet shaping: BGP4 notification + payload[raw]
                     16:  *   Wrote 61 byte TCP packet; check the wire.
                     17:  *   
                     18:  *   14:37:45.398786 1.1.1.1.26214 > 2.2.2.2.179: S [tcp sum ok] 
                     19:  *            16843009:16843030(21) win 32767: BGP (ttl 64, id 242, len 61)
                     20:  *   0x0000   4500 003d 00f2 0000 4006 73c4 0101 0101        E..=....@.s.....
                     21:  *   0x0010   0202 0202 6666 00b3 0101 0101 0202 0202        ....ff..........
                     22:  *   0x0020   5002 7fff ac8a 0000 0101 0101 0101 0101        P...............
                     23:  *   0x0030   0101 0101 0101 0101 0015 0302 05               .............
                     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 code, subcode;
                     65:     
                     66:     printf("libnet 1.1 packet shaping: BGP4 notification + payload[raw]\n");
                     67: 
                     68:     /*
                     69:      *  Initialize the library.  Root priviledges are required.
                     70:      */
                     71:     l = libnet_init(
                     72:             LIBNET_RAW4,                            /* injection type */
                     73:             NULL,                                   /* network interface */
                     74:             errbuf);                                /* error buffer */
                     75: 
                     76:     if (l == NULL)
                     77:     {
                     78:         fprintf(stderr, "libnet_init() failed: %s", errbuf);
                     79:         exit(EXIT_FAILURE); 
                     80:     }
                     81: 
                     82:     src_ip  = 0;
                     83:     dst_ip  = 0;
                     84:     code = subcode = 0;
                     85:     memset(marker, 0x1, LIBNET_BGP4_MARKER_SIZE);
                     86: 
                     87:     while ((c = getopt(argc, argv, "d:s:t:m:p:c:e:")) != EOF)
                     88:     {
                     89:         switch (c)
                     90:         {
                     91:             /*
                     92:              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
                     93:              *  point cp to the last dot of the IP address/port string and
                     94:              *  then seperate them with a NULL byte.  The optarg now points to
                     95:              *  just the IP address, and cp points to the port.
                     96:              */
                     97:             case 'd':
                     98:                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                     99:                 {
                    100:                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
                    101:                     exit(EXIT_FAILURE);
                    102:                 }
                    103:                 break;
                    104: 
                    105:             case 's':
                    106:                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
                    107:                 {
                    108:                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
                    109:                     exit(EXIT_FAILURE);
                    110:                 }
                    111:                 break;
                    112: 
                    113:            case 'm':
                    114:                memcpy(marker, optarg, LIBNET_BGP4_MARKER_SIZE);
                    115:                break;
                    116: 
                    117:            case 'e':
                    118:                code = atoi(optarg);
                    119:                break;
                    120: 
                    121:            case 'c':
                    122:                subcode = atoi(optarg);
                    123:                break;
                    124: 
                    125:            case 'p':
                    126:                payload = optarg;
                    127:                payload_s = strlen(optarg);
                    128:                break;
                    129: 
                    130:             default:
                    131:                 exit(EXIT_FAILURE);
                    132:         }
                    133:     }
                    134: 
                    135:     if (!src_ip || !dst_ip)
                    136:     {
                    137:         usage(argv[0]);
                    138:         exit(EXIT_FAILURE);
                    139:     }
                    140: 
                    141:     length = LIBNET_BGP4_NOTIFICATION_H + payload_s;
                    142:     t = libnet_build_bgp4_notification(
                    143:        code,                                       /* error code */   
                    144:        subcode,                                    /* error subcode */
                    145:         NULL,                                       /* payload */
                    146:         0,                                          /* payload size */
                    147:         l,                                          /* libnet handle */
                    148:         0);                                         /* libnet id */
                    149:     if (t == -1)
                    150:     {
                    151:         fprintf(stderr, "Can't build BGP4 notification: %s\n", libnet_geterror(l));
                    152:         goto bad;
                    153:     }
                    154: 
                    155:     length+=LIBNET_BGP4_HEADER_H;
                    156:     t = libnet_build_bgp4_header(
                    157:        marker,                                     /* marker */   
                    158:        length,                                     /* length */
                    159:        LIBNET_BGP4_NOTIFICATION,                   /* message type */
                    160:         NULL,                                       /* payload */
                    161:         0,                                          /* payload size */
                    162:         l,                                          /* libnet handle */
                    163:         0);                                         /* libnet id */
                    164:     if (t == -1)
                    165:     {
                    166:         fprintf(stderr, "Can't build BGP4 header: %s\n", libnet_geterror(l));
                    167:         goto bad;
                    168:     }
                    169: 
                    170:     length+=LIBNET_TCP_H;
                    171:     t = libnet_build_tcp(
                    172:         0x6666,                                     /* source port */
                    173:         179,                                        /* destination port */
                    174:         0x01010101,                                 /* sequence number */
                    175:         0x02020202,                                 /* acknowledgement num */
                    176:         TH_SYN,                                     /* control flags */
                    177:         32767,                                      /* window size */
                    178:         0,                                          /* checksum */
                    179:         0,                                          /* urgent pointer */
                    180:        length,                                     /* TCP packet size */
                    181:         NULL,                                       /* payload */
                    182:         0,                                          /* payload size */
                    183:         l,                                          /* libnet handle */
                    184:         0);                                         /* libnet id */
                    185:     if (t == -1)
                    186:     {
                    187:         fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
                    188:         goto bad;
                    189:     }
                    190: 
                    191:     length+=LIBNET_IPV4_H;
                    192:     t = libnet_build_ipv4(
                    193:         length,                                     /* length */
                    194:         0,                                          /* TOS */
                    195:         242,                                        /* IP ID */
                    196:         0,                                          /* IP Frag */
                    197:         64,                                         /* TTL */
                    198:         IPPROTO_TCP,                                /* protocol */
                    199:         0,                                          /* checksum */
                    200:         src_ip,                                     /* source IP */
                    201:         dst_ip,                                     /* destination IP */
                    202:         NULL,                                       /* payload */
                    203:         0,                                          /* payload size */
                    204:         l,                                          /* libnet handle */
                    205:         0);                                         /* libnet id */
                    206:     if (t == -1)
                    207:     {
                    208:         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
                    209:         goto bad;
                    210:     }
                    211: 
                    212:     /*
                    213:      *  Write it to the wire.
                    214:      */
                    215:     c = libnet_write(l);
                    216:     if (c == -1)
                    217:     {
                    218:         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
                    219:         goto bad;
                    220:     }
                    221:     else
                    222:     {
                    223:         fprintf(stderr, "Wrote %d byte TCP packet; check the wire.\n", c);
                    224:     }
                    225: 
                    226:     libnet_destroy(l);
                    227:     return (EXIT_SUCCESS);
                    228: bad:
                    229:     libnet_destroy(l);
                    230:     return (EXIT_FAILURE);
                    231: }
                    232: 
                    233: void
                    234: usage(char *name)
                    235: {
                    236:     fprintf(stderr,
                    237:         "usage: %s -s source_ip -d destination_ip"
                    238:         " [-m marker] [-e error] [-c subcode] [-p payload] \n",
                    239:         name);
                    240: }
                    241: 
                    242: /* EOF */

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