1    | /*
2    |  * Layer Two Tunnelling Protocol Daemon
3    |  * Copyright (C) 1998 Adtran, Inc.
4    |  *
5    |  * Mark Spencer
6    |  *
7    |  * This software is distributed under the terms
8    |  * of the GPL, which you should have received
9    |  * along with this source.
10   |  *
11   |  * Protocol and implementation information,
12   |  * structures and constants.
13   |  */
14   | /*
15   | typedef unsigned short _u16;
16   | typedef unsigned long long _u64;
17   |  */
18   | #ifndef _L2TP_H
19   | #define _L2TP_H
20   | 
21   | #define MAXSTRLEN 120		/* Maximum length of common strings */
22   | 
23   | #include <netinet/in.h>
24   | #include <termios.h>
25   | #include "osport.h"
26   | #include "scheduler.h"
27   | #include "misc.h"
28   | #include "file.h"
29   | #include "call.h"
30   | #include "avp.h"
31   | #include "control.h"
32   | #include "aaa.h"
33   | #include "common.h"
34   | 
35   | #define CONTROL_PIPE "/var/run/l2tp-control"
36   | 
37   | #define BINARY "l2tpd"
38   | #define SERVER_VERSION "0.65.1"
39   | #define VENDOR_NAME "Adtran, l2tpd"
40   | #define PPPD		"/usr/sbin/pppd"
41   | #define CALL_PPP_OPTS "defaultroute"
42   | #define FIRMWARE_REV	0x0651	/* Revision of our firmware (software, in this case) */
43   | #define DEF_MAX_TUNNELS 32	/* By default only allow this many
44   | 				   tunnels to exist */
45   | 
46   | #define HELLO_DELAY 60		/* How often to send a Hello message */
47   | 
48   | struct control_hdr
49   | {
50   |     _u16 ver;			/* Version and more */
51   |     _u16 length;		/* Length field */
52   |     _u16 tid;			/* Tunnel ID */
53   |     _u16 cid;			/* Call ID */
54   |     _u16 Ns;			/* Next sent */
55   |     _u16 Nr;			/* Next received */
56   | };
57   | 
58   | #define CTBIT(ver) (ver & 0x8000)	/* Determins if control or not */
59   | #define CLBIT(ver) (ver & 0x4000)	/* Length bit present.  Must be 1
60   | 					   for control messages */
61   | 
62   | #define CZBITS(ver) (ver &0x37F8)	/* Reserved bits:  We must drop 
63   | 					   anything with these there */
64   | 
65   | #define CFBIT(ver) (ver & 0x0800)	/* Presence of Ns and Nr fields
66   | 					   flow bit? */
67   | 
68   | #define CVER(ver) (ver & 0x0007)	/* Version of encapsulation */
69   | 
70   | 
71   | struct payload_hdr
72   | {
73   |     _u16 ver;			/* Version and friends */
74   |     _u16 length;		/* Optional Length */
75   |     _u16 tid;			/* Tunnel ID */
76   |     _u16 cid;			/* Caller ID */
77   |     _u16 Ns;			/* Optional next sent */
78   |     _u16 Nr;			/* Optional next received */
79   |     _u16 o_size;		/* Optional offset size */
80   |     _u16 o_pad;			/* Optional offset padding */
81   | };
82   | 
83   | #define NZL_TIMEOUT_DIVISOR 4	/* Divide TIMEOUT by this and
84   | 				   you know how often to send
85   | 				   a zero byte packet */
86   | 
87   | #define PAYLOAD_BUF 10		/* Provide 10 expansion bytes
88   | 				   so we can "decompress" the
89   | 				   payloads and simplify coding */
90   | #define DEFAULT_MAX_RETRIES 5	/* Recommended value from spec */
91   | #define DEFAULT_RWS_SIZE   4	/* Default max outstanding 
92   | 				   control packets in queue */
93   | #define DEFAULT_TX_BPS		10000000	/* For outgoing calls, report this speed */
94   | #define DEFAULT_RX_BPS		10000000
95   | #define DEFAULT_MAX_BPS		10000000	/* jz: outgoing calls max bps */
96   | #define DEFAULT_MIN_BPS		10000	/* jz: outgoing calls min bps */
97   | #define PAYLOAD_FUDGE		2	/* How many packets we're willing to drop */
98   | #define MIN_PAYLOAD_HDR_LEN 6
99   | 
100  | #define UDP_LISTEN_PORT  1701
101  | 				/* FIXME: MAX_RECV_SIZE, what is it? */
102  | #define MAX_RECV_SIZE 4096	/* Biggest packet we'll accept */
103  | 
104  | #define OUR_L2TP_VERSION 0x100	/* We support version 1, revision 0 */
105  | 
106  | #define PTBIT(ver) CTBIT(ver)	/* Type bit:  Must be zero for us */
107  | #define PLBIT(ver) CLBIT(ver)	/* Length specified? */
108  | #define PFBIT(ver) CFBIT(ver)	/* Flow control specified? */
109  | #define PVER(ver) CVER(ver)	/* Version */
110  | #define PZBITS(ver) (ver & 0x14F8)	/* Reserved bits */
111  | #define PRBIT(ver) (ver & 0x2000)	/* Reset Sr bit */
112  | #define PSBIT(ver) (ver & 0x0200)	/* Offset size bit */
113  | #define PPBIT(ver) (ver & 0x0100)	/* Preference bit */
114  | 
115  | struct tunnel
116  | {
117  |     struct call *call_head;	/* Member calls */
118  |     struct tunnel *next;	/* Allows us to be linked easily */
119  | 
120  |     int fc;			/* Framing capabilities of peer */
121  |     struct schedule_entry *hello;
122  |     int ourfc;			/* Our framing capabilities */
123  |     int bc;			/* Peer's bearer channels */
124  |     int hbit;			/* Allow hidden AVP's? */
125  |     int ourbc;			/* Our bearer channels */
126  |     _u64 tb;			/* Their tie breaker */
127  |     _u64 ourtb;			/* Our tie breaker */
128  |     int tid;			/* Peer's tunnel identifier */
129  |     int ourtid;			/* Our tunnel identifier */
130  |     int qtid;			/* TID for disconnection */
131  |     int firmware;		/* Peer's firmware revision */
132  | #if 0
133  |     unsigned int addr;		/* Remote address */
134  |     unsigned short port;	/* Port on remote end */
135  | #else
136  |     struct sockaddr_in peer;	/* Peer's Address */
137  | #endif
138  |     int debug;			/* Are we debugging or not? */
139  |     int nego;			/* Show Negotiation? */
140  |     int count;			/* How many membmer calls? */
141  |     int state;			/* State of tunnel */
142  |     _u16 cSs;			/* Sequence for next packet */
143  |     _u16 cSr;			/* Next expected to receive */
144  |     int cLr;			/* Last packet received by peer */
145  |     char hostname[MAXSTRLEN];	/* Remote hostname */
146  |     char vendor[MAXSTRLEN];	/* Vendor of remote product */
147  |     struct challenge chal_us;	/* Their Challenge to us */
148  |     struct challenge chal_them;	/* Our challenge to them */
149  |     char secret[MAXSTRLEN];	/* Secret to use */
150  | #ifdef SANITY
151  |     int sanity;			/* check for sanity? */
152  | #endif
153  |     int rws;			/* Peer's Receive Window Size */
154  |     int ourrws;			/* Receive Window Size */
155  |     struct call *self;
156  |     struct lns *lns;		/* LNS that owns us */
157  |     struct lac *lac;		/* LAC that owns us */
158  | };
159  | 
160  | struct tunnel_list
161  | {
162  |     struct tunnel *head;
163  |     int count;
164  |     int calls;
165  | };
166  | 
167  | /* Values for version */
168  | #define VER_L2TP 2
169  | #define VER_PPTP 3
170  | 
171  | /* Some PPP sync<->async stuff */
172  | #define fcstab  ppp_crc16_table
173  | 
174  | #define PPP_FLAG 0x7e
175  | #define PPP_ESCAPE 0x7d
176  | #define PPP_TRANS 0x20
177  | 
178  | #define PPP_INITFCS 0xffff
179  | #define PPP_GOODFCS 0xf0b8
180  | #define PPP_FCS(fcs,c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
181  | 
182  | /* Error Values */
183  | 
184  | extern struct tunnel_list tunnels;
185  | extern void tunnel_close (struct tunnel *t);
186  | extern void network_thread ();
187  | extern int init_network ();
188  | extern int debug_tunnel;
189  | extern int packet_dump;
190  | extern int debug_avp;
191  | extern int debug_state;
192  | extern int max_tunnels;
193  | extern int kernel_support;
194  | extern int server_socket;
195  | extern struct tunnel *new_tunnel ();
196  | extern struct packet_queue xmit_udp;
197  | extern void destroy_tunnel (struct tunnel *);
198  | extern struct buffer *new_payload (struct sockaddr_in);
199  | extern void recycle_payload (struct buffer *, struct sockaddr_in);
200  | extern void add_payload_hdr (struct tunnel *, struct call *, struct buffer *);
201  | extern int read_packet (struct buffer *, int, int);
202  | extern void udp_xmit (struct buffer *);
203  | extern void control_xmit (void *);
204  | extern int ppd;
205  | extern int switch_io;		/* jz */
206  | extern int control_fd;
207  | extern int start_pppd (struct call *c, struct ppp_opts *);
208  | extern void magic_lac_dial (void *);
209  | #ifndef MIN
210  | #define MIN(a,b) (((a)<(b)) ? (a) : (b))
211  | #endif
212  | #endif
213  | 
214  | 
215  | /* 
216  |  * This is just some stuff to take
217  |  * care of kernel definitions
218  |  */
219  | 
220  | #ifdef USE_KERNEL
221  | #include <linux/sockios.h>
222  | #ifndef SIOCSETL2TP
223  | #warning This kernel source does not have l2tp support
224  | #undef USE_KERNEL
225  | #else
226  | #include <asm/types.h>
227  | #include <linux/l2tp.h>
228  | #endif
229  | #endif