File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_build_ipsec.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 11:54:42 2013 UTC (10 years, 11 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_1_6p5, v1_1_6p4, v1_1_6p0, v1_1_6, HEAD
1.1.6

    1: /*
    2:  *  $Id: libnet_build_ipsec.c,v 1.1.1.2 2013/07/22 11:54:42 misho Exp $
    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: 
   34: #if (HAVE_CONFIG_H)
   35: #include "../include/config.h"
   36: #endif
   37: #if (!(_WIN32) || (__CYGWIN__)) 
   38: #include "../include/libnet.h"
   39: #else
   40: #include "../include/win32/libnet.h"
   41: #endif
   42: 
   43: 
   44: libnet_ptag_t
   45: libnet_build_ipsec_esp_hdr(uint32_t spi, uint32_t seq, uint32_t iv,
   46: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
   47: {
   48:     uint32_t n, h;
   49:     libnet_pblock_t *p;
   50:     struct libnet_esp_hdr esp_hdr;
   51: 
   52:     if (l == NULL)
   53:     { 
   54:         return (-1);
   55:     } 
   56: 
   57:     n = LIBNET_IPSEC_ESP_HDR_H + payload_s;/* size of memory block */
   58:     h = 0;
   59: 
   60:     memset(&esp_hdr, 0, sizeof(esp_hdr));
   61:     esp_hdr.esp_spi = htonl(spi);      /* SPI */
   62:     esp_hdr.esp_seq = htonl(seq);      /* ESP sequence number */
   63:     esp_hdr.esp_iv = htonl(iv);        /* initialization vector */
   64: 
   65:     /*
   66:      *  Find the existing protocol block if a ptag is specified, or create
   67:      *  a new one.
   68:      */
   69:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_IPSEC_ESP_HDR_H);
   70:     if (p == NULL)
   71:     {
   72:         return (-1);
   73:     }
   74: 
   75:     n = libnet_pblock_append(l, p, (uint8_t *)&esp_hdr, LIBNET_IPSEC_ESP_HDR_H);
   76:     if (n == -1)
   77:     {
   78:         goto bad;
   79:     }
   80: 
   81:     /* boilerplate payload sanity check / append macro */
   82:     LIBNET_DO_PAYLOAD(l, p);
   83: 
   84:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
   85:             LIBNET_PBLOCK_IPSEC_ESP_HDR_H));
   86: bad:
   87:     libnet_pblock_delete(l, p);
   88:     return (-1);
   89: }
   90: 
   91: 
   92: libnet_ptag_t
   93: libnet_build_ipsec_esp_ftr(uint8_t len, uint8_t nh, int8_t *auth,
   94:             const uint8_t *payload, uint32_t payload_s, libnet_t *l,
   95:             libnet_ptag_t ptag)
   96: {
   97:     /* XXX we need to know the size of auth */
   98:     uint32_t n, h;
   99:     libnet_pblock_t *p;
  100:     struct libnet_esp_ftr esp_ftr;
  101: 
  102:     if (l == NULL)
  103:     { 
  104:         return (-1);
  105:     } 
  106: 
  107:     n = LIBNET_IPSEC_ESP_FTR_H + payload_s;/* size of memory block */
  108:     h = 0;
  109: 
  110:     memset(&esp_ftr, 0, sizeof(esp_ftr));
  111:     esp_ftr.esp_pad_len = len;      /* pad length */
  112:     esp_ftr.esp_nh = nh;  /* next header pointer */
  113:     esp_ftr.esp_auth = auth;        /* authentication data */
  114: 
  115:     /*
  116:      *  Find the existing protocol block if a ptag is specified, or create
  117:      *  a new one.
  118:      */
  119:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_IPSEC_ESP_FTR_H);
  120:     if (p == NULL)
  121:     {
  122:         return (-1);
  123:     }
  124: 
  125:     n = libnet_pblock_append(l, p, (uint8_t *)&esp_ftr, LIBNET_IPSEC_ESP_FTR_H);
  126:     if (n == -1)
  127:     {
  128:         goto bad;
  129:     }
  130: 
  131:     /* boilerplate payload sanity check / append macro */
  132:     LIBNET_DO_PAYLOAD(l, p);
  133: 
  134:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
  135:             LIBNET_PBLOCK_IPSEC_ESP_FTR_H));
  136: bad:
  137:     libnet_pblock_delete(l, p);
  138:     return (-1);
  139: }
  140: 
  141: 
  142: libnet_ptag_t
  143: libnet_build_ipsec_ah(uint8_t nh, uint8_t len, uint16_t res,
  144: uint32_t spi, uint32_t seq, uint32_t auth, const uint8_t *payload,
  145: uint32_t payload_s,  libnet_t *l, libnet_ptag_t ptag)
  146: {
  147:     uint32_t n, h;
  148:     libnet_pblock_t *p;
  149:     struct libnet_ah_hdr ah_hdr;
  150: 
  151:     if (l == NULL)
  152:     { 
  153:         return (-1);
  154:     } 
  155: 
  156:     n = LIBNET_IPSEC_AH_H + payload_s;/* size of memory block */
  157:     h = 0;
  158: 
  159:     /*
  160:      *  Find the existing protocol block if a ptag is specified, or create
  161:      *  a new one.
  162:      */
  163:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_IPSEC_AH_H);
  164:     if (p == NULL)
  165:     {
  166:         return (-1);
  167:     }
  168: 
  169:     memset(&ah_hdr, 0, sizeof(ah_hdr));
  170:     ah_hdr.ah_nh = nh;       /* next header */
  171:     ah_hdr.ah_len = len;               /* length */
  172:     ah_hdr.ah_res = (res ? htons(res) : 0);
  173:     ah_hdr.ah_spi = htonl(spi);        /* SPI */
  174:     ah_hdr.ah_seq = htonl(seq);        /* AH sequence number */
  175:     ah_hdr.ah_auth = htonl(auth);      /* authentication data */
  176: 
  177:     n = libnet_pblock_append(l, p, (uint8_t *)&ah_hdr, LIBNET_IPSEC_AH_H);
  178:     if (n == -1)
  179:     {
  180:         goto bad;
  181:     }
  182: 
  183:     /* boilerplate payload sanity check / append macro */
  184:     LIBNET_DO_PAYLOAD(l, p);
  185: 
  186:     return (ptag ? ptag : libnet_pblock_update(l, p, h, 
  187:             LIBNET_PBLOCK_IPSEC_AH_H));
  188: bad:
  189:     libnet_pblock_delete(l, p);
  190:     return (-1);
  191: }
  192: 
  193: /* EOF */

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