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: ppp_ipcp.c 315 2017-07-23 05:29:40Z coas-nagasima $
|
---|
32 | */
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * ipcp.c - PPP IP Control Protocol.
|
---|
36 | *
|
---|
37 | * Copyright (c) 1989 Carnegie Mellon University.
|
---|
38 | * All rights reserved.
|
---|
39 | *
|
---|
40 | * Redistribution and use in source and binary forms are permitted
|
---|
41 | * provided that the above copyright notice and this paragraph are
|
---|
42 | * duplicated in all such forms and that any documentation,
|
---|
43 | * advertising materials, and other materials related to such
|
---|
44 | * distribution and use acknowledge that the software was developed
|
---|
45 | * by Carnegie Mellon University. The name of the
|
---|
46 | * University may not be used to endorse or promote products derived
|
---|
47 | * from this software without specific prior written permission.
|
---|
48 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
---|
49 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
---|
50 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
51 | */
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * PPP IP Control Protocol (IPCP) Module
|
---|
55 | *
|
---|
56 | * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
---|
57 | *
|
---|
58 | * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
---|
59 | *
|
---|
60 | * Redistribution and use in source and binary forms are permitted
|
---|
61 | * provided that the above copyright notice and this paragraph are
|
---|
62 | * duplicated in all such forms and that any documentation,
|
---|
63 | * advertising materials, and other materials related to such
|
---|
64 | * distribution and use acknowledge that the software was developed
|
---|
65 | * by the Internet Initiative Japan, Inc. The name of the
|
---|
66 | * IIJ may not be used to endorse or promote products derived
|
---|
67 | * from this software without specific prior written permission.
|
---|
68 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
---|
69 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
---|
70 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
71 | *
|
---|
72 | * $FreeBSD: src/usr.sbin/ppp/ipcp.c,v 1.90.2.4 2000/08/19 09:30:03 brian Exp $
|
---|
73 | *
|
---|
74 | * TODO:
|
---|
75 | * o Support IPADDRS properly
|
---|
76 | * o Validate the length in IpcpDecodeConfig
|
---|
77 | */
|
---|
78 |
|
---|
79 | #include <string.h>
|
---|
80 |
|
---|
81 | #include <s_services.h>
|
---|
82 | #include <t_services.h>
|
---|
83 | #include "kernel_id.h"
|
---|
84 |
|
---|
85 | #include <tinet_defs.h>
|
---|
86 | #include <tinet_config.h>
|
---|
87 |
|
---|
88 | #include <net/if.h>
|
---|
89 | #include <net/if_ppp.h>
|
---|
90 | #include <net/net.h>
|
---|
91 | #include <net/net_endian.h>
|
---|
92 | #include <net/net_buf.h>
|
---|
93 | #include <net/net_count.h>
|
---|
94 | #include <net/ppp.h>
|
---|
95 | #include <net/ppp_var.h>
|
---|
96 | #include <net/ppp_fsm.h>
|
---|
97 | #include <net/ppp_lcp.h>
|
---|
98 | #include <net/ppp_ipcp.h>
|
---|
99 | #include <net/ppp_modem.h>
|
---|
100 |
|
---|
101 | #include <netinet/in.h>
|
---|
102 | #include <netinet/in_var.h>
|
---|
103 | #include <netinet/ip.h>
|
---|
104 | #include <netinet/tcp.h>
|
---|
105 | #include <netinet/tcp_var.h>
|
---|
106 | #include <netinet/tcp_timer.h>
|
---|
107 | #include <netinet/udp_var.h>
|
---|
108 |
|
---|
109 | #include <net/if_var.h>
|
---|
110 |
|
---|
111 | #ifdef SUPPORT_PPP
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * 関数
|
---|
115 | */
|
---|
116 |
|
---|
117 | static void ipcp_init (void);
|
---|
118 | static void ipcp_input (T_NET_BUF *input);
|
---|
119 | static void ipcp_protrej (void);
|
---|
120 | static void ipcp_lowerup (void);
|
---|
121 | static void ipcp_lowerdown (void);
|
---|
122 | static void ipcp_open (void);
|
---|
123 | static void ipcp_close (void);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * FSM から呼出されるコールバック関
|
---|
127 | */
|
---|
128 |
|
---|
129 | static void ipcp_resetci(T_PPP_FSM *fsm); /* 自構成情報をリセットする。 */
|
---|
130 | static int_t ipcp_cilen (T_PPP_FSM *fsm); /* 構成情報の長さを返す。 */
|
---|
131 | static void ipcp_addci (T_PPP_FSM *fsm, T_NET_BUF *output);
|
---|
132 | /* 自構成情報を追加する。 */
|
---|
133 | static bool_t ipcp_ackci (T_PPP_FSM *fsm, T_NET_BUF *input);
|
---|
134 | /* ACK を受信したときの処理 */
|
---|
135 | static bool_t ipcp_nakci (T_PPP_FSM *fsm, T_NET_BUF *input);
|
---|
136 | /* NAK を受信したときの処理 */
|
---|
137 | static bool_t ipcp_rejci (T_PPP_FSM *fsm, T_NET_BUF *input);
|
---|
138 | /* REJ を受信したときの処理 */
|
---|
139 | static int_t ipcp_reqci (T_PPP_FSM *fsm, T_NET_BUF *input, T_NET_BUF *output);
|
---|
140 | /* 相手の構成情報を確認する。 */
|
---|
141 | static void ipcp_up (T_PPP_FSM *fsm); /* リンク接続を確立する。 */
|
---|
142 | static void ipcp_down (T_PPP_FSM *fsm); /* リンク接続を解放する。 */
|
---|
143 | static void ipcp_finished(T_PPP_FSM *fsm); /* 下位層を終了する。 */
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * 変数
|
---|
147 | */
|
---|
148 |
|
---|
149 | static T_IFNET ipcp_local_def_cfg = { /* 自構成情報の規定値 */
|
---|
150 | NULL,
|
---|
151 | {
|
---|
152 | IPV4_ADDR_LOCAL, /* IPv4 アドレス */
|
---|
153 | UINT_C(0), /* サブネットマスク */
|
---|
154 | },
|
---|
155 | };
|
---|
156 |
|
---|
157 | static T_IFNET ipcp_remote_def_cfg = { /* 相手の構成情報の規定値 */
|
---|
158 | NULL,
|
---|
159 | {
|
---|
160 | IPV4_ADDR_REMOTE, /* IPv4 アドレス */
|
---|
161 | UINT_C(0), /* サブネットマスク */
|
---|
162 | }
|
---|
163 | };
|
---|
164 |
|
---|
165 | static T_IFNET ipcp_local_neg_cfg; /* ネゴ中の自構成情報 */
|
---|
166 |
|
---|
167 | static T_IFNET ipcp_remote_neg_cfg; /* ネゴ中の相手の構成情報 */
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * 全域変数
|
---|
171 | */
|
---|
172 |
|
---|
173 | T_PPP_FSM_CALLBACKS ipcp_callbacks = {
|
---|
174 | ipcp_resetci, /* 自構成情報をリセットする。 */
|
---|
175 | ipcp_cilen, /* 構成情報の長さを返す。 */
|
---|
176 | ipcp_addci, /* 自構成情報を追加する。 */
|
---|
177 | ipcp_ackci, /* ACK を受信したときの処理 */
|
---|
178 | ipcp_nakci, /* NAK を受信したときの処理 */
|
---|
179 | ipcp_rejci, /* REJ を受信したときの処理 */
|
---|
180 | ipcp_reqci, /* 相手の構成情報を確認する。 */
|
---|
181 | ipcp_up, /* リンク接続を確立する。 */
|
---|
182 | ipcp_down, /* リンク接続を解放する。 */
|
---|
183 | NULL, /* 下位層を開始する。 */
|
---|
184 | ipcp_finished, /* 下位層を終了する。 */
|
---|
185 | NULL, /* 再送する。 */
|
---|
186 | NULL, /* 不明な CP を受信したときの処理 */
|
---|
187 | };
|
---|
188 |
|
---|
189 | T_PPP_FSM ipcp_fsm = {
|
---|
190 | &ipcp_callbacks, /* コールバック関数 */
|
---|
191 | PPP_IPCP, /* プロトコル */
|
---|
192 | };
|
---|
193 |
|
---|
194 | T_PPP_PROTENT ipcp_protent = {
|
---|
195 | PPP_IPCP,
|
---|
196 | ipcp_init, /* 初期化 */
|
---|
197 | ipcp_input, /* 入力 */
|
---|
198 | ipcp_protrej, /* Proto-REJ 受信処理 */
|
---|
199 | ipcp_lowerup, /* 下位層を起動する */
|
---|
200 | ipcp_lowerdown, /* 下位層を停止する */
|
---|
201 | ipcp_open, /* オープンする */
|
---|
202 | ipcp_close, /* クローズする */
|
---|
203 | ip_input, /* データ入力、IPv4 入力 */
|
---|
204 | };
|
---|
205 |
|
---|
206 | T_IFNET ipcp_local_ack_cfg; /* 相手に許可されたの自構成情報 */
|
---|
207 | T_IFNET ipcp_remote_ack_cfg; /* 相手に許可した構成情報 */
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * ppp_get_ifnet -- ネットワークインタフェース構造体を返す。
|
---|
211 | */
|
---|
212 |
|
---|
213 | T_IFNET *
|
---|
214 | ppp_get_ifnet (void)
|
---|
215 | {
|
---|
216 | return &ipcp_local_ack_cfg;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * wait_ipcp -- IP 接続完了まで待つ。
|
---|
221 | *
|
---|
222 | * 戻り値: 接続に失敗したら E_OBJ を返す。
|
---|
223 | */
|
---|
224 |
|
---|
225 | ER
|
---|
226 | wait_ipcp (void)
|
---|
227 | {
|
---|
228 | #ifdef PPP_CFG_MODEM
|
---|
229 | ER error;
|
---|
230 | #endif /* of #ifdef PPP_CFG_MODEM */
|
---|
231 |
|
---|
232 | if (ipcp_fsm.state <= PPP_FSM_STOPPED) {
|
---|
233 |
|
---|
234 | #ifdef PPP_CFG_MODEM
|
---|
235 |
|
---|
236 | /* モデムの接続完了まで待つ。*/
|
---|
237 | if ((error = wait_modem()) != E_OK)
|
---|
238 | return error;
|
---|
239 |
|
---|
240 | #else /* of #ifdef PPP_CFG_MODEM */
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * 受動オープンし、相手からの接続待ちなら LCP を一度クローズし、
|
---|
244 | * 能動オープン状態で、再オープンする。
|
---|
245 | */
|
---|
246 | lcp_close();
|
---|
247 | lcp_open(PPP_OPEN_ACTIVE);
|
---|
248 |
|
---|
249 | #endif /* of #ifdef PPP_CFG_MODEM */
|
---|
250 |
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (ipcp_fsm.state != PPP_FSM_OPENED) {
|
---|
254 | /* IP 接続完了まで待つ。*/
|
---|
255 | wai_sem(SEM_IPCP_READY);
|
---|
256 | if (ipcp_fsm.state != PPP_FSM_OPENED)
|
---|
257 | return E_OBJ;
|
---|
258 | }
|
---|
259 |
|
---|
260 | return E_OK;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /*
|
---|
264 | * ipcp_init -- IPCP モジュールの初期化
|
---|
265 | */
|
---|
266 |
|
---|
267 | static void
|
---|
268 | ipcp_init (void)
|
---|
269 | {
|
---|
270 | fsm_init(&ipcp_fsm);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * ipcp_input -- IPCP 入力
|
---|
275 | */
|
---|
276 |
|
---|
277 | static void
|
---|
278 | ipcp_input (T_NET_BUF *input)
|
---|
279 | {
|
---|
280 | NET_COUNT_PPP_IPCP(net_count_ppp_ipcp_in_octets, input->len);
|
---|
281 | NET_COUNT_PPP_IPCP(net_count_ppp_ipcp_in_packets, 1);
|
---|
282 | fsm_input(&ipcp_fsm, input);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * ipcp_resetci -- IPCPの構成情報をリセットする。
|
---|
287 | */
|
---|
288 |
|
---|
289 | static void
|
---|
290 | ipcp_resetci (T_PPP_FSM *fsm)
|
---|
291 | {
|
---|
292 | /* 自構成情報の初期設定 */
|
---|
293 | ipcp_local_neg_cfg = ipcp_local_def_cfg;
|
---|
294 | memset((void*)&ipcp_local_ack_cfg, 0, sizeof(T_IFNET));
|
---|
295 |
|
---|
296 | /* 相手の構成情報の初期設定 */
|
---|
297 | ipcp_remote_neg_cfg = ipcp_remote_def_cfg;
|
---|
298 | memset((void*)&ipcp_remote_ack_cfg, 0, sizeof(T_IFNET));
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * ipcp_cilen -- 構成情報の長さを返す。
|
---|
303 | */
|
---|
304 |
|
---|
305 | static int_t
|
---|
306 | ipcp_cilen (T_PPP_FSM *fsm)
|
---|
307 | {
|
---|
308 | int_t cilen = 0;
|
---|
309 |
|
---|
310 | cilen += sizeof(T_PPP_CI_HDR) + sizeof(uint32_t); /* IPv4 アドレス */
|
---|
311 |
|
---|
312 | return cilen;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /*
|
---|
316 | * ipcp_addci -- IPCPの構成情報を追加する。
|
---|
317 | */
|
---|
318 |
|
---|
319 | static void
|
---|
320 | ipcp_addci (T_PPP_FSM *fsm, T_NET_BUF *output)
|
---|
321 | {
|
---|
322 | uint8_t *buf;
|
---|
323 |
|
---|
324 | buf = output->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
325 |
|
---|
326 | /* IPv4 アドレスオプションを追加する。 */
|
---|
327 | *buf ++ = IPCP_CIT_ADDR;
|
---|
328 | *buf ++ = sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
329 | ahtonl(buf, ipcp_local_neg_cfg.in4_ifaddr.addr);
|
---|
330 | buf += sizeof(uint32_t);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * ipcp_ackci -- ACK を受信したときの処理
|
---|
335 | *
|
---|
336 | * 戻り値: true ACK は正常
|
---|
337 | * false ACK は異常
|
---|
338 | */
|
---|
339 |
|
---|
340 | static bool_t
|
---|
341 | ipcp_ackci (T_PPP_FSM *fsm, T_NET_BUF *input)
|
---|
342 | {
|
---|
343 | uint8_t *buf;
|
---|
344 | uint16_t len;
|
---|
345 |
|
---|
346 | buf = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
347 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * 構成情報は送信した順に完全に一致しなければならない。
|
---|
351 | * 従って、ipcp_addci で作成した順に解析する。
|
---|
352 | */
|
---|
353 |
|
---|
354 | /* ADDR オプションを解析する。 */
|
---|
355 | if (len < sizeof(T_PPP_CI_HDR) + sizeof(uint32_t) ||
|
---|
356 | *buf ++ != IPCP_CIT_ADDR ||
|
---|
357 | *buf ++ != sizeof(T_PPP_CI_HDR) + sizeof(uint32_t)||
|
---|
358 | nahcmpl(buf, ipcp_local_neg_cfg.in4_ifaddr.addr))
|
---|
359 | return false;
|
---|
360 | buf += sizeof(uint32_t);
|
---|
361 | len -= sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
362 |
|
---|
363 | return true;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * ipcp_nakci -- NAK を受信したときの処理
|
---|
368 | *
|
---|
369 | * 戻り値: 1 NAK は正常
|
---|
370 | * 0 NAK は異常
|
---|
371 | */
|
---|
372 |
|
---|
373 | static int_t
|
---|
374 | ipcp_nakci (T_PPP_FSM *fsm, T_NET_BUF *input)
|
---|
375 | {
|
---|
376 | T_IFNET new_cfg;
|
---|
377 | uint32_t cilong;
|
---|
378 | uint16_t len;
|
---|
379 | uint8_t *buf, cilen;
|
---|
380 |
|
---|
381 | buf = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
382 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
383 |
|
---|
384 | new_cfg = ipcp_local_neg_cfg;
|
---|
385 |
|
---|
386 | /* ADDR オプションを解析する。 */
|
---|
387 | if (len >= sizeof(T_PPP_CI_HDR) + sizeof(uint32_t) &&
|
---|
388 | * buf == IPCP_CIT_ADDR &&
|
---|
389 | *(buf + 1) == sizeof(T_PPP_CI_HDR) + sizeof(uint32_t)) {
|
---|
390 | ntoahl(cilong, buf + 2);
|
---|
391 | buf += sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
392 | len -= sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
393 | new_cfg.in4_ifaddr.addr = cilong;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /* 後は無視する。*/
|
---|
397 | while (len > sizeof(T_PPP_CI_HDR)) {
|
---|
398 | cilen = *(buf + 1);
|
---|
399 | if (len < cilen)
|
---|
400 | return 0;
|
---|
401 | buf += cilen;
|
---|
402 | len -= cilen;
|
---|
403 | }
|
---|
404 |
|
---|
405 | /* 長さが 0 でなければエラー */
|
---|
406 | if (len != 0)
|
---|
407 | return 0;
|
---|
408 |
|
---|
409 | /* 新しいオプションを設定する。*/
|
---|
410 | if (fsm->state != PPP_FSM_OPENED)
|
---|
411 | ipcp_local_neg_cfg = new_cfg;
|
---|
412 |
|
---|
413 | return 1;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * ipcp_rejci -- REJ を受信したときの処理
|
---|
418 | *
|
---|
419 | * 戻り値: 1 REJ は正常
|
---|
420 | * 0 REJ は異常
|
---|
421 | */
|
---|
422 |
|
---|
423 | static int_t
|
---|
424 | ipcp_rejci (T_PPP_FSM *fsm, T_NET_BUF *input)
|
---|
425 | {
|
---|
426 | T_IFNET new_cfg;
|
---|
427 | uint8_t *buf;
|
---|
428 | uint16_t len;
|
---|
429 |
|
---|
430 | buf = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
431 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
432 |
|
---|
433 | new_cfg = ipcp_local_neg_cfg;
|
---|
434 |
|
---|
435 | /* ADDR オプションを解析する。 */
|
---|
436 | if (len >= sizeof(T_PPP_CI_HDR) + sizeof(uint32_t) &&
|
---|
437 | * buf == IPCP_CIT_ADDR &&
|
---|
438 | *(buf + 1) == sizeof(T_PPP_CI_HDR) + sizeof(uint32_t)) {
|
---|
439 | buf += sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
440 | len -= sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
441 | new_cfg.in4_ifaddr.addr = 0;
|
---|
442 | }
|
---|
443 |
|
---|
444 | /* 長さが 0 でなければエラー */
|
---|
445 | if (len != 0)
|
---|
446 | return 0;
|
---|
447 |
|
---|
448 | /* 新しいオプションを設定する。*/
|
---|
449 | if (fsm->state != PPP_FSM_OPENED)
|
---|
450 | ipcp_local_neg_cfg = new_cfg;
|
---|
451 |
|
---|
452 | return 1;
|
---|
453 | }
|
---|
454 |
|
---|
455 | /*
|
---|
456 | * ipcp_reqci -- 相手の構成情報を解析する。
|
---|
457 | */
|
---|
458 |
|
---|
459 | static int_t
|
---|
460 | ipcp_reqci (T_PPP_FSM *fsm, T_NET_BUF *input, T_NET_BUF *output)
|
---|
461 | {
|
---|
462 | uint32_t addr;
|
---|
463 | uint16_t code, final, len, cilen;
|
---|
464 | uint8_t *np, *rp, *ap, *cp, type = 0;
|
---|
465 |
|
---|
466 | rp = ap = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
467 | /* rp: REJ、ap: ACK する CI のポインタ */
|
---|
468 | /* 拒否する場合は、引数 buf に上書きする。 */
|
---|
469 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
470 | np = output->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
471 | final = PPP_CONFACK; /* 最終的に送信するコード */
|
---|
472 | while (len > 0) {
|
---|
473 | code = PPP_CONFACK;
|
---|
474 | cp = ap; /* cp: 現在処理中の CI */
|
---|
475 |
|
---|
476 | if (len < sizeof(T_PPP_CI_HDR)) {
|
---|
477 |
|
---|
478 | /* 残りが CI ヘッダより短い */
|
---|
479 | syslog(LOG_NOTICE, "[PPP/IPCP] bad CI len: %d.", len);
|
---|
480 | cilen = len; /* データ長が異常の場合の処置 */
|
---|
481 | len = 0; /* ループから抜ける。 */
|
---|
482 | code = PPP_CONFREJ;
|
---|
483 | goto endswitch;
|
---|
484 | }
|
---|
485 |
|
---|
486 | type = *ap ++; /* CI の型 */
|
---|
487 | cilen = *ap ++; /* CI の長さ */
|
---|
488 | if (len < cilen) {
|
---|
489 |
|
---|
490 | /* 残りが CI 長より短い */
|
---|
491 | syslog(LOG_NOTICE, "[PPP/IPCP] bad CI len: %d.", cilen);
|
---|
492 | cilen = len; /* データ長が異常の場合の処置 */
|
---|
493 | len = 0; /* ループから抜ける。 */
|
---|
494 | code = PPP_CONFREJ;
|
---|
495 | goto endswitch;
|
---|
496 | }
|
---|
497 | len -= sizeof(T_PPP_CI_HDR);
|
---|
498 |
|
---|
499 | /* CI の型により分岐する。*/
|
---|
500 | switch (type) {
|
---|
501 |
|
---|
502 | case IPCP_CIT_ADDR: /* IPv4 アドレス */
|
---|
503 |
|
---|
504 | /* CI 長が、ヘッダ + 4 オクテットでなければエラー */
|
---|
505 | if (cilen != sizeof(T_PPP_CI_HDR) + sizeof(uint32_t))
|
---|
506 | code = PPP_CONFREJ;
|
---|
507 | else {
|
---|
508 | ntoahl(addr, ap);
|
---|
509 | if (addr != ipcp_remote_neg_cfg.in4_ifaddr.addr &&
|
---|
510 | (addr == 0 || ipcp_remote_neg_cfg.in4_ifaddr.addr != 0)) {
|
---|
511 | *np ++ = IPCP_CIT_ADDR;
|
---|
512 | *np ++ = sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
513 | ahtonl(np, ipcp_remote_neg_cfg.in4_ifaddr.addr);
|
---|
514 | np += sizeof(uint32_t);
|
---|
515 | code = PPP_CONFNAK;
|
---|
516 | }
|
---|
517 | #if 1 /* 要確認 */
|
---|
518 | else if (addr == 0 && ipcp_remote_neg_cfg.in4_ifaddr.addr == 0)
|
---|
519 | #else
|
---|
520 | else if (addr == 0 || ipcp_remote_neg_cfg.in4_ifaddr.addr == 0)
|
---|
521 | #endif
|
---|
522 | code = PPP_CONFREJ;
|
---|
523 | else
|
---|
524 | ipcp_remote_neg_cfg.in4_ifaddr.addr = addr;
|
---|
525 | }
|
---|
526 | break;
|
---|
527 |
|
---|
528 | default:
|
---|
529 | syslog(LOG_INFO, "[PPP/IPCP] unexp opt: %d.", type);
|
---|
530 | code = PPP_CONFREJ;
|
---|
531 | break;
|
---|
532 | }
|
---|
533 | endswitch:
|
---|
534 |
|
---|
535 | if (code == PPP_CONFNAK) {
|
---|
536 | /* CI の型が Magic Number の場合を除いて、NAK の回数が */
|
---|
537 | /* 最大値 MAX_PPP_FAILURES を超えたら拒否する。 */
|
---|
538 | if (fsm->failures >= MAX_PPP_FAILURES)
|
---|
539 | code = PPP_CONFREJ;
|
---|
540 | else if (final != PPP_CONFREJ)
|
---|
541 | final = PPP_CONFNAK;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* この CI を拒否する */
|
---|
545 | if (code == PPP_CONFREJ) {
|
---|
546 | if (rp < cp) /* 前に詰める。*/
|
---|
547 | memcpy(rp, cp, cilen);
|
---|
548 | rp += cilen;
|
---|
549 | final = PPP_CONFREJ;
|
---|
550 | }
|
---|
551 |
|
---|
552 | ap += cilen - sizeof(T_PPP_CI_HDR);
|
---|
553 | len -= cilen - sizeof(T_PPP_CI_HDR);
|
---|
554 | }
|
---|
555 |
|
---|
556 | /* 最終的な長さを調整する。*/
|
---|
557 | switch (final) {
|
---|
558 | case PPP_CONFNAK:
|
---|
559 | output->len = np - output->buf;
|
---|
560 | memcpy(output->buf, input->buf, sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
561 | break;
|
---|
562 | case PPP_CONFREJ:
|
---|
563 | memcpy(output->buf, input->buf, output->len = rp - input->buf);
|
---|
564 | break;
|
---|
565 | case PPP_CONFACK:
|
---|
566 | memcpy(output->buf, input->buf, input->len);
|
---|
567 | break;
|
---|
568 | }
|
---|
569 |
|
---|
570 | return final;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * ipcp_up -- リンク接続を確立する。
|
---|
575 | */
|
---|
576 |
|
---|
577 | static void
|
---|
578 | ipcp_up (T_PPP_FSM *fsm)
|
---|
579 | {
|
---|
580 | /* 自構成情報の初期設定 */
|
---|
581 | ipcp_local_ack_cfg = ipcp_local_neg_cfg;
|
---|
582 |
|
---|
583 | /*
|
---|
584 | * サブネットマスクと
|
---|
585 | * ローカル・ブロードキャスト IPv4 アドレスを設定する。
|
---|
586 | */
|
---|
587 | ipcp_local_ack_cfg.in4_ifaddr.mask = MAKE_IPV4_LOCAL_MASK(ipcp_local_neg_cfg.in4_ifaddr.addr);
|
---|
588 |
|
---|
589 | /* 相手の構成情報の初期設定 */
|
---|
590 | ipcp_remote_ack_cfg = ipcp_remote_neg_cfg;
|
---|
591 |
|
---|
592 | /*
|
---|
593 | * サブネットマスクと
|
---|
594 | * ローカル・ブロードキャスト IPv4 アドレスを設定する。
|
---|
595 | */
|
---|
596 | ipcp_remote_ack_cfg.in4_ifaddr.mask = MAKE_IPV4_LOCAL_MASK(ipcp_remote_neg_cfg.in4_ifaddr.addr);
|
---|
597 |
|
---|
598 | sig_sem(SEM_IPCP_READY);
|
---|
599 |
|
---|
600 | syslog(LOG_NOTICE, "[PPP/IPCP] up, Local IPv4 Addr: %s, Remote IPv4 Addr: %s.",
|
---|
601 | ip2str(NULL, &ipcp_local_neg_cfg.in4_ifaddr.addr),
|
---|
602 | ip2str(NULL, &ipcp_remote_neg_cfg.in4_ifaddr.addr));
|
---|
603 | }
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * ipcp_down -- リンク接続を解放する。
|
---|
607 | */
|
---|
608 |
|
---|
609 | static void
|
---|
610 | ipcp_down (T_PPP_FSM *fsm)
|
---|
611 | {
|
---|
612 | sig_sem(SEM_IPCP_READY);
|
---|
613 | syslog(LOG_NOTICE, "[PPP/IPCP] down.");
|
---|
614 | }
|
---|
615 |
|
---|
616 | /*
|
---|
617 | * ipcp_finished -- 下位層を終了する。
|
---|
618 | */
|
---|
619 |
|
---|
620 | static void
|
---|
621 | ipcp_finished (T_PPP_FSM *fsm)
|
---|
622 | {
|
---|
623 | }
|
---|
624 |
|
---|
625 | /*
|
---|
626 | * ipcp_protrej -- Proto-REJ を受信したときの処理
|
---|
627 | */
|
---|
628 |
|
---|
629 | static void
|
---|
630 | ipcp_protrej (void)
|
---|
631 | {
|
---|
632 | fsm_lowerdown(&ipcp_fsm);
|
---|
633 | }
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * ipcp_lowerup -- IPCP 下位層を起動する。
|
---|
637 | */
|
---|
638 |
|
---|
639 | static void
|
---|
640 | ipcp_lowerup (void)
|
---|
641 | {
|
---|
642 | fsm_lowerup(&ipcp_fsm);
|
---|
643 | }
|
---|
644 |
|
---|
645 | /*
|
---|
646 | * ipcp_lowerdown -- IPCP 下位層を停止する。
|
---|
647 | */
|
---|
648 |
|
---|
649 | static void
|
---|
650 | ipcp_lowerdown (void)
|
---|
651 | {
|
---|
652 | fsm_lowerdown(&ipcp_fsm);
|
---|
653 | }
|
---|
654 |
|
---|
655 | /*
|
---|
656 | * ipcp_open -- IPCP のオープン
|
---|
657 | */
|
---|
658 |
|
---|
659 | static void
|
---|
660 | ipcp_open (void)
|
---|
661 | {
|
---|
662 | fsm_open(&ipcp_fsm);
|
---|
663 | }
|
---|
664 |
|
---|
665 | /*
|
---|
666 | * ipcp_close -- IPCP のクローズ
|
---|
667 | */
|
---|
668 |
|
---|
669 | static void
|
---|
670 | ipcp_close (void)
|
---|
671 | {
|
---|
672 | fsm_close(&ipcp_fsm);
|
---|
673 | }
|
---|
674 |
|
---|
675 | #endif /* fo #ifdef SUPPORT_PPP */
|
---|