1 /*
2  *	IrNET protocol module : Synchronous PPP over an IrDA socket.
3  *
4  *		Jean II - HPL `00 - <jt@hpl.hp.com>
5  *
6  * This file contains definitions and declarations global to the IrNET module,
7  * all grouped in one place...
8  * This file is a *private* header, so other modules don't want to know
9  * what's in there...
10  *
11  * Note : as most part of the Linux kernel, this module is available
12  * under the GNU General Public License (GPL).
13  */
14 
15 #ifndef IRNET_H
16 #define IRNET_H
17 
18 /************************** DOCUMENTATION ***************************/
19 /*
20  * What is IrNET
21  * -------------
22  * IrNET is a protocol allowing to carry TCP/IP traffic between two
23  * IrDA peers in an efficient fashion. It is a thin layer, passing PPP
24  * packets to IrTTP and vice versa. It uses PPP in synchronous mode,
25  * because IrTTP offer a reliable sequenced packet service (as opposed
26  * to a byte stream). In fact, you could see IrNET as carrying TCP/IP
27  * in a IrDA socket, using PPP to provide the glue.
28  *
29  * The main difference with traditional PPP over IrCOMM is that we
30  * avoid the framing and serial emulation which are a performance
31  * bottleneck. It also allows multipoint communications in a sensible
32  * fashion.
33  *
34  * The main difference with IrLAN is that we use PPP for the link
35  * management, which is more standard, interoperable and flexible than
36  * the IrLAN protocol. For example, PPP adds authentication,
37  * encryption, compression, header compression and automated routing
38  * setup. And, as IrNET let PPP do the hard work, the implementation
39  * is much simpler than IrLAN.
40  *
41  * The Linux implementation
42  * ------------------------
43  * IrNET is written on top of the Linux-IrDA stack, and interface with
44  * the generic Linux PPP driver. Because IrNET depend on recent
45  * changes of the PPP driver interface, IrNET will work only with very
46  * recent kernel (2.3.99-pre6 and up).
47  *
48  * The present implementation offer the following features :
49  *	o simple user interface using pppd
50  *	o efficient implementation (interface directly to PPP and IrTTP)
51  *	o addressing (you can specify the name of the IrNET recipient)
52  *	o multipoint operation (limited by IrLAP specification)
53  *	o information in /proc/net/irda/irnet
54  *	o IrNET events on /dev/irnet (for user space daemon)
55  *	o IrNET daemon (irnetd) to automatically handle incoming requests
56  *	o Windows 2000 compatibility (tested, but need more work)
57  * Currently missing :
58  *	o Lot's of testing (that's your job)
59  *	o Connection retries (may be too hard to do)
60  *	o Check pppd persist mode
61  *	o User space daemon (to automatically handle incoming requests)
62  *
63  * The setup is not currently the most easy, but this should get much
64  * better when everything will get integrated...
65  *
66  * Acknowledgements
67  * ----------------
68  * This module is based on :
69  *	o The PPP driver (ppp_synctty/ppp_generic) by Paul Mackerras
70  *	o The IrLAN protocol (irlan_common/XXX) by Dag Brattli
71  *	o The IrSock interface (af_irda) by Dag Brattli
72  *	o Some other bits from the kernel and my drivers...
73  * Infinite thanks to those brave souls for providing the infrastructure
74  * upon which IrNET is built.
75  *
76  * Thanks to all my collegues in HP for helping me. In particular,
77  * thanks to Salil Pradhan and Bill Serra for W2k testing...
78  * Thanks to Luiz Magalhaes for irnetd and much testing...
79  *
80  * Thanks to Alan Cox for answering lot's of my stupid questions, and
81  * to Paul Mackerras answering my questions on how to best integrate
82  * IrNET and pppd.
83  *
84  * Jean II
85  *
86  * Note on some implementations choices...
87  * ------------------------------------
88  *	1) Direct interface vs tty/socket
89  * I could have used a tty interface to hook to ppp and use the full
90  * socket API to connect to IrDA. The code would have been easier to
91  * maintain, and maybe the code would have been smaller...
92  * Instead, we hook directly to ppp_generic and to IrTTP, which make
93  * things more complicated...
94  *
95  * The first reason is flexibility : this allow us to create IrNET
96  * instances on demand (no /dev/ircommX crap) and to allow linkname
97  * specification on pppd command line...
98  *
99  * Second reason is speed optimisation. If you look closely at the
100  * transmit and receive paths, you will notice that they are "super lean"
101  * (that's why they look ugly), with no function calls and as little data
102  * copy and modification as I could...
103  *
104  *	2) irnetd in user space
105  * irnetd is implemented in user space, which is necessary to call pppd.
106  * This also give maximum benefits in term of flexibility and customability,
107  * and allow to offer the event channel, useful for other stuff like debug.
108  *
109  * On the other hand, this require a loose coordination between the
110  * present module and irnetd. One critical area is how incoming request
111  * are handled.
112  * When irnet receive an incoming request, it send an event to irnetd and
113  * drop the incoming IrNET socket.
114  * irnetd start a pppd instance, which create a new IrNET socket. This new
115  * socket is then connected in the originating node to the pppd instance.
116  * At this point, in the originating node, the first socket is closed.
117  *
118  * I admit, this is a bit messy and waste some ressources. The alternative
119  * is caching incoming socket, and that's also quite messy and waste
120  * ressources.
121  * We also make connection time slower. For example, on a 115 kb/s link it
122  * adds 60ms to the connection time (770 ms). However, this is slower than
123  * the time it takes to fire up pppd on my P133...
124  *
125  *
126  * History :
127  * -------
128  *
129  * v1 - 15.5.00 - Jean II
130  *	o Basic IrNET (hook to ppp_generic & IrTTP - incl. multipoint)
131  *	o control channel on /dev/irnet (set name/address)
132  *	o event channel on /dev/irnet (for user space daemon)
133  *
134  * v2 - 5.6.00 - Jean II
135  *	o Enable DROP_NOT_READY to avoid PPP timeouts & other weirdness...
136  *	o Add DISCONNECT_TO event and rename DISCONNECT_FROM.
137  *	o Set official device number alloaction on /dev/irnet
138  *
139  * v3 - 30.8.00 - Jean II
140  *	o Update to latest Linux-IrDA changes :
141  *		- queue_t => irda_queue_t
142  *	o Update to ppp-2.4.0 :
143  *		- move irda_irnet_connect from PPPIOCATTACH to TIOCSETD
144  *	o Add EXPIRE event (depend on new IrDA-Linux patch)
145  *	o Switch from `hashbin_remove' to `hashbin_remove_this' to fix
146  *	  a multilink bug... (depend on new IrDA-Linux patch)
147  *	o fix a self->daddr to self->raddr in irda_irnet_connect to fix
148  *	  another multilink bug (darn !)
149  *	o Remove LINKNAME_IOCTL cruft
150  *
151  * v3b - 31.8.00 - Jean II
152  *	o Dump discovery log at event channel startup
153  *
154  * v4 - 28.9.00 - Jean II
155  *	o Fix interaction between poll/select and dump discovery log
156  *	o Add IRNET_BLOCKED_LINK event (depend on new IrDA-Linux patch)
157  *	o Add IRNET_NOANSWER_FROM event (mostly to help support)
158  *	o Release flow control in disconnect_indication
159  *	o Block packets while connecting (speed up connections)
160  *
161  * v5 - 11.01.01 - Jean II
162  *	o Init self->max_header_size, just in case...
163  *	o Set up ap->chan.hdrlen, to get zero copy on tx side working.
164  *	o avoid tx->ttp->flow->ppp->tx->... loop, by checking flow state
165  *		Thanks to Christian Gennerat for finding this bug !
166  *	---
167  *	o Declare the proper MTU/MRU that we can support
168  *		(but PPP doesn't read the MTU value :-()
169  *	o Declare hashbin HB_NOLOCK instead of HB_LOCAL to avoid
170  *		disabling and enabling irq twice
171  *
172  * v6 - 31.05.01 - Jean II
173  *	o Print source address in Found, Discovery, Expiry & Request events
174  *	o Print requested source address in /proc/net/irnet
175  *	o Change control channel input. Allow multiple commands in one line.
176  *	o Add saddr command to change ap->rsaddr (and use that in IrDA)
177  *	---
178  *	o Make the IrDA connection procedure totally asynchronous.
179  *	  Heavy rewrite of the IAS query code and the whole connection
180  *	  procedure. Now, irnet_connect() no longer need to be called from
181  *	  a process context...
182  *	o Enable IrDA connect retries in ppp_irnet_send(). The good thing
183  *	  is that IrDA connect retries are directly driven by PPP LCP
184  *	  retries (we retry for each LCP packet), so that everything
185  *	  is transparently controlled from pppd lcp-max-configure.
186  *	o Add ttp_connect flag to prevent rentry on the connect procedure
187  *	o Test and fixups to eliminate side effects of retries
188  *
189  * v7 - 22.08.01 - Jean II
190  *	o Cleanup : Change "saddr = 0x0" to "saddr = DEV_ADDR_ANY"
191  *	o Fix bug in BLOCK_WHEN_CONNECT introduced in v6 : due to the
192  *	  asynchronous IAS query, self->tsap is NULL when PPP send the
193  *	  first packet.  This was preventing "connect-delay 0" to work.
194  *	  Change the test in ppp_irnet_send() to self->ttp_connect.
195  *
196  * v8 - 1.11.01 - Jean II
197  *	o Tighten the use of self->ttp_connect and self->ttp_open to
198  *	  prevent various race conditions.
199  *	o Avoid leaking discovery log and skb
200  *	o Replace "self" with "server" in irnet_connect_indication() to
201  *	  better detect cut'n'paste error ;-)
202  *
203  * v9 - 29.11.01 - Jean II
204  *	o Fix event generation in disconnect indication that I broke in v8
205  *	  It was always generation "No-Answer" because I was testing ttp_open
206  *	  just after clearing it. *blush*.
207  *	o Use newly created irttp_listen() to fix potential crash when LAP
208  *	  destroyed before irnet module removed.
209  *
210  * v10 - 4.3.2 - Jean II
211  *	o When receiving a disconnect indication, don't reenable the
212  *	  PPP Tx queue, this will trigger a reconnect. Instead, close
213  *	  the channel, which will kill pppd...
214  *
215  * v11 - 20.3.02 - Jean II
216  *	o Oops ! v10 fix disabled IrNET retries and passive behaviour.
217  *	  Better fix in irnet_disconnect_indication() :
218  *	  - if connected, kill pppd via hangup.
219  *	  - if not connected, reenable ppp Tx, which trigger IrNET retry.
220  *
221  * v12 - 10.4.02 - Jean II
222  *	o Fix race condition in irnet_connect_indication().
223  *	  If the socket was already trying to connect, drop old connection
224  *	  and use new one only if acting as primary. See comments.
225  */
226 
227 /***************************** INCLUDES *****************************/
228 
229 #include <linux/module.h>
230 
231 #include <linux/kernel.h>
232 #include <linux/skbuff.h>
233 #include <linux/tty.h>
234 #include <linux/proc_fs.h>
235 #include <linux/devfs_fs_kernel.h>
236 #include <linux/netdevice.h>
237 #include <linux/miscdevice.h>
238 #include <linux/poll.h>
239 #include <linux/config.h>
240 #include <linux/ctype.h>	/* isspace() */
241 #include <asm/uaccess.h>
242 
243 #include <linux/ppp_defs.h>
244 #include <linux/if_ppp.h>
245 #include <linux/ppp_channel.h>
246 
247 #include <net/irda/irda.h>
248 #include <net/irda/iriap.h>
249 #include <net/irda/irias_object.h>
250 #include <net/irda/irlmp.h>
251 #include <net/irda/irttp.h>
252 #include <net/irda/discovery.h>
253 
254 /***************************** OPTIONS *****************************/
255 /*
256  * Define or undefine to compile or not some optional part of the
257  * IrNET driver...
258  * Note : the present defaults make sense, play with that at your
259  * own risk...
260  */
261 /* IrDA side of the business... */
262 #define DISCOVERY_NOMASK	/* To enable W2k compatibility... */
263 #define ADVERTISE_HINT		/* Advertise IrLAN hint bit */
264 #define ALLOW_SIMULT_CONNECT	/* This seem to work, cross fingers... */
265 #define DISCOVERY_EVENTS	/* Query the discovery log to post events */
266 #define INITIAL_DISCOVERY	/* Dump current discovery log as events */
267 #undef STREAM_COMPAT		/* Not needed - potentially messy */
268 #undef CONNECT_INDIC_KICK	/* Might mess IrDA, not needed */
269 #undef FAIL_SEND_DISCONNECT	/* Might mess IrDA, not needed */
270 #undef PASS_CONNECT_PACKETS	/* Not needed ? Safe */
271 
272 /* PPP side of the business */
273 #define BLOCK_WHEN_CONNECT	/* Block packets when connecting */
274 #define CONNECT_IN_SEND		/* Retry IrDA connection procedure */
275 #undef FLUSH_TO_PPP		/* Not sure about this one, let's play safe */
276 #undef SECURE_DEVIRNET		/* Bah... */
277 
278 /****************************** DEBUG ******************************/
279 
280 /*
281  * This set of flags enable and disable all the various warning,
282  * error and debug message of this driver.
283  * Each section can be enabled and disabled independantly
284  */
285 /* In the PPP part */
286 #define DEBUG_CTRL_TRACE	0	/* Control channel */
287 #define DEBUG_CTRL_INFO		0	/* various info */
288 #define DEBUG_CTRL_ERROR	1	/* problems */
289 #define DEBUG_FS_TRACE		0	/* filesystem callbacks */
290 #define DEBUG_FS_INFO		0	/* various info */
291 #define DEBUG_FS_ERROR		1	/* problems */
292 #define DEBUG_PPP_TRACE		0	/* PPP related functions */
293 #define DEBUG_PPP_INFO		0	/* various info */
294 #define DEBUG_PPP_ERROR		1	/* problems */
295 #define DEBUG_MODULE_TRACE	0	/* module insertion/removal */
296 #define DEBUG_MODULE_ERROR	1	/* problems */
297 
298 /* In the IrDA part */
299 #define DEBUG_IRDA_SR_TRACE	0	/* IRDA subroutines */
300 #define DEBUG_IRDA_SR_INFO	0	/* various info */
301 #define DEBUG_IRDA_SR_ERROR	1	/* problems */
302 #define DEBUG_IRDA_SOCK_TRACE	0	/* IRDA main socket functions */
303 #define DEBUG_IRDA_SOCK_INFO	0	/* various info */
304 #define DEBUG_IRDA_SOCK_ERROR	1	/* problems */
305 #define DEBUG_IRDA_SERV_TRACE	0	/* The IrNET server */
306 #define DEBUG_IRDA_SERV_INFO	0	/* various info */
307 #define DEBUG_IRDA_SERV_ERROR	1	/* problems */
308 #define DEBUG_IRDA_TCB_TRACE	0	/* IRDA IrTTP callbacks */
309 #define DEBUG_IRDA_CB_INFO	0	/* various info */
310 #define DEBUG_IRDA_CB_ERROR	1	/* problems */
311 #define DEBUG_IRDA_OCB_TRACE	0	/* IRDA other callbacks */
312 #define DEBUG_IRDA_OCB_INFO	0	/* various info */
313 #define DEBUG_IRDA_OCB_ERROR	1	/* problems */
314 
315 #define DEBUG_ASSERT		0	/* Verify all assertions */
316 
317 /*
318  * These are the macros we are using to actually print the debug
319  * statements. Don't look at it, it's ugly...
320  *
321  * One of the trick is that, as the DEBUG_XXX are constant, the
322  * compiler will optimise away the if() in all cases.
323  */
324 /* All error messages (will show up in the normal logs) */
325 #define DERROR(dbg, format, args...) \
326 	{if(DEBUG_##dbg) \
327 		printk(KERN_INFO "irnet: %s(): " format, __FUNCTION__ , ##args);}
328 
329 /* Normal debug message (will show up in /var/log/debug) */
330 #define DEBUG(dbg, format, args...) \
331 	{if(DEBUG_##dbg) \
332 		printk(KERN_DEBUG "irnet: %s(): " format, __FUNCTION__ , ##args);}
333 
334 /* Entering a function (trace) */
335 #define DENTER(dbg, format, args...) \
336 	{if(DEBUG_##dbg) \
337 		printk(KERN_DEBUG "irnet: -> %s" format, __FUNCTION__ , ##args);}
338 
339 /* Entering and exiting a function in one go (trace) */
340 #define DPASS(dbg, format, args...) \
341 	{if(DEBUG_##dbg) \
342 		printk(KERN_DEBUG "irnet: <>%s" format, __FUNCTION__ , ##args);}
343 
344 /* Exiting a function (trace) */
345 #define DEXIT(dbg, format, args...) \
346 	{if(DEBUG_##dbg) \
347 		printk(KERN_DEBUG "irnet: <-%s()" format, __FUNCTION__ , ##args);}
348 
349 /* Exit a function with debug */
350 #define DRETURN(ret, dbg, args...) \
351 	{DEXIT(dbg, ": " args);\
352 	return(ret); }
353 
354 /* Exit a function on failed condition */
355 #define DABORT(cond, ret, dbg, args...) \
356 	{if(cond) {\
357 		DERROR(dbg, args);\
358 		return(ret); }}
359 
360 /* Invalid assertion, print out an error and exit... */
361 #define DASSERT(cond, ret, dbg, args...) \
362 	{if((DEBUG_ASSERT) && !(cond)) {\
363 		DERROR(dbg, "Invalid assertion: " args);\
364 		return ret; }}
365 
366 /************************ CONSTANTS & MACROS ************************/
367 
368 /* Paranoia */
369 #define IRNET_MAGIC	0xB00754
370 
371 /* Number of control events in the control channel buffer... */
372 #define IRNET_MAX_EVENTS	8	/* Should be more than enough... */
373 
374 /****************************** TYPES ******************************/
375 
376 /*
377  * This is the main structure where we store all the data pertaining to
378  * one instance of irnet.
379  * Note : in irnet functions, a pointer this structure is usually called
380  * "ap" or "self". If the code is borrowed from the IrDA stack, it tend
381  * to be called "self", and if it is borrowed from the PPP driver it is
382  * "ap". Apart from that, it's exactly the same structure ;-)
383  */
384 typedef struct irnet_socket
385 {
386   /* ------------------- Instance management ------------------- */
387   /* We manage a linked list of IrNET socket instances */
388   irda_queue_t		q;		/* Must be first - for hasbin */
389   int			magic;		/* Paranoia */
390 
391   /* --------------------- FileSystem part --------------------- */
392   /* "pppd" interact directly with us on a /dev/ file */
393   struct file *		file;		/* File descriptor of this instance */
394   /* TTY stuff - to keep "pppd" happy */
395   struct termios	termios;	/* Various tty flags */
396   /* Stuff for the control channel */
397   int			event_index;	/* Last read in the event log */
398 
399   /* ------------------------- PPP part ------------------------- */
400   /* We interface directly to the ppp_generic driver in the kernel */
401   int			ppp_open;	/* registered with ppp_generic */
402   struct ppp_channel	chan;		/* Interface to generic ppp layer */
403 
404   int			mru;		/* Max size of PPP payload */
405   u32			xaccm[8];	/* Asynchronous character map (just */
406   u32			raccm;		/* to please pppd - dummy) */
407   unsigned int		flags;		/* PPP flags (compression, ...) */
408   unsigned int		rbits;		/* Unused receive flags ??? */
409 
410   /* ------------------------ IrTTP part ------------------------ */
411   /* We create a pseudo "socket" over the IrDA tranport */
412   int			ttp_open;	/* Set when IrTTP is ready */
413   int			ttp_connect;	/* Set when IrTTP is connecting */
414   struct tsap_cb *	tsap;		/* IrTTP instance (the connection) */
415 
416   char			rname[NICKNAME_MAX_LEN + 1];
417 					/* IrDA nickname of destination */
418   __u32			rdaddr;		/* Requested peer IrDA address */
419   __u32			rsaddr;		/* Requested local IrDA address */
420   __u32			daddr;		/* actual peer IrDA address */
421   __u32			saddr;		/* my local IrDA address */
422   __u8			dtsap_sel;	/* Remote TSAP selector */
423   __u8			stsap_sel;	/* Local TSAP selector */
424 
425   __u32			max_sdu_size_rx;/* Socket parameters used for IrTTP */
426   __u32			max_sdu_size_tx;
427   __u32			max_data_size;
428   __u8			max_header_size;
429   LOCAL_FLOW		tx_flow;	/* State of the Tx path in IrTTP */
430 
431   /* ------------------- IrLMP and IrIAS part ------------------- */
432   /* Used for IrDA Discovery and socket name resolution */
433   __u32			ckey;		/* IrLMP client handle */
434   __u16			mask;		/* Hint bits mask (filter discov.)*/
435   int			nslots;		/* Number of slots for discovery */
436 
437   struct iriap_cb *	iriap;		/* Used to query remote IAS */
438   int			errno;		/* status of the IAS query */
439 
440   /* -------------------- Discovery log part -------------------- */
441   /* Used by initial discovery on the control channel
442    * and by irnet_discover_daddr_and_lsap_sel() */
443   struct irda_device_info *discoveries;	/* Copy of the discovery log */
444   int			disco_index;	/* Last read in the discovery log */
445   int			disco_number;	/* Size of the discovery log */
446 
447 } irnet_socket;
448 
449 /*
450  * This is the various event that we will generate on the control channel
451  */
452 typedef enum irnet_event
453 {
454   IRNET_DISCOVER,		/* New IrNET node discovered */
455   IRNET_EXPIRE,			/* IrNET node expired */
456   IRNET_CONNECT_TO,		/* IrNET socket has connected to other node */
457   IRNET_CONNECT_FROM,		/* Other node has connected to IrNET socket */
458   IRNET_REQUEST_FROM,		/* Non satisfied connection request */
459   IRNET_NOANSWER_FROM,		/* Failed connection request */
460   IRNET_BLOCKED_LINK,		/* Link (IrLAP) is blocked for > 3s */
461   IRNET_DISCONNECT_FROM,	/* IrNET socket has disconnected */
462   IRNET_DISCONNECT_TO		/* Closing IrNET socket */
463 } irnet_event;
464 
465 /*
466  * This is the storage for an event and its arguments
467  */
468 typedef struct irnet_log
469 {
470   irnet_event	event;
471   int		unit;
472   __u32		saddr;
473   __u32		daddr;
474   char		name[NICKNAME_MAX_LEN + 1];	/* 21 + 1 */
475 } irnet_log;
476 
477 /*
478  * This is the storage for all events and related stuff...
479  */
480 typedef struct irnet_ctrl_channel
481 {
482   irnet_log	log[IRNET_MAX_EVENTS];	/* Event log */
483   int		index;		/* Current index in log */
484   spinlock_t	spinlock;	/* Serialize access to the event log */
485   wait_queue_head_t	rwait;	/* processes blocked on read (or poll) */
486 } irnet_ctrl_channel;
487 
488 /**************************** PROTOTYPES ****************************/
489 /*
490  * Global functions of the IrNET module
491  * Note : we list here also functions called from one file to the other.
492  */
493 
494 /* -------------------------- IRDA PART -------------------------- */
495 extern int
496 	irda_irnet_create(irnet_socket *);	/* Initialise a IrNET socket */
497 extern int
498 	irda_irnet_connect(irnet_socket *);	/* Try to connect over IrDA */
499 extern void
500 	irda_irnet_destroy(irnet_socket *);	/* Teardown  a IrNET socket */
501 extern int
502 	irda_irnet_init(void);		/* Initialise IrDA part of IrNET */
503 extern void
504 	irda_irnet_cleanup(void);	/* Teardown IrDA part of IrNET */
505 /* --------------------------- PPP PART --------------------------- */
506 extern int
507 	ppp_irnet_init(void);		/* Initialise PPP part of IrNET */
508 extern void
509 	ppp_irnet_cleanup(void);	/* Teardown PPP part of IrNET */
510 /* ---------------------------- MODULE ---------------------------- */
511 extern int
512 	init_module(void);		/* Initialise IrNET module */
513 extern void
514 	cleanup_module(void);		/* Teardown IrNET module  */
515 
516 /**************************** VARIABLES ****************************/
517 
518 /* Control channel stuff - allocated in irnet_irda.h */
519 extern struct irnet_ctrl_channel	irnet_events;
520 
521 #endif /* IRNET_H */
522