|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_build_icmp.c,v 1.17 2004/04/13 17:32:28 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_build_icmp.c - ICMP packet assemblers
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
|
Packit |
03b34a |
* All rights reserved.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
03b34a |
* modification, are permitted provided that the following conditions
|
|
Packit |
03b34a |
* are met:
|
|
Packit |
03b34a |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
03b34a |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
03b34a |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
03b34a |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
03b34a |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
03b34a |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
Packit |
03b34a |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
03b34a |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
03b34a |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
03b34a |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
03b34a |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
03b34a |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
03b34a |
* SUCH DAMAGE.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if (HAVE_CONFIG_H)
|
|
Packit |
03b34a |
#include "../include/config.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#if (!(_WIN32) || (__CYGWIN__))
|
|
Packit |
03b34a |
#include "../include/libnet.h"
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include "../include/win32/libnet.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#include <assert.h>
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* some common cruft for completing ICMP error packets */
|
|
Packit |
03b34a |
#define LIBNET_BUILD_ICMP_ERR_FINISH(len) \
|
|
Packit |
03b34a |
do \
|
|
Packit |
03b34a |
{ \
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, (uint8_t *)&icmp_hdr, len); \
|
|
Packit |
03b34a |
if (n == -1) \
|
|
Packit |
03b34a |
{ \
|
|
Packit |
03b34a |
goto bad; \
|
|
Packit |
03b34a |
} \
|
|
Packit |
03b34a |
\
|
|
Packit |
03b34a |
if (payload_s && !payload) \
|
|
Packit |
03b34a |
{ \
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, \
|
|
Packit |
03b34a |
"%s(): payload inconsistency\n", __func__); \
|
|
Packit |
03b34a |
goto bad; \
|
|
Packit |
03b34a |
} \
|
|
Packit |
03b34a |
\
|
|
Packit |
03b34a |
if (payload_s) \
|
|
Packit |
03b34a |
{ \
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, payload, payload_s); \
|
|
Packit |
03b34a |
if (n == -1) \
|
|
Packit |
03b34a |
{ \
|
|
Packit |
03b34a |
goto bad; \
|
|
Packit |
03b34a |
} \
|
|
Packit |
03b34a |
} \
|
|
Packit |
03b34a |
\
|
|
Packit |
03b34a |
if (sum == 0) \
|
|
Packit |
03b34a |
{ \
|
|
Packit |
03b34a |
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM); \
|
|
Packit |
03b34a |
} \
|
|
Packit |
03b34a |
} while (0)
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv4_echo(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint16_t id, uint16_t seq, const uint8_t *payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv4_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_ICMPV4_ECHO_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
h = LIBNET_ICMPV4_ECHO_H + payload_s; /* hl for checksum */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_ECHO_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type; /* packet type */
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code; /* packet code */
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = (sum ? htons(sum) : 0); /* checksum */
|
|
Packit |
03b34a |
icmp_hdr.icmp_id = htons(id); /* packet id */
|
|
Packit |
03b34a |
icmp_hdr.icmp_seq = htons(seq); /* packet seq */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, (uint8_t *)&icmp_hdr, LIBNET_ICMPV4_ECHO_H);
|
|
Packit |
03b34a |
if (n == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* boilerplate payload sanity check / append macro */
|
|
Packit |
03b34a |
LIBNET_DO_PAYLOAD(l, p);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (sum == 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* If checksum is zero, by default libnet will compute a checksum
|
|
Packit |
03b34a |
* for the user. The programmer can override this by calling
|
|
Packit |
03b34a |
* libnet_toggle_checksum(l, ptag, 1);
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_ICMPV4_ECHO_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv4_mask(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint16_t id, uint16_t seq, uint32_t mask, const uint8_t *payload,
|
|
Packit |
03b34a |
uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv4_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_ICMPV4_MASK_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
h = LIBNET_ICMPV4_MASK_H + payload_s; /* hl for checksum */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_MASK_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type; /* packet type */
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code; /* packet code */
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = (sum ? htons(sum) : 0); /* checksum */
|
|
Packit |
03b34a |
icmp_hdr.icmp_id = htons(id); /* packet id */
|
|
Packit |
03b34a |
icmp_hdr.icmp_seq = htons(seq); /* packet seq */
|
|
Packit |
03b34a |
icmp_hdr.icmp_mask = htonl(mask); /* address mask */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, (uint8_t *)&icmp_hdr, LIBNET_ICMPV4_MASK_H);
|
|
Packit |
03b34a |
if (n == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* boilerplate payload sanity check / append macro */
|
|
Packit |
03b34a |
LIBNET_DO_PAYLOAD(l, p);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (sum == 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* If checksum is zero, by default libnet will compute a checksum
|
|
Packit |
03b34a |
* for the user. The programmer can override this by calling
|
|
Packit |
03b34a |
* libnet_toggle_checksum(l, ptag, 1);
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_ICMPV4_MASK_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv4_timestamp(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint16_t id, uint16_t seq, uint32_t otime, uint32_t rtime, uint32_t ttime,
|
|
Packit |
03b34a |
const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv4_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_ICMPV4_TS_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
h = LIBNET_ICMPV4_TS_H + payload_s; /* hl for checksum */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_TS_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type; /* packet type */
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code; /* packet code */
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = (sum ? htons(sum) : 0); /* checksum */
|
|
Packit |
03b34a |
icmp_hdr.icmp_id = htons(id); /* packet id */
|
|
Packit |
03b34a |
icmp_hdr.icmp_seq = htons(seq); /* packet seq */
|
|
Packit |
03b34a |
icmp_hdr.icmp_otime = htonl(otime); /* original timestamp */
|
|
Packit |
03b34a |
icmp_hdr.icmp_rtime = htonl(rtime); /* receive timestamp */
|
|
Packit |
03b34a |
icmp_hdr.icmp_ttime = htonl(ttime); /* transmit timestamp */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = libnet_pblock_append(l, p, (uint8_t *)&icmp_hdr, LIBNET_ICMPV4_TS_H);
|
|
Packit |
03b34a |
if (n == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* boilerplate payload sanity check / append macro */
|
|
Packit |
03b34a |
LIBNET_DO_PAYLOAD(l, p);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (sum == 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* If checksum is zero, by default libnet will compute a checksum
|
|
Packit |
03b34a |
* for the user. The programmer can override this by calling
|
|
Packit |
03b34a |
* libnet_toggle_checksum(l, ptag, 1);
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_ICMPV4_TS_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv4_unreach(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv4_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
n = LIBNET_ICMPV4_UNREACH_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* FREDRAYNAL: as ICMP checksum includes what is embedded in
|
|
Packit |
03b34a |
* the payload, and what is after the ICMP header, we need to include
|
|
Packit |
03b34a |
* those 2 sizes.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
h = LIBNET_ICMPV4_UNREACH_H + payload_s + l->total_size;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_UNREACH_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type; /* packet type */
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code; /* packet code */
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = (sum ? htons(sum) : 0); /* checksum */
|
|
Packit |
03b34a |
icmp_hdr.icmp_id = 0; /* must be 0 */
|
|
Packit |
03b34a |
icmp_hdr.icmp_seq = 0; /* must be 0 */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
LIBNET_BUILD_ICMP_ERR_FINISH(LIBNET_ICMPV4_UNREACH_H);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_ICMPV4_UNREACH_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv4_timeexceed(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv4_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* size of memory block */
|
|
Packit |
03b34a |
n = LIBNET_ICMPV4_TIMXCEED_H + payload_s;
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* FREDRAYNAL: as ICMP checksum includes what is embedded in
|
|
Packit |
03b34a |
* the payload, and what is after the ICMP header, we need to include
|
|
Packit |
03b34a |
* those 2 sizes.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
h = LIBNET_ICMPV4_TIMXCEED_H + payload_s + l->total_size;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_TIMXCEED_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type; /* packet type */
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code; /* packet code */
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = (sum ? htons(sum) : 0); /* checksum */
|
|
Packit |
03b34a |
icmp_hdr.icmp_id = 0; /* must be 0 */
|
|
Packit |
03b34a |
icmp_hdr.icmp_seq = 0; /* must be 0 */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
LIBNET_BUILD_ICMP_ERR_FINISH(LIBNET_ICMPV4_TIMXCEED_H);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_ICMPV4_TIMXCEED_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv4_redirect(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint32_t gateway, const uint8_t *payload, uint32_t payload_s, libnet_t *l,
|
|
Packit |
03b34a |
libnet_ptag_t ptag)
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n, h;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv4_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_ICMPV4_REDIRECT_H + payload_s; /* size of memory block */
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* FREDRAYNAL: as ICMP checksum includes what is embedded in
|
|
Packit |
03b34a |
* the payload, and what is after the ICMP header, we need to include
|
|
Packit |
03b34a |
* those 2 sizes.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
h = LIBNET_ICMPV4_REDIRECT_H + payload_s + l->total_size;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Find the existing protocol block if a ptag is specified, or create
|
|
Packit |
03b34a |
* a new one.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV4_REDIRECT_H);
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type; /* packet type */
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code; /* packet code */
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = (sum ? htons(sum) : 0); /* checksum */
|
|
Packit |
03b34a |
icmp_hdr.hun.gateway = gateway;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
LIBNET_BUILD_ICMP_ERR_FINISH(LIBNET_ICMPV4_REDIRECT_H);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (ptag ? ptag : libnet_pblock_update(l, p, h,
|
|
Packit |
03b34a |
LIBNET_PBLOCK_ICMPV4_REDIRECT_H));
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv6_common(
|
|
Packit |
03b34a |
uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
const void* specific, uint32_t specific_s, uint8_t pblock_type,
|
|
Packit |
03b34a |
uint8_t *payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t *l, libnet_ptag_t ptag
|
|
Packit |
03b34a |
)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint32_t n;
|
|
Packit |
03b34a |
libnet_pblock_t *p;
|
|
Packit |
03b34a |
struct libnet_icmpv6_hdr icmp_hdr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
n = LIBNET_ICMPV6_COMMON_H + specific_s + payload_s;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, pblock_type);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (p == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&icmp_hdr, 0, sizeof(icmp_hdr));
|
|
Packit |
03b34a |
icmp_hdr.icmp_type = type;
|
|
Packit |
03b34a |
icmp_hdr.icmp_code = code;
|
|
Packit |
03b34a |
icmp_hdr.icmp_sum = htons(sum);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (libnet_pblock_append(l, p, (uint8_t *)&icmp_hdr, LIBNET_ICMPV6_COMMON_H) < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (libnet_pblock_append(l, p, specific, specific_s) < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (libnet_pblock_append(l, p, payload, payload_s) < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (sum == 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return ptag ? ptag : libnet_pblock_update(l, p, 0, pblock_type);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return -1;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t libnet_build_icmpv6(uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint8_t* payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t* l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return libnet_build_icmpv6_common(
|
|
Packit |
03b34a |
type, code, sum,
|
|
Packit |
03b34a |
NULL, 0, LIBNET_PBLOCK_ICMPV6_UNREACH_H,
|
|
Packit |
03b34a |
payload, payload_s,
|
|
Packit |
03b34a |
l, ptag);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv6_unreach(
|
|
Packit |
03b34a |
uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint8_t *payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t *l, libnet_ptag_t ptag
|
|
Packit |
03b34a |
)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct libnet_icmpv6_unreach specific;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&specific, 0, sizeof(specific));
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return libnet_build_icmpv6_common(
|
|
Packit |
03b34a |
type, code, sum,
|
|
Packit |
03b34a |
&specific, sizeof(specific), LIBNET_PBLOCK_ICMPV6_UNREACH_H,
|
|
Packit |
03b34a |
payload, payload_s,
|
|
Packit |
03b34a |
l, ptag);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t
|
|
Packit |
03b34a |
libnet_build_icmpv6_echo(
|
|
Packit |
03b34a |
uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint16_t id, uint16_t seq,
|
|
Packit |
03b34a |
uint8_t *payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t *l, libnet_ptag_t ptag
|
|
Packit |
03b34a |
)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct libnet_icmpv6_echo specific;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&specific, 0, sizeof(specific));
|
|
Packit |
03b34a |
specific.id = htons(id);
|
|
Packit |
03b34a |
specific.seq = htons(seq);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return libnet_build_icmpv6_common(
|
|
Packit |
03b34a |
type, code, sum,
|
|
Packit |
03b34a |
&specific, sizeof(specific), LIBNET_PBLOCK_ICMPV6_ECHO_H,
|
|
Packit |
03b34a |
payload, payload_s,
|
|
Packit |
03b34a |
l, ptag);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t libnet_build_icmpv6_ndp_nsol(
|
|
Packit |
03b34a |
uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
struct libnet_in6_addr target,
|
|
Packit |
03b34a |
uint8_t *payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t* l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct libnet_icmpv6_ndp_nsol specific;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&specific, 0, sizeof(specific));
|
|
Packit |
03b34a |
specific.reserved = 0;
|
|
Packit |
03b34a |
specific.target_addr = target;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return libnet_build_icmpv6_common(
|
|
Packit |
03b34a |
type, code, sum,
|
|
Packit |
03b34a |
&specific, sizeof(specific), LIBNET_PBLOCK_ICMPV6_NDP_NSOL_H,
|
|
Packit |
03b34a |
payload, payload_s,
|
|
Packit |
03b34a |
l, ptag);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t libnet_build_icmpv6_ndp_nadv(
|
|
Packit |
03b34a |
uint8_t type, uint8_t code, uint16_t sum,
|
|
Packit |
03b34a |
uint32_t flags, struct libnet_in6_addr target,
|
|
Packit |
03b34a |
uint8_t *payload, uint32_t payload_s,
|
|
Packit |
03b34a |
libnet_t* l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
struct libnet_icmpv6_ndp_nadv specific;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&specific, 0, sizeof(specific));
|
|
Packit |
03b34a |
specific.flags = htonl(flags);
|
|
Packit |
03b34a |
specific.target_addr = target;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return libnet_build_icmpv6_common(
|
|
Packit |
03b34a |
type, code, sum,
|
|
Packit |
03b34a |
&specific, sizeof(specific), LIBNET_PBLOCK_ICMPV6_NDP_NADV_H,
|
|
Packit |
03b34a |
payload, payload_s,
|
|
Packit |
03b34a |
l, ptag);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
libnet_ptag_t libnet_build_icmpv6_ndp_opt(
|
|
Packit |
03b34a |
uint8_t type, uint8_t* option, uint32_t option_s,
|
|
Packit |
03b34a |
libnet_t* l, libnet_ptag_t ptag)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct libnet_icmpv6_ndp_opt opt;
|
|
Packit |
03b34a |
uint32_t n;
|
|
Packit |
03b34a |
static uint8_t pad[8] = { 0 };
|
|
Packit |
03b34a |
uint32_t pad_s = 0;
|
|
Packit |
03b34a |
libnet_pblock_t* p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(l == NULL)
|
|
Packit |
03b34a |
return -1;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(!option)
|
|
Packit |
03b34a |
option_s = 0;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* options need to be padded to a multiple of 8-bytes, and opts.len is in units of 8-bytes */
|
|
Packit |
03b34a |
n = sizeof(opt) + option_s;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(n % 8)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
n += 8 - (n % 8);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(n > (0xff * 8))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return -1;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
pad_s = n - option_s - sizeof(opt);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
assert((n % 8) == 0);
|
|
Packit |
03b34a |
assert(pad_s < sizeof(pad));
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_ICMPV6_NDP_OPT_H);
|
|
Packit |
03b34a |
if(p == NULL)
|
|
Packit |
03b34a |
return -1;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&opt, 0, sizeof(opt));
|
|
Packit |
03b34a |
opt.type = type;
|
|
Packit |
03b34a |
opt.len = n / 8;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(libnet_pblock_append(l, p, &opt, sizeof(opt)) == -1)
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(libnet_pblock_append(l, p, option, option_s) == -1)
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if(libnet_pblock_append(l, p, pad, pad_s) == -1)
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return ptag ? ptag : libnet_pblock_update(l, p, n, LIBNET_PBLOCK_ICMPV6_NDP_OPT_H);
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
bad:
|
|
Packit |
03b34a |
libnet_pblock_delete(l, p);
|
|
Packit |
03b34a |
return -1;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|