/*1* IrNET protocol module : Synchronous PPP over an IrDA socket.2*3* Jean II - HPL `00 - <[email protected]>4*5* This file contains definitions and declarations global to the IrNET module,6* all grouped in one place...7* This file is a *private* header, so other modules don't want to know8* what's in there...9*10* Note : as most part of the Linux kernel, this module is available11* under the GNU General Public License (GPL).12*/1314#ifndef IRNET_H15#define IRNET_H1617/************************** DOCUMENTATION ***************************/18/*19* What is IrNET20* -------------21* IrNET is a protocol allowing to carry TCP/IP traffic between two22* IrDA peers in an efficient fashion. It is a thin layer, passing PPP23* packets to IrTTP and vice versa. It uses PPP in synchronous mode,24* because IrTTP offer a reliable sequenced packet service (as opposed25* to a byte stream). In fact, you could see IrNET as carrying TCP/IP26* in a IrDA socket, using PPP to provide the glue.27*28* The main difference with traditional PPP over IrCOMM is that we29* avoid the framing and serial emulation which are a performance30* bottleneck. It also allows multipoint communications in a sensible31* fashion.32*33* The main difference with IrLAN is that we use PPP for the link34* management, which is more standard, interoperable and flexible than35* the IrLAN protocol. For example, PPP adds authentication,36* encryption, compression, header compression and automated routing37* setup. And, as IrNET let PPP do the hard work, the implementation38* is much simpler than IrLAN.39*40* The Linux implementation41* ------------------------42* IrNET is written on top of the Linux-IrDA stack, and interface with43* the generic Linux PPP driver. Because IrNET depend on recent44* changes of the PPP driver interface, IrNET will work only with very45* recent kernel (2.3.99-pre6 and up).46*47* The present implementation offer the following features :48* o simple user interface using pppd49* o efficient implementation (interface directly to PPP and IrTTP)50* o addressing (you can specify the name of the IrNET recipient)51* o multipoint operation (limited by IrLAP specification)52* o information in /proc/net/irda/irnet53* o IrNET events on /dev/irnet (for user space daemon)54* o IrNET daemon (irnetd) to automatically handle incoming requests55* o Windows 2000 compatibility (tested, but need more work)56* Currently missing :57* o Lot's of testing (that's your job)58* o Connection retries (may be too hard to do)59* o Check pppd persist mode60* o User space daemon (to automatically handle incoming requests)61*62* The setup is not currently the most easy, but this should get much63* better when everything will get integrated...64*65* Acknowledgements66* ----------------67* This module is based on :68* o The PPP driver (ppp_synctty/ppp_generic) by Paul Mackerras69* o The IrLAN protocol (irlan_common/XXX) by Dag Brattli70* o The IrSock interface (af_irda) by Dag Brattli71* o Some other bits from the kernel and my drivers...72* Infinite thanks to those brave souls for providing the infrastructure73* upon which IrNET is built.74*75* Thanks to all my colleagues in HP for helping me. In particular,76* thanks to Salil Pradhan and Bill Serra for W2k testing...77* Thanks to Luiz Magalhaes for irnetd and much testing...78*79* Thanks to Alan Cox for answering lot's of my stupid questions, and80* to Paul Mackerras answering my questions on how to best integrate81* IrNET and pppd.82*83* Jean II84*85* Note on some implementations choices...86* ------------------------------------87* 1) Direct interface vs tty/socket88* I could have used a tty interface to hook to ppp and use the full89* socket API to connect to IrDA. The code would have been easier to90* maintain, and maybe the code would have been smaller...91* Instead, we hook directly to ppp_generic and to IrTTP, which make92* things more complicated...93*94* The first reason is flexibility : this allow us to create IrNET95* instances on demand (no /dev/ircommX crap) and to allow linkname96* specification on pppd command line...97*98* Second reason is speed optimisation. If you look closely at the99* transmit and receive paths, you will notice that they are "super lean"100* (that's why they look ugly), with no function calls and as little data101* copy and modification as I could...102*103* 2) irnetd in user space104* irnetd is implemented in user space, which is necessary to call pppd.105* This also give maximum benefits in term of flexibility and customability,106* and allow to offer the event channel, useful for other stuff like debug.107*108* On the other hand, this require a loose coordination between the109* present module and irnetd. One critical area is how incoming request110* are handled.111* When irnet receive an incoming request, it send an event to irnetd and112* drop the incoming IrNET socket.113* irnetd start a pppd instance, which create a new IrNET socket. This new114* socket is then connected in the originating node to the pppd instance.115* At this point, in the originating node, the first socket is closed.116*117* I admit, this is a bit messy and waste some resources. The alternative118* is caching incoming socket, and that's also quite messy and waste119* resources.120* We also make connection time slower. For example, on a 115 kb/s link it121* adds 60ms to the connection time (770 ms). However, this is slower than122* the time it takes to fire up pppd on my P133...123*124*125* History :126* -------127*128* v1 - 15.5.00 - Jean II129* o Basic IrNET (hook to ppp_generic & IrTTP - incl. multipoint)130* o control channel on /dev/irnet (set name/address)131* o event channel on /dev/irnet (for user space daemon)132*133* v2 - 5.6.00 - Jean II134* o Enable DROP_NOT_READY to avoid PPP timeouts & other weirdness...135* o Add DISCONNECT_TO event and rename DISCONNECT_FROM.136* o Set official device number alloaction on /dev/irnet137*138* v3 - 30.8.00 - Jean II139* o Update to latest Linux-IrDA changes :140* - queue_t => irda_queue_t141* o Update to ppp-2.4.0 :142* - move irda_irnet_connect from PPPIOCATTACH to TIOCSETD143* o Add EXPIRE event (depend on new IrDA-Linux patch)144* o Switch from `hashbin_remove' to `hashbin_remove_this' to fix145* a multilink bug... (depend on new IrDA-Linux patch)146* o fix a self->daddr to self->raddr in irda_irnet_connect to fix147* another multilink bug (darn !)148* o Remove LINKNAME_IOCTL cruft149*150* v3b - 31.8.00 - Jean II151* o Dump discovery log at event channel startup152*153* v4 - 28.9.00 - Jean II154* o Fix interaction between poll/select and dump discovery log155* o Add IRNET_BLOCKED_LINK event (depend on new IrDA-Linux patch)156* o Add IRNET_NOANSWER_FROM event (mostly to help support)157* o Release flow control in disconnect_indication158* o Block packets while connecting (speed up connections)159*160* v5 - 11.01.01 - Jean II161* o Init self->max_header_size, just in case...162* o Set up ap->chan.hdrlen, to get zero copy on tx side working.163* o avoid tx->ttp->flow->ppp->tx->... loop, by checking flow state164* Thanks to Christian Gennerat for finding this bug !165* ---166* o Declare the proper MTU/MRU that we can support167* (but PPP doesn't read the MTU value :-()168* o Declare hashbin HB_NOLOCK instead of HB_LOCAL to avoid169* disabling and enabling irq twice170*171* v6 - 31.05.01 - Jean II172* o Print source address in Found, Discovery, Expiry & Request events173* o Print requested source address in /proc/net/irnet174* o Change control channel input. Allow multiple commands in one line.175* o Add saddr command to change ap->rsaddr (and use that in IrDA)176* ---177* o Make the IrDA connection procedure totally asynchronous.178* Heavy rewrite of the IAS query code and the whole connection179* procedure. Now, irnet_connect() no longer need to be called from180* a process context...181* o Enable IrDA connect retries in ppp_irnet_send(). The good thing182* is that IrDA connect retries are directly driven by PPP LCP183* retries (we retry for each LCP packet), so that everything184* is transparently controlled from pppd lcp-max-configure.185* o Add ttp_connect flag to prevent rentry on the connect procedure186* o Test and fixups to eliminate side effects of retries187*188* v7 - 22.08.01 - Jean II189* o Cleanup : Change "saddr = 0x0" to "saddr = DEV_ADDR_ANY"190* o Fix bug in BLOCK_WHEN_CONNECT introduced in v6 : due to the191* asynchronous IAS query, self->tsap is NULL when PPP send the192* first packet. This was preventing "connect-delay 0" to work.193* Change the test in ppp_irnet_send() to self->ttp_connect.194*195* v8 - 1.11.01 - Jean II196* o Tighten the use of self->ttp_connect and self->ttp_open to197* prevent various race conditions.198* o Avoid leaking discovery log and skb199* o Replace "self" with "server" in irnet_connect_indication() to200* better detect cut'n'paste error ;-)201*202* v9 - 29.11.01 - Jean II203* o Fix event generation in disconnect indication that I broke in v8204* It was always generation "No-Answer" because I was testing ttp_open205* just after clearing it. *blush*.206* o Use newly created irttp_listen() to fix potential crash when LAP207* destroyed before irnet module removed.208*209* v10 - 4.3.2 - Jean II210* o When receiving a disconnect indication, don't reenable the211* PPP Tx queue, this will trigger a reconnect. Instead, close212* the channel, which will kill pppd...213*214* v11 - 20.3.02 - Jean II215* o Oops ! v10 fix disabled IrNET retries and passive behaviour.216* Better fix in irnet_disconnect_indication() :217* - if connected, kill pppd via hangup.218* - if not connected, reenable ppp Tx, which trigger IrNET retry.219*220* v12 - 10.4.02 - Jean II221* o Fix race condition in irnet_connect_indication().222* If the socket was already trying to connect, drop old connection223* and use new one only if acting as primary. See comments.224*225* v13 - 30.5.02 - Jean II226* o Update module init code227*228* v14 - 20.2.03 - Jean II229* o Add discovery hint bits in the control channel.230* o Remove obsolete MOD_INC/DEC_USE_COUNT in favor of .owner231*232* v15 - 7.4.03 - Jean II233* o Replace spin_lock_irqsave() with spin_lock_bh() so that we can234* use ppp_unit_number(). It's probably also better overall...235* o Disable call to ppp_unregister_channel(), because we can't do it.236*/237238/***************************** INCLUDES *****************************/239240#include <linux/module.h>241242#include <linux/kernel.h>243#include <linux/skbuff.h>244#include <linux/tty.h>245#include <linux/proc_fs.h>246#include <linux/netdevice.h>247#include <linux/miscdevice.h>248#include <linux/poll.h>249#include <linux/capability.h>250#include <linux/ctype.h> /* isspace() */251#include <linux/string.h> /* skip_spaces() */252#include <asm/uaccess.h>253#include <linux/init.h>254255#include <linux/ppp_defs.h>256#include <linux/if_ppp.h>257#include <linux/ppp_channel.h>258259#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>265266/***************************** OPTIONS *****************************/267/*268* Define or undefine to compile or not some optional part of the269* IrNET driver...270* Note : the present defaults make sense, play with that at your271* own risk...272*/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 */284285/* 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... */290291/****************************** DEBUG ******************************/292293/*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 independently297*/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 */310311/* 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 */327328#define DEBUG_ASSERT 0 /* Verify all assertions */329330/*331* These are the macros we are using to actually print the debug332* statements. Don't look at it, it's ugly...333*334* One of the trick is that, as the DEBUG_XXX are constant, the335* compiler will optimise away the if() in all cases.336*/337/* All error messages (will show up in the normal logs) */338#define DERROR(dbg, format, args...) \339{if(DEBUG_##dbg) \340printk(KERN_INFO "irnet: %s(): " format, __func__ , ##args);}341342/* Normal debug message (will show up in /var/log/debug) */343#define DEBUG(dbg, format, args...) \344{if(DEBUG_##dbg) \345printk(KERN_DEBUG "irnet: %s(): " format, __func__ , ##args);}346347/* Entering a function (trace) */348#define DENTER(dbg, format, args...) \349{if(DEBUG_##dbg) \350printk(KERN_DEBUG "irnet: -> %s" format, __func__ , ##args);}351352/* Entering and exiting a function in one go (trace) */353#define DPASS(dbg, format, args...) \354{if(DEBUG_##dbg) \355printk(KERN_DEBUG "irnet: <>%s" format, __func__ , ##args);}356357/* Exiting a function (trace) */358#define DEXIT(dbg, format, args...) \359{if(DEBUG_##dbg) \360printk(KERN_DEBUG "irnet: <-%s()" format, __func__ , ##args);}361362/* Exit a function with debug */363#define DRETURN(ret, dbg, args...) \364{DEXIT(dbg, ": " args);\365return ret; }366367/* Exit a function on failed condition */368#define DABORT(cond, ret, dbg, args...) \369{if(cond) {\370DERROR(dbg, args);\371return ret; }}372373/* Invalid assertion, print out an error and exit... */374#define DASSERT(cond, ret, dbg, args...) \375{if((DEBUG_ASSERT) && !(cond)) {\376DERROR(dbg, "Invalid assertion: " args);\377return ret; }}378379/************************ CONSTANTS & MACROS ************************/380381/* Paranoia */382#define IRNET_MAGIC 0xB00754383384/* Number of control events in the control channel buffer... */385#define IRNET_MAX_EVENTS 8 /* Should be more than enough... */386387/****************************** TYPES ******************************/388389/*390* This is the main structure where we store all the data pertaining to391* one instance of irnet.392* Note : in irnet functions, a pointer this structure is usually called393* "ap" or "self". If the code is borrowed from the IrDA stack, it tend394* to be called "self", and if it is borrowed from the PPP driver it is395* "ap". Apart from that, it's exactly the same structure ;-)396*/397typedef struct irnet_socket398{399/* ------------------- Instance management ------------------- */400/* We manage a linked list of IrNET socket instances */401irda_queue_t q; /* Must be first - for hasbin */402int magic; /* Paranoia */403404/* --------------------- FileSystem part --------------------- */405/* "pppd" interact directly with us on a /dev/ file */406struct file * file; /* File descriptor of this instance */407/* TTY stuff - to keep "pppd" happy */408struct ktermios termios; /* Various tty flags */409/* Stuff for the control channel */410int event_index; /* Last read in the event log */411412/* ------------------------- PPP part ------------------------- */413/* We interface directly to the ppp_generic driver in the kernel */414int ppp_open; /* registered with ppp_generic */415struct ppp_channel chan; /* Interface to generic ppp layer */416417int mru; /* Max size of PPP payload */418u32 xaccm[8]; /* Asynchronous character map (just */419u32 raccm; /* to please pppd - dummy) */420unsigned int flags; /* PPP flags (compression, ...) */421unsigned int rbits; /* Unused receive flags ??? */422struct work_struct disconnect_work; /* Process context disconnection */423/* ------------------------ IrTTP part ------------------------ */424/* We create a pseudo "socket" over the IrDA tranport */425unsigned long ttp_open; /* Set when IrTTP is ready */426unsigned long ttp_connect; /* Set when IrTTP is connecting */427struct tsap_cb * tsap; /* IrTTP instance (the connection) */428429char 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 */437438__u32 max_sdu_size_rx;/* Socket parameters used for IrTTP */439__u32 max_sdu_size_tx;440__u32 max_data_size;441__u8 max_header_size;442LOCAL_FLOW tx_flow; /* State of the Tx path in IrTTP */443444/* ------------------- IrLMP and IrIAS part ------------------- */445/* Used for IrDA Discovery and socket name resolution */446void * ckey; /* IrLMP client handle */447__u16 mask; /* Hint bits mask (filter discov.)*/448int nslots; /* Number of slots for discovery */449450struct iriap_cb * iriap; /* Used to query remote IAS */451int errno; /* status of the IAS query */452453/* -------------------- Discovery log part -------------------- */454/* Used by initial discovery on the control channel455* and by irnet_discover_daddr_and_lsap_sel() */456struct irda_device_info *discoveries; /* Copy of the discovery log */457int disco_index; /* Last read in the discovery log */458int disco_number; /* Size of the discovery log */459460struct mutex lock;461462} irnet_socket;463464/*465* This is the various event that we will generate on the control channel466*/467typedef enum irnet_event468{469IRNET_DISCOVER, /* New IrNET node discovered */470IRNET_EXPIRE, /* IrNET node expired */471IRNET_CONNECT_TO, /* IrNET socket has connected to other node */472IRNET_CONNECT_FROM, /* Other node has connected to IrNET socket */473IRNET_REQUEST_FROM, /* Non satisfied connection request */474IRNET_NOANSWER_FROM, /* Failed connection request */475IRNET_BLOCKED_LINK, /* Link (IrLAP) is blocked for > 3s */476IRNET_DISCONNECT_FROM, /* IrNET socket has disconnected */477IRNET_DISCONNECT_TO /* Closing IrNET socket */478} irnet_event;479480/*481* This is the storage for an event and its arguments482*/483typedef struct irnet_log484{485irnet_event event;486int unit;487__u32 saddr;488__u32 daddr;489char name[NICKNAME_MAX_LEN + 1]; /* 21 + 1 */490__u16_host_order hints; /* Discovery hint bits */491} irnet_log;492493/*494* This is the storage for all events and related stuff...495*/496typedef struct irnet_ctrl_channel497{498irnet_log log[IRNET_MAX_EVENTS]; /* Event log */499int index; /* Current index in log */500spinlock_t spinlock; /* Serialize access to the event log */501wait_queue_head_t rwait; /* processes blocked on read (or poll) */502} irnet_ctrl_channel;503504/**************************** PROTOTYPES ****************************/505/*506* Global functions of the IrNET module507* Note : we list here also functions called from one file to the other.508*/509510/* -------------------------- IRDA PART -------------------------- */511extern int512irda_irnet_create(irnet_socket *); /* Initialise a IrNET socket */513extern int514irda_irnet_connect(irnet_socket *); /* Try to connect over IrDA */515extern void516irda_irnet_destroy(irnet_socket *); /* Teardown a IrNET socket */517extern int518irda_irnet_init(void); /* Initialise IrDA part of IrNET */519extern void520irda_irnet_cleanup(void); /* Teardown IrDA part of IrNET */521522/**************************** VARIABLES ****************************/523524/* Control channel stuff - allocated in irnet_irda.h */525extern struct irnet_ctrl_channel irnet_events;526527#endif /* IRNET_H */528529530