| 1 | /* |
| 2 | * libnet |
| 3 | * libnet_build_fddi.c - Fiber Distributed Data Interface packet assembler |
| 4 | * |
| 5 | * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com> |
| 6 | * Jason Damron <jsdamron@hushmail.com> |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #if (HAVE_CONFIG_H) |
| 33 | #include "../include/config.h" |
| 34 | #endif |
| 35 | #if (!(_WIN32) || (__CYGWIN__)) |
| 36 | #include "../include/libnet.h" |
| 37 | #else |
| 38 | #include "../include/win32/libnet.h" |
| 39 | #endif |
| 40 | |
| 41 | libnet_ptag_t |
| 42 | libnet_build_fddi(u_int8_t fc, u_int8_t *dst, u_int8_t *src, u_int8_t dsap, |
| 43 | u_int8_t ssap, u_int8_t cf, u_int8_t *org, u_int16_t type, u_int8_t *payload, |
| 44 | u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag) |
| 45 | { |
| 46 | u_int32_t n, h; |
| 47 | u_int16_t protocol_type; |
| 48 | libnet_pblock_t *p; |
| 49 | struct libnet_fddi_hdr fddi_hdr; |
| 50 | |
| 51 | if (l == NULL) |
| 52 | { |
| 53 | return (-1); |
| 54 | } |
| 55 | |
| 56 | /* sanity check injection type if we're not in advanced mode */ |
| 57 | if (l->injection_type != LIBNET_LINK && |
| 58 | !(((l->injection_type) & LIBNET_ADV_MASK))) |
| 59 | { |
| 60 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 61 | "%s(): called with non-link layer wire injection primitive", |
| 62 | __func__); |
| 63 | p = NULL; |
| 64 | goto bad; |
| 65 | } |
| 66 | |
| 67 | n = LIBNET_FDDI_H + payload_s; |
| 68 | h = 0; |
| 69 | |
| 70 | /* |
| 71 | * Find the existing protocol block if a ptag is specified, or create |
| 72 | * a new one. |
| 73 | */ |
| 74 | p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_FDDI_H); |
| 75 | if (p == NULL) |
| 76 | { |
| 77 | return (-1); |
| 78 | } |
| 79 | |
| 80 | memset(&fddi_hdr, 0, sizeof(fddi_hdr)); |
| 81 | fddi_hdr.fddi_frame_control = fc; /* Class/Format/Priority */ |
| 82 | memcpy(fddi_hdr.fddi_dhost, dst, FDDI_ADDR_LEN); /* dst fddi address */ |
| 83 | memcpy(fddi_hdr.fddi_shost, src, FDDI_ADDR_LEN); /* source fddi address */ |
| 84 | fddi_hdr.fddi_llc_dsap = dsap; /* */ |
| 85 | fddi_hdr.fddi_llc_ssap = ssap; /* */ |
| 86 | fddi_hdr.fddi_llc_control_field = cf; /* Class/Format/Priority */ |
| 87 | memcpy(&fddi_hdr.fddi_llc_org_code, org, LIBNET_ORG_CODE_SIZE); |
| 88 | |
| 89 | /* Deal with unaligned int16_t for type */ |
| 90 | protocol_type = htons(type); |
| 91 | memcpy(&fddi_hdr.fddi_type, &protocol_type, sizeof(int16_t)); /* Protocol Type */ |
| 92 | |
| 93 | n = libnet_pblock_append(l, p, (u_int8_t *)&fddi_hdr, LIBNET_FDDI_H); |
| 94 | if (n == -1) |
| 95 | { |
| 96 | goto bad; |
| 97 | } |
| 98 | |
| 99 | if ((payload && !payload_s) || (!payload && payload_s)) |
| 100 | { |
| 101 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 102 | "%s(): payload inconsistency\n", __func__); |
| 103 | goto bad; |
| 104 | } |
| 105 | |
| 106 | if (payload && payload_s) |
| 107 | { |
| 108 | n = libnet_pblock_append(l, p, payload, payload_s); |
| 109 | if (n == -1) |
| 110 | { |
| 111 | goto bad; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_FDDI_H)); |
| 116 | bad: |
| 117 | libnet_pblock_delete(l, p); |
| 118 | return (-1); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | libnet_ptag_t |
| 123 | libnet_autobuild_fddi(u_int8_t fc, u_int8_t *dst, u_int8_t dsap, u_int8_t ssap, |
| 124 | u_int8_t cf, u_int8_t *org, u_int16_t type, libnet_t *l) |
| 125 | { |
| 126 | u_int32_t n, h; |
| 127 | u_int16_t protocol_type; |
| 128 | struct libnet_fddi_addr *src; |
| 129 | libnet_pblock_t *p; |
| 130 | libnet_ptag_t ptag; |
| 131 | struct libnet_fddi_hdr fddi_hdr; |
| 132 | |
| 133 | if (l == NULL) |
| 134 | { |
| 135 | return (-1); |
| 136 | } |
| 137 | |
| 138 | /* sanity check injection type if we're not in advanced mode */ |
| 139 | if (l->injection_type != LIBNET_LINK && |
| 140 | !(((l->injection_type) & LIBNET_ADV_MASK))) |
| 141 | { |
| 142 | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| 143 | "%s(): called with non-link layer wire injection primitive", |
| 144 | __func__); |
| 145 | p = NULL; |
| 146 | goto bad; |
| 147 | } |
| 148 | |
| 149 | n = LIBNET_FDDI_H; |
| 150 | h = 0; |
| 151 | ptag = LIBNET_PTAG_INITIALIZER; |
| 152 | |
| 153 | /* FDDI and Ethernet have the same address size - so just typecast */ |
| 154 | src = (struct libnet_fddi_addr *) libnet_get_hwaddr(l); |
| 155 | if (src == NULL) |
| 156 | { |
| 157 | /* err msg set in libnet_get_hwaddr() */ |
| 158 | return (-1); |
| 159 | } |
| 160 | |
| 161 | p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_FDDI_H); |
| 162 | if (p == NULL) |
| 163 | { |
| 164 | return (-1); |
| 165 | } |
| 166 | |
| 167 | memset(&fddi_hdr, 0, sizeof(fddi_hdr)); |
| 168 | fddi_hdr.fddi_frame_control = fc; /* Class/Format/Priority */ |
| 169 | memcpy(fddi_hdr.fddi_dhost, dst, FDDI_ADDR_LEN); /* destination fddi address */ |
| 170 | memcpy(fddi_hdr.fddi_shost, src, FDDI_ADDR_LEN); /* source fddi address */ |
| 171 | fddi_hdr.fddi_llc_dsap = dsap; /* */ |
| 172 | fddi_hdr.fddi_llc_ssap = ssap; /* */ |
| 173 | fddi_hdr.fddi_llc_control_field = cf; /* Class/Format/Priority */ |
| 174 | memcpy(&fddi_hdr.fddi_llc_org_code, org, LIBNET_ORG_CODE_SIZE); |
| 175 | |
| 176 | /* Deal with unaligned int16_t for type */ |
| 177 | protocol_type = htons(type); |
| 178 | memcpy(&fddi_hdr.fddi_type, &protocol_type, sizeof(int16_t)); /* Protocol Type */ |
| 179 | |
| 180 | n = libnet_pblock_append(l, p, (u_int8_t *)&fddi_hdr, LIBNET_FDDI_H); |
| 181 | if (n == -1) |
| 182 | { |
| 183 | goto bad; |
| 184 | } |
| 185 | |
| 186 | return (libnet_pblock_update(l, p, h, LIBNET_PBLOCK_FDDI_H)); |
| 187 | bad: |
| 188 | libnet_pblock_delete(l, p); |
| 189 | return (-1); |
| 190 | } |
| 191 | /* EOF */ |