]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcpdump/print-lcp.c
This commit was generated by cvs2svn to compensate for changes in r104470,
[FreeBSD/FreeBSD.git] / contrib / tcpdump / print-lcp.c
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-lcp.c,v 1.9 2000/10/06 04:23:12 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "extract.h"                    /* must come after interface.h */
43 #include "ppp.h"
44
45 /* Codes */
46 enum { 
47   LCP_CONFREQ = 1,
48   LCP_CONFACK = 2,
49   LCP_CONFNAK = 3,
50   LCP_CONFREJ = 4,
51   LCP_TERMREQ = 5,
52   LCP_TERMACK = 6,
53   LCP_CODEREJ = 7,
54   LCP_PROTREJ = 8,
55   LCP_ECHOREQ = 9,
56   LCP_ECHOREP = 10,
57   LCP_DISCARD = 11
58 };
59
60 static struct tok lcpcode2str[] = {
61   { LCP_CONFREQ, "ConfReq" },
62   { LCP_CONFACK, "ConfAck" },
63   { LCP_CONFNAK, "ConfNak" },
64   { LCP_CONFREJ, "ConfRej" },
65   { LCP_TERMREQ, "TermReq" },
66   { LCP_TERMACK, "TermAck" },
67   { LCP_CODEREJ, "CodeRej" },
68   { LCP_PROTREJ, "ProtRej" },
69   { LCP_ECHOREQ, "EchoReq" },
70   { LCP_ECHOREP, "EchoRep" },
71   { LCP_DISCARD, "Discard" },
72   { 0, NULL }
73 };
74
75
76 enum {
77   LCP_RESERVED  = 0,
78   LCP_MRU       = 1,
79   LCP_ASYNCMAP  = 2,
80   LCP_AUTHPROTO = 3,
81   LCP_QUALPROTO = 4,
82   LCP_MAGICNUM  = 5,
83   LCP_PCOMP     = 7,
84   LCP_ACFCOMP   = 8,
85   LCP_CALLBACK  = 13
86 };
87
88 static struct tok lcpoption2str[] = {
89   { LCP_RESERVED, "reserved"},
90   { LCP_MRU, "mru"},
91   { LCP_ASYNCMAP, "asyncmap"},
92   { LCP_AUTHPROTO, "auth"},
93   { LCP_QUALPROTO, "qual"},
94   { LCP_MAGICNUM, "magic"},
95   { LCP_PCOMP, "pcomp"},
96   { LCP_ACFCOMP, "acfcomp"},
97   { LCP_CALLBACK, "callback"},
98   { 0, NULL }
99 };
100
101 static struct tok lcpauth2str[] = {
102   {0xc023, "PAP"},
103   {0xc223, "CHAP"},
104   { 0, NULL }
105 };
106
107 static struct tok lcpqual2str[] = {
108   {0xc025, "LQR"},
109   { 0, NULL }
110 };
111
112 static struct tok lcpchap2str[] = {
113   {0x05, "MD5"},
114   {0x80, "MS"},
115   { 0, NULL }
116 };
117
118 void
119 lcp_print(register const u_char *bp, u_int length)
120 {
121   u_short lcp_code, lcp_id, lcp_length;
122   const u_char *lcp_data;
123
124   lcp_data = bp+4;
125         
126   if (snapend < lcp_data) {
127     printf(" [LCP|]");
128     return;
129   }
130   
131   lcp_code  = bp[0];
132   lcp_id    = bp[1];
133   lcp_length = EXTRACT_16BITS(bp+2);
134
135   printf("LCP %s id=0x%x", tok2str(lcpcode2str, "LCP-#%d", lcp_code), lcp_id);
136   
137   switch (lcp_code) {
138   case LCP_CONFREQ:
139   case LCP_CONFACK:
140   case LCP_CONFNAK:
141   case LCP_CONFREJ:
142     /* Print Options */
143     {
144       u_char lcpopt_type, lcpopt_length;
145       const u_char *p=lcp_data;
146       while (p+2 < lcp_data+lcp_length && p+2 < snapend) {
147         lcpopt_type = p[0];
148         lcpopt_length = p[1];
149         p+=2;
150         printf(" <%s ",tok2str(lcpoption2str, "option-#%d", lcpopt_type));
151         if (lcpopt_length)
152           switch (lcpopt_type) {
153           case LCP_MRU:
154             if (snapend < p+2) return;
155             printf("%d",ntohs(*(u_short*)p));
156             if (lcpopt_length != 4) printf(" len=%d!",lcpopt_length);
157             break;
158           case LCP_AUTHPROTO:
159             if (snapend < p+2) return;
160             printf("%s",tok2str(lcpauth2str, "AUTH-%#x", ntohs(*(u_short*)p)));
161             if (lcpopt_length < 4) printf(" len=%d!",lcpopt_length);
162             if (lcpopt_length >= 5 && p < snapend) 
163               printf(" %s",tok2str(lcpchap2str, "%#x", p[0]));
164             break;
165           case LCP_QUALPROTO:
166             if (snapend < p+2) return;
167             printf("%s",tok2str(lcpqual2str, "QUAL-%#x", ntohs(*(u_short*)p)));
168             if (lcpopt_length < 4) printf(" len=%d!",lcpopt_length);
169             /* Print data field of auth? */
170             break;
171           case LCP_ASYNCMAP:
172           case LCP_MAGICNUM:
173             if (snapend < p+4) return;
174             printf("%#x", (unsigned)ntohl(*(u_long*)p));
175             if (lcpopt_length != 6) printf(" len=%d!",lcpopt_length);
176             break;
177           case LCP_PCOMP:
178           case LCP_ACFCOMP:
179           case LCP_RESERVED:
180             if (lcpopt_length != 2) printf(" len=%d!",lcpopt_length);
181             break;
182           default:
183             if (lcpopt_length != 2) printf(" len=%d",lcpopt_length);
184             break;
185           }
186         printf(">");
187         p+=lcpopt_length-2;
188       }
189     }
190     break;
191   case LCP_ECHOREQ:
192   case LCP_ECHOREP:
193   case LCP_DISCARD:
194     if (snapend < lcp_data+4) return;
195     printf(" magic=%#x", (unsigned)ntohl(*(u_long *) lcp_data));
196     lcp_data +=4;
197     break;
198   case LCP_PROTREJ:
199     if (snapend < lcp_data+2) return;
200     printf(" prot=%s", tok2str(ppptype2str, "PROT-%#x", ntohs(*(u_short *) lcp_data)));
201     /* TODO print rejected packet too ? */
202     break;
203   case LCP_CODEREJ:
204     if (snapend < lcp_data+4) return;
205     printf(" ");
206     lcp_print(lcp_data, (lcp_length+lcp_data > snapend ? snapend-lcp_data : lcp_length));
207     break;
208   case LCP_TERMREQ:
209   case LCP_TERMACK:
210     break;
211   default:
212     break;
213   }
214   
215   return;
216 }