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