source: azure_iot_hub/trunk/asp3_dcre/tinet/netinet/ip_output.c@ 388

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

Azure IoT Hub Device C SDK を使ったサンプルの追加

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