[337] | 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 | * PPP High Level Link Control (HDLC) Module
|
---|
| 36 | *
|
---|
| 37 | * Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
---|
| 38 | *
|
---|
| 39 | * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
---|
| 40 | *
|
---|
| 41 | * Redistribution and use in source and binary forms are permitted
|
---|
| 42 | * provided that the above copyright notice and this paragraph are
|
---|
| 43 | * duplicated in all such forms and that any documentation,
|
---|
| 44 | * advertising materials, and other materials related to such
|
---|
| 45 | * distribution and use acknowledge that the software was developed
|
---|
| 46 | * by the Internet Initiative Japan, Inc. The name of the
|
---|
| 47 | * IIJ may not be used to endorse or promote products derived
|
---|
| 48 | * from this software without specific prior written permission.
|
---|
| 49 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
---|
| 50 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
---|
| 51 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 52 | *
|
---|
| 53 | * $FreeBSD: src/usr.sbin/ppp/hdlc.c,v 1.46 1999/12/20 20:29:42 brian Exp $
|
---|
| 54 | *
|
---|
| 55 | * TODO:
|
---|
| 56 | */
|
---|
| 57 |
|
---|
| 58 | #include <s_services.h>
|
---|
| 59 | #include <t_services.h>
|
---|
| 60 |
|
---|
| 61 | #include <tinet_defs.h>
|
---|
| 62 | #include <tinet_config.h>
|
---|
| 63 |
|
---|
| 64 | #include <net/if.h>
|
---|
| 65 | #include <net/if_ppp.h>
|
---|
| 66 | #include <net/net.h>
|
---|
| 67 | #include <net/net_endian.h>
|
---|
| 68 | #include <net/net_buf.h>
|
---|
| 69 | #include <net/net_count.h>
|
---|
| 70 | #include <net/ppp.h>
|
---|
| 71 | #include <net/ppp_var.h>
|
---|
| 72 | #include <net/ppp_hdlc.h>
|
---|
| 73 | #include <net/ppp_fsm.h>
|
---|
| 74 | #include <net/ppp_lcp.h>
|
---|
| 75 | #include <net/ppp_modem.h>
|
---|
| 76 |
|
---|
| 77 | #ifdef SUPPORT_PPP
|
---|
| 78 |
|
---|
| 79 | /*
|
---|
| 80 | * RFC1171 (PPP) の HDLC 部の定義
|
---|
| 81 | */
|
---|
| 82 |
|
---|
| 83 | /*
|
---|
| 84 | * 変数
|
---|
| 85 | */
|
---|
| 86 |
|
---|
| 87 | uint32_t remote_ACCM = DEF_REMOTE_ACCM;
|
---|
| 88 |
|
---|
| 89 | /*
|
---|
| 90 | * 関数
|
---|
| 91 | */
|
---|
| 92 |
|
---|
| 93 | static ER HDLC_putoctet (uint8_t octet);
|
---|
| 94 | static ER HDLC_getoctet (uint8_t *octet);
|
---|
| 95 |
|
---|
| 96 | /*
|
---|
| 97 | * genfcstab.c により作成した FCS のルックアップ表
|
---|
| 98 | * RFC1171 参照
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | static uint16_t fcstab[256] = {
|
---|
| 102 | UINT_C(0x0000), UINT_C(0x1189), UINT_C(0x2312), UINT_C(0x329b),
|
---|
| 103 | UINT_C(0x4624), UINT_C(0x57ad), UINT_C(0x6536), UINT_C(0x74bf),
|
---|
| 104 | UINT_C(0x8c48), UINT_C(0x9dc1), UINT_C(0xaf5a), UINT_C(0xbed3),
|
---|
| 105 | UINT_C(0xca6c), UINT_C(0xdbe5), UINT_C(0xe97e), UINT_C(0xf8f7),
|
---|
| 106 | UINT_C(0x1081), UINT_C(0x0108), UINT_C(0x3393), UINT_C(0x221a),
|
---|
| 107 | UINT_C(0x56a5), UINT_C(0x472c), UINT_C(0x75b7), UINT_C(0x643e),
|
---|
| 108 | UINT_C(0x9cc9), UINT_C(0x8d40), UINT_C(0xbfdb), UINT_C(0xae52),
|
---|
| 109 | UINT_C(0xdaed), UINT_C(0xcb64), UINT_C(0xf9ff), UINT_C(0xe876),
|
---|
| 110 | UINT_C(0x2102), UINT_C(0x308b), UINT_C(0x0210), UINT_C(0x1399),
|
---|
| 111 | UINT_C(0x6726), UINT_C(0x76af), UINT_C(0x4434), UINT_C(0x55bd),
|
---|
| 112 | UINT_C(0xad4a), UINT_C(0xbcc3), UINT_C(0x8e58), UINT_C(0x9fd1),
|
---|
| 113 | UINT_C(0xeb6e), UINT_C(0xfae7), UINT_C(0xc87c), UINT_C(0xd9f5),
|
---|
| 114 | UINT_C(0x3183), UINT_C(0x200a), UINT_C(0x1291), UINT_C(0x0318),
|
---|
| 115 | UINT_C(0x77a7), UINT_C(0x662e), UINT_C(0x54b5), UINT_C(0x453c),
|
---|
| 116 | UINT_C(0xbdcb), UINT_C(0xac42), UINT_C(0x9ed9), UINT_C(0x8f50),
|
---|
| 117 | UINT_C(0xfbef), UINT_C(0xea66), UINT_C(0xd8fd), UINT_C(0xc974),
|
---|
| 118 | UINT_C(0x4204), UINT_C(0x538d), UINT_C(0x6116), UINT_C(0x709f),
|
---|
| 119 | UINT_C(0x0420), UINT_C(0x15a9), UINT_C(0x2732), UINT_C(0x36bb),
|
---|
| 120 | UINT_C(0xce4c), UINT_C(0xdfc5), UINT_C(0xed5e), UINT_C(0xfcd7),
|
---|
| 121 | UINT_C(0x8868), UINT_C(0x99e1), UINT_C(0xab7a), UINT_C(0xbaf3),
|
---|
| 122 | UINT_C(0x5285), UINT_C(0x430c), UINT_C(0x7197), UINT_C(0x601e),
|
---|
| 123 | UINT_C(0x14a1), UINT_C(0x0528), UINT_C(0x37b3), UINT_C(0x263a),
|
---|
| 124 | UINT_C(0xdecd), UINT_C(0xcf44), UINT_C(0xfddf), UINT_C(0xec56),
|
---|
| 125 | UINT_C(0x98e9), UINT_C(0x8960), UINT_C(0xbbfb), UINT_C(0xaa72),
|
---|
| 126 | UINT_C(0x6306), UINT_C(0x728f), UINT_C(0x4014), UINT_C(0x519d),
|
---|
| 127 | UINT_C(0x2522), UINT_C(0x34ab), UINT_C(0x0630), UINT_C(0x17b9),
|
---|
| 128 | UINT_C(0xef4e), UINT_C(0xfec7), UINT_C(0xcc5c), UINT_C(0xddd5),
|
---|
| 129 | UINT_C(0xa96a), UINT_C(0xb8e3), UINT_C(0x8a78), UINT_C(0x9bf1),
|
---|
| 130 | UINT_C(0x7387), UINT_C(0x620e), UINT_C(0x5095), UINT_C(0x411c),
|
---|
| 131 | UINT_C(0x35a3), UINT_C(0x242a), UINT_C(0x16b1), UINT_C(0x0738),
|
---|
| 132 | UINT_C(0xffcf), UINT_C(0xee46), UINT_C(0xdcdd), UINT_C(0xcd54),
|
---|
| 133 | UINT_C(0xb9eb), UINT_C(0xa862), UINT_C(0x9af9), UINT_C(0x8b70),
|
---|
| 134 | UINT_C(0x8408), UINT_C(0x9581), UINT_C(0xa71a), UINT_C(0xb693),
|
---|
| 135 | UINT_C(0xc22c), UINT_C(0xd3a5), UINT_C(0xe13e), UINT_C(0xf0b7),
|
---|
| 136 | UINT_C(0x0840), UINT_C(0x19c9), UINT_C(0x2b52), UINT_C(0x3adb),
|
---|
| 137 | UINT_C(0x4e64), UINT_C(0x5fed), UINT_C(0x6d76), UINT_C(0x7cff),
|
---|
| 138 | UINT_C(0x9489), UINT_C(0x8500), UINT_C(0xb79b), UINT_C(0xa612),
|
---|
| 139 | UINT_C(0xd2ad), UINT_C(0xc324), UINT_C(0xf1bf), UINT_C(0xe036),
|
---|
| 140 | UINT_C(0x18c1), UINT_C(0x0948), UINT_C(0x3bd3), UINT_C(0x2a5a),
|
---|
| 141 | UINT_C(0x5ee5), UINT_C(0x4f6c), UINT_C(0x7df7), UINT_C(0x6c7e),
|
---|
| 142 | UINT_C(0xa50a), UINT_C(0xb483), UINT_C(0x8618), UINT_C(0x9791),
|
---|
| 143 | UINT_C(0xe32e), UINT_C(0xf2a7), UINT_C(0xc03c), UINT_C(0xd1b5),
|
---|
| 144 | UINT_C(0x2942), UINT_C(0x38cb), UINT_C(0x0a50), UINT_C(0x1bd9),
|
---|
| 145 | UINT_C(0x6f66), UINT_C(0x7eef), UINT_C(0x4c74), UINT_C(0x5dfd),
|
---|
| 146 | UINT_C(0xb58b), UINT_C(0xa402), UINT_C(0x9699), UINT_C(0x8710),
|
---|
| 147 | UINT_C(0xf3af), UINT_C(0xe226), UINT_C(0xd0bd), UINT_C(0xc134),
|
---|
| 148 | UINT_C(0x39c3), UINT_C(0x284a), UINT_C(0x1ad1), UINT_C(0x0b58),
|
---|
| 149 | UINT_C(0x7fe7), UINT_C(0x6e6e), UINT_C(0x5cf5), UINT_C(0x4d7c),
|
---|
| 150 | UINT_C(0xc60c), UINT_C(0xd785), UINT_C(0xe51e), UINT_C(0xf497),
|
---|
| 151 | UINT_C(0x8028), UINT_C(0x91a1), UINT_C(0xa33a), UINT_C(0xb2b3),
|
---|
| 152 | UINT_C(0x4a44), UINT_C(0x5bcd), UINT_C(0x6956), UINT_C(0x78df),
|
---|
| 153 | UINT_C(0x0c60), UINT_C(0x1de9), UINT_C(0x2f72), UINT_C(0x3efb),
|
---|
| 154 | UINT_C(0xd68d), UINT_C(0xc704), UINT_C(0xf59f), UINT_C(0xe416),
|
---|
| 155 | UINT_C(0x90a9), UINT_C(0x8120), UINT_C(0xb3bb), UINT_C(0xa232),
|
---|
| 156 | UINT_C(0x5ac5), UINT_C(0x4b4c), UINT_C(0x79d7), UINT_C(0x685e),
|
---|
| 157 | UINT_C(0x1ce1), UINT_C(0x0d68), UINT_C(0x3ff3), UINT_C(0x2e7a),
|
---|
| 158 | UINT_C(0xe70e), UINT_C(0xf687), UINT_C(0xc41c), UINT_C(0xd595),
|
---|
| 159 | UINT_C(0xa12a), UINT_C(0xb0a3), UINT_C(0x8238), UINT_C(0x93b1),
|
---|
| 160 | UINT_C(0x6b46), UINT_C(0x7acf), UINT_C(0x4854), UINT_C(0x59dd),
|
---|
| 161 | UINT_C(0x2d62), UINT_C(0x3ceb), UINT_C(0x0e70), UINT_C(0x1ff9),
|
---|
| 162 | UINT_C(0xf78f), UINT_C(0xe606), UINT_C(0xd49d), UINT_C(0xc514),
|
---|
| 163 | UINT_C(0xb1ab), UINT_C(0xa022), UINT_C(0x92b9), UINT_C(0x8330),
|
---|
| 164 | UINT_C(0x7bc7), UINT_C(0x6a4e), UINT_C(0x58d5), UINT_C(0x495c),
|
---|
| 165 | UINT_C(0x3de3), UINT_C(0x2c6a), UINT_C(0x1ef1), UINT_C(0x0f78),
|
---|
| 166 | };
|
---|
| 167 |
|
---|
| 168 | /*
|
---|
| 169 | * インライン関数
|
---|
| 170 | */
|
---|
| 171 |
|
---|
| 172 | Inline uint16_t Next_FCS (uint16_t FCS, uint8_t Octet) {
|
---|
| 173 | return (FCS >> 8) ^ fcstab[(FCS ^ Octet) & 0xff];
|
---|
| 174 | };
|
---|
| 175 |
|
---|
| 176 | /*
|
---|
| 177 | * HDLC_putoctet -- HDLC オクテット出力
|
---|
| 178 | */
|
---|
| 179 |
|
---|
| 180 | static
|
---|
| 181 | ER HDLC_putoctet (uint8_t octet)
|
---|
| 182 | {
|
---|
| 183 | static uint8_t escape[2] = { HDLC_CNTL_ESC };
|
---|
| 184 |
|
---|
| 185 | ER error;
|
---|
| 186 |
|
---|
| 187 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_octets, 1);
|
---|
| 188 |
|
---|
| 189 | if ((octet < 0x20 && ((1 << octet) & lcp_remote_ack_cfg.ACCM)) ||
|
---|
| 190 | octet == HDLC_CNTL_ESC || octet == HDLC_FLAG_SEQ) {
|
---|
| 191 | escape[1] = octet ^ HDLC_ESC_MASK;
|
---|
| 192 | error = serial_wri_dat(HDLC_PORTID, escape, sizeof(escape));
|
---|
| 193 | }
|
---|
| 194 | else
|
---|
| 195 | error = serial_wri_dat(HDLC_PORTID, &octet, sizeof(octet));
|
---|
| 196 |
|
---|
| 197 | return error > 0 ? E_OK : error;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /*
|
---|
| 201 | * HDLC_wirte -- HDLC 出力
|
---|
| 202 | */
|
---|
| 203 |
|
---|
| 204 | ER HDLC_write (T_NET_BUF *nbuf)
|
---|
| 205 | {
|
---|
| 206 | static char flag_seq_ac[4] = {
|
---|
| 207 | HDLC_FLAG_SEQ,
|
---|
| 208 | HDLC_ADDR_ALL,
|
---|
| 209 | HDLC_CNTL_ESC,
|
---|
| 210 | HDLC_CNTL_UI ^ HDLC_ESC_MASK,
|
---|
| 211 | };
|
---|
| 212 |
|
---|
| 213 | #ifdef LCP_CFG_ACCOMP
|
---|
| 214 |
|
---|
| 215 | static char flag_seq = HDLC_FLAG_SEQ;
|
---|
| 216 |
|
---|
| 217 | #endif /* of #ifdef LCP_CFG_ACCOMP */
|
---|
| 218 |
|
---|
| 219 | ER error;
|
---|
| 220 | uint32_t fcs;
|
---|
| 221 | uint16_t len;
|
---|
| 222 | uint8_t *buf;
|
---|
| 223 |
|
---|
| 224 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_packets, 1);
|
---|
| 225 |
|
---|
| 226 | #ifdef LCP_CFG_ACCOMP
|
---|
| 227 |
|
---|
| 228 | if (lcp_local_ack_cfg.options & LCP_CFG_ACCOMP) {
|
---|
| 229 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_octets, sizeof(flag_seq));
|
---|
| 230 | /* フレーム開始フラグシーケンス を送信する */
|
---|
| 231 | if ((error = serial_wri_dat(HDLC_PORTID, &flag_seq, sizeof(flag_seq))) < 0) {
|
---|
| 232 | goto err_ret;
|
---|
| 233 | }
|
---|
| 234 | fcs = HDLC_INIT_FCS;
|
---|
| 235 | }
|
---|
| 236 | else {
|
---|
| 237 |
|
---|
| 238 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_octets, sizeof(flag_seq_ac));
|
---|
| 239 | /* フレーム開始フラグシーケンス、Address と Control を送信する */
|
---|
| 240 | if ((error = serial_wri_dat(HDLC_PORTID, flag_seq_ac, sizeof(flag_seq_ac))) < 0) {
|
---|
| 241 | goto err_ret;
|
---|
| 242 | }
|
---|
| 243 | /* Address と Control の FCS の計算は HDLC_INIT_FCS_AC に含めている */
|
---|
| 244 | fcs = HDLC_INIT_FCS_AC;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | #else /* of #ifdef LCP_CFG_ACCOMP */
|
---|
| 248 |
|
---|
| 249 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_octets, sizeof(flag_seq_ac));
|
---|
| 250 | /* フレーム開始フラグシーケンス、Address と Control を送信する */
|
---|
| 251 | if ((error = serial_wri_dat(HDLC_PORTID, flag_seq_ac, sizeof(flag_seq_ac))) < 0) {
|
---|
| 252 | goto err_ret;
|
---|
| 253 | }
|
---|
| 254 | /* Address と Control の FCS の計算は HDLC_INIT_FCS_AC に含めている */
|
---|
| 255 | fcs = HDLC_INIT_FCS_AC;
|
---|
| 256 |
|
---|
| 257 | #endif /* of #ifdef LCP_CFG_ACCOMP */
|
---|
| 258 |
|
---|
| 259 | buf = nbuf->buf;
|
---|
| 260 | len = nbuf->len;
|
---|
| 261 |
|
---|
| 262 | #ifdef LCP_CFG_PCOMP
|
---|
| 263 |
|
---|
| 264 | if ((lcp_local_ack_cfg.options & LCP_CFG_ACCOMP) && *buf == 0 && *(buf + 1) < 0xff) {
|
---|
| 265 | buf ++;
|
---|
| 266 | len --;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | #endif /* of #ifdef LCP_CFG_PCOMP */
|
---|
| 270 |
|
---|
| 271 | /* データを送信する */
|
---|
| 272 | while (len -- > 0) {
|
---|
| 273 |
|
---|
| 274 | if ((error = HDLC_putoctet(*buf)) != E_OK)
|
---|
| 275 | goto err_ret;
|
---|
| 276 | fcs = Next_FCS(fcs, *buf);
|
---|
| 277 | buf ++;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /* FCS を送信する */
|
---|
| 281 | fcs = ~fcs;
|
---|
| 282 | if ((error = HDLC_putoctet(fcs & 0xff)) != E_OK)
|
---|
| 283 | goto err_ret;
|
---|
| 284 | if ((error = HDLC_putoctet(fcs >> 8)) != E_OK)
|
---|
| 285 | goto err_ret;
|
---|
| 286 |
|
---|
| 287 | /* フレーム終了フラグシーケンスを送信する */
|
---|
| 288 | if ((error = serial_wri_dat(HDLC_PORTID, &flag_seq_ac[0], sizeof(char))) < 0)
|
---|
| 289 | goto err_ret;
|
---|
| 290 |
|
---|
| 291 | return E_OK;
|
---|
| 292 |
|
---|
| 293 | err_ret:
|
---|
| 294 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_err_packets, 1);
|
---|
| 295 | return error;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /*
|
---|
| 299 | * HDLC_getoctet -- HDLC オクテット入力
|
---|
| 300 | *
|
---|
| 301 | * 戻り値が true ならフラグシーケンス
|
---|
| 302 | */
|
---|
| 303 |
|
---|
| 304 | static
|
---|
| 305 | ER_BOOL HDLC_getoctet (uint8_t *octet)
|
---|
| 306 | {
|
---|
| 307 | ER error;
|
---|
| 308 |
|
---|
| 309 | /* 1オクテット入力する */
|
---|
| 310 | if ((error = serial_rea_dat(HDLC_PORTID, octet, sizeof(uint8_t))) <= 0) {
|
---|
| 311 | return error;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /* 入力したオクテットがフラグシーケンスなら true を返す */
|
---|
| 315 | if (*octet == HDLC_FLAG_SEQ)
|
---|
| 316 | return true;
|
---|
| 317 |
|
---|
| 318 | /* 入力したオクテットがエスケープなら、もう1オクテット入力しマスクを解除する */
|
---|
| 319 | if (*octet == HDLC_CNTL_ESC) {
|
---|
| 320 | if ((error = serial_rea_dat(HDLC_PORTID, octet, sizeof(uint8_t))) <= 0)
|
---|
| 321 | return error;
|
---|
| 322 | *octet ^= HDLC_ESC_MASK;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | NET_COUNT_PPP_HDLC(net_count_hdlc.in_octets, 1);
|
---|
| 326 | return E_OK;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /*
|
---|
| 330 | * HDLC_read -- HDLC 入力
|
---|
| 331 | *
|
---|
| 332 | * HDLC フレームを入力する。
|
---|
| 333 | */
|
---|
| 334 |
|
---|
| 335 | ER HDLC_read (T_NET_BUF *nbuf, uint16_t size)
|
---|
| 336 | {
|
---|
| 337 | ER error;
|
---|
| 338 | uint32_t fcs = 0;
|
---|
| 339 | uint16_t olen = 0;
|
---|
| 340 | uint8_t *buf, octet;
|
---|
| 341 |
|
---|
| 342 | buf = nbuf->buf;
|
---|
| 343 |
|
---|
| 344 | /* フレーム開始フラグシーケンスまで入力を読み飛ばす */
|
---|
| 345 | while ((error = HDLC_getoctet(&octet)) != true) {
|
---|
| 346 |
|
---|
| 347 | if (error != E_OK)
|
---|
| 348 | goto err_ret;
|
---|
| 349 |
|
---|
| 350 | #ifdef PPP_CFG_MODEM
|
---|
| 351 |
|
---|
| 352 | else if (octet == '\n') {
|
---|
| 353 | /* モデム制御を呼び出す。*/
|
---|
| 354 | modem_cntl(modem_getline(nbuf, size));
|
---|
| 355 | /* 一度、この関数を終了する。*/
|
---|
| 356 | nbuf->len = 0;
|
---|
| 357 | return E_OK;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | #endif/* of #ifdef PPP_CFG_MODEM */
|
---|
| 361 |
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /* フレーム開始フラグシーケンスの間は入力を読み飛ばす */
|
---|
| 365 | while (1) {
|
---|
| 366 | if ((error = HDLC_getoctet(&octet)) == E_OK)
|
---|
| 367 | break;
|
---|
| 368 | else if (error != true)
|
---|
| 369 | goto err_ret;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | NET_COUNT_PPP_HDLC(net_count_hdlc.in_packets, 1);
|
---|
| 373 |
|
---|
| 374 | /* 入力したオクテットは Address: All-Stations か */
|
---|
| 375 | if (octet == HDLC_ADDR_ALL) {
|
---|
| 376 |
|
---|
| 377 | /* 次は Control: UI */
|
---|
| 378 | if ((error = HDLC_getoctet(&octet)) != E_OK || octet != HDLC_CNTL_UI) {
|
---|
| 379 | error = E_SYS;
|
---|
| 380 | goto err_ret;
|
---|
| 381 | }
|
---|
| 382 | if ((error = HDLC_getoctet(&octet)) != E_OK)
|
---|
| 383 | goto err_ret;
|
---|
| 384 | /* Address と Control の FCS の計算は HDLC_INIT_FCS_AC に含めている */
|
---|
| 385 | fcs = HDLC_INIT_FCS_AC;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | #ifdef LCP_CFG_ACCOMP
|
---|
| 389 |
|
---|
| 390 | else if (lcp_remote_ack_cfg.options & LCP_CFG_ACCOMP) {
|
---|
| 391 | fcs = HDLC_INIT_FCS;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | #endif/* of #ifdef LCP_CFG_ACCOMP */
|
---|
| 395 |
|
---|
| 396 | else {
|
---|
| 397 | error = E_SYS;
|
---|
| 398 | goto err_ret;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | nbuf->len = 0;
|
---|
| 402 |
|
---|
| 403 | #ifdef LCP_CFG_PCOMP
|
---|
| 404 |
|
---|
| 405 | if ((lcp_remote_ack_cfg.options & LCP_CFG_PCOMP) && (octet & 0x01) != 0) {
|
---|
| 406 | *buf ++ = 0x00;
|
---|
| 407 | size --;
|
---|
| 408 | nbuf->len ++;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | #endif /* of #ifdef LCP_CFG_PCOMP */
|
---|
| 412 |
|
---|
| 413 | fcs = Next_FCS(fcs, octet);
|
---|
| 414 | *buf ++ = octet;
|
---|
| 415 | size --;
|
---|
| 416 | nbuf->len ++;
|
---|
| 417 |
|
---|
| 418 | /* SUD + FCS を入力する */
|
---|
| 419 | while ((error = HDLC_getoctet(&octet)) == E_OK) {
|
---|
| 420 | fcs = Next_FCS(fcs, octet);
|
---|
| 421 | if (size > 0) {
|
---|
| 422 | *buf ++ = octet;
|
---|
| 423 | size --;
|
---|
| 424 | }
|
---|
| 425 | else
|
---|
| 426 | olen ++;
|
---|
| 427 | nbuf->len ++;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | if (error == true) {
|
---|
| 431 | if (size > 0)
|
---|
| 432 | nbuf->len -= 2;
|
---|
| 433 | else if (olen <= 2)
|
---|
| 434 | nbuf->len -= olen;
|
---|
| 435 | else {
|
---|
| 436 | error = E_BOVR;
|
---|
| 437 | goto err_ret;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | else
|
---|
| 441 | goto err_ret;
|
---|
| 442 |
|
---|
| 443 | /* FCS をチェックする */
|
---|
| 444 | if (fcs != HDLC_GOOD_FCS) {
|
---|
| 445 | error = E_SYS;
|
---|
| 446 | goto err_ret;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | return E_OK;
|
---|
| 450 |
|
---|
| 451 | err_ret:
|
---|
| 452 | NET_COUNT_PPP_HDLC(net_count_hdlc.in_err_packets, 1);
|
---|
| 453 | return error;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | /*
|
---|
| 457 | * HDLC_dummy_read -- HDLC 入力 (空読み)
|
---|
| 458 | *
|
---|
| 459 | * HDLC フレームを空読みする。
|
---|
| 460 | */
|
---|
| 461 |
|
---|
| 462 | void
|
---|
| 463 | HDLC_dummy_read (void)
|
---|
| 464 | {
|
---|
| 465 | uint8_t octet;
|
---|
| 466 | ER error;
|
---|
| 467 |
|
---|
| 468 | /* フレーム開始フラグシーケンスまで入力を読み飛ばす */
|
---|
| 469 | while ((error = HDLC_getoctet(&octet)) != true) {
|
---|
| 470 | if (error != E_OK)
|
---|
| 471 | return;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | /* フレーム開始フラグシーケンスの間は入力を読み飛ばす */
|
---|
| 475 | while (octet == HDLC_FLAG_SEQ)
|
---|
| 476 | if (HDLC_getoctet(&octet) != E_OK)
|
---|
| 477 | return;
|
---|
| 478 |
|
---|
| 479 | /* 入力したオクテットは Address: All-Stations か */
|
---|
| 480 | if (octet == HDLC_ADDR_ALL) {
|
---|
| 481 |
|
---|
| 482 | /* 次は Control: UI */
|
---|
| 483 | if (HDLC_getoctet(&octet) != E_OK || octet != HDLC_CNTL_UI)
|
---|
| 484 | return;
|
---|
| 485 | if (HDLC_getoctet(&octet) != E_OK)
|
---|
| 486 | return;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | #ifdef LCP_CFG_ACCOMP
|
---|
| 490 |
|
---|
| 491 | else if (lcp_remote_ack_cfg.options & LCP_CFG_ACCOMP)
|
---|
| 492 | ;
|
---|
| 493 |
|
---|
| 494 | #endif/* of #ifdef LCP_CFG_ACCOMP */
|
---|
| 495 |
|
---|
| 496 | else
|
---|
| 497 | return;
|
---|
| 498 |
|
---|
| 499 | while (HDLC_getoctet(&octet) == E_OK)
|
---|
| 500 | ;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | #ifdef PPP_CFG_MODEM
|
---|
| 504 |
|
---|
| 505 | /*
|
---|
| 506 | * HDLC_raw_getoctet -- HDLC オクテット (Raw) 入力
|
---|
| 507 | */
|
---|
| 508 |
|
---|
| 509 | int HDLC_raw_getoctet (void)
|
---|
| 510 | {
|
---|
| 511 | uint8_t ch;
|
---|
| 512 |
|
---|
| 513 | /* 1オクテット入力する */
|
---|
| 514 | if (serial_rea_dat(HDLC_PORTID, &ch, sizeof(ch)) <= 0)
|
---|
| 515 | return '\0';
|
---|
| 516 |
|
---|
| 517 | NET_COUNT_PPP_HDLC(net_count_hdlc.in_octets, 1);
|
---|
| 518 | return ch;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | /*
|
---|
| 522 | * HDLC_raw_putoctet -- HDLC オクテット (Raw) 出力
|
---|
| 523 | */
|
---|
| 524 |
|
---|
| 525 | void HDLC_raw_putoctet (uint8_t ch)
|
---|
| 526 | {
|
---|
| 527 | NET_COUNT_PPP_HDLC(net_count_hdlc.out_octets, 1);
|
---|
| 528 | /* 1オクテット出力する */
|
---|
| 529 | serial_wri_dat(HDLC_PORTID, &ch, sizeof(ch));
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | #endif /* of #ifdef PPP_CFG_MODEM */
|
---|
| 533 |
|
---|
| 534 | #endif /* fo #ifdef SUPPORT_PPP */
|
---|