summaryrefslogtreecommitdiff
path: root/src/libnet_write.c
blob: 7d571f2c84080415d4d87758fdb896e4007ddebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 *  libnet
 *  libnet_write.c - writes a prebuilt packet to the network
 *
 *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
 *  All rights reserved.
 *  win32 specific code
 *  Copyright (c) 2002 - 2003 Roberto Larcher <roberto.larcher@libero.it>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 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
 * SUCH DAMAGE.
 *
 */

#include "common.h"

int
libnet_write(libnet_t *l)
{
    int c;
    uint32_t len;
    uint8_t *packet = NULL;

    if (l == NULL)
    {
        return (-1);
    }

    c = libnet_pblock_coalesce(l, &packet, &len);
    if (c == - 1)
    {
        /* err msg set in libnet_pblock_coalesce() */
        return (-1);
    }

    /* assume error */
    c = -1;
    switch (l->injection_type)
    {
        case LIBNET_RAW4:
        case LIBNET_RAW4_ADV:
            if (len > LIBNET_MAX_PACKET)
            {
                snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                        "%s(): packet is too large (%d bytes)",
                        __func__, len);
                goto done;
            }
            c = libnet_write_raw_ipv4(l, packet, len);
            break;
        case LIBNET_RAW6:
        case LIBNET_RAW6_ADV:
            c = libnet_write_raw_ipv6(l, packet, len);
            break;
        case LIBNET_LINK:
        case LIBNET_LINK_ADV:
            c = libnet_write_link(l, packet, len);
            break;
        default:
            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                        "%s(): unsupported injection type", __func__);
            goto done;
    }

    /* do statistics */
    if (c == len)
    {
        l->stats.packets_sent++;
        l->stats.bytes_written += c;
    }
    else
    {
        l->stats.packet_errors++;
        /*
         *  XXX - we probably should have a way to retrieve the number of
         *  bytes actually written (since we might have written something).
         */
        if (c > 0)
        {
            l->stats.bytes_written += c;
        }
    }
done:
    /*
     *  Restore original pointer address so free won't complain about a
     *  modified chunk pointer.
     */
    if (l->aligner > 0)
    {
        packet = packet - l->aligner;
    }
    free(packet);
    return (c);
}

#if defined (__WIN32__)
libnet_ptag_t
libnet_win32_build_fake_ethernet (uint8_t *dst, uint8_t *src, uint16_t type,
                                  const uint8_t *payload, uint32_t payload_s,
                                  uint8_t *packet, libnet_t *l, libnet_ptag_t ptag)
{
    struct libnet_ethernet_hdr eth_hdr;

    if (!packet)
    {
        return (-1);
    }

    memset(&eth_hdr, 0, sizeof(eth_hdr));
    eth_hdr.ether_type = htons(type);
    memcpy(eth_hdr.ether_dhost, dst, ETHER_ADDR_LEN);  /* destination address */
    memcpy(eth_hdr.ether_shost, src, ETHER_ADDR_LEN);  /* source address */

    if (payload && payload_s)
    {
        /*
         *  Unchecked runtime error for buf + ETH_H payload to be greater than
         *  the allocated heap memory.
         */
        memcpy(packet + LIBNET_ETH_H, payload, payload_s);
    }
    memcpy(packet, &eth_hdr, sizeof(eth_hdr));
    return (1);
}

libnet_ptag_t
libnet_win32_build_fake_token (uint8_t *dst, uint8_t *src, uint16_t type,
                               const uint8_t *payload, uint32_t payload_s,
                               uint8_t *packet, libnet_t *l, libnet_ptag_t ptag)
{
    struct libnet_token_ring_hdr token_ring_hdr;

    if (!packet)
    {
        return (-1);
    }

    memset(&token_ring_hdr, 0, sizeof(token_ring_hdr));
    token_ring_hdr.token_ring_access_control    = 0x10;
    token_ring_hdr.token_ring_frame_control     = 0x40;
    token_ring_hdr.token_ring_llc_dsap          = 0xaa;
    token_ring_hdr.token_ring_llc_ssap          = 0xaa;
    token_ring_hdr.token_ring_llc_control_field = 0x03;
    token_ring_hdr.token_ring_type              = htons(type);
    memcpy(token_ring_hdr.token_ring_dhost, dst, ETHER_ADDR_LEN);
    memcpy(token_ring_hdr.token_ring_shost, libnet_get_hwaddr(l),
           ETHER_ADDR_LEN);

    if (payload && payload_s)
    {
        /*
         *  Unchecked runtime error for buf + ETH_H payload to be greater than
         *  the allocated heap memory.
         */
        memcpy(packet + LIBNET_TOKEN_RING_H, payload, payload_s);
    }
    memcpy(packet, &token_ring_hdr, sizeof(token_ring_hdr));
    return (1);
}

int
libnet_win32_write_raw_ipv4(libnet_t *l, const uint8_t *payload, uint32_t payload_s)
{
    static BYTE dst[ETHER_ADDR_LEN];
    static BYTE src[ETHER_ADDR_LEN];

    uint8_t *packet;
    uint32_t packet_s;

    DWORD remoteip = 0;
    NetType type;
    struct libnet_ipv4_hdr *ip_hdr = NULL;

    memset(dst, 0, sizeof(dst));
    memset(src, 0, sizeof(src));

    packet_s = payload_s + l->link_offset;
    packet = (uint8_t*) alloca(packet_s);

    /* we have to do the IP checksum
     * FIXME: warning is correct, checksum modifies its input.
     *        Fix is to build checksum inside the allocated 'packet'
     */
    if (libnet_inet_checksum(l, (uint8_t*)payload, IPPROTO_IP, LIBNET_IPV4_H, payload, payload+payload_s) == -1)
    {
        /* error msg set in libnet_do_checksum */
        return (-1);
    }

    /* MACs, IPs and other stuff... */
    ip_hdr = (struct libnet_ipv4_hdr *)payload;
    memcpy(src, libnet_get_hwaddr(l), sizeof(src));
    remoteip = ip_hdr->ip_dst.S_un.S_addr;

    /* check if the remote station is the local station */
    if (remoteip == libnet_get_ipaddr4(l))
    {
        memcpy(dst, src, sizeof(dst));
    }
    else
    {
        memcpy(dst, libnet_win32_get_remote_mac(l, remoteip), sizeof(dst));
    }

    PacketGetNetType(l->lpAdapter, &type);

    switch(type.LinkType)
    {
        case NdisMedium802_3:
            libnet_win32_build_fake_ethernet(dst, src, ETHERTYPE_IP, payload,
                    payload_s, packet, l , 0);
            break;
        case NdisMedium802_5:
            libnet_win32_build_fake_token(dst, src, ETHERTYPE_IP, payload,
                    payload_s, packet, l, 0);
            break;
        case NdisMediumFddi:
            break;
        case NdisMediumWan:
        case NdisMediumAtm:
        case NdisMediumArcnet878_2:
        default:
            snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): network type (%d) is not supported", __func__,
                type.LinkType);
            return (-1);
    }
    return libnet_write_link (l, packet, packet_s);
}

int
libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size)
{
    return (libnet_win32_write_raw_ipv4(l, packet, size));
}

int
libnet_write_raw_ipv6(libnet_t *l, const uint8_t *packet, uint32_t size)
{
    /* no difference in win32 */
    return (libnet_write_raw_ipv4(l, packet, size));
}

#else /* __WIN32__ */

int
libnet_write_raw_ipv4(libnet_t *l, const uint8_t *packet, uint32_t size)
{
    int c;
    struct sockaddr_in sin;
    struct libnet_ipv4_hdr *ip_hdr;

    if (l == NULL)
    {
        return (-1);
    }

    ip_hdr = (struct libnet_ipv4_hdr *)packet;

#if (LIBNET_BSD_BYTE_SWAP)
    /*
     *  For link access, we don't need to worry about the inconsistencies of
     *  certain BSD kernels.  However, raw socket nuances abound.  Certain
     *  BSD implementations require the ip_len and ip_off fields to be in host
     *  byte order.
     */
    ip_hdr->ip_len = FIX(ip_hdr->ip_len);
    ip_hdr->ip_off = FIX(ip_hdr->ip_off);
#endif /* LIBNET_BSD_BYTE_SWAP */

    memset(&sin, 0, sizeof(sin));
    sin.sin_family  = AF_INET;
    sin.sin_addr.s_addr = ip_hdr->ip_dst.s_addr;

    c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin,
            sizeof(sin));

#if (LIBNET_BSD_BYTE_SWAP)
    ip_hdr->ip_len = UNFIX(ip_hdr->ip_len);
    ip_hdr->ip_off = UNFIX(ip_hdr->ip_off);
#endif /* LIBNET_BSD_BYTE_SWAP */

    if (c != size)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): %d bytes written (%s)", __func__, c,
                strerror(errno));
    }
    return (c);
}

int
libnet_write_raw_ipv6(libnet_t *l, const uint8_t *packet, uint32_t size)
{
    int c = -1;

#if defined HAVE_SOLARIS && !defined HAVE_SOLARIS_IPV6
    snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): no IPv6 support",
            __func__, strerror(errno));
#else
    struct sockaddr_in6 sin;
    struct libnet_ipv6_hdr *ip_hdr;

    if (l == NULL)
    {
        return (-1);
    }

    ip_hdr = (struct libnet_ipv6_hdr *)packet;

    memset(&sin, 0, sizeof(sin));
    sin.sin6_family  = AF_INET6;
    memcpy(sin.sin6_addr.s6_addr, ip_hdr->ip_dst.libnet_s6_addr,
            sizeof(ip_hdr->ip_dst.libnet_s6_addr));

    c = sendto(l->fd, packet, size, 0, (struct sockaddr *)&sin, sizeof(sin));
    if (c != size)
    {
        snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                "%s(): %d bytes written (%s)", __func__, c,
                strerror(errno));
    }
#endif  /* HAVE_SOLARIS && !HAVE_SOLARIS_IPV6 */
    return (c);
}
#endif /* __WIN32__ */