1 /*****************************************************************************
2 * af_wanpipe.c WANPIPE(tm) Secure Socket Layer.
4 * Author: Nenad Corbic <ncorbic@sangoma.com>
6 * Copyright: (c) 2000 Sangoma Technologies Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 * ============================================================================
14 * Wanpipe socket layer is based on Packet and
15 * the X25 socket layers. The above sockets were
16 * used for the specific use of Sangoma Technoloiges
18 * Packet socket Authors: Ross Biro, Fred N. van Kempen and
20 * X25 socket Author: Jonathan Naylor.
21 * ============================================================================
22 * Mar 15, 2002 Arnaldo C. Melo o Use wp_sk()->num, as it isnt anymore in sock
23 * Apr 25, 2000 Nenad Corbic o Added the ability to send zero length packets.
24 * Mar 13, 2000 Nenad Corbic o Added a tx buffer check via ioctl call.
25 * Mar 06, 2000 Nenad Corbic o Fixed the corrupt sock lcn problem.
26 * Server and client applicaton can run
27 * simultaneously without conflicts.
28 * Feb 29, 2000 Nenad Corbic o Added support for PVC protocols, such as
29 * CHDLC, Frame Relay and HDLC API.
30 * Jan 17, 2000 Nenad Corbic o Initial version, based on AF_PACKET socket.
31 * X25API support only.
33 ******************************************************************************/
35 #include <linux/config.h>
36 #include <linux/types.h>
37 #include <linux/sched.h>
39 #include <linux/fcntl.h>
40 #include <linux/socket.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <linux/poll.h>
45 #include <linux/wireless.h>
46 #include <linux/kmod.h>
48 #include <net/protocol.h>
49 #include <linux/skbuff.h>
51 #include <linux/errno.h>
52 #include <linux/timer.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/wanpipe.h>
58 #include <linux/if_wanpipe.h>
59 #include <linux/pkt_sched.h>
60 #include <linux/tcp.h>
61 #include <linux/if_wanpipe_common.h>
62 #include <linux/sdla_x25.h>
65 #include <net/inet_common.h>
68 #define SLOW_BACKOFF 0.1*HZ
69 #define FAST_BACKOFF 0.01*HZ
73 #define DBG_PRINTK(format, a...) printk(format, ## a)
75 #define DBG_PRINTK(format, a...)
79 /* SECURE SOCKET IMPLEMENTATION
83 * When the user sends a packet via send() system call
84 * the wanpipe_sendmsg() function is executed.
86 * Each packet is enqueud into sk->sk_write_queue transmit
87 * queue. When the packet is enqueued, a delayed transmit
88 * timer is triggerd which acts as a Bottom Half hander.
90 * wanpipe_delay_transmit() function (BH), dequeues packets
91 * from the sk->sk_write_queue transmit queue and sends it
92 * to the deriver via dev->hard_start_xmit(skb, dev) function.
93 * Note, this function is actual a function pointer of if_send()
94 * routine in the wanpipe driver.
96 * X25API GUARANTEED DELIVERY:
98 * In order to provide 100% guaranteed packet delivery,
99 * an atomic 'packet_sent' counter is implemented. Counter
100 * is incremented for each packet enqueued
101 * into sk->sk_write_queue. Counter is decremented each
102 * time wanpipe_delayed_transmit() function successfuly
103 * passes the packet to the driver. Before each send(), a poll
104 * routine checks the sock resources The maximum value of
105 * packet sent counter is 1, thus if one packet is queued, the
106 * application will block until that packet is passed to the
111 * Wanpipe device drivers call the socket bottom half
112 * function, wanpipe_rcv() to queue the incoming packets
113 * into an AF_WANPIPE socket queue. Based on wanpipe_rcv()
114 * return code, the driver knows whether the packet was
115 * successfully queued. If the socket queue is full,
116 * protocol flow control is used by the driver, if any,
117 * to slow down the traffic until the sock queue is free.
119 * Every time a packet arrives into a socket queue the
120 * socket wakes up processes which are waiting to receive
123 * If the socket queue is full, the driver sets a block
124 * bit which signals the socket to kick the wanpipe driver
125 * bottom half hander when the socket queue is partialy
126 * empty. wanpipe_recvmsg() function performs this action.
128 * In case of x25api, packets will never be dropped, since
129 * flow control is available.
131 * In case of streaming protocols like CHDLC, packets will
132 * be dropped but the statistics will be generated.
136 /* The code below is used to test memory leaks. It prints out
137 * a message every time kmalloc and kfree system calls get executed.
138 * If the calls match there is no leak :)
141 /***********FOR DEBUGGING PURPOSES*********************************************
142 #define KMEM_SAFETYZONE 8
144 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
145 void * v = kmalloc(size,prio);
146 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
149 static void dbg_kfree(void * v, int line) {
150 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
154 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
155 #define kfree(x) dbg_kfree(x,__LINE__)
156 ******************************************************************************/
159 /* List of all wanpipe sockets. */
160 HLIST_HEAD(wanpipe_sklist);
161 static rwlock_t wanpipe_sklist_lock = RW_LOCK_UNLOCKED;
163 atomic_t wanpipe_socks_nr;
164 static unsigned long wanpipe_tx_critical;
167 /* Private wanpipe socket structures. */
170 void *mbox; /* Mail box */
171 void *card; /* Card bouded to */
172 struct net_device *dev; /* Bounded device */
173 unsigned short lcn; /* Binded LCN */
174 unsigned char svc; /* 0=pvc, 1=svc */
175 unsigned char timer; /* flag for delayed transmit*/
176 struct timer_list tx_timer;
178 unsigned char force; /* Used to force sock release */
179 atomic_t packet_sent;
184 extern struct proto_ops wanpipe_ops;
185 static unsigned long find_free_critical;
187 static void wanpipe_unlink_driver(struct sock *sk);
188 static void wanpipe_link_driver(struct net_device *dev, struct sock *sk);
189 static void wanpipe_wakeup_driver(struct sock *sk);
190 static int execute_command(struct sock *, unsigned char, unsigned int);
191 static int check_dev(struct net_device *dev, sdla_t *card);
192 struct net_device *wanpipe_find_free_dev(sdla_t *card);
193 static void wanpipe_unlink_card (struct sock *);
194 static int wanpipe_link_card (struct sock *);
195 static struct sock *wanpipe_make_new(struct sock *);
196 static struct sock *wanpipe_alloc_socket(void);
197 static inline int get_atomic_device(struct net_device *dev);
198 static int wanpipe_exec_cmd(struct sock *, int, unsigned int);
199 static int get_ioctl_cmd (struct sock *, void *);
200 static int set_ioctl_cmd (struct sock *, void *);
201 static void release_device(struct net_device *dev);
202 static void wanpipe_kill_sock_timer (unsigned long data);
203 static void wanpipe_kill_sock_irq (struct sock *);
204 static void wanpipe_kill_sock_accept (struct sock *);
205 static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
207 struct sock * get_newsk_from_skb (struct sk_buff *);
208 static int wanpipe_debug (struct sock *, void *);
209 static void wanpipe_delayed_transmit (unsigned long data);
210 static void release_driver(struct sock *);
211 static void start_cleanup_timer (struct sock *);
212 static void check_write_queue(struct sock *);
213 static int check_driver_busy (struct sock *);
215 /*============================================================
218 * Wanpipe socket bottom half handler. This function
219 * is called by the WANPIPE device drivers to queue a
220 * incoming packet into the socket receive queue.
221 * Once the packet is queued, all processes waiting to
224 * During socket bind, this function is bounded into
225 * WANPIPE driver private.
226 *===========================================================*/
228 static int wanpipe_rcv(struct sk_buff *skb, struct net_device *dev,
231 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
232 wanpipe_common_t *chan = dev->priv;
234 * When we registered the protocol we saved the socket in the data
235 * field for just this event.
240 sll->sll_family = AF_WANPIPE;
241 sll->sll_hatype = dev->type;
242 sll->sll_protocol = skb->protocol;
243 sll->sll_pkttype = skb->pkt_type;
244 sll->sll_ifindex = dev->ifindex;
247 if (dev->hard_header_parse)
248 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
251 * WAN_PACKET_DATA : Data which should be passed up the receive queue.
252 * WAN_PACKET_ASYC : Asynchronous data like place call, which should
253 * be passed up the listening sock.
254 * WAN_PACKET_ERR : Asynchronous data like clear call or restart
255 * which should go into an error queue.
257 switch (skb->pkt_type){
259 case WAN_PACKET_DATA:
260 if (sock_queue_rcv_skb(sk,skb)<0){
265 sk->sk_state = chan->state;
266 /* Bug fix: update Mar6.
267 * Do not set the sock lcn number here, since
268 * cmd is not guaranteed to be executed on the
269 * board, thus Lcn could be wrong */
270 sk->sk_data_ready(sk, skb->len);
274 sk->sk_state = chan->state;
275 if (sock_queue_err_skb(sk,skb)<0){
280 printk(KERN_INFO "wansock: BH Illegal Packet Type Dropping\n");
285 //??????????????????????
286 // if (sk->sk_state == WANSOCK_DISCONNECTED){
287 // if (sk->sk_zapped) {
288 // //printk(KERN_INFO "wansock: Disconnected, killing early\n");
289 // wanpipe_unlink_driver(sk);
290 // sk->sk_bound_dev_if = 0;
297 /*============================================================
300 * Wanpipe LISTEN socket bottom half handler. This function
301 * is called by the WANPIPE device drivers to queue an
302 * incoming call into the socket listening queue.
303 * Once the packet is queued, the waiting accept() process
306 * During socket bind, this function is bounded into
307 * WANPIPE driver private.
310 * The accept call() is waiting for an skb packet
311 * which contains a pointer to a device structure.
313 * When we do a bind to a device structre, we
314 * bind a newly created socket into "chan->sk". Thus,
315 * when accept receives the skb packet, it will know
316 * from which dev it came form, and in turn it will know
317 * the address of the new sock.
319 * NOTE: This function gets called from driver ISR.
320 *===========================================================*/
322 static int wanpipe_listen_rcv (struct sk_buff *skb, struct sock *sk)
324 wanpipe_opt *wp = wp_sk(sk), *newwp;
325 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
327 struct net_device *dev;
329 mbox_cmd_t *mbox_ptr;
330 wanpipe_common_t *chan;
332 /* Find a free device, if none found, all svc's are busy
335 card = (sdla_t*)wp->card;
337 printk(KERN_INFO "wansock: LISTEN ERROR, No Card\n");
341 dev = wanpipe_find_free_dev(card);
343 printk(KERN_INFO "wansock: LISTEN ERROR, No Free Device\n");
348 chan->state = WANSOCK_CONNECTING;
350 /* Allocate a new sock, which accept will bind
351 * and pass up to the user
353 if ((newsk = wanpipe_make_new(sk)) == NULL){
359 /* Initialize the new sock structure
361 newsk->sk_bound_dev_if = dev->ifindex;
362 newwp = wp_sk(newsk);
363 newwp->card = wp->card;
365 /* Insert the sock into the main wanpipe
368 atomic_inc(&wanpipe_socks_nr);
370 /* Allocate and fill in the new Mail Box. Then
371 * bind the mail box to the sock. It will be
372 * used by the ioctl call to read call information
373 * and to execute commands.
375 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL) {
376 wanpipe_kill_sock_irq (newsk);
380 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
381 memcpy(mbox_ptr,skb->data,skb->len);
383 /* Register the lcn on which incoming call came
384 * from. Thus, if we have to clear it, we know
388 newwp->lcn = mbox_ptr->cmd.lcn;
389 newwp->mbox = (void *)mbox_ptr;
391 DBG_PRINTK(KERN_INFO "NEWSOCK : Device %s, bind to lcn %i\n",
392 dev->name,mbox_ptr->cmd.lcn);
394 chan->lcn = mbox_ptr->cmd.lcn;
395 card->u.x.svc_to_dev_map[(chan->lcn%MAX_X25_LCN)] = dev;
397 newsk->sk_zapped = 0;
398 newwp->num = htons(X25_PROT);
400 if (wanpipe_do_bind(newsk, dev, newwp->num)) {
401 wanpipe_kill_sock_irq (newsk);
405 newsk->sk_state = WANSOCK_CONNECTING;
408 /* Fill in the standard sock address info */
410 sll->sll_family = AF_WANPIPE;
411 sll->sll_hatype = dev->type;
412 sll->sll_protocol = skb->protocol;
413 sll->sll_pkttype = skb->pkt_type;
414 sll->sll_ifindex = dev->ifindex;
418 sk->sk_ack_backlog++;
420 /* We must do this manually, since the sock_queue_rcv_skb()
421 * function sets the skb->dev to NULL. However, we use
422 * the dev field in the accept function.*/
423 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
424 (unsigned)sk->sk_rcvbuf) {
426 wanpipe_unlink_driver(newsk);
427 wanpipe_kill_sock_irq (newsk);
428 --sk->sk_ack_backlog;
432 skb_set_owner_r(skb, sk);
433 skb_queue_tail(&sk->sk_receive_queue, skb);
434 sk->sk_data_ready(sk, skb->len);
441 /*============================================================
444 * Create a new sock, and allocate a wanpipe private
445 * structure to it. Also, copy the important data
446 * from the original sock to the new sock.
448 * This function is used by wanpipe_listen_rcv() listen
449 * bottom half handler. A copy of the listening sock
450 * is created using this function.
452 *===========================================================*/
454 static struct sock *wanpipe_make_new(struct sock *osk)
458 if (osk->sk_type != SOCK_RAW)
461 if ((sk = wanpipe_alloc_socket()) == NULL)
464 sk->sk_type = osk->sk_type;
465 sk->sk_socket = osk->sk_socket;
466 sk->sk_priority = osk->sk_priority;
467 sk->sk_protocol = osk->sk_protocol;
468 wp_sk(sk)->num = wp_sk(osk)->num;
469 sk->sk_rcvbuf = osk->sk_rcvbuf;
470 sk->sk_sndbuf = osk->sk_sndbuf;
471 sk->sk_debug = osk->sk_debug;
472 sk->sk_state = WANSOCK_CONNECTING;
473 sk->sk_sleep = osk->sk_sleep;
478 /*============================================================
481 * Allocate memory for the a new sock, and sock
484 * Increment the module use count.
486 * This function is used by wanpipe_create() and
487 * wanpipe_make_new() functions.
489 *===========================================================*/
491 static struct sock *wanpipe_alloc_socket(void)
494 struct wanpipe_opt *wan_opt;
496 if ((sk = sk_alloc(PF_WANPIPE, GFP_ATOMIC, 1, NULL)) == NULL)
499 if ((wan_opt = kmalloc(sizeof(struct wanpipe_opt), GFP_ATOMIC)) == NULL) {
503 memset(wan_opt, 0x00, sizeof(struct wanpipe_opt));
507 /* Use timer to send data to the driver. This will act
508 * as a BH handler for sendmsg functions */
509 init_timer(&wan_opt->tx_timer);
510 wan_opt->tx_timer.data = (unsigned long)sk;
511 wan_opt->tx_timer.function = wanpipe_delayed_transmit;
513 sock_init_data(NULL, sk);
518 /*============================================================
521 * This function implements a sendto() system call,
522 * for AF_WANPIPE socket family.
523 * During socket bind() sk->sk_bound_dev_if is initialized
524 * to a correct network device. This number is used
525 * to find a network device to which the packet should
528 * Each packet is queued into sk->sk_write_queue and
529 * delayed transmit bottom half handler is marked for
532 * A socket must be in WANSOCK_CONNECTED state before
533 * a packet is queued into sk->sk_write_queue.
534 *===========================================================*/
536 static int wanpipe_sendmsg(struct kiocb *iocb, struct socket *sock,
537 struct msghdr *msg, int len)
540 struct sock *sk = sock->sk;
541 struct wan_sockaddr_ll *saddr=(struct wan_sockaddr_ll *)msg->msg_name;
543 struct net_device *dev;
544 unsigned short proto;
546 int ifindex, err, reserve = 0;
552 if (sk->sk_state != WANSOCK_CONNECTED)
555 if (msg->msg_flags&~MSG_DONTWAIT)
558 /* it was <=, now one can send
559 * zero length packets */
560 if (len < sizeof(x25api_hdr_t))
566 ifindex = sk->sk_bound_dev_if;
571 if (msg->msg_namelen < sizeof(struct wan_sockaddr_ll)){
575 ifindex = sk->sk_bound_dev_if;
576 proto = saddr->sll_protocol;
577 addr = saddr->sll_addr;
580 dev = dev_get_by_index(ifindex);
582 printk(KERN_INFO "wansock: Send failed, dev index: %i\n",ifindex);
587 if (sock->type == SOCK_RAW)
588 reserve = dev->hard_header_len;
590 if (len > dev->mtu+reserve){
594 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
595 msg->msg_flags & MSG_DONTWAIT, &err);
601 skb_reserve(skb, LL_RESERVED_SPACE(dev));
602 skb->nh.raw = skb->data;
604 /* Returns -EFAULT on error */
605 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
610 if (dev->hard_header) {
613 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
619 skb->protocol = proto;
621 skb->priority = sk->sk_priority;
622 skb->pkt_type = WAN_PACKET_DATA;
625 if (!(dev->flags & IFF_UP))
628 if (atomic_read(&sk->sk_wmem_alloc) + skb->truesize >
629 (unsigned int)sk->sk_sndbuf){
634 skb_queue_tail(&sk->sk_write_queue,skb);
635 atomic_inc(&wp->packet_sent);
637 if (!(test_and_set_bit(0, &wp->timer)))
638 mod_timer(&wp->tx_timer, jiffies + 1);
648 /*============================================================
649 * wanpipe_delayed_tarnsmit
651 * Transmit bottom half handler. It dequeues packets
652 * from sk->sk_write_queue and passes them to the
653 * driver. If the driver is busy, the packet is
656 * Packet Sent counter is decremented on successful
658 *===========================================================*/
661 static void wanpipe_delayed_transmit (unsigned long data)
663 struct sock *sk=(struct sock *)data;
665 wanpipe_opt *wp = wp_sk(sk);
666 struct net_device *dev = wp->dev;
667 sdla_t *card = (sdla_t*)wp->card;
670 clear_bit(0, &wp->timer);
671 DBG_PRINTK(KERN_INFO "wansock: Transmit delay, no dev or card\n");
675 if (sk->sk_state != WANSOCK_CONNECTED || !sk->sk_zapped) {
676 clear_bit(0, &wp->timer);
677 DBG_PRINTK(KERN_INFO "wansock: Tx Timer, State not CONNECTED\n");
681 /* If driver is executing command, we must offload
682 * the board by not sending data. Otherwise a
683 * pending command will never get a free buffer
685 if (atomic_read(&card->u.x.command_busy)){
686 wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
687 add_timer(&wp->tx_timer);
688 DBG_PRINTK(KERN_INFO "wansock: Tx Timer, command bys BACKOFF\n");
693 if (test_and_set_bit(0,&wanpipe_tx_critical)){
694 printk(KERN_INFO "WanSock: Tx timer critical %s\n",dev->name);
695 wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
696 add_timer(&wp->tx_timer);
700 /* Check for a packet in the fifo and send */
701 if ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL){
703 if (dev->hard_start_xmit(skb, dev) != 0){
705 /* Driver failed to transmit, re-enqueue
706 * the packet and retry again later */
707 skb_queue_head(&sk->sk_write_queue,skb);
708 clear_bit(0,&wanpipe_tx_critical);
712 /* Packet Sent successful. Check for more packets
713 * if more packets, re-trigger the transmit routine
716 atomic_dec(&wp->packet_sent);
718 if (skb_peek(&sk->sk_write_queue) == NULL) {
719 /* If there is nothing to send, kick
720 * the poll routine, which will trigger
721 * the application to send more data */
722 sk->sk_data_ready(sk, 0);
723 clear_bit(0, &wp->timer);
725 /* Reschedule as fast as possible */
726 wp->tx_timer.expires = jiffies + 1;
727 add_timer(&wp->tx_timer);
731 clear_bit(0,&wanpipe_tx_critical);
734 /*============================================================
737 * Execute x25api commands. The atomic variable
738 * chan->command is used to indicate to the driver that
739 * command is pending for execution. The acutal command
740 * structure is placed into a sock mbox structure
743 * The sock private structure, mbox is
744 * used as shared memory between sock and the driver.
745 * Driver uses the sock mbox to execute the command
746 * and return the result.
748 * For all command except PLACE CALL, the function
749 * waits for the result. PLACE CALL can be ether
750 * blocking or nonblocking. The user sets this option
752 *===========================================================*/
755 static int execute_command(struct sock *sk, unsigned char cmd, unsigned int flags)
757 wanpipe_opt *wp = wp_sk(sk);
758 struct net_device *dev;
759 wanpipe_common_t *chan=NULL;
761 DECLARE_WAITQUEUE(wait, current);
763 dev = dev_get_by_index(sk->sk_bound_dev_if);
765 printk(KERN_INFO "wansock: Exec failed no dev %i\n",
766 sk->sk_bound_dev_if);
771 if ((chan=dev->priv) == NULL){
772 printk(KERN_INFO "wansock: Exec cmd failed no priv area\n");
776 if (atomic_read(&chan->command)){
777 printk(KERN_INFO "wansock: ERROR: Command already running %x, %s\n",
778 atomic_read(&chan->command),dev->name);
783 printk(KERN_INFO "wansock: In execute without MBOX\n");
787 ((mbox_cmd_t*)wp->mbox)->cmd.command = cmd;
788 ((mbox_cmd_t*)wp->mbox)->cmd.lcn = wp->lcn;
789 ((mbox_cmd_t*)wp->mbox)->cmd.result = 0x7F;
792 if (flags & O_NONBLOCK){
794 atomic_set(&chan->command, cmd);
796 atomic_set(&chan->command, cmd);
799 add_wait_queue(sk->sk_sleep,&wait);
800 current->state = TASK_INTERRUPTIBLE;
802 if (((mbox_cmd_t*)wp->mbox)->cmd.result != 0x7F) {
806 if (signal_pending(current)) {
812 current->state = TASK_RUNNING;
813 remove_wait_queue(sk->sk_sleep,&wait);
818 /*============================================================
819 * wanpipe_destroy_timer
821 * Used by wanpipe_release, to delay release of
823 *===========================================================*/
825 static void wanpipe_destroy_timer(unsigned long data)
827 struct sock *sk=(struct sock *)data;
828 wanpipe_opt *wp = wp_sk(sk);
830 if ((!atomic_read(&sk->sk_wmem_alloc) &&
831 !atomic_read(&sk->sk_rmem_alloc)) ||
832 (++wp->force == 5)) {
834 if (atomic_read(&sk->sk_wmem_alloc) ||
835 atomic_read(&sk->sk_rmem_alloc))
836 printk(KERN_INFO "wansock: Warning, Packet Discarded due to sock shutdown!\n");
841 if (atomic_read(&sk->sk_refcnt) != 1) {
842 atomic_set(&sk->sk_refcnt, 1);
843 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :delay.\n",
844 atomic_read(&sk->sk_refcnt));
847 atomic_dec(&wanpipe_socks_nr);
851 sk->sk_timer.expires = jiffies + 5 * HZ;
852 add_timer(&sk->sk_timer);
853 printk(KERN_INFO "wansock: packet sk destroy delayed\n");
856 /*============================================================
857 * wanpipe_unlink_driver
859 * When the socket is released, this function is
860 * used to remove links that bind the sock and the
862 *===========================================================*/
863 static void wanpipe_unlink_driver (struct sock *sk)
865 struct net_device *dev;
866 wanpipe_common_t *chan=NULL;
869 sk->sk_state = WANSOCK_DISCONNECTED;
870 wp_sk(sk)->dev = NULL;
872 dev = dev_get_by_index(sk->sk_bound_dev_if);
874 printk(KERN_INFO "wansock: No dev on release\n");
879 if ((chan = dev->priv) == NULL){
880 printk(KERN_INFO "wansock: No Priv Area on release\n");
884 set_bit(0,&chan->common_critical);
889 clear_bit(0,&chan->common_critical);
895 /*============================================================
896 * wanpipe_link_driver
898 * Upon successful bind(), sock is linked to a driver
899 * by binding in the wanpipe_rcv() bottom half handler
900 * to the driver function pointer, as well as sock and
901 * sock mailbox addresses. This way driver can pass
902 * data up the socket.
903 *===========================================================*/
905 static void wanpipe_link_driver(struct net_device *dev, struct sock *sk)
907 wanpipe_opt *wp = wp_sk(sk);
908 wanpipe_common_t *chan = dev->priv;
911 set_bit(0,&chan->common_critical);
913 chan->func=wanpipe_rcv;
914 chan->mbox = wp->mbox;
915 chan->tx_timer = &wp->tx_timer;
918 clear_bit(0,&chan->common_critical);
922 /*============================================================
925 * During sock release, clear a critical bit, which
926 * marks the device a being taken.
927 *===========================================================*/
930 static void release_device(struct net_device *dev)
932 wanpipe_common_t *chan=dev->priv;
933 clear_bit(0,(void*)&chan->rw_bind);
936 /*============================================================
939 * Close a PACKET socket. This is fairly simple. We
940 * immediately go to 'closed' state and remove our
941 * protocol entry in the device list.
942 *===========================================================*/
944 static int wanpipe_release(struct socket *sock)
947 struct sock *sk = sock->sk;
953 check_write_queue(sk);
955 /* Kill the tx timer, if we don't kill it now, the timer
956 * will run after we kill the sock. Timer code will
957 * try to access the sock which has been killed and cause
960 del_timer(&wp->tx_timer);
963 * Unhook packet receive handler.
966 if (wp->num == htons(X25_PROT) &&
967 sk->sk_state != WANSOCK_DISCONNECTED && sk->sk_zapped) {
968 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
969 wanpipe_common_t *chan;
972 atomic_set(&chan->disconnect,1);
973 DBG_PRINTK(KERN_INFO "wansock: Sending Clear Indication %i\n",
979 set_bit(1,&wanpipe_tx_critical);
980 write_lock(&wanpipe_sklist_lock);
981 sk_del_node_init(sk);
982 write_unlock(&wanpipe_sklist_lock);
983 clear_bit(1,&wanpipe_tx_critical);
991 * Now the socket is dead. No more input will appear.
994 sk->sk_state_change(sk); /* It is useless. Just for sanity. */
997 sk->sk_socket = NULL;
998 sock_set_flag(sk, SOCK_DEAD);
1001 skb_queue_purge(&sk->sk_receive_queue);
1002 skb_queue_purge(&sk->sk_write_queue);
1003 skb_queue_purge(&sk->sk_error_queue);
1005 if (atomic_read(&sk->sk_rmem_alloc) ||
1006 atomic_read(&sk->sk_wmem_alloc)) {
1007 del_timer(&sk->sk_timer);
1008 printk(KERN_INFO "wansock: Killing in Timer R %i , W %i\n",
1009 atomic_read(&sk->sk_rmem_alloc),
1010 atomic_read(&sk->sk_wmem_alloc));
1011 sk->sk_timer.data = (unsigned long)sk;
1012 sk->sk_timer.expires = jiffies + HZ;
1013 sk->sk_timer.function = wanpipe_destroy_timer;
1014 add_timer(&sk->sk_timer);
1021 if (atomic_read(&sk->sk_refcnt) != 1) {
1022 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:release.\n",
1023 atomic_read(&sk->sk_refcnt));
1024 atomic_set(&sk->sk_refcnt, 1);
1027 atomic_dec(&wanpipe_socks_nr);
1031 /*============================================================
1034 * During sock shutdown, if the sock state is
1035 * WANSOCK_CONNECTED and there is transmit data
1036 * pending. Wait until data is released
1037 * before proceeding.
1038 *===========================================================*/
1040 static void check_write_queue(struct sock *sk)
1043 if (sk->sk_state != WANSOCK_CONNECTED)
1046 if (!atomic_read(&sk->sk_wmem_alloc))
1049 printk(KERN_INFO "wansock: MAJOR ERROR, Data lost on sock release !!!\n");
1053 /*============================================================
1056 * This function is called during sock shutdown, to
1057 * release any resources and links that bind the sock
1058 * to the driver. It also changes the state of the
1059 * sock to WANSOCK_DISCONNECTED
1060 *===========================================================*/
1062 static void release_driver(struct sock *sk)
1065 struct sk_buff *skb=NULL;
1066 struct sock *deadsk=NULL;
1068 if (sk->sk_state == WANSOCK_LISTEN ||
1069 sk->sk_state == WANSOCK_BIND_LISTEN) {
1070 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1071 if ((deadsk = get_newsk_from_skb(skb))){
1072 DBG_PRINTK (KERN_INFO "wansock: RELEASE: FOUND DEAD SOCK\n");
1073 sock_set_flag(deadsk, SOCK_DEAD);
1074 start_cleanup_timer(deadsk);
1079 wanpipe_unlink_card(sk);
1082 wanpipe_unlink_driver(sk);
1084 sk->sk_state = WANSOCK_DISCONNECTED;
1085 sk->sk_bound_dev_if = 0;
1089 if (wp && wp->mbox) {
1095 /*============================================================
1096 * start_cleanup_timer
1098 * If new incoming call's are pending but the socket
1099 * is being released, start the timer which will
1100 * envoke the kill routines for pending socks.
1101 *===========================================================*/
1104 static void start_cleanup_timer (struct sock *sk)
1106 del_timer(&sk->sk_timer);
1107 sk->sk_timer.data = (unsigned long)sk;
1108 sk->sk_timer.expires = jiffies + HZ;
1109 sk->sk_timer.function = wanpipe_kill_sock_timer;
1110 add_timer(&sk->sk_timer);
1114 /*============================================================
1117 * This is a function which performs actual killing
1118 * of the sock. It releases socket resources,
1119 * and unlinks the sock from the driver.
1120 *===========================================================*/
1122 static void wanpipe_kill_sock_timer (unsigned long data)
1125 struct sock *sk = (struct sock *)data;
1131 /* This function can be called from interrupt. We must use
1132 * appropriate locks */
1134 if (test_bit(1,&wanpipe_tx_critical)){
1135 sk->sk_timer.expires = jiffies + 10;
1136 add_timer(&sk->sk_timer);
1140 write_lock(&wanpipe_sklist_lock);
1141 sk_del_node_init(sk);
1142 write_unlock(&wanpipe_sklist_lock);
1145 if (wp_sk(sk)->num == htons(X25_PROT) &&
1146 sk->sk_state != WANSOCK_DISCONNECTED) {
1147 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
1148 wanpipe_common_t *chan;
1151 atomic_set(&chan->disconnect,1);
1158 sk->sk_socket = NULL;
1161 skb_queue_purge(&sk->sk_receive_queue);
1162 skb_queue_purge(&sk->sk_write_queue);
1163 skb_queue_purge(&sk->sk_error_queue);
1165 if (atomic_read(&sk->sk_rmem_alloc) ||
1166 atomic_read(&sk->sk_wmem_alloc)) {
1167 del_timer(&sk->sk_timer);
1168 printk(KERN_INFO "wansock: Killing SOCK in Timer\n");
1169 sk->sk_timer.data = (unsigned long)sk;
1170 sk->sk_timer.expires = jiffies + HZ;
1171 sk->sk_timer.function = wanpipe_destroy_timer;
1172 add_timer(&sk->sk_timer);
1181 if (atomic_read(&sk->sk_refcnt) != 1) {
1182 atomic_set(&sk->sk_refcnt, 1);
1183 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
1184 atomic_read(&sk->sk_refcnt));
1187 atomic_dec(&wanpipe_socks_nr);
1191 static void wanpipe_kill_sock_accept (struct sock *sk)
1199 /* This function can be called from interrupt. We must use
1200 * appropriate locks */
1202 write_lock(&wanpipe_sklist_lock);
1203 sk_del_node_init(sk);
1204 write_unlock(&wanpipe_sklist_lock);
1206 sk->sk_socket = NULL;
1214 if (atomic_read(&sk->sk_refcnt) != 1) {
1215 atomic_set(&sk->sk_refcnt, 1);
1216 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
1217 atomic_read(&sk->sk_refcnt));
1220 atomic_dec(&wanpipe_socks_nr);
1225 static void wanpipe_kill_sock_irq (struct sock *sk)
1231 sk->sk_socket = NULL;
1238 if (atomic_read(&sk->sk_refcnt) != 1) {
1239 atomic_set(&sk->sk_refcnt, 1);
1240 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:listen.\n",
1241 atomic_read(&sk->sk_refcnt));
1244 atomic_dec(&wanpipe_socks_nr);
1248 /*============================================================
1251 * Bottom half of the binding system call.
1252 * Once the wanpipe_bind() function checks the
1253 * legality of the call, this function binds the
1254 * sock to the driver.
1255 *===========================================================*/
1257 static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
1260 wanpipe_opt *wp = wp_sk(sk);
1261 wanpipe_common_t *chan=NULL;
1264 if (sk->sk_zapped) {
1266 goto bind_unlock_exit;
1272 release_device(dev);
1274 goto bind_unlock_exit;
1278 if (dev->flags&IFF_UP) {
1280 sk->sk_state = chan->state;
1282 if (wp->num == htons(X25_PROT) &&
1283 sk->sk_state != WANSOCK_DISCONNECTED &&
1284 sk->sk_state != WANSOCK_CONNECTING) {
1285 DBG_PRINTK(KERN_INFO
1286 "wansock: Binding to Device not DISCONNECTED %i\n",
1288 release_device(dev);
1290 goto bind_unlock_exit;
1293 wanpipe_link_driver(dev,sk);
1294 sk->sk_bound_dev_if = dev->ifindex;
1296 /* X25 Specific option */
1297 if (wp->num == htons(X25_PROT))
1298 wp_sk(sk)->svc = chan->svc;
1301 sk->sk_err = ENETDOWN;
1302 sk->sk_error_report(sk);
1303 release_device(dev);
1310 /* FIXME where is this lock */
1315 /*============================================================
1318 * BIND() System call, which is bound to the AF_WANPIPE
1319 * operations structure. It checks for correct wanpipe
1320 * card name, and cross references interface names with
1321 * the card names. Thus, interface name must belong to
1323 *===========================================================*/
1326 static int wanpipe_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1328 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1329 struct sock *sk=sock->sk;
1330 wanpipe_opt *wp = wp_sk(sk);
1331 struct net_device *dev = NULL;
1339 if (addr_len < sizeof(struct wan_sockaddr_ll)){
1340 printk(KERN_INFO "wansock: Address length error\n");
1343 if (sll->sll_family != AF_WANPIPE){
1344 printk(KERN_INFO "wansock: Illegal family name specified.\n");
1348 card = wanpipe_find_card (sll->sll_card);
1350 printk(KERN_INFO "wansock: Wanpipe card not found: %s\n",sll->sll_card);
1353 wp_sk(sk)->card = (void *)card;
1356 if (!strcmp(sll->sll_device,"svc_listen")){
1358 /* Bind a sock to a card structure for listening
1362 /* This is x25 specific area if protocol doesn't
1363 * match, return error */
1364 if (sll->sll_protocol != htons(X25_PROT))
1367 err= wanpipe_link_card (sk);
1371 if (sll->sll_protocol)
1372 wp->num = sll->sll_protocol;
1373 sk->sk_state = WANSOCK_BIND_LISTEN;
1376 }else if (!strcmp(sll->sll_device,"svc_connect")){
1378 /* This is x25 specific area if protocol doesn't
1379 * match, return error */
1380 if (sll->sll_protocol != htons(X25_PROT))
1383 /* Find a free device
1385 dev = wanpipe_find_free_dev(card);
1387 DBG_PRINTK(KERN_INFO "wansock: No free network devices for card %s\n",
1392 /* Bind a socket to a interface name
1393 * This is used by PVC mostly
1395 strlcpy(name,sll->sll_device,sizeof(name));
1396 dev = dev_get_by_name(name);
1398 printk(KERN_INFO "wansock: Failed to get Dev from name: %s,\n",
1405 if (check_dev(dev, card)){
1406 printk(KERN_INFO "wansock: Device %s, doesn't belong to card %s\n",
1407 dev->name, card->devname);
1410 if (get_atomic_device (dev))
1414 return wanpipe_do_bind(sk, dev, sll->sll_protocol ? : wp->num);
1417 /*============================================================
1420 * Sets a bit atomically which indicates that
1421 * the interface is taken. This avoids race conditions.
1422 *===========================================================*/
1425 static inline int get_atomic_device(struct net_device *dev)
1427 wanpipe_common_t *chan = dev->priv;
1428 if (!test_and_set_bit(0,(void *)&chan->rw_bind)){
1434 /*============================================================
1437 * Check that device name belongs to a particular card.
1438 *===========================================================*/
1440 static int check_dev(struct net_device *dev, sdla_t *card)
1442 struct net_device* tmp_dev;
1444 for (tmp_dev = card->wandev.dev; tmp_dev;
1445 tmp_dev = *((struct net_device **)tmp_dev->priv)) {
1446 if (tmp_dev->ifindex == dev->ifindex){
1453 /*============================================================
1454 * wanpipe_find_free_dev
1456 * Find a free network interface. If found set atomic
1457 * bit indicating that the interface is taken.
1459 *===========================================================*/
1461 struct net_device *wanpipe_find_free_dev(sdla_t *card)
1463 struct net_device* dev;
1464 volatile wanpipe_common_t *chan;
1466 if (test_and_set_bit(0,&find_free_critical)){
1467 printk(KERN_INFO "CRITICAL in Find Free\n");
1470 for (dev = card->wandev.dev; dev;
1471 dev = *((struct net_device **)dev->priv)) {
1475 if (chan->usedby == API && chan->svc){
1476 if (!get_atomic_device (dev)){
1477 if (chan->state != WANSOCK_DISCONNECTED){
1478 release_device(dev);
1480 clear_bit(0,&find_free_critical);
1486 clear_bit(0,&find_free_critical);
1490 /*============================================================
1493 * SOCKET() System call. It allocates a sock structure
1494 * and adds the socket to the wanpipe_sk_list.
1495 * Crates AF_WANPIPE socket.
1496 *===========================================================*/
1498 static int wanpipe_create(struct socket *sock, int protocol)
1502 //FIXME: This checks for root user, SECURITY ?
1503 //if (!capable(CAP_NET_RAW))
1506 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1507 return -ESOCKTNOSUPPORT;
1509 sock->state = SS_UNCONNECTED;
1511 if ((sk = wanpipe_alloc_socket()) == NULL)
1515 sock->ops = &wanpipe_ops;
1516 sock_init_data(sock,sk);
1519 sk->sk_family = PF_WANPIPE;
1520 wp_sk(sk)->num = protocol;
1521 sk->sk_state = WANSOCK_DISCONNECTED;
1522 sk->sk_ack_backlog = 0;
1523 sk->sk_bound_dev_if = 0;
1525 atomic_inc(&wanpipe_socks_nr);
1527 /* We must disable interrupts because the ISR
1528 * can also change the list */
1529 set_bit(1,&wanpipe_tx_critical);
1530 write_lock(&wanpipe_sklist_lock);
1531 sk_add_node(sk, &wanpipe_sklist);
1532 write_unlock(&wanpipe_sklist_lock);
1533 clear_bit(1,&wanpipe_tx_critical);
1539 /*============================================================
1542 * Pull a packet from our receive queue and hand it
1543 * to the user. If necessary we block.
1544 *===========================================================*/
1546 static int wanpipe_recvmsg(struct kiocb *iocb, struct socket *sock,
1547 struct msghdr *msg, int len, int flags)
1549 struct sock *sk = sock->sk;
1550 struct sk_buff *skb;
1551 int copied, err=-ENOBUFS;
1555 * If the address length field is there to be filled in, we fill
1559 msg->msg_namelen = sizeof(struct wan_sockaddr_ll);
1562 * Call the generic datagram receiver. This handles all sorts
1563 * of horrible races and re-entrancy so we can forget about it
1564 * in the protocol layers.
1566 * Now it will return ENETDOWN, if device have just gone down,
1567 * but then it will block.
1570 if (flags & MSG_OOB){
1571 skb = skb_dequeue(&sk->sk_error_queue);
1573 skb=skb_recv_datagram(sk,flags,1,&err);
1576 * An error occurred so return it. Because skb_recv_datagram()
1577 * handles the blocking we don't see and worry about blocking
1585 * You lose any data beyond the buffer you gave. If it worries a
1586 * user program they can ask the device for its MTU anyway.
1593 msg->msg_flags|=MSG_TRUNC;
1596 wanpipe_wakeup_driver(sk);
1598 /* We can't use skb_copy_datagram here */
1599 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
1603 sock_recv_timestamp(msg, sk, skb);
1606 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1609 * Free or return the buffer as appropriate. Again this
1610 * hides all the races and re-entrancy issues from us.
1612 err = (flags&MSG_TRUNC) ? skb->len : copied;
1615 skb_free_datagram(sk, skb);
1621 /*============================================================
1622 * wanpipe_wakeup_driver
1624 * If socket receive buffer is full and driver cannot
1625 * pass data up the sock, it sets a packet_block flag.
1626 * This function check that flag and if sock receive
1627 * queue has room it kicks the driver BH handler.
1629 * This way, driver doesn't have to poll the sock
1631 *===========================================================*/
1633 static void wanpipe_wakeup_driver(struct sock *sk)
1635 struct net_device *dev = NULL;
1636 wanpipe_common_t *chan=NULL;
1638 dev = dev_get_by_index(sk->sk_bound_dev_if);
1644 if ((chan = dev->priv) == NULL)
1647 if (atomic_read(&chan->receive_block)){
1648 if (atomic_read(&sk->sk_rmem_alloc) <
1649 ((unsigned)sk->sk_rcvbuf * 0.9)) {
1650 printk(KERN_INFO "wansock: Queuing task for wanpipe\n");
1651 atomic_set(&chan->receive_block,0);
1652 wanpipe_queue_tq(&chan->wanpipe_task);
1658 /*============================================================
1661 * I don't know what to do with this yet.
1662 * User can use this function to get sock address
1663 * information. Not very useful for Sangoma's purposes.
1664 *===========================================================*/
1667 static int wanpipe_getname(struct socket *sock, struct sockaddr *uaddr,
1668 int *uaddr_len, int peer)
1670 struct net_device *dev;
1671 struct sock *sk = sock->sk;
1672 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1674 sll->sll_family = AF_WANPIPE;
1675 sll->sll_ifindex = sk->sk_bound_dev_if;
1676 sll->sll_protocol = wp_sk(sk)->num;
1677 dev = dev_get_by_index(sk->sk_bound_dev_if);
1679 sll->sll_hatype = dev->type;
1680 sll->sll_halen = dev->addr_len;
1681 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1683 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1686 *uaddr_len = sizeof(*sll);
1693 /*============================================================
1696 * If driver turns off network interface, this function
1697 * will be envoked. Currently I treate it as a
1698 * call disconnect. More thought should go into this
1701 * FIXME: More thought should go into this function.
1703 *===========================================================*/
1705 static int wanpipe_notifier(struct notifier_block *this, unsigned long msg, void *data)
1709 struct net_device *dev = (struct net_device *)data;
1711 sk_for_each(sk, node, &wanpipe_sklist) {
1712 struct wanpipe_opt *po = wp_sk(sk);
1721 case NETDEV_UNREGISTER:
1722 if (dev->ifindex == sk->sk_bound_dev_if) {
1723 printk(KERN_INFO "wansock: Device down %s\n",dev->name);
1724 if (sk->sk_zapped) {
1725 wanpipe_unlink_driver(sk);
1726 sk->sk_err = ENETDOWN;
1727 sk->sk_error_report(sk);
1730 if (msg == NETDEV_UNREGISTER) {
1731 printk(KERN_INFO "wansock: Unregistering Device: %s\n",
1733 wanpipe_unlink_driver(sk);
1734 sk->sk_bound_dev_if = 0;
1739 if (dev->ifindex == sk->sk_bound_dev_if &&
1740 po->num && !sk->sk_zapped) {
1741 printk(KERN_INFO "wansock: Registering Device: %s\n",
1743 wanpipe_link_driver(dev,sk);
1751 /*============================================================
1754 * Execute a user commands, and set socket options.
1756 * FIXME: More thought should go into this function.
1758 *===========================================================*/
1760 static int wanpipe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1762 struct sock *sk = sock->sk;
1768 return sock_get_timestamp(sk, (struct timeval *)arg);
1770 case SIOC_WANPIPE_CHECK_TX:
1772 return atomic_read(&sk->sk_wmem_alloc);
1774 case SIOC_WANPIPE_SOCK_STATE:
1776 if (sk->sk_state == WANSOCK_CONNECTED)
1782 case SIOC_WANPIPE_GET_CALL_DATA:
1784 return get_ioctl_cmd (sk,(void*)arg);
1786 case SIOC_WANPIPE_SET_CALL_DATA:
1788 return set_ioctl_cmd (sk,(void*)arg);
1790 case SIOC_WANPIPE_ACCEPT_CALL:
1791 case SIOC_WANPIPE_CLEAR_CALL:
1792 case SIOC_WANPIPE_RESET_CALL:
1794 if ((err=set_ioctl_cmd(sk,(void*)arg)) < 0)
1797 err=wanpipe_exec_cmd(sk,cmd,0);
1798 get_ioctl_cmd(sk,(void*)arg);
1801 case SIOC_WANPIPE_DEBUG:
1803 return wanpipe_debug(sk,(void*)arg);
1805 case SIOC_WANPIPE_SET_NONBLOCK:
1807 if (sk->sk_state != WANSOCK_DISCONNECTED)
1810 sock->file->f_flags |= O_NONBLOCK;
1824 case SIOCGIFBRDADDR:
1825 case SIOCSIFBRDADDR:
1826 case SIOCGIFNETMASK:
1827 case SIOCSIFNETMASK:
1828 case SIOCGIFDSTADDR:
1829 case SIOCSIFDSTADDR:
1831 return inet_dgram_ops.ioctl(sock, cmd, arg);
1835 return dev_ioctl(cmd,(void *) arg);
1840 /*============================================================
1843 * This function will pass up information about all
1846 * FIXME: More thought should go into this function.
1848 *===========================================================*/
1850 static int wanpipe_debug (struct sock *origsk, void *arg)
1853 struct hlist_node *node;
1854 struct net_device *dev = NULL;
1855 wanpipe_common_t *chan=NULL;
1857 wan_debug_t *dbg_data = (wan_debug_t *)arg;
1859 sk_for_each(sk, node, &wanpipe_sklist) {
1860 wanpipe_opt *wp = wp_sk(sk);
1866 if ((err=put_user(1, &dbg_data->debug[cnt].free)))
1868 if ((err = put_user(sk->sk_state,
1869 &dbg_data->debug[cnt].state_sk)))
1871 if ((err = put_user(sk->sk_rcvbuf,
1872 &dbg_data->debug[cnt].rcvbuf)))
1874 if ((err = put_user(atomic_read(&sk->sk_rmem_alloc),
1875 &dbg_data->debug[cnt].rmem)))
1877 if ((err = put_user(atomic_read(&sk->sk_wmem_alloc),
1878 &dbg_data->debug[cnt].wmem)))
1880 if ((err = put_user(sk->sk_sndbuf,
1881 &dbg_data->debug[cnt].sndbuf)))
1883 if ((err=put_user(sk_count, &dbg_data->debug[cnt].sk_count)))
1885 if ((err=put_user(wp->poll_cnt, &dbg_data->debug[cnt].poll_cnt)))
1887 if ((err = put_user(sk->sk_bound_dev_if,
1888 &dbg_data->debug[cnt].bound)))
1891 if (sk->sk_bound_dev_if) {
1892 dev = dev_get_by_index(sk->sk_bound_dev_if);
1899 if ((err=put_user(chan->state, &dbg_data->debug[cnt].d_state)))
1901 if ((err=put_user(chan->svc, &dbg_data->debug[cnt].svc)))
1904 if ((err=put_user(atomic_read(&chan->command),
1905 &dbg_data->debug[cnt].command)))
1910 sdla_t *card = (sdla_t*)wp->card;
1913 if ((err=put_user(atomic_read(&card->u.x.command_busy),
1914 &dbg_data->debug[cnt].cmd_busy)))
1918 if ((err=put_user(wp->lcn,
1919 &dbg_data->debug[cnt].lcn)))
1923 if ((err=put_user(1, &dbg_data->debug[cnt].mbox)))
1928 if ((err=put_user(atomic_read(&chan->receive_block),
1929 &dbg_data->debug[cnt].rblock)))
1932 if (copy_to_user(dbg_data->debug[cnt].name, dev->name, strlen(dev->name)))
1936 if (++cnt == MAX_NUM_DEBUG)
1942 /*============================================================
1945 * Pass up the contents of socket MBOX to the user.
1946 *===========================================================*/
1948 static int get_ioctl_cmd (struct sock *sk, void *arg)
1950 x25api_t *usr_data = (x25api_t *)arg;
1951 mbox_cmd_t *mbox_ptr;
1954 if (usr_data == NULL)
1957 if (!wp_sk(sk)->mbox) {
1961 mbox_ptr = (mbox_cmd_t *)wp_sk(sk)->mbox;
1963 if ((err=put_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
1965 if ((err=put_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
1967 if ((err=put_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
1969 if ((err=put_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
1971 if ((err=put_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
1973 if ((err=put_user(mbox_ptr->cmd.lcn, &usr_data->hdr.lcn)))
1976 if (mbox_ptr->cmd.length > 0){
1977 if (mbox_ptr->cmd.length > X25_MAX_DATA)
1980 if (copy_to_user(usr_data->data, mbox_ptr->data, mbox_ptr->cmd.length)){
1981 printk(KERN_INFO "wansock: Copy failed !!!\n");
1988 /*============================================================
1991 * Before command can be execute, socket MBOX must
1992 * be created, and initialized with user data.
1993 *===========================================================*/
1995 static int set_ioctl_cmd (struct sock *sk, void *arg)
1997 x25api_t *usr_data = (x25api_t *)arg;
1998 mbox_cmd_t *mbox_ptr;
2001 if (!wp_sk(sk)->mbox) {
2003 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2009 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL)
2012 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2013 wp_sk(sk)->mbox = mbox_ptr;
2015 wanpipe_link_driver(dev,sk);
2018 mbox_ptr = (mbox_cmd_t*)wp_sk(sk)->mbox;
2019 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2021 if (usr_data == NULL){
2024 if ((err=get_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
2026 if ((err=get_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
2028 if ((err=get_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
2030 if ((err=get_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
2032 if ((err=get_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
2035 if (mbox_ptr->cmd.length > 0){
2036 if (mbox_ptr->cmd.length > X25_MAX_DATA)
2039 if (copy_from_user(mbox_ptr->data, usr_data->data, mbox_ptr->cmd.length)){
2040 printk(KERN_INFO "Copy failed\n");
2048 /*======================================================================
2051 * Datagram poll: Again totally generic. This also handles
2052 * sequenced packet sockets providing the socket receive queue
2053 * is only ever holding data ready to receive.
2055 * Note: when you _don't_ use this routine for this protocol,
2056 * and you use a different write policy from sock_writeable()
2057 * then please supply your own write_space callback.
2058 *=====================================================================*/
2060 unsigned int wanpipe_poll(struct file * file, struct socket *sock, poll_table *wait)
2062 struct sock *sk = sock->sk;
2065 ++wp_sk(sk)->poll_cnt;
2067 poll_wait(file, sk->sk_sleep, wait);
2070 /* exceptional events? */
2071 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) {
2075 if (sk->sk_shutdown & RCV_SHUTDOWN)
2079 if (!skb_queue_empty(&sk->sk_receive_queue)) {
2080 mask |= POLLIN | POLLRDNORM;
2083 /* connection hasn't started yet */
2084 if (sk->sk_state == WANSOCK_CONNECTING) {
2088 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2093 /* This check blocks the user process if there is
2094 * a packet already queued in the socket write queue.
2095 * This option is only for X25API protocol, for other
2096 * protocol like chdlc enable streaming mode,
2097 * where multiple packets can be pending in the socket
2100 if (wp_sk(sk)->num == htons(X25_PROT)) {
2101 if (atomic_read(&wp_sk(sk)->packet_sent))
2106 if (sock_writeable(sk)){
2107 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2109 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2115 /*======================================================================
2118 * X25API Specific function. Set a socket into LISTENING MODE.
2119 *=====================================================================*/
2122 static int wanpipe_listen(struct socket *sock, int backlog)
2124 struct sock *sk = sock->sk;
2126 /* This is x25 specific area if protocol doesn't
2127 * match, return error */
2128 if (wp_sk(sk)->num != htons(X25_PROT))
2131 if (sk->sk_state == WANSOCK_BIND_LISTEN) {
2133 sk->sk_max_ack_backlog = backlog;
2134 sk->sk_state = WANSOCK_LISTEN;
2137 printk(KERN_INFO "wansock: Listening sock was not binded\n");
2143 /*======================================================================
2146 * Connects the listening socket to the driver
2147 *=====================================================================*/
2149 static int wanpipe_link_card (struct sock *sk)
2151 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2156 if ((card->sk != NULL) || (card->func != NULL)){
2157 printk(KERN_INFO "wansock: Listening queue is already established\n");
2162 card->func=wanpipe_listen_rcv;
2168 /*======================================================================
2171 * X25API Specific function. Disconnect listening socket from
2173 *=====================================================================*/
2175 static void wanpipe_unlink_card (struct sock *sk)
2177 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2185 /*======================================================================
2188 * Ioctl function calls this function to execute user command.
2189 * Connect() sytem call also calls this function to execute
2190 * place call. This function blocks until command is executed.
2191 *=====================================================================*/
2193 static int wanpipe_exec_cmd(struct sock *sk, int cmd, unsigned int flags)
2196 wanpipe_opt *wp = wp_sk(sk);
2197 mbox_cmd_t *mbox_ptr = (mbox_cmd_t*)wp->mbox;
2200 printk(KERN_INFO "NO MBOX PTR !!!!!\n");
2204 /* This is x25 specific area if protocol doesn't
2205 * match, return error */
2206 if (wp->num != htons(X25_PROT))
2212 case SIOC_WANPIPE_ACCEPT_CALL:
2214 if (sk->sk_state != WANSOCK_CONNECTING) {
2219 err = execute_command(sk,X25_ACCEPT_CALL,0);
2223 /* Update. Mar6 2000.
2224 * Do not set the sock lcn number here, since
2225 * it is done in wanpipe_listen_rcv().
2227 if (sk->sk_state == WANSOCK_CONNECTED) {
2228 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2229 DBG_PRINTK(KERN_INFO "\nwansock: Accept OK %i\n",
2234 DBG_PRINTK (KERN_INFO "\nwansock: Accept Failed %i\n",
2237 err = -ECONNREFUSED;
2241 case SIOC_WANPIPE_CLEAR_CALL:
2243 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2249 /* Check if data buffers are pending for transmission,
2250 * if so, check whether user wants to wait until data
2251 * is transmitted, or clear a call and drop packets */
2253 if (atomic_read(&sk->sk_wmem_alloc) ||
2254 check_driver_busy(sk)) {
2255 mbox_cmd_t *mbox = wp->mbox;
2256 if (mbox->cmd.qdm & 0x80){
2257 mbox->cmd.result = 0x35;
2263 sk->sk_state = WANSOCK_DISCONNECTING;
2265 err = execute_command(sk,X25_CLEAR_CALL,0);
2269 err = -ECONNREFUSED;
2270 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2271 DBG_PRINTK(KERN_INFO "\nwansock: CLEAR OK %i\n",
2278 case SIOC_WANPIPE_RESET_CALL:
2280 if (sk->sk_state != WANSOCK_CONNECTED) {
2286 /* Check if data buffers are pending for transmission,
2287 * if so, check whether user wants to wait until data
2288 * is transmitted, or reset a call and drop packets */
2290 if (atomic_read(&sk->sk_wmem_alloc) ||
2291 check_driver_busy(sk)) {
2292 mbox_cmd_t *mbox = wp->mbox;
2293 if (mbox->cmd.qdm & 0x80){
2294 mbox->cmd.result = 0x35;
2301 err = execute_command(sk, X25_RESET,0);
2305 err = mbox_ptr->cmd.result;
2309 case X25_PLACE_CALL:
2311 err=execute_command(sk,X25_PLACE_CALL,flags);
2315 if (sk->sk_state == WANSOCK_CONNECTED) {
2317 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2319 DBG_PRINTK(KERN_INFO "\nwansock: PLACE CALL OK %i\n",
2323 } else if (sk->sk_state == WANSOCK_CONNECTING &&
2324 (flags & O_NONBLOCK)) {
2325 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2326 DBG_PRINTK(KERN_INFO "\nwansock: Place Call OK: Waiting %i\n",
2332 DBG_PRINTK(KERN_INFO "\nwansock: Place call Failed\n");
2333 err = -ECONNREFUSED;
2345 static int check_driver_busy (struct sock *sk)
2347 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2348 wanpipe_common_t *chan;
2355 if ((chan=dev->priv) == NULL)
2358 return atomic_read(&chan->driver_busy);
2362 /*======================================================================
2365 * ACCEPT() System call. X25API Specific function.
2366 * For each incoming call, create a new socket and
2367 * return it to the user.
2368 *=====================================================================*/
2370 static int wanpipe_accept(struct socket *sock, struct socket *newsock, int flags)
2374 struct sk_buff *skb;
2375 DECLARE_WAITQUEUE(wait, current);
2378 if (newsock->sk != NULL){
2379 wanpipe_kill_sock_accept(newsock->sk);
2383 if ((sk = sock->sk) == NULL)
2386 if (sk->sk_type != SOCK_RAW)
2389 if (sk->sk_state != WANSOCK_LISTEN)
2392 if (wp_sk(sk)->num != htons(X25_PROT))
2395 add_wait_queue(sk->sk_sleep,&wait);
2396 current->state = TASK_INTERRUPTIBLE;
2398 skb = skb_dequeue(&sk->sk_receive_queue);
2403 if (signal_pending(current)) {
2409 current->state = TASK_RUNNING;
2410 remove_wait_queue(sk->sk_sleep,&wait);
2415 newsk = get_newsk_from_skb(skb);
2420 set_bit(1,&wanpipe_tx_critical);
2421 write_lock(&wanpipe_sklist_lock);
2422 sk_add_node(newsk, &wanpipe_sklist);
2423 write_unlock(&wanpipe_sklist_lock);
2424 clear_bit(1,&wanpipe_tx_critical);
2426 newsk->sk_pair = NULL;
2427 newsk->sk_socket = newsock;
2428 newsk->sk_sleep = &newsock->wait;
2430 /* Now attach up the new socket */
2431 sk->sk_ack_backlog--;
2432 newsock->sk = newsk;
2436 DBG_PRINTK(KERN_INFO "\nwansock: ACCEPT Got LCN %i\n",
2441 /*======================================================================
2442 * get_newsk_from_skb
2444 * Accept() uses this function to get the address of the new
2446 *=====================================================================*/
2448 struct sock * get_newsk_from_skb (struct sk_buff *skb)
2450 struct net_device *dev = skb->dev;
2451 wanpipe_common_t *chan;
2457 if ((chan = dev->priv) == NULL){
2464 return (struct sock *)chan->sk;
2467 /*======================================================================
2470 * CONNECT() System Call. X25API specific function
2471 * Check the state of the sock, and execute PLACE_CALL command.
2472 * Connect can ether block or return without waiting for connection,
2473 * if specified by user.
2474 *=====================================================================*/
2476 static int wanpipe_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
2478 struct sock *sk = sock->sk;
2479 struct wan_sockaddr_ll *addr = (struct wan_sockaddr_ll*)uaddr;
2480 struct net_device *dev;
2483 if (wp_sk(sk)->num != htons(X25_PROT))
2486 if (sk->sk_state == WANSOCK_CONNECTED)
2487 return -EISCONN; /* No reconnect on a seqpacket socket */
2489 if (sk->sk_state != WAN_DISCONNECTED) {
2490 printk(KERN_INFO "wansock: Trying to connect on channel NON DISCONNECT\n");
2491 return -ECONNREFUSED;
2494 sk->sk_state = WANSOCK_DISCONNECTED;
2495 sock->state = SS_UNCONNECTED;
2497 if (addr_len != sizeof(struct wan_sockaddr_ll))
2500 if (addr->sll_family != AF_WANPIPE)
2503 if ((dev = dev_get_by_index(sk->sk_bound_dev_if)) == NULL)
2504 return -ENETUNREACH;
2508 if (!sk->sk_zapped) /* Must bind first - autobinding does not work */
2511 sock->state = SS_CONNECTING;
2512 sk->sk_state = WANSOCK_CONNECTING;
2514 if (!wp_sk(sk)->mbox) {
2515 if (wp_sk (sk)->svc)
2519 if ((err=set_ioctl_cmd(sk,NULL)) < 0)
2524 if ((err=wanpipe_exec_cmd(sk, X25_PLACE_CALL,flags)) != 0){
2525 sock->state = SS_UNCONNECTED;
2526 sk->sk_state = WANSOCK_CONNECTED;
2530 if (sk->sk_state != WANSOCK_CONNECTED && (flags & O_NONBLOCK)) {
2534 if (sk->sk_state != WANSOCK_CONNECTED) {
2535 sock->state = SS_UNCONNECTED;
2536 return -ECONNREFUSED;
2539 sock->state = SS_CONNECTED;
2543 struct proto_ops wanpipe_ops = {
2544 .family = PF_WANPIPE,
2545 .owner = THIS_MODULE,
2546 .release = wanpipe_release,
2547 .bind = wanpipe_bind,
2548 .connect = wanpipe_connect,
2549 .socketpair = sock_no_socketpair,
2550 .accept = wanpipe_accept,
2551 .getname = wanpipe_getname,
2552 .poll = wanpipe_poll,
2553 .ioctl = wanpipe_ioctl,
2554 .listen = wanpipe_listen,
2555 .shutdown = sock_no_shutdown,
2556 .setsockopt = sock_no_setsockopt,
2557 .getsockopt = sock_no_getsockopt,
2558 .sendmsg = wanpipe_sendmsg,
2559 .recvmsg = wanpipe_recvmsg
2562 static struct net_proto_family wanpipe_family_ops = {
2563 .family = PF_WANPIPE,
2564 .create = wanpipe_create,
2565 .owner = THIS_MODULE,
2568 struct notifier_block wanpipe_netdev_notifier = {
2569 .notifier_call = wanpipe_notifier,
2574 void cleanup_module(void)
2576 printk(KERN_INFO "wansock: Cleaning up \n");
2577 unregister_netdevice_notifier(&wanpipe_netdev_notifier);
2578 sock_unregister(PF_WANPIPE);
2583 int init_module(void)
2586 printk(KERN_INFO "wansock: Registering Socket \n");
2587 sock_register(&wanpipe_family_ops);
2588 register_netdevice_notifier(&wanpipe_netdev_notifier);
2592 MODULE_LICENSE("GPL");
2593 MODULE_ALIAS_NETPROTO(PF_WANPIPE);