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/capability.h>
40 #include <linux/fcntl.h>
41 #include <linux/socket.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <linux/poll.h>
46 #include <linux/wireless.h>
47 #include <linux/kmod.h>
49 #include <net/protocol.h>
50 #include <linux/skbuff.h>
52 #include <linux/errno.h>
53 #include <linux/timer.h>
54 #include <asm/system.h>
55 #include <asm/uaccess.h>
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/if_wanpipe.h>
59 #include <linux/pkt_sched.h>
60 #include <linux/tcp_states.h>
61 #include <linux/if_wanpipe_common.h>
64 #include <net/inet_common.h>
67 #define SLOW_BACKOFF 0.1*HZ
68 #define FAST_BACKOFF 0.01*HZ
72 #define DBG_PRINTK(format, a...) printk(format, ## a)
74 #define DBG_PRINTK(format, a...)
78 /* SECURE SOCKET IMPLEMENTATION
82 * When the user sends a packet via send() system call
83 * the wanpipe_sendmsg() function is executed.
85 * Each packet is enqueud into sk->sk_write_queue transmit
86 * queue. When the packet is enqueued, a delayed transmit
87 * timer is triggerd which acts as a Bottom Half hander.
89 * wanpipe_delay_transmit() function (BH), dequeues packets
90 * from the sk->sk_write_queue transmit queue and sends it
91 * to the deriver via dev->hard_start_xmit(skb, dev) function.
92 * Note, this function is actual a function pointer of if_send()
93 * routine in the wanpipe driver.
95 * X25API GUARANTEED DELIVERY:
97 * In order to provide 100% guaranteed packet delivery,
98 * an atomic 'packet_sent' counter is implemented. Counter
99 * is incremented for each packet enqueued
100 * into sk->sk_write_queue. Counter is decremented each
101 * time wanpipe_delayed_transmit() function successfuly
102 * passes the packet to the driver. Before each send(), a poll
103 * routine checks the sock resources The maximum value of
104 * packet sent counter is 1, thus if one packet is queued, the
105 * application will block until that packet is passed to the
110 * Wanpipe device drivers call the socket bottom half
111 * function, wanpipe_rcv() to queue the incoming packets
112 * into an AF_WANPIPE socket queue. Based on wanpipe_rcv()
113 * return code, the driver knows whether the packet was
114 * successfully queued. If the socket queue is full,
115 * protocol flow control is used by the driver, if any,
116 * to slow down the traffic until the sock queue is free.
118 * Every time a packet arrives into a socket queue the
119 * socket wakes up processes which are waiting to receive
122 * If the socket queue is full, the driver sets a block
123 * bit which signals the socket to kick the wanpipe driver
124 * bottom half hander when the socket queue is partialy
125 * empty. wanpipe_recvmsg() function performs this action.
127 * In case of x25api, packets will never be dropped, since
128 * flow control is available.
130 * In case of streaming protocols like CHDLC, packets will
131 * be dropped but the statistics will be generated.
135 /* The code below is used to test memory leaks. It prints out
136 * a message every time kmalloc and kfree system calls get executed.
137 * If the calls match there is no leak :)
140 /***********FOR DEBUGGING PURPOSES*********************************************
141 #define KMEM_SAFETYZONE 8
143 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
144 void * v = kmalloc(size,prio);
145 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
148 static void dbg_kfree(void * v, int line) {
149 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
153 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
154 #define kfree(x) dbg_kfree(x,__LINE__)
155 ******************************************************************************/
158 /* List of all wanpipe sockets. */
159 HLIST_HEAD(wanpipe_sklist);
160 static DEFINE_RWLOCK(wanpipe_sklist_lock);
162 atomic_t wanpipe_socks_nr;
163 static unsigned long wanpipe_tx_critical;
166 /* Private wanpipe socket structures. */
169 void *mbox; /* Mail box */
170 void *card; /* Card bouded to */
171 struct net_device *dev; /* Bounded device */
172 unsigned short lcn; /* Binded LCN */
173 unsigned char svc; /* 0=pvc, 1=svc */
174 unsigned char timer; /* flag for delayed transmit*/
175 struct timer_list tx_timer;
177 unsigned char force; /* Used to force sock release */
178 atomic_t packet_sent;
183 extern const struct proto_ops wanpipe_ops;
184 static unsigned long find_free_critical;
186 static void wanpipe_unlink_driver(struct sock *sk);
187 static void wanpipe_link_driver(struct net_device *dev, struct sock *sk);
188 static void wanpipe_wakeup_driver(struct sock *sk);
189 static int execute_command(struct sock *, unsigned char, unsigned int);
190 static int check_dev(struct net_device *dev, sdla_t *card);
191 struct net_device *wanpipe_find_free_dev(sdla_t *card);
192 static void wanpipe_unlink_card (struct sock *);
193 static int wanpipe_link_card (struct sock *);
194 static struct sock *wanpipe_make_new(struct sock *);
195 static struct sock *wanpipe_alloc_socket(void);
196 static inline int get_atomic_device(struct net_device *dev);
197 static int wanpipe_exec_cmd(struct sock *, int, unsigned int);
198 static int get_ioctl_cmd (struct sock *, void *);
199 static int set_ioctl_cmd (struct sock *, void *);
200 static void release_device(struct net_device *dev);
201 static void wanpipe_kill_sock_timer (unsigned long data);
202 static void wanpipe_kill_sock_irq (struct sock *);
203 static void wanpipe_kill_sock_accept (struct sock *);
204 static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
206 struct sock * get_newsk_from_skb (struct sk_buff *);
207 static int wanpipe_debug (struct sock *, void *);
208 static void wanpipe_delayed_transmit (unsigned long data);
209 static void release_driver(struct sock *);
210 static void start_cleanup_timer (struct sock *);
211 static void check_write_queue(struct sock *);
212 static int check_driver_busy (struct sock *);
214 /*============================================================
217 * Wanpipe socket bottom half handler. This function
218 * is called by the WANPIPE device drivers to queue a
219 * incoming packet into the socket receive queue.
220 * Once the packet is queued, all processes waiting to
223 * During socket bind, this function is bounded into
224 * WANPIPE driver private.
225 *===========================================================*/
227 static int wanpipe_rcv(struct sk_buff *skb, struct net_device *dev,
230 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
231 wanpipe_common_t *chan = dev->priv;
233 * When we registered the protocol we saved the socket in the data
234 * field for just this event.
239 sll->sll_family = AF_WANPIPE;
240 sll->sll_hatype = dev->type;
241 sll->sll_protocol = skb->protocol;
242 sll->sll_pkttype = skb->pkt_type;
243 sll->sll_ifindex = dev->ifindex;
246 if (dev->hard_header_parse)
247 sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
250 * WAN_PACKET_DATA : Data which should be passed up the receive queue.
251 * WAN_PACKET_ASYC : Asynchronous data like place call, which should
252 * be passed up the listening sock.
253 * WAN_PACKET_ERR : Asynchronous data like clear call or restart
254 * which should go into an error queue.
256 switch (skb->pkt_type){
258 case WAN_PACKET_DATA:
259 if (sock_queue_rcv_skb(sk,skb)<0){
264 sk->sk_state = chan->state;
265 /* Bug fix: update Mar6.
266 * Do not set the sock lcn number here, since
267 * cmd is not guaranteed to be executed on the
268 * board, thus Lcn could be wrong */
269 sk->sk_data_ready(sk, skb->len);
273 sk->sk_state = chan->state;
274 if (sock_queue_err_skb(sk,skb)<0){
279 printk(KERN_INFO "wansock: BH Illegal Packet Type Dropping\n");
284 //??????????????????????
285 // if (sk->sk_state == WANSOCK_DISCONNECTED){
286 // if (sk->sk_zapped) {
287 // //printk(KERN_INFO "wansock: Disconnected, killing early\n");
288 // wanpipe_unlink_driver(sk);
289 // sk->sk_bound_dev_if = 0;
296 /*============================================================
299 * Wanpipe LISTEN socket bottom half handler. This function
300 * is called by the WANPIPE device drivers to queue an
301 * incoming call into the socket listening queue.
302 * Once the packet is queued, the waiting accept() process
305 * During socket bind, this function is bounded into
306 * WANPIPE driver private.
309 * The accept call() is waiting for an skb packet
310 * which contains a pointer to a device structure.
312 * When we do a bind to a device structre, we
313 * bind a newly created socket into "chan->sk". Thus,
314 * when accept receives the skb packet, it will know
315 * from which dev it came form, and in turn it will know
316 * the address of the new sock.
318 * NOTE: This function gets called from driver ISR.
319 *===========================================================*/
321 static int wanpipe_listen_rcv (struct sk_buff *skb, struct sock *sk)
323 wanpipe_opt *wp = wp_sk(sk), *newwp;
324 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
326 struct net_device *dev;
328 mbox_cmd_t *mbox_ptr;
329 wanpipe_common_t *chan;
331 /* Find a free device, if none found, all svc's are busy
334 card = (sdla_t*)wp->card;
336 printk(KERN_INFO "wansock: LISTEN ERROR, No Card\n");
340 dev = wanpipe_find_free_dev(card);
342 printk(KERN_INFO "wansock: LISTEN ERROR, No Free Device\n");
347 chan->state = WANSOCK_CONNECTING;
349 /* Allocate a new sock, which accept will bind
350 * and pass up to the user
352 if ((newsk = wanpipe_make_new(sk)) == NULL){
358 /* Initialize the new sock structure
360 newsk->sk_bound_dev_if = dev->ifindex;
361 newwp = wp_sk(newsk);
362 newwp->card = wp->card;
364 /* Insert the sock into the main wanpipe
367 atomic_inc(&wanpipe_socks_nr);
369 /* Allocate and fill in the new Mail Box. Then
370 * bind the mail box to the sock. It will be
371 * used by the ioctl call to read call information
372 * and to execute commands.
374 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL) {
375 wanpipe_kill_sock_irq (newsk);
379 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
380 memcpy(mbox_ptr,skb->data,skb->len);
382 /* Register the lcn on which incoming call came
383 * from. Thus, if we have to clear it, we know
387 newwp->lcn = mbox_ptr->cmd.lcn;
388 newwp->mbox = (void *)mbox_ptr;
390 DBG_PRINTK(KERN_INFO "NEWSOCK : Device %s, bind to lcn %i\n",
391 dev->name,mbox_ptr->cmd.lcn);
393 chan->lcn = mbox_ptr->cmd.lcn;
394 card->u.x.svc_to_dev_map[(chan->lcn%MAX_X25_LCN)] = dev;
396 sock_reset_flag(newsk, SOCK_ZAPPED);
397 newwp->num = htons(X25_PROT);
399 if (wanpipe_do_bind(newsk, dev, newwp->num)) {
400 wanpipe_kill_sock_irq (newsk);
404 newsk->sk_state = WANSOCK_CONNECTING;
407 /* Fill in the standard sock address info */
409 sll->sll_family = AF_WANPIPE;
410 sll->sll_hatype = dev->type;
411 sll->sll_protocol = skb->protocol;
412 sll->sll_pkttype = skb->pkt_type;
413 sll->sll_ifindex = dev->ifindex;
417 sk->sk_ack_backlog++;
419 /* We must do this manually, since the sock_queue_rcv_skb()
420 * function sets the skb->dev to NULL. However, we use
421 * the dev field in the accept function.*/
422 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
423 (unsigned)sk->sk_rcvbuf) {
425 wanpipe_unlink_driver(newsk);
426 wanpipe_kill_sock_irq (newsk);
427 --sk->sk_ack_backlog;
431 skb_set_owner_r(skb, sk);
432 skb_queue_tail(&sk->sk_receive_queue, skb);
433 sk->sk_data_ready(sk, skb->len);
440 /*============================================================
443 * Create a new sock, and allocate a wanpipe private
444 * structure to it. Also, copy the important data
445 * from the original sock to the new sock.
447 * This function is used by wanpipe_listen_rcv() listen
448 * bottom half handler. A copy of the listening sock
449 * is created using this function.
451 *===========================================================*/
453 static struct sock *wanpipe_make_new(struct sock *osk)
457 if (osk->sk_type != SOCK_RAW)
460 if ((sk = wanpipe_alloc_socket()) == NULL)
463 sk->sk_type = osk->sk_type;
464 sk->sk_socket = osk->sk_socket;
465 sk->sk_priority = osk->sk_priority;
466 sk->sk_protocol = osk->sk_protocol;
467 wp_sk(sk)->num = wp_sk(osk)->num;
468 sk->sk_rcvbuf = osk->sk_rcvbuf;
469 sk->sk_sndbuf = osk->sk_sndbuf;
470 sk->sk_state = WANSOCK_CONNECTING;
471 sk->sk_sleep = osk->sk_sleep;
473 if (sock_flag(osk, SOCK_DBG))
474 sock_set_flag(sk, SOCK_DBG);
480 * FIXME: wanpipe_opt has to include a sock in its definition and stop using
481 * sk_protinfo, but this code is not even compilable now, so lets leave it for
484 static struct proto wanpipe_proto = {
486 .owner = THIS_MODULE,
487 .obj_size = sizeof(struct sock),
490 /*============================================================
493 * Allocate memory for the a new sock, and sock
496 * Increment the module use count.
498 * This function is used by wanpipe_create() and
499 * wanpipe_make_new() functions.
501 *===========================================================*/
503 static struct sock *wanpipe_alloc_socket(void)
506 struct wanpipe_opt *wan_opt;
508 if ((sk = sk_alloc(PF_WANPIPE, GFP_ATOMIC, &wanpipe_proto, 1)) == NULL)
511 if ((wan_opt = kmalloc(sizeof(struct wanpipe_opt), GFP_ATOMIC)) == NULL) {
515 memset(wan_opt, 0x00, sizeof(struct wanpipe_opt));
519 /* Use timer to send data to the driver. This will act
520 * as a BH handler for sendmsg functions */
521 init_timer(&wan_opt->tx_timer);
522 wan_opt->tx_timer.data = (unsigned long)sk;
523 wan_opt->tx_timer.function = wanpipe_delayed_transmit;
525 sock_init_data(NULL, sk);
530 /*============================================================
533 * This function implements a sendto() system call,
534 * for AF_WANPIPE socket family.
535 * During socket bind() sk->sk_bound_dev_if is initialized
536 * to a correct network device. This number is used
537 * to find a network device to which the packet should
540 * Each packet is queued into sk->sk_write_queue and
541 * delayed transmit bottom half handler is marked for
544 * A socket must be in WANSOCK_CONNECTED state before
545 * a packet is queued into sk->sk_write_queue.
546 *===========================================================*/
548 static int wanpipe_sendmsg(struct kiocb *iocb, struct socket *sock,
549 struct msghdr *msg, int len)
552 struct sock *sk = sock->sk;
553 struct wan_sockaddr_ll *saddr=(struct wan_sockaddr_ll *)msg->msg_name;
555 struct net_device *dev;
556 unsigned short proto;
558 int ifindex, err, reserve = 0;
561 if (!sock_flag(sk, SOCK_ZAPPED))
564 if (sk->sk_state != WANSOCK_CONNECTED)
567 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
570 /* it was <=, now one can send
571 * zero length packets */
572 if (len < sizeof(x25api_hdr_t))
578 ifindex = sk->sk_bound_dev_if;
583 if (msg->msg_namelen < sizeof(struct wan_sockaddr_ll)){
587 ifindex = sk->sk_bound_dev_if;
588 proto = saddr->sll_protocol;
589 addr = saddr->sll_addr;
592 dev = dev_get_by_index(ifindex);
594 printk(KERN_INFO "wansock: Send failed, dev index: %i\n",ifindex);
599 if (sock->type == SOCK_RAW)
600 reserve = dev->hard_header_len;
602 if (len > dev->mtu+reserve){
606 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
607 msg->msg_flags & MSG_DONTWAIT, &err);
613 skb_reserve(skb, LL_RESERVED_SPACE(dev));
614 skb->nh.raw = skb->data;
616 /* Returns -EFAULT on error */
617 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
622 if (dev->hard_header) {
625 res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
631 skb->protocol = proto;
633 skb->priority = sk->sk_priority;
634 skb->pkt_type = WAN_PACKET_DATA;
637 if (!(dev->flags & IFF_UP))
640 if (atomic_read(&sk->sk_wmem_alloc) + skb->truesize >
641 (unsigned int)sk->sk_sndbuf){
646 skb_queue_tail(&sk->sk_write_queue,skb);
647 atomic_inc(&wp->packet_sent);
649 if (!(test_and_set_bit(0, &wp->timer)))
650 mod_timer(&wp->tx_timer, jiffies + 1);
660 /*============================================================
661 * wanpipe_delayed_tarnsmit
663 * Transmit bottom half handler. It dequeues packets
664 * from sk->sk_write_queue and passes them to the
665 * driver. If the driver is busy, the packet is
668 * Packet Sent counter is decremented on successful
670 *===========================================================*/
673 static void wanpipe_delayed_transmit (unsigned long data)
675 struct sock *sk=(struct sock *)data;
677 wanpipe_opt *wp = wp_sk(sk);
678 struct net_device *dev = wp->dev;
679 sdla_t *card = (sdla_t*)wp->card;
682 clear_bit(0, &wp->timer);
683 DBG_PRINTK(KERN_INFO "wansock: Transmit delay, no dev or card\n");
687 if (sk->sk_state != WANSOCK_CONNECTED || !sock_flag(sk, SOCK_ZAPPED)) {
688 clear_bit(0, &wp->timer);
689 DBG_PRINTK(KERN_INFO "wansock: Tx Timer, State not CONNECTED\n");
693 /* If driver is executing command, we must offload
694 * the board by not sending data. Otherwise a
695 * pending command will never get a free buffer
697 if (atomic_read(&card->u.x.command_busy)){
698 wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
699 add_timer(&wp->tx_timer);
700 DBG_PRINTK(KERN_INFO "wansock: Tx Timer, command bys BACKOFF\n");
705 if (test_and_set_bit(0,&wanpipe_tx_critical)){
706 printk(KERN_INFO "WanSock: Tx timer critical %s\n",dev->name);
707 wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
708 add_timer(&wp->tx_timer);
712 /* Check for a packet in the fifo and send */
713 if ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL){
715 if (dev->hard_start_xmit(skb, dev) != 0){
717 /* Driver failed to transmit, re-enqueue
718 * the packet and retry again later */
719 skb_queue_head(&sk->sk_write_queue,skb);
720 clear_bit(0,&wanpipe_tx_critical);
724 /* Packet Sent successful. Check for more packets
725 * if more packets, re-trigger the transmit routine
728 atomic_dec(&wp->packet_sent);
730 if (skb_peek(&sk->sk_write_queue) == NULL) {
731 /* If there is nothing to send, kick
732 * the poll routine, which will trigger
733 * the application to send more data */
734 sk->sk_data_ready(sk, 0);
735 clear_bit(0, &wp->timer);
737 /* Reschedule as fast as possible */
738 wp->tx_timer.expires = jiffies + 1;
739 add_timer(&wp->tx_timer);
743 clear_bit(0,&wanpipe_tx_critical);
746 /*============================================================
749 * Execute x25api commands. The atomic variable
750 * chan->command is used to indicate to the driver that
751 * command is pending for execution. The acutal command
752 * structure is placed into a sock mbox structure
755 * The sock private structure, mbox is
756 * used as shared memory between sock and the driver.
757 * Driver uses the sock mbox to execute the command
758 * and return the result.
760 * For all command except PLACE CALL, the function
761 * waits for the result. PLACE CALL can be ether
762 * blocking or nonblocking. The user sets this option
764 *===========================================================*/
767 static int execute_command(struct sock *sk, unsigned char cmd, unsigned int flags)
769 wanpipe_opt *wp = wp_sk(sk);
770 struct net_device *dev;
771 wanpipe_common_t *chan=NULL;
773 DECLARE_WAITQUEUE(wait, current);
775 dev = dev_get_by_index(sk->sk_bound_dev_if);
777 printk(KERN_INFO "wansock: Exec failed no dev %i\n",
778 sk->sk_bound_dev_if);
783 if ((chan=dev->priv) == NULL){
784 printk(KERN_INFO "wansock: Exec cmd failed no priv area\n");
788 if (atomic_read(&chan->command)){
789 printk(KERN_INFO "wansock: ERROR: Command already running %x, %s\n",
790 atomic_read(&chan->command),dev->name);
795 printk(KERN_INFO "wansock: In execute without MBOX\n");
799 ((mbox_cmd_t*)wp->mbox)->cmd.command = cmd;
800 ((mbox_cmd_t*)wp->mbox)->cmd.lcn = wp->lcn;
801 ((mbox_cmd_t*)wp->mbox)->cmd.result = 0x7F;
804 if (flags & O_NONBLOCK){
806 atomic_set(&chan->command, cmd);
808 atomic_set(&chan->command, cmd);
811 add_wait_queue(sk->sk_sleep,&wait);
812 current->state = TASK_INTERRUPTIBLE;
814 if (((mbox_cmd_t*)wp->mbox)->cmd.result != 0x7F) {
818 if (signal_pending(current)) {
824 current->state = TASK_RUNNING;
825 remove_wait_queue(sk->sk_sleep,&wait);
830 /*============================================================
831 * wanpipe_destroy_timer
833 * Used by wanpipe_release, to delay release of
835 *===========================================================*/
837 static void wanpipe_destroy_timer(unsigned long data)
839 struct sock *sk=(struct sock *)data;
840 wanpipe_opt *wp = wp_sk(sk);
842 if ((!atomic_read(&sk->sk_wmem_alloc) &&
843 !atomic_read(&sk->sk_rmem_alloc)) ||
844 (++wp->force == 5)) {
846 if (atomic_read(&sk->sk_wmem_alloc) ||
847 atomic_read(&sk->sk_rmem_alloc))
848 printk(KERN_INFO "wansock: Warning, Packet Discarded due to sock shutdown!\n");
853 if (atomic_read(&sk->sk_refcnt) != 1) {
854 atomic_set(&sk->sk_refcnt, 1);
855 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :delay.\n",
856 atomic_read(&sk->sk_refcnt));
859 atomic_dec(&wanpipe_socks_nr);
863 sk->sk_timer.expires = jiffies + 5 * HZ;
864 add_timer(&sk->sk_timer);
865 printk(KERN_INFO "wansock: packet sk destroy delayed\n");
868 /*============================================================
869 * wanpipe_unlink_driver
871 * When the socket is released, this function is
872 * used to remove links that bind the sock and the
874 *===========================================================*/
875 static void wanpipe_unlink_driver (struct sock *sk)
877 struct net_device *dev;
878 wanpipe_common_t *chan=NULL;
880 sock_reset_flag(sk, SOCK_ZAPPED);
881 sk->sk_state = WANSOCK_DISCONNECTED;
882 wp_sk(sk)->dev = NULL;
884 dev = dev_get_by_index(sk->sk_bound_dev_if);
886 printk(KERN_INFO "wansock: No dev on release\n");
891 if ((chan = dev->priv) == NULL){
892 printk(KERN_INFO "wansock: No Priv Area on release\n");
896 set_bit(0,&chan->common_critical);
901 clear_bit(0,&chan->common_critical);
907 /*============================================================
908 * wanpipe_link_driver
910 * Upon successful bind(), sock is linked to a driver
911 * by binding in the wanpipe_rcv() bottom half handler
912 * to the driver function pointer, as well as sock and
913 * sock mailbox addresses. This way driver can pass
914 * data up the socket.
915 *===========================================================*/
917 static void wanpipe_link_driver(struct net_device *dev, struct sock *sk)
919 wanpipe_opt *wp = wp_sk(sk);
920 wanpipe_common_t *chan = dev->priv;
923 set_bit(0,&chan->common_critical);
925 chan->func=wanpipe_rcv;
926 chan->mbox = wp->mbox;
927 chan->tx_timer = &wp->tx_timer;
929 sock_set_flag(sk, SOCK_ZAPPED);
930 clear_bit(0,&chan->common_critical);
934 /*============================================================
937 * During sock release, clear a critical bit, which
938 * marks the device a being taken.
939 *===========================================================*/
942 static void release_device(struct net_device *dev)
944 wanpipe_common_t *chan=dev->priv;
945 clear_bit(0,(void*)&chan->rw_bind);
948 /*============================================================
951 * Close a PACKET socket. This is fairly simple. We
952 * immediately go to 'closed' state and remove our
953 * protocol entry in the device list.
954 *===========================================================*/
956 static int wanpipe_release(struct socket *sock)
959 struct sock *sk = sock->sk;
965 check_write_queue(sk);
967 /* Kill the tx timer, if we don't kill it now, the timer
968 * will run after we kill the sock. Timer code will
969 * try to access the sock which has been killed and cause
972 del_timer(&wp->tx_timer);
975 * Unhook packet receive handler.
978 if (wp->num == htons(X25_PROT) &&
979 sk->sk_state != WANSOCK_DISCONNECTED && sock_flag(sk, SOCK_ZAPPED)) {
980 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
981 wanpipe_common_t *chan;
984 atomic_set(&chan->disconnect,1);
985 DBG_PRINTK(KERN_INFO "wansock: Sending Clear Indication %i\n",
991 set_bit(1,&wanpipe_tx_critical);
992 write_lock(&wanpipe_sklist_lock);
993 sk_del_node_init(sk);
994 write_unlock(&wanpipe_sklist_lock);
995 clear_bit(1,&wanpipe_tx_critical);
1003 * Now the socket is dead. No more input will appear.
1006 sk->sk_state_change(sk); /* It is useless. Just for sanity. */
1009 sk->sk_socket = NULL;
1010 sock_set_flag(sk, SOCK_DEAD);
1013 skb_queue_purge(&sk->sk_receive_queue);
1014 skb_queue_purge(&sk->sk_write_queue);
1015 skb_queue_purge(&sk->sk_error_queue);
1017 if (atomic_read(&sk->sk_rmem_alloc) ||
1018 atomic_read(&sk->sk_wmem_alloc)) {
1019 del_timer(&sk->sk_timer);
1020 printk(KERN_INFO "wansock: Killing in Timer R %i , W %i\n",
1021 atomic_read(&sk->sk_rmem_alloc),
1022 atomic_read(&sk->sk_wmem_alloc));
1023 sk->sk_timer.data = (unsigned long)sk;
1024 sk->sk_timer.expires = jiffies + HZ;
1025 sk->sk_timer.function = wanpipe_destroy_timer;
1026 add_timer(&sk->sk_timer);
1033 if (atomic_read(&sk->sk_refcnt) != 1) {
1034 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:release.\n",
1035 atomic_read(&sk->sk_refcnt));
1036 atomic_set(&sk->sk_refcnt, 1);
1039 atomic_dec(&wanpipe_socks_nr);
1043 /*============================================================
1046 * During sock shutdown, if the sock state is
1047 * WANSOCK_CONNECTED and there is transmit data
1048 * pending. Wait until data is released
1049 * before proceeding.
1050 *===========================================================*/
1052 static void check_write_queue(struct sock *sk)
1055 if (sk->sk_state != WANSOCK_CONNECTED)
1058 if (!atomic_read(&sk->sk_wmem_alloc))
1061 printk(KERN_INFO "wansock: MAJOR ERROR, Data lost on sock release !!!\n");
1065 /*============================================================
1068 * This function is called during sock shutdown, to
1069 * release any resources and links that bind the sock
1070 * to the driver. It also changes the state of the
1071 * sock to WANSOCK_DISCONNECTED
1072 *===========================================================*/
1074 static void release_driver(struct sock *sk)
1077 struct sk_buff *skb=NULL;
1078 struct sock *deadsk=NULL;
1080 if (sk->sk_state == WANSOCK_LISTEN ||
1081 sk->sk_state == WANSOCK_BIND_LISTEN) {
1082 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1083 if ((deadsk = get_newsk_from_skb(skb))){
1084 DBG_PRINTK (KERN_INFO "wansock: RELEASE: FOUND DEAD SOCK\n");
1085 sock_set_flag(deadsk, SOCK_DEAD);
1086 start_cleanup_timer(deadsk);
1090 if (sock_flag(sk, SOCK_ZAPPED))
1091 wanpipe_unlink_card(sk);
1093 if (sock_flag(sk, SOCK_ZAPPED))
1094 wanpipe_unlink_driver(sk);
1096 sk->sk_state = WANSOCK_DISCONNECTED;
1097 sk->sk_bound_dev_if = 0;
1098 sock_reset_flag(sk, SOCK_ZAPPED);
1107 /*============================================================
1108 * start_cleanup_timer
1110 * If new incoming call's are pending but the socket
1111 * is being released, start the timer which will
1112 * envoke the kill routines for pending socks.
1113 *===========================================================*/
1116 static void start_cleanup_timer (struct sock *sk)
1118 del_timer(&sk->sk_timer);
1119 sk->sk_timer.data = (unsigned long)sk;
1120 sk->sk_timer.expires = jiffies + HZ;
1121 sk->sk_timer.function = wanpipe_kill_sock_timer;
1122 add_timer(&sk->sk_timer);
1126 /*============================================================
1129 * This is a function which performs actual killing
1130 * of the sock. It releases socket resources,
1131 * and unlinks the sock from the driver.
1132 *===========================================================*/
1134 static void wanpipe_kill_sock_timer (unsigned long data)
1137 struct sock *sk = (struct sock *)data;
1143 /* This function can be called from interrupt. We must use
1144 * appropriate locks */
1146 if (test_bit(1,&wanpipe_tx_critical)){
1147 sk->sk_timer.expires = jiffies + 10;
1148 add_timer(&sk->sk_timer);
1152 write_lock(&wanpipe_sklist_lock);
1153 sk_del_node_init(sk);
1154 write_unlock(&wanpipe_sklist_lock);
1157 if (wp_sk(sk)->num == htons(X25_PROT) &&
1158 sk->sk_state != WANSOCK_DISCONNECTED) {
1159 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
1160 wanpipe_common_t *chan;
1163 atomic_set(&chan->disconnect,1);
1170 sk->sk_socket = NULL;
1173 skb_queue_purge(&sk->sk_receive_queue);
1174 skb_queue_purge(&sk->sk_write_queue);
1175 skb_queue_purge(&sk->sk_error_queue);
1177 if (atomic_read(&sk->sk_rmem_alloc) ||
1178 atomic_read(&sk->sk_wmem_alloc)) {
1179 del_timer(&sk->sk_timer);
1180 printk(KERN_INFO "wansock: Killing SOCK in Timer\n");
1181 sk->sk_timer.data = (unsigned long)sk;
1182 sk->sk_timer.expires = jiffies + HZ;
1183 sk->sk_timer.function = wanpipe_destroy_timer;
1184 add_timer(&sk->sk_timer);
1191 if (atomic_read(&sk->sk_refcnt) != 1) {
1192 atomic_set(&sk->sk_refcnt, 1);
1193 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
1194 atomic_read(&sk->sk_refcnt));
1197 atomic_dec(&wanpipe_socks_nr);
1201 static void wanpipe_kill_sock_accept (struct sock *sk)
1209 /* This function can be called from interrupt. We must use
1210 * appropriate locks */
1212 write_lock(&wanpipe_sklist_lock);
1213 sk_del_node_init(sk);
1214 write_unlock(&wanpipe_sklist_lock);
1216 sk->sk_socket = NULL;
1222 if (atomic_read(&sk->sk_refcnt) != 1) {
1223 atomic_set(&sk->sk_refcnt, 1);
1224 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
1225 atomic_read(&sk->sk_refcnt));
1228 atomic_dec(&wanpipe_socks_nr);
1233 static void wanpipe_kill_sock_irq (struct sock *sk)
1239 sk->sk_socket = NULL;
1244 if (atomic_read(&sk->sk_refcnt) != 1) {
1245 atomic_set(&sk->sk_refcnt, 1);
1246 DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:listen.\n",
1247 atomic_read(&sk->sk_refcnt));
1250 atomic_dec(&wanpipe_socks_nr);
1254 /*============================================================
1257 * Bottom half of the binding system call.
1258 * Once the wanpipe_bind() function checks the
1259 * legality of the call, this function binds the
1260 * sock to the driver.
1261 *===========================================================*/
1263 static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
1266 wanpipe_opt *wp = wp_sk(sk);
1267 wanpipe_common_t *chan=NULL;
1270 if (sock_flag(sk, SOCK_ZAPPED)) {
1272 goto bind_unlock_exit;
1278 release_device(dev);
1280 goto bind_unlock_exit;
1284 if (dev->flags&IFF_UP) {
1286 sk->sk_state = chan->state;
1288 if (wp->num == htons(X25_PROT) &&
1289 sk->sk_state != WANSOCK_DISCONNECTED &&
1290 sk->sk_state != WANSOCK_CONNECTING) {
1291 DBG_PRINTK(KERN_INFO
1292 "wansock: Binding to Device not DISCONNECTED %i\n",
1294 release_device(dev);
1296 goto bind_unlock_exit;
1299 wanpipe_link_driver(dev,sk);
1300 sk->sk_bound_dev_if = dev->ifindex;
1302 /* X25 Specific option */
1303 if (wp->num == htons(X25_PROT))
1304 wp_sk(sk)->svc = chan->svc;
1307 sk->sk_err = ENETDOWN;
1308 sk->sk_error_report(sk);
1309 release_device(dev);
1316 /* FIXME where is this lock */
1321 /*============================================================
1324 * BIND() System call, which is bound to the AF_WANPIPE
1325 * operations structure. It checks for correct wanpipe
1326 * card name, and cross references interface names with
1327 * the card names. Thus, interface name must belong to
1329 *===========================================================*/
1332 static int wanpipe_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1334 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1335 struct sock *sk=sock->sk;
1336 wanpipe_opt *wp = wp_sk(sk);
1337 struct net_device *dev = NULL;
1345 if (addr_len < sizeof(struct wan_sockaddr_ll)){
1346 printk(KERN_INFO "wansock: Address length error\n");
1349 if (sll->sll_family != AF_WANPIPE){
1350 printk(KERN_INFO "wansock: Illegal family name specified.\n");
1354 card = wanpipe_find_card (sll->sll_card);
1356 printk(KERN_INFO "wansock: Wanpipe card not found: %s\n",sll->sll_card);
1359 wp_sk(sk)->card = (void *)card;
1362 if (!strcmp(sll->sll_device,"svc_listen")){
1364 /* Bind a sock to a card structure for listening
1368 /* This is x25 specific area if protocol doesn't
1369 * match, return error */
1370 if (sll->sll_protocol != htons(X25_PROT))
1373 err= wanpipe_link_card (sk);
1377 if (sll->sll_protocol)
1378 wp->num = sll->sll_protocol;
1379 sk->sk_state = WANSOCK_BIND_LISTEN;
1382 }else if (!strcmp(sll->sll_device,"svc_connect")){
1384 /* This is x25 specific area if protocol doesn't
1385 * match, return error */
1386 if (sll->sll_protocol != htons(X25_PROT))
1389 /* Find a free device
1391 dev = wanpipe_find_free_dev(card);
1393 DBG_PRINTK(KERN_INFO "wansock: No free network devices for card %s\n",
1398 /* Bind a socket to a interface name
1399 * This is used by PVC mostly
1401 strlcpy(name,sll->sll_device,sizeof(name));
1402 dev = dev_get_by_name(name);
1404 printk(KERN_INFO "wansock: Failed to get Dev from name: %s,\n",
1411 if (check_dev(dev, card)){
1412 printk(KERN_INFO "wansock: Device %s, doesn't belong to card %s\n",
1413 dev->name, card->devname);
1416 if (get_atomic_device (dev))
1420 return wanpipe_do_bind(sk, dev, sll->sll_protocol ? : wp->num);
1423 /*============================================================
1426 * Sets a bit atomically which indicates that
1427 * the interface is taken. This avoids race conditions.
1428 *===========================================================*/
1431 static inline int get_atomic_device(struct net_device *dev)
1433 wanpipe_common_t *chan = dev->priv;
1434 if (!test_and_set_bit(0,(void *)&chan->rw_bind)){
1440 /*============================================================
1443 * Check that device name belongs to a particular card.
1444 *===========================================================*/
1446 static int check_dev(struct net_device *dev, sdla_t *card)
1448 struct net_device* tmp_dev;
1450 for (tmp_dev = card->wandev.dev; tmp_dev;
1451 tmp_dev = *((struct net_device **)tmp_dev->priv)) {
1452 if (tmp_dev->ifindex == dev->ifindex){
1459 /*============================================================
1460 * wanpipe_find_free_dev
1462 * Find a free network interface. If found set atomic
1463 * bit indicating that the interface is taken.
1465 *===========================================================*/
1467 struct net_device *wanpipe_find_free_dev(sdla_t *card)
1469 struct net_device* dev;
1470 volatile wanpipe_common_t *chan;
1472 if (test_and_set_bit(0,&find_free_critical)){
1473 printk(KERN_INFO "CRITICAL in Find Free\n");
1476 for (dev = card->wandev.dev; dev;
1477 dev = *((struct net_device **)dev->priv)) {
1481 if (chan->usedby == API && chan->svc){
1482 if (!get_atomic_device (dev)){
1483 if (chan->state != WANSOCK_DISCONNECTED){
1484 release_device(dev);
1486 clear_bit(0,&find_free_critical);
1492 clear_bit(0,&find_free_critical);
1496 /*============================================================
1499 * SOCKET() System call. It allocates a sock structure
1500 * and adds the socket to the wanpipe_sk_list.
1501 * Crates AF_WANPIPE socket.
1502 *===========================================================*/
1504 static int wanpipe_create(struct socket *sock, int protocol)
1508 //FIXME: This checks for root user, SECURITY ?
1509 //if (!capable(CAP_NET_RAW))
1512 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
1513 return -ESOCKTNOSUPPORT;
1515 sock->state = SS_UNCONNECTED;
1517 if ((sk = wanpipe_alloc_socket()) == NULL)
1521 sock->ops = &wanpipe_ops;
1522 sock_init_data(sock,sk);
1524 sock_reset_flag(sk, SOCK_ZAPPED);
1525 sk->sk_family = PF_WANPIPE;
1526 wp_sk(sk)->num = protocol;
1527 sk->sk_state = WANSOCK_DISCONNECTED;
1528 sk->sk_ack_backlog = 0;
1529 sk->sk_bound_dev_if = 0;
1531 atomic_inc(&wanpipe_socks_nr);
1533 /* We must disable interrupts because the ISR
1534 * can also change the list */
1535 set_bit(1,&wanpipe_tx_critical);
1536 write_lock(&wanpipe_sklist_lock);
1537 sk_add_node(sk, &wanpipe_sklist);
1538 write_unlock(&wanpipe_sklist_lock);
1539 clear_bit(1,&wanpipe_tx_critical);
1545 /*============================================================
1548 * Pull a packet from our receive queue and hand it
1549 * to the user. If necessary we block.
1550 *===========================================================*/
1552 static int wanpipe_recvmsg(struct kiocb *iocb, struct socket *sock,
1553 struct msghdr *msg, int len, int flags)
1555 struct sock *sk = sock->sk;
1556 struct sk_buff *skb;
1557 int copied, err=-ENOBUFS;
1561 * If the address length field is there to be filled in, we fill
1565 msg->msg_namelen = sizeof(struct wan_sockaddr_ll);
1568 * Call the generic datagram receiver. This handles all sorts
1569 * of horrible races and re-entrancy so we can forget about it
1570 * in the protocol layers.
1572 * Now it will return ENETDOWN, if device have just gone down,
1573 * but then it will block.
1576 if (flags & MSG_OOB){
1577 skb = skb_dequeue(&sk->sk_error_queue);
1579 skb=skb_recv_datagram(sk,flags,1,&err);
1582 * An error occurred so return it. Because skb_recv_datagram()
1583 * handles the blocking we don't see and worry about blocking
1591 * You lose any data beyond the buffer you gave. If it worries a
1592 * user program they can ask the device for its MTU anyway.
1599 msg->msg_flags|=MSG_TRUNC;
1602 wanpipe_wakeup_driver(sk);
1604 /* We can't use skb_copy_datagram here */
1605 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
1609 sock_recv_timestamp(msg, sk, skb);
1612 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1615 * Free or return the buffer as appropriate. Again this
1616 * hides all the races and re-entrancy issues from us.
1618 err = (flags&MSG_TRUNC) ? skb->len : copied;
1621 skb_free_datagram(sk, skb);
1627 /*============================================================
1628 * wanpipe_wakeup_driver
1630 * If socket receive buffer is full and driver cannot
1631 * pass data up the sock, it sets a packet_block flag.
1632 * This function check that flag and if sock receive
1633 * queue has room it kicks the driver BH handler.
1635 * This way, driver doesn't have to poll the sock
1637 *===========================================================*/
1639 static void wanpipe_wakeup_driver(struct sock *sk)
1641 struct net_device *dev = NULL;
1642 wanpipe_common_t *chan=NULL;
1644 dev = dev_get_by_index(sk->sk_bound_dev_if);
1650 if ((chan = dev->priv) == NULL)
1653 if (atomic_read(&chan->receive_block)){
1654 if (atomic_read(&sk->sk_rmem_alloc) <
1655 ((unsigned)sk->sk_rcvbuf * 0.9)) {
1656 printk(KERN_INFO "wansock: Queuing task for wanpipe\n");
1657 atomic_set(&chan->receive_block,0);
1658 wanpipe_queue_tq(&chan->wanpipe_task);
1664 /*============================================================
1667 * I don't know what to do with this yet.
1668 * User can use this function to get sock address
1669 * information. Not very useful for Sangoma's purposes.
1670 *===========================================================*/
1673 static int wanpipe_getname(struct socket *sock, struct sockaddr *uaddr,
1674 int *uaddr_len, int peer)
1676 struct net_device *dev;
1677 struct sock *sk = sock->sk;
1678 struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
1680 sll->sll_family = AF_WANPIPE;
1681 sll->sll_ifindex = sk->sk_bound_dev_if;
1682 sll->sll_protocol = wp_sk(sk)->num;
1683 dev = dev_get_by_index(sk->sk_bound_dev_if);
1685 sll->sll_hatype = dev->type;
1686 sll->sll_halen = dev->addr_len;
1687 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1689 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1692 *uaddr_len = sizeof(*sll);
1699 /*============================================================
1702 * If driver turns off network interface, this function
1703 * will be envoked. Currently I treate it as a
1704 * call disconnect. More thought should go into this
1707 * FIXME: More thought should go into this function.
1709 *===========================================================*/
1711 static int wanpipe_notifier(struct notifier_block *this, unsigned long msg, void *data)
1715 struct net_device *dev = (struct net_device *)data;
1717 sk_for_each(sk, node, &wanpipe_sklist) {
1718 struct wanpipe_opt *po = wp_sk(sk);
1727 case NETDEV_UNREGISTER:
1728 if (dev->ifindex == sk->sk_bound_dev_if) {
1729 printk(KERN_INFO "wansock: Device down %s\n",dev->name);
1730 if (sock_flag(sk, SOCK_ZAPPED)) {
1731 wanpipe_unlink_driver(sk);
1732 sk->sk_err = ENETDOWN;
1733 sk->sk_error_report(sk);
1736 if (msg == NETDEV_UNREGISTER) {
1737 printk(KERN_INFO "wansock: Unregistering Device: %s\n",
1739 wanpipe_unlink_driver(sk);
1740 sk->sk_bound_dev_if = 0;
1745 if (dev->ifindex == sk->sk_bound_dev_if &&
1746 po->num && !sock_flag(sk, SOCK_ZAPPED)) {
1747 printk(KERN_INFO "wansock: Registering Device: %s\n",
1749 wanpipe_link_driver(dev,sk);
1757 /*============================================================
1760 * Execute a user commands, and set socket options.
1762 * FIXME: More thought should go into this function.
1764 *===========================================================*/
1766 static int wanpipe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1768 struct sock *sk = sock->sk;
1774 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1776 case SIOC_WANPIPE_CHECK_TX:
1778 return atomic_read(&sk->sk_wmem_alloc);
1780 case SIOC_WANPIPE_SOCK_STATE:
1782 if (sk->sk_state == WANSOCK_CONNECTED)
1788 case SIOC_WANPIPE_GET_CALL_DATA:
1790 return get_ioctl_cmd (sk,(void*)arg);
1792 case SIOC_WANPIPE_SET_CALL_DATA:
1794 return set_ioctl_cmd (sk,(void*)arg);
1796 case SIOC_WANPIPE_ACCEPT_CALL:
1797 case SIOC_WANPIPE_CLEAR_CALL:
1798 case SIOC_WANPIPE_RESET_CALL:
1800 if ((err=set_ioctl_cmd(sk,(void*)arg)) < 0)
1803 err=wanpipe_exec_cmd(sk,cmd,0);
1804 get_ioctl_cmd(sk,(void*)arg);
1807 case SIOC_WANPIPE_DEBUG:
1809 return wanpipe_debug(sk,(void*)arg);
1811 case SIOC_WANPIPE_SET_NONBLOCK:
1813 if (sk->sk_state != WANSOCK_DISCONNECTED)
1816 sock->file->f_flags |= O_NONBLOCK;
1830 case SIOCGIFBRDADDR:
1831 case SIOCSIFBRDADDR:
1832 case SIOCGIFNETMASK:
1833 case SIOCSIFNETMASK:
1834 case SIOCGIFDSTADDR:
1835 case SIOCSIFDSTADDR:
1837 return inet_dgram_ops.ioctl(sock, cmd, arg);
1841 return -ENOIOCTLCMD;
1846 /*============================================================
1849 * This function will pass up information about all
1852 * FIXME: More thought should go into this function.
1854 *===========================================================*/
1856 static int wanpipe_debug (struct sock *origsk, void *arg)
1859 struct hlist_node *node;
1860 struct net_device *dev = NULL;
1861 wanpipe_common_t *chan=NULL;
1863 wan_debug_t *dbg_data = (wan_debug_t *)arg;
1865 sk_for_each(sk, node, &wanpipe_sklist) {
1866 wanpipe_opt *wp = wp_sk(sk);
1872 if ((err=put_user(1, &dbg_data->debug[cnt].free)))
1874 if ((err = put_user(sk->sk_state,
1875 &dbg_data->debug[cnt].state_sk)))
1877 if ((err = put_user(sk->sk_rcvbuf,
1878 &dbg_data->debug[cnt].rcvbuf)))
1880 if ((err = put_user(atomic_read(&sk->sk_rmem_alloc),
1881 &dbg_data->debug[cnt].rmem)))
1883 if ((err = put_user(atomic_read(&sk->sk_wmem_alloc),
1884 &dbg_data->debug[cnt].wmem)))
1886 if ((err = put_user(sk->sk_sndbuf,
1887 &dbg_data->debug[cnt].sndbuf)))
1889 if ((err=put_user(sk_count, &dbg_data->debug[cnt].sk_count)))
1891 if ((err=put_user(wp->poll_cnt, &dbg_data->debug[cnt].poll_cnt)))
1893 if ((err = put_user(sk->sk_bound_dev_if,
1894 &dbg_data->debug[cnt].bound)))
1897 if (sk->sk_bound_dev_if) {
1898 dev = dev_get_by_index(sk->sk_bound_dev_if);
1905 if ((err=put_user(chan->state, &dbg_data->debug[cnt].d_state)))
1907 if ((err=put_user(chan->svc, &dbg_data->debug[cnt].svc)))
1910 if ((err=put_user(atomic_read(&chan->command),
1911 &dbg_data->debug[cnt].command)))
1916 sdla_t *card = (sdla_t*)wp->card;
1919 if ((err=put_user(atomic_read(&card->u.x.command_busy),
1920 &dbg_data->debug[cnt].cmd_busy)))
1924 if ((err=put_user(wp->lcn,
1925 &dbg_data->debug[cnt].lcn)))
1929 if ((err=put_user(1, &dbg_data->debug[cnt].mbox)))
1934 if ((err=put_user(atomic_read(&chan->receive_block),
1935 &dbg_data->debug[cnt].rblock)))
1938 if (copy_to_user(dbg_data->debug[cnt].name, dev->name, strlen(dev->name)))
1942 if (++cnt == MAX_NUM_DEBUG)
1948 /*============================================================
1951 * Pass up the contents of socket MBOX to the user.
1952 *===========================================================*/
1954 static int get_ioctl_cmd (struct sock *sk, void *arg)
1956 x25api_t *usr_data = (x25api_t *)arg;
1957 mbox_cmd_t *mbox_ptr;
1960 if (usr_data == NULL)
1963 if (!wp_sk(sk)->mbox) {
1967 mbox_ptr = (mbox_cmd_t *)wp_sk(sk)->mbox;
1969 if ((err=put_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
1971 if ((err=put_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
1973 if ((err=put_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
1975 if ((err=put_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
1977 if ((err=put_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
1979 if ((err=put_user(mbox_ptr->cmd.lcn, &usr_data->hdr.lcn)))
1982 if (mbox_ptr->cmd.length > 0){
1983 if (mbox_ptr->cmd.length > X25_MAX_DATA)
1986 if (copy_to_user(usr_data->data, mbox_ptr->data, mbox_ptr->cmd.length)){
1987 printk(KERN_INFO "wansock: Copy failed !!!\n");
1994 /*============================================================
1997 * Before command can be execute, socket MBOX must
1998 * be created, and initialized with user data.
1999 *===========================================================*/
2001 static int set_ioctl_cmd (struct sock *sk, void *arg)
2003 x25api_t *usr_data = (x25api_t *)arg;
2004 mbox_cmd_t *mbox_ptr;
2007 if (!wp_sk(sk)->mbox) {
2009 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2015 if ((mbox_ptr = kmalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL)
2018 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2019 wp_sk(sk)->mbox = mbox_ptr;
2021 wanpipe_link_driver(dev,sk);
2024 mbox_ptr = (mbox_cmd_t*)wp_sk(sk)->mbox;
2025 memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
2027 if (usr_data == NULL){
2030 if ((err=get_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
2032 if ((err=get_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
2034 if ((err=get_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
2036 if ((err=get_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
2038 if ((err=get_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
2041 if (mbox_ptr->cmd.length > 0){
2042 if (mbox_ptr->cmd.length > X25_MAX_DATA)
2045 if (copy_from_user(mbox_ptr->data, usr_data->data, mbox_ptr->cmd.length)){
2046 printk(KERN_INFO "Copy failed\n");
2054 /*======================================================================
2057 * Datagram poll: Again totally generic. This also handles
2058 * sequenced packet sockets providing the socket receive queue
2059 * is only ever holding data ready to receive.
2061 * Note: when you _don't_ use this routine for this protocol,
2062 * and you use a different write policy from sock_writeable()
2063 * then please supply your own write_space callback.
2064 *=====================================================================*/
2066 unsigned int wanpipe_poll(struct file * file, struct socket *sock, poll_table *wait)
2068 struct sock *sk = sock->sk;
2071 ++wp_sk(sk)->poll_cnt;
2073 poll_wait(file, sk->sk_sleep, wait);
2076 /* exceptional events? */
2077 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) {
2081 if (sk->sk_shutdown & RCV_SHUTDOWN)
2085 if (!skb_queue_empty(&sk->sk_receive_queue)) {
2086 mask |= POLLIN | POLLRDNORM;
2089 /* connection hasn't started yet */
2090 if (sk->sk_state == WANSOCK_CONNECTING) {
2094 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2099 /* This check blocks the user process if there is
2100 * a packet already queued in the socket write queue.
2101 * This option is only for X25API protocol, for other
2102 * protocol like chdlc enable streaming mode,
2103 * where multiple packets can be pending in the socket
2106 if (wp_sk(sk)->num == htons(X25_PROT)) {
2107 if (atomic_read(&wp_sk(sk)->packet_sent))
2112 if (sock_writeable(sk)){
2113 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
2115 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
2121 /*======================================================================
2124 * X25API Specific function. Set a socket into LISTENING MODE.
2125 *=====================================================================*/
2128 static int wanpipe_listen(struct socket *sock, int backlog)
2130 struct sock *sk = sock->sk;
2132 /* This is x25 specific area if protocol doesn't
2133 * match, return error */
2134 if (wp_sk(sk)->num != htons(X25_PROT))
2137 if (sk->sk_state == WANSOCK_BIND_LISTEN) {
2139 sk->sk_max_ack_backlog = backlog;
2140 sk->sk_state = WANSOCK_LISTEN;
2143 printk(KERN_INFO "wansock: Listening sock was not binded\n");
2149 /*======================================================================
2152 * Connects the listening socket to the driver
2153 *=====================================================================*/
2155 static int wanpipe_link_card (struct sock *sk)
2157 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2162 if ((card->sk != NULL) || (card->func != NULL)){
2163 printk(KERN_INFO "wansock: Listening queue is already established\n");
2168 card->func=wanpipe_listen_rcv;
2169 sock_set_flag(sk, SOCK_ZAPPED);
2174 /*======================================================================
2177 * X25API Specific function. Disconnect listening socket from
2179 *=====================================================================*/
2181 static void wanpipe_unlink_card (struct sock *sk)
2183 sdla_t *card = (sdla_t*)wp_sk(sk)->card;
2191 /*======================================================================
2194 * Ioctl function calls this function to execute user command.
2195 * Connect() sytem call also calls this function to execute
2196 * place call. This function blocks until command is executed.
2197 *=====================================================================*/
2199 static int wanpipe_exec_cmd(struct sock *sk, int cmd, unsigned int flags)
2202 wanpipe_opt *wp = wp_sk(sk);
2203 mbox_cmd_t *mbox_ptr = (mbox_cmd_t*)wp->mbox;
2206 printk(KERN_INFO "NO MBOX PTR !!!!!\n");
2210 /* This is x25 specific area if protocol doesn't
2211 * match, return error */
2212 if (wp->num != htons(X25_PROT))
2218 case SIOC_WANPIPE_ACCEPT_CALL:
2220 if (sk->sk_state != WANSOCK_CONNECTING) {
2225 err = execute_command(sk,X25_ACCEPT_CALL,0);
2229 /* Update. Mar6 2000.
2230 * Do not set the sock lcn number here, since
2231 * it is done in wanpipe_listen_rcv().
2233 if (sk->sk_state == WANSOCK_CONNECTED) {
2234 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2235 DBG_PRINTK(KERN_INFO "\nwansock: Accept OK %i\n",
2240 DBG_PRINTK (KERN_INFO "\nwansock: Accept Failed %i\n",
2243 err = -ECONNREFUSED;
2247 case SIOC_WANPIPE_CLEAR_CALL:
2249 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2255 /* Check if data buffers are pending for transmission,
2256 * if so, check whether user wants to wait until data
2257 * is transmitted, or clear a call and drop packets */
2259 if (atomic_read(&sk->sk_wmem_alloc) ||
2260 check_driver_busy(sk)) {
2261 mbox_cmd_t *mbox = wp->mbox;
2262 if (mbox->cmd.qdm & 0x80){
2263 mbox->cmd.result = 0x35;
2269 sk->sk_state = WANSOCK_DISCONNECTING;
2271 err = execute_command(sk,X25_CLEAR_CALL,0);
2275 err = -ECONNREFUSED;
2276 if (sk->sk_state == WANSOCK_DISCONNECTED) {
2277 DBG_PRINTK(KERN_INFO "\nwansock: CLEAR OK %i\n",
2284 case SIOC_WANPIPE_RESET_CALL:
2286 if (sk->sk_state != WANSOCK_CONNECTED) {
2292 /* Check if data buffers are pending for transmission,
2293 * if so, check whether user wants to wait until data
2294 * is transmitted, or reset a call and drop packets */
2296 if (atomic_read(&sk->sk_wmem_alloc) ||
2297 check_driver_busy(sk)) {
2298 mbox_cmd_t *mbox = wp->mbox;
2299 if (mbox->cmd.qdm & 0x80){
2300 mbox->cmd.result = 0x35;
2307 err = execute_command(sk, X25_RESET,0);
2311 err = mbox_ptr->cmd.result;
2315 case X25_PLACE_CALL:
2317 err=execute_command(sk,X25_PLACE_CALL,flags);
2321 if (sk->sk_state == WANSOCK_CONNECTED) {
2323 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2325 DBG_PRINTK(KERN_INFO "\nwansock: PLACE CALL OK %i\n",
2329 } else if (sk->sk_state == WANSOCK_CONNECTING &&
2330 (flags & O_NONBLOCK)) {
2331 wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
2332 DBG_PRINTK(KERN_INFO "\nwansock: Place Call OK: Waiting %i\n",
2338 DBG_PRINTK(KERN_INFO "\nwansock: Place call Failed\n");
2339 err = -ECONNREFUSED;
2351 static int check_driver_busy (struct sock *sk)
2353 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
2354 wanpipe_common_t *chan;
2361 if ((chan=dev->priv) == NULL)
2364 return atomic_read(&chan->driver_busy);
2368 /*======================================================================
2371 * ACCEPT() System call. X25API Specific function.
2372 * For each incoming call, create a new socket and
2373 * return it to the user.
2374 *=====================================================================*/
2376 static int wanpipe_accept(struct socket *sock, struct socket *newsock, int flags)
2380 struct sk_buff *skb;
2381 DECLARE_WAITQUEUE(wait, current);
2384 if (newsock->sk != NULL){
2385 wanpipe_kill_sock_accept(newsock->sk);
2389 if ((sk = sock->sk) == NULL)
2392 if (sk->sk_type != SOCK_RAW)
2395 if (sk->sk_state != WANSOCK_LISTEN)
2398 if (wp_sk(sk)->num != htons(X25_PROT))
2401 add_wait_queue(sk->sk_sleep,&wait);
2402 current->state = TASK_INTERRUPTIBLE;
2404 skb = skb_dequeue(&sk->sk_receive_queue);
2409 if (signal_pending(current)) {
2415 current->state = TASK_RUNNING;
2416 remove_wait_queue(sk->sk_sleep,&wait);
2421 newsk = get_newsk_from_skb(skb);
2426 set_bit(1,&wanpipe_tx_critical);
2427 write_lock(&wanpipe_sklist_lock);
2428 sk_add_node(newsk, &wanpipe_sklist);
2429 write_unlock(&wanpipe_sklist_lock);
2430 clear_bit(1,&wanpipe_tx_critical);
2432 newsk->sk_socket = newsock;
2433 newsk->sk_sleep = &newsock->wait;
2435 /* Now attach up the new socket */
2436 sk->sk_ack_backlog--;
2437 newsock->sk = newsk;
2441 DBG_PRINTK(KERN_INFO "\nwansock: ACCEPT Got LCN %i\n",
2446 /*======================================================================
2447 * get_newsk_from_skb
2449 * Accept() uses this function to get the address of the new
2451 *=====================================================================*/
2453 struct sock * get_newsk_from_skb (struct sk_buff *skb)
2455 struct net_device *dev = skb->dev;
2456 wanpipe_common_t *chan;
2462 if ((chan = dev->priv) == NULL){
2469 return (struct sock *)chan->sk;
2472 /*======================================================================
2475 * CONNECT() System Call. X25API specific function
2476 * Check the state of the sock, and execute PLACE_CALL command.
2477 * Connect can ether block or return without waiting for connection,
2478 * if specified by user.
2479 *=====================================================================*/
2481 static int wanpipe_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
2483 struct sock *sk = sock->sk;
2484 struct wan_sockaddr_ll *addr = (struct wan_sockaddr_ll*)uaddr;
2485 struct net_device *dev;
2488 if (wp_sk(sk)->num != htons(X25_PROT))
2491 if (sk->sk_state == WANSOCK_CONNECTED)
2492 return -EISCONN; /* No reconnect on a seqpacket socket */
2494 if (sk->sk_state != WAN_DISCONNECTED) {
2495 printk(KERN_INFO "wansock: Trying to connect on channel NON DISCONNECT\n");
2496 return -ECONNREFUSED;
2499 sk->sk_state = WANSOCK_DISCONNECTED;
2500 sock->state = SS_UNCONNECTED;
2502 if (addr_len != sizeof(struct wan_sockaddr_ll))
2505 if (addr->sll_family != AF_WANPIPE)
2508 if ((dev = dev_get_by_index(sk->sk_bound_dev_if)) == NULL)
2509 return -ENETUNREACH;
2513 if (!sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
2516 sock->state = SS_CONNECTING;
2517 sk->sk_state = WANSOCK_CONNECTING;
2519 if (!wp_sk(sk)->mbox) {
2520 if (wp_sk (sk)->svc)
2524 if ((err=set_ioctl_cmd(sk,NULL)) < 0)
2529 if ((err=wanpipe_exec_cmd(sk, X25_PLACE_CALL,flags)) != 0){
2530 sock->state = SS_UNCONNECTED;
2531 sk->sk_state = WANSOCK_CONNECTED;
2535 if (sk->sk_state != WANSOCK_CONNECTED && (flags & O_NONBLOCK)) {
2539 if (sk->sk_state != WANSOCK_CONNECTED) {
2540 sock->state = SS_UNCONNECTED;
2541 return -ECONNREFUSED;
2544 sock->state = SS_CONNECTED;
2548 const struct proto_ops wanpipe_ops = {
2549 .family = PF_WANPIPE,
2550 .owner = THIS_MODULE,
2551 .release = wanpipe_release,
2552 .bind = wanpipe_bind,
2553 .connect = wanpipe_connect,
2554 .socketpair = sock_no_socketpair,
2555 .accept = wanpipe_accept,
2556 .getname = wanpipe_getname,
2557 .poll = wanpipe_poll,
2558 .ioctl = wanpipe_ioctl,
2559 .listen = wanpipe_listen,
2560 .shutdown = sock_no_shutdown,
2561 .setsockopt = sock_no_setsockopt,
2562 .getsockopt = sock_no_getsockopt,
2563 .sendmsg = wanpipe_sendmsg,
2564 .recvmsg = wanpipe_recvmsg
2567 static struct net_proto_family wanpipe_family_ops = {
2568 .family = PF_WANPIPE,
2569 .create = wanpipe_create,
2570 .owner = THIS_MODULE,
2573 struct notifier_block wanpipe_netdev_notifier = {
2574 .notifier_call = wanpipe_notifier,
2579 void cleanup_module(void)
2581 printk(KERN_INFO "wansock: Cleaning up \n");
2582 unregister_netdevice_notifier(&wanpipe_netdev_notifier);
2583 sock_unregister(PF_WANPIPE);
2584 proto_unregister(&wanpipe_proto);
2587 int init_module(void)
2591 printk(KERN_INFO "wansock: Registering Socket \n");
2593 rc = proto_register(&wanpipe_proto, 0);
2597 sock_register(&wanpipe_family_ops);
2598 register_netdevice_notifier(&wanpipe_netdev_notifier);
2603 MODULE_LICENSE("GPL");
2604 MODULE_ALIAS_NETPROTO(PF_WANPIPE);