source: EcnlProtoTool/trunk/asp3_dcre/tinet/net/ppp.c@ 270

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

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 10.8 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 * 上記著作権者
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: ppp.c 270 2017-02-09 04:03:47Z coas-nagasima $
44 */
45
46/*
47 * Copyright (c) 1989 Carnegie Mellon University.
48 * All rights reserved.
49 *
50 * Redistribution and use in source and binary forms are permitted
51 * provided that the above copyright notice and this paragraph are
52 * duplicated in all such forms and that any documentation,
53 * advertising materials, and other materials related to such
54 * distribution and use acknowledge that the software was developed
55 * by Carnegie Mellon University. The name of the
56 * University may not be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
60 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
61 */
62
63/*
64 * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
65 *
66 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
67 *
68 * Redistribution and use in source and binary forms are permitted
69 * provided that the above copyright notice and this paragraph are
70 * duplicated in all such forms and that any documentation,
71 * advertising materials, and other materials related to such
72 * distribution and use acknowledge that the software was developed
73 * by the Internet Initiative Japan. The name of the
74 * IIJ may not be used to endorse or promote products derived
75 * from this software without specific prior written permission.
76 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
77 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
78 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
79 */
80
81/*
82 * PPP 処理タスクの本体
83 */
84
85#include <s_services.h>
86#include <t_services.h>
87#include "kernel_id.h"
88
89#include <tinet_defs.h>
90#include <tinet_config.h>
91
92#include <net/if.h>
93#include <net/if_ppp.h>
94#include <net/net.h>
95#include <net/net_buf.h>
96#include <net/net_timer.h>
97#include <net/net_count.h>
98#include <net/ppp.h>
99#include <net/ppp_var.h>
100#include <net/ppp_hdlc.h>
101#include <net/ppp_fsm.h>
102#include <net/ppp_lcp.h>
103#include <net/ppp_ipcp.h>
104#include <net/ppp_ccp.h>
105#include <net/ppp_upap.h>
106#include <net/ppp_modem.h>
107
108#ifdef SUPPORT_PPP
109
110/*
111 * å…
112¨åŸŸå¤‰æ•°
113 */
114
115uint8_t ppp_phase = PPP_PHASE_INITIALIZE; /* リンクの状æ…
116‹ */
117
118/*
119 * プロトコル表
120 */
121
122T_PPP_PROTENT *protocols[] = {
123 &lcp_protent,
124 &ipcp_protent,
125
126#ifdef SUPPORT_CCP
127 &ccp_protent,
128#endif /* #ifdef SUPPORT_CCP */
129
130#ifdef LCP_CFG_PAP
131 &pap_protent,
132#endif /* #ifdef LCP_CFG_PAP */
133
134#ifdef LCP_CFG_CHAP
135 &chap_protent,
136#endif /* #ifdef LCP_CFG_CHAP */
137
138 NULL
139 };
140
141/*
142 * 変数
143 */
144
145#ifdef PPP_IDLE_TIMEOUT
146
147static bool_t idle = false;
148
149#endif /* of #ifdef PPP_IDLE_TIMEOUT */
150
151/*
152 * 関数
153 */
154
155static void parse_input (T_NET_BUF *input);
156
157#ifdef PPP_IDLE_TIMEOUT
158
159static void idle_timeout (void *arg);
160
161#endif /* of #ifdef PPP_IDLE_TIMEOUT */
162
163/*
164 * PPP å…
165¥åŠ›ã®è§£æž
166 */
167
168static void
169parse_input(T_NET_BUF *input)
170{
171 T_PPP_PROTENT *entry;
172 int_t ix;
173 uint16_t proto;
174
175 NET_COUNT_PPP(net_count_ppp.in_octets, input->len);
176 NET_COUNT_PPP(net_count_ppp.in_packets, 1);
177
178 /* フレーム長をチェックする */
179 if (input->len < sizeof(T_PPP_HDR)) { /* FreeBSD では PPP_HDRLEN は 4 */
180 syslog(LOG_NOTICE, "[PPP] recv short frame.");
181 goto buf_rel;
182 }
183
184 proto = ntohs(*GET_PPP_HDR(input));
185
186 /* プロトコルが LCP 以外で、LCP がオープンされていなければエラー */
187 if (proto != PPP_LCP && lcp_fsm.state != PPP_FSM_OPENED) {
188 syslog(LOG_NOTICE, "[PPP] LCP not open.");
189 goto buf_rel;
190 }
191
192 /* リンクが認証状æ…
193‹ä»¥å‰ã¯ã€LCP、LQR、PAP、CHAP のみ受け付ける */
194 if (ppp_phase <= PPP_PHASE_AUTHENTICATE &&
195 !(proto == PPP_LCP || proto == PPP_LQR ||
196 proto == PPP_PAP || proto == PPP_CHAP)) {
197 syslog(LOG_NOTICE, "[PPP] disc proto: 0x%04x, phase: %d.", proto, ppp_phase);
198 goto buf_rel;
199 }
200
201 /* 上位プロトコルのå…
202¥åŠ›é–¢æ•°ã‚’呼出す */
203 for (ix = 0; (entry = protocols[ix]) != NULL; ix ++) {
204 if (entry->proto == proto && entry->input != NULL) {
205 (*entry->input)(input);
206 syscall(rel_net_buf(input));
207 return;
208 }
209 if ((entry->proto & ~0x8000) == proto && entry->datainput != NULL) {
210 (*entry->datainput)(input);
211 return;
212 }
213 }
214
215 syslog(LOG_INFO, "[PPP] unexp proto: 0x%04x.", proto);
216 lcp_sprotrej(input);
217
218buf_rel:
219 NET_COUNT_PPP(net_count_ppp.in_err_packets, 1);
220 syscall(rel_net_buf(input));
221 }
222
223/*
224 * ppp_output -- PPP インタフェースの出力関数
225 */
226
227ER
228ppp_output (T_NET_BUF *output, TMO tmout)
229{
230 ER error = E_OK;
231
232#ifdef PPP_CFG_MODEM
233
234#if 0 /* 保留 */
235 /* モデムの接続完了まで待
236つ。*/
237 if ((error = wait_modem()) != E_OK)
238 goto buf_ret;
239#endif
240
241#endif /* of #ifdef PPP_CFG_MODEM */
242
243 /* IPCP の接続完了まで待
244つ。*/
245 if ((error = wait_ipcp()) != E_OK)
246 goto buf_ret;
247
248#ifdef PPP_IDLE_TIMEOUT
249 wai_sem(SEM_IDLE_TIMEOUT);
250 if (idle) {
251 untimeout((FP)idle_timeout, NULL);
252 idle = false;
253 }
254 sig_sem(SEM_IDLE_TIMEOUT);
255#endif /* of #ifdef PPP_IDLE_TIMEOUT */
256
257 /* PPP 出力キューに投å…
258¥ã™ã‚‹ã€‚*/
259 if ((error = tsnd_dtq(DTQ_PPP_OUTPUT, output, tmout)) != E_OK)
260 goto buf_ret;
261
262#ifdef PPP_IDLE_TIMEOUT
263 wai_sem(SEM_IDLE_TIMEOUT);
264 if (!idle && ppp_phase == PPP_PHASE_NETWORK) {
265 timeout((FP)idle_timeout, NULL, PPP_IDLE_TIMEOUT);
266 idle = true;
267 }
268 sig_sem(SEM_IDLE_TIMEOUT);
269#endif /* of #ifdef PPP_IDLE_TIMEOUT */
270
271 return error;
272
273buf_ret:
274 syscall(rel_net_buf(output));
275 NET_COUNT_PPP(net_count_ppp.out_err_packets, 1);
276 return error;
277 }
278
279/*
280 * ppp_cp_output -- CP フレームの送信
281 */
282
283void
284ppp_cp_output (uint8_t code, uint8_t id, T_NET_BUF *output)
285{
286 T_PPP_CP_HDR *cph;
287
288 /* CP ヘッダを設定する */
289 cph = GET_PPP_CP_HDR(output);
290 cph->code = code;
291 cph->id = id;
292 cph->len = htons(output->len - sizeof(T_PPP_HDR));
293
294 /* PPP 出力キューに投å…
295¥ã™ã‚‹ã€‚*/
296 if (snd_dtq(DTQ_PPP_OUTPUT, output) != E_OK) {
297 syscall(rel_net_buf(output));
298 NET_COUNT_PPP(net_count_ppp.out_err_packets, 1);
299 }
300 }
301
302/*
303 * PPP å…
304¥åŠ›ã‚¿ã‚¹ã‚¯
305 */
306
307void
308ppp_input_task(intptr_t exinf)
309{
310 T_NET_BUF *input;
311 T_PPP_PROTENT *proto;
312 ID tskid;
313 int_t ix;
314
315 /* ポートを初期設定する */
316 syscall(serial_opn_por(HDLC_PORTID));
317 syscall(serial_ctl_por(HDLC_PORTID, IOCTL_FCSND | IOCTL_FCRCV));
318
319 get_tid(&tskid);
320
321#ifdef PPP_CFG_MODEM
322
323 init_modem();
324 syslog(LOG_NOTICE, "[PPP INPUT:%d] started with modem on port %d.", tskid, HDLC_PORTID);
325
326#else /* of #ifdef PPP_CFG_MODEM */
327
328 syslog(LOG_NOTICE, "[PPP INPUT:%d] started on port %d.", tskid, HDLC_PORTID);
329
330#endif /* of #ifdef PPP_CFG_MODEM */
331
332 /* ネットワークタイマタスクを起動する */
333 syscall(act_tsk(NET_TIMER_TASK));
334
335 /* 上位プロトコルを初期化する */
336 for (ix = 0; (proto = protocols[ix]) != NULL; ix ++)
337 if (proto->init != NULL)
338 (*proto->init)();
339
340 lcp_lowerup();
341 lcp_open(PPP_OPEN_PASSIVE);
342
343 /* PPP 出力タスクを起動する */
344 syscall(act_tsk(PPP_OUTPUT_TASK));
345
346 while (true) {
347 if (tget_net_buf(&input, IF_PDU_SIZE, TMO_PPP_GET_NET_BUF) == E_OK) {
348 while (HDLC_read(input, IF_PDU_SIZE) != E_OK)
349 ;
350 if (input->len > 0) {
351
352 #ifdef PPP_IDLE_TIMEOUT
353 wai_sem(SEM_IDLE_TIMEOUT);
354 if (idle && ntohs(*GET_PPP_HDR(input)) != PPP_LCP) {
355 untimeout((FP)idle_timeout, NULL);
356 idle = false;
357 }
358 sig_sem(SEM_IDLE_TIMEOUT);
359 #endif /* of #ifdef PPP_IDLE_TIMEOUT */
360
361 parse_input(input);
362 }
363 else
364 syscall(rel_net_buf(input));
365
366 #ifdef PPP_IDLE_TIMEOUT
367 wai_sem(SEM_IDLE_TIMEOUT);
368 if (!idle && ppp_phase == PPP_PHASE_NETWORK) {
369 timeout((FP)idle_timeout, NULL, PPP_IDLE_TIMEOUT);
370 idle = true;
371 }
372 else if (idle && ppp_phase != PPP_PHASE_NETWORK) {
373 untimeout((FP)idle_timeout, NULL);
374 idle = false;
375 }
376 sig_sem(SEM_IDLE_TIMEOUT);
377 #endif /* of #ifdef PPP_IDLE_TIMEOUT */
378
379 }
380 else {
381 HDLC_dummy_read();
382 NET_COUNT_PPP(net_count_ppp.in_err_packets, 1);
383 NET_COUNT_PPP(net_count_ppp_no_bufs, 1);
384 }
385 }
386 }
387
388/*
389 * PPP 出力タスク
390 */
391
392void
393ppp_output_task(intptr_t exinf)
394{
395 T_NET_BUF *output;
396 ID tskid;
397
398 get_tid(&tskid);
399 syslog(LOG_NOTICE, "[PPP OUTPUT:%d] started.", tskid);
400
401 while (true) {
402 while (rcv_dtq(DTQ_PPP_OUTPUT, (intptr_t*)&output) == E_OK) {
403 NET_COUNT_PPP(net_count_ppp.out_octets, output->len);
404 NET_COUNT_PPP(net_count_ppp.out_packets, 1);
405 syscall(HDLC_write(output));
406 syscall(rel_net_buf(output));
407 }
408 }
409 }
410
411#ifdef PPP_IDLE_TIMEOUT
412
413/*
414 * アイドルタイムアウト処理
415 */
416
417static void
418idle_timeout (void *arg)
419{
420 syslog(LOG_NOTICE, "[PPP] idel %d [s], disconnecting.", (uint16_t)(PPP_IDLE_TIMEOUT / NET_TIMER_HZ));
421 lcp_close();
422 }
423
424#endif /* of #ifdef PPP_IDLE_TIMEOUT */
425
426#endif /* fo #ifdef SUPPORT_PPP */
Note: See TracBrowser for help on using the repository browser.