3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #define PPP_U8(frm) (get_u8(frm))
37 #define PPP_U16(frm) (btohs(htons(get_u16(frm))))
38 #define PPP_U32(frm) (btohl(htonl(get_u32(frm))))
40 static int ppp_traffic = 0;
42 static unsigned char ppp_magic1[] = { 0x7e, 0xff, 0x03, 0xc0, 0x21 };
43 static unsigned char ppp_magic2[] = { 0x7e, 0xff, 0x7d, 0x23, 0xc0, 0x21 };
44 static unsigned char ppp_magic3[] = { 0x7e, 0x7d, 0xdf, 0x7d, 0x23, 0xc0, 0x21 };
46 static inline int check_for_ppp_traffic(unsigned char *data, int size)
50 for (i = 0; i < size - sizeof(ppp_magic1); i++)
51 if (!memcmp(data + i, ppp_magic1, sizeof(ppp_magic1))) {
56 for (i = 0; i < size - sizeof(ppp_magic2); i++)
57 if (!memcmp(data + i, ppp_magic2, sizeof(ppp_magic2))) {
62 for (i = 0; i < size - sizeof(ppp_magic3); i++)
63 if (!memcmp(data + i, ppp_magic3, sizeof(ppp_magic3))) {
71 static inline char *dir2str(uint8_t in)
73 return in ? "DCE" : "DTE";
76 static inline char *proto2str(uint16_t proto)
80 return "Padding Protocol";
84 return "IP Control Protocol";
86 return "Compression Control Protocol";
88 return "Link Control Protocol";
90 return "Password Authentication Protocol";
92 return "Link Quality Report";
94 return "Challenge Handshake Authentication Protocol";
96 return "Unknown Protocol";
100 static void hdlc_dump(int level, struct frame *frm)
102 uint8_t addr = p_get_u8(frm);
103 uint8_t ctrl = p_get_u8(frm);
106 fcs = get_unaligned((uint16_t *) (frm->ptr + frm->len - 2));
109 p_indent(level, frm);
111 if (addr != 0xff || ctrl != 0x03) {
114 printf("HDLC: %s: len %d fcs 0x%04x\n",
115 dir2str(frm->in), frm->len, fcs);
117 printf("HDLC: %s: addr 0x%02x ctrl 0x%02x len %d fcs 0x%04x\n",
118 dir2str(frm->in), addr, ctrl, frm->len, fcs);
120 if (*((uint8_t *) frm->ptr) & 0x80)
121 proto = p_get_u16(frm);
123 proto = p_get_u8(frm);
125 p_indent(level + 1, frm);
126 printf("PPP: %s (0x%04x): len %d\n", proto2str(proto), proto, frm->len);
128 raw_dump(level + 1, frm);
131 static inline void unslip_frame(int level, struct frame *frm, int len)
134 unsigned char *data, *ptr;
137 data = malloc(len * 2);
143 for (i = 0; i < len; i++) {
144 if (ptr[i] == 0x7d) {
145 data[p++] = ptr[i + 1] ^ 0x20;
151 memset(&msg, 0, sizeof(msg));
153 msg.data_len = len * 2;
158 msg.handle = frm->handle;
161 hdlc_dump(level, &msg);
166 void ppp_dump(int level, struct frame *frm)
169 int err, len, pos = 0;
171 if (frm->pppdump_fd > fileno(stderr)) {
173 uint16_t len = htons(frm->len);
174 uint32_t ts = htonl(frm->ts.tv_sec & 0xffffffff);
177 err = write(frm->pppdump_fd, &id, 1);
181 err = write(frm->pppdump_fd, &ts, 4);
185 id = frm->in ? 0x02 : 0x01;
186 err = write(frm->pppdump_fd, &id, 1);
189 err = write(frm->pppdump_fd, &len, 2);
192 err = write(frm->pppdump_fd, frm->ptr, frm->len);
198 pos = check_for_ppp_traffic(frm->ptr, frm->len);
200 raw_dump(level, frm);
205 raw_ndump(level, frm, pos);
211 frm = add_frame(frm);
213 while (frm->len > 0) {
214 ptr = memchr(frm->ptr, 0x7e, frm->len);
218 if (frm->ptr != ptr) {
219 frm->len -= (ptr - frm->ptr);
223 end = memchr(frm->ptr + 1, 0x7e, frm->len - 1);
233 unslip_frame(level, frm, len);