root / slirp / ip_output.c @ 5bc89ef6
History | View | Annotate | Download (4.8 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 | 7878ff6b | blueswir1 | /* Number of packets queued before we start sending
|
44 | 7878ff6b | blueswir1 | * (to prevent allocing too many mbufs) */
|
45 | 7878ff6b | blueswir1 | #define IF_THRESH 10 |
46 | 7878ff6b | blueswir1 | |
47 | f0cbd3ec | bellard | /*
|
48 | f0cbd3ec | bellard | * IP output. The packet in mbuf chain m contains a skeletal IP
|
49 | f0cbd3ec | bellard | * header (with len, off, ttl, proto, tos, src, dst).
|
50 | f0cbd3ec | bellard | * The mbuf chain containing the packet will be freed.
|
51 | f0cbd3ec | bellard | * The mbuf opt, if present, will not be freed.
|
52 | f0cbd3ec | bellard | */
|
53 | f0cbd3ec | bellard | int
|
54 | 511d2b14 | blueswir1 | ip_output(struct socket *so, struct mbuf *m0) |
55 | f0cbd3ec | bellard | { |
56 | 460fec67 | Jan Kiszka | Slirp *slirp = m0->slirp; |
57 | f0cbd3ec | bellard | register struct ip *ip; |
58 | f0cbd3ec | bellard | register struct mbuf *m = m0; |
59 | f0cbd3ec | bellard | register int hlen = sizeof(struct ip ); |
60 | f0cbd3ec | bellard | int len, off, error = 0; |
61 | f0cbd3ec | bellard | |
62 | f0cbd3ec | bellard | DEBUG_CALL("ip_output");
|
63 | f0cbd3ec | bellard | DEBUG_ARG("so = %lx", (long)so); |
64 | f0cbd3ec | bellard | DEBUG_ARG("m0 = %lx", (long)m0); |
65 | 5fafdf24 | ths | |
66 | f0cbd3ec | bellard | ip = mtod(m, struct ip *);
|
67 | f0cbd3ec | bellard | /*
|
68 | f0cbd3ec | bellard | * Fill in IP header.
|
69 | f0cbd3ec | bellard | */
|
70 | f0cbd3ec | bellard | ip->ip_v = IPVERSION; |
71 | f0cbd3ec | bellard | ip->ip_off &= IP_DF; |
72 | 460fec67 | Jan Kiszka | ip->ip_id = htons(slirp->ip_id++); |
73 | f0cbd3ec | bellard | ip->ip_hl = hlen >> 2;
|
74 | f0cbd3ec | bellard | |
75 | f0cbd3ec | bellard | /*
|
76 | f0cbd3ec | bellard | * If small enough for interface, can just send directly.
|
77 | f0cbd3ec | bellard | */
|
78 | 9634d903 | blueswir1 | if ((u_int16_t)ip->ip_len <= IF_MTU) {
|
79 | f0cbd3ec | bellard | ip->ip_len = htons((u_int16_t)ip->ip_len); |
80 | f0cbd3ec | bellard | ip->ip_off = htons((u_int16_t)ip->ip_off); |
81 | f0cbd3ec | bellard | ip->ip_sum = 0;
|
82 | f0cbd3ec | bellard | ip->ip_sum = cksum(m, hlen); |
83 | f0cbd3ec | bellard | |
84 | f0cbd3ec | bellard | if_output(so, m); |
85 | f0cbd3ec | bellard | goto done;
|
86 | f0cbd3ec | bellard | } |
87 | f0cbd3ec | bellard | |
88 | f0cbd3ec | bellard | /*
|
89 | f0cbd3ec | bellard | * Too large for interface; fragment if possible.
|
90 | f0cbd3ec | bellard | * Must be able to put at least 8 bytes per fragment.
|
91 | f0cbd3ec | bellard | */
|
92 | f0cbd3ec | bellard | if (ip->ip_off & IP_DF) {
|
93 | f0cbd3ec | bellard | error = -1;
|
94 | f0cbd3ec | bellard | goto bad;
|
95 | f0cbd3ec | bellard | } |
96 | 5fafdf24 | ths | |
97 | 9634d903 | blueswir1 | len = (IF_MTU - hlen) &~ 7; /* ip databytes per packet */ |
98 | f0cbd3ec | bellard | if (len < 8) { |
99 | f0cbd3ec | bellard | error = -1;
|
100 | f0cbd3ec | bellard | goto bad;
|
101 | f0cbd3ec | bellard | } |
102 | f0cbd3ec | bellard | |
103 | f0cbd3ec | bellard | { |
104 | f0cbd3ec | bellard | int mhlen, firstlen = len;
|
105 | f0cbd3ec | bellard | struct mbuf **mnext = &m->m_nextpkt;
|
106 | f0cbd3ec | bellard | |
107 | f0cbd3ec | bellard | /*
|
108 | f0cbd3ec | bellard | * Loop through length of segment after first fragment,
|
109 | f0cbd3ec | bellard | * make new header and copy data of each part and link onto chain.
|
110 | f0cbd3ec | bellard | */
|
111 | f0cbd3ec | bellard | m0 = m; |
112 | f0cbd3ec | bellard | mhlen = sizeof (struct ip); |
113 | f0cbd3ec | bellard | for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
|
114 | f0cbd3ec | bellard | register struct ip *mhip; |
115 | 460fec67 | Jan Kiszka | m = m_get(slirp); |
116 | 511d2b14 | blueswir1 | if (m == NULL) { |
117 | f0cbd3ec | bellard | error = -1;
|
118 | f0cbd3ec | bellard | goto sendorfree;
|
119 | f0cbd3ec | bellard | } |
120 | 9634d903 | blueswir1 | m->m_data += IF_MAXLINKHDR; |
121 | f0cbd3ec | bellard | mhip = mtod(m, struct ip *);
|
122 | f0cbd3ec | bellard | *mhip = *ip; |
123 | 3b46e624 | ths | |
124 | f0cbd3ec | bellard | m->m_len = mhlen; |
125 | f0cbd3ec | bellard | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
|
126 | f0cbd3ec | bellard | if (ip->ip_off & IP_MF)
|
127 | f0cbd3ec | bellard | mhip->ip_off |= IP_MF; |
128 | f0cbd3ec | bellard | if (off + len >= (u_int16_t)ip->ip_len)
|
129 | f0cbd3ec | bellard | len = (u_int16_t)ip->ip_len - off; |
130 | 5fafdf24 | ths | else
|
131 | f0cbd3ec | bellard | mhip->ip_off |= IP_MF; |
132 | f0cbd3ec | bellard | mhip->ip_len = htons((u_int16_t)(len + mhlen)); |
133 | 3b46e624 | ths | |
134 | f0cbd3ec | bellard | if (m_copy(m, m0, off, len) < 0) { |
135 | f0cbd3ec | bellard | error = -1;
|
136 | f0cbd3ec | bellard | goto sendorfree;
|
137 | f0cbd3ec | bellard | } |
138 | 3b46e624 | ths | |
139 | f0cbd3ec | bellard | mhip->ip_off = htons((u_int16_t)mhip->ip_off); |
140 | f0cbd3ec | bellard | mhip->ip_sum = 0;
|
141 | f0cbd3ec | bellard | mhip->ip_sum = cksum(m, mhlen); |
142 | f0cbd3ec | bellard | *mnext = m; |
143 | f0cbd3ec | bellard | mnext = &m->m_nextpkt; |
144 | f0cbd3ec | bellard | } |
145 | f0cbd3ec | bellard | /*
|
146 | f0cbd3ec | bellard | * Update first fragment by trimming what's been copied out
|
147 | f0cbd3ec | bellard | * and updating header, then send each fragment (in order).
|
148 | f0cbd3ec | bellard | */
|
149 | f0cbd3ec | bellard | m = m0; |
150 | f0cbd3ec | bellard | m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len); |
151 | f0cbd3ec | bellard | ip->ip_len = htons((u_int16_t)m->m_len); |
152 | f0cbd3ec | bellard | ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF)); |
153 | f0cbd3ec | bellard | ip->ip_sum = 0;
|
154 | f0cbd3ec | bellard | ip->ip_sum = cksum(m, hlen); |
155 | f0cbd3ec | bellard | sendorfree:
|
156 | f0cbd3ec | bellard | for (m = m0; m; m = m0) {
|
157 | f0cbd3ec | bellard | m0 = m->m_nextpkt; |
158 | 511d2b14 | blueswir1 | m->m_nextpkt = NULL;
|
159 | f0cbd3ec | bellard | if (error == 0) |
160 | f0cbd3ec | bellard | if_output(so, m); |
161 | f0cbd3ec | bellard | else
|
162 | f0cbd3ec | bellard | m_freem(m); |
163 | f0cbd3ec | bellard | } |
164 | f0cbd3ec | bellard | } |
165 | f0cbd3ec | bellard | |
166 | f0cbd3ec | bellard | done:
|
167 | f0cbd3ec | bellard | return (error);
|
168 | f0cbd3ec | bellard | |
169 | f0cbd3ec | bellard | bad:
|
170 | f0cbd3ec | bellard | m_freem(m0); |
171 | f0cbd3ec | bellard | goto done;
|
172 | f0cbd3ec | bellard | } |