/*1* IrNET protocol module : Synchronous PPP over an IrDA socket.2*3* Jean II - HPL `00 - <[email protected]>4*5* This file implement the PPP interface and /dev/irnet character device.6* The PPP interface hook to the ppp_generic module, handle all our7* relationship to the PPP code in the kernel (and by extension to pppd),8* and exchange PPP frames with this module (send/receive).9* The /dev/irnet device is used primarily for 2 functions :10* 1) as a stub for pppd (the ppp daemon), so that we can appropriately11* generate PPP sessions (we pretend we are a tty).12* 2) as a control channel (write commands, read events)13*/1415#include <linux/sched.h>16#include <linux/slab.h>17#include "irnet_ppp.h" /* Private header */18/* Please put other headers in irnet.h - Thanks */1920/* Generic PPP callbacks (to call us) */21static const struct ppp_channel_ops irnet_ppp_ops = {22.start_xmit = ppp_irnet_send,23.ioctl = ppp_irnet_ioctl24};2526/************************* CONTROL CHANNEL *************************/27/*28* When a pppd instance is not active on /dev/irnet, it acts as a control29* channel.30* Writing allow to set up the IrDA destination of the IrNET channel,31* and any application may be read events happening in IrNET...32*/3334/*------------------------------------------------------------------*/35/*36* Write is used to send a command to configure a IrNET channel37* before it is open by pppd. The syntax is : "command argument"38* Currently there is only two defined commands :39* o name : set the requested IrDA nickname of the IrNET peer.40* o addr : set the requested IrDA address of the IrNET peer.41* Note : the code is crude, but effective...42*/43static inline ssize_t44irnet_ctrl_write(irnet_socket * ap,45const char __user *buf,46size_t count)47{48char command[IRNET_MAX_COMMAND];49char * start; /* Current command being processed */50char * next; /* Next command to process */51int length; /* Length of current command */5253DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);5455/* Check for overflow... */56DABORT(count >= IRNET_MAX_COMMAND, -ENOMEM,57CTRL_ERROR, "Too much data !!!\n");5859/* Get the data in the driver */60if(copy_from_user(command, buf, count))61{62DERROR(CTRL_ERROR, "Invalid user space pointer.\n");63return -EFAULT;64}6566/* Safe terminate the string */67command[count] = '\0';68DEBUG(CTRL_INFO, "Command line received is ``%s'' (%Zd).\n",69command, count);7071/* Check every commands in the command line */72next = command;73while(next != NULL)74{75/* Look at the next command */76start = next;7778/* Scrap whitespaces before the command */79start = skip_spaces(start);8081/* ',' is our command separator */82next = strchr(start, ',');83if(next)84{85*next = '\0'; /* Terminate command */86length = next - start; /* Length */87next++; /* Skip the '\0' */88}89else90length = strlen(start);9192DEBUG(CTRL_INFO, "Found command ``%s'' (%d).\n", start, length);9394/* Check if we recognised one of the known command95* We can't use "switch" with strings, so hack with "continue" */9697/* First command : name -> Requested IrDA nickname */98if(!strncmp(start, "name", 4))99{100/* Copy the name only if is included and not "any" */101if((length > 5) && (strcmp(start + 5, "any")))102{103/* Strip out trailing whitespaces */104while(isspace(start[length - 1]))105length--;106107DABORT(length < 5 || length > NICKNAME_MAX_LEN + 5,108-EINVAL, CTRL_ERROR, "Invalid nickname.\n");109110/* Copy the name for later reuse */111memcpy(ap->rname, start + 5, length - 5);112ap->rname[length - 5] = '\0';113}114else115ap->rname[0] = '\0';116DEBUG(CTRL_INFO, "Got rname = ``%s''\n", ap->rname);117118/* Restart the loop */119continue;120}121122/* Second command : addr, daddr -> Requested IrDA destination address123* Also process : saddr -> Requested IrDA source address */124if((!strncmp(start, "addr", 4)) ||125(!strncmp(start, "daddr", 5)) ||126(!strncmp(start, "saddr", 5)))127{128__u32 addr = DEV_ADDR_ANY;129130/* Copy the address only if is included and not "any" */131if((length > 5) && (strcmp(start + 5, "any")))132{133char * begp = start + 5;134char * endp;135136/* Scrap whitespaces before the command */137begp = skip_spaces(begp);138139/* Convert argument to a number (last arg is the base) */140addr = simple_strtoul(begp, &endp, 16);141/* Has it worked ? (endp should be start + length) */142DABORT(endp <= (start + 5), -EINVAL,143CTRL_ERROR, "Invalid address.\n");144}145/* Which type of address ? */146if(start[0] == 's')147{148/* Save it */149ap->rsaddr = addr;150DEBUG(CTRL_INFO, "Got rsaddr = %08x\n", ap->rsaddr);151}152else153{154/* Save it */155ap->rdaddr = addr;156DEBUG(CTRL_INFO, "Got rdaddr = %08x\n", ap->rdaddr);157}158159/* Restart the loop */160continue;161}162163/* Other possible command : connect N (number of retries) */164165/* No command matched -> Failed... */166DABORT(1, -EINVAL, CTRL_ERROR, "Not a recognised IrNET command.\n");167}168169/* Success : we have parsed all commands successfully */170return count;171}172173#ifdef INITIAL_DISCOVERY174/*------------------------------------------------------------------*/175/*176* Function irnet_get_discovery_log (self)177*178* Query the content on the discovery log if not done179*180* This function query the current content of the discovery log181* at the startup of the event channel and save it in the internal struct.182*/183static void184irnet_get_discovery_log(irnet_socket * ap)185{186__u16 mask = irlmp_service_to_hint(S_LAN);187188/* Ask IrLMP for the current discovery log */189ap->discoveries = irlmp_get_discoveries(&ap->disco_number, mask,190DISCOVERY_DEFAULT_SLOTS);191192/* Check if the we got some results */193if(ap->discoveries == NULL)194ap->disco_number = -1;195196DEBUG(CTRL_INFO, "Got the log (0x%p), size is %d\n",197ap->discoveries, ap->disco_number);198}199200/*------------------------------------------------------------------*/201/*202* Function irnet_read_discovery_log (self, event)203*204* Read the content on the discovery log205*206* This function dump the current content of the discovery log207* at the startup of the event channel.208* Return 1 if wrote an event on the control channel...209*210* State of the ap->disco_XXX variables :211* Socket creation : discoveries = NULL ; disco_index = 0 ; disco_number = 0212* While reading : discoveries = ptr ; disco_index = X ; disco_number = Y213* After reading : discoveries = NULL ; disco_index = Y ; disco_number = -1214*/215static inline int216irnet_read_discovery_log(irnet_socket * ap,217char * event)218{219int done_event = 0;220221DENTER(CTRL_TRACE, "(ap=0x%p, event=0x%p)\n",222ap, event);223224/* Test if we have some work to do or we have already finished */225if(ap->disco_number == -1)226{227DEBUG(CTRL_INFO, "Already done\n");228return 0;229}230231/* Test if it's the first time and therefore we need to get the log */232if(ap->discoveries == NULL)233irnet_get_discovery_log(ap);234235/* Check if we have more item to dump */236if(ap->disco_index < ap->disco_number)237{238/* Write an event */239sprintf(event, "Found %08x (%s) behind %08x {hints %02X-%02X}\n",240ap->discoveries[ap->disco_index].daddr,241ap->discoveries[ap->disco_index].info,242ap->discoveries[ap->disco_index].saddr,243ap->discoveries[ap->disco_index].hints[0],244ap->discoveries[ap->disco_index].hints[1]);245DEBUG(CTRL_INFO, "Writing discovery %d : %s\n",246ap->disco_index, ap->discoveries[ap->disco_index].info);247248/* We have an event */249done_event = 1;250/* Next discovery */251ap->disco_index++;252}253254/* Check if we have done the last item */255if(ap->disco_index >= ap->disco_number)256{257/* No more items : remove the log and signal termination */258DEBUG(CTRL_INFO, "Cleaning up log (0x%p)\n",259ap->discoveries);260if(ap->discoveries != NULL)261{262/* Cleanup our copy of the discovery log */263kfree(ap->discoveries);264ap->discoveries = NULL;265}266ap->disco_number = -1;267}268269return done_event;270}271#endif /* INITIAL_DISCOVERY */272273/*------------------------------------------------------------------*/274/*275* Read is used to get IrNET events276*/277static inline ssize_t278irnet_ctrl_read(irnet_socket * ap,279struct file * file,280char __user * buf,281size_t count)282{283DECLARE_WAITQUEUE(wait, current);284char event[64]; /* Max event is 61 char */285ssize_t ret = 0;286287DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);288289/* Check if we can write an event out in one go */290DABORT(count < sizeof(event), -EOVERFLOW, CTRL_ERROR, "Buffer to small.\n");291292#ifdef INITIAL_DISCOVERY293/* Check if we have read the log */294if(irnet_read_discovery_log(ap, event))295{296/* We have an event !!! Copy it to the user */297if(copy_to_user(buf, event, strlen(event)))298{299DERROR(CTRL_ERROR, "Invalid user space pointer.\n");300return -EFAULT;301}302303DEXIT(CTRL_TRACE, "\n");304return strlen(event);305}306#endif /* INITIAL_DISCOVERY */307308/* Put ourselves on the wait queue to be woken up */309add_wait_queue(&irnet_events.rwait, &wait);310current->state = TASK_INTERRUPTIBLE;311for(;;)312{313/* If there is unread events */314ret = 0;315if(ap->event_index != irnet_events.index)316break;317ret = -EAGAIN;318if(file->f_flags & O_NONBLOCK)319break;320ret = -ERESTARTSYS;321if(signal_pending(current))322break;323/* Yield and wait to be woken up */324schedule();325}326current->state = TASK_RUNNING;327remove_wait_queue(&irnet_events.rwait, &wait);328329/* Did we got it ? */330if(ret != 0)331{332/* No, return the error code */333DEXIT(CTRL_TRACE, " - ret %Zd\n", ret);334return ret;335}336337/* Which event is it ? */338switch(irnet_events.log[ap->event_index].event)339{340case IRNET_DISCOVER:341sprintf(event, "Discovered %08x (%s) behind %08x {hints %02X-%02X}\n",342irnet_events.log[ap->event_index].daddr,343irnet_events.log[ap->event_index].name,344irnet_events.log[ap->event_index].saddr,345irnet_events.log[ap->event_index].hints.byte[0],346irnet_events.log[ap->event_index].hints.byte[1]);347break;348case IRNET_EXPIRE:349sprintf(event, "Expired %08x (%s) behind %08x {hints %02X-%02X}\n",350irnet_events.log[ap->event_index].daddr,351irnet_events.log[ap->event_index].name,352irnet_events.log[ap->event_index].saddr,353irnet_events.log[ap->event_index].hints.byte[0],354irnet_events.log[ap->event_index].hints.byte[1]);355break;356case IRNET_CONNECT_TO:357sprintf(event, "Connected to %08x (%s) on ppp%d\n",358irnet_events.log[ap->event_index].daddr,359irnet_events.log[ap->event_index].name,360irnet_events.log[ap->event_index].unit);361break;362case IRNET_CONNECT_FROM:363sprintf(event, "Connection from %08x (%s) on ppp%d\n",364irnet_events.log[ap->event_index].daddr,365irnet_events.log[ap->event_index].name,366irnet_events.log[ap->event_index].unit);367break;368case IRNET_REQUEST_FROM:369sprintf(event, "Request from %08x (%s) behind %08x\n",370irnet_events.log[ap->event_index].daddr,371irnet_events.log[ap->event_index].name,372irnet_events.log[ap->event_index].saddr);373break;374case IRNET_NOANSWER_FROM:375sprintf(event, "No-answer from %08x (%s) on ppp%d\n",376irnet_events.log[ap->event_index].daddr,377irnet_events.log[ap->event_index].name,378irnet_events.log[ap->event_index].unit);379break;380case IRNET_BLOCKED_LINK:381sprintf(event, "Blocked link with %08x (%s) on ppp%d\n",382irnet_events.log[ap->event_index].daddr,383irnet_events.log[ap->event_index].name,384irnet_events.log[ap->event_index].unit);385break;386case IRNET_DISCONNECT_FROM:387sprintf(event, "Disconnection from %08x (%s) on ppp%d\n",388irnet_events.log[ap->event_index].daddr,389irnet_events.log[ap->event_index].name,390irnet_events.log[ap->event_index].unit);391break;392case IRNET_DISCONNECT_TO:393sprintf(event, "Disconnected to %08x (%s)\n",394irnet_events.log[ap->event_index].daddr,395irnet_events.log[ap->event_index].name);396break;397default:398sprintf(event, "Bug\n");399}400/* Increment our event index */401ap->event_index = (ap->event_index + 1) % IRNET_MAX_EVENTS;402403DEBUG(CTRL_INFO, "Event is :%s", event);404405/* Copy it to the user */406if(copy_to_user(buf, event, strlen(event)))407{408DERROR(CTRL_ERROR, "Invalid user space pointer.\n");409return -EFAULT;410}411412DEXIT(CTRL_TRACE, "\n");413return strlen(event);414}415416/*------------------------------------------------------------------*/417/*418* Poll : called when someone do a select on /dev/irnet.419* Just check if there are new events...420*/421static inline unsigned int422irnet_ctrl_poll(irnet_socket * ap,423struct file * file,424poll_table * wait)425{426unsigned int mask;427428DENTER(CTRL_TRACE, "(ap=0x%p)\n", ap);429430poll_wait(file, &irnet_events.rwait, wait);431mask = POLLOUT | POLLWRNORM;432/* If there is unread events */433if(ap->event_index != irnet_events.index)434mask |= POLLIN | POLLRDNORM;435#ifdef INITIAL_DISCOVERY436if(ap->disco_number != -1)437{438/* Test if it's the first time and therefore we need to get the log */439if(ap->discoveries == NULL)440irnet_get_discovery_log(ap);441/* Recheck */442if(ap->disco_number != -1)443mask |= POLLIN | POLLRDNORM;444}445#endif /* INITIAL_DISCOVERY */446447DEXIT(CTRL_TRACE, " - mask=0x%X\n", mask);448return mask;449}450451452/*********************** FILESYSTEM CALLBACKS ***********************/453/*454* Implement the usual open, read, write functions that will be called455* by the file system when some action is performed on /dev/irnet.456* Most of those actions will in fact be performed by "pppd" or457* the control channel, we just act as a redirector...458*/459460/*------------------------------------------------------------------*/461/*462* Open : when somebody open /dev/irnet463* We basically create a new instance of irnet and initialise it.464*/465static int466dev_irnet_open(struct inode * inode,467struct file * file)468{469struct irnet_socket * ap;470int err;471472DENTER(FS_TRACE, "(file=0x%p)\n", file);473474#ifdef SECURE_DEVIRNET475/* This could (should?) be enforced by the permissions on /dev/irnet. */476if(!capable(CAP_NET_ADMIN))477return -EPERM;478#endif /* SECURE_DEVIRNET */479480/* Allocate a private structure for this IrNET instance */481ap = kzalloc(sizeof(*ap), GFP_KERNEL);482DABORT(ap == NULL, -ENOMEM, FS_ERROR, "Can't allocate struct irnet...\n");483484/* initialize the irnet structure */485ap->file = file;486487/* PPP channel setup */488ap->ppp_open = 0;489ap->chan.private = ap;490ap->chan.ops = &irnet_ppp_ops;491ap->chan.mtu = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);492ap->chan.hdrlen = 2 + TTP_MAX_HEADER; /* for A/C + Max IrDA hdr */493/* PPP parameters */494ap->mru = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);495ap->xaccm[0] = ~0U;496ap->xaccm[3] = 0x60000000U;497ap->raccm = ~0U;498499/* Setup the IrDA part... */500err = irda_irnet_create(ap);501if(err)502{503DERROR(FS_ERROR, "Can't setup IrDA link...\n");504kfree(ap);505506return err;507}508509/* For the control channel */510ap->event_index = irnet_events.index; /* Cancel all past events */511512mutex_init(&ap->lock);513514/* Put our stuff where we will be able to find it later */515file->private_data = ap;516517DEXIT(FS_TRACE, " - ap=0x%p\n", ap);518519return 0;520}521522523/*------------------------------------------------------------------*/524/*525* Close : when somebody close /dev/irnet526* Destroy the instance of /dev/irnet527*/528static int529dev_irnet_close(struct inode * inode,530struct file * file)531{532irnet_socket * ap = file->private_data;533534DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",535file, ap);536DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");537538/* Detach ourselves */539file->private_data = NULL;540541/* Close IrDA stuff */542irda_irnet_destroy(ap);543544/* Disconnect from the generic PPP layer if not already done */545if(ap->ppp_open)546{547DERROR(FS_ERROR, "Channel still registered - deregistering !\n");548ap->ppp_open = 0;549ppp_unregister_channel(&ap->chan);550}551552kfree(ap);553554DEXIT(FS_TRACE, "\n");555return 0;556}557558/*------------------------------------------------------------------*/559/*560* Write does nothing.561* (we receive packet from ppp_generic through ppp_irnet_send())562*/563static ssize_t564dev_irnet_write(struct file * file,565const char __user *buf,566size_t count,567loff_t * ppos)568{569irnet_socket * ap = file->private_data;570571DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",572file, ap, count);573DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");574575/* If we are connected to ppp_generic, let it handle the job */576if(ap->ppp_open)577return -EAGAIN;578else579return irnet_ctrl_write(ap, buf, count);580}581582/*------------------------------------------------------------------*/583/*584* Read doesn't do much either.585* (pppd poll us, but ultimately reads through /dev/ppp)586*/587static ssize_t588dev_irnet_read(struct file * file,589char __user * buf,590size_t count,591loff_t * ppos)592{593irnet_socket * ap = file->private_data;594595DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",596file, ap, count);597DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");598599/* If we are connected to ppp_generic, let it handle the job */600if(ap->ppp_open)601return -EAGAIN;602else603return irnet_ctrl_read(ap, file, buf, count);604}605606/*------------------------------------------------------------------*/607/*608* Poll : called when someone do a select on /dev/irnet609*/610static unsigned int611dev_irnet_poll(struct file * file,612poll_table * wait)613{614irnet_socket * ap = file->private_data;615unsigned int mask;616617DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",618file, ap);619620mask = POLLOUT | POLLWRNORM;621DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");622623/* If we are connected to ppp_generic, let it handle the job */624if(!ap->ppp_open)625mask |= irnet_ctrl_poll(ap, file, wait);626627DEXIT(FS_TRACE, " - mask=0x%X\n", mask);628return mask;629}630631/*------------------------------------------------------------------*/632/*633* IOCtl : Called when someone does some ioctls on /dev/irnet634* This is the way pppd configure us and control us while the PPP635* instance is active.636*/637static long638dev_irnet_ioctl(639struct file * file,640unsigned int cmd,641unsigned long arg)642{643irnet_socket * ap = file->private_data;644int err;645int val;646void __user *argp = (void __user *)arg;647648DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",649file, ap, cmd);650651/* Basic checks... */652DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");653#ifdef SECURE_DEVIRNET654if(!capable(CAP_NET_ADMIN))655return -EPERM;656#endif /* SECURE_DEVIRNET */657658err = -EFAULT;659switch(cmd)660{661/* Set discipline (should be N_SYNC_PPP or N_TTY) */662case TIOCSETD:663if(get_user(val, (int __user *)argp))664break;665if((val == N_SYNC_PPP) || (val == N_PPP))666{667DEBUG(FS_INFO, "Entering PPP discipline.\n");668/* PPP channel setup (ap->chan in configured in dev_irnet_open())*/669if (mutex_lock_interruptible(&ap->lock))670return -EINTR;671672err = ppp_register_channel(&ap->chan);673if(err == 0)674{675/* Our ppp side is active */676ap->ppp_open = 1;677678DEBUG(FS_INFO, "Trying to establish a connection.\n");679/* Setup the IrDA link now - may fail... */680irda_irnet_connect(ap);681}682else683DERROR(FS_ERROR, "Can't setup PPP channel...\n");684685mutex_unlock(&ap->lock);686}687else688{689/* In theory, should be N_TTY */690DEBUG(FS_INFO, "Exiting PPP discipline.\n");691/* Disconnect from the generic PPP layer */692if (mutex_lock_interruptible(&ap->lock))693return -EINTR;694695if(ap->ppp_open)696{697ap->ppp_open = 0;698ppp_unregister_channel(&ap->chan);699}700else701DERROR(FS_ERROR, "Channel not registered !\n");702err = 0;703704mutex_unlock(&ap->lock);705}706break;707708/* Query PPP channel and unit number */709case PPPIOCGCHAN:710if (mutex_lock_interruptible(&ap->lock))711return -EINTR;712713if(ap->ppp_open && !put_user(ppp_channel_index(&ap->chan),714(int __user *)argp))715err = 0;716717mutex_unlock(&ap->lock);718break;719case PPPIOCGUNIT:720if (mutex_lock_interruptible(&ap->lock))721return -EINTR;722723if(ap->ppp_open && !put_user(ppp_unit_number(&ap->chan),724(int __user *)argp))725err = 0;726727mutex_unlock(&ap->lock);728break;729730/* All these ioctls can be passed both directly and from ppp_generic,731* so we just deal with them in one place...732*/733case PPPIOCGFLAGS:734case PPPIOCSFLAGS:735case PPPIOCGASYNCMAP:736case PPPIOCSASYNCMAP:737case PPPIOCGRASYNCMAP:738case PPPIOCSRASYNCMAP:739case PPPIOCGXASYNCMAP:740case PPPIOCSXASYNCMAP:741case PPPIOCGMRU:742case PPPIOCSMRU:743DEBUG(FS_INFO, "Standard PPP ioctl.\n");744if(!capable(CAP_NET_ADMIN))745err = -EPERM;746else {747if (mutex_lock_interruptible(&ap->lock))748return -EINTR;749750err = ppp_irnet_ioctl(&ap->chan, cmd, arg);751752mutex_unlock(&ap->lock);753}754break;755756/* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */757/* Get termios */758case TCGETS:759DEBUG(FS_INFO, "Get termios.\n");760if (mutex_lock_interruptible(&ap->lock))761return -EINTR;762763#ifndef TCGETS2764if(!kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))765err = 0;766#else767if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))768err = 0;769#endif770771mutex_unlock(&ap->lock);772break;773/* Set termios */774case TCSETSF:775DEBUG(FS_INFO, "Set termios.\n");776if (mutex_lock_interruptible(&ap->lock))777return -EINTR;778779#ifndef TCGETS2780if(!user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))781err = 0;782#else783if(!user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))784err = 0;785#endif786787mutex_unlock(&ap->lock);788break;789790/* Set DTR/RTS */791case TIOCMBIS:792case TIOCMBIC:793/* Set exclusive/non-exclusive mode */794case TIOCEXCL:795case TIOCNXCL:796DEBUG(FS_INFO, "TTY compatibility.\n");797err = 0;798break;799800case TCGETA:801DEBUG(FS_INFO, "TCGETA\n");802break;803804case TCFLSH:805DEBUG(FS_INFO, "TCFLSH\n");806/* Note : this will flush buffers in PPP, so it *must* be done807* We should also worry that we don't accept junk here and that808* we get rid of our own buffers */809#ifdef FLUSH_TO_PPP810if (mutex_lock_interruptible(&ap->lock))811return -EINTR;812ppp_output_wakeup(&ap->chan);813mutex_unlock(&ap->lock);814#endif /* FLUSH_TO_PPP */815err = 0;816break;817818case FIONREAD:819DEBUG(FS_INFO, "FIONREAD\n");820val = 0;821if(put_user(val, (int __user *)argp))822break;823err = 0;824break;825826default:827DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);828err = -ENOTTY;829}830831DEXIT(FS_TRACE, " - err = 0x%X\n", err);832return err;833}834835/************************** PPP CALLBACKS **************************/836/*837* This are the functions that the generic PPP driver in the kernel838* will call to communicate to us.839*/840841/*------------------------------------------------------------------*/842/*843* Prepare the ppp frame for transmission over the IrDA socket.844* We make sure that the header space is enough, and we change ppp header845* according to flags passed by pppd.846* This is not a callback, but just a helper function used in ppp_irnet_send()847*/848static inline struct sk_buff *849irnet_prepare_skb(irnet_socket * ap,850struct sk_buff * skb)851{852unsigned char * data;853int proto; /* PPP protocol */854int islcp; /* Protocol == LCP */855int needaddr; /* Need PPP address */856857DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",858ap, skb);859860/* Extract PPP protocol from the frame */861data = skb->data;862proto = (data[0] << 8) + data[1];863864/* LCP packets with codes between 1 (configure-request)865* and 7 (code-reject) must be sent as though no options866* have been negotiated. */867islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);868869/* compress protocol field if option enabled */870if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))871skb_pull(skb,1);872873/* Check if we need address/control fields */874needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);875876/* Is the skb headroom large enough to contain all IrDA-headers? */877if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||878(skb_shared(skb)))879{880struct sk_buff * new_skb;881882DEBUG(PPP_INFO, "Reallocating skb\n");883884/* Create a new skb */885new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);886887/* We have to free the original skb anyway */888dev_kfree_skb(skb);889890/* Did the realloc succeed ? */891DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");892893/* Use the new skb instead */894skb = new_skb;895}896897/* prepend address/control fields if necessary */898if(needaddr)899{900skb_push(skb, 2);901skb->data[0] = PPP_ALLSTATIONS;902skb->data[1] = PPP_UI;903}904905DEXIT(PPP_TRACE, "\n");906907return skb;908}909910/*------------------------------------------------------------------*/911/*912* Send a packet to the peer over the IrTTP connection.913* Returns 1 iff the packet was accepted.914* Returns 0 iff packet was not consumed.915* If the packet was not accepted, we will call ppp_output_wakeup916* at some later time to reactivate flow control in ppp_generic.917*/918static int919ppp_irnet_send(struct ppp_channel * chan,920struct sk_buff * skb)921{922irnet_socket * self = (struct irnet_socket *) chan->private;923int ret;924925DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",926chan, self);927928/* Check if things are somewhat valid... */929DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");930931/* Check if we are connected */932if(!(test_bit(0, &self->ttp_open)))933{934#ifdef CONNECT_IN_SEND935/* Let's try to connect one more time... */936/* Note : we won't be connected after this call, but we should be937* ready for next packet... */938/* If we are already connecting, this will fail */939irda_irnet_connect(self);940#endif /* CONNECT_IN_SEND */941942DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",943self->ttp_open, self->ttp_connect);944945/* Note : we can either drop the packet or block the packet.946*947* Blocking the packet allow us a better connection time,948* because by calling ppp_output_wakeup() we can have949* ppp_generic resending the LCP request immediately to us,950* rather than waiting for one of pppd periodic transmission of951* LCP request.952*953* On the other hand, if we block all packet, all those periodic954* transmissions of pppd accumulate in ppp_generic, creating a955* backlog of LCP request. When we eventually connect later on,956* we have to transmit all this backlog before we can connect957* proper (if we don't timeout before).958*959* The current strategy is as follow :960* While we are attempting to connect, we block packets to get961* a better connection time.962* If we fail to connect, we drain the queue and start dropping packets963*/964#ifdef BLOCK_WHEN_CONNECT965/* If we are attempting to connect */966if(test_bit(0, &self->ttp_connect))967{968/* Blocking packet, ppp_generic will retry later */969return 0;970}971#endif /* BLOCK_WHEN_CONNECT */972973/* Dropping packet, pppd will retry later */974dev_kfree_skb(skb);975return 1;976}977978/* Check if the queue can accept any packet, otherwise block */979if(self->tx_flow != FLOW_START)980DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",981skb_queue_len(&self->tsap->tx_queue));982983/* Prepare ppp frame for transmission */984skb = irnet_prepare_skb(self, skb);985DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");986987/* Send the packet to IrTTP */988ret = irttp_data_request(self->tsap, skb);989if(ret < 0)990{991/*992* > IrTTPs tx queue is full, so we just have to993* > drop the frame! You might think that we should994* > just return -1 and don't deallocate the frame,995* > but that is dangerous since it's possible that996* > we have replaced the original skb with a new997* > one with larger headroom, and that would really998* > confuse do_dev_queue_xmit() in dev.c! I have999* > tried :-) DB1000* Correction : we verify the flow control above (self->tx_flow),1001* so we come here only if IrTTP doesn't like the packet (empty,1002* too large, IrTTP not connected). In those rare cases, it's ok1003* to drop it, we don't want to see it here again...1004* Jean II1005*/1006DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);1007/* irttp_data_request already free the packet */1008}10091010DEXIT(PPP_TRACE, "\n");1011return 1; /* Packet has been consumed */1012}10131014/*------------------------------------------------------------------*/1015/*1016* Take care of the ioctls that ppp_generic doesn't want to deal with...1017* Note : we are also called from dev_irnet_ioctl().1018*/1019static int1020ppp_irnet_ioctl(struct ppp_channel * chan,1021unsigned int cmd,1022unsigned long arg)1023{1024irnet_socket * ap = (struct irnet_socket *) chan->private;1025int err;1026int val;1027u32 accm[8];1028void __user *argp = (void __user *)arg;10291030DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",1031chan, ap, cmd);10321033/* Basic checks... */1034DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");10351036err = -EFAULT;1037switch(cmd)1038{1039/* PPP flags */1040case PPPIOCGFLAGS:1041val = ap->flags | ap->rbits;1042if(put_user(val, (int __user *) argp))1043break;1044err = 0;1045break;1046case PPPIOCSFLAGS:1047if(get_user(val, (int __user *) argp))1048break;1049ap->flags = val & ~SC_RCV_BITS;1050ap->rbits = val & SC_RCV_BITS;1051err = 0;1052break;10531054/* Async map stuff - all dummy to please pppd */1055case PPPIOCGASYNCMAP:1056if(put_user(ap->xaccm[0], (u32 __user *) argp))1057break;1058err = 0;1059break;1060case PPPIOCSASYNCMAP:1061if(get_user(ap->xaccm[0], (u32 __user *) argp))1062break;1063err = 0;1064break;1065case PPPIOCGRASYNCMAP:1066if(put_user(ap->raccm, (u32 __user *) argp))1067break;1068err = 0;1069break;1070case PPPIOCSRASYNCMAP:1071if(get_user(ap->raccm, (u32 __user *) argp))1072break;1073err = 0;1074break;1075case PPPIOCGXASYNCMAP:1076if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))1077break;1078err = 0;1079break;1080case PPPIOCSXASYNCMAP:1081if(copy_from_user(accm, argp, sizeof(accm)))1082break;1083accm[2] &= ~0x40000000U; /* can't escape 0x5e */1084accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */1085memcpy(ap->xaccm, accm, sizeof(ap->xaccm));1086err = 0;1087break;10881089/* Max PPP frame size */1090case PPPIOCGMRU:1091if(put_user(ap->mru, (int __user *) argp))1092break;1093err = 0;1094break;1095case PPPIOCSMRU:1096if(get_user(val, (int __user *) argp))1097break;1098if(val < PPP_MRU)1099val = PPP_MRU;1100ap->mru = val;1101err = 0;1102break;11031104default:1105DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);1106err = -ENOIOCTLCMD;1107}11081109DEXIT(PPP_TRACE, " - err = 0x%X\n", err);1110return err;1111}11121113/************************** INITIALISATION **************************/1114/*1115* Module initialisation and all that jazz...1116*/11171118/*------------------------------------------------------------------*/1119/*1120* Hook our device callbacks in the filesystem, to connect our code1121* to /dev/irnet1122*/1123static inline int __init1124ppp_irnet_init(void)1125{1126int err = 0;11271128DENTER(MODULE_TRACE, "()\n");11291130/* Allocate ourselves as a minor in the misc range */1131err = misc_register(&irnet_misc_device);11321133DEXIT(MODULE_TRACE, "\n");1134return err;1135}11361137/*------------------------------------------------------------------*/1138/*1139* Cleanup at exit...1140*/1141static inline void __exit1142ppp_irnet_cleanup(void)1143{1144DENTER(MODULE_TRACE, "()\n");11451146/* De-allocate /dev/irnet minor in misc range */1147misc_deregister(&irnet_misc_device);11481149DEXIT(MODULE_TRACE, "\n");1150}11511152/*------------------------------------------------------------------*/1153/*1154* Module main entry point1155*/1156static int __init1157irnet_init(void)1158{1159int err;11601161/* Initialise both parts... */1162err = irda_irnet_init();1163if(!err)1164err = ppp_irnet_init();1165return err;1166}11671168/*------------------------------------------------------------------*/1169/*1170* Module exit1171*/1172static void __exit1173irnet_cleanup(void)1174{1175irda_irnet_cleanup();1176ppp_irnet_cleanup();1177}11781179/*------------------------------------------------------------------*/1180/*1181* Module magic1182*/1183module_init(irnet_init);1184module_exit(irnet_cleanup);1185MODULE_AUTHOR("Jean Tourrilhes <[email protected]>");1186MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");1187MODULE_LICENSE("GPL");1188MODULE_ALIAS_CHARDEV(10, 187);118911901191