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