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