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 | /* $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_endian.h>
|
---|
128 | #include <net/net_buf.h>
|
---|
129 | #include <net/net_count.h>
|
---|
130 |
|
---|
131 | #include <netinet/in.h>
|
---|
132 | #include <netinet/in_var.h>
|
---|
133 | #include <netinet/ip.h>
|
---|
134 | #include <netinet/ip_var.h>
|
---|
135 | #include <netinet/ip_icmp.h>
|
---|
136 |
|
---|
137 | #include <netinet6/nd6.h>
|
---|
138 |
|
---|
139 | #include <net/if_var.h>
|
---|
140 |
|
---|
141 | #ifdef _IP6_CFG
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * 全域変数
|
---|
145 | */
|
---|
146 |
|
---|
147 | /* リンク MTU */
|
---|
148 |
|
---|
149 | uint32_t linkmtu = IF_MTU;
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * 局所変数
|
---|
153 | */
|
---|
154 |
|
---|
155 | #ifdef IP6_CFG_FRAGMENT
|
---|
156 |
|
---|
157 | static uint32_t ip6_id;
|
---|
158 |
|
---|
159 | #endif /* of #ifdef IP6_CFG_FRAGMENT */
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * ip6_output -- IPv6 の出力関数
|
---|
163 | */
|
---|
164 |
|
---|
165 | ER
|
---|
166 | ip6_output (T_NET_BUF *output, uint16_t flags, TMO tmout)
|
---|
167 | {
|
---|
168 | T_IP6_HDR *ip6h;
|
---|
169 | const T_IN6_ADDR *gw;
|
---|
170 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
171 | ER error = E_OK;
|
---|
172 |
|
---|
173 | #ifdef IP6_CFG_FRAGMENT
|
---|
174 |
|
---|
175 | T_IP6_HDR *fip6h;
|
---|
176 | T_IP6_FRAG_HDR *fip6f;
|
---|
177 | T_NET_BUF *frag;
|
---|
178 | uint32_t mtu, id;
|
---|
179 | uint16_t plen, foff, flen;
|
---|
180 | uint8_t nextproto;
|
---|
181 | bool_t alwaysfrag = false;
|
---|
182 |
|
---|
183 | #endif /* of #ifdef IP6_CFG_FRAGMENT */
|
---|
184 |
|
---|
185 | ip6h = GET_IP6_HDR(output);
|
---|
186 |
|
---|
187 | NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_OCTETS],
|
---|
188 | GET_IP6_HDR_SIZE(output) + GET_IP6_SDU_SIZE(output));
|
---|
189 | NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_PACKETS], 1);
|
---|
190 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutRequests, 1);
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * 重複アドレス検出要請以外には、送信元アドレスに
|
---|
194 | * 無指定を指定できない。
|
---|
195 | */
|
---|
196 | if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->src) && (flags & IPV6_OUT_FLG_DAD) == 0) {
|
---|
197 | error = E_PAR;
|
---|
198 | goto drop;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* 送信元アドレスにはマルチキャストアドレスを指定できない。*/
|
---|
202 | if (IN6_IS_ADDR_MULTICAST(&ip6h->src)) {
|
---|
203 | error = E_PAR;
|
---|
204 | goto drop;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * 経路選択を行う。
|
---|
209 | */
|
---|
210 | if ((gw = in6_rtalloc(ifp, &ip6h->dst)) == NULL) {
|
---|
211 | error = E_PAR;
|
---|
212 | goto drop;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (!IN6_IS_ADDR_MULTICAST(&ip6h->dst)) {
|
---|
216 | /* ユニキャストアドレスの処理 */
|
---|
217 | }
|
---|
218 | else {
|
---|
219 | /* マルチキャストアドレスの処理 */
|
---|
220 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutMcastPkts, 1);
|
---|
221 | if (flags & IPV6_OUT_FLG_HOP_LIMIT)
|
---|
222 | ip6h->hlim = IPV6_OUT_GET_HOP_LIMIT(flags);
|
---|
223 | else
|
---|
224 | ip6h->hlim = IPV6_DEFAULT_MULTICAST_HOPS;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* Traffic Class と Flow Label をサポートしないので 0 に設定する。*/
|
---|
228 | ip6h->vcf = htonl(IP6_MAKE_VCF(IPV6_VERSION, 0));
|
---|
229 |
|
---|
230 | #ifdef IP6_CFG_FRAGMENT
|
---|
231 |
|
---|
232 |
|
---|
233 | #if NUM_IN6_HOSTCACHE_ENTRY > 0
|
---|
234 |
|
---|
235 | mtu = in6_hostcache_getmtu(&ip6h->dst);
|
---|
236 |
|
---|
237 | if (0 < mtu && mtu <= IPV6_MMTU) {
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Path MTU が登録されている場合、
|
---|
241 | * MTU が IPv6 MMTU(1280 オクテット)以下の場合は、
|
---|
242 | * MTU を IPv6 MMTU に設定し分割して送信する。
|
---|
243 | */
|
---|
244 | mtu = IPV6_MMTU;
|
---|
245 | alwaysfrag = true;
|
---|
246 | }
|
---|
247 | else if (mtu == 0)
|
---|
248 | mtu = linkmtu;
|
---|
249 |
|
---|
250 | #else /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
|
---|
251 |
|
---|
252 | mtu = linkmtu;
|
---|
253 |
|
---|
254 | #endif /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
|
---|
255 |
|
---|
256 | plen = ntohs(ip6h->plen);
|
---|
257 | if (plen + IP6_HDR_SIZE <= mtu && !alwaysfrag) {
|
---|
258 |
|
---|
259 | /* 分割する必要がないときの処理 */
|
---|
260 | if ((error = nd6_output(ifp, output, gw, NULL, tmout)) != E_OK) {
|
---|
261 | NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_ERR_PACKETS], 1);
|
---|
262 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutDiscards, 1);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | else {
|
---|
266 |
|
---|
267 | /* 断片 ID を設定する。*/
|
---|
268 | id = ip6_id ++;
|
---|
269 |
|
---|
270 | /* MTU が IPv6 の最大パケットサイズを超えないようにする。*/
|
---|
271 | if (mtu > IPV6_MAXPACKET)
|
---|
272 | mtu = IPV6_MAXPACKET;
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * MTU から、分割不能部分(TINET-1.4 では、分割ヘッダ以外の
|
---|
276 | * 拡張ヘッダの出力は未実装のため 0 オクテット)
|
---|
277 | * と断片ヘッダのサイズを引いたとき、8 オクテット以上なければ
|
---|
278 | * 分割して送信できないためエラーにする。
|
---|
279 | */
|
---|
280 | if (((mtu - sizeof(T_IP6_FRAG_HDR)) & ~7) < 8) {
|
---|
281 | error = E_PAR;
|
---|
282 | goto drop;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * IPv6 ヘッダの next フィールドに設定する値は、断片ヘッダ。
|
---|
287 | * 断片ヘッダの next フィールドに設定する値は、
|
---|
288 | * 元のデータグラムの next フィールドの値。
|
---|
289 | */
|
---|
290 | nextproto = ip6h->next;
|
---|
291 | ip6h->next = IPPROTO_FRAGMENT;
|
---|
292 |
|
---|
293 | /* 分割して送信する。*/
|
---|
294 | NET_COUNT_IP6(net_count_ip6[NC_IP6_FRAG_OUT], 1);
|
---|
295 | for (foff = 0; foff < plen; foff += flen) {
|
---|
296 |
|
---|
297 | /* 断片の長さを計算し、8 オクテット境界に調整する。*/
|
---|
298 | if (foff + (mtu - (IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR))) < plen)
|
---|
299 | flen = (mtu - (IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR))) >> 3 << 3;
|
---|
300 | else
|
---|
301 | flen = plen - foff;
|
---|
302 |
|
---|
303 | /* ネットワークバッファを獲得する。*/
|
---|
304 | if (tget_net_buf(&frag, flen + IF_IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR), TMO_IP6_FRAG_GET_NET_BUF) == E_OK) {
|
---|
305 |
|
---|
306 | /* IPv6 ヘッダをコピーする。*/
|
---|
307 | fip6h = GET_IP6_HDR(frag);
|
---|
308 | *fip6h = *ip6h;
|
---|
309 |
|
---|
310 | /* IPv6 ヘッダを埋める。*/
|
---|
311 | fip6h->plen = htons(flen + sizeof(T_IP6_FRAG_HDR));
|
---|
312 |
|
---|
313 | /* 断片ヘッダを埋める。*/
|
---|
314 | fip6f = (T_IP6_FRAG_HDR *)GET_IP6_NEXT_HDR(frag);
|
---|
315 | fip6f->off_flag = htons((uint16_t)(foff & ~7));
|
---|
316 | if (foff + (mtu - (IP6_HDR_SIZE + sizeof(T_IP6_FRAG_HDR))) < plen)
|
---|
317 | fip6f->off_flag |= IP6F_MORE_FRAG;
|
---|
318 | fip6f->reserved = 0;
|
---|
319 | fip6f->ident = htonl(id);
|
---|
320 | fip6f->next = nextproto;
|
---|
321 |
|
---|
322 | /* SDU をコピーする。*/
|
---|
323 | memcpy((uint8_t*)(fip6f + 1), GET_IP6_NEXT_HDR(output) + foff, flen);
|
---|
324 | NET_COUNT_IP6(net_count_ip6[NC_IP6_FRAG_OUT_FRAGS], 1);
|
---|
325 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutFragCreates, 1);
|
---|
326 |
|
---|
327 | /* 送信する。*/
|
---|
328 | if ((error = nd6_output(ifp, frag, gw, NULL, tmout)) != E_OK) {
|
---|
329 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutFragFails, 1);
|
---|
330 | goto drop;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | else {
|
---|
334 | /* ネットワークバッファが獲得できなければ、送信をあきらめる。*/
|
---|
335 | error = E_NOMEM;
|
---|
336 | goto drop;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* IF でネットワークバッファを開放しないフラグをチェックする。*/
|
---|
341 | if ((output->flags & NB_FLG_NOREL_IFOUT) == 0) {
|
---|
342 | syscall(rel_net_buf(output));
|
---|
343 | }
|
---|
344 | else
|
---|
345 | output->flags &= (uint8_t)~NB_FLG_NOREL_IFOUT;
|
---|
346 |
|
---|
347 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutFragOKs, 1);
|
---|
348 | }
|
---|
349 |
|
---|
350 | #else /* #ifdef IP6_CFG_FRAGMENT */
|
---|
351 |
|
---|
352 | if ((error = nd6_output(ifp, output, gw, NULL, tmout)) != E_OK) {
|
---|
353 | NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_ERR_PACKETS], 1);
|
---|
354 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutDiscards, 1);
|
---|
355 | }
|
---|
356 |
|
---|
357 | #endif /* #ifdef IP6_CFG_FRAGMENT */
|
---|
358 |
|
---|
359 | return error;
|
---|
360 |
|
---|
361 | drop:
|
---|
362 | /* IF でネットワークバッファを開放しないフラグをチェックする。*/
|
---|
363 | if ((output->flags & NB_FLG_NOREL_IFOUT) == 0) {
|
---|
364 | syscall(rel_net_buf(output));
|
---|
365 | }
|
---|
366 | else
|
---|
367 | output->flags &= (uint8_t)~NB_FLG_NOREL_IFOUT;
|
---|
368 |
|
---|
369 | NET_COUNT_IP6(net_count_ip6[NC_IP6_OUT_ERR_PACKETS], 1);
|
---|
370 | NET_COUNT_MIB(in6_ifstat.ipv6IfStatsOutDiscards, 1);
|
---|
371 | return error;
|
---|
372 | }
|
---|
373 |
|
---|
374 | #endif /* of #ifdef _IP6_CFG */
|
---|