Annotation of embedaddon/libnet/src/libnet_build_ipsec.c, revision 1.1.1.3

1.1       misho       1: /*
1.1.1.2   misho       2:  *  $Id: libnet_build_ipsec.c,v 1.12 2004/04/13 17:32:28 mike Exp $
1.1       misho       3:  *
                      4:  *  libnet
                      5:  *  libnet_build_ipsec.c - IP packet assembler
                      6:  *
                      7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
                      8:  *  Copyright (c) 2002 Jose Nazario <jose@crimelabs.net>
                      9:  *  All rights reserved.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  *
                     32:  */
                     33: 
1.1.1.3 ! misho      34: #include "common.h"
1.1       misho      35: 
                     36: libnet_ptag_t
1.1.1.2   misho      37: libnet_build_ipsec_esp_hdr(uint32_t spi, uint32_t seq, uint32_t iv,
                     38: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
1.1       misho      39: {
1.1.1.2   misho      40:     uint32_t n, h;
1.1       misho      41:     libnet_pblock_t *p;
                     42:     struct libnet_esp_hdr esp_hdr;
                     43: 
                     44:     if (l == NULL)
                     45:     { 
                     46:         return (-1);
                     47:     } 
                     48: 
                     49:     n = LIBNET_IPSEC_ESP_HDR_H + payload_s;/* size of memory block */
                     50:     h = 0;
                     51: 
                     52:     memset(&esp_hdr, 0, sizeof(esp_hdr));
                     53:     esp_hdr.esp_spi = htonl(spi);      /* SPI */
                     54:     esp_hdr.esp_seq = htonl(seq);      /* ESP sequence number */
                     55:     esp_hdr.esp_iv = htonl(iv);        /* initialization vector */
                     56: 
                     57:     /*
                     58:      *  Find the existing protocol block if a ptag is specified, or create
                     59:      *  a new one.
                     60:      */
                     61:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_IPSEC_ESP_HDR_H);
                     62:     if (p == NULL)
                     63:     {
                     64:         return (-1);
                     65:     }
                     66: 
1.1.1.2   misho      67:     n = libnet_pblock_append(l, p, (uint8_t *)&esp_hdr, LIBNET_IPSEC_ESP_HDR_H);
1.1       misho      68:     if (n == -1)
                     69:     {
                     70:         goto bad;
                     71:     }
                     72: 
1.1.1.2   misho      73:     /* boilerplate payload sanity check / append macro */
                     74:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho      75: 
                     76:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
                     77:             LIBNET_PBLOCK_IPSEC_ESP_HDR_H));
                     78: bad:
                     79:     libnet_pblock_delete(l, p);
                     80:     return (-1);
                     81: }
                     82: 
                     83: 
                     84: libnet_ptag_t
1.1.1.2   misho      85: libnet_build_ipsec_esp_ftr(uint8_t len, uint8_t nh, int8_t *auth,
                     86:             const uint8_t *payload, uint32_t payload_s, libnet_t *l,
1.1       misho      87:             libnet_ptag_t ptag)
                     88: {
                     89:     /* XXX we need to know the size of auth */
1.1.1.2   misho      90:     uint32_t n, h;
1.1       misho      91:     libnet_pblock_t *p;
                     92:     struct libnet_esp_ftr esp_ftr;
                     93: 
                     94:     if (l == NULL)
                     95:     { 
                     96:         return (-1);
                     97:     } 
                     98: 
                     99:     n = LIBNET_IPSEC_ESP_FTR_H + payload_s;/* size of memory block */
                    100:     h = 0;
                    101: 
                    102:     memset(&esp_ftr, 0, sizeof(esp_ftr));
                    103:     esp_ftr.esp_pad_len = len;      /* pad length */
                    104:     esp_ftr.esp_nh = nh;  /* next header pointer */
                    105:     esp_ftr.esp_auth = auth;        /* authentication data */
                    106: 
                    107:     /*
                    108:      *  Find the existing protocol block if a ptag is specified, or create
                    109:      *  a new one.
                    110:      */
                    111:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_IPSEC_ESP_FTR_H);
                    112:     if (p == NULL)
                    113:     {
                    114:         return (-1);
                    115:     }
                    116: 
1.1.1.2   misho     117:     n = libnet_pblock_append(l, p, (uint8_t *)&esp_ftr, LIBNET_IPSEC_ESP_FTR_H);
1.1       misho     118:     if (n == -1)
                    119:     {
                    120:         goto bad;
                    121:     }
                    122: 
1.1.1.2   misho     123:     /* boilerplate payload sanity check / append macro */
                    124:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho     125: 
                    126:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
                    127:             LIBNET_PBLOCK_IPSEC_ESP_FTR_H));
                    128: bad:
                    129:     libnet_pblock_delete(l, p);
                    130:     return (-1);
                    131: }
                    132: 
                    133: 
                    134: libnet_ptag_t
1.1.1.2   misho     135: libnet_build_ipsec_ah(uint8_t nh, uint8_t len, uint16_t res,
                    136: uint32_t spi, uint32_t seq, uint32_t auth, const uint8_t *payload,
                    137: uint32_t payload_s,  libnet_t *l, libnet_ptag_t ptag)
1.1       misho     138: {
1.1.1.2   misho     139:     uint32_t n, h;
1.1       misho     140:     libnet_pblock_t *p;
                    141:     struct libnet_ah_hdr ah_hdr;
                    142: 
                    143:     if (l == NULL)
                    144:     { 
                    145:         return (-1);
                    146:     } 
                    147: 
                    148:     n = LIBNET_IPSEC_AH_H + payload_s;/* size of memory block */
                    149:     h = 0;
                    150: 
                    151:     /*
                    152:      *  Find the existing protocol block if a ptag is specified, or create
                    153:      *  a new one.
                    154:      */
                    155:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_IPSEC_AH_H);
                    156:     if (p == NULL)
                    157:     {
                    158:         return (-1);
                    159:     }
                    160: 
                    161:     memset(&ah_hdr, 0, sizeof(ah_hdr));
                    162:     ah_hdr.ah_nh = nh;       /* next header */
                    163:     ah_hdr.ah_len = len;               /* length */
                    164:     ah_hdr.ah_res = (res ? htons(res) : 0);
                    165:     ah_hdr.ah_spi = htonl(spi);        /* SPI */
                    166:     ah_hdr.ah_seq = htonl(seq);        /* AH sequence number */
                    167:     ah_hdr.ah_auth = htonl(auth);      /* authentication data */
                    168: 
1.1.1.2   misho     169:     n = libnet_pblock_append(l, p, (uint8_t *)&ah_hdr, LIBNET_IPSEC_AH_H);
1.1       misho     170:     if (n == -1)
                    171:     {
                    172:         goto bad;
                    173:     }
                    174: 
1.1.1.2   misho     175:     /* boilerplate payload sanity check / append macro */
                    176:     LIBNET_DO_PAYLOAD(l, p);
1.1       misho     177: 
                    178:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
                    179:             LIBNET_PBLOCK_IPSEC_AH_H));
                    180: bad:
                    181:     libnet_pblock_delete(l, p);
                    182:     return (-1);
                    183: }
                    184: 
1.1.1.3 ! misho     185: /**
        !           186:  * Local Variables:
        !           187:  *  indent-tabs-mode: nil
        !           188:  *  c-file-style: "stroustrup"
        !           189:  * End:
        !           190:  */

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