1: /*
2: * $Id: libnet_build_ospf.c,v 1.1.1.2 2013/07/22 11:54:42 misho Exp $
3: *
4: * libnet
5: * libnet_build_ospf.c - OSPF packet assembler
6: *
7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8: * All rights reserved.
9: *
10: * Copyright (c) 1999, 2000 Andrew Reiter <areiter@bindview.com>
11: * Bindview Development
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
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.
21: *
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
32: * SUCH DAMAGE.
33: *
34: */
35:
36: #if (HAVE_CONFIG_H)
37: #include "../include/config.h"
38: #endif
39: #if (!(_WIN32) || (__CYGWIN__))
40: #include "../include/libnet.h"
41: #else
42: #include "../include/win32/libnet.h"
43: #endif
44:
45: libnet_ptag_t
46: libnet_build_ospfv2(uint16_t len, uint8_t type, uint32_t rtr_id,
47: uint32_t area_id, uint16_t sum, uint16_t autype, const uint8_t *payload,
48: uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
49: {
50: uint32_t n, h;
51: libnet_pblock_t *p;
52: struct libnet_ospf_hdr ospf_hdr;
53:
54: if (l == NULL)
55: {
56: return (-1);
57: }
58:
59: n = LIBNET_OSPF_H + payload_s;
60: h = LIBNET_OSPF_H + payload_s + len;
61:
62: /*
63: * Find the existing protocol block if a ptag is specified, or create
64: * a new one.
65: */
66: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_OSPF_H);
67: if (p == NULL)
68: {
69: return (-1);
70: }
71:
72: memset(&ospf_hdr, 0, sizeof(ospf_hdr));
73: ospf_hdr.ospf_v = 2; /* OSPF version 2 */
74: ospf_hdr.ospf_type = type; /* Type of pkt */
75: ospf_hdr.ospf_len = htons(h); /* Pkt len */
76: ospf_hdr.ospf_rtr_id.s_addr = rtr_id; /* Router ID */
77: ospf_hdr.ospf_area_id.s_addr = area_id; /* Area ID */
78: ospf_hdr.ospf_sum = sum;
79: ospf_hdr.ospf_auth_type = htons(autype); /* Type of auth */
80:
81: n = libnet_pblock_append(l, p, (uint8_t *)&ospf_hdr, LIBNET_OSPF_H);
82: if (n == -1)
83: {
84: goto bad;
85: }
86:
87: /* boilerplate payload sanity check / append macro */
88: LIBNET_DO_PAYLOAD(l, p);
89:
90: if (sum == 0)
91: {
92: /*
93: * If checksum is zero, by default libnet will compute a checksum
94: * for the user. The programmer can override this by calling
95: * libnet_toggle_checksum(l, ptag, 1);
96: */
97: libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
98: }
99: return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_OSPF_H));
100: bad:
101: libnet_pblock_delete(l, p);
102: return (-1);
103: }
104:
105:
106: libnet_ptag_t
107: libnet_build_ospfv2_hello(uint32_t netmask, uint16_t interval, uint8_t opts,
108: uint8_t priority, uint32_t dead_int, uint32_t des_rtr, uint32_t bkup_rtr,
109: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
110: {
111: uint32_t n, h;
112: libnet_pblock_t *p;
113: struct libnet_ospf_hello_hdr hello_hdr;
114:
115: if (l == NULL)
116: {
117: return (-1);
118: }
119:
120: n = LIBNET_OSPF_HELLO_H + payload_s;
121: h = 0;
122:
123: /*
124: * Find the existing protocol block if a ptag is specified, or create
125: * a new one.
126: */
127: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_OSPF_HELLO_H);
128: if (p == NULL)
129: {
130: return (-1);
131: }
132:
133: memset(&hello_hdr, 0, sizeof(hello_hdr));
134: hello_hdr.hello_nmask.s_addr = netmask; /* Netmask */
135: hello_hdr.hello_intrvl = htons(interval); /* # seconds since last packet sent */
136: hello_hdr.hello_opts = opts; /* OSPF_* options */
137: hello_hdr.hello_rtr_pri = priority; /* If 0, can't be backup */
138: hello_hdr.hello_dead_intvl = htonl(dead_int); /* Time til router is deemed down */
139: hello_hdr.hello_des_rtr.s_addr = des_rtr; /* Networks designated router */
140: hello_hdr.hello_bkup_rtr.s_addr = bkup_rtr; /* Networks backup router */
141: /*hello_hdr.hello_nbr.s_addr = htonl(neighbor); */
142:
143: n = libnet_pblock_append(l, p, (uint8_t *)&hello_hdr, LIBNET_OSPF_HELLO_H);
144: if (n == -1)
145: {
146: goto bad;
147: }
148:
149: /* boilerplate payload sanity check / append macro */
150: LIBNET_DO_PAYLOAD(l, p);
151:
152: return (ptag ? ptag : libnet_pblock_update(l, p, h,
153: LIBNET_PBLOCK_OSPF_HELLO_H));
154: bad:
155: libnet_pblock_delete(l, p);
156: return (-1);
157: }
158:
159:
160: libnet_ptag_t
161: libnet_build_ospfv2_dbd(uint16_t dgram_len, uint8_t opts, uint8_t type,
162: uint32_t seqnum, const uint8_t *payload, uint32_t payload_s, libnet_t *l,
163: libnet_ptag_t ptag)
164: {
165: uint32_t n, h;
166: libnet_pblock_t *p;
167: struct libnet_dbd_hdr dbd_hdr;
168:
169: if (l == NULL)
170: {
171: return (-1);
172: }
173:
174: n = LIBNET_OSPF_DBD_H + payload_s;
175: h = 0;
176:
177: /*
178: * Find the existing protocol block if a ptag is specified, or create
179: * a new one.
180: */
181: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_OSPF_DBD_H);
182: if (p == NULL)
183: {
184: return (-1);
185: }
186:
187: memset(&dbd_hdr, 0, sizeof(dbd_hdr));
188: dbd_hdr.dbd_mtu_len = htons(dgram_len); /* Max length of IP packet IF can use */
189: dbd_hdr.dbd_opts = opts; /* OSPF_* options */
190: dbd_hdr.dbd_type = type; /* Type of exchange occuring */
191: dbd_hdr.dbd_seq = htonl(seqnum); /* DBD sequence number */
192:
193: n = libnet_pblock_append(l, p, (uint8_t *)&dbd_hdr, LIBNET_OSPF_DBD_H);
194: if (n == -1)
195: {
196: goto bad;
197: }
198:
199: /* boilerplate payload sanity check / append macro */
200: LIBNET_DO_PAYLOAD(l, p);
201:
202: return (ptag ? ptag : libnet_pblock_update(l, p, h,
203: LIBNET_PBLOCK_OSPF_DBD_H));
204: bad:
205: libnet_pblock_delete(l, p);
206: return (-1);
207: }
208:
209:
210: libnet_ptag_t
211: libnet_build_ospfv2_lsr(uint32_t type, uint lsid, uint32_t advrtr,
212: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
213: {
214: uint32_t n, h;
215: libnet_pblock_t *p;
216: struct libnet_lsr_hdr lsr_hdr;
217:
218: if (l == NULL)
219: {
220: return (-1);
221: }
222:
223: n = LIBNET_OSPF_LSR_H + payload_s;
224: h = 0;
225:
226: /*
227: * Find the existing protocol block if a ptag is specified, or create
228: * a new one.
229: */
230: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_OSPF_LSR_H);
231: if (p == NULL)
232: {
233: return (-1);
234: }
235:
236: memset(&lsr_hdr, 0, sizeof(lsr_hdr));
237: lsr_hdr.lsr_type = htonl(type); /* Type of LS being requested */
238: lsr_hdr.lsr_lsid = htonl(lsid); /* Link State ID */
239: lsr_hdr.lsr_adrtr.s_addr = htonl(advrtr); /* Advertising router */
240:
241: n = libnet_pblock_append(l, p, (uint8_t *)&lsr_hdr, LIBNET_OSPF_LSR_H);
242: if (n == -1)
243: {
244: goto bad;
245: }
246:
247: /* boilerplate payload sanity check / append macro */
248: LIBNET_DO_PAYLOAD(l, p);
249:
250: return (ptag ? ptag : libnet_pblock_update(l, p, h,
251: LIBNET_PBLOCK_OSPF_LSR_H));
252: bad:
253: libnet_pblock_delete(l, p);
254: return (-1);
255: }
256:
257:
258: libnet_ptag_t
259: libnet_build_ospfv2_lsu(uint32_t num, const uint8_t *payload, uint32_t payload_s,
260: libnet_t *l, libnet_ptag_t ptag)
261: {
262: uint32_t n, h;
263: libnet_pblock_t *p;
264: struct libnet_lsu_hdr lh_hdr;
265:
266: if (l == NULL)
267: {
268: return (-1);
269: }
270:
271: n = LIBNET_OSPF_LSU_H + payload_s;
272: h = 0;
273:
274: /*
275: * Find the existing protocol block if a ptag is specified, or create
276: * a new one.
277: */
278: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_OSPF_LSU_H);
279: if (p == NULL)
280: {
281: return (-1);
282: }
283:
284: memset(&lh_hdr, 0, sizeof(lh_hdr));
285: lh_hdr.lsu_num = htonl(num); /* Number of LSAs that will be bcasted */
286:
287: n = libnet_pblock_append(l, p, (uint8_t *)&lh_hdr, LIBNET_OSPF_LSU_H);
288: if (n == -1)
289: {
290: goto bad;
291: }
292:
293: /* boilerplate payload sanity check / append macro */
294: LIBNET_DO_PAYLOAD(l, p);
295:
296: return (ptag ? ptag : libnet_pblock_update(l, p, h,
297: LIBNET_PBLOCK_OSPF_LSU_H));
298: bad:
299: libnet_pblock_delete(l, p);
300: return (-1);
301: }
302:
303:
304: libnet_ptag_t
305: libnet_build_ospfv2_lsa(uint16_t age, uint8_t opts, uint8_t type, uint lsid,
306: uint32_t advrtr, uint32_t seqnum, uint16_t sum, uint16_t len,
307: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
308: {
309: uint32_t n, h;
310: libnet_pblock_t *p;
311: struct libnet_lsa_hdr lsa_hdr;
312:
313: if (l == NULL)
314: {
315: return (-1);
316: }
317:
318: n = LIBNET_OSPF_LSA_H + payload_s;
319: h = len + payload_s;
320:
321: /*
322: * Find the existing protocol block if a ptag is specified, or create
323: * a new one.
324: */
325: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_OSPF_LSA_H);
326: if (p == NULL)
327: {
328: return (-1);
329: }
330:
331: memset(&lsa_hdr, 0, sizeof(lsa_hdr));
332: lsa_hdr.lsa_age = htons(age);
333: lsa_hdr.lsa_opts = opts;
334: lsa_hdr.lsa_type = type;
335: lsa_hdr.lsa_id = htonl(lsid);
336: lsa_hdr.lsa_adv.s_addr = htonl(advrtr);
337: lsa_hdr.lsa_seq = htonl(seqnum);
338: lsa_hdr.lsa_sum = sum;
339: lsa_hdr.lsa_len = htons(h);
340:
341: n = libnet_pblock_append(l, p, (uint8_t *)&lsa_hdr, LIBNET_OSPF_LSA_H);
342: if (n == -1)
343: {
344: goto bad;
345: }
346:
347: /* boilerplate payload sanity check / append macro */
348: LIBNET_DO_PAYLOAD(l, p);
349:
350: if (sum == 0)
351: {
352: /*
353: * If checksum is zero, by default libnet will compute a checksum
354: * for the user. The programmer can override this by calling
355: * libnet_toggle_checksum(l, ptag, 1);
356: */
357: libnet_pblock_setflags(p, LIBNET_PBLOCK_DO_CHECKSUM);
358: }
359: return (ptag ? ptag : libnet_pblock_update(l, p, h,
360: LIBNET_PBLOCK_OSPF_LSA_H));
361: bad:
362: libnet_pblock_delete(l, p);
363: return (-1);
364: }
365:
366:
367: libnet_ptag_t
368: libnet_build_ospfv2_lsa_rtr(uint16_t flags, uint16_t num, uint32_t id,
369: uint32_t data, uint8_t type, uint8_t tos, uint16_t metric,
370: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
371: {
372: uint32_t n, h;
373: libnet_pblock_t *p;
374: struct libnet_rtr_lsa_hdr rtr_lsa_hdr;
375:
376: if (l == NULL)
377: {
378: return (-1);
379: }
380:
381: n = LIBNET_OSPF_LS_RTR_H + payload_s;
382: h = 0;
383:
384: /*
385: * Find the existing protocol block if a ptag is specified, or create
386: * a new one.
387: */
388: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LS_RTR_H);
389: if (p == NULL)
390: {
391: return (-1);
392: }
393:
394: memset(&rtr_lsa_hdr, 0, sizeof(rtr_lsa_hdr));
395: rtr_lsa_hdr.rtr_flags = htons(flags);
396: rtr_lsa_hdr.rtr_num = htons(num);
397: rtr_lsa_hdr.rtr_link_id = htonl(id);
398: rtr_lsa_hdr.rtr_link_data = htonl(data);
399: rtr_lsa_hdr.rtr_type = type;
400: rtr_lsa_hdr.rtr_tos_num = tos;
401: rtr_lsa_hdr.rtr_metric = htons(metric);
402:
403: n = libnet_pblock_append(l, p, (uint8_t *)&rtr_lsa_hdr,
404: LIBNET_OSPF_LS_RTR_H);
405: if (n == -1)
406: {
407: goto bad;
408: }
409:
410: /* boilerplate payload sanity check / append macro */
411: LIBNET_DO_PAYLOAD(l, p);
412:
413: return (ptag ? ptag : libnet_pblock_update(l, p, h,
414: LIBNET_PBLOCK_LS_RTR_H));
415: bad:
416: libnet_pblock_delete(l, p);
417: return (-1);
418: }
419:
420:
421: libnet_ptag_t
422: libnet_build_ospfv2_lsa_net(uint32_t nmask, uint32_t rtrid,
423: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
424: {
425: uint32_t n, h;
426: libnet_pblock_t *p;
427: struct libnet_net_lsa_hdr net_lsa_hdr;
428:
429: if (l == NULL)
430: {
431: return (-1);
432: }
433:
434: n = LIBNET_OSPF_LS_NET_H + payload_s;
435: h = 0;
436:
437: /*
438: * Find the existing protocol block if a ptag is specified, or create
439: * a new one.
440: */
441: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LS_NET_H);
442: if (p == NULL)
443: {
444: return (-1);
445: }
446:
447: memset(&net_lsa_hdr, 0, sizeof(net_lsa_hdr));
448: net_lsa_hdr.net_nmask.s_addr = htonl(nmask);
449: net_lsa_hdr.net_rtr_id = htonl(rtrid);
450:
451: n = libnet_pblock_append(l, p, (uint8_t *)&net_lsa_hdr,
452: LIBNET_OSPF_LS_NET_H);
453: if (n == -1)
454: {
455: goto bad;
456: }
457:
458: /* boilerplate payload sanity check / append macro */
459: LIBNET_DO_PAYLOAD(l, p);
460:
461: return (ptag ? ptag : libnet_pblock_update(l, p, h,
462: LIBNET_PBLOCK_LS_NET_H));
463: bad:
464: libnet_pblock_delete(l, p);
465: return (-1);
466: }
467:
468:
469: libnet_ptag_t
470: libnet_build_ospfv2_lsa_sum(uint32_t nmask, uint32_t metric, uint tos,
471: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
472: {
473: uint32_t n, h;
474: libnet_pblock_t *p;
475: struct libnet_sum_lsa_hdr sum_lsa_hdr;
476:
477: if (l == NULL)
478: {
479: return (-1);
480: }
481:
482: n = LIBNET_OSPF_LS_SUM_H + payload_s;
483: h = 0;
484:
485: /*
486: * Find the existing protocol block if a ptag is specified, or create
487: * a new one.
488: */
489: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LS_SUM_H);
490: if (p == NULL)
491: {
492: return (-1);
493: }
494:
495: memset(&sum_lsa_hdr, 0, sizeof(sum_lsa_hdr));
496: sum_lsa_hdr.sum_nmask.s_addr = htonl(nmask);
497: sum_lsa_hdr.sum_metric = htonl(metric);
498: sum_lsa_hdr.sum_tos_metric = htonl(tos);
499:
500: n = libnet_pblock_append(l, p, (uint8_t *)&sum_lsa_hdr,
501: LIBNET_OSPF_LS_SUM_H);
502: if (n == -1)
503: {
504: goto bad;
505: }
506:
507: /* boilerplate payload sanity check / append macro */
508: LIBNET_DO_PAYLOAD(l, p);
509:
510: return (ptag ? ptag : libnet_pblock_update(l, p, h,
511: LIBNET_PBLOCK_LS_SUM_H));
512: bad:
513: libnet_pblock_delete(l, p);
514: return (-1);
515: }
516:
517:
518: libnet_ptag_t
519: libnet_build_ospfv2_lsa_as(uint32_t nmask, uint metric, uint32_t fwdaddr,
520: uint32_t tag, const uint8_t *payload, uint32_t payload_s, libnet_t *l,
521: libnet_ptag_t ptag)
522: {
523: uint32_t n, h;
524: libnet_pblock_t *p;
525: struct libnet_as_lsa_hdr as_lsa_hdr;
526:
527: if (l == NULL)
528: {
529: return (-1);
530: }
531:
532: n = LIBNET_OSPF_LS_AS_EXT_H + payload_s;
533: h = 0;
534:
535: /*
536: * Find the existing protocol block if a ptag is specified, or create
537: * a new one.
538: */
539: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_LS_AS_EXT_H);
540: if (p == NULL)
541: {
542: return (-1);
543: }
544:
545: memset(&as_lsa_hdr, 0, sizeof(as_lsa_hdr));
546: as_lsa_hdr.as_nmask.s_addr = htonl(nmask);
547: as_lsa_hdr.as_metric = htonl(metric);
548: as_lsa_hdr.as_fwd_addr.s_addr = htonl(fwdaddr);
549: as_lsa_hdr.as_rte_tag = htonl(tag);
550:
551: n = libnet_pblock_append(l, p, (uint8_t *)&as_lsa_hdr,
552: LIBNET_OSPF_LS_AS_EXT_H);
553: if (n == -1)
554: {
555: goto bad;
556: }
557:
558: /* boilerplate payload sanity check / append macro */
559: LIBNET_DO_PAYLOAD(l, p);
560:
561: return (ptag ? ptag : libnet_pblock_update(l, p, h,
562: LIBNET_PBLOCK_LS_AS_EXT_H));
563: bad:
564: libnet_pblock_delete(l, p);
565: return (-1);
566: }
567:
568: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>