[313] | 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_ipv6cp.c 315 2017-07-23 05:29:40Z coas-nagasima $
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /*
|
---|
| 35 | * ipv6cp.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 (IPV6CP) 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/ipv6cp.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_ipv6cp.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 ipv6cp_init (void);
|
---|
| 118 | static void ipv6cp_input (T_NET_BUF *input);
|
---|
| 119 | static void ipv6cp_protrej (void);
|
---|
| 120 | static void ipv6cp_lowerup (void);
|
---|
| 121 | static void ipv6cp_lowerdown (void);
|
---|
| 122 | static void ipv6cp_open (void);
|
---|
| 123 | static void ipv6cp_close (void);
|
---|
| 124 |
|
---|
| 125 | /*
|
---|
| 126 | * FSM から呼出されるコールバック関
|
---|
| 127 | */
|
---|
| 128 |
|
---|
| 129 | static void ipv6cp_resetci(T_PPP_FSM *fsm); /* 自構成情報をリセットする。 */
|
---|
| 130 | static int_t ipv6cp_cilen (T_PPP_FSM *fsm); /* 構成情報の長さを返す。 */
|
---|
| 131 | static void ipv6cp_addci (T_PPP_FSM *fsm, T_NET_BUF *output);
|
---|
| 132 | /* 自構成情報を追加する。 */
|
---|
| 133 | static bool_t ipv6cp_ackci (T_PPP_FSM *fsm, T_NET_BUF *input);
|
---|
| 134 | /* ACK を受信したときの処理 */
|
---|
| 135 | static bool_t ipv6cp_nakci (T_PPP_FSM *fsm, T_NET_BUF *input);
|
---|
| 136 | /* NAK を受信したときの処理 */
|
---|
| 137 | static bool_t ipv6cp_rejci (T_PPP_FSM *fsm, T_NET_BUF *input);
|
---|
| 138 | /* REJ を受信したときの処理 */
|
---|
| 139 | static int_t ipv6cp_reqci (T_PPP_FSM *fsm, T_NET_BUF *input, T_NET_BUF *output);
|
---|
| 140 | /* 相手の構成情報を確認する。 */
|
---|
| 141 | static void ipv6cp_up (T_PPP_FSM *fsm); /* リンク接続を確立する。 */
|
---|
| 142 | static void ipv6cp_down (T_PPP_FSM *fsm); /* リンク接続を解放する。 */
|
---|
| 143 | static void ipv6cp_finished(T_PPP_FSM *fsm); /* 下位層を終了する。 */
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | * 変数
|
---|
| 147 | */
|
---|
| 148 |
|
---|
| 149 | static T_IFNET ipv6cp_local_def_cfg = { /* 自構成情報の規定値 */
|
---|
| 150 | NULL, /* ネットワークインタフェースのソフトウェア情報 */
|
---|
| 151 | {}, /* IPv6 アドレス情報 */
|
---|
| 152 | {}, /* マルチキャスト IPv6 アドレス */
|
---|
| 153 | 0, /* フラグ */
|
---|
| 154 | };
|
---|
| 155 |
|
---|
| 156 | static T_IFNET ipv6cp_remote_def_cfg = { /* 相手の構成情報の規定値 */
|
---|
| 157 | NULL, /* ネットワークインタフェースのソフトウェア情報 */
|
---|
| 158 | {}, /* IPv6 アドレス情報 */
|
---|
| 159 | {}, /* マルチキャスト IPv6 アドレス */
|
---|
| 160 | 0, /* フラグ */
|
---|
| 161 | };
|
---|
| 162 |
|
---|
| 163 | static T_IFNET ipv6cp_local_neg_cfg; /* ネゴ中の自構成情報 */
|
---|
| 164 |
|
---|
| 165 | static T_IFNET ipv6cp_remote_neg_cfg; /* ネゴ中の相手の構成情報 */
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * 全域変数
|
---|
| 169 | */
|
---|
| 170 |
|
---|
| 171 | T_PPP_FSM_CALLBACKS ipv6cp_callbacks = {
|
---|
| 172 | ipv6cp_resetci, /* 自構成情報をリセットする。 */
|
---|
| 173 | ipv6cp_cilen, /* 構成情報の長さを返す。 */
|
---|
| 174 | ipv6cp_addci, /* 自構成情報を追加する。 */
|
---|
| 175 | ipv6cp_ackci, /* ACK を受信したときの処理 */
|
---|
| 176 | ipv6cp_nakci, /* NAK を受信したときの処理 */
|
---|
| 177 | ipv6cp_rejci, /* REJ を受信したときの処理 */
|
---|
| 178 | ipv6cp_reqci, /* 相手の構成情報を確認する。 */
|
---|
| 179 | ipv6cp_up, /* リンク接続を確立する。 */
|
---|
| 180 | ipv6cp_down, /* リンク接続を解放する。 */
|
---|
| 181 | NULL, /* 下位層を開始する。 */
|
---|
| 182 | ipv6cp_finished, /* 下位層を終了する。 */
|
---|
| 183 | NULL, /* 再送する。 */
|
---|
| 184 | NULL, /* 不明な CP を受信したときの処理 */
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | T_PPP_FSM ipv6cp_fsm = {
|
---|
| 188 | &ipv6cp_callbacks, /* コールバック関数 */
|
---|
| 189 | PPP_IPV6CP, /* プロトコル */
|
---|
| 190 | };
|
---|
| 191 |
|
---|
| 192 | T_PPP_PROTENT ipv6cp_protent = {
|
---|
| 193 | PPP_IPV6CP,
|
---|
| 194 | ipv6cp_init, /* 初期化 */
|
---|
| 195 | ipv6cp_input, /* 入力 */
|
---|
| 196 | ipv6cp_protrej, /* Proto-REJ 受信処理 */
|
---|
| 197 | ipv6cp_lowerup, /* 下位層を起動する */
|
---|
| 198 | ipv6cp_lowerdown, /* 下位層を停止する */
|
---|
| 199 | ipv6cp_open, /* オープンする */
|
---|
| 200 | ipv6cp_close, /* クローズする */
|
---|
| 201 | ip6_input, /* データ入力、IPv6 入力 */
|
---|
| 202 | };
|
---|
| 203 |
|
---|
| 204 | T_IFNET ipv6cp_local_ack_cfg; /* 相手に許可されたの自構成情報 */
|
---|
| 205 | T_IFNET ipv6cp_remote_ack_cfg; /* 相手に許可した構成情報 */
|
---|
| 206 |
|
---|
| 207 | /*
|
---|
| 208 | * ppp_get_ifnet -- ネットワークインタフェース構造体を返す。
|
---|
| 209 | */
|
---|
| 210 |
|
---|
| 211 | T_IFNET *
|
---|
| 212 | ppp_get_ifnet (void)
|
---|
| 213 | {
|
---|
| 214 | return &ipv6cp_local_ack_cfg;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /*
|
---|
| 218 | * wait_ipv6cp -- IP 接続完了まで待つ。
|
---|
| 219 | *
|
---|
| 220 | * 戻り値: 接続に失敗したら E_OBJ を返す。
|
---|
| 221 | */
|
---|
| 222 |
|
---|
| 223 | ER
|
---|
| 224 | wait_ipv6cp (void)
|
---|
| 225 | {
|
---|
| 226 | #ifdef PPP_CFG_MODEM
|
---|
| 227 | ER error;
|
---|
| 228 | #endif /* of #ifdef PPP_CFG_MODEM */
|
---|
| 229 |
|
---|
| 230 | if (ipv6cp_fsm.state <= PPP_FSM_STOPPED) {
|
---|
| 231 |
|
---|
| 232 | #ifdef PPP_CFG_MODEM
|
---|
| 233 |
|
---|
| 234 | /* モデムの接続完了まで待つ。*/
|
---|
| 235 | if ((error = wait_modem()) != E_OK)
|
---|
| 236 | return error;
|
---|
| 237 |
|
---|
| 238 | #else /* of #ifdef PPP_CFG_MODEM */
|
---|
| 239 |
|
---|
| 240 | /*
|
---|
| 241 | * 受動オープンし、相手からの接続待ちなら LCP を一度クローズし、
|
---|
| 242 | * 能動オープン状態で、再オープンする。
|
---|
| 243 | */
|
---|
| 244 | lcp_close();
|
---|
| 245 | lcp_open(PPP_OPEN_ACTIVE);
|
---|
| 246 |
|
---|
| 247 | #endif /* of #ifdef PPP_CFG_MODEM */
|
---|
| 248 |
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if (ipv6cp_fsm.state != PPP_FSM_OPENED) {
|
---|
| 252 | /* IP 接続完了まで待つ。*/
|
---|
| 253 | wai_sem(SEM_IPV6CP_READY);
|
---|
| 254 | if (ipv6cp_fsm.state != PPP_FSM_OPENED)
|
---|
| 255 | return E_OBJ;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | return E_OK;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /*
|
---|
| 262 | * ipv6cp_init -- IPV6CP モジュールの初期化
|
---|
| 263 | */
|
---|
| 264 |
|
---|
| 265 | static void
|
---|
| 266 | ipv6cp_init (void)
|
---|
| 267 | {
|
---|
| 268 | fsm_init(&ipv6cp_fsm);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | /*
|
---|
| 272 | * ipv6cp_input -- IPV6CP 入力
|
---|
| 273 | */
|
---|
| 274 |
|
---|
| 275 | static void
|
---|
| 276 | ipv6cp_input (T_NET_BUF *input)
|
---|
| 277 | {
|
---|
| 278 | NET_COUNT_PPP_IPV6CP(net_count_ppp_ipv6cp_in_octets, input->len);
|
---|
| 279 | NET_COUNT_PPP_IPV6CP(net_count_ppp_ipv6cp_in_packets, 1);
|
---|
| 280 | fsm_input(&ipv6cp_fsm, input);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /*
|
---|
| 284 | * ipv6cp_resetci -- IPV6CPの構成情報をリセットする。
|
---|
| 285 | */
|
---|
| 286 |
|
---|
| 287 | static void
|
---|
| 288 | ipv6cp_resetci (T_PPP_FSM *fsm)
|
---|
| 289 | {
|
---|
| 290 | /* 自構成情報の初期設定 */
|
---|
| 291 | ipv6cp_local_neg_cfg = ipv6cp_local_def_cfg;
|
---|
| 292 | memset((void*)&ipv6cp_local_ack_cfg, 0, sizeof(T_IFNET));
|
---|
| 293 |
|
---|
| 294 | /* 相手の構成情報の初期設定 */
|
---|
| 295 | ipv6cp_remote_neg_cfg = ipv6cp_remote_def_cfg;
|
---|
| 296 | memset((void*)&ipv6cp_remote_ack_cfg, 0, sizeof(T_IFNET));
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /*
|
---|
| 300 | * ipv6cp_cilen -- 構成情報の長さを返す。
|
---|
| 301 | */
|
---|
| 302 |
|
---|
| 303 | static int_t
|
---|
| 304 | ipv6cp_cilen (T_PPP_FSM *fsm)
|
---|
| 305 | {
|
---|
| 306 | int_t cilen = 0;
|
---|
| 307 |
|
---|
| 308 | cilen += sizeof(T_PPP_CI_HDR) + sizeof(uint32_t); /* IPv6 アドレス */
|
---|
| 309 |
|
---|
| 310 | return cilen;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /*
|
---|
| 314 | * ipv6cp_addci -- IPV6CPの構成情報を追加する。
|
---|
| 315 | */
|
---|
| 316 |
|
---|
| 317 | static void
|
---|
| 318 | ipv6cp_addci (T_PPP_FSM *fsm, T_NET_BUF *output)
|
---|
| 319 | {
|
---|
| 320 | uint8_t *buf;
|
---|
| 321 |
|
---|
| 322 | buf = output->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
| 323 |
|
---|
| 324 | /* IPv6 アドレスオプションを追加する。 */
|
---|
| 325 | *buf ++ = IPV6CP_CIT_ADDR;
|
---|
| 326 | *buf ++ = sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
| 327 | ahtonl(buf, ipv6cp_local_neg_cfg.in6_ifaddrs[0].addr);
|
---|
| 328 | buf += sizeof(uint32_t);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /*
|
---|
| 332 | * ipv6cp_ackci -- ACK を受信したときの処理
|
---|
| 333 | *
|
---|
| 334 | * 戻り値: true ACK は正常
|
---|
| 335 | * false ACK は異常
|
---|
| 336 | */
|
---|
| 337 |
|
---|
| 338 | static bool_t
|
---|
| 339 | ipv6cp_ackci (T_PPP_FSM *fsm, T_NET_BUF *input)
|
---|
| 340 | {
|
---|
| 341 | uint8_t *buf;
|
---|
| 342 | uint16_t len;
|
---|
| 343 |
|
---|
| 344 | buf = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
| 345 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
| 346 |
|
---|
| 347 | /*
|
---|
| 348 | * 構成情報は送信した順に完全に一致しなければならない。
|
---|
| 349 | * 従って、ipv6cp_addci で作成した順に解析する。
|
---|
| 350 | */
|
---|
| 351 |
|
---|
| 352 | /* ADDR オプションを解析する。 */
|
---|
| 353 | if (len < sizeof(T_PPP_CI_HDR) + sizeof(uint32_t) ||
|
---|
| 354 | *buf ++ != IPV6CP_CIT_ADDR ||
|
---|
| 355 | *buf ++ != sizeof(T_PPP_CI_HDR) + sizeof(uint32_t)||
|
---|
| 356 | nahcmpl(buf, ipv6cp_local_neg_cfg.in6_ifaddrs[0].addr))
|
---|
| 357 | return false;
|
---|
| 358 | buf += sizeof(uint32_t);
|
---|
| 359 | len -= sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
| 360 |
|
---|
| 361 | return true;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /*
|
---|
| 365 | * ipv6cp_nakci -- NAK を受信したときの処理
|
---|
| 366 | *
|
---|
| 367 | * 戻り値: 1 NAK は正常
|
---|
| 368 | * 0 NAK は異常
|
---|
| 369 | */
|
---|
| 370 |
|
---|
| 371 | static int_t
|
---|
| 372 | ipv6cp_nakci (T_PPP_FSM *fsm, T_NET_BUF *input)
|
---|
| 373 | {
|
---|
| 374 | T_IFNET new_cfg;
|
---|
| 375 | uint32_t addr;
|
---|
| 376 | uint16_t len;
|
---|
| 377 | uint8_t *buf, cilen;
|
---|
| 378 |
|
---|
| 379 | buf = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
| 380 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
| 381 |
|
---|
| 382 | new_cfg = ipv6cp_local_neg_cfg;
|
---|
| 383 |
|
---|
| 384 | /* ADDR オプションを解析する。 */
|
---|
| 385 | if (len >= sizeof(T_PPP_CI_HDR) + sizeof(addr) &&
|
---|
| 386 | * buf == IPV6CP_CIT_ADDR &&
|
---|
| 387 | *(buf + 1) == sizeof(T_PPP_CI_HDR) + sizeof(addr)) {
|
---|
| 388 | memcpy(&new_cfg.in6_ifaddrs[0].addr, buf + 2, sizeof(addr));
|
---|
| 389 | buf += sizeof(T_PPP_CI_HDR) + sizeof(addr);
|
---|
| 390 | len -= sizeof(T_PPP_CI_HDR) + sizeof(addr);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /* 後は無視する。*/
|
---|
| 394 | while (len > sizeof(T_PPP_CI_HDR)) {
|
---|
| 395 | cilen = *(buf + 1);
|
---|
| 396 | if (len < cilen)
|
---|
| 397 | return 0;
|
---|
| 398 | buf += cilen;
|
---|
| 399 | len -= cilen;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | /* 長さが 0 でなければエラー */
|
---|
| 403 | if (len != 0)
|
---|
| 404 | return 0;
|
---|
| 405 |
|
---|
| 406 | /* 新しいオプションを設定する。*/
|
---|
| 407 | if (fsm->state != PPP_FSM_OPENED)
|
---|
| 408 | ipv6cp_local_neg_cfg = new_cfg;
|
---|
| 409 |
|
---|
| 410 | return 1;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | /*
|
---|
| 414 | * ipv6cp_rejci -- REJ を受信したときの処理
|
---|
| 415 | *
|
---|
| 416 | * 戻り値: 1 REJ は正常
|
---|
| 417 | * 0 REJ は異常
|
---|
| 418 | */
|
---|
| 419 |
|
---|
| 420 | static int_t
|
---|
| 421 | ipv6cp_rejci (T_PPP_FSM *fsm, T_NET_BUF *input)
|
---|
| 422 | {
|
---|
| 423 | T_IFNET new_cfg;
|
---|
| 424 | uint8_t *buf;
|
---|
| 425 | uint16_t len;
|
---|
| 426 |
|
---|
| 427 | buf = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
| 428 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
| 429 |
|
---|
| 430 | new_cfg = ipv6cp_local_neg_cfg;
|
---|
| 431 |
|
---|
| 432 | /* ADDR オプションを解析する。 */
|
---|
| 433 | if (len >= sizeof(T_PPP_CI_HDR) + sizeof(new_cfg.in6_ifaddrs[0].addr) &&
|
---|
| 434 | * buf == IPV6CP_CIT_ADDR &&
|
---|
| 435 | *(buf + 1) == sizeof(T_PPP_CI_HDR) + sizeof(new_cfg.in6_ifaddrs[0].addr)) {
|
---|
| 436 | memcpy(&new_cfg.in6_ifaddrs[0].addr, &in6_addr_unspecified,
|
---|
| 437 | sizeof(new_cfg.in6_ifaddrs[0].addr));
|
---|
| 438 | buf += sizeof(T_PPP_CI_HDR) + sizeof(new_cfg.in6_ifaddrs[0].addr);
|
---|
| 439 | len -= sizeof(T_PPP_CI_HDR) + sizeof(new_cfg.in6_ifaddrs[0].addr);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /* 長さが 0 でなければエラー */
|
---|
| 443 | if (len != 0)
|
---|
| 444 | return 0;
|
---|
| 445 |
|
---|
| 446 | /* 新しいオプションを設定する。*/
|
---|
| 447 | if (fsm->state != PPP_FSM_OPENED)
|
---|
| 448 | ipv6cp_local_neg_cfg = new_cfg;
|
---|
| 449 |
|
---|
| 450 | return 1;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | /*
|
---|
| 454 | * ipv6cp_reqci -- 相手の構成情報を解析する。
|
---|
| 455 | */
|
---|
| 456 |
|
---|
| 457 | static int_t
|
---|
| 458 | ipv6cp_reqci (T_PPP_FSM *fsm, T_NET_BUF *input, T_NET_BUF *output)
|
---|
| 459 | {
|
---|
| 460 | T_IN6_ADDR addr;
|
---|
| 461 | uint16_t code, final, len, cilen;
|
---|
| 462 | uint8_t *np, *rp, *ap, *cp, type = 0;
|
---|
| 463 |
|
---|
| 464 | rp = ap = input->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
| 465 | /* rp: REJ、ap: ACK する CI のポインタ */
|
---|
| 466 | /* 拒否する場合は、引数 buf に上書きする。 */
|
---|
| 467 | len = input->len - (sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
| 468 | np = output->buf + sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR);
|
---|
| 469 | final = PPP_CONFACK; /* 最終的に送信するコード */
|
---|
| 470 | while (len > 0) {
|
---|
| 471 | code = PPP_CONFACK;
|
---|
| 472 | cp = ap; /* cp: 現在処理中の CI */
|
---|
| 473 |
|
---|
| 474 | if (len < sizeof(T_PPP_CI_HDR)) {
|
---|
| 475 |
|
---|
| 476 | /* 残りが CI ヘッダより短い */
|
---|
| 477 | syslog(LOG_NOTICE, "[PPP/IPV6CP] bad CI len: %d.", len);
|
---|
| 478 | cilen = len; /* データ長が異常の場合の処置 */
|
---|
| 479 | len = 0; /* ループから抜ける。 */
|
---|
| 480 | code = PPP_CONFREJ;
|
---|
| 481 | goto endswitch;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | type = *ap ++; /* CI の型 */
|
---|
| 485 | cilen = *ap ++; /* CI の長さ */
|
---|
| 486 | if (len < cilen) {
|
---|
| 487 |
|
---|
| 488 | /* 残りが CI 長より短い */
|
---|
| 489 | syslog(LOG_NOTICE, "[PPP/IPV6CP] bad CI len: %d.", cilen);
|
---|
| 490 | cilen = len; /* データ長が異常の場合の処置 */
|
---|
| 491 | len = 0; /* ループから抜ける。 */
|
---|
| 492 | code = PPP_CONFREJ;
|
---|
| 493 | goto endswitch;
|
---|
| 494 | }
|
---|
| 495 | len -= sizeof(T_PPP_CI_HDR);
|
---|
| 496 |
|
---|
| 497 | /* CI の型により分岐する。*/
|
---|
| 498 | switch (type) {
|
---|
| 499 |
|
---|
| 500 | case IPV6CP_CIT_ADDR: /* IPv6 アドレス */
|
---|
| 501 |
|
---|
| 502 | /* CI 長が、ヘッダ + 4 オクテットでなければエラー */
|
---|
| 503 | if (cilen != sizeof(T_PPP_CI_HDR) + sizeof(addr))
|
---|
| 504 | code = PPP_CONFREJ;
|
---|
| 505 | else {
|
---|
| 506 | memcpy((uint8_t*)&ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr, ap,
|
---|
| 507 | sizeof(ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr));
|
---|
| 508 | if (!IN6_ARE_ADDR_EQUAL(&addr, &ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr) &&
|
---|
| 509 | (IN6_IS_ADDR_UNSPECIFIED(&addr) ||
|
---|
| 510 | IN6_IS_ADDR_UNSPECIFIED(&ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr))) {
|
---|
| 511 | memcpy(np, (uint8_t*)&ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr,
|
---|
| 512 | sizeof(ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr));
|
---|
| 513 | *np ++ = IPV6CP_CIT_ADDR;
|
---|
| 514 | *np ++ = sizeof(T_PPP_CI_HDR) + sizeof(uint32_t);
|
---|
| 515 | np += sizeof(ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr);
|
---|
| 516 | code = PPP_CONFNAK;
|
---|
| 517 | }
|
---|
| 518 | #if 1 /* 要確認 */
|
---|
| 519 | else if (IN6_IS_ADDR_UNSPECIFIED(&addr) &&
|
---|
| 520 | IN6_IS_ADDR_UNSPECIFIED(&ipv6cp_remote_neg_cfg.in6_ifaddrs[0]))
|
---|
| 521 | #else
|
---|
| 522 | else if (IN6_IS_ADDR_UNSPECIFIED(&addr) ||
|
---|
| 523 | IN6_IS_ADDR_UNSPECIFIED(&ipv6cp_remote_neg_cfg.in6_ifaddrs[0]))
|
---|
| 524 | #endif
|
---|
| 525 | code = PPP_CONFREJ;
|
---|
| 526 | else
|
---|
| 527 | ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr = addr;
|
---|
| 528 | }
|
---|
| 529 | break;
|
---|
| 530 |
|
---|
| 531 | default:
|
---|
| 532 | syslog(LOG_INFO, "[PPP/IPV6CP] unexp opt: %d.", type);
|
---|
| 533 | code = PPP_CONFREJ;
|
---|
| 534 | break;
|
---|
| 535 | }
|
---|
| 536 | endswitch:
|
---|
| 537 |
|
---|
| 538 | if (code == PPP_CONFNAK) {
|
---|
| 539 | /* CI の型が Magic Number の場合を除いて、NAK の回数が */
|
---|
| 540 | /* 最大値 MAX_PPP_FAILURES を超えたら拒否する。 */
|
---|
| 541 | if (fsm->failures >= MAX_PPP_FAILURES)
|
---|
| 542 | code = PPP_CONFREJ;
|
---|
| 543 | else if (final != PPP_CONFREJ)
|
---|
| 544 | final = PPP_CONFNAK;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /* この CI を拒否する */
|
---|
| 548 | if (code == PPP_CONFREJ) {
|
---|
| 549 | if (rp < cp) /* 前に詰める。*/
|
---|
| 550 | memcpy(rp, cp, cilen);
|
---|
| 551 | rp += cilen;
|
---|
| 552 | final = PPP_CONFREJ;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | ap += cilen - sizeof(T_PPP_CI_HDR);
|
---|
| 556 | len -= cilen - sizeof(T_PPP_CI_HDR);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | /* 最終的な長さを調整する。*/
|
---|
| 560 | switch (final) {
|
---|
| 561 | case PPP_CONFNAK:
|
---|
| 562 | output->len = np - output->buf;
|
---|
| 563 | memcpy(output->buf, input->buf, sizeof(T_PPP_HDR) + sizeof(T_PPP_CP_HDR));
|
---|
| 564 | break;
|
---|
| 565 | case PPP_CONFREJ:
|
---|
| 566 | memcpy(output->buf, input->buf, output->len = rp - input->buf);
|
---|
| 567 | break;
|
---|
| 568 | case PPP_CONFACK:
|
---|
| 569 | memcpy(output->buf, input->buf, input->len);
|
---|
| 570 | break;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | return final;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 | /*
|
---|
| 577 | * ipv6cp_up -- リンク接続を確立する。
|
---|
| 578 | */
|
---|
| 579 |
|
---|
| 580 | static void
|
---|
| 581 | ipv6cp_up (T_PPP_FSM *fsm)
|
---|
| 582 | {
|
---|
| 583 | /* 自構成情報の初期設定 */
|
---|
| 584 | ipv6cp_local_ack_cfg = ipv6cp_local_neg_cfg;
|
---|
| 585 |
|
---|
| 586 | sig_sem(SEM_IPV6CP_READY);
|
---|
| 587 |
|
---|
| 588 | syslog(LOG_NOTICE, "[PPP/IPV6CP] up, Local IPv6 Addr: %s, Remote IPv6 Addr: %s.",
|
---|
| 589 | ipv62str(NULL, &ipv6cp_local_neg_cfg.in6_ifaddrs[0].addr),
|
---|
| 590 | ipv62str(NULL, &ipv6cp_remote_neg_cfg.in6_ifaddrs[0].addr));
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | /*
|
---|
| 594 | * ipv6cp_down -- リンク接続を解放する。
|
---|
| 595 | */
|
---|
| 596 |
|
---|
| 597 | static void
|
---|
| 598 | ipv6cp_down (T_PPP_FSM *fsm)
|
---|
| 599 | {
|
---|
| 600 | sig_sem(SEM_IPV6CP_READY);
|
---|
| 601 | syslog(LOG_NOTICE, "[PPP/IPV6CP] down.");
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | /*
|
---|
| 605 | * ipv6cp_finished -- 下位層を終了する。
|
---|
| 606 | */
|
---|
| 607 |
|
---|
| 608 | static void
|
---|
| 609 | ipv6cp_finished (T_PPP_FSM *fsm)
|
---|
| 610 | {
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | /*
|
---|
| 614 | * ipv6cp_protrej -- Proto-REJ を受信したときの処理
|
---|
| 615 | */
|
---|
| 616 |
|
---|
| 617 | static void
|
---|
| 618 | ipv6cp_protrej (void)
|
---|
| 619 | {
|
---|
| 620 | fsm_lowerdown(&ipv6cp_fsm);
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | /*
|
---|
| 624 | * ipv6cp_lowerup -- IPV6CP 下位層を起動する。
|
---|
| 625 | */
|
---|
| 626 |
|
---|
| 627 | static void
|
---|
| 628 | ipv6cp_lowerup (void)
|
---|
| 629 | {
|
---|
| 630 | fsm_lowerup(&ipv6cp_fsm);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | /*
|
---|
| 634 | * ipv6cp_lowerdown -- IPV6CP 下位層を停止する。
|
---|
| 635 | */
|
---|
| 636 |
|
---|
| 637 | static void
|
---|
| 638 | ipv6cp_lowerdown (void)
|
---|
| 639 | {
|
---|
| 640 | fsm_lowerdown(&ipv6cp_fsm);
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /*
|
---|
| 644 | * ipv6cp_open -- IPV6CP のオープン
|
---|
| 645 | */
|
---|
| 646 |
|
---|
| 647 | static void
|
---|
| 648 | ipv6cp_open (void)
|
---|
| 649 | {
|
---|
| 650 | fsm_open(&ipv6cp_fsm);
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | /*
|
---|
| 654 | * ipv6cp_close -- IPV6CP のクローズ
|
---|
| 655 | */
|
---|
| 656 |
|
---|
| 657 | static void
|
---|
| 658 | ipv6cp_close (void)
|
---|
| 659 | {
|
---|
| 660 | fsm_close(&ipv6cp_fsm);
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | #endif /* fo #ifdef SUPPORT_PPP */
|
---|