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, 1995
|
---|
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 |
|
---|
67 | #if (API_PROTO == API_PROTO_IPV6) || (TNUM_UDP4_CEPID > 0)
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * udp_input_select -- UDP の入力処理の選択
|
---|
71 | */
|
---|
72 |
|
---|
73 | void
|
---|
74 | UDP_INPUT_SELECT (T_UDP_CEP *cep, T_NET_BUF *input, uint_t off)
|
---|
75 | {
|
---|
76 | T_UDP_HDR *udph;
|
---|
77 | T_UDP_RCV_DAT_PARA para;
|
---|
78 |
|
---|
79 | udph = (T_UDP_HDR *)(input->buf + off);
|
---|
80 | para.len = (uint_t)(ntohs(udph->ulen) - UDP_HDR_SIZE);
|
---|
81 |
|
---|
82 | if (cep->rcv_tskid != TA_NULL) { /* 非ノンブロッキングコールでペンディング中 */
|
---|
83 | if (psnd_dtq(cep->rcvqid, (intptr_t)input) != E_OK) {
|
---|
84 | NET_COUNT_UDP(net_count_udp.in_err_packets, 1);
|
---|
85 | NET_COUNT_MIB(udp_stats.udpInErrors, 1);
|
---|
86 | syscall(rel_net_buf(input));
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | #ifdef UDP_CFG_NON_BLOCKING
|
---|
91 |
|
---|
92 | else if (cep->rcv_p_dstaddr != NULL) { /* ノンブロッキングコールでペンディング中 */
|
---|
93 |
|
---|
94 | /* p_dstaddr を設定する。*/
|
---|
95 | cep->rcv_p_dstaddr->portno = ntohs(udph->sport);
|
---|
96 | IN_COPY_TO_HOST(&cep->rcv_p_dstaddr->ipaddr, input);
|
---|
97 |
|
---|
98 | /* データをバッファに移す。*/
|
---|
99 | memcpy(cep->rcv_data, GET_UDP_SDU(input, off),
|
---|
100 | (size_t)(para.len < cep->rcv_len ? para.len : cep->rcv_len));
|
---|
101 | syscall(rel_net_buf(input));
|
---|
102 |
|
---|
103 | if (IS_PTR_DEFINED(cep->callback))
|
---|
104 |
|
---|
105 | #ifdef TCP_CFG_NON_BLOCKING_COMPAT14
|
---|
106 | (*cep->callback)(GET_UDP_CEPID(cep), TFN_UDP_RCV_DAT, (void*)(uint32_t)para.len);
|
---|
107 | #else
|
---|
108 | (*cep->callback)(GET_UDP_CEPID(cep), TFN_UDP_RCV_DAT, (void*)¶.len);
|
---|
109 | #endif
|
---|
110 | else
|
---|
111 | syslog(LOG_WARNING, "[UDP] no call back, CEP: %d.", GET_UDP_CEPID(cep));
|
---|
112 | cep->rcv_p_dstaddr = NULL;
|
---|
113 | }
|
---|
114 |
|
---|
115 | #endif/* of #ifdef UDP_CFG_NON_BLOCKING */
|
---|
116 |
|
---|
117 | else if (IS_PTR_DEFINED(cep->callback)) {
|
---|
118 |
|
---|
119 | /* コールバック関数を呼び出す。*/
|
---|
120 | cep->cb_netbuf = input;
|
---|
121 |
|
---|
122 | #ifdef TCP_CFG_NON_BLOCKING_COMPAT14
|
---|
123 | (*cep->callback)(GET_UDP_CEPID(cep), TEV_UDP_RCV_DAT, (void*)(uint32_t)para.len);
|
---|
124 | #else
|
---|
125 | para.input = input;
|
---|
126 | para.off = off;
|
---|
127 | #if API_PROTO == API_PROTO_IPV4
|
---|
128 | para.rep4.portno = ntohs(udph->sport);
|
---|
129 | IN_COPY_TO_HOST(¶.rep4.ipaddr, input);
|
---|
130 | #else
|
---|
131 | para.rep6.portno = ntohs(udph->sport);
|
---|
132 | IN_COPY_TO_HOST(¶.rep6.ipaddr, input);
|
---|
133 | #endif
|
---|
134 | (*cep->callback)(GET_UDP_CEPID(cep), TEV_UDP_RCV_DAT, (void*)¶.len);
|
---|
135 | #endif
|
---|
136 | /*
|
---|
137 | * ネットワークバッファがそのままであれば、コールバック関数内で
|
---|
138 | * データを読み出さなかったことになるので、捨てる。
|
---|
139 | */
|
---|
140 | if (cep->cb_netbuf != NULL) {
|
---|
141 | if ((input->flags & NB_FLG_NOREL_IFOUT) == 0)
|
---|
142 | syscall(rel_net_buf(cep->cb_netbuf));
|
---|
143 | cep->cb_netbuf = NULL;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | else {
|
---|
147 | NET_COUNT_UDP(net_count_udp.in_err_packets, 1);
|
---|
148 | NET_COUNT_MIB(udp_stats.udpInErrors, 1);
|
---|
149 | syscall(rel_net_buf(input));
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | #endif /* of #if (API_PROTO == API_PROTO_IPV6) || (TNUM_UDP4_CEPID > 0) */
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * udp_input -- UDP の入力関数
|
---|
158 | */
|
---|
159 |
|
---|
160 | uint_t
|
---|
161 | UDP_INPUT (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp)
|
---|
162 | {
|
---|
163 | T_NET_BUF *input = *inputp;
|
---|
164 | T_UDP_CEP *cep;
|
---|
165 | T_UDP_HDR *udph;
|
---|
166 | uint_t hlen, ulen;
|
---|
167 |
|
---|
168 | #if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4
|
---|
169 | T_UDP6_CEP *cep6;
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | hlen = (uint_t)GET_IF_IP_HDR_SIZE(input);
|
---|
173 |
|
---|
174 | NET_COUNT_UDP(net_count_udp.in_octets, input->len - hlen);
|
---|
175 | NET_COUNT_UDP(net_count_udp.in_packets, 1);
|
---|
176 | NET_COUNT_MIB(udp_stats.udpInDatagrams, 1);
|
---|
177 |
|
---|
178 | /* ヘッダ長をチェックする。*/
|
---|
179 | if (input->len < hlen + UDP_HDR_SIZE)
|
---|
180 | goto buf_rel;
|
---|
181 |
|
---|
182 | udph = (T_UDP_HDR *)(input->buf + *offp);
|
---|
183 |
|
---|
184 | /* データグラム長をチェックする */
|
---|
185 | ulen = ntohs(udph->ulen);
|
---|
186 | if (ulen != input->len - hlen)
|
---|
187 | goto buf_rel;
|
---|
188 |
|
---|
189 | /* 宛先ポートが 0 のデータグラムは破棄する。RFC768 */
|
---|
190 | if (udph->dport == 0)
|
---|
191 | goto buf_rel;
|
---|
192 |
|
---|
193 | #ifdef UDP_CFG_IN_CHECKSUM
|
---|
194 |
|
---|
195 | /* チェックサムをチェックする */
|
---|
196 | if (udph->sum && IN_CKSUM(input, IPPROTO_UDP, *offp, ulen) != 0)
|
---|
197 | goto buf_rel;
|
---|
198 |
|
---|
199 | #endif /* of #ifdef UDP_CFG_IN_CHECKSUM */
|
---|
200 |
|
---|
201 | #if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4
|
---|
202 |
|
---|
203 | #if TNUM_UDP4_CEPID > 0
|
---|
204 |
|
---|
205 | /* UDP 通信端点を得る。*/
|
---|
206 | cep = udp4_find_cep(input, *offp);
|
---|
207 |
|
---|
208 | if (cep != NULL)
|
---|
209 | udp4_input_select(cep, input, *offp);
|
---|
210 | else if ((cep6 = udp6_find_cep(input, *offp)) != NULL)
|
---|
211 | udp6_input_select(cep6, input, *offp);
|
---|
212 | else
|
---|
213 | udp4_reply_no_port(input, *offp);
|
---|
214 |
|
---|
215 | return IPPROTO_DONE;
|
---|
216 |
|
---|
217 | #else /* of #if TNUM_UDP4_CEPID > 0 */
|
---|
218 |
|
---|
219 | cep = 0; /* warning 回避用のダミー */
|
---|
220 | if ((cep6 = udp6_find_cep(input, *offp)) != NULL)
|
---|
221 | udp6_input_select(cep6, input, *offp);
|
---|
222 | else
|
---|
223 | udp4_reply_no_port(input, *offp);
|
---|
224 |
|
---|
225 | return IPPROTO_DONE;
|
---|
226 |
|
---|
227 | #endif /* of #if TNUM_UDP4_CEPID > 0 */
|
---|
228 |
|
---|
229 | #else /* of #if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4 */
|
---|
230 |
|
---|
231 | /* UDP 通信端点を得る。*/
|
---|
232 | cep = UDP_FIND_CEP(input, *offp);
|
---|
233 |
|
---|
234 | if (cep != NULL)
|
---|
235 | UDP_INPUT_SELECT(cep, input, *offp);
|
---|
236 | else
|
---|
237 | UDP_REPLY_NO_PORT(input, *offp);
|
---|
238 |
|
---|
239 | return IPPROTO_DONE;
|
---|
240 |
|
---|
241 | #endif /* of #if defined(_IP6_CFG) && defined(_IP4_CFG) && API_PROTO == API_PROTO_IPV4 */
|
---|
242 |
|
---|
243 | buf_rel:
|
---|
244 | NET_COUNT_UDP(net_count_udp.in_err_packets, 1);
|
---|
245 | NET_COUNT_MIB(udp_stats.udpInErrors, 1);
|
---|
246 | syscall(rel_net_buf(input));
|
---|
247 | return IPPROTO_DONE;
|
---|
248 | }
|
---|