1 | /*
|
---|
2 | * TINET (TCP/IP Protocol Stack)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2001-2017 by Dep. of Computer Science and Engineering
|
---|
5 | * Tomakomai National College of Technology, JAPAN
|
---|
6 | *
|
---|
7 | * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
|
---|
8 | * によって公表されている GNU General Public License の Version 2 に記
|
---|
9 | * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
|
---|
10 | * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
|
---|
11 | * 利用と呼ぶ)することを無償で許諾する.
|
---|
12 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
13 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
14 | * スコード中に含まれていること.
|
---|
15 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
16 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
17 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
18 | * の無保証規定を掲載すること.
|
---|
19 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
20 | * 用できない形で再配布する場合には,次の条件を満たすこと.
|
---|
21 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
22 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | *
|
---|
26 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
27 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
|
---|
28 | * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
|
---|
29 | * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
|
---|
30 | *
|
---|
31 | * @(#) $Id$
|
---|
32 | */
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
---|
36 | * The Regents of the University of California. All rights reserved.
|
---|
37 | *
|
---|
38 | * Redistribution and use in source and binary forms, with or without
|
---|
39 | * modification, are permitted provided that the following conditions
|
---|
40 | * are met:
|
---|
41 | * 1. Redistributions of source code must retain the above copyright
|
---|
42 | * notice, this list of conditions and the following disclaimer.
|
---|
43 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
44 | * notice, this list of conditions and the following disclaimer in the
|
---|
45 | * documentation and/or other materials provided with the distribution.
|
---|
46 | * 3. All advertising materials mentioning features or use of this software
|
---|
47 | * must display the following acknowledgement:
|
---|
48 | * This product includes software developed by the University of
|
---|
49 | * California, Berkeley and its contributors.
|
---|
50 | * 4. Neither the name of the University nor the names of its contributors
|
---|
51 | * may be used to endorse or promote products derived from this software
|
---|
52 | * without specific prior written permission.
|
---|
53 | *
|
---|
54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
64 | * SUCH DAMAGE.
|
---|
65 | *
|
---|
66 | * @(#)output.c 8.3 (Berkeley) 1/21/94
|
---|
67 | * $FreeBSD: src/sys/netinet/output.c,v 1.85.2.4 1999/08/29 16:29:49 peter Exp $
|
---|
68 | */
|
---|
69 |
|
---|
70 | #include <string.h>
|
---|
71 |
|
---|
72 | #ifdef TARGET_KERNEL_ASP
|
---|
73 |
|
---|
74 | #include <kernel.h>
|
---|
75 | #include <sil.h>
|
---|
76 |
|
---|
77 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
78 |
|
---|
79 | #ifdef TARGET_KERNEL_JSP
|
---|
80 |
|
---|
81 | #include <s_services.h>
|
---|
82 | #include <t_services.h>
|
---|
83 |
|
---|
84 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
85 |
|
---|
86 | #include <tinet_defs.h>
|
---|
87 | #include <tinet_config.h>
|
---|
88 |
|
---|
89 | #include <net/if.h>
|
---|
90 | #include <net/if_loop.h>
|
---|
91 | #include <net/if_ppp.h>
|
---|
92 | #include <net/ethernet.h>
|
---|
93 | #include <net/ppp.h>
|
---|
94 | #include <net/net.h>
|
---|
95 | #include <net/net_endian.h>
|
---|
96 | #include <net/net_buf.h>
|
---|
97 | #include <net/net_count.h>
|
---|
98 |
|
---|
99 | #include <netinet/in.h>
|
---|
100 | #include <netinet/in_var.h>
|
---|
101 | #include <netinet/ip.h>
|
---|
102 | #include <netinet/ip_var.h>
|
---|
103 |
|
---|
104 | #if defined(SUPPORT_IPSEC)
|
---|
105 | #include <netinet6/ipsec.h>
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | #if defined(_IP4_CFG)
|
---|
109 |
|
---|
110 | static uint16_t frag_id = 0;
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * IP output -- IP の出力関数
|
---|
114 | *
|
---|
115 | * 注意: バージョンとヘッダ長は上位層で設定する。
|
---|
116 | */
|
---|
117 |
|
---|
118 | ER
|
---|
119 | ip_output (T_NET_BUF *output, TMO tmout)
|
---|
120 | {
|
---|
121 | T_IP4_HDR *ip4h;
|
---|
122 | ER error = E_OK;
|
---|
123 | T_IN4_ADDR gw;
|
---|
124 |
|
---|
125 | #ifdef SUPPORT_IPSEC
|
---|
126 | T_SECPOLICY* sp;
|
---|
127 | int ipsec_error;
|
---|
128 | int policy;
|
---|
129 | #endif /* SUPPORT_IPSEC */
|
---|
130 |
|
---|
131 | NET_COUNT_MIB(ip_stats.ipOutRequests, 1);
|
---|
132 |
|
---|
133 | #ifdef SUPPORT_IPSEC
|
---|
134 | /* XXX: IPsecの処理はパケット断片化の前に行われる */
|
---|
135 |
|
---|
136 | /* SPDを取得する */
|
---|
137 | sp = ipsec4_getpolicybyaddr(output, IPSEC_DIR_OUTBOUND, &ipsec_error);
|
---|
138 | if (sp==NULL) {
|
---|
139 | error = ipsec_error;
|
---|
140 | return error;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* ポリシをチェックする*/
|
---|
144 | #if 0
|
---|
145 | switch (sp->policy) {
|
---|
146 |
|
---|
147 | case IPSEC_POLICY_BYPASS:
|
---|
148 | case IPSEC_POLICY_NONE:
|
---|
149 | case IPSEC_POLICY_TCP:
|
---|
150 | goto skip_ipsec;
|
---|
151 |
|
---|
152 | break; /* NOTREACHED */
|
---|
153 |
|
---|
154 | case IPSEC_POLICY_IPSEC:
|
---|
155 | /* XXX: SH2ではここでillegal Instructionが発生する。コンパイラのバグ? */
|
---|
156 | if (sp->req == NULL) {
|
---|
157 | /* 鍵交換デーモンに対してSAの取得を要求する */
|
---|
158 | goto bad;
|
---|
159 | }
|
---|
160 | break;
|
---|
161 |
|
---|
162 | default:
|
---|
163 | /* 不正なポリシ */
|
---|
164 | goto bad;
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | #else /* 0 */
|
---|
168 | /* XXX: コンパイラの不具合回避のため switchをやめif文で書き直した */
|
---|
169 | policy = sp->spinitb->policy;
|
---|
170 |
|
---|
171 | if (policy == IPSEC_POLICY_BYPASS ||
|
---|
172 | policy == IPSEC_POLICY_NONE ||
|
---|
173 | policy == IPSEC_POLICY_TCP)
|
---|
174 | goto skip_ipsec;
|
---|
175 | else if (policy == IPSEC_POLICY_IPSEC) {
|
---|
176 | if (sp->req == NULL)
|
---|
177 | /* 鍵交換デーモンに対してSAの取得を要求する */
|
---|
178 | goto bad;
|
---|
179 | } else
|
---|
180 | goto bad;
|
---|
181 |
|
---|
182 | #endif /* 0 */
|
---|
183 |
|
---|
184 | {
|
---|
185 | T_IPSEC_OUTPUT_STATE state;
|
---|
186 |
|
---|
187 | state.m = output;
|
---|
188 | /* XXX: ipsec4_output()内では、ip->{len,off}をネットワークバイトオーダとする */
|
---|
189 | error = ipsec4_output (&state, sp, 0 /* flags */ );
|
---|
190 | output = state.m;
|
---|
191 |
|
---|
192 | if (error) {
|
---|
193 | /* net_buf はipsec4_outputによって既に解放されている */
|
---|
194 | switch (error) {
|
---|
195 | default:
|
---|
196 | syslog (LOG_NOTICE, "ip_output:%d error", __LINE__);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* ipsec4_outputに影響を受けた値を更新する */
|
---|
202 | /* XXX: 必要か? */
|
---|
203 |
|
---|
204 | skip_ipsec:
|
---|
205 | #endif /* SUPPORT_IPSEC */
|
---|
206 |
|
---|
207 | #ifdef IP4_CFG_FRAGMENT
|
---|
208 |
|
---|
209 | ip4h = GET_IP4_HDR(output);
|
---|
210 |
|
---|
211 | gw = in4_rtalloc(ntohl(ip4h->dst));
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * データグラムサイズがネットワークの MTU を超えていれば、
|
---|
215 | * 分割して送信する。
|
---|
216 | */
|
---|
217 | if (ntohs(ip4h->len) > IF_MTU) {
|
---|
218 | T_NET_BUF *frag;
|
---|
219 | T_IP4_HDR *fip4h;
|
---|
220 | uint_t off, hlen, len, flen, align;
|
---|
221 |
|
---|
222 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_OUT], 1);
|
---|
223 | NET_COUNT_MIB(ip_stats.ipFragCreates, 1);
|
---|
224 |
|
---|
225 | len = ntohs(ip4h->len);
|
---|
226 | off = hlen = GET_IP4_HDR_SIZE(output);
|
---|
227 | while (off < len) {
|
---|
228 | if (off + (IF_MTU - IP4_HDR_SIZE) < len)
|
---|
229 | flen = IF_MTU - IP4_HDR_SIZE;
|
---|
230 | else
|
---|
231 | flen = len - off;
|
---|
232 |
|
---|
233 | /* データ長は 4 オクテット境界に調整する。 */
|
---|
234 | align = (flen + 3) >> 2 << 2;
|
---|
235 |
|
---|
236 | /* ネットワークバッファを獲得する。*/
|
---|
237 | if (tget_net_buf(&frag, align + IF_IP4_HDR_SIZE, TMO_IP4_FRAG_GET_NET_BUF) == E_OK) {
|
---|
238 |
|
---|
239 | /* フラグメントをコピーする。*/
|
---|
240 | memcpy(GET_IP4_SDU(frag), (uint8_t *)ip4h + off, flen);
|
---|
241 |
|
---|
242 | /* ヘッダを埋める。*/
|
---|
243 | fip4h = GET_IP4_HDR(frag);
|
---|
244 | *fip4h = *ip4h;
|
---|
245 | fip4h->flg_off = htons(IP4_MAKE_FLGOFF(off + flen == len ? 0 : IP4_MF,
|
---|
246 | (off - hlen) >> 3));
|
---|
247 | fip4h->len = htons(flen + IP4_HDR_SIZE);
|
---|
248 | fip4h->id = htons(frag_id);
|
---|
249 | fip4h->sum = 0;
|
---|
250 | fip4h->sum = in_cksum(fip4h, GET_IP4_HDR_SIZE(frag));
|
---|
251 |
|
---|
252 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(fip4h->len));
|
---|
253 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
|
---|
254 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_OUT_FRAGS], 1);
|
---|
255 |
|
---|
256 | IF_SET_PROTO(frag, IF_PROTO_IP);
|
---|
257 | if ((error = IF_OUTPUT(frag, &gw, NULL, tmout)) != E_OK) {
|
---|
258 | syscall(rel_net_buf(output));
|
---|
259 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
260 | NET_COUNT_MIB(ip_stats.ipFragFails, 1);
|
---|
261 | return error;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | else {
|
---|
265 | /* 獲得できなければ、送信をあきらめる。*/
|
---|
266 | syscall(rel_net_buf(output));
|
---|
267 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
268 | NET_COUNT_MIB(ip_stats.ipFragFails, 1);
|
---|
269 | return E_NOMEM;
|
---|
270 | }
|
---|
271 |
|
---|
272 | off += IF_MTU - IP4_HDR_SIZE;
|
---|
273 | }
|
---|
274 | syscall(rel_net_buf(output));
|
---|
275 | frag_id ++;
|
---|
276 | NET_COUNT_MIB(ip_stats.ipFragOKs, 1);
|
---|
277 | }
|
---|
278 | else {
|
---|
279 |
|
---|
280 | /* ヘッダを埋める。*/
|
---|
281 | ip4h->id = htons(frag_id);
|
---|
282 | frag_id ++;
|
---|
283 | ip4h->sum = 0;
|
---|
284 | ip4h->sum = in_cksum(ip4h, GET_IP4_HDR_SIZE(output));
|
---|
285 |
|
---|
286 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(ip4h->len));
|
---|
287 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
|
---|
288 |
|
---|
289 | IF_SET_PROTO(output, IF_PROTO_IP);
|
---|
290 | if ((error = IF_OUTPUT(output, &gw, NULL, tmout)) != E_OK)
|
---|
291 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
292 | }
|
---|
293 |
|
---|
294 | #else /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
295 |
|
---|
296 | ip4h = GET_IP4_HDR(output);
|
---|
297 |
|
---|
298 | /* データグラムサイズがネットワークの MTU を超えていればエラー */
|
---|
299 | if (ntohs(ip4h->len) > IF_MTU)
|
---|
300 | return E_PAR;
|
---|
301 |
|
---|
302 | /* ヘッダを埋める。*/
|
---|
303 | ip4h->id = htons(frag_id);
|
---|
304 | frag_id ++;
|
---|
305 | ip4h->sum = 0;
|
---|
306 | ip4h->sum = in_cksum(ip4h, (uint_t)GET_IP4_HDR_SIZE(output));
|
---|
307 |
|
---|
308 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(ip4h->len));
|
---|
309 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
|
---|
310 |
|
---|
311 | #ifdef ETHER_CFG_MULTICAST
|
---|
312 | /* マルチキャストアドレスの場合はIPアドレスはそのまま */
|
---|
313 | gw = ntohl(ip4h->dst);
|
---|
314 | if(!IN4_IS_ADDR_MULTICAST(gw))
|
---|
315 | gw = in4_rtalloc(gw);
|
---|
316 | #else
|
---|
317 | gw = in4_rtalloc(ntohl(ip4h->dst));
|
---|
318 | #endif
|
---|
319 | IF_SET_PROTO(output, IF_PROTO_IP);
|
---|
320 | if ((error = IF_OUTPUT(output, &gw, NULL, tmout)) != E_OK)
|
---|
321 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
322 | NET_COUNT_MIB(ip_stats.ipOutDiscards, 1);
|
---|
323 |
|
---|
324 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
325 |
|
---|
326 | #ifdef SUPPORT_IPSEC
|
---|
327 | bad:
|
---|
328 | #endif /* SUPPORT_IPSEC */
|
---|
329 |
|
---|
330 | return error;
|
---|
331 | }
|
---|
332 |
|
---|
333 | #endif /* of #if defined(_IP4_CFG) */
|
---|