* $Id: libnet_build_bgp.c,v 1.20 2005/11/29 22:21:52 carlosc Exp $
* libnet_build_bgp.c - BGP packet assembler (RFC 1771)
* Copyright (c) 2003 Frederic Raynal <pappy@security-labs.org>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#include "../include/config.h"
#if (!(_WIN32) || (__CYGWIN__))
#include "../include/libnet.h"
#include "../include/win32/libnet.h"
libnet_build_bgp4_header(u_int8_t marker
[LIBNET_BGP4_MARKER_SIZE
],
u_int16_t len
, u_int8_t type
, u_int8_t
*payload
, u_int32_t payload_s
,
libnet_t
*l
, libnet_ptag_t ptag
)
struct libnet_bgp4_header_hdr bgp4_hdr
;
n
= LIBNET_BGP4_HEADER_H
+ payload_s
; /* size of memory block */
h
= 0; /* BGP headers have no checksum */
* Find the existing protocol block if a ptag is specified, or create
p
= libnet_pblock_probe(l
, ptag
, n
, LIBNET_PBLOCK_BGP4_HEADER_H
);
memset(&bgp4_hdr
, 0, sizeof(bgp4_hdr
));
memcpy(bgp4_hdr
.marker
, marker
, LIBNET_BGP4_MARKER_SIZE
* sizeof (u_int8_t
));
bgp4_hdr
.len
= htons(len
);
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&bgp4_hdr
, LIBNET_BGP4_HEADER_H
);
if ((payload
&& !payload_s
) || (!payload
&& payload_s
))
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): payload inconsistency\n", __func__
);
if (payload
&& payload_s
)
n
= libnet_pblock_append(l
, p
, payload
, payload_s
);
/* err msg set in libnet_pblock_append() */
return (ptag
? ptag
: libnet_pblock_update(l
, p
, h
,
LIBNET_PBLOCK_BGP4_HEADER_H
));
libnet_pblock_delete(l
, p
);
libnet_build_bgp4_open(u_int8_t version
, u_int16_t src_as
, u_int16_t hold_time
,
u_int32_t bgp_id
, u_int8_t opt_len
, u_int8_t
*payload
, u_int32_t payload_s
,
libnet_t
*l
, libnet_ptag_t ptag
)
n
= LIBNET_BGP4_OPEN_H
+ payload_s
; /* size of memory block */
h
= 0; /* BGP msg have no checksum */
* Find the existing protocol block if a ptag is specified, or create
p
= libnet_pblock_probe(l
, ptag
, n
, LIBNET_PBLOCK_BGP4_OPEN_H
);
/* for memory alignment reason, we need to append each field separately */
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&version
, sizeof (version
));
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&val
, sizeof(src_as
));
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&val
, sizeof(hold_time
));
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&n
, sizeof(bgp_id
));
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&opt_len
, sizeof(opt_len
));
if ((payload
&& !payload_s
) || (!payload
&& payload_s
))
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): payload inconsistency\n", __func__
);
if (payload
&& payload_s
)
n
= libnet_pblock_append(l
, p
, payload
, payload_s
);
/* err msg set in libnet_pblock_append() */
return (ptag
? ptag
: libnet_pblock_update(l
, p
, h
,
LIBNET_PBLOCK_BGP4_OPEN_H
));
libnet_pblock_delete(l
, p
);
libnet_build_bgp4_update(u_int16_t unfeasible_rt_len
, u_int8_t
*withdrawn_rt
,
u_int16_t total_path_attr_len
, u_int8_t
*path_attributes
,
u_int16_t info_len
, u_int8_t
*reachability_info
,
u_int8_t
*payload
, u_int32_t payload_s
, libnet_t
*l
, libnet_ptag_t ptag
)
/* size of memory block */
n
= LIBNET_BGP4_UPDATE_H
+ unfeasible_rt_len
+ total_path_attr_len
+
/* BGP msg have no checksum */
* Find the existing protocol block if a ptag is specified, or create
p
= libnet_pblock_probe(l
, ptag
, n
, LIBNET_PBLOCK_BGP4_UPDATE_H
);
/* for memory alignment reason, we need to append each field separately */
length
= htons(unfeasible_rt_len
);
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&length
,
sizeof (unfeasible_rt_len
));
if (unfeasible_rt_len
&& withdrawn_rt
)
n
= libnet_pblock_append(l
, p
, withdrawn_rt
, unfeasible_rt_len
);
length
= htons(total_path_attr_len
);
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&length
,
sizeof (total_path_attr_len
));
if (total_path_attr_len
&& path_attributes
)
n
= libnet_pblock_append(l
, p
, path_attributes
, total_path_attr_len
);
if (info_len
&& reachability_info
)
n
= libnet_pblock_append(l
, p
, reachability_info
, info_len
);
if ((payload
&& !payload_s
) || (!payload
&& payload_s
))
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): payload inconsistency\n", __func__
);
if (payload
&& payload_s
)
n
= libnet_pblock_append(l
, p
, payload
, payload_s
);
/* err msg set in libnet_pblock_append() */
return (ptag
? ptag
: libnet_pblock_update(l
, p
, h
,
LIBNET_PBLOCK_BGP4_UPDATE_H
));
libnet_pblock_delete(l
, p
);
libnet_build_bgp4_notification(u_int8_t err_code
, u_int8_t err_subcode
,
u_int8_t
*payload
, u_int32_t payload_s
, libnet_t
*l
, libnet_ptag_t ptag
)
struct libnet_bgp4_notification_hdr bgp4_hdr
;
n
= LIBNET_BGP4_NOTIFICATION_H
+ + payload_s
; /* size of memory block */
* Find the existing protocol block if a ptag is specified, or create
p
= libnet_pblock_probe(l
, ptag
, n
, LIBNET_PBLOCK_BGP4_NOTIFICATION_H
);
memset(&bgp4_hdr
, 0, sizeof(bgp4_hdr
));
bgp4_hdr
.err_code
= err_code
;
bgp4_hdr
.err_subcode
= err_subcode
;
n
= libnet_pblock_append(l
, p
, (u_int8_t
*)&bgp4_hdr
,
LIBNET_BGP4_NOTIFICATION_H
);
if ((payload
&& !payload_s
) || (!payload
&& payload_s
))
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): payload inconsistency\n", __func__
);
if (payload
&& payload_s
)
n
= libnet_pblock_append(l
, p
, payload
, payload_s
);
/* err msg set in libnet_pblock_append() */
return (ptag
? ptag
: libnet_pblock_update(l
, p
, h
,
LIBNET_PBLOCK_BGP4_NOTIFICATION_H
));
libnet_pblock_delete(l
, p
);