source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet6/ip6_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: 12.7 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/* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.43 2002/10/31 19:45:48 ume Exp $ */
35/* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
36
37/*
38 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
39 * All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the project nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 */
65
66/*
67 * Copyright (c) 1982, 1986, 1988, 1990, 1993
68 * The Regents of the University of California. All rights reserved.
69 *
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
72 * are met:
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 * 3. All advertising materials mentioning features or use of this software
79 * must display the following acknowledgement:
80 * This product includes software developed by the University of
81 * California, Berkeley and its contributors.
82 * 4. Neither the name of the University nor the names of its contributors
83 * may be used to endorse or promote products derived from this software
84 * without specific prior written permission.
85 *
86 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
87 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
92 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
93 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
94 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
95 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
96 * SUCH DAMAGE.
97 *
98 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
99 */
100
101#include <string.h>
102
103#ifdef TARGET_KERNEL_ASP
104
105#include <kernel.h>
106#include <sil.h>
107
108#endif /* of #ifdef TARGET_KERNEL_ASP */
109
110#ifdef TARGET_KERNEL_JSP
111
112#include <s_services.h>
113#include <t_services.h>
114
115#endif /* of #ifdef TARGET_KERNEL_JSP */
116
117#include <tinet_defs.h>
118#include <tinet_config.h>
119
120#include <net/if.h>
121#include <net/if_loop.h>
122#include <net/if_ppp.h>
123#include <net/ethernet.h>
124#include <net/if_arp.h>
125#include <net/ppp_ipcp.h>
126#include <net/net.h>
127#include <net/net_buf.h>
128#include <net/net_count.h>
129
130#include <netinet/in.h>
131#include <netinet6/in6.h>
132#include <netinet6/in6_var.h>
133#include <netinet6/nd6.h>
134#include <netinet/ip6.h>
135#include <netinet6/ip6_var.h>
136#include <netinet/icmp6.h>
137
138#include <net/if6_var.h>
139
140#ifdef SUPPORT_INET6
141
142/*
143 * 全域変数
144 */
145
146/* リンク MTU */
147
148uint32_t linkmtu = IF_MTU;
149
150/*
151 * 局所変数
152 */
153
154#ifdef IP6_CFG_FRAGMENT
155
156static uint32_t ip6_id;
157
158#endif /* of #ifdef IP6_CFG_FRAGMENT */
159
160/*
161 * ip6_output -- IPv6 の出力関数
162 */
163
164ER
165ip6_output (T_NET_BUF *output, uint16_t flags, TMO tmout)
166{
167 T_IP6_HDR *ip6h;
168 T_IN6_ADDR *gw;
169 T_IFNET *ifp = IF_GET_IFNET();
170 ER error = E_OK;
171
172#ifdef IP6_CFG_FRAGMENT
173
174 T_IP6_HDR *fip6h;
175 T_IP6_FRAG_HDR *fip6f;
176 T_NET_BUF *frag;
177 uint32_t mtu, id;
178 uint16_t plen, foff, flen;
179 uint8_t nextproto;
180 bool_t alwaysfrag = false;
181
182#endif /* of #ifdef IP6_CFG_FRAGMENT */
183
184 ip6h = GET_IP6_HDR(output);
185
186 NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_OCTETS],
187 GET_IP_HDR_SIZE(ip6h) + GET_IP_SDU_SIZE(ip6h));
188 NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_PACKETS], 1);
189 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutRequests, 1);
190
191 /*
192 * 重複アドレス検出要請以外には、送信元アドレスに
193 * 無指定を指定できない。
194 */
195 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->src) && (flags & IPV6_OUT_FLG_DAD) == 0) {
196 error = E_PAR;
197 goto drop;
198 }
199
200 /* 送信元アドレスにはマルチキャストアドレスを指定できない。*/
201 if (IN6_IS_ADDR_MULTICAST(&ip6h->src)) {
202 error = E_PAR;
203 goto drop;
204 }
205
206 /*
207 * 経路選択を行う。
208 */
209 if ((gw = in6_rtalloc(ifp, &ip6h->dst)) == NULL) {
210 error = E_PAR;
211 goto drop;
212 }
213
214 if (!IN6_IS_ADDR_MULTICAST(&ip6h->dst)) {
215 /* ユニキャストアドレスの処理 */
216 }
217 else {
218 /* マルチキャストアドレスの処理 */
219 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutMcastPkts, 1);
220 if (flags & IPV6_OUT_FLG_HOP_LIMIT)
221 ip6h->hlim = IPV6_OUT_GET_HOP_LIMIT(flags);
222 else
223 ip6h->hlim = IPV6_DEFAULT_MULTICAST_HOPS;
224 }
225
226 /* Traffic Class と Flow Label をサポートしないので 0 に設定する。*/
227 ip6h->vcf = htonl(IP6_MAKE_VCF(IPV6_VERSION, 0));
228
229#ifdef IP6_CFG_FRAGMENT
230
231
232#if NUM_IN6_HOSTCACHE_ENTRY > 0
233
234 mtu = in6_hostcache_getmtu(&ip6h->dst);
235
236 if (0 < mtu && mtu <= IPV6_MMTU) {
237
238 /*
239 * Path MTU が登録されている場合、
240 * MTU が IPv6 MMTU(1280 オクテット)以下の場合は、
241 * MTU を IPv6 MMTU に設定し分割して送信する。
242 */
243 mtu = IPV6_MMTU;
244 alwaysfrag = true;
245 }
246 else if (mtu == 0)
247 mtu = linkmtu;
248
249#else /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
250
251 mtu = linkmtu;
252
253#endif /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
254
255 plen = ntohs(ip6h->plen);
256 if (plen + IP6_HDR_SIZE <= mtu && !alwaysfrag) {
257
258 /* 分割する必要がないときの処理 */
259 if ((error = nd6_output(ifp, output, gw, NULL, tmout)) != E_OK) {
260 NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_ERR_PACKETS], 1);
261 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutDiscards, 1);
262 }
263 }
264 else {
265
266 /* 断片 ID を設定する。*/
267 id = ip6_id ++;
268
269 /* MTU が IPv6 の最大パケットサイズを超えないようにする。*/
270 if (mtu > IPV6_MAXPACKET)
271 mtu = IPV6_MAXPACKET;
272
273 /*
274 * MTU から、分割不能部分(TINET-1.4 では、分割ヘッダ以外の
275 * 拡張ヘッダの出力は未実装のため 0 オクテット)
276 * と断片ヘッダのサイズを引いたとき、8 オクテット以上なければ
277 * 分割して送信できないためエラーにする。
278 */
279 if (((mtu - sizeof(T_IP6_FRAG_HDR)) & ~7) < 8) {
280 error = E_PAR;
281 goto drop;
282 }
283
284 /*
285 * IPv6 ヘッダの next フィールドに設定する値は、断片ヘッダ。
286 * 断片ヘッダの next フィールドに設定する値は、
287 * 元のデータグラムの next フィールドの値。
288 */
289 nextproto = ip6h->next;
290 ip6h->next = IPPROTO_FRAGMENT;
291
292 /* 分割して送信する。*/
293 NET_COUNT_IP6(net_count_ip6[NC_IP6_FRAG_OUT], 1);
294 for (foff = 0; foff < plen; foff += flen) {
295
296 /* 断片の長さを計算し、8 オクテット境界に調整する。*/
297 if (foff + (mtu - (IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR))) < plen)
298 flen = (mtu - (IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR))) >> 3 << 3;
299 else
300 flen = plen - foff;
301
302 /* ネットワークバッファを獲得する。*/
303 if (tget_net_buf(&frag, flen + IF_IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR), TMO_IP6_FRAG_GET_NET_BUF) == E_OK) {
304
305 /* IPv6 ヘッダをコピーする。*/
306 fip6h = GET_IP6_HDR(frag);
307 *fip6h = *ip6h;
308
309 /* IPv6 ヘッダを埋める。*/
310 fip6h->plen = htons(flen + sizeof(T_IP6_FRAG_HDR));
311
312 /* 断片ヘッダを埋める。*/
313 fip6f = (T_IP6_FRAG_HDR *)GET_IP6_NEXT_HDR(frag);
314 fip6f->off_flag = htons((uint16_t)(foff & ~7));
315 if (foff + (mtu - (IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR))) < plen)
316 fip6f->off_flag |= IP6F_MORE_FRAG;
317 fip6f->reserved = 0;
318 fip6f->ident = htonl(id);
319 fip6f->next = nextproto;
320
321 /* SDU をコピーする。*/
322 memcpy((uint8_t*)(fip6f + 1), GET_IP6_NEXT_HDR(output) + foff, flen);
323 NET_COUNT_IP6(net_count_ip6[NC_IP6_FRAG_OUT_FRAGS], 1);
324 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutFragCreates, 1);
325
326 /* 送信する。*/
327 if ((error = nd6_output(ifp, frag, gw, NULL, tmout)) != E_OK) {
328 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutFragFails, 1);
329 goto drop;
330 }
331 }
332 else {
333 /* ネットワークバッファが獲得できなければ、送信をあきらめる。*/
334 error = E_NOMEM;
335 goto drop;
336 }
337 }
338
339 /* IF でネットワークバッファを開放しないフラグをチェックする。*/
340 if ((output->flags & NB_FLG_NOREL_IFOUT) == 0) {
341 syscall(rel_net_buf(output));
342 }
343 else
344 output->flags &= (uint8_t)~NB_FLG_NOREL_IFOUT;
345
346 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutFragOKs, 1);
347 }
348
349#else /* #ifdef IP6_CFG_FRAGMENT */
350
351 if ((error = nd6_output(ifp, output, gw, NULL, tmout)) != E_OK) {
352 NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_ERR_PACKETS], 1);
353 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutDiscards, 1);
354 }
355
356#endif /* #ifdef IP6_CFG_FRAGMENT */
357
358 return error;
359
360drop:
361 /* IF でネットワークバッファを開放しないフラグをチェックする。*/
362 if ((output->flags & NB_FLG_NOREL_IFOUT) == 0) {
363 syscall(rel_net_buf(output));
364 }
365 else
366 output->flags &= (uint8_t)~NB_FLG_NOREL_IFOUT;
367
368 NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_ERR_PACKETS], 1);
369 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutDiscards, 1);
370 return error;
371 }
372
373#endif /* of #ifdef SUPPORT_INET6 */
Note: See TracBrowser for help on using the repository browser.