2 * IrNET protocol module : Synchronous PPP over an IrDA socket.
4 * Jean II - HPL `00 - <jt@hpl.hp.com>
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
11 * Note : as most part of the Linux kernel, this module is available
12 * under the GNU General Public License (GPL).
18 /************************** DOCUMENTATION ***************************/
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.
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
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.
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).
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)
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)
63 * The setup is not currently the most easy, but this should get much
64 * better when everything will get integrated...
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.
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...
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
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...
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...
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...
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.
109 * On the other hand, this require a loose coordination between the
110 * present module and irnetd. One critical area is how incoming request
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.
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
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...
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)
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
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
151 * v3b - 31.8.00 - Jean II
152 * o Dump discovery log at event channel startup
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)
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 !
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
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)
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
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.
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 ;-)
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.
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...
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.
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.
226 * v13 - 30.5.02 - Jean II
227 * o Update module init code
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
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.
239 /***************************** INCLUDES *****************************/
241 #include <linux/module.h>
243 #include <linux/kernel.h>
244 #include <linux/skbuff.h>
245 #include <linux/tty.h>
246 #include <linux/proc_fs.h>
247 #include <linux/devfs_fs_kernel.h>
248 #include <linux/netdevice.h>
249 #include <linux/miscdevice.h>
250 #include <linux/poll.h>
251 #include <linux/capability.h>
252 #include <linux/config.h>
253 #include <linux/ctype.h> /* isspace() */
254 #include <asm/uaccess.h>
255 #include <linux/init.h>
257 #include <linux/ppp_defs.h>
258 #include <linux/if_ppp.h>
259 #include <linux/ppp_channel.h>
261 #include <net/irda/irda.h>
262 #include <net/irda/iriap.h>
263 #include <net/irda/irias_object.h>
264 #include <net/irda/irlmp.h>
265 #include <net/irda/irttp.h>
266 #include <net/irda/discovery.h>
268 /***************************** OPTIONS *****************************/
270 * Define or undefine to compile or not some optional part of the
272 * Note : the present defaults make sense, play with that at your
275 /* IrDA side of the business... */
276 #define DISCOVERY_NOMASK /* To enable W2k compatibility... */
277 #define ADVERTISE_HINT /* Advertise IrLAN hint bit */
278 #define ALLOW_SIMULT_CONNECT /* This seem to work, cross fingers... */
279 #define DISCOVERY_EVENTS /* Query the discovery log to post events */
280 #define INITIAL_DISCOVERY /* Dump current discovery log as events */
281 #undef STREAM_COMPAT /* Not needed - potentially messy */
282 #undef CONNECT_INDIC_KICK /* Might mess IrDA, not needed */
283 #undef FAIL_SEND_DISCONNECT /* Might mess IrDA, not needed */
284 #undef PASS_CONNECT_PACKETS /* Not needed ? Safe */
285 #undef MISSING_PPP_API /* Stuff I wish I could do */
287 /* PPP side of the business */
288 #define BLOCK_WHEN_CONNECT /* Block packets when connecting */
289 #define CONNECT_IN_SEND /* Retry IrDA connection procedure */
290 #undef FLUSH_TO_PPP /* Not sure about this one, let's play safe */
291 #undef SECURE_DEVIRNET /* Bah... */
293 /****************************** DEBUG ******************************/
296 * This set of flags enable and disable all the various warning,
297 * error and debug message of this driver.
298 * Each section can be enabled and disabled independently
300 /* In the PPP part */
301 #define DEBUG_CTRL_TRACE 0 /* Control channel */
302 #define DEBUG_CTRL_INFO 0 /* various info */
303 #define DEBUG_CTRL_ERROR 1 /* problems */
304 #define DEBUG_FS_TRACE 0 /* filesystem callbacks */
305 #define DEBUG_FS_INFO 0 /* various info */
306 #define DEBUG_FS_ERROR 1 /* problems */
307 #define DEBUG_PPP_TRACE 0 /* PPP related functions */
308 #define DEBUG_PPP_INFO 0 /* various info */
309 #define DEBUG_PPP_ERROR 1 /* problems */
310 #define DEBUG_MODULE_TRACE 0 /* module insertion/removal */
311 #define DEBUG_MODULE_ERROR 1 /* problems */
313 /* In the IrDA part */
314 #define DEBUG_IRDA_SR_TRACE 0 /* IRDA subroutines */
315 #define DEBUG_IRDA_SR_INFO 0 /* various info */
316 #define DEBUG_IRDA_SR_ERROR 1 /* problems */
317 #define DEBUG_IRDA_SOCK_TRACE 0 /* IRDA main socket functions */
318 #define DEBUG_IRDA_SOCK_INFO 0 /* various info */
319 #define DEBUG_IRDA_SOCK_ERROR 1 /* problems */
320 #define DEBUG_IRDA_SERV_TRACE 0 /* The IrNET server */
321 #define DEBUG_IRDA_SERV_INFO 0 /* various info */
322 #define DEBUG_IRDA_SERV_ERROR 1 /* problems */
323 #define DEBUG_IRDA_TCB_TRACE 0 /* IRDA IrTTP callbacks */
324 #define DEBUG_IRDA_CB_INFO 0 /* various info */
325 #define DEBUG_IRDA_CB_ERROR 1 /* problems */
326 #define DEBUG_IRDA_OCB_TRACE 0 /* IRDA other callbacks */
327 #define DEBUG_IRDA_OCB_INFO 0 /* various info */
328 #define DEBUG_IRDA_OCB_ERROR 1 /* problems */
330 #define DEBUG_ASSERT 0 /* Verify all assertions */
333 * These are the macros we are using to actually print the debug
334 * statements. Don't look at it, it's ugly...
336 * One of the trick is that, as the DEBUG_XXX are constant, the
337 * compiler will optimise away the if() in all cases.
339 /* All error messages (will show up in the normal logs) */
340 #define DERROR(dbg, format, args...) \
342 printk(KERN_INFO "irnet: %s(): " format, __FUNCTION__ , ##args);}
344 /* Normal debug message (will show up in /var/log/debug) */
345 #define DEBUG(dbg, format, args...) \
347 printk(KERN_DEBUG "irnet: %s(): " format, __FUNCTION__ , ##args);}
349 /* Entering a function (trace) */
350 #define DENTER(dbg, format, args...) \
352 printk(KERN_DEBUG "irnet: -> %s" format, __FUNCTION__ , ##args);}
354 /* Entering and exiting a function in one go (trace) */
355 #define DPASS(dbg, format, args...) \
357 printk(KERN_DEBUG "irnet: <>%s" format, __FUNCTION__ , ##args);}
359 /* Exiting a function (trace) */
360 #define DEXIT(dbg, format, args...) \
362 printk(KERN_DEBUG "irnet: <-%s()" format, __FUNCTION__ , ##args);}
364 /* Exit a function with debug */
365 #define DRETURN(ret, dbg, args...) \
366 {DEXIT(dbg, ": " args);\
369 /* Exit a function on failed condition */
370 #define DABORT(cond, ret, dbg, args...) \
375 /* Invalid assertion, print out an error and exit... */
376 #define DASSERT(cond, ret, dbg, args...) \
377 {if((DEBUG_ASSERT) && !(cond)) {\
378 DERROR(dbg, "Invalid assertion: " args);\
381 /************************ CONSTANTS & MACROS ************************/
384 #define IRNET_MAGIC 0xB00754
386 /* Number of control events in the control channel buffer... */
387 #define IRNET_MAX_EVENTS 8 /* Should be more than enough... */
389 /****************************** TYPES ******************************/
392 * This is the main structure where we store all the data pertaining to
393 * one instance of irnet.
394 * Note : in irnet functions, a pointer this structure is usually called
395 * "ap" or "self". If the code is borrowed from the IrDA stack, it tend
396 * to be called "self", and if it is borrowed from the PPP driver it is
397 * "ap". Apart from that, it's exactly the same structure ;-)
399 typedef struct irnet_socket
401 /* ------------------- Instance management ------------------- */
402 /* We manage a linked list of IrNET socket instances */
403 irda_queue_t q
; /* Must be first - for hasbin */
404 int magic
; /* Paranoia */
406 /* --------------------- FileSystem part --------------------- */
407 /* "pppd" interact directly with us on a /dev/ file */
408 struct file
* file
; /* File descriptor of this instance */
409 /* TTY stuff - to keep "pppd" happy */
410 struct termios termios
; /* Various tty flags */
411 /* Stuff for the control channel */
412 int event_index
; /* Last read in the event log */
414 /* ------------------------- PPP part ------------------------- */
415 /* We interface directly to the ppp_generic driver in the kernel */
416 int ppp_open
; /* registered with ppp_generic */
417 struct ppp_channel chan
; /* Interface to generic ppp layer */
419 int mru
; /* Max size of PPP payload */
420 u32 xaccm
[8]; /* Asynchronous character map (just */
421 u32 raccm
; /* to please pppd - dummy) */
422 unsigned int flags
; /* PPP flags (compression, ...) */
423 unsigned int rbits
; /* Unused receive flags ??? */
425 /* ------------------------ IrTTP part ------------------------ */
426 /* We create a pseudo "socket" over the IrDA tranport */
427 unsigned long ttp_open
; /* Set when IrTTP is ready */
428 unsigned long ttp_connect
; /* Set when IrTTP is connecting */
429 struct tsap_cb
* tsap
; /* IrTTP instance (the connection) */
431 char rname
[NICKNAME_MAX_LEN
+ 1];
432 /* IrDA nickname of destination */
433 __u32 rdaddr
; /* Requested peer IrDA address */
434 __u32 rsaddr
; /* Requested local IrDA address */
435 __u32 daddr
; /* actual peer IrDA address */
436 __u32 saddr
; /* my local IrDA address */
437 __u8 dtsap_sel
; /* Remote TSAP selector */
438 __u8 stsap_sel
; /* Local TSAP selector */
440 __u32 max_sdu_size_rx
;/* Socket parameters used for IrTTP */
441 __u32 max_sdu_size_tx
;
443 __u8 max_header_size
;
444 LOCAL_FLOW tx_flow
; /* State of the Tx path in IrTTP */
446 /* ------------------- IrLMP and IrIAS part ------------------- */
447 /* Used for IrDA Discovery and socket name resolution */
448 void * ckey
; /* IrLMP client handle */
449 __u16 mask
; /* Hint bits mask (filter discov.)*/
450 int nslots
; /* Number of slots for discovery */
452 struct iriap_cb
* iriap
; /* Used to query remote IAS */
453 int errno
; /* status of the IAS query */
455 /* -------------------- Discovery log part -------------------- */
456 /* Used by initial discovery on the control channel
457 * and by irnet_discover_daddr_and_lsap_sel() */
458 struct irda_device_info
*discoveries
; /* Copy of the discovery log */
459 int disco_index
; /* Last read in the discovery log */
460 int disco_number
; /* Size of the discovery log */
465 * This is the various event that we will generate on the control channel
467 typedef enum irnet_event
469 IRNET_DISCOVER
, /* New IrNET node discovered */
470 IRNET_EXPIRE
, /* IrNET node expired */
471 IRNET_CONNECT_TO
, /* IrNET socket has connected to other node */
472 IRNET_CONNECT_FROM
, /* Other node has connected to IrNET socket */
473 IRNET_REQUEST_FROM
, /* Non satisfied connection request */
474 IRNET_NOANSWER_FROM
, /* Failed connection request */
475 IRNET_BLOCKED_LINK
, /* Link (IrLAP) is blocked for > 3s */
476 IRNET_DISCONNECT_FROM
, /* IrNET socket has disconnected */
477 IRNET_DISCONNECT_TO
/* Closing IrNET socket */
481 * This is the storage for an event and its arguments
483 typedef struct irnet_log
489 char name
[NICKNAME_MAX_LEN
+ 1]; /* 21 + 1 */
490 __u16_host_order hints
; /* Discovery hint bits */
494 * This is the storage for all events and related stuff...
496 typedef struct irnet_ctrl_channel
498 irnet_log log
[IRNET_MAX_EVENTS
]; /* Event log */
499 int index
; /* Current index in log */
500 spinlock_t spinlock
; /* Serialize access to the event log */
501 wait_queue_head_t rwait
; /* processes blocked on read (or poll) */
502 } irnet_ctrl_channel
;
504 /**************************** PROTOTYPES ****************************/
506 * Global functions of the IrNET module
507 * Note : we list here also functions called from one file to the other.
510 /* -------------------------- IRDA PART -------------------------- */
512 irda_irnet_create(irnet_socket
*); /* Initialise a IrNET socket */
514 irda_irnet_connect(irnet_socket
*); /* Try to connect over IrDA */
516 irda_irnet_destroy(irnet_socket
*); /* Teardown a IrNET socket */
518 irda_irnet_init(void); /* Initialise IrDA part of IrNET */
520 irda_irnet_cleanup(void); /* Teardown IrDA part of IrNET */
522 /**************************** VARIABLES ****************************/
524 /* Control channel stuff - allocated in irnet_irda.h */
525 extern struct irnet_ctrl_channel irnet_events
;