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/types.h>
36 #include <linux/sched.h>
38 #include <linux/capability.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/if_wanpipe.h>
58 #include <linux/pkt_sched.h>
59 #include <linux/tcp_states.h>
60 #include <linux/if_wanpipe_common.h>
63 #include <net/inet_common.h>
66 #define SLOW_BACKOFF 0.1*HZ
67 #define FAST_BACKOFF 0.01*HZ
71 #define DBG_PRINTK(format, a...) printk(format, ## a)
73 #define DBG_PRINTK(format, a...)
77 /* SECURE SOCKET IMPLEMENTATION
81 * When the user sends a packet via send() system call
82 * the wanpipe_sendmsg() function is executed.
84 * Each packet is enqueud into sk->sk_write_queue transmit
85 * queue. When the packet is enqueued, a delayed transmit
86 * timer is triggerd which acts as a Bottom Half hander.
88 * wanpipe_delay_transmit() function (BH), dequeues packets
89 * from the sk->sk_write_queue transmit queue and sends it
90 * to the deriver via dev->hard_start_xmit(skb, dev) function.
91 * Note, this function is actual a function pointer of if_send()
92 * routine in the wanpipe driver.
94 * X25API GUARANTEED DELIVERY:
96 * In order to provide 100% guaranteed packet delivery,
97 * an atomic 'packet_sent' counter is implemented. Counter
98 * is incremented for each packet enqueued
99 * into sk->sk_write_queue. Counter is decremented each
100 * time wanpipe_delayed_transmit() function successfuly
101 * passes the packet to the driver. Before each send(), a poll
102 * routine checks the sock resources The maximum value of
103 * packet sent counter is 1, thus if one packet is queued, the
104 * application will block until that packet is passed to the
109 * Wanpipe device drivers call the socket bottom half
110 * function, wanpipe_rcv() to queue the incoming packets
111 * into an AF_WANPIPE socket queue. Based on wanpipe_rcv()
112 * return code, the driver knows whether the packet was
113 * successfully queued. If the socket queue is full,
114 * protocol flow control is used by the driver, if any,
115 * to slow down the traffic until the sock queue is free.
117 * Every time a packet arrives into a socket queue the
118 * socket wakes up processes which are waiting to receive
121 * If the socket queue is full, the driver sets a block
122 * bit which signals the socket to kick the wanpipe driver
123 * bottom half hander when the socket queue is partialy
124 * empty. wanpipe_recvmsg() function performs this action.
126 * In case of x25api, packets will never be dropped, since
127 * flow control is available.
129 * In case of streaming protocols like CHDLC, packets will
130 * be dropped but the statistics will be generated.
134 /* The code below is used to test memory leaks. It prints out
135 * a message every time kmalloc and kfree system calls get executed.
136 * If the calls match there is no leak :)
139 /***********FOR DEBUGGING PURPOSES*********************************************
140 #define KMEM_SAFETYZONE 8
142 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
143 void * v = kmalloc(size,prio);
144 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
147 static void dbg_kfree(void * v, int line) {
148 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
152 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
153 #define kfree(x) dbg_kfree(x,__LINE__)
154 ******************************************************************************/
157 /* List of all wanpipe sockets. */
158 HLIST_HEAD(wanpipe_sklist);
159 static DEFINE_RWLOCK(wanpipe_sklist_lock);
161 atomic_t wanpipe_socks_nr;
162 static unsigned long wanpipe_tx_critical;
165 /* Private wanpipe socket structures. */
168 void *mbox; /* Mail box */
169 void *card; /* Card bouded to */
170 struct net_device *dev; /* Bounded device */
171 unsigned short lcn; /* Binded LCN */
172 unsigned char svc; /* 0=pvc, 1=svc */
173 unsigned char timer; /* flag for delayed transmit*/
174 struct timer_list tx_timer;
176 unsigned char force; /* Used to force sock release */
177 atomic_t packet_sent;
182 extern const struct proto_ops wanpipe_ops;
183 static unsigned long find_free_critical;
185 static void wanpipe_unlink_driver(struct sock *sk);
186 static void wanpipe_link_driver(struct net_device *dev, struct sock *sk);
187 static void wanpipe_wakeup_driver(struct sock *sk);
188 static int execute_command(struct sock *, unsigned char, unsigned int);
189 static int check_dev(struct net_device *dev, sdla_t *card);
190 struct net_device *wanpipe_find_free_dev(sdla_t *card);
191 static void wanpipe_unlink_card (struct sock *);
192 static int wanpipe_link_card (struct sock *);
193 static struct sock *wanpipe_make_new(struct sock *);
194 static struct sock *wanpipe_alloc_socket(void);
195 static inline int get_atomic_device(struct net_device *dev);
196 static int wanpipe_exec_cmd(struct sock *, int, unsigned int);
197 static int get_ioctl_cmd (struct sock *, void *);
198 static int set_ioctl_cmd (struct sock *, void *);
199 static void release_device(struct net_device *dev);
200 static void wanpipe_kill_sock_timer (unsigned long data);
201 static void wanpipe_kill_sock_irq (struct sock *);
202 static void wanpipe_kill_sock_accept (struct sock *);
203 static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
205 struct sock * get_newsk_from_skb (struct sk_buff *);
206 static int wanpipe_debug (struct sock *, void *);
207 static void wanpipe_delayed_transmit (unsigned long data);
208 static void release_driver(struct sock *);
209 static void start_cleanup_timer (struct sock *);
210 static void check_write_queue(struct sock *);
211 static int check_driver_busy (struct sock *);
213 /*============================================================
216 * Wanpipe socket bottom half handler. This function
217 * is called by the WANPIPE device drivers to queue a
218 * incoming packet into the socket receive queue.
219 * Once the packet is queued, all processes waiting to
222 * During socket bind, this function is bounded into
223 * WANPIPE driver private.
224 *===========================================================*/
226 static int wanpipe_rcv(struct sk_buff *skb, struct net_device *dev,
229 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
230 wanpipe_common_t *chan = dev->priv;
232 * When we registered the protocol we saved the socket in the data
233 * field for just this event.
238 sll->sll_family = AF_WANPIPE;
239 sll->sll_hatype = dev->type;
240 sll->sll_protocol = skb->protocol;
241 sll->sll_pkttype = skb->pkt_type;
242 sll->sll_ifindex = dev->ifindex;
245 if (dev->hard_header_parse)
246 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
249 * WAN_PACKET_DATA : Data which should be passed up the receive queue.
250 * WAN_PACKET_ASYC : Asynchronous data like place call, which should
251 * be passed up the listening sock.
252 * WAN_PACKET_ERR : Asynchronous data like clear call or restart
253 * which should go into an error queue.
255 switch (skb->pkt_type){
257 case WAN_PACKET_DATA:
258 if (sock_queue_rcv_skb(sk,skb)<0){
263 sk->sk_state = chan->state;
264 /* Bug fix: update Mar6.
265 * Do not set the sock lcn number here, since
266 * cmd is not guaranteed to be executed on the
267 * board, thus Lcn could be wrong */
268 sk->sk_data_ready(sk, skb->len);
272 sk->sk_state = chan->state;
273 if (sock_queue_err_skb(sk,skb)<0){
278 printk(KERN_INFO "wansock: BH Illegal Packet Type Dropping\n");
283 //??????????????????????
284 // if (sk->sk_state == WANSOCK_DISCONNECTED){
285 // if (sk->sk_zapped) {
286 // //printk(KERN_INFO "wansock: Disconnected, killing early\n");
287 // wanpipe_unlink_driver(sk);
288 // sk->sk_bound_dev_if = 0;
295 /*============================================================
298 * Wanpipe LISTEN socket bottom half handler. This function
299 * is called by the WANPIPE device drivers to queue an
300 * incoming call into the socket listening queue.
301 * Once the packet is queued, the waiting accept() process
304 * During socket bind, this function is bounded into
305 * WANPIPE driver private.
308 * The accept call() is waiting for an skb packet
309 * which contains a pointer to a device structure.
311 * When we do a bind to a device structre, we
312 * bind a newly created socket into "chan->sk". Thus,
313 * when accept receives the skb packet, it will know
314 * from which dev it came form, and in turn it will know
315 * the address of the new sock.
317 * NOTE: This function gets called from driver ISR.
318 *===========================================================*/
320 static int wanpipe_listen_rcv (struct sk_buff *skb, struct sock *sk)
322 wanpipe_opt *wp = wp_sk(sk), *newwp;
323 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
325 struct net_device *dev;
327 mbox_cmd_t *mbox_ptr;
328 wanpipe_common_t *chan;
330 /* Find a free device, if none found, all svc's are busy
333 card = (sdla_t*)wp->card;
335 printk(KERN_INFO "wansock: LISTEN ERROR, No Card\n");
339 dev = wanpipe_find_free_dev(card);
341 printk(KERN_INFO "wansock: LISTEN ERROR, No Free Device\n");
346 chan->state = WANSOCK_CONNECTING;
348 /* Allocate a new sock, which accept will bind
349 * and pass up to the user
351 if ((newsk = wanpipe_make_new(sk)) == NULL){
357 /* Initialize the new sock structure
359 newsk->sk_bound_dev_if = dev->ifindex;
360 newwp = wp_sk(newsk);
361 newwp->card = wp->card;
363 /* Insert the sock into the main wanpipe
366 atomic_inc(&wanpipe_socks_nr);
368 /* Allocate and fill in the new Mail Box. Then
369 * bind the mail box to the sock. It will be
370 * used by the ioctl call to read call information
371 * and to execute commands.
373 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL) {
374 wanpipe_kill_sock_irq (newsk);
378 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
379 memcpy(mbox_ptr,skb->data,skb->len);
381 /* Register the lcn on which incoming call came
382 * from. Thus, if we have to clear it, we know
386 newwp->lcn = mbox_ptr->cmd.lcn;
387 newwp->mbox = (void *)mbox_ptr;
389 DBG_PRINTK(KERN_INFO "NEWSOCK : Device %s, bind to lcn %i\n",
390 dev->name,mbox_ptr->cmd.lcn);
392 chan->lcn = mbox_ptr->cmd.lcn;
393 card->u.x.svc_to_dev_map[(chan->lcn%MAX_X25_LCN)] = dev;
395 sock_reset_flag(newsk, SOCK_ZAPPED);
396 newwp->num = htons(X25_PROT);
398 if (wanpipe_do_bind(newsk, dev, newwp->num)) {
399 wanpipe_kill_sock_irq (newsk);
403 newsk->sk_state = WANSOCK_CONNECTING;
406 /* Fill in the standard sock address info */
408 sll->sll_family = AF_WANPIPE;
409 sll->sll_hatype = dev->type;
410 sll->sll_protocol = skb->protocol;
411 sll->sll_pkttype = skb->pkt_type;
412 sll->sll_ifindex = dev->ifindex;
416 sk->sk_ack_backlog++;
418 /* We must do this manually, since the sock_queue_rcv_skb()
419 * function sets the skb->dev to NULL. However, we use
420 * the dev field in the accept function.*/
421 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
422 (unsigned)sk->sk_rcvbuf) {
424 wanpipe_unlink_driver(newsk);
425 wanpipe_kill_sock_irq (newsk);
426 --sk->sk_ack_backlog;
430 skb_set_owner_r(skb, sk);
431 skb_queue_tail(&sk->sk_receive_queue, skb);
432 sk->sk_data_ready(sk, skb->len);
439 /*============================================================
442 * Create a new sock, and allocate a wanpipe private
443 * structure to it. Also, copy the important data
444 * from the original sock to the new sock.
446 * This function is used by wanpipe_listen_rcv() listen
447 * bottom half handler. A copy of the listening sock
448 * is created using this function.
450 *===========================================================*/
452 static struct sock *wanpipe_make_new(struct sock *osk)
456 if (osk->sk_type != SOCK_RAW)
459 if ((sk = wanpipe_alloc_socket()) == NULL)
462 sk->sk_type = osk->sk_type;
463 sk->sk_socket = osk->sk_socket;
464 sk->sk_priority = osk->sk_priority;
465 sk->sk_protocol = osk->sk_protocol;
466 wp_sk(sk)->num = wp_sk(osk)->num;
467 sk->sk_rcvbuf = osk->sk_rcvbuf;
468 sk->sk_sndbuf = osk->sk_sndbuf;
469 sk->sk_state = WANSOCK_CONNECTING;
470 sk->sk_sleep = osk->sk_sleep;
472 if (sock_flag(osk, SOCK_DBG))
473 sock_set_flag(sk, SOCK_DBG);
479 * FIXME: wanpipe_opt has to include a sock in its definition and stop using
480 * sk_protinfo, but this code is not even compilable now, so lets leave it for
483 static struct proto wanpipe_proto = {
485 .owner = THIS_MODULE,
486 .obj_size = sizeof(struct sock),
489 /*============================================================
492 * Allocate memory for the a new sock, and sock
495 * Increment the module use count.
497 * This function is used by wanpipe_create() and
498 * wanpipe_make_new() functions.
500 *===========================================================*/
502 static struct sock *wanpipe_alloc_socket(void)
505 struct wanpipe_opt *wan_opt;
507 if ((sk = sk_alloc(PF_WANPIPE, GFP_ATOMIC, &wanpipe_proto, 1)) == NULL)
510 if ((wan_opt = kmalloc(sizeof(struct wanpipe_opt), GFP_ATOMIC)) == NULL) {
514 memset(wan_opt, 0x00, sizeof(struct wanpipe_opt));
518 /* Use timer to send data to the driver. This will act
519 * as a BH handler for sendmsg functions */
520 init_timer(&wan_opt->tx_timer);
521 wan_opt->tx_timer.data = (unsigned long)sk;
522 wan_opt->tx_timer.function = wanpipe_delayed_transmit;
524 sock_init_data(NULL, sk);
529 /*============================================================
532 * This function implements a sendto() system call,
533 * for AF_WANPIPE socket family.
534 * During socket bind() sk->sk_bound_dev_if is initialized
535 * to a correct network device. This number is used
536 * to find a network device to which the packet should
539 * Each packet is queued into sk->sk_write_queue and
540 * delayed transmit bottom half handler is marked for
543 * A socket must be in WANSOCK_CONNECTED state before
544 * a packet is queued into sk->sk_write_queue.
545 *===========================================================*/
547 static int wanpipe_sendmsg(struct kiocb *iocb, struct socket *sock,
548 struct msghdr *msg, int len)
551 struct sock *sk = sock->sk;
552 struct wan_sockaddr_ll *saddr=(struct wan_sockaddr_ll *)msg->msg_name;
554 struct net_device *dev;
555 unsigned short proto;
557 int ifindex, err, reserve = 0;
560 if (!sock_flag(sk, SOCK_ZAPPED))
563 if (sk->sk_state != WANSOCK_CONNECTED)
566 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
569 /* it was <=, now one can send
570 * zero length packets */
571 if (len < sizeof(x25api_hdr_t))
577 ifindex = sk->sk_bound_dev_if;
582 if (msg->msg_namelen < sizeof(struct wan_sockaddr_ll)){
586 ifindex = sk->sk_bound_dev_if;
587 proto = saddr->sll_protocol;
588 addr = saddr->sll_addr;
591 dev = dev_get_by_index(ifindex);
593 printk(KERN_INFO "wansock: Send failed, dev index: %i\n",ifindex);
598 if (sock->type == SOCK_RAW)
599 reserve = dev->hard_header_len;
601 if (len > dev->mtu+reserve){
605 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
606 msg->msg_flags & MSG_DONTWAIT, &err);
612 skb_reserve(skb, LL_RESERVED_SPACE(dev));
613 skb->nh.raw = skb->data;
615 /* Returns -EFAULT on error */
616 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
621 if (dev->hard_header) {
624 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
630 skb->protocol = proto;
632 skb->priority = sk->sk_priority;
633 skb->pkt_type = WAN_PACKET_DATA;
636 if (!(dev->flags & IFF_UP))
639 if (atomic_read(&sk->sk_wmem_alloc) + skb->truesize >
640 (unsigned int)sk->sk_sndbuf){
645 skb_queue_tail(&sk->sk_write_queue,skb);
646 atomic_inc(&wp->packet_sent);
648 if (!(test_and_set_bit(0, &wp->timer)))
649 mod_timer(&wp->tx_timer, jiffies + 1);
659 /*============================================================
660 * wanpipe_delayed_tarnsmit
662 * Transmit bottom half handler. It dequeues packets
663 * from sk->sk_write_queue and passes them to the
664 * driver. If the driver is busy, the packet is
667 * Packet Sent counter is decremented on successful
669 *===========================================================*/
672 static void wanpipe_delayed_transmit (unsigned long data)
674 struct sock *sk=(struct sock *)data;
676 wanpipe_opt *wp = wp_sk(sk);
677 struct net_device *dev = wp->dev;
678 sdla_t *card = (sdla_t*)wp->card;
681 clear_bit(0, &wp->timer);
682 DBG_PRINTK(KERN_INFO "wansock: Transmit delay, no dev or card\n");
686 if (sk->sk_state != WANSOCK_CONNECTED || !sock_flag(sk, SOCK_ZAPPED)) {
687 clear_bit(0, &wp->timer);
688 DBG_PRINTK(KERN_INFO "wansock: Tx Timer, State not CONNECTED\n");
692 /* If driver is executing command, we must offload
693 * the board by not sending data. Otherwise a
694 * pending command will never get a free buffer
696 if (atomic_read(&card->u.x.command_busy)){
697 wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
698 add_timer(&wp->tx_timer);
699 DBG_PRINTK(KERN_INFO "wansock: Tx Timer, command bys BACKOFF\n");
704 if (test_and_set_bit(0,&wanpipe_tx_critical)){
705 printk(KERN_INFO "WanSock: Tx timer critical %s\n",dev->name);
706 wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
707 add_timer(&wp->tx_timer);
711 /* Check for a packet in the fifo and send */
712 if ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL){
714 if (dev->hard_start_xmit(skb, dev) != 0){
716 /* Driver failed to transmit, re-enqueue
717 * the packet and retry again later */
718 skb_queue_head(&sk->sk_write_queue,skb);
719 clear_bit(0,&wanpipe_tx_critical);
723 /* Packet Sent successful. Check for more packets
724 * if more packets, re-trigger the transmit routine
727 atomic_dec(&wp->packet_sent);
729 if (skb_peek(&sk->sk_write_queue) == NULL) {
730 /* If there is nothing to send, kick
731 * the poll routine, which will trigger
732 * the application to send more data */
733 sk->sk_data_ready(sk, 0);
734 clear_bit(0, &wp->timer);
736 /* Reschedule as fast as possible */
737 wp->tx_timer.expires = jiffies + 1;
738 add_timer(&wp->tx_timer);
742 clear_bit(0,&wanpipe_tx_critical);
745 /*============================================================
748 * Execute x25api commands. The atomic variable
749 * chan->command is used to indicate to the driver that
750 * command is pending for execution. The acutal command
751 * structure is placed into a sock mbox structure
754 * The sock private structure, mbox is
755 * used as shared memory between sock and the driver.
756 * Driver uses the sock mbox to execute the command
757 * and return the result.
759 * For all command except PLACE CALL, the function
760 * waits for the result. PLACE CALL can be ether
761 * blocking or nonblocking. The user sets this option
763 *===========================================================*/
766 static int execute_command(struct sock *sk, unsigned char cmd, unsigned int flags)
768 wanpipe_opt *wp = wp_sk(sk);
769 struct net_device *dev;
770 wanpipe_common_t *chan=NULL;
772 DECLARE_WAITQUEUE(wait, current);
774 dev = dev_get_by_index(sk->sk_bound_dev_if);
776 printk(KERN_INFO "wansock: Exec failed no dev %i\n",
777 sk->sk_bound_dev_if);
782 if ((chan=dev->priv) == NULL){
783 printk(KERN_INFO "wansock: Exec cmd failed no priv area\n");
787 if (atomic_read(&chan->command)){
788 printk(KERN_INFO "wansock: ERROR: Command already running %x, %s\n",
789 atomic_read(&chan->command),dev->name);
794 printk(KERN_INFO "wansock: In execute without MBOX\n");
798 ((mbox_cmd_t*)wp->mbox)->cmd.command = cmd;
799 ((mbox_cmd_t*)wp->mbox)->cmd.lcn = wp->lcn;
800 ((mbox_cmd_t*)wp->mbox)->cmd.result = 0x7F;
803 if (flags & O_NONBLOCK){
805 atomic_set(&chan->command, cmd);
807 atomic_set(&chan->command, cmd);
810 add_wait_queue(sk->sk_sleep,&wait);
811 current->state = TASK_INTERRUPTIBLE;
813 if (((mbox_cmd_t*)wp->mbox)->cmd.result != 0x7F) {
817 if (signal_pending(current)) {
823 current->state = TASK_RUNNING;
824 remove_wait_queue(sk->sk_sleep,&wait);
829 /*============================================================
830 * wanpipe_destroy_timer
832 * Used by wanpipe_release, to delay release of
834 *===========================================================*/
836 static void wanpipe_destroy_timer(unsigned long data)
838 struct sock *sk=(struct sock *)data;
839 wanpipe_opt *wp = wp_sk(sk);
841 if ((!atomic_read(&sk->sk_wmem_alloc) &&
842 !atomic_read(&sk->sk_rmem_alloc)) ||
843 (++wp->force == 5)) {
845 if (atomic_read(&sk->sk_wmem_alloc) ||
846 atomic_read(&sk->sk_rmem_alloc))
847 printk(KERN_INFO "wansock: Warning, Packet Discarded due to sock shutdown!\n");
852 if (atomic_read(&sk->sk_refcnt) != 1) {
853 atomic_set(&sk->sk_refcnt, 1);
854 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :delay.\n",
855 atomic_read(&sk->sk_refcnt));
858 atomic_dec(&wanpipe_socks_nr);
862 sk->sk_timer.expires = jiffies + 5 * HZ;
863 add_timer(&sk->sk_timer);
864 printk(KERN_INFO "wansock: packet sk destroy delayed\n");
867 /*============================================================
868 * wanpipe_unlink_driver
870 * When the socket is released, this function is
871 * used to remove links that bind the sock and the
873 *===========================================================*/
874 static void wanpipe_unlink_driver (struct sock *sk)
876 struct net_device *dev;
877 wanpipe_common_t *chan=NULL;
879 sock_reset_flag(sk, SOCK_ZAPPED);
880 sk->sk_state = WANSOCK_DISCONNECTED;
881 wp_sk(sk)->dev = NULL;
883 dev = dev_get_by_index(sk->sk_bound_dev_if);
885 printk(KERN_INFO "wansock: No dev on release\n");
890 if ((chan = dev->priv) == NULL){
891 printk(KERN_INFO "wansock: No Priv Area on release\n");
895 set_bit(0,&chan->common_critical);
900 clear_bit(0,&chan->common_critical);
906 /*============================================================
907 * wanpipe_link_driver
909 * Upon successful bind(), sock is linked to a driver
910 * by binding in the wanpipe_rcv() bottom half handler
911 * to the driver function pointer, as well as sock and
912 * sock mailbox addresses. This way driver can pass
913 * data up the socket.
914 *===========================================================*/
916 static void wanpipe_link_driver(struct net_device *dev, struct sock *sk)
918 wanpipe_opt *wp = wp_sk(sk);
919 wanpipe_common_t *chan = dev->priv;
922 set_bit(0,&chan->common_critical);
924 chan->func=wanpipe_rcv;
925 chan->mbox = wp->mbox;
926 chan->tx_timer = &wp->tx_timer;
928 sock_set_flag(sk, SOCK_ZAPPED);
929 clear_bit(0,&chan->common_critical);
933 /*============================================================
936 * During sock release, clear a critical bit, which
937 * marks the device a being taken.
938 *===========================================================*/
941 static void release_device(struct net_device *dev)
943 wanpipe_common_t *chan=dev->priv;
944 clear_bit(0,(void*)&chan->rw_bind);
947 /*============================================================
950 * Close a PACKET socket. This is fairly simple. We
951 * immediately go to 'closed' state and remove our
952 * protocol entry in the device list.
953 *===========================================================*/
955 static int wanpipe_release(struct socket *sock)
958 struct sock *sk = sock->sk;
964 check_write_queue(sk);
966 /* Kill the tx timer, if we don't kill it now, the timer
967 * will run after we kill the sock. Timer code will
968 * try to access the sock which has been killed and cause
971 del_timer(&wp->tx_timer);
974 * Unhook packet receive handler.
977 if (wp->num == htons(X25_PROT) &&
978 sk->sk_state != WANSOCK_DISCONNECTED && sock_flag(sk, SOCK_ZAPPED)) {
979 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
980 wanpipe_common_t *chan;
983 atomic_set(&chan->disconnect,1);
984 DBG_PRINTK(KERN_INFO "wansock: Sending Clear Indication %i\n",
990 set_bit(1,&wanpipe_tx_critical);
991 write_lock(&wanpipe_sklist_lock);
992 sk_del_node_init(sk);
993 write_unlock(&wanpipe_sklist_lock);
994 clear_bit(1,&wanpipe_tx_critical);
1002 * Now the socket is dead. No more input will appear.
1005 sk->sk_state_change(sk); /* It is useless. Just for sanity. */
1008 sk->sk_socket = NULL;
1009 sock_set_flag(sk, SOCK_DEAD);
1012 skb_queue_purge(&sk->sk_receive_queue);
1013 skb_queue_purge(&sk->sk_write_queue);
1014 skb_queue_purge(&sk->sk_error_queue);
1016 if (atomic_read(&sk->sk_rmem_alloc) ||
1017 atomic_read(&sk->sk_wmem_alloc)) {
1018 del_timer(&sk->sk_timer);
1019 printk(KERN_INFO "wansock: Killing in Timer R %i , W %i\n",
1020 atomic_read(&sk->sk_rmem_alloc),
1021 atomic_read(&sk->sk_wmem_alloc));
1022 sk->sk_timer.data = (unsigned long)sk;
1023 sk->sk_timer.expires = jiffies + HZ;
1024 sk->sk_timer.function = wanpipe_destroy_timer;
1025 add_timer(&sk->sk_timer);
1032 if (atomic_read(&sk->sk_refcnt) != 1) {
1033 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:release.\n",
1034 atomic_read(&sk->sk_refcnt));
1035 atomic_set(&sk->sk_refcnt, 1);
1038 atomic_dec(&wanpipe_socks_nr);
1042 /*============================================================
1045 * During sock shutdown, if the sock state is
1046 * WANSOCK_CONNECTED and there is transmit data
1047 * pending. Wait until data is released
1048 * before proceeding.
1049 *===========================================================*/
1051 static void check_write_queue(struct sock *sk)
1054 if (sk->sk_state != WANSOCK_CONNECTED)
1057 if (!atomic_read(&sk->sk_wmem_alloc))
1060 printk(KERN_INFO "wansock: MAJOR ERROR, Data lost on sock release !!!\n");
1064 /*============================================================
1067 * This function is called during sock shutdown, to
1068 * release any resources and links that bind the sock
1069 * to the driver. It also changes the state of the
1070 * sock to WANSOCK_DISCONNECTED
1071 *===========================================================*/
1073 static void release_driver(struct sock *sk)
1076 struct sk_buff *skb=NULL;
1077 struct sock *deadsk=NULL;
1079 if (sk->sk_state == WANSOCK_LISTEN ||
1080 sk->sk_state == WANSOCK_BIND_LISTEN) {
1081 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1082 if ((deadsk = get_newsk_from_skb(skb))){
1083 DBG_PRINTK (KERN_INFO "wansock: RELEASE: FOUND DEAD SOCK\n");
1084 sock_set_flag(deadsk, SOCK_DEAD);
1085 start_cleanup_timer(deadsk);
1089 if (sock_flag(sk, SOCK_ZAPPED))
1090 wanpipe_unlink_card(sk);
1092 if (sock_flag(sk, SOCK_ZAPPED))
1093 wanpipe_unlink_driver(sk);
1095 sk->sk_state = WANSOCK_DISCONNECTED;
1096 sk->sk_bound_dev_if = 0;
1097 sock_reset_flag(sk, SOCK_ZAPPED);
1106 /*============================================================
1107 * start_cleanup_timer
1109 * If new incoming call's are pending but the socket
1110 * is being released, start the timer which will
1111 * envoke the kill routines for pending socks.
1112 *===========================================================*/
1115 static void start_cleanup_timer (struct sock *sk)
1117 del_timer(&sk->sk_timer);
1118 sk->sk_timer.data = (unsigned long)sk;
1119 sk->sk_timer.expires = jiffies + HZ;
1120 sk->sk_timer.function = wanpipe_kill_sock_timer;
1121 add_timer(&sk->sk_timer);
1125 /*============================================================
1128 * This is a function which performs actual killing
1129 * of the sock. It releases socket resources,
1130 * and unlinks the sock from the driver.
1131 *===========================================================*/
1133 static void wanpipe_kill_sock_timer (unsigned long data)
1136 struct sock *sk = (struct sock *)data;
1142 /* This function can be called from interrupt. We must use
1143 * appropriate locks */
1145 if (test_bit(1,&wanpipe_tx_critical)){
1146 sk->sk_timer.expires = jiffies + 10;
1147 add_timer(&sk->sk_timer);
1151 write_lock(&wanpipe_sklist_lock);
1152 sk_del_node_init(sk);
1153 write_unlock(&wanpipe_sklist_lock);
1156 if (wp_sk(sk)->num == htons(X25_PROT) &&
1157 sk->sk_state != WANSOCK_DISCONNECTED) {
1158 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
1159 wanpipe_common_t *chan;
1162 atomic_set(&chan->disconnect,1);
1169 sk->sk_socket = NULL;
1172 skb_queue_purge(&sk->sk_receive_queue);
1173 skb_queue_purge(&sk->sk_write_queue);
1174 skb_queue_purge(&sk->sk_error_queue);
1176 if (atomic_read(&sk->sk_rmem_alloc) ||
1177 atomic_read(&sk->sk_wmem_alloc)) {
1178 del_timer(&sk->sk_timer);
1179 printk(KERN_INFO "wansock: Killing SOCK in Timer\n");
1180 sk->sk_timer.data = (unsigned long)sk;
1181 sk->sk_timer.expires = jiffies + HZ;
1182 sk->sk_timer.function = wanpipe_destroy_timer;
1183 add_timer(&sk->sk_timer);
1190 if (atomic_read(&sk->sk_refcnt) != 1) {
1191 atomic_set(&sk->sk_refcnt, 1);
1192 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
1193 atomic_read(&sk->sk_refcnt));
1196 atomic_dec(&wanpipe_socks_nr);
1200 static void wanpipe_kill_sock_accept (struct sock *sk)
1208 /* This function can be called from interrupt. We must use
1209 * appropriate locks */
1211 write_lock(&wanpipe_sklist_lock);
1212 sk_del_node_init(sk);
1213 write_unlock(&wanpipe_sklist_lock);
1215 sk->sk_socket = NULL;
1221 if (atomic_read(&sk->sk_refcnt) != 1) {
1222 atomic_set(&sk->sk_refcnt, 1);
1223 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
1224 atomic_read(&sk->sk_refcnt));
1227 atomic_dec(&wanpipe_socks_nr);
1232 static void wanpipe_kill_sock_irq (struct sock *sk)
1238 sk->sk_socket = NULL;
1243 if (atomic_read(&sk->sk_refcnt) != 1) {
1244 atomic_set(&sk->sk_refcnt, 1);
1245 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:listen.\n",
1246 atomic_read(&sk->sk_refcnt));
1249 atomic_dec(&wanpipe_socks_nr);
1253 /*============================================================
1256 * Bottom half of the binding system call.
1257 * Once the wanpipe_bind() function checks the
1258 * legality of the call, this function binds the
1259 * sock to the driver.
1260 *===========================================================*/
1262 static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
1265 wanpipe_opt *wp = wp_sk(sk);
1266 wanpipe_common_t *chan=NULL;
1269 if (sock_flag(sk, SOCK_ZAPPED)) {
1271 goto bind_unlock_exit;
1277 release_device(dev);
1279 goto bind_unlock_exit;
1283 if (dev->flags&IFF_UP) {
1285 sk->sk_state = chan->state;
1287 if (wp->num == htons(X25_PROT) &&
1288 sk->sk_state != WANSOCK_DISCONNECTED &&
1289 sk->sk_state != WANSOCK_CONNECTING) {
1290 DBG_PRINTK(KERN_INFO
1291 "wansock: Binding to Device not DISCONNECTED %i\n",
1293 release_device(dev);
1295 goto bind_unlock_exit;
1298 wanpipe_link_driver(dev,sk);
1299 sk->sk_bound_dev_if = dev->ifindex;
1301 /* X25 Specific option */
1302 if (wp->num == htons(X25_PROT))
1303 wp_sk(sk)->svc = chan->svc;
1306 sk->sk_err = ENETDOWN;
1307 sk->sk_error_report(sk);
1308 release_device(dev);
1315 /* FIXME where is this lock */
1320 /*============================================================
1323 * BIND() System call, which is bound to the AF_WANPIPE
1324 * operations structure. It checks for correct wanpipe
1325 * card name, and cross references interface names with
1326 * the card names. Thus, interface name must belong to
1328 *===========================================================*/
1331 static int wanpipe_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1333 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1334 struct sock *sk=sock->sk;
1335 wanpipe_opt *wp = wp_sk(sk);
1336 struct net_device *dev = NULL;
1344 if (addr_len < sizeof(struct wan_sockaddr_ll)){
1345 printk(KERN_INFO "wansock: Address length error\n");
1348 if (sll->sll_family != AF_WANPIPE){
1349 printk(KERN_INFO "wansock: Illegal family name specified.\n");
1353 card = wanpipe_find_card (sll->sll_card);
1355 printk(KERN_INFO "wansock: Wanpipe card not found: %s\n",sll->sll_card);
1358 wp_sk(sk)->card = (void *)card;
1361 if (!strcmp(sll->sll_device,"svc_listen")){
1363 /* Bind a sock to a card structure for listening
1367 /* This is x25 specific area if protocol doesn't
1368 * match, return error */
1369 if (sll->sll_protocol != htons(X25_PROT))
1372 err= wanpipe_link_card (sk);
1376 if (sll->sll_protocol)
1377 wp->num = sll->sll_protocol;
1378 sk->sk_state = WANSOCK_BIND_LISTEN;
1381 }else if (!strcmp(sll->sll_device,"svc_connect")){
1383 /* This is x25 specific area if protocol doesn't
1384 * match, return error */
1385 if (sll->sll_protocol != htons(X25_PROT))
1388 /* Find a free device
1390 dev = wanpipe_find_free_dev(card);
1392 DBG_PRINTK(KERN_INFO "wansock: No free network devices for card %s\n",
1397 /* Bind a socket to a interface name
1398 * This is used by PVC mostly
1400 strlcpy(name,sll->sll_device,sizeof(name));
1401 dev = dev_get_by_name(name);
1403 printk(KERN_INFO "wansock: Failed to get Dev from name: %s,\n",
1410 if (check_dev(dev, card)){
1411 printk(KERN_INFO "wansock: Device %s, doesn't belong to card %s\n",
1412 dev->name, card->devname);
1415 if (get_atomic_device (dev))
1419 return wanpipe_do_bind(sk, dev, sll->sll_protocol ? : wp->num);
1422 /*============================================================
1425 * Sets a bit atomically which indicates that
1426 * the interface is taken. This avoids race conditions.
1427 *===========================================================*/
1430 static inline int get_atomic_device(struct net_device *dev)
1432 wanpipe_common_t *chan = dev->priv;
1433 if (!test_and_set_bit(0,(void *)&chan->rw_bind)){
1439 /*============================================================
1442 * Check that device name belongs to a particular card.
1443 *===========================================================*/
1445 static int check_dev(struct net_device *dev, sdla_t *card)
1447 struct net_device* tmp_dev;
1449 for (tmp_dev = card->wandev.dev; tmp_dev;
1450 tmp_dev = *((struct net_device **)tmp_dev->priv)) {
1451 if (tmp_dev->ifindex == dev->ifindex){
1458 /*============================================================
1459 * wanpipe_find_free_dev
1461 * Find a free network interface. If found set atomic
1462 * bit indicating that the interface is taken.
1464 *===========================================================*/
1466 struct net_device *wanpipe_find_free_dev(sdla_t *card)
1468 struct net_device* dev;
1469 volatile wanpipe_common_t *chan;
1471 if (test_and_set_bit(0,&find_free_critical)){
1472 printk(KERN_INFO "CRITICAL in Find Free\n");
1475 for (dev = card->wandev.dev; dev;
1476 dev = *((struct net_device **)dev->priv)) {
1480 if (chan->usedby == API && chan->svc){
1481 if (!get_atomic_device (dev)){
1482 if (chan->state != WANSOCK_DISCONNECTED){
1483 release_device(dev);
1485 clear_bit(0,&find_free_critical);
1491 clear_bit(0,&find_free_critical);
1495 /*============================================================
1498 * SOCKET() System call. It allocates a sock structure
1499 * and adds the socket to the wanpipe_sk_list.
1500 * Crates AF_WANPIPE socket.
1501 *===========================================================*/
1503 static int wanpipe_create(struct socket *sock, int protocol)
1507 //FIXME: This checks for root user, SECURITY ?
1508 //if (!capable(CAP_NET_RAW))
1511 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1512 return -ESOCKTNOSUPPORT;
1514 sock->state = SS_UNCONNECTED;
1516 if ((sk = wanpipe_alloc_socket()) == NULL)
1520 sock->ops = &wanpipe_ops;
1521 sock_init_data(sock,sk);
1523 sock_reset_flag(sk, SOCK_ZAPPED);
1524 sk->sk_family = PF_WANPIPE;
1525 wp_sk(sk)->num = protocol;
1526 sk->sk_state = WANSOCK_DISCONNECTED;
1527 sk->sk_ack_backlog = 0;
1528 sk->sk_bound_dev_if = 0;
1530 atomic_inc(&wanpipe_socks_nr);
1532 /* We must disable interrupts because the ISR
1533 * can also change the list */
1534 set_bit(1,&wanpipe_tx_critical);
1535 write_lock(&wanpipe_sklist_lock);
1536 sk_add_node(sk, &wanpipe_sklist);
1537 write_unlock(&wanpipe_sklist_lock);
1538 clear_bit(1,&wanpipe_tx_critical);
1544 /*============================================================
1547 * Pull a packet from our receive queue and hand it
1548 * to the user. If necessary we block.
1549 *===========================================================*/
1551 static int wanpipe_recvmsg(struct kiocb *iocb, struct socket *sock,
1552 struct msghdr *msg, int len, int flags)
1554 struct sock *sk = sock->sk;
1555 struct sk_buff *skb;
1556 int copied, err=-ENOBUFS;
1560 * If the address length field is there to be filled in, we fill
1564 msg->msg_namelen = sizeof(struct wan_sockaddr_ll);
1567 * Call the generic datagram receiver. This handles all sorts
1568 * of horrible races and re-entrancy so we can forget about it
1569 * in the protocol layers.
1571 * Now it will return ENETDOWN, if device have just gone down,
1572 * but then it will block.
1575 if (flags & MSG_OOB){
1576 skb = skb_dequeue(&sk->sk_error_queue);
1578 skb=skb_recv_datagram(sk,flags,1,&err);
1581 * An error occurred so return it. Because skb_recv_datagram()
1582 * handles the blocking we don't see and worry about blocking
1590 * You lose any data beyond the buffer you gave. If it worries a
1591 * user program they can ask the device for its MTU anyway.
1598 msg->msg_flags|=MSG_TRUNC;
1601 wanpipe_wakeup_driver(sk);
1603 /* We can't use skb_copy_datagram here */
1604 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
1608 sock_recv_timestamp(msg, sk, skb);
1611 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1614 * Free or return the buffer as appropriate. Again this
1615 * hides all the races and re-entrancy issues from us.
1617 err = (flags&MSG_TRUNC) ? skb->len : copied;
1620 skb_free_datagram(sk, skb);
1626 /*============================================================
1627 * wanpipe_wakeup_driver
1629 * If socket receive buffer is full and driver cannot
1630 * pass data up the sock, it sets a packet_block flag.
1631 * This function check that flag and if sock receive
1632 * queue has room it kicks the driver BH handler.
1634 * This way, driver doesn't have to poll the sock
1636 *===========================================================*/
1638 static void wanpipe_wakeup_driver(struct sock *sk)
1640 struct net_device *dev = NULL;
1641 wanpipe_common_t *chan=NULL;
1643 dev = dev_get_by_index(sk->sk_bound_dev_if);
1649 if ((chan = dev->priv) == NULL)
1652 if (atomic_read(&chan->receive_block)){
1653 if (atomic_read(&sk->sk_rmem_alloc) <
1654 ((unsigned)sk->sk_rcvbuf * 0.9)) {
1655 printk(KERN_INFO "wansock: Queuing task for wanpipe\n");
1656 atomic_set(&chan->receive_block,0);
1657 wanpipe_queue_tq(&chan->wanpipe_task);
1663 /*============================================================
1666 * I don't know what to do with this yet.
1667 * User can use this function to get sock address
1668 * information. Not very useful for Sangoma's purposes.
1669 *===========================================================*/
1672 static int wanpipe_getname(struct socket *sock, struct sockaddr *uaddr,
1673 int *uaddr_len, int peer)
1675 struct net_device *dev;
1676 struct sock *sk = sock->sk;
1677 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1679 sll->sll_family = AF_WANPIPE;
1680 sll->sll_ifindex = sk->sk_bound_dev_if;
1681 sll->sll_protocol = wp_sk(sk)->num;
1682 dev = dev_get_by_index(sk->sk_bound_dev_if);
1684 sll->sll_hatype = dev->type;
1685 sll->sll_halen = dev->addr_len;
1686 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1688 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1691 *uaddr_len = sizeof(*sll);
1698 /*============================================================
1701 * If driver turns off network interface, this function
1702 * will be envoked. Currently I treate it as a
1703 * call disconnect. More thought should go into this
1706 * FIXME: More thought should go into this function.
1708 *===========================================================*/
1710 static int wanpipe_notifier(struct notifier_block *this, unsigned long msg, void *data)
1714 struct net_device *dev = (struct net_device *)data;
1716 sk_for_each(sk, node, &wanpipe_sklist) {
1717 struct wanpipe_opt *po = wp_sk(sk);
1726 case NETDEV_UNREGISTER:
1727 if (dev->ifindex == sk->sk_bound_dev_if) {
1728 printk(KERN_INFO "wansock: Device down %s\n",dev->name);
1729 if (sock_flag(sk, SOCK_ZAPPED)) {
1730 wanpipe_unlink_driver(sk);
1731 sk->sk_err = ENETDOWN;
1732 sk->sk_error_report(sk);
1735 if (msg == NETDEV_UNREGISTER) {
1736 printk(KERN_INFO "wansock: Unregistering Device: %s\n",
1738 wanpipe_unlink_driver(sk);
1739 sk->sk_bound_dev_if = 0;
1744 if (dev->ifindex == sk->sk_bound_dev_if &&
1745 po->num && !sock_flag(sk, SOCK_ZAPPED)) {
1746 printk(KERN_INFO "wansock: Registering Device: %s\n",
1748 wanpipe_link_driver(dev,sk);
1756 /*============================================================
1759 * Execute a user commands, and set socket options.
1761 * FIXME: More thought should go into this function.
1763 *===========================================================*/
1765 static int wanpipe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1767 struct sock *sk = sock->sk;
1773 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1775 case SIOC_WANPIPE_CHECK_TX:
1777 return atomic_read(&sk->sk_wmem_alloc);
1779 case SIOC_WANPIPE_SOCK_STATE:
1781 if (sk->sk_state == WANSOCK_CONNECTED)
1787 case SIOC_WANPIPE_GET_CALL_DATA:
1789 return get_ioctl_cmd (sk,(void*)arg);
1791 case SIOC_WANPIPE_SET_CALL_DATA:
1793 return set_ioctl_cmd (sk,(void*)arg);
1795 case SIOC_WANPIPE_ACCEPT_CALL:
1796 case SIOC_WANPIPE_CLEAR_CALL:
1797 case SIOC_WANPIPE_RESET_CALL:
1799 if ((err=set_ioctl_cmd(sk,(void*)arg)) < 0)
1802 err=wanpipe_exec_cmd(sk,cmd,0);
1803 get_ioctl_cmd(sk,(void*)arg);
1806 case SIOC_WANPIPE_DEBUG:
1808 return wanpipe_debug(sk,(void*)arg);
1810 case SIOC_WANPIPE_SET_NONBLOCK:
1812 if (sk->sk_state != WANSOCK_DISCONNECTED)
1815 sock->file->f_flags |= O_NONBLOCK;
1829 case SIOCGIFBRDADDR:
1830 case SIOCSIFBRDADDR:
1831 case SIOCGIFNETMASK:
1832 case SIOCSIFNETMASK:
1833 case SIOCGIFDSTADDR:
1834 case SIOCSIFDSTADDR:
1836 return inet_dgram_ops.ioctl(sock, cmd, arg);
1840 return -ENOIOCTLCMD;
1845 /*============================================================
1848 * This function will pass up information about all
1851 * FIXME: More thought should go into this function.
1853 *===========================================================*/
1855 static int wanpipe_debug (struct sock *origsk, void *arg)
1858 struct hlist_node *node;
1859 struct net_device *dev = NULL;
1860 wanpipe_common_t *chan=NULL;
1862 wan_debug_t *dbg_data = (wan_debug_t *)arg;
1864 sk_for_each(sk, node, &wanpipe_sklist) {
1865 wanpipe_opt *wp = wp_sk(sk);
1871 if ((err=put_user(1, &dbg_data->debug[cnt].free)))
1873 if ((err = put_user(sk->sk_state,
1874 &dbg_data->debug[cnt].state_sk)))
1876 if ((err = put_user(sk->sk_rcvbuf,
1877 &dbg_data->debug[cnt].rcvbuf)))
1879 if ((err = put_user(atomic_read(&sk->sk_rmem_alloc),
1880 &dbg_data->debug[cnt].rmem)))
1882 if ((err = put_user(atomic_read(&sk->sk_wmem_alloc),
1883 &dbg_data->debug[cnt].wmem)))
1885 if ((err = put_user(sk->sk_sndbuf,
1886 &dbg_data->debug[cnt].sndbuf)))
1888 if ((err=put_user(sk_count, &dbg_data->debug[cnt].sk_count)))
1890 if ((err=put_user(wp->poll_cnt, &dbg_data->debug[cnt].poll_cnt)))
1892 if ((err = put_user(sk->sk_bound_dev_if,
1893 &dbg_data->debug[cnt].bound)))
1896 if (sk->sk_bound_dev_if) {
1897 dev = dev_get_by_index(sk->sk_bound_dev_if);
1904 if ((err=put_user(chan->state, &dbg_data->debug[cnt].d_state)))
1906 if ((err=put_user(chan->svc, &dbg_data->debug[cnt].svc)))
1909 if ((err=put_user(atomic_read(&chan->command),
1910 &dbg_data->debug[cnt].command)))
1915 sdla_t *card = (sdla_t*)wp->card;
1918 if ((err=put_user(atomic_read(&card->u.x.command_busy),
1919 &dbg_data->debug[cnt].cmd_busy)))
1923 if ((err=put_user(wp->lcn,
1924 &dbg_data->debug[cnt].lcn)))
1928 if ((err=put_user(1, &dbg_data->debug[cnt].mbox)))
1933 if ((err=put_user(atomic_read(&chan->receive_block),
1934 &dbg_data->debug[cnt].rblock)))
1937 if (copy_to_user(dbg_data->debug[cnt].name, dev->name, strlen(dev->name)))
1941 if (++cnt == MAX_NUM_DEBUG)
1947 /*============================================================
1950 * Pass up the contents of socket MBOX to the user.
1951 *===========================================================*/
1953 static int get_ioctl_cmd (struct sock *sk, void *arg)
1955 x25api_t *usr_data = (x25api_t *)arg;
1956 mbox_cmd_t *mbox_ptr;
1959 if (usr_data == NULL)
1962 if (!wp_sk(sk)->mbox) {
1966 mbox_ptr = (mbox_cmd_t *)wp_sk(sk)->mbox;
1968 if ((err=put_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
1970 if ((err=put_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
1972 if ((err=put_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
1974 if ((err=put_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
1976 if ((err=put_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
1978 if ((err=put_user(mbox_ptr->cmd.lcn, &usr_data->hdr.lcn)))
1981 if (mbox_ptr->cmd.length > 0){
1982 if (mbox_ptr->cmd.length > X25_MAX_DATA)
1985 if (copy_to_user(usr_data->data, mbox_ptr->data, mbox_ptr->cmd.length)){
1986 printk(KERN_INFO "wansock: Copy failed !!!\n");
1993 /*============================================================
1996 * Before command can be execute, socket MBOX must
1997 * be created, and initialized with user data.
1998 *===========================================================*/
2000 static int set_ioctl_cmd (struct sock *sk, void *arg)
2002 x25api_t *usr_data = (x25api_t *)arg;
2003 mbox_cmd_t *mbox_ptr;
2006 if (!wp_sk(sk)->mbox) {
2008 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2014 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL)
2017 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2018 wp_sk(sk)->mbox = mbox_ptr;
2020 wanpipe_link_driver(dev,sk);
2023 mbox_ptr = (mbox_cmd_t*)wp_sk(sk)->mbox;
2024 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2026 if (usr_data == NULL){
2029 if ((err=get_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
2031 if ((err=get_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
2033 if ((err=get_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
2035 if ((err=get_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
2037 if ((err=get_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
2040 if (mbox_ptr->cmd.length > 0){
2041 if (mbox_ptr->cmd.length > X25_MAX_DATA)
2044 if (copy_from_user(mbox_ptr->data, usr_data->data, mbox_ptr->cmd.length)){
2045 printk(KERN_INFO "Copy failed\n");
2053 /*======================================================================
2056 * Datagram poll: Again totally generic. This also handles
2057 * sequenced packet sockets providing the socket receive queue
2058 * is only ever holding data ready to receive.
2060 * Note: when you _don't_ use this routine for this protocol,
2061 * and you use a different write policy from sock_writeable()
2062 * then please supply your own write_space callback.
2063 *=====================================================================*/
2065 unsigned int wanpipe_poll(struct file * file, struct socket *sock, poll_table *wait)
2067 struct sock *sk = sock->sk;
2070 ++wp_sk(sk)->poll_cnt;
2072 poll_wait(file, sk->sk_sleep, wait);
2075 /* exceptional events? */
2076 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) {
2080 if (sk->sk_shutdown & RCV_SHUTDOWN)
2084 if (!skb_queue_empty(&sk->sk_receive_queue)) {
2085 mask |= POLLIN | POLLRDNORM;
2088 /* connection hasn't started yet */
2089 if (sk->sk_state == WANSOCK_CONNECTING) {
2093 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2098 /* This check blocks the user process if there is
2099 * a packet already queued in the socket write queue.
2100 * This option is only for X25API protocol, for other
2101 * protocol like chdlc enable streaming mode,
2102 * where multiple packets can be pending in the socket
2105 if (wp_sk(sk)->num == htons(X25_PROT)) {
2106 if (atomic_read(&wp_sk(sk)->packet_sent))
2111 if (sock_writeable(sk)){
2112 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2114 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2120 /*======================================================================
2123 * X25API Specific function. Set a socket into LISTENING MODE.
2124 *=====================================================================*/
2127 static int wanpipe_listen(struct socket *sock, int backlog)
2129 struct sock *sk = sock->sk;
2131 /* This is x25 specific area if protocol doesn't
2132 * match, return error */
2133 if (wp_sk(sk)->num != htons(X25_PROT))
2136 if (sk->sk_state == WANSOCK_BIND_LISTEN) {
2138 sk->sk_max_ack_backlog = backlog;
2139 sk->sk_state = WANSOCK_LISTEN;
2142 printk(KERN_INFO "wansock: Listening sock was not binded\n");
2148 /*======================================================================
2151 * Connects the listening socket to the driver
2152 *=====================================================================*/
2154 static int wanpipe_link_card (struct sock *sk)
2156 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2161 if ((card->sk != NULL) || (card->func != NULL)){
2162 printk(KERN_INFO "wansock: Listening queue is already established\n");
2167 card->func=wanpipe_listen_rcv;
2168 sock_set_flag(sk, SOCK_ZAPPED);
2173 /*======================================================================
2176 * X25API Specific function. Disconnect listening socket from
2178 *=====================================================================*/
2180 static void wanpipe_unlink_card (struct sock *sk)
2182 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2190 /*======================================================================
2193 * Ioctl function calls this function to execute user command.
2194 * Connect() sytem call also calls this function to execute
2195 * place call. This function blocks until command is executed.
2196 *=====================================================================*/
2198 static int wanpipe_exec_cmd(struct sock *sk, int cmd, unsigned int flags)
2201 wanpipe_opt *wp = wp_sk(sk);
2202 mbox_cmd_t *mbox_ptr = (mbox_cmd_t*)wp->mbox;
2205 printk(KERN_INFO "NO MBOX PTR !!!!!\n");
2209 /* This is x25 specific area if protocol doesn't
2210 * match, return error */
2211 if (wp->num != htons(X25_PROT))
2217 case SIOC_WANPIPE_ACCEPT_CALL:
2219 if (sk->sk_state != WANSOCK_CONNECTING) {
2224 err = execute_command(sk,X25_ACCEPT_CALL,0);
2228 /* Update. Mar6 2000.
2229 * Do not set the sock lcn number here, since
2230 * it is done in wanpipe_listen_rcv().
2232 if (sk->sk_state == WANSOCK_CONNECTED) {
2233 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2234 DBG_PRINTK(KERN_INFO "\nwansock: Accept OK %i\n",
2239 DBG_PRINTK (KERN_INFO "\nwansock: Accept Failed %i\n",
2242 err = -ECONNREFUSED;
2246 case SIOC_WANPIPE_CLEAR_CALL:
2248 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2254 /* Check if data buffers are pending for transmission,
2255 * if so, check whether user wants to wait until data
2256 * is transmitted, or clear a call and drop packets */
2258 if (atomic_read(&sk->sk_wmem_alloc) ||
2259 check_driver_busy(sk)) {
2260 mbox_cmd_t *mbox = wp->mbox;
2261 if (mbox->cmd.qdm & 0x80){
2262 mbox->cmd.result = 0x35;
2268 sk->sk_state = WANSOCK_DISCONNECTING;
2270 err = execute_command(sk,X25_CLEAR_CALL,0);
2274 err = -ECONNREFUSED;
2275 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2276 DBG_PRINTK(KERN_INFO "\nwansock: CLEAR OK %i\n",
2283 case SIOC_WANPIPE_RESET_CALL:
2285 if (sk->sk_state != WANSOCK_CONNECTED) {
2291 /* Check if data buffers are pending for transmission,
2292 * if so, check whether user wants to wait until data
2293 * is transmitted, or reset a call and drop packets */
2295 if (atomic_read(&sk->sk_wmem_alloc) ||
2296 check_driver_busy(sk)) {
2297 mbox_cmd_t *mbox = wp->mbox;
2298 if (mbox->cmd.qdm & 0x80){
2299 mbox->cmd.result = 0x35;
2306 err = execute_command(sk, X25_RESET,0);
2310 err = mbox_ptr->cmd.result;
2314 case X25_PLACE_CALL:
2316 err=execute_command(sk,X25_PLACE_CALL,flags);
2320 if (sk->sk_state == WANSOCK_CONNECTED) {
2322 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2324 DBG_PRINTK(KERN_INFO "\nwansock: PLACE CALL OK %i\n",
2328 } else if (sk->sk_state == WANSOCK_CONNECTING &&
2329 (flags & O_NONBLOCK)) {
2330 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2331 DBG_PRINTK(KERN_INFO "\nwansock: Place Call OK: Waiting %i\n",
2337 DBG_PRINTK(KERN_INFO "\nwansock: Place call Failed\n");
2338 err = -ECONNREFUSED;
2350 static int check_driver_busy (struct sock *sk)
2352 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2353 wanpipe_common_t *chan;
2360 if ((chan=dev->priv) == NULL)
2363 return atomic_read(&chan->driver_busy);
2367 /*======================================================================
2370 * ACCEPT() System call. X25API Specific function.
2371 * For each incoming call, create a new socket and
2372 * return it to the user.
2373 *=====================================================================*/
2375 static int wanpipe_accept(struct socket *sock, struct socket *newsock, int flags)
2379 struct sk_buff *skb;
2380 DECLARE_WAITQUEUE(wait, current);
2383 if (newsock->sk != NULL){
2384 wanpipe_kill_sock_accept(newsock->sk);
2388 if ((sk = sock->sk) == NULL)
2391 if (sk->sk_type != SOCK_RAW)
2394 if (sk->sk_state != WANSOCK_LISTEN)
2397 if (wp_sk(sk)->num != htons(X25_PROT))
2400 add_wait_queue(sk->sk_sleep,&wait);
2401 current->state = TASK_INTERRUPTIBLE;
2403 skb = skb_dequeue(&sk->sk_receive_queue);
2408 if (signal_pending(current)) {
2414 current->state = TASK_RUNNING;
2415 remove_wait_queue(sk->sk_sleep,&wait);
2420 newsk = get_newsk_from_skb(skb);
2425 set_bit(1,&wanpipe_tx_critical);
2426 write_lock(&wanpipe_sklist_lock);
2427 sk_add_node(newsk, &wanpipe_sklist);
2428 write_unlock(&wanpipe_sklist_lock);
2429 clear_bit(1,&wanpipe_tx_critical);
2431 newsk->sk_socket = newsock;
2432 newsk->sk_sleep = &newsock->wait;
2434 /* Now attach up the new socket */
2435 sk->sk_ack_backlog--;
2436 newsock->sk = newsk;
2440 DBG_PRINTK(KERN_INFO "\nwansock: ACCEPT Got LCN %i\n",
2445 /*======================================================================
2446 * get_newsk_from_skb
2448 * Accept() uses this function to get the address of the new
2450 *=====================================================================*/
2452 struct sock * get_newsk_from_skb (struct sk_buff *skb)
2454 struct net_device *dev = skb->dev;
2455 wanpipe_common_t *chan;
2461 if ((chan = dev->priv) == NULL){
2468 return (struct sock *)chan->sk;
2471 /*======================================================================
2474 * CONNECT() System Call. X25API specific function
2475 * Check the state of the sock, and execute PLACE_CALL command.
2476 * Connect can ether block or return without waiting for connection,
2477 * if specified by user.
2478 *=====================================================================*/
2480 static int wanpipe_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
2482 struct sock *sk = sock->sk;
2483 struct wan_sockaddr_ll *addr = (struct wan_sockaddr_ll*)uaddr;
2484 struct net_device *dev;
2487 if (wp_sk(sk)->num != htons(X25_PROT))
2490 if (sk->sk_state == WANSOCK_CONNECTED)
2491 return -EISCONN; /* No reconnect on a seqpacket socket */
2493 if (sk->sk_state != WAN_DISCONNECTED) {
2494 printk(KERN_INFO "wansock: Trying to connect on channel NON DISCONNECT\n");
2495 return -ECONNREFUSED;
2498 sk->sk_state = WANSOCK_DISCONNECTED;
2499 sock->state = SS_UNCONNECTED;
2501 if (addr_len != sizeof(struct wan_sockaddr_ll))
2504 if (addr->sll_family != AF_WANPIPE)
2507 if ((dev = dev_get_by_index(sk->sk_bound_dev_if)) == NULL)
2508 return -ENETUNREACH;
2512 if (!sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
2515 sock->state = SS_CONNECTING;
2516 sk->sk_state = WANSOCK_CONNECTING;
2518 if (!wp_sk(sk)->mbox) {
2519 if (wp_sk (sk)->svc)
2523 if ((err=set_ioctl_cmd(sk,NULL)) < 0)
2528 if ((err=wanpipe_exec_cmd(sk, X25_PLACE_CALL,flags)) != 0){
2529 sock->state = SS_UNCONNECTED;
2530 sk->sk_state = WANSOCK_CONNECTED;
2534 if (sk->sk_state != WANSOCK_CONNECTED && (flags & O_NONBLOCK)) {
2538 if (sk->sk_state != WANSOCK_CONNECTED) {
2539 sock->state = SS_UNCONNECTED;
2540 return -ECONNREFUSED;
2543 sock->state = SS_CONNECTED;
2547 const struct proto_ops wanpipe_ops = {
2548 .family = PF_WANPIPE,
2549 .owner = THIS_MODULE,
2550 .release = wanpipe_release,
2551 .bind = wanpipe_bind,
2552 .connect = wanpipe_connect,
2553 .socketpair = sock_no_socketpair,
2554 .accept = wanpipe_accept,
2555 .getname = wanpipe_getname,
2556 .poll = wanpipe_poll,
2557 .ioctl = wanpipe_ioctl,
2558 .listen = wanpipe_listen,
2559 .shutdown = sock_no_shutdown,
2560 .setsockopt = sock_no_setsockopt,
2561 .getsockopt = sock_no_getsockopt,
2562 .sendmsg = wanpipe_sendmsg,
2563 .recvmsg = wanpipe_recvmsg
2566 static struct net_proto_family wanpipe_family_ops = {
2567 .family = PF_WANPIPE,
2568 .create = wanpipe_create,
2569 .owner = THIS_MODULE,
2572 struct notifier_block wanpipe_netdev_notifier = {
2573 .notifier_call = wanpipe_notifier,
2578 void cleanup_module(void)
2580 printk(KERN_INFO "wansock: Cleaning up \n");
2581 unregister_netdevice_notifier(&wanpipe_netdev_notifier);
2582 sock_unregister(PF_WANPIPE);
2583 proto_unregister(&wanpipe_proto);
2586 int init_module(void)
2590 printk(KERN_INFO "wansock: Registering Socket \n");
2592 rc = proto_register(&wanpipe_proto, 0);
2596 sock_register(&wanpipe_family_ops);
2597 register_netdevice_notifier(&wanpipe_netdev_notifier);
2602 MODULE_LICENSE("GPL");
2603 MODULE_ALIAS_NETPROTO(PF_WANPIPE);