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 colleagues 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 resources. The alternative
119 * is caching incoming socket, and that's also quite messy and waste
120 * resources.
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 * v13 - 30.5.02 - Jean II
227 *	o Update module init code
228 *
229 * v14 - 20.2.03 - Jean II
230 *	o Add discovery hint bits in the control channel.
231 *	o Remove obsolete MOD_INC/DEC_USE_COUNT in favor of .owner
232 *
233 * v15 - 7.4.03 - Jean II
234 *	o Replace spin_lock_irqsave() with spin_lock_bh() so that we can
235 *	  use ppp_unit_number(). It's probably also better overall...
236 *	o Disable call to ppp_unregister_channel(), because we can't do it.
237 */
238
239/***************************** INCLUDES *****************************/
240
241#include <linux/module.h>
242
243#include <linux/kernel.h>
244#include <linux/skbuff.h>
245#include <linux/tty.h>
246#include <linux/proc_fs.h>
247#include <linux/netdevice.h>
248#include <linux/miscdevice.h>
249#include <linux/poll.h>
250#include <linux/capability.h>
251#include <linux/ctype.h>	/* isspace() */
252#include <linux/string.h>	/* skip_spaces() */
253#include <asm/uaccess.h>
254#include <linux/init.h>
255
256#include <linux/ppp_defs.h>
257#include <linux/ppp-ioctl.h>
258#include <linux/ppp_channel.h>
259
260#include <net/irda/irda.h>
261#include <net/irda/iriap.h>
262#include <net/irda/irias_object.h>
263#include <net/irda/irlmp.h>
264#include <net/irda/irttp.h>
265#include <net/irda/discovery.h>
266
267/***************************** OPTIONS *****************************/
268/*
269 * Define or undefine to compile or not some optional part of the
270 * IrNET driver...
271 * Note : the present defaults make sense, play with that at your
272 * own risk...
273 */
274/* IrDA side of the business... */
275#define DISCOVERY_NOMASK	/* To enable W2k compatibility... */
276#define ADVERTISE_HINT		/* Advertise IrLAN hint bit */
277#define ALLOW_SIMULT_CONNECT	/* This seem to work, cross fingers... */
278#define DISCOVERY_EVENTS	/* Query the discovery log to post events */
279#define INITIAL_DISCOVERY	/* Dump current discovery log as events */
280#undef STREAM_COMPAT		/* Not needed - potentially messy */
281#undef CONNECT_INDIC_KICK	/* Might mess IrDA, not needed */
282#undef FAIL_SEND_DISCONNECT	/* Might mess IrDA, not needed */
283#undef PASS_CONNECT_PACKETS	/* Not needed ? Safe */
284#undef MISSING_PPP_API		/* Stuff I wish I could do */
285
286/* PPP side of the business */
287#define BLOCK_WHEN_CONNECT	/* Block packets when connecting */
288#define CONNECT_IN_SEND		/* Retry IrDA connection procedure */
289#undef FLUSH_TO_PPP		/* Not sure about this one, let's play safe */
290#undef SECURE_DEVIRNET		/* Bah... */
291
292/****************************** DEBUG ******************************/
293
294/*
295 * This set of flags enable and disable all the various warning,
296 * error and debug message of this driver.
297 * Each section can be enabled and disabled independently
298 */
299/* In the PPP part */
300#define DEBUG_CTRL_TRACE	0	/* Control channel */
301#define DEBUG_CTRL_INFO		0	/* various info */
302#define DEBUG_CTRL_ERROR	1	/* problems */
303#define DEBUG_FS_TRACE		0	/* filesystem callbacks */
304#define DEBUG_FS_INFO		0	/* various info */
305#define DEBUG_FS_ERROR		1	/* problems */
306#define DEBUG_PPP_TRACE		0	/* PPP related functions */
307#define DEBUG_PPP_INFO		0	/* various info */
308#define DEBUG_PPP_ERROR		1	/* problems */
309#define DEBUG_MODULE_TRACE	0	/* module insertion/removal */
310#define DEBUG_MODULE_ERROR	1	/* problems */
311
312/* In the IrDA part */
313#define DEBUG_IRDA_SR_TRACE	0	/* IRDA subroutines */
314#define DEBUG_IRDA_SR_INFO	0	/* various info */
315#define DEBUG_IRDA_SR_ERROR	1	/* problems */
316#define DEBUG_IRDA_SOCK_TRACE	0	/* IRDA main socket functions */
317#define DEBUG_IRDA_SOCK_INFO	0	/* various info */
318#define DEBUG_IRDA_SOCK_ERROR	1	/* problems */
319#define DEBUG_IRDA_SERV_TRACE	0	/* The IrNET server */
320#define DEBUG_IRDA_SERV_INFO	0	/* various info */
321#define DEBUG_IRDA_SERV_ERROR	1	/* problems */
322#define DEBUG_IRDA_TCB_TRACE	0	/* IRDA IrTTP callbacks */
323#define DEBUG_IRDA_CB_INFO	0	/* various info */
324#define DEBUG_IRDA_CB_ERROR	1	/* problems */
325#define DEBUG_IRDA_OCB_TRACE	0	/* IRDA other callbacks */
326#define DEBUG_IRDA_OCB_INFO	0	/* various info */
327#define DEBUG_IRDA_OCB_ERROR	1	/* problems */
328
329#define DEBUG_ASSERT		0	/* Verify all assertions */
330
331/*
332 * These are the macros we are using to actually print the debug
333 * statements. Don't look at it, it's ugly...
334 *
335 * One of the trick is that, as the DEBUG_XXX are constant, the
336 * compiler will optimise away the if() in all cases.
337 */
338/* All error messages (will show up in the normal logs) */
339#define DERROR(dbg, format, args...) \
340	{if(DEBUG_##dbg) \
341		printk(KERN_INFO "irnet: %s(): " format, __func__ , ##args);}
342
343/* Normal debug message (will show up in /var/log/debug) */
344#define DEBUG(dbg, format, args...) \
345	{if(DEBUG_##dbg) \
346		printk(KERN_DEBUG "irnet: %s(): " format, __func__ , ##args);}
347
348/* Entering a function (trace) */
349#define DENTER(dbg, format, args...) \
350	{if(DEBUG_##dbg) \
351		printk(KERN_DEBUG "irnet: -> %s" format, __func__ , ##args);}
352
353/* Entering and exiting a function in one go (trace) */
354#define DPASS(dbg, format, args...) \
355	{if(DEBUG_##dbg) \
356		printk(KERN_DEBUG "irnet: <>%s" format, __func__ , ##args);}
357
358/* Exiting a function (trace) */
359#define DEXIT(dbg, format, args...) \
360	{if(DEBUG_##dbg) \
361		printk(KERN_DEBUG "irnet: <-%s()" format, __func__ , ##args);}
362
363/* Exit a function with debug */
364#define DRETURN(ret, dbg, args...) \
365	{DEXIT(dbg, ": " args);\
366	return ret; }
367
368/* Exit a function on failed condition */
369#define DABORT(cond, ret, dbg, args...) \
370	{if(cond) {\
371		DERROR(dbg, args);\
372		return ret; }}
373
374/* Invalid assertion, print out an error and exit... */
375#define DASSERT(cond, ret, dbg, args...) \
376	{if((DEBUG_ASSERT) && !(cond)) {\
377		DERROR(dbg, "Invalid assertion: " args);\
378		return ret; }}
379
380/************************ CONSTANTS & MACROS ************************/
381
382/* Paranoia */
383#define IRNET_MAGIC	0xB00754
384
385/* Number of control events in the control channel buffer... */
386#define IRNET_MAX_EVENTS	8	/* Should be more than enough... */
387
388/****************************** TYPES ******************************/
389
390/*
391 * This is the main structure where we store all the data pertaining to
392 * one instance of irnet.
393 * Note : in irnet functions, a pointer this structure is usually called
394 * "ap" or "self". If the code is borrowed from the IrDA stack, it tend
395 * to be called "self", and if it is borrowed from the PPP driver it is
396 * "ap". Apart from that, it's exactly the same structure ;-)
397 */
398typedef struct irnet_socket
399{
400  /* ------------------- Instance management ------------------- */
401  /* We manage a linked list of IrNET socket instances */
402  irda_queue_t		q;		/* Must be first - for hasbin */
403  int			magic;		/* Paranoia */
404
405  /* --------------------- FileSystem part --------------------- */
406  /* "pppd" interact directly with us on a /dev/ file */
407  struct file *		file;		/* File descriptor of this instance */
408  /* TTY stuff - to keep "pppd" happy */
409  struct ktermios	termios;	/* Various tty flags */
410  /* Stuff for the control channel */
411  int			event_index;	/* Last read in the event log */
412
413  /* ------------------------- PPP part ------------------------- */
414  /* We interface directly to the ppp_generic driver in the kernel */
415  int			ppp_open;	/* registered with ppp_generic */
416  struct ppp_channel	chan;		/* Interface to generic ppp layer */
417
418  int			mru;		/* Max size of PPP payload */
419  u32			xaccm[8];	/* Asynchronous character map (just */
420  u32			raccm;		/* to please pppd - dummy) */
421  unsigned int		flags;		/* PPP flags (compression, ...) */
422  unsigned int		rbits;		/* Unused receive flags ??? */
423  struct work_struct disconnect_work;   /* Process context disconnection */
424  /* ------------------------ IrTTP part ------------------------ */
425  /* We create a pseudo "socket" over the IrDA tranport */
426  unsigned long		ttp_open;	/* Set when IrTTP is ready */
427  unsigned long		ttp_connect;	/* Set when IrTTP is connecting */
428  struct tsap_cb *	tsap;		/* IrTTP instance (the connection) */
429
430  char			rname[NICKNAME_MAX_LEN + 1];
431					/* IrDA nickname of destination */
432  __u32			rdaddr;		/* Requested peer IrDA address */
433  __u32			rsaddr;		/* Requested local IrDA address */
434  __u32			daddr;		/* actual peer IrDA address */
435  __u32			saddr;		/* my local IrDA address */
436  __u8			dtsap_sel;	/* Remote TSAP selector */
437  __u8			stsap_sel;	/* Local TSAP selector */
438
439  __u32			max_sdu_size_rx;/* Socket parameters used for IrTTP */
440  __u32			max_sdu_size_tx;
441  __u32			max_data_size;
442  __u8			max_header_size;
443  LOCAL_FLOW		tx_flow;	/* State of the Tx path in IrTTP */
444
445  /* ------------------- IrLMP and IrIAS part ------------------- */
446  /* Used for IrDA Discovery and socket name resolution */
447  void *		ckey;		/* IrLMP client handle */
448  __u16			mask;		/* Hint bits mask (filter discov.)*/
449  int			nslots;		/* Number of slots for discovery */
450
451  struct iriap_cb *	iriap;		/* Used to query remote IAS */
452  int			errno;		/* status of the IAS query */
453
454  /* -------------------- Discovery log part -------------------- */
455  /* Used by initial discovery on the control channel
456   * and by irnet_discover_daddr_and_lsap_sel() */
457  struct irda_device_info *discoveries;	/* Copy of the discovery log */
458  int			disco_index;	/* Last read in the discovery log */
459  int			disco_number;	/* Size of the discovery log */
460
461  struct mutex		lock;
462
463} irnet_socket;
464
465/*
466 * This is the various event that we will generate on the control channel
467 */
468typedef enum irnet_event
469{
470  IRNET_DISCOVER,		/* New IrNET node discovered */
471  IRNET_EXPIRE,			/* IrNET node expired */
472  IRNET_CONNECT_TO,		/* IrNET socket has connected to other node */
473  IRNET_CONNECT_FROM,		/* Other node has connected to IrNET socket */
474  IRNET_REQUEST_FROM,		/* Non satisfied connection request */
475  IRNET_NOANSWER_FROM,		/* Failed connection request */
476  IRNET_BLOCKED_LINK,		/* Link (IrLAP) is blocked for > 3s */
477  IRNET_DISCONNECT_FROM,	/* IrNET socket has disconnected */
478  IRNET_DISCONNECT_TO		/* Closing IrNET socket */
479} irnet_event;
480
481/*
482 * This is the storage for an event and its arguments
483 */
484typedef struct irnet_log
485{
486  irnet_event	event;
487  int		unit;
488  __u32		saddr;
489  __u32		daddr;
490  char		name[NICKNAME_MAX_LEN + 1];	/* 21 + 1 */
491  __u16_host_order hints;			/* Discovery hint bits */
492} irnet_log;
493
494/*
495 * This is the storage for all events and related stuff...
496 */
497typedef struct irnet_ctrl_channel
498{
499  irnet_log	log[IRNET_MAX_EVENTS];	/* Event log */
500  int		index;		/* Current index in log */
501  spinlock_t	spinlock;	/* Serialize access to the event log */
502  wait_queue_head_t	rwait;	/* processes blocked on read (or poll) */
503} irnet_ctrl_channel;
504
505/**************************** PROTOTYPES ****************************/
506/*
507 * Global functions of the IrNET module
508 * Note : we list here also functions called from one file to the other.
509 */
510
511/* -------------------------- IRDA PART -------------------------- */
512int irda_irnet_create(irnet_socket *);	/* Initialise an IrNET socket */
513int irda_irnet_connect(irnet_socket *);	/* Try to connect over IrDA */
514void irda_irnet_destroy(irnet_socket *);	/* Teardown an IrNET socket */
515int irda_irnet_init(void);		/* Initialise IrDA part of IrNET */
516void irda_irnet_cleanup(void);		/* Teardown IrDA part of IrNET */
517
518/**************************** VARIABLES ****************************/
519
520/* Control channel stuff - allocated in irnet_irda.h */
521extern struct irnet_ctrl_channel	irnet_events;
522
523#endif /* IRNET_H */
524