OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / src / libnet_insert_tcpo.c
1 /*
2  *  $Id: libnet_insert_tcpo.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  libnet_insert_tcpo.c - inserts TCP options into a prebuilt IP packet
6  *
7  *  Copyright (c) 1998 - 2001 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 #include "../include/libnet.h"
37
38 int
39 libnet_insert_tcpo(struct tcpoption *opt, u_char opt_len, u_char *buf)
40 {
41     struct libnet_ip_hdr  *ip_hdr;
42     struct libnet_tcp_hdr *tcp_hdr;
43     u_char *p;
44     u_short s, j;
45     u_char i, opt_padded_len;
46     u_char opt_padded_buf[MAX_IPOPTLEN];
47
48     if (!buf)
49     {
50         return (-1);
51     }
52
53     ip_hdr = (struct libnet_ip_hdr *)(buf);
54
55     if (ip_hdr->ip_p != IPPROTO_TCP)
56     {
57         /*
58          *  Hey retard, where's the TCP header?
59          */
60 #if (__DEBUG)
61         libnet_error(LIBNET_ERR_WARNING,
62             "insert_tcpo: tried to insert TCP options into a NON-TCP packet!\n");
63 #endif
64         return (-1);
65     }
66
67     if (opt_len > MAX_IPOPTLEN)
68     {
69         /*
70          *  Hey retard, TCP options can only be 20 octets long!
71          */
72 #if (__DEBUG)
73         libnet_error(LIBNET_ERR_WARNING,
74             "insert_tcpo: options list too long!\n");
75 #endif
76         return (-1);
77     }
78
79      s = ntohs(ip_hdr->ip_len);
80
81     /*
82      *  Figure out length of options with any necessary padding.
83      */
84     if (opt_len % 4)
85     {
86         opt_padded_len = opt_len + 4 - (opt_len % 4);
87     }
88     else
89     {
90         opt_padded_len = opt_len;
91     }
92
93     if ((s + opt_padded_len) > IP_MAXPACKET)
94     {
95         /*
96          *  Nope.  Too big.
97          */
98 #if (__DEBUG)
99         libnet_error(LIBNET_ERR_WARNING,
100             "insert_tcpo: options list would result in too large of a packet\n");
101 #endif
102         return (-1);
103     }
104
105
106     /*
107      *  Fixed pointer math -- now uses ip_hl instead of IP_H to calculate
108      *  offset.
109      */
110     tcp_hdr = (struct libnet_tcp_hdr *)(buf + (ip_hdr->ip_hl << 2));
111
112     /*
113      *  Do we have more then just an TCP header?  (We are ignoring the IP
114      *  header at this point).
115      */
116     if (s > (ip_hdr->ip_hl << 2) + LIBNET_TCP_H)
117     {
118         /*
119          *  Move over whatever's in the way.
120          */
121         memmove((u_char *)tcp_hdr + LIBNET_TCP_H + opt_padded_len,
122                 (u_char *)tcp_hdr + LIBNET_TCP_H,
123                 (s - (ip_hdr->ip_hl << 2) - LIBNET_TCP_H));
124     }
125
126     /*
127      *  Copy over option list.  We rely on the programmer having been
128      *  smart enough to allocate enough heap memory here.  Uh oh.
129      */
130     p = (u_char *)tcp_hdr + LIBNET_TCP_H;
131     memcpy(opt_padded_buf, (u_char *)opt->tcpopt_list, opt_len);
132     memcpy((u_char *)p, opt_padded_buf, opt_padded_len);
133
134     /*
135      *  Count up number of 32-bit words in options list, padding if
136      *  neccessary.
137      */
138     for (i = 0, j = 0; i < opt_padded_len; i++) (i % 4) ? j : j++;
139
140     tcp_hdr->th_off += j;
141     tcp_hdr->th_sum = 0;
142     ip_hdr->ip_len  = htons(opt_padded_len + s); 
143
144     return (1);
145 }
146
147 /* EOF */