1 /*
2 * $Id: libnet_build_stp.c,v 1.9 2004/04/13 17:32:28 mike Exp $
3 *
4 * libnet
5 * libnet_build_stp.c - STP packet assembler
6 *
7 * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 #if (HAVE_CONFIG_H)
34 #include "../include/config.h"
35 #endif
36 #if (!(_WIN32) || (__CYGWIN__))
37 #include "../include/libnet.h"
38 #else
39 #include "../include/win32/libnet.h"
40 #endif
41
42 libnet_ptag_t
libnet_build_stp_conf(uint16_t id,uint8_t version,uint8_t bpdu_type,uint8_t flags,const uint8_t * root_id,uint32_t root_pc,const uint8_t * bridge_id,uint16_t port_id,uint16_t message_age,uint16_t max_age,uint16_t hello_time,uint16_t f_delay,const uint8_t * payload,uint32_t payload_s,libnet_t * l,libnet_ptag_t ptag)43 libnet_build_stp_conf(uint16_t id, uint8_t version, uint8_t bpdu_type,
44 uint8_t flags, const uint8_t *root_id, uint32_t root_pc, const uint8_t *bridge_id,
45 uint16_t port_id, uint16_t message_age, uint16_t max_age,
46 uint16_t hello_time, uint16_t f_delay, const uint8_t *payload,
47 uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
48 {
49 uint32_t n, h;
50 libnet_pblock_t *p;
51
52 /* until we get some data marshalling in place we can't use this */
53 /* struct libnet_stp_conf_hdr stp_hdr; */
54 uint8_t stp_hdr[35];
55 uint16_t value_s;
56 uint32_t value_l;
57
58 if (l == NULL)
59 {
60 return (-1);
61 }
62
63 n = LIBNET_STP_CONF_H + payload_s; /* size of memory block */
64 h = 0; /* no checksum */
65
66 /*
67 * Find the existing protocol block if a ptag is specified, or create
68 * a new one.
69 */
70 p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_CONF_H);
71 if (p == NULL)
72 {
73 return (-1);
74 }
75
76 /* until we get some data marshalling in place we can't use this */
77 /*
78 stp_hdr.stp_id = htons(id);
79 stp_hdr.stp_version = version;
80 stp_hdr.stp_bpdu_type = bpdu_type;
81 stp_hdr.stp_flags = flags;
82 memcpy(&stp_hdr.stp_rootid, root_id, 8);
83 stp_hdr.stp_rootpc = htonl(root_pc);
84 memcpy(&stp_hdr.stp_bridgeid, bridge_id, 8);
85 stp_hdr.stp_portid = htons(port_id);
86 stp_hdr.stp_mage = htons(message_age);
87 stp_hdr.stp_maxage = htons(max_age);
88 stp_hdr.stp_hellot = htons(hello_time);
89 stp_hdr.stp_fdelay = htons(f_delay);
90 */
91
92 value_s = htons(id);
93 memcpy(stp_hdr, &value_s, 2);
94 stp_hdr[2] = version;
95 stp_hdr[3] = bpdu_type;
96 stp_hdr[4] = flags;
97 memcpy(&stp_hdr[5], root_id, 8);
98 value_l = htonl(root_pc);
99 memcpy(&stp_hdr[13], &value_l, 4);
100 memcpy(&stp_hdr[17], bridge_id, 8);
101 value_s = htons(port_id);
102 memcpy(&stp_hdr[25], &value_s, 2);
103 #if (LIBNET_BIG_ENDIAN == 1)
104 value_s = htons(message_age);
105 #else
106 value_s = message_age;
107 #endif
108 memcpy(&stp_hdr[27], &value_s, 2);
109 #if (LIBNET_BIG_ENDIAN == 1)
110 value_s = htons(max_age);
111 #else
112 value_s = max_age;
113 #endif
114 memcpy(&stp_hdr[29], &value_s, 2);
115 #if (LIBNET_BIG_ENDIAN == 1)
116 value_s = htons(hello_time);
117 #else
118 value_s = hello_time;
119 #endif
120 memcpy(&stp_hdr[31], &value_s, 2);
121 #if (LIBNET_BIG_ENDIAN == 1)
122 value_s = htons(f_delay);
123 #else
124 value_s = f_delay;
125 #endif
126 memcpy(&stp_hdr[33], &value_s, 2);
127
128
129 /* until we get some data marshalling in place we can't use this */
130 /*n = libnet_pblock_append(l, p, (uint8_t *)&stp_hdr, LIBNET_STP_CONF_H); */
131 n = libnet_pblock_append(l, p, stp_hdr, LIBNET_STP_CONF_H);
132 if (n == -1)
133 {
134 goto bad;
135 }
136
137 /* boilerplate payload sanity check / append macro */
138 LIBNET_DO_PAYLOAD(l, p);
139
140 return (ptag ? ptag : libnet_pblock_update(l, p, h,
141 LIBNET_PBLOCK_STP_CONF_H));
142 bad:
143 libnet_pblock_delete(l, p);
144 return (-1);
145 }
146
147
148 libnet_ptag_t
libnet_build_stp_tcn(uint16_t id,uint8_t version,uint8_t bpdu_type,const uint8_t * payload,uint32_t payload_s,libnet_t * l,libnet_ptag_t ptag)149 libnet_build_stp_tcn(uint16_t id, uint8_t version, uint8_t bpdu_type,
150 const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
151 {
152 uint32_t n, h;
153 libnet_pblock_t *p;
154
155 struct libnet_stp_tcn_hdr stp_hdr;
156
157 if (l == NULL)
158 {
159 return (-1);
160 }
161
162 n = LIBNET_STP_TCN_H + payload_s; /* size of memory block */
163 h = 0; /* no checksum */
164
165 /*
166 * Find the existing protocol block if a ptag is specified, or create
167 * a new one.
168 */
169 p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_STP_TCN_H);
170 if (p == NULL)
171 {
172 return (-1);
173 }
174
175 memset(&stp_hdr, 0, sizeof(stp_hdr));
176 stp_hdr.stp_id = htons(id);
177 stp_hdr.stp_version = version;
178 stp_hdr.stp_bpdu_type = bpdu_type;
179
180 n = libnet_pblock_append(l, p, (uint8_t *)&stp_hdr, LIBNET_STP_TCN_H);
181 if (n == -1)
182 {
183 goto bad;
184 }
185
186 /* boilerplate payload sanity check / append macro */
187 LIBNET_DO_PAYLOAD(l, p);
188
189 return (ptag ? ptag : libnet_pblock_update(l, p, h,
190 LIBNET_PBLOCK_STP_TCN_H));
191 bad:
192 libnet_pblock_delete(l, p);
193 return (-1);
194 }
195
196 /* EOF */
197