2 * $Id: libnet_build_ospf.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_build_ospf.c - OSPF packet assembler
7 * Copyright (c) 1999, 2000 Andrew Reiter <areiter@bindview.com>
10 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "../include/config.h"
39 #include "../include/libnet.h"
43 libnet_build_ospf(u_short len, u_char type, u_long rtr_id, u_long area_id,
44 u_short autype, const char *payload, int payload_s,
48 struct libnet_ospf_hdr ospf_hdr;
55 ospf_hdr.ospf_v = 2; /* OSPF version 2 */
56 ospf_hdr.ospf_type = type; /* Type of pkt */
57 ospf_hdr.ospf_len = len + LIBNET_OSPF_H;/* Pkt len */
58 ospf_hdr.ospf_rtr_id.s_addr = rtr_id; /* Router ID */
59 ospf_hdr.ospf_area_id.s_addr = area_id; /* Area ID */
60 ospf_hdr.ospf_cksum = 0; /* Fix later */
61 ospf_hdr.ospf_auth_type = autype; /* Type of auth */
63 if (payload && payload_s)
66 * Unchecked runtime error for buf + OSPF_H + payload to be greater
67 * than the allocated heap memory.
69 memcpy(buf + LIBNET_OSPF_H, payload, payload_s);
71 memcpy((u_char *)buf, (u_char *)&ospf_hdr, sizeof(ospf_hdr));
76 libnet_build_ospf_hello(u_long netmask, u_short interval, u_char opts,
77 u_char priority, u_int dead_int, u_long des_rtr, u_long bkup_rtr,
78 u_long neighbor, const char *payload, int payload_s, u_char *buf)
80 struct libnet_ospf_hello_hdr hello_hdr;
87 hello_hdr.hello_nmask.s_addr = netmask; /* Netmask */
88 hello_hdr.hello_intrvl = interval; /* # seconds since last packet sent */
89 hello_hdr.hello_opts = opts; /* OSPF_* options */
90 hello_hdr.hello_rtr_pri = priority; /* If 0, can't be backup */
91 hello_hdr.hello_dead_intvl = dead_int; /* Time til router is deemed down */
92 hello_hdr.hello_des_rtr.s_addr = des_rtr; /* Networks designated router */
93 hello_hdr.hello_bkup_rtr.s_addr = bkup_rtr; /* Networks backup router */
94 hello_hdr.hello_nbr.s_addr = neighbor;
96 if (payload && payload_s)
98 memcpy((u_char *)buf + LIBNET_HELLO_H, (u_char *)&payload, payload_s);
100 memcpy((u_char *)buf, (u_char *)&hello_hdr, sizeof(hello_hdr));
105 libnet_build_ospf_dbd(u_short dgram_len, u_char opts, u_char type, u_int seqnum,
106 const char *payload, int payload_s, u_char *buf)
108 struct libnet_dbd_hdr dbd_hdr;
115 dbd_hdr.dbd_mtu_len = dgram_len; /* Max length of IP packet IF can use */
116 dbd_hdr.dbd_opts = opts; /* OSPF_* options */
117 dbd_hdr.dbd_type = type; /* Type of exchange occuring */
118 dbd_hdr.dbd_seq = seqnum; /* DBD sequence number */
120 if (payload && payload_s)
122 memcpy((u_char *)buf + LIBNET_DBD_H, payload, payload_s);
125 memcpy((u_char *)buf, (u_char *)&dbd_hdr, sizeof(dbd_hdr));
130 libnet_build_ospf_lsr(u_int type, u_int lsid, u_long advrtr,
131 const char *payload, int payload_s, u_char *buf)
133 struct libnet_lsr_hdr lsr_hdr;
140 lsr_hdr.lsr_type = type; /* Type of LS being requested */
141 lsr_hdr.lsr_lsid = lsid; /* Link State ID */
142 lsr_hdr.lsr_adrtr.s_addr = advrtr; /* Advertising router */
144 if (payload && payload_s)
146 memcpy((u_char *)buf + LIBNET_LSR_H, payload, payload_s);
149 memcpy((u_char *)buf, (u_char *)&lsr_hdr, sizeof(lsr_hdr));
154 libnet_build_ospf_lsu(u_int num, const char *payload, int payload_s,
157 struct libnet_lsu_hdr lh_hdr;
164 lh_hdr.lsu_num = num; /* Number of LSAs that will be bcasted */
166 if (payload && payload_s)
168 memcpy((u_char *)buf + LIBNET_LSU_H, payload, payload_s);
171 memcpy((u_char *)buf, (u_char *)&lh_hdr, sizeof(lh_hdr));
176 libnet_build_ospf_lsa(u_short age, u_char opts, u_char type, u_int lsid,
177 u_long advrtr, u_int seqnum, u_short len, const char *payload,
178 int payload_s, u_char *buf)
180 struct libnet_lsa_hdr lsa_hdr;
187 lsa_hdr.lsa_age = age;
188 lsa_hdr.lsa_opts = opts;
189 lsa_hdr.lsa_type = type;
190 lsa_hdr.lsa_id = lsid;
191 lsa_hdr.lsa_adv.s_addr = advrtr;
192 lsa_hdr.lsa_seq = seqnum;
193 lsa_hdr.lsa_cksum[0] = 0;
194 lsa_hdr.lsa_cksum[1] = 0;
195 lsa_hdr.lsa_len = len + LIBNET_LSA_H;
197 if (payload && payload_s)
199 memcpy((u_char *)buf + LIBNET_LSA_H, payload, payload_s);
202 memcpy((u_char *)buf, (u_char *)&lsa_hdr, sizeof(lsa_hdr));
207 libnet_build_ospf_lsa_rtr(u_short flags, u_short num, u_int id, u_int data,
208 u_char type, u_char tos, u_short metric, const char *payload,
209 int payload_s, u_char *buf)
211 struct libnet_rtr_lsa_hdr rtr_lsa_hdr;
218 rtr_lsa_hdr.rtr_flags = flags;
219 rtr_lsa_hdr.rtr_num = num;
220 rtr_lsa_hdr.rtr_link_id = id;
221 rtr_lsa_hdr.rtr_link_data = data;
222 rtr_lsa_hdr.rtr_type = type;
223 rtr_lsa_hdr.rtr_tos_num = tos;
224 rtr_lsa_hdr.rtr_metric = metric;
226 if (payload && payload_s)
228 memcpy((u_char *)buf + LIBNET_LS_RTR_LEN, payload, payload_s);
231 memcpy((u_char *)buf, (u_char *)&rtr_lsa_hdr, sizeof(rtr_lsa_hdr));
236 libnet_build_ospf_lsa_net(u_long nmask, u_int rtrid, const char *payload,
237 int payload_s, u_char *buf)
239 struct libnet_net_lsa_hdr net_lsa_hdr;
246 net_lsa_hdr.net_nmask.s_addr = nmask;
247 net_lsa_hdr.net_rtr_id = rtrid;
249 if (payload && payload_s)
251 memcpy((u_char *)buf + LIBNET_LS_NET_LEN, payload, payload_s);
254 memcpy((u_char *)buf, (u_char *)&net_lsa_hdr, sizeof(net_lsa_hdr));
259 libnet_build_ospf_lsa_sum(u_long nmask, u_int metric, u_int tos,
260 const char *payload, int payload_s, u_char *buf)
262 struct libnet_sum_lsa_hdr sum_lsa_hdr;
269 sum_lsa_hdr.sum_nmask.s_addr = nmask;
270 sum_lsa_hdr.sum_metric = metric;
271 sum_lsa_hdr.sum_tos_metric = tos;
273 if (payload && payload_s)
275 memcpy((u_char *)buf + LIBNET_LS_SUM_LEN, payload, payload_s);
278 memcpy((u_char *)buf, (u_char *)&sum_lsa_hdr, sizeof(sum_lsa_hdr));
283 libnet_build_ospf_lsa_as(u_long nmask, u_int metric, u_long fwdaddr, u_int tag,
284 const char *payload, int payload_s, u_char *buf)
286 struct libnet_as_lsa_hdr as_lsa_hdr;
293 as_lsa_hdr.as_nmask.s_addr = nmask;
294 as_lsa_hdr.as_metric = metric;
295 as_lsa_hdr.as_fwd_addr.s_addr = fwdaddr;
296 as_lsa_hdr.as_rte_tag = tag;
298 if (payload && payload_s)
300 memcpy((u_char *)buf + LIBNET_LS_AS_EXT_LEN, payload, payload_s);
303 memcpy((u_char *)buf, (u_char *)&as_lsa_hdr, sizeof(as_lsa_hdr));