source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/ip_output.c@ 321

Last change on this file since 321 was 321, checked in by coas-nagasima, 7 years ago

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 10.9 KB
Line 
1/*
2 * TINET (TCP/IP Protocol Stack)
3 *
4 * Copyright (C) 2001-2009 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_buf.h>
96#include <net/net_count.h>
97
98#include <netinet/in.h>
99#include <netinet/in_var.h>
100#include <netinet/ip.h>
101#include <netinet/ip_var.h>
102
103#if defined(SUPPORT_IPSEC)
104#include <netinet6/ipsec.h>
105#endif
106
107static uint16_t frag_id = 0;
108
109/*
110 * IP output -- IP の出力関数
111 *
112 * 注意: バージョンとヘッダ長は上位層で設定する。
113 */
114
115ER
116ip_output (T_NET_BUF *output, TMO tmout)
117{
118 T_IP4_HDR *ip4h;
119 ER error = E_OK;
120 T_IN4_ADDR gw;
121
122#ifdef SUPPORT_IPSEC
123 T_SECPOLICY* sp;
124 int ipsec_error;
125 int policy;
126#endif /* SUPPORT_IPSEC */
127
128 NET_COUNT_MIB(ip_stats.ipOutRequests, 1);
129
130#ifdef SUPPORT_IPSEC
131 /* XXX: IPsecの処理はパケット断片化の前に行われる */
132
133 /* SPDを取得する */
134 sp = ipsec4_getpolicybyaddr(output, IPSEC_DIR_OUTBOUND, &ipsec_error);
135 if (sp==NULL) {
136 error = ipsec_error;
137 return error;
138 }
139
140 /* ポリシをチェックする*/
141#if 0
142 switch (sp->policy) {
143
144 case IPSEC_POLICY_BYPASS:
145 case IPSEC_POLICY_NONE:
146 case IPSEC_POLICY_TCP:
147 goto skip_ipsec;
148
149 break; /* NOTREACHED */
150
151 case IPSEC_POLICY_IPSEC:
152 /* XXX: SH2ではここでillegal Instructionが発生する。コンパイラのバグ? */
153 if (sp->req == NULL) {
154 /* 鍵交換デーモンに対してSAの取得を要求する */
155 goto bad;
156 }
157 break;
158
159 default:
160 /* 不正なポリシ */
161 goto bad;
162 break;
163 }
164#else /* 0 */
165 /* XXX: コンパイラの不具合回避のため switchをやめif文で書き直した */
166 policy = sp->spinitb->policy;
167
168 if (policy == IPSEC_POLICY_BYPASS ||
169 policy == IPSEC_POLICY_NONE ||
170 policy == IPSEC_POLICY_TCP)
171 goto skip_ipsec;
172 else if (policy == IPSEC_POLICY_IPSEC) {
173 if (sp->req == NULL)
174 /* 鍵交換デーモンに対してSAの取得を要求する */
175 goto bad;
176 } else
177 goto bad;
178
179#endif /* 0 */
180
181 {
182 T_IPSEC_OUTPUT_STATE state;
183
184 state.m = output;
185 /* XXX: ipsec4_output()内では、ip->{len,off}をネットワークバイトオーダとする */
186 error = ipsec4_output (&state, sp, 0 /* flags */ );
187 output = state.m;
188
189 if (error) {
190 /* net_buf はipsec4_outputによって既に解放されている */
191 switch (error) {
192 default:
193 syslog (LOG_NOTICE, "ip_output:%d error", __LINE__);
194 }
195 }
196 }
197
198 /* ipsec4_outputに影響を受けた値を更新する */
199 /* XXX: 必要か? */
200
201skip_ipsec:
202#endif /* SUPPORT_IPSEC */
203
204#ifdef IP4_CFG_FRAGMENT
205
206 ip4h = GET_IP4_HDR(output);
207
208 gw = in4_rtalloc(ntohl(ip4h->dst));
209
210 /*
211 * データグラムサイズがネットワークの MTU を超えていれば、
212 * 分割して送信する。
213 */
214 if (ntohs(ip4h->len) > IF_MTU) {
215 T_NET_BUF *frag;
216 T_IP4_HDR *fip4h;
217 uint_t off, hlen, len, flen, align;
218
219 NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_OUT], 1);
220 NET_COUNT_MIB(ip_stats.ipFragCreates, 1);
221
222 len = ntohs(ip4h->len);
223 off = hlen = GET_IP4_HDR_SIZE(ip4h);
224 while (off < len) {
225 if (off + (IF_MTU - IP4_HDR_SIZE) < len)
226 flen = IF_MTU - IP4_HDR_SIZE;
227 else
228 flen = len - off;
229
230 /* データ長は 4 オクテット境界に調整する。 */
231 align = (flen + 3) >> 2 << 2;
232
233 /* ネットワークバッファを獲得する。*/
234 if (tget_net_buf(&frag, align + IF_IP4_HDR_SIZE, TMO_IP4_FRAG_GET_NET_BUF) == E_OK) {
235
236 /* フラグメントをコピーする。*/
237 memcpy(GET_IP4_SDU(frag), (uint8_t *)ip4h + off, flen);
238
239 /* ヘッダを埋める。*/
240 fip4h = GET_IP4_HDR(frag);
241 *fip4h = *ip4h;
242 fip4h->flg_off = htons(IP4_MAKE_FLGOFF(off + flen == len ? 0 : IP4_MF,
243 (off - hlen) >> 3));
244 fip4h->len = htons(flen + IP4_HDR_SIZE);
245 fip4h->id = htons(frag_id);
246 fip4h->sum = 0;
247 fip4h->sum = in_cksum(fip4h, GET_IP4_HDR_SIZE(fip4h));
248
249 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(fip4h->len));
250 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
251 NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_OUT_FRAGS], 1);
252
253 IF_SET_PROTO(frag, IF_PROTO_IP);
254 if ((error = IF_OUTPUT(frag, &gw, NULL, tmout)) != E_OK) {
255 syscall(rel_net_buf(output));
256 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
257 NET_COUNT_MIB(ip_stats.ipFragFails, 1);
258 return error;
259 }
260 }
261 else {
262 /* 獲得できなければ、送信をあきらめる。*/
263 syscall(rel_net_buf(output));
264 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
265 NET_COUNT_MIB(ip_stats.ipFragFails, 1);
266 return E_NOMEM;
267 }
268
269 off += IF_MTU - IP4_HDR_SIZE;
270 }
271 syscall(rel_net_buf(output));
272 frag_id ++;
273 NET_COUNT_MIB(ip_stats.ipFragOKs, 1);
274 }
275 else {
276
277 /* ヘッダを埋める。*/
278 ip4h->id = htons(frag_id);
279 frag_id ++;
280 ip4h->sum = 0;
281 ip4h->sum = in_cksum(ip4h, GET_IP4_HDR_SIZE(ip4h));
282
283 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(ip4h->len));
284 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
285
286 IF_SET_PROTO(output, IF_PROTO_IP);
287 if ((error = IF_OUTPUT(output, &gw, NULL, tmout)) != E_OK)
288 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
289 }
290
291#else /* of #ifdef IP4_CFG_FRAGMENT */
292
293 ip4h = GET_IP4_HDR(output);
294
295 /* データグラムサイズがネットワークの MTU を超えていればエラー */
296 if (ntohs(ip4h->len) > IF_MTU)
297 return E_PAR;
298
299 /* ヘッダを埋める。*/
300 ip4h->id = htons(frag_id);
301 frag_id ++;
302 ip4h->sum = 0;
303 ip4h->sum = in_cksum(ip4h, (uint_t)GET_IP4_HDR_SIZE(ip4h));
304
305 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(ip4h->len));
306 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
307
308#ifdef ETHER_CFG_MULTICAST
309 /* マルチキャストアドレスの場合はIPアドレスはそのまま */
310 gw = ntohl(ip4h->dst);
311 if(!IN4_IS_ADDR_MULTICAST(gw))
312 gw = in4_rtalloc(gw);
313#else
314 gw = in4_rtalloc(ntohl(ip4h->dst));
315#endif
316 IF_SET_PROTO(output, IF_PROTO_IP);
317 if ((error = IF_OUTPUT(output, &gw, NULL, tmout)) != E_OK)
318 NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
319 NET_COUNT_MIB(ip_stats.ipOutDiscards, 1);
320
321#endif /* of #ifdef IP4_CFG_FRAGMENT */
322
323#ifdef SUPPORT_IPSEC
324 bad:
325#endif /* SUPPORT_IPSEC */
326
327 return error;
328 }
Note: See TracBrowser for help on using the repository browser.