Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / tools / parser / ppp.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "parser.h"
35
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))))
39
40 static int ppp_traffic = 0;
41
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 };
45
46 static inline int check_for_ppp_traffic(unsigned char *data, int size)
47 {
48         unsigned int i;
49
50         for (i = 0; i < size - sizeof(ppp_magic1); i++)
51                 if (!memcmp(data + i, ppp_magic1, sizeof(ppp_magic1))) {
52                         ppp_traffic = 1;
53                         return i;
54                 }
55
56         for (i = 0; i < size - sizeof(ppp_magic2); i++)
57                 if (!memcmp(data + i, ppp_magic2, sizeof(ppp_magic2))) {
58                         ppp_traffic = 1;
59                         return i;
60                 }
61
62         for (i = 0; i < size - sizeof(ppp_magic3); i++)
63                 if (!memcmp(data + i, ppp_magic3, sizeof(ppp_magic3))) {
64                         ppp_traffic = 1;
65                         return i;
66                 }
67
68         return -1;
69 }
70
71 static inline char *dir2str(uint8_t in)
72 {
73         return in ? "DCE" : "DTE";
74 }
75
76 static inline char *proto2str(uint16_t proto)
77 {
78         switch (proto) {
79         case 0x0001:
80                 return "Padding Protocol";
81         case 0x0021:
82                 return "IP";
83         case 0x8021:
84                 return "IP Control Protocol";
85         case 0x80fd:
86                 return "Compression Control Protocol";
87         case 0xc021:
88                 return "Link Control Protocol";
89         case 0xc023:
90                 return "Password Authentication Protocol";
91         case 0xc025:
92                 return "Link Quality Report";
93         case 0xc223:
94                 return "Challenge Handshake Authentication Protocol";
95         default:
96                 return "Unknown Protocol";
97         }
98 }
99
100 static void hdlc_dump(int level, struct frame *frm)
101 {
102         uint8_t addr = p_get_u8(frm);
103         uint8_t ctrl = p_get_u8(frm);
104         uint16_t fcs, proto;
105
106         fcs = get_unaligned((uint16_t *) (frm->ptr + frm->len - 2));
107         frm->len -= 2;
108
109         p_indent(level, frm);
110
111         if (addr != 0xff || ctrl != 0x03) {
112                 frm->ptr -= 2;
113                 frm->len += 2;
114                 printf("HDLC: %s: len %d fcs 0x%04x\n",
115                                 dir2str(frm->in), frm->len, fcs);
116         } else
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);
119
120         if (*((uint8_t *) frm->ptr) & 0x80)
121                 proto = p_get_u16(frm);
122         else
123                 proto = p_get_u8(frm);
124
125         p_indent(level + 1, frm);
126         printf("PPP: %s (0x%04x): len %d\n", proto2str(proto), proto, frm->len);
127
128         raw_dump(level + 1, frm);
129 }
130
131 static inline void unslip_frame(int level, struct frame *frm, int len)
132 {
133         struct frame msg;
134         unsigned char *data, *ptr;
135         int i, p = 0;
136
137         data = malloc(len * 2);
138         if (!data)
139                 return;
140
141         ptr = frm->ptr;
142
143         for (i = 0; i < len; i++) {
144                 if (ptr[i] == 0x7d) {
145                         data[p++] = ptr[i + 1] ^ 0x20;
146                         i++;
147                 } else
148                         data[p++] = ptr[i];
149         }
150
151         memset(&msg, 0, sizeof(msg));
152         msg.data     = data;
153         msg.data_len = len * 2;
154         msg.ptr      = msg.data;
155         msg.len      = p;
156         msg.in       = frm->in;
157         msg.ts       = frm->ts;
158         msg.handle   = frm->handle;
159         msg.cid      = frm->cid;
160
161         hdlc_dump(level, &msg);
162
163         free(data);
164 }
165
166 void ppp_dump(int level, struct frame *frm)
167 {
168         void *ptr, *end;
169         int err, len, pos = 0;
170
171         if (frm->pppdump_fd > fileno(stderr)) {
172                 unsigned char id;
173                 uint16_t len = htons(frm->len);
174                 uint32_t ts = htonl(frm->ts.tv_sec & 0xffffffff);
175
176                 id = 0x07;
177                 err = write(frm->pppdump_fd, &id, 1);
178                 if (err < 0)
179                         return;
180
181                 err = write(frm->pppdump_fd, &ts, 4);
182                 if (err < 0)
183                         return;
184
185                 id = frm->in ? 0x02 : 0x01;
186                 err = write(frm->pppdump_fd, &id, 1);
187                 if (err < 0)
188                         return;
189                 err = write(frm->pppdump_fd, &len, 2);
190                 if (err < 0)
191                         return;
192                 err = write(frm->pppdump_fd, frm->ptr, frm->len);
193                 if (err < 0)
194                         return;
195         }
196
197         if (!ppp_traffic) {
198                 pos = check_for_ppp_traffic(frm->ptr, frm->len);
199                 if (pos < 0) {
200                         raw_dump(level, frm);
201                         return;
202                 }
203
204                 if (pos > 0) {
205                         raw_ndump(level, frm, pos);
206                         frm->ptr += pos;
207                         frm->len -= pos;
208                 }
209         }
210
211         frm = add_frame(frm);
212
213         while (frm->len > 0) {
214                 ptr = memchr(frm->ptr, 0x7e, frm->len);
215                 if (!ptr)
216                         break;
217
218                 if (frm->ptr != ptr) {
219                         frm->len -= (ptr - frm->ptr);
220                         frm->ptr = ptr;
221                 }
222
223                 end = memchr(frm->ptr + 1, 0x7e, frm->len - 1);
224                 if (!end)
225                         break;
226
227                 len = end - ptr - 1;
228
229                 frm->ptr++;
230                 frm->len--;
231
232                 if (len > 0) {
233                         unslip_frame(level, frm, len);
234
235                         frm->ptr += len;
236                         frm->len -= len;
237                 }
238         }
239 }