root / slirp / ip_output.c @ 651721b2
History | View | Annotate | Download (5.4 kB)
1 | f0cbd3ec | bellard | /*
|
---|---|---|---|
2 | f0cbd3ec | bellard | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
3 | f0cbd3ec | bellard | * The Regents of the University of California. All rights reserved.
|
4 | f0cbd3ec | bellard | *
|
5 | f0cbd3ec | bellard | * Redistribution and use in source and binary forms, with or without
|
6 | f0cbd3ec | bellard | * modification, are permitted provided that the following conditions
|
7 | f0cbd3ec | bellard | * are met:
|
8 | f0cbd3ec | bellard | * 1. Redistributions of source code must retain the above copyright
|
9 | f0cbd3ec | bellard | * notice, this list of conditions and the following disclaimer.
|
10 | f0cbd3ec | bellard | * 2. Redistributions in binary form must reproduce the above copyright
|
11 | f0cbd3ec | bellard | * notice, this list of conditions and the following disclaimer in the
|
12 | f0cbd3ec | bellard | * documentation and/or other materials provided with the distribution.
|
13 | 2f5f8996 | aliguori | * 3. Neither the name of the University nor the names of its contributors
|
14 | f0cbd3ec | bellard | * may be used to endorse or promote products derived from this software
|
15 | f0cbd3ec | bellard | * without specific prior written permission.
|
16 | f0cbd3ec | bellard | *
|
17 | f0cbd3ec | bellard | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
18 | f0cbd3ec | bellard | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 | f0cbd3ec | bellard | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 | f0cbd3ec | bellard | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
21 | f0cbd3ec | bellard | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22 | f0cbd3ec | bellard | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
23 | f0cbd3ec | bellard | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
24 | f0cbd3ec | bellard | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | f0cbd3ec | bellard | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
26 | f0cbd3ec | bellard | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 | f0cbd3ec | bellard | * SUCH DAMAGE.
|
28 | f0cbd3ec | bellard | *
|
29 | f0cbd3ec | bellard | * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
|
30 | f0cbd3ec | bellard | * ip_output.c,v 1.9 1994/11/16 10:17:10 jkh Exp
|
31 | f0cbd3ec | bellard | */
|
32 | f0cbd3ec | bellard | |
33 | f0cbd3ec | bellard | /*
|
34 | f0cbd3ec | bellard | * Changes and additions relating to SLiRP are
|
35 | f0cbd3ec | bellard | * Copyright (c) 1995 Danny Gasparovski.
|
36 | f0cbd3ec | bellard | *
|
37 | f0cbd3ec | bellard | * Please read the file COPYRIGHT for the
|
38 | f0cbd3ec | bellard | * terms and conditions of the copyright.
|
39 | f0cbd3ec | bellard | */
|
40 | f0cbd3ec | bellard | |
41 | f0cbd3ec | bellard | #include <slirp.h> |
42 | f0cbd3ec | bellard | |
43 | f0cbd3ec | bellard | u_int16_t ip_id; |
44 | f0cbd3ec | bellard | |
45 | 7878ff6b | blueswir1 | /* Number of packets queued before we start sending
|
46 | 7878ff6b | blueswir1 | * (to prevent allocing too many mbufs) */
|
47 | 7878ff6b | blueswir1 | #define IF_THRESH 10 |
48 | 7878ff6b | blueswir1 | |
49 | f0cbd3ec | bellard | /*
|
50 | f0cbd3ec | bellard | * IP output. The packet in mbuf chain m contains a skeletal IP
|
51 | f0cbd3ec | bellard | * header (with len, off, ttl, proto, tos, src, dst).
|
52 | f0cbd3ec | bellard | * The mbuf chain containing the packet will be freed.
|
53 | f0cbd3ec | bellard | * The mbuf opt, if present, will not be freed.
|
54 | f0cbd3ec | bellard | */
|
55 | f0cbd3ec | bellard | int
|
56 | 511d2b14 | blueswir1 | ip_output(struct socket *so, struct mbuf *m0) |
57 | f0cbd3ec | bellard | { |
58 | f0cbd3ec | bellard | register struct ip *ip; |
59 | f0cbd3ec | bellard | register struct mbuf *m = m0; |
60 | f0cbd3ec | bellard | register int hlen = sizeof(struct ip ); |
61 | f0cbd3ec | bellard | int len, off, error = 0; |
62 | f0cbd3ec | bellard | |
63 | f0cbd3ec | bellard | DEBUG_CALL("ip_output");
|
64 | f0cbd3ec | bellard | DEBUG_ARG("so = %lx", (long)so); |
65 | f0cbd3ec | bellard | DEBUG_ARG("m0 = %lx", (long)m0); |
66 | 5fafdf24 | ths | |
67 | f0cbd3ec | bellard | /* We do no options */
|
68 | f0cbd3ec | bellard | /* if (opt) {
|
69 | f0cbd3ec | bellard | * m = ip_insertoptions(m, opt, &len);
|
70 | f0cbd3ec | bellard | * hlen = len;
|
71 | f0cbd3ec | bellard | * }
|
72 | f0cbd3ec | bellard | */
|
73 | f0cbd3ec | bellard | ip = mtod(m, struct ip *);
|
74 | f0cbd3ec | bellard | /*
|
75 | f0cbd3ec | bellard | * Fill in IP header.
|
76 | f0cbd3ec | bellard | */
|
77 | f0cbd3ec | bellard | ip->ip_v = IPVERSION; |
78 | f0cbd3ec | bellard | ip->ip_off &= IP_DF; |
79 | f0cbd3ec | bellard | ip->ip_id = htons(ip_id++); |
80 | f0cbd3ec | bellard | ip->ip_hl = hlen >> 2;
|
81 | 31a60e22 | blueswir1 | STAT(ipstat.ips_localout++); |
82 | f0cbd3ec | bellard | |
83 | f0cbd3ec | bellard | /*
|
84 | f0cbd3ec | bellard | * Verify that we have any chance at all of being able to queue
|
85 | f0cbd3ec | bellard | * the packet or packet fragments
|
86 | f0cbd3ec | bellard | */
|
87 | f0cbd3ec | bellard | /* XXX Hmmm... */
|
88 | 7878ff6b | blueswir1 | /* if (if_queued > IF_THRESH && towrite <= 0) {
|
89 | f0cbd3ec | bellard | * error = ENOBUFS;
|
90 | f0cbd3ec | bellard | * goto bad;
|
91 | f0cbd3ec | bellard | * }
|
92 | f0cbd3ec | bellard | */
|
93 | 5fafdf24 | ths | |
94 | f0cbd3ec | bellard | /*
|
95 | f0cbd3ec | bellard | * If small enough for interface, can just send directly.
|
96 | f0cbd3ec | bellard | */
|
97 | 9634d903 | blueswir1 | if ((u_int16_t)ip->ip_len <= IF_MTU) {
|
98 | f0cbd3ec | bellard | ip->ip_len = htons((u_int16_t)ip->ip_len); |
99 | f0cbd3ec | bellard | ip->ip_off = htons((u_int16_t)ip->ip_off); |
100 | f0cbd3ec | bellard | ip->ip_sum = 0;
|
101 | f0cbd3ec | bellard | ip->ip_sum = cksum(m, hlen); |
102 | f0cbd3ec | bellard | |
103 | f0cbd3ec | bellard | if_output(so, m); |
104 | f0cbd3ec | bellard | goto done;
|
105 | f0cbd3ec | bellard | } |
106 | f0cbd3ec | bellard | |
107 | f0cbd3ec | bellard | /*
|
108 | f0cbd3ec | bellard | * Too large for interface; fragment if possible.
|
109 | f0cbd3ec | bellard | * Must be able to put at least 8 bytes per fragment.
|
110 | f0cbd3ec | bellard | */
|
111 | f0cbd3ec | bellard | if (ip->ip_off & IP_DF) {
|
112 | f0cbd3ec | bellard | error = -1;
|
113 | 31a60e22 | blueswir1 | STAT(ipstat.ips_cantfrag++); |
114 | f0cbd3ec | bellard | goto bad;
|
115 | f0cbd3ec | bellard | } |
116 | 5fafdf24 | ths | |
117 | 9634d903 | blueswir1 | len = (IF_MTU - hlen) &~ 7; /* ip databytes per packet */ |
118 | f0cbd3ec | bellard | if (len < 8) { |
119 | f0cbd3ec | bellard | error = -1;
|
120 | f0cbd3ec | bellard | goto bad;
|
121 | f0cbd3ec | bellard | } |
122 | f0cbd3ec | bellard | |
123 | f0cbd3ec | bellard | { |
124 | f0cbd3ec | bellard | int mhlen, firstlen = len;
|
125 | f0cbd3ec | bellard | struct mbuf **mnext = &m->m_nextpkt;
|
126 | f0cbd3ec | bellard | |
127 | f0cbd3ec | bellard | /*
|
128 | f0cbd3ec | bellard | * Loop through length of segment after first fragment,
|
129 | f0cbd3ec | bellard | * make new header and copy data of each part and link onto chain.
|
130 | f0cbd3ec | bellard | */
|
131 | f0cbd3ec | bellard | m0 = m; |
132 | f0cbd3ec | bellard | mhlen = sizeof (struct ip); |
133 | f0cbd3ec | bellard | for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
|
134 | f0cbd3ec | bellard | register struct ip *mhip; |
135 | f0cbd3ec | bellard | m = m_get(); |
136 | 511d2b14 | blueswir1 | if (m == NULL) { |
137 | f0cbd3ec | bellard | error = -1;
|
138 | 31a60e22 | blueswir1 | STAT(ipstat.ips_odropped++); |
139 | f0cbd3ec | bellard | goto sendorfree;
|
140 | f0cbd3ec | bellard | } |
141 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
142 | f0cbd3ec | bellard | mhip = mtod(m, struct ip *);
|
143 | f0cbd3ec | bellard | *mhip = *ip; |
144 | 3b46e624 | ths | |
145 | f0cbd3ec | bellard | /* No options */
|
146 | f0cbd3ec | bellard | /* if (hlen > sizeof (struct ip)) {
|
147 | f0cbd3ec | bellard | * mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
|
148 | f0cbd3ec | bellard | * mhip->ip_hl = mhlen >> 2;
|
149 | f0cbd3ec | bellard | * }
|
150 | f0cbd3ec | bellard | */
|
151 | f0cbd3ec | bellard | m->m_len = mhlen; |
152 | f0cbd3ec | bellard | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
|
153 | f0cbd3ec | bellard | if (ip->ip_off & IP_MF)
|
154 | f0cbd3ec | bellard | mhip->ip_off |= IP_MF; |
155 | f0cbd3ec | bellard | if (off + len >= (u_int16_t)ip->ip_len)
|
156 | f0cbd3ec | bellard | len = (u_int16_t)ip->ip_len - off; |
157 | 5fafdf24 | ths | else
|
158 | f0cbd3ec | bellard | mhip->ip_off |= IP_MF; |
159 | f0cbd3ec | bellard | mhip->ip_len = htons((u_int16_t)(len + mhlen)); |
160 | 3b46e624 | ths | |
161 | f0cbd3ec | bellard | if (m_copy(m, m0, off, len) < 0) { |
162 | f0cbd3ec | bellard | error = -1;
|
163 | f0cbd3ec | bellard | goto sendorfree;
|
164 | f0cbd3ec | bellard | } |
165 | 3b46e624 | ths | |
166 | f0cbd3ec | bellard | mhip->ip_off = htons((u_int16_t)mhip->ip_off); |
167 | f0cbd3ec | bellard | mhip->ip_sum = 0;
|
168 | f0cbd3ec | bellard | mhip->ip_sum = cksum(m, mhlen); |
169 | f0cbd3ec | bellard | *mnext = m; |
170 | f0cbd3ec | bellard | mnext = &m->m_nextpkt; |
171 | 31a60e22 | blueswir1 | STAT(ipstat.ips_ofragments++); |
172 | f0cbd3ec | bellard | } |
173 | f0cbd3ec | bellard | /*
|
174 | f0cbd3ec | bellard | * Update first fragment by trimming what's been copied out
|
175 | f0cbd3ec | bellard | * and updating header, then send each fragment (in order).
|
176 | f0cbd3ec | bellard | */
|
177 | f0cbd3ec | bellard | m = m0; |
178 | f0cbd3ec | bellard | m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len); |
179 | f0cbd3ec | bellard | ip->ip_len = htons((u_int16_t)m->m_len); |
180 | f0cbd3ec | bellard | ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF)); |
181 | f0cbd3ec | bellard | ip->ip_sum = 0;
|
182 | f0cbd3ec | bellard | ip->ip_sum = cksum(m, hlen); |
183 | f0cbd3ec | bellard | sendorfree:
|
184 | f0cbd3ec | bellard | for (m = m0; m; m = m0) {
|
185 | f0cbd3ec | bellard | m0 = m->m_nextpkt; |
186 | 511d2b14 | blueswir1 | m->m_nextpkt = NULL;
|
187 | f0cbd3ec | bellard | if (error == 0) |
188 | f0cbd3ec | bellard | if_output(so, m); |
189 | f0cbd3ec | bellard | else
|
190 | f0cbd3ec | bellard | m_freem(m); |
191 | f0cbd3ec | bellard | } |
192 | f0cbd3ec | bellard | |
193 | f0cbd3ec | bellard | if (error == 0) |
194 | 31a60e22 | blueswir1 | STAT(ipstat.ips_fragmented++); |
195 | f0cbd3ec | bellard | } |
196 | f0cbd3ec | bellard | |
197 | f0cbd3ec | bellard | done:
|
198 | f0cbd3ec | bellard | return (error);
|
199 | f0cbd3ec | bellard | |
200 | f0cbd3ec | bellard | bad:
|
201 | f0cbd3ec | bellard | m_freem(m0); |
202 | f0cbd3ec | bellard | goto done;
|
203 | f0cbd3ec | bellard | } |