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